Home | History | Annotate | Line # | Download | only in bfd
elf.c revision 1.1.1.13
      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;
   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   if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
   4830     {
   4831       /* If we have a loadable interpreter section, we need a
   4832 	 PT_INTERP segment.  In this case, assume we also need a
   4833 	 PT_PHDR segment, although that may not be true for all
   4834 	 targets.  */
   4835       segs += 2;
   4836     }
   4837 
   4838   if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
   4839     {
   4840       /* We need a PT_DYNAMIC segment.  */
   4841       ++segs;
   4842     }
   4843 
   4844   if (info != NULL && info->relro)
   4845     {
   4846       /* We need a PT_GNU_RELRO segment.  */
   4847       ++segs;
   4848     }
   4849 
   4850   if (elf_eh_frame_hdr (info))
   4851     {
   4852       /* We need a PT_GNU_EH_FRAME segment.  */
   4853       ++segs;
   4854     }
   4855 
   4856   if (elf_stack_flags (abfd))
   4857     {
   4858       /* We need a PT_GNU_STACK segment.  */
   4859       ++segs;
   4860     }
   4861 
   4862   if (elf_sframe (abfd))
   4863     {
   4864       /* We need a PT_GNU_SFRAME segment.  */
   4865       ++segs;
   4866     }
   4867 
   4868   s = bfd_get_section_by_name (abfd,
   4869 			       NOTE_GNU_PROPERTY_SECTION_NAME);
   4870   if (s != NULL && s->size != 0)
   4871     {
   4872       /* We need a PT_GNU_PROPERTY segment.  */
   4873       ++segs;
   4874     }
   4875 
   4876   for (s = abfd->sections; s != NULL; s = s->next)
   4877     {
   4878       if ((s->flags & SEC_LOAD) != 0
   4879 	  && elf_section_type (s) == SHT_NOTE)
   4880 	{
   4881 	  unsigned int alignment_power;
   4882 	  /* We need a PT_NOTE segment.  */
   4883 	  ++segs;
   4884 	  /* Try to create just one PT_NOTE segment for all adjacent
   4885 	     loadable SHT_NOTE sections.  gABI requires that within a
   4886 	     PT_NOTE segment (and also inside of each SHT_NOTE section)
   4887 	     each note should have the same alignment.  So we check
   4888 	     whether the sections are correctly aligned.  */
   4889 	  alignment_power = s->alignment_power;
   4890 	  while (s->next != NULL
   4891 		 && s->next->alignment_power == alignment_power
   4892 		 && (s->next->flags & SEC_LOAD) != 0
   4893 		 && elf_section_type (s->next) == SHT_NOTE)
   4894 	    s = s->next;
   4895 	}
   4896     }
   4897 
   4898   for (s = abfd->sections; s != NULL; s = s->next)
   4899     {
   4900       if (s->flags & SEC_THREAD_LOCAL)
   4901 	{
   4902 	  /* We need a PT_TLS segment.  */
   4903 	  ++segs;
   4904 	  break;
   4905 	}
   4906     }
   4907 
   4908   bed = get_elf_backend_data (abfd);
   4909 
   4910   if ((abfd->flags & D_PAGED) != 0
   4911       && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
   4912     {
   4913       /* Add a PT_GNU_MBIND segment for each mbind section.  */
   4914       bfd_vma commonpagesize;
   4915       unsigned int page_align_power;
   4916 
   4917       if (info != NULL)
   4918 	commonpagesize = info->commonpagesize;
   4919       else
   4920 	commonpagesize = bed->commonpagesize;
   4921       page_align_power = bfd_log2 (commonpagesize);
   4922       for (s = abfd->sections; s != NULL; s = s->next)
   4923 	if (elf_section_flags (s) & SHF_GNU_MBIND)
   4924 	  {
   4925 	    if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
   4926 	      {
   4927 		_bfd_error_handler
   4928 		  /* xgettext:c-format */
   4929 		  (_("%pB: GNU_MBIND section `%pA' has invalid "
   4930 		     "sh_info field: %d"),
   4931 		   abfd, s, elf_section_data (s)->this_hdr.sh_info);
   4932 		continue;
   4933 	      }
   4934 	    /* Align mbind section to page size.  */
   4935 	    if (s->alignment_power < page_align_power)
   4936 	      s->alignment_power = page_align_power;
   4937 	    segs ++;
   4938 	  }
   4939     }
   4940 
   4941   /* Let the backend count up any program headers it might need.  */
   4942   if (bed->elf_backend_additional_program_headers)
   4943     {
   4944       int a;
   4945 
   4946       a = (*bed->elf_backend_additional_program_headers) (abfd, info);
   4947       if (a == -1)
   4948 	abort ();
   4949       segs += a;
   4950     }
   4951 
   4952   return segs * bed->s->sizeof_phdr;
   4953 }
   4954 
   4955 /* Find the segment that contains the output_section of section.  */
   4956 
   4957 Elf_Internal_Phdr *
   4958 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
   4959 {
   4960   struct elf_segment_map *m;
   4961   Elf_Internal_Phdr *p;
   4962 
   4963   for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
   4964        m != NULL;
   4965        m = m->next, p++)
   4966     {
   4967       int i;
   4968 
   4969       for (i = m->count - 1; i >= 0; i--)
   4970 	if (m->sections[i] == section)
   4971 	  return p;
   4972     }
   4973 
   4974   return NULL;
   4975 }
   4976 
   4977 /* Create a mapping from a set of sections to a program segment.  */
   4978 
   4979 static struct elf_segment_map *
   4980 make_mapping (bfd *abfd,
   4981 	      asection **sections,
   4982 	      unsigned int from,
   4983 	      unsigned int to,
   4984 	      bool phdr)
   4985 {
   4986   struct elf_segment_map *m;
   4987   unsigned int i;
   4988   asection **hdrpp;
   4989   size_t amt;
   4990 
   4991   amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   4992   amt += (to - from) * sizeof (asection *);
   4993   m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4994   if (m == NULL)
   4995     return NULL;
   4996   m->next = NULL;
   4997   m->p_type = PT_LOAD;
   4998   for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
   4999     m->sections[i - from] = *hdrpp;
   5000   m->count = to - from;
   5001 
   5002   if (from == 0 && phdr)
   5003     {
   5004       /* Include the headers in the first PT_LOAD segment.  */
   5005       m->includes_filehdr = 1;
   5006       m->includes_phdrs = 1;
   5007     }
   5008 
   5009   return m;
   5010 }
   5011 
   5012 /* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
   5013    on failure.  */
   5014 
   5015 struct elf_segment_map *
   5016 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
   5017 {
   5018   struct elf_segment_map *m;
   5019 
   5020   m = (struct elf_segment_map *) bfd_zalloc (abfd,
   5021 					     sizeof (struct elf_segment_map));
   5022   if (m == NULL)
   5023     return NULL;
   5024   m->next = NULL;
   5025   m->p_type = PT_DYNAMIC;
   5026   m->count = 1;
   5027   m->sections[0] = dynsec;
   5028 
   5029   return m;
   5030 }
   5031 
   5032 /* Possibly add or remove segments from the segment map.  */
   5033 
   5034 static bool
   5035 elf_modify_segment_map (bfd *abfd,
   5036 			struct bfd_link_info *info,
   5037 			bool remove_empty_load)
   5038 {
   5039   struct elf_segment_map **m;
   5040   elf_backend_data *bed;
   5041 
   5042   /* The placement algorithm assumes that non allocated sections are
   5043      not in PT_LOAD segments.  We ensure this here by removing such
   5044      sections from the segment map.  We also remove excluded
   5045      sections.  Finally, any PT_LOAD segment without sections is
   5046      removed.  */
   5047   m = &elf_seg_map (abfd);
   5048   while (*m)
   5049     {
   5050       unsigned int i, new_count;
   5051 
   5052       for (new_count = 0, i = 0; i < (*m)->count; i++)
   5053 	{
   5054 	  if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
   5055 	      && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
   5056 		  || (*m)->p_type != PT_LOAD))
   5057 	    {
   5058 	      (*m)->sections[new_count] = (*m)->sections[i];
   5059 	      new_count++;
   5060 	    }
   5061 	}
   5062       (*m)->count = new_count;
   5063 
   5064       if (remove_empty_load
   5065 	  && (*m)->p_type == PT_LOAD
   5066 	  && (*m)->count == 0
   5067 	  && !(*m)->includes_phdrs)
   5068 	*m = (*m)->next;
   5069       else
   5070 	m = &(*m)->next;
   5071     }
   5072 
   5073   bed = get_elf_backend_data (abfd);
   5074   if (bed->elf_backend_modify_segment_map != NULL)
   5075     {
   5076       if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
   5077 	return false;
   5078     }
   5079 
   5080   return true;
   5081 }
   5082 
   5083 #define IS_TBSS(s) \
   5084   ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
   5085 
   5086 /* Set up a mapping from BFD sections to program segments.  Update
   5087    NEED_LAYOUT if the section layout is changed.  */
   5088 
   5089 bool
   5090 bfd_elf_map_sections_to_segments (bfd *abfd,
   5091 				  struct bfd_link_info *info,
   5092 				  bool *need_layout)
   5093 {
   5094   unsigned int count;
   5095   struct elf_segment_map *m;
   5096   asection **sections = NULL;
   5097   elf_backend_data *bed = get_elf_backend_data (abfd);
   5098   bool no_user_phdrs;
   5099 
   5100   no_user_phdrs = elf_seg_map (abfd) == NULL;
   5101 
   5102   if (info != NULL)
   5103     {
   5104       info->user_phdrs = !no_user_phdrs;
   5105 
   5106       /* Size the relative relocations if DT_RELR is enabled.  */
   5107       if (info->enable_dt_relr
   5108 	  && need_layout != NULL
   5109 	  && bed->size_relative_relocs
   5110 	  && !bed->size_relative_relocs (info, need_layout))
   5111 	info->callbacks->fatal
   5112 	  (_("%P: failed to size relative relocations\n"));
   5113     }
   5114 
   5115   if (no_user_phdrs && bfd_count_sections (abfd) != 0)
   5116     {
   5117       asection *s;
   5118       unsigned int i;
   5119       struct elf_segment_map *mfirst;
   5120       struct elf_segment_map **pm;
   5121       asection *last_hdr;
   5122       bfd_vma last_size;
   5123       unsigned int hdr_index;
   5124       bfd_vma maxpagesize;
   5125       asection **hdrpp;
   5126       bool phdr_in_segment;
   5127       bool writable;
   5128       bool executable;
   5129       unsigned int tls_count = 0;
   5130       asection *first_tls = NULL;
   5131       asection *first_mbind = NULL;
   5132       asection *dynsec, *eh_frame_hdr;
   5133       asection *sframe;
   5134       size_t amt;
   5135       bfd_vma addr_mask, wrap_to = 0;  /* Bytes.  */
   5136       bfd_size_type phdr_size;  /* Octets/bytes.  */
   5137       unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   5138 
   5139       /* Select the allocated sections, and sort them.  */
   5140 
   5141       amt = bfd_count_sections (abfd) * sizeof (asection *);
   5142       sections = (asection **) bfd_malloc (amt);
   5143       if (sections == NULL)
   5144 	goto error_return;
   5145 
   5146       /* Calculate top address, avoiding undefined behaviour of shift
   5147 	 left operator when shift count is equal to size of type
   5148 	 being shifted.  */
   5149       addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
   5150       addr_mask = (addr_mask << 1) + 1;
   5151 
   5152       i = 0;
   5153       for (s = abfd->sections; s != NULL; s = s->next)
   5154 	{
   5155 	  if ((s->flags & SEC_ALLOC) != 0)
   5156 	    {
   5157 	      /* target_index is unused until bfd_elf_final_link
   5158 		 starts output of section symbols.  Use it to make
   5159 		 qsort stable.  */
   5160 	      s->target_index = i;
   5161 	      sections[i] = s;
   5162 	      ++i;
   5163 	      /* A wrapping section potentially clashes with header.  */
   5164 	      if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
   5165 		wrap_to = (s->lma + s->size / opb) & addr_mask;
   5166 	    }
   5167 	}
   5168       BFD_ASSERT (i <= bfd_count_sections (abfd));
   5169       count = i;
   5170 
   5171       qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
   5172 
   5173       phdr_size = elf_program_header_size (abfd);
   5174       if (phdr_size == (bfd_size_type) -1)
   5175 	phdr_size = get_program_header_size (abfd, info);
   5176       phdr_size += bed->s->sizeof_ehdr;
   5177       /* phdr_size is compared to LMA values which are in bytes.  */
   5178       phdr_size /= opb;
   5179       if (info != NULL)
   5180 	maxpagesize = info->maxpagesize;
   5181       else
   5182 	maxpagesize = bed->maxpagesize;
   5183       if (maxpagesize == 0)
   5184 	maxpagesize = 1;
   5185       phdr_in_segment = info != NULL && info->load_phdrs;
   5186       if (count != 0
   5187 	  && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
   5188 	      >= (phdr_size & (maxpagesize - 1))))
   5189 	/* For compatibility with old scripts that may not be using
   5190 	   SIZEOF_HEADERS, add headers when it looks like space has
   5191 	   been left for them.  */
   5192 	phdr_in_segment = true;
   5193 
   5194       /* Build the mapping.  */
   5195       mfirst = NULL;
   5196       pm = &mfirst;
   5197 
   5198       /* If we have a .interp section, then create a PT_PHDR segment for
   5199 	 the program headers and a PT_INTERP segment for the .interp
   5200 	 section.  */
   5201       s = bfd_get_section_by_name (abfd, ".interp");
   5202       if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
   5203 	{
   5204 	  amt = sizeof (struct elf_segment_map);
   5205 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5206 	  if (m == NULL)
   5207 	    goto error_return;
   5208 	  m->next = NULL;
   5209 	  m->p_type = PT_PHDR;
   5210 	  m->p_flags = PF_R;
   5211 	  m->p_flags_valid = 1;
   5212 	  m->includes_phdrs = 1;
   5213 	  phdr_in_segment = true;
   5214 	  *pm = m;
   5215 	  pm = &m->next;
   5216 
   5217 	  amt = sizeof (struct elf_segment_map);
   5218 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5219 	  if (m == NULL)
   5220 	    goto error_return;
   5221 	  m->next = NULL;
   5222 	  m->p_type = PT_INTERP;
   5223 	  m->count = 1;
   5224 	  m->sections[0] = s;
   5225 
   5226 	  *pm = m;
   5227 	  pm = &m->next;
   5228 	}
   5229 
   5230       /* Look through the sections.  We put sections in the same program
   5231 	 segment when the start of the second section can be placed within
   5232 	 a few bytes of the end of the first section.  */
   5233       last_hdr = NULL;
   5234       last_size = 0;
   5235       hdr_index = 0;
   5236       writable = false;
   5237       executable = false;
   5238       dynsec = bfd_get_section_by_name (abfd, ".dynamic");
   5239       if (dynsec != NULL
   5240 	  && (dynsec->flags & SEC_LOAD) == 0)
   5241 	dynsec = NULL;
   5242 
   5243       if ((abfd->flags & D_PAGED) == 0)
   5244 	phdr_in_segment = false;
   5245 
   5246       /* Deal with -Ttext or something similar such that the first section
   5247 	 is not adjacent to the program headers.  This is an
   5248 	 approximation, since at this point we don't know exactly how many
   5249 	 program headers we will need.  */
   5250       if (phdr_in_segment && count > 0)
   5251 	{
   5252 	  bfd_vma phdr_lma;  /* Bytes.  */
   5253 	  bool separate_phdr = false;
   5254 
   5255 	  phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
   5256 	  if (info != NULL
   5257 	      && info->separate_code
   5258 	      && (sections[0]->flags & SEC_CODE) != 0)
   5259 	    {
   5260 	      /* If data sections should be separate from code and
   5261 		 thus not executable, and the first section is
   5262 		 executable then put the file and program headers in
   5263 		 their own PT_LOAD.  */
   5264 	      if (!info->one_rosegment)
   5265 		separate_phdr = true;
   5266 
   5267 	      if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
   5268 		   == (sections[0]->lma & addr_mask & -maxpagesize)))
   5269 		{
   5270 		  /* The file and program headers are currently on the
   5271 		     same page as the first section.  Put them on the
   5272 		     previous page if we can.  */
   5273 		  if (phdr_lma >= maxpagesize)
   5274 		    phdr_lma -= maxpagesize;
   5275 		  else
   5276 		    separate_phdr = false;
   5277 		}
   5278 	    }
   5279 	  if ((sections[0]->lma & addr_mask) < phdr_lma
   5280 	      || (sections[0]->lma & addr_mask) < phdr_size)
   5281 	    /* If file and program headers would be placed at the end
   5282 	       of memory then it's probably better to omit them.  */
   5283 	    phdr_in_segment = false;
   5284 	  else if (phdr_lma < wrap_to)
   5285 	    /* If a section wraps around to where we'll be placing
   5286 	       file and program headers, then the headers will be
   5287 	       overwritten.  */
   5288 	    phdr_in_segment = false;
   5289 	  else if (separate_phdr)
   5290 	    {
   5291 	      m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
   5292 	      if (m == NULL)
   5293 		goto error_return;
   5294 	      m->p_paddr = phdr_lma * opb;
   5295 	      m->p_vaddr_offset
   5296 		= (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
   5297 	      m->p_paddr_valid = 1;
   5298 	      *pm = m;
   5299 	      pm = &m->next;
   5300 	      phdr_in_segment = false;
   5301 	    }
   5302 	}
   5303 
   5304       for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
   5305 	{
   5306 	  asection *hdr;
   5307 	  bool new_segment;
   5308 
   5309 	  hdr = *hdrpp;
   5310 
   5311 	  /* See if this section and the last one will fit in the same
   5312 	     segment.  */
   5313 
   5314 	  if (last_hdr == NULL)
   5315 	    {
   5316 	      /* If we don't have a segment yet, then we don't need a new
   5317 		 one (we build the last one after this loop).  */
   5318 	      new_segment = false;
   5319 	    }
   5320 	  else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
   5321 	    {
   5322 	      /* If this section has a different relation between the
   5323 		 virtual address and the load address, then we need a new
   5324 		 segment.  */
   5325 	      new_segment = true;
   5326 	    }
   5327 	  else if (hdr->lma < last_hdr->lma + last_size
   5328 		   || last_hdr->lma + last_size < last_hdr->lma)
   5329 	    {
   5330 	      /* If this section has a load address that makes it overlap
   5331 		 the previous section, then we need a new segment.  */
   5332 	      new_segment = true;
   5333 	    }
   5334 	  else if ((abfd->flags & D_PAGED) != 0
   5335 		   && (((last_hdr->lma + last_size - 1) & -maxpagesize)
   5336 		       == (hdr->lma & -maxpagesize)))
   5337 	    {
   5338 	      /* If we are demand paged then we can't map two disk
   5339 		 pages onto the same memory page.  */
   5340 	      new_segment = false;
   5341 	    }
   5342 	  /* In the next test we have to be careful when last_hdr->lma is close
   5343 	     to the end of the address space.  If the aligned address wraps
   5344 	     around to the start of the address space, then there are no more
   5345 	     pages left in memory and it is OK to assume that the current
   5346 	     section can be included in the current segment.  */
   5347 	  else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
   5348 		    + maxpagesize > last_hdr->lma)
   5349 		   && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
   5350 		       + maxpagesize <= hdr->lma))
   5351 	    {
   5352 	      /* If putting this section in this segment would force us to
   5353 		 skip a page in the segment, then we need a new segment.  */
   5354 	      new_segment = true;
   5355 	    }
   5356 	  else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
   5357 		   && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
   5358 	    {
   5359 	      /* We don't want to put a loaded section after a
   5360 		 nonloaded (ie. bss style) section in the same segment
   5361 		 as that will force the non-loaded section to be loaded.
   5362 		 Consider .tbss sections as loaded for this purpose.  */
   5363 	      new_segment = true;
   5364 	    }
   5365 	  else if ((abfd->flags & D_PAGED) == 0)
   5366 	    {
   5367 	      /* If the file is not demand paged, which means that we
   5368 		 don't require the sections to be correctly aligned in the
   5369 		 file, then there is no other reason for a new segment.  */
   5370 	      new_segment = false;
   5371 	    }
   5372 	  else if (info != NULL
   5373 		   && info->separate_code
   5374 		   && executable != ((hdr->flags & SEC_CODE) != 0))
   5375 	    {
   5376 	      new_segment = true;
   5377 	    }
   5378 	  else if (! writable
   5379 		   && (hdr->flags & SEC_READONLY) == 0)
   5380 	    {
   5381 	      /* We don't want to put a writable section in a read only
   5382 		 segment.  */
   5383 	      new_segment = true;
   5384 	    }
   5385 	  else
   5386 	    {
   5387 	      /* Otherwise, we can use the same segment.  */
   5388 	      new_segment = false;
   5389 	    }
   5390 
   5391 	  /* Allow interested parties a chance to override our decision.  */
   5392 	  if (last_hdr != NULL
   5393 	      && info != NULL
   5394 	      && info->callbacks->override_segment_assignment != NULL)
   5395 	    new_segment
   5396 	      = info->callbacks->override_segment_assignment (info, abfd, hdr,
   5397 							      last_hdr,
   5398 							      new_segment);
   5399 
   5400 	  if (! new_segment)
   5401 	    {
   5402 	      if ((hdr->flags & SEC_READONLY) == 0)
   5403 		writable = true;
   5404 	      if ((hdr->flags & SEC_CODE) != 0)
   5405 		executable = true;
   5406 	      last_hdr = hdr;
   5407 	      /* .tbss sections effectively have zero size.  */
   5408 	      last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
   5409 	      continue;
   5410 	    }
   5411 
   5412 	  /* We need a new program segment.  We must create a new program
   5413 	     header holding all the sections from hdr_index until hdr.  */
   5414 
   5415 	  m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
   5416 	  if (m == NULL)
   5417 	    goto error_return;
   5418 
   5419 	  *pm = m;
   5420 	  pm = &m->next;
   5421 
   5422 	  if ((hdr->flags & SEC_READONLY) == 0)
   5423 	    writable = true;
   5424 	  else
   5425 	    writable = false;
   5426 
   5427 	  if ((hdr->flags & SEC_CODE) == 0)
   5428 	    executable = false;
   5429 	  else
   5430 	    executable = true;
   5431 
   5432 	  last_hdr = hdr;
   5433 	  /* .tbss sections effectively have zero size.  */
   5434 	  last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
   5435 	  hdr_index = i;
   5436 	  phdr_in_segment = false;
   5437 	}
   5438 
   5439       /* Create a final PT_LOAD program segment, but not if it's just
   5440 	 for .tbss.  */
   5441       if (last_hdr != NULL
   5442 	  && (i - hdr_index != 1
   5443 	      || !IS_TBSS (last_hdr)))
   5444 	{
   5445 	  m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
   5446 	  if (m == NULL)
   5447 	    goto error_return;
   5448 
   5449 	  *pm = m;
   5450 	  pm = &m->next;
   5451 	}
   5452 
   5453       /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
   5454       if (dynsec != NULL)
   5455 	{
   5456 	  m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
   5457 	  if (m == NULL)
   5458 	    goto error_return;
   5459 	  *pm = m;
   5460 	  pm = &m->next;
   5461 	}
   5462 
   5463       /* For each batch of consecutive loadable SHT_NOTE  sections,
   5464 	 add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
   5465 	 because if we link together nonloadable .note sections and
   5466 	 loadable .note sections, we will generate two .note sections
   5467 	 in the output file.  */
   5468       for (s = abfd->sections; s != NULL; s = s->next)
   5469 	{
   5470 	  if ((s->flags & SEC_LOAD) != 0
   5471 	      && elf_section_type (s) == SHT_NOTE)
   5472 	    {
   5473 	      asection *s2;
   5474 	      unsigned int alignment_power = s->alignment_power;
   5475 
   5476 	      count = 1;
   5477 	      for (s2 = s; s2->next != NULL; s2 = s2->next)
   5478 		{
   5479 		  if (s2->next->alignment_power == alignment_power
   5480 		      && (s2->next->flags & SEC_LOAD) != 0
   5481 		      && elf_section_type (s2->next) == SHT_NOTE
   5482 		      && align_power (s2->lma + s2->size / opb,
   5483 				      alignment_power)
   5484 		      == s2->next->lma)
   5485 		    count++;
   5486 		  else
   5487 		    break;
   5488 		}
   5489 	      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   5490 	      amt += count * sizeof (asection *);
   5491 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5492 	      if (m == NULL)
   5493 		goto error_return;
   5494 	      m->next = NULL;
   5495 	      m->p_type = PT_NOTE;
   5496 	      m->count = count;
   5497 	      while (count > 1)
   5498 		{
   5499 		  m->sections[m->count - count--] = s;
   5500 		  BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   5501 		  s = s->next;
   5502 		}
   5503 	      m->sections[m->count - 1] = s;
   5504 	      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   5505 	      *pm = m;
   5506 	      pm = &m->next;
   5507 	    }
   5508 	  if (s->flags & SEC_THREAD_LOCAL)
   5509 	    {
   5510 	      if (! tls_count)
   5511 		first_tls = s;
   5512 	      tls_count++;
   5513 	    }
   5514 	  if (first_mbind == NULL
   5515 	      && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
   5516 	    first_mbind = s;
   5517 	}
   5518 
   5519       /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
   5520       if (tls_count > 0)
   5521 	{
   5522 	  amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   5523 	  amt += tls_count * sizeof (asection *);
   5524 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5525 	  if (m == NULL)
   5526 	    goto error_return;
   5527 	  m->next = NULL;
   5528 	  m->p_type = PT_TLS;
   5529 	  m->count = tls_count;
   5530 	  /* Mandated PF_R.  */
   5531 	  m->p_flags = PF_R;
   5532 	  m->p_flags_valid = 1;
   5533 	  s = first_tls;
   5534 	  for (i = 0; i < tls_count; ++i)
   5535 	    {
   5536 	      if ((s->flags & SEC_THREAD_LOCAL) == 0)
   5537 		{
   5538 		  _bfd_error_handler
   5539 		    (_("%pB: TLS sections are not adjacent:"), abfd);
   5540 		  s = first_tls;
   5541 		  i = 0;
   5542 		  while (i < tls_count)
   5543 		    {
   5544 		      if ((s->flags & SEC_THREAD_LOCAL) != 0)
   5545 			{
   5546 			  _bfd_error_handler (_("	    TLS: %pA"), s);
   5547 			  i++;
   5548 			}
   5549 		      else
   5550 			_bfd_error_handler (_("	non-TLS: %pA"), s);
   5551 		      s = s->next;
   5552 		    }
   5553 		  bfd_set_error (bfd_error_bad_value);
   5554 		  goto error_return;
   5555 		}
   5556 	      m->sections[i] = s;
   5557 	      s = s->next;
   5558 	    }
   5559 
   5560 	  *pm = m;
   5561 	  pm = &m->next;
   5562 	}
   5563 
   5564       if (first_mbind
   5565 	  && (abfd->flags & D_PAGED) != 0
   5566 	  && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
   5567 	for (s = first_mbind; s != NULL; s = s->next)
   5568 	  if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
   5569 	      && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
   5570 	    {
   5571 	      /* Mandated PF_R.  */
   5572 	      unsigned long p_flags = PF_R;
   5573 	      if ((s->flags & SEC_READONLY) == 0)
   5574 		p_flags |= PF_W;
   5575 	      if ((s->flags & SEC_CODE) != 0)
   5576 		p_flags |= PF_X;
   5577 
   5578 	      amt = sizeof (struct elf_segment_map) + sizeof (asection *);
   5579 	      m = bfd_zalloc (abfd, amt);
   5580 	      if (m == NULL)
   5581 		goto error_return;
   5582 	      m->next = NULL;
   5583 	      m->p_type = (PT_GNU_MBIND_LO
   5584 			   + elf_section_data (s)->this_hdr.sh_info);
   5585 	      m->count = 1;
   5586 	      m->p_flags_valid = 1;
   5587 	      m->sections[0] = s;
   5588 	      m->p_flags = p_flags;
   5589 
   5590 	      *pm = m;
   5591 	      pm = &m->next;
   5592 	    }
   5593 
   5594       s = bfd_get_section_by_name (abfd,
   5595 				   NOTE_GNU_PROPERTY_SECTION_NAME);
   5596       if (s != NULL && s->size != 0)
   5597 	{
   5598 	  amt = sizeof (struct elf_segment_map) + sizeof (asection *);
   5599 	  m = bfd_zalloc (abfd, amt);
   5600 	  if (m == NULL)
   5601 	    goto error_return;
   5602 	  m->next = NULL;
   5603 	  m->p_type = PT_GNU_PROPERTY;
   5604 	  m->count = 1;
   5605 	  m->p_flags_valid = 1;
   5606 	  m->sections[0] = s;
   5607 	  m->p_flags = PF_R;
   5608 	  *pm = m;
   5609 	  pm = &m->next;
   5610 	}
   5611 
   5612       /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
   5613 	 segment.  */
   5614       eh_frame_hdr = elf_eh_frame_hdr (info);
   5615       if (eh_frame_hdr != NULL
   5616 	  && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
   5617 	{
   5618 	  amt = sizeof (struct elf_segment_map);
   5619 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5620 	  if (m == NULL)
   5621 	    goto error_return;
   5622 	  m->next = NULL;
   5623 	  m->p_type = PT_GNU_EH_FRAME;
   5624 	  m->count = 1;
   5625 	  m->sections[0] = eh_frame_hdr->output_section;
   5626 
   5627 	  *pm = m;
   5628 	  pm = &m->next;
   5629 	}
   5630 
   5631       /* If there is a .sframe section, throw in a PT_GNU_SFRAME
   5632 	 segment.  */
   5633       sframe = elf_sframe (abfd);
   5634       if (sframe != NULL
   5635 	  && (sframe->output_section->flags & SEC_LOAD) != 0
   5636 	  && sframe->size != 0)
   5637 	{
   5638 	  amt = sizeof (struct elf_segment_map);
   5639 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5640 	  if (m == NULL)
   5641 	    goto error_return;
   5642 	  m->next = NULL;
   5643 	  m->p_type = PT_GNU_SFRAME;
   5644 	  m->count = 1;
   5645 	  m->sections[0] = sframe->output_section;
   5646 
   5647 	  *pm = m;
   5648 	  pm = &m->next;
   5649 	}
   5650 
   5651       if (elf_stack_flags (abfd))
   5652 	{
   5653 	  amt = sizeof (struct elf_segment_map);
   5654 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5655 	  if (m == NULL)
   5656 	    goto error_return;
   5657 	  m->next = NULL;
   5658 	  m->p_type = PT_GNU_STACK;
   5659 	  m->p_flags = elf_stack_flags (abfd);
   5660 	  m->p_align = bed->stack_align;
   5661 	  m->p_flags_valid = 1;
   5662 	  m->p_align_valid = m->p_align != 0;
   5663 	  if (info->stacksize > 0)
   5664 	    {
   5665 	      m->p_size = info->stacksize;
   5666 	      m->p_size_valid = 1;
   5667 	    }
   5668 
   5669 	  *pm = m;
   5670 	  pm = &m->next;
   5671 	}
   5672 
   5673       if (info != NULL && info->relro)
   5674 	{
   5675 	  for (m = mfirst; m != NULL; m = m->next)
   5676 	    {
   5677 	      if (m->p_type == PT_LOAD
   5678 		  && m->count != 0
   5679 		  && m->sections[0]->vma >= info->relro_start
   5680 		  && m->sections[0]->vma < info->relro_end)
   5681 		{
   5682 		  i = m->count;
   5683 		  while (--i != (unsigned) -1)
   5684 		    {
   5685 		      if (m->sections[i]->size > 0
   5686 			  && (m->sections[i]->flags & SEC_LOAD) != 0
   5687 			  && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
   5688 			break;
   5689 		    }
   5690 
   5691 		  if (i != (unsigned) -1)
   5692 		    break;
   5693 		}
   5694 	    }
   5695 
   5696 	  /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
   5697 	  if (m != NULL)
   5698 	    {
   5699 	      amt = sizeof (struct elf_segment_map);
   5700 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5701 	      if (m == NULL)
   5702 		goto error_return;
   5703 	      m->next = NULL;
   5704 	      m->p_type = PT_GNU_RELRO;
   5705 	      *pm = m;
   5706 	      pm = &m->next;
   5707 	    }
   5708 	}
   5709 
   5710       free (sections);
   5711       elf_seg_map (abfd) = mfirst;
   5712     }
   5713 
   5714   if (!elf_modify_segment_map (abfd, info, no_user_phdrs || info == NULL))
   5715     return false;
   5716 
   5717   for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
   5718     ++count;
   5719   elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
   5720 
   5721   return true;
   5722 
   5723  error_return:
   5724   free (sections);
   5725   return false;
   5726 }
   5727 
   5728 /* Sort sections by address.  */
   5729 
   5730 static int
   5731 elf_sort_sections (const void *arg1, const void *arg2)
   5732 {
   5733   const asection *sec1 = *(const asection **) arg1;
   5734   const asection *sec2 = *(const asection **) arg2;
   5735   bfd_size_type size1, size2;
   5736 
   5737   /* Sort by LMA first, since this is the address used to
   5738      place the section into a segment.  */
   5739   if (sec1->lma < sec2->lma)
   5740     return -1;
   5741   else if (sec1->lma > sec2->lma)
   5742     return 1;
   5743 
   5744   /* Then sort by VMA.  Normally the LMA and the VMA will be
   5745      the same, and this will do nothing.  */
   5746   if (sec1->vma < sec2->vma)
   5747     return -1;
   5748   else if (sec1->vma > sec2->vma)
   5749     return 1;
   5750 
   5751   /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
   5752 
   5753 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
   5754 		  && (x)->size != 0)
   5755 
   5756   if (TOEND (sec1))
   5757     {
   5758       if (!TOEND (sec2))
   5759 	return 1;
   5760     }
   5761   else if (TOEND (sec2))
   5762     return -1;
   5763 
   5764 #undef TOEND
   5765 
   5766   /* Sort by size, to put zero sized sections
   5767      before others at the same address.  */
   5768 
   5769   size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
   5770   size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
   5771 
   5772   if (size1 < size2)
   5773     return -1;
   5774   if (size1 > size2)
   5775     return 1;
   5776 
   5777   return sec1->target_index - sec2->target_index;
   5778 }
   5779 
   5780 /* This qsort comparison functions sorts PT_LOAD segments first and
   5781    by p_paddr, for assign_file_positions_for_load_sections.  */
   5782 
   5783 static int
   5784 elf_sort_segments (const void *arg1, const void *arg2)
   5785 {
   5786   const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
   5787   const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
   5788 
   5789   if (m1->p_type != m2->p_type)
   5790     {
   5791       if (m1->p_type == PT_NULL)
   5792 	return 1;
   5793       if (m2->p_type == PT_NULL)
   5794 	return -1;
   5795       return m1->p_type < m2->p_type ? -1 : 1;
   5796     }
   5797   if (m1->includes_filehdr != m2->includes_filehdr)
   5798     return m1->includes_filehdr ? -1 : 1;
   5799   if (m1->no_sort_lma != m2->no_sort_lma)
   5800     return m1->no_sort_lma ? -1 : 1;
   5801   if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
   5802     {
   5803       bfd_vma lma1, lma2;  /* Octets.  */
   5804       lma1 = 0;
   5805       if (m1->p_paddr_valid)
   5806 	lma1 = m1->p_paddr;
   5807       else if (m1->count != 0)
   5808 	{
   5809 	  unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
   5810 						  m1->sections[0]);
   5811 	  lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
   5812 	}
   5813       lma2 = 0;
   5814       if (m2->p_paddr_valid)
   5815 	lma2 = m2->p_paddr;
   5816       else if (m2->count != 0)
   5817 	{
   5818 	  unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
   5819 						  m2->sections[0]);
   5820 	  lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
   5821 	}
   5822       if (lma1 != lma2)
   5823 	return lma1 < lma2 ? -1 : 1;
   5824     }
   5825   if (m1->idx != m2->idx)
   5826     return m1->idx < m2->idx ? -1 : 1;
   5827   return 0;
   5828 }
   5829 
   5830 /* Ian Lance Taylor writes:
   5831 
   5832    We shouldn't be using % with a negative signed number.  That's just
   5833    not good.  We have to make sure either that the number is not
   5834    negative, or that the number has an unsigned type.  When the types
   5835    are all the same size they wind up as unsigned.  When file_ptr is a
   5836    larger signed type, the arithmetic winds up as signed long long,
   5837    which is wrong.
   5838 
   5839    What we're trying to say here is something like ``increase OFF by
   5840    the least amount that will cause it to be equal to the VMA modulo
   5841    the page size.''  */
   5842 /* In other words, something like:
   5843 
   5844    vma_offset = m->sections[0]->vma % bed->maxpagesize;
   5845    off_offset = off % bed->maxpagesize;
   5846    if (vma_offset < off_offset)
   5847      adjustment = vma_offset + bed->maxpagesize - off_offset;
   5848    else
   5849      adjustment = vma_offset - off_offset;
   5850 
   5851    which can be collapsed into the expression below.  */
   5852 
   5853 static file_ptr
   5854 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
   5855 {
   5856   /* PR binutils/16199: Handle an alignment of zero.  */
   5857   if (maxpagesize == 0)
   5858     maxpagesize = 1;
   5859   return ((vma - off) % maxpagesize);
   5860 }
   5861 
   5862 static void
   5863 print_segment_map (const struct elf_segment_map *m)
   5864 {
   5865   unsigned int j;
   5866   const char *pt = get_segment_type (m->p_type);
   5867   char buf[32];
   5868 
   5869   if (pt == NULL)
   5870     {
   5871       if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
   5872 	sprintf (buf, "LOPROC+%7.7x",
   5873 		 (unsigned int) (m->p_type - PT_LOPROC));
   5874       else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
   5875 	sprintf (buf, "LOOS+%7.7x",
   5876 		 (unsigned int) (m->p_type - PT_LOOS));
   5877       else
   5878 	snprintf (buf, sizeof (buf), "%8.8x",
   5879 		  (unsigned int) m->p_type);
   5880       pt = buf;
   5881     }
   5882   fflush (stdout);
   5883   fprintf (stderr, "%s:", pt);
   5884   for (j = 0; j < m->count; j++)
   5885     fprintf (stderr, " %s", m->sections [j]->name);
   5886   putc ('\n',stderr);
   5887   fflush (stderr);
   5888 }
   5889 
   5890 /* Assign file positions to the sections based on the mapping from
   5891    sections to segments.  This function also sets up some fields in
   5892    the file header.  */
   5893 
   5894 static bool
   5895 assign_file_positions_for_load_sections (bfd *abfd,
   5896 					 struct bfd_link_info *link_info)
   5897 {
   5898   elf_backend_data *bed = get_elf_backend_data (abfd);
   5899   struct elf_segment_map *m;
   5900   struct elf_segment_map *phdr_load_seg;
   5901   Elf_Internal_Phdr *phdrs;
   5902   Elf_Internal_Phdr *p;
   5903   file_ptr off;  /* Octets.  */
   5904   bfd_size_type maxpagesize;
   5905   unsigned int alloc, actual;
   5906   unsigned int i, j;
   5907   struct elf_segment_map **sorted_seg_map;
   5908   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   5909 
   5910   if (link_info == NULL
   5911       && !bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
   5912     return false;
   5913 
   5914   alloc = 0;
   5915   for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   5916     m->idx = alloc++;
   5917 
   5918   if (alloc)
   5919     {
   5920       elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
   5921       elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
   5922     }
   5923   else
   5924     {
   5925       /* PR binutils/12467.  */
   5926       elf_elfheader (abfd)->e_phoff = 0;
   5927       elf_elfheader (abfd)->e_phentsize = 0;
   5928     }
   5929 
   5930   elf_elfheader (abfd)->e_phnum = alloc;
   5931 
   5932   if (elf_program_header_size (abfd) == (bfd_size_type) -1)
   5933     {
   5934       actual = alloc;
   5935       elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
   5936     }
   5937   else
   5938     {
   5939       actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
   5940       BFD_ASSERT (elf_program_header_size (abfd)
   5941 		  == actual * bed->s->sizeof_phdr);
   5942       BFD_ASSERT (actual >= alloc);
   5943     }
   5944 
   5945   if (alloc == 0)
   5946     {
   5947       elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
   5948       return true;
   5949     }
   5950 
   5951   /* We're writing the size in elf_program_header_size (abfd),
   5952      see assign_file_positions_except_relocs, so make sure we have
   5953      that amount allocated, with trailing space cleared.
   5954      The variable alloc contains the computed need, while
   5955      elf_program_header_size (abfd) contains the size used for the
   5956      layout.
   5957      See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
   5958      where the layout is forced to according to a larger size in the
   5959      last iterations for the testcase ld-elf/header.  */
   5960   phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
   5961 			     + alloc * sizeof (*sorted_seg_map)));
   5962   sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
   5963   elf_tdata (abfd)->phdr = phdrs;
   5964   if (phdrs == NULL)
   5965     return false;
   5966 
   5967   for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
   5968     {
   5969       sorted_seg_map[j] = m;
   5970       /* If elf_segment_map is not from map_sections_to_segments, the
   5971 	 sections may not be correctly ordered.  NOTE: sorting should
   5972 	 not be done to the PT_NOTE section of a corefile, which may
   5973 	 contain several pseudo-sections artificially created by bfd.
   5974 	 Sorting these pseudo-sections breaks things badly.  */
   5975       if (m->count > 1
   5976 	  && !(elf_elfheader (abfd)->e_type == ET_CORE
   5977 	       && m->p_type == PT_NOTE))
   5978 	{
   5979 	  for (i = 0; i < m->count; i++)
   5980 	    m->sections[i]->target_index = i;
   5981 	  qsort (m->sections, (size_t) m->count, sizeof (asection *),
   5982 		 elf_sort_sections);
   5983 	}
   5984     }
   5985   if (alloc > 1)
   5986     qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
   5987 	   elf_sort_segments);
   5988 
   5989   maxpagesize = 1;
   5990   if ((abfd->flags & D_PAGED) != 0)
   5991     {
   5992       if (link_info != NULL)
   5993 	maxpagesize = link_info->maxpagesize;
   5994       else
   5995 	maxpagesize = bed->maxpagesize;
   5996     }
   5997 
   5998   /* Sections must map to file offsets past the ELF file header.  */
   5999   off = bed->s->sizeof_ehdr;
   6000   /* And if one of the PT_LOAD headers doesn't include the program
   6001      headers then we'll be mapping program headers in the usual
   6002      position after the ELF file header.  */
   6003   phdr_load_seg = NULL;
   6004   for (j = 0; j < alloc; j++)
   6005     {
   6006       m = sorted_seg_map[j];
   6007       if (m->p_type != PT_LOAD)
   6008 	break;
   6009       if (m->includes_phdrs)
   6010 	{
   6011 	  phdr_load_seg = m;
   6012 	  break;
   6013 	}
   6014     }
   6015   if (phdr_load_seg == NULL)
   6016     off += actual * bed->s->sizeof_phdr;
   6017 
   6018   for (j = 0; j < alloc; j++)
   6019     {
   6020       asection **secpp;
   6021       bfd_vma off_adjust;  /* Octets.  */
   6022       bool no_contents;
   6023       bfd_size_type align_pagesize;
   6024 
   6025       /* An ELF segment (described by Elf_Internal_Phdr) may contain a
   6026 	 number of sections with contents contributing to both p_filesz
   6027 	 and p_memsz, followed by a number of sections with no contents
   6028 	 that just contribute to p_memsz.  In this loop, OFF tracks next
   6029 	 available file offset for PT_LOAD and PT_NOTE segments.  */
   6030       m = sorted_seg_map[j];
   6031       p = phdrs + m->idx;
   6032       p->p_type = m->p_type;
   6033       p->p_flags = m->p_flags;
   6034 
   6035       if (m->count == 0)
   6036 	p->p_vaddr = m->p_vaddr_offset * opb;
   6037       else
   6038 	p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
   6039 
   6040       if (m->p_paddr_valid)
   6041 	p->p_paddr = m->p_paddr;
   6042       else if (m->count == 0)
   6043 	p->p_paddr = 0;
   6044       else
   6045 	p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
   6046 
   6047       align_pagesize = 0;
   6048       if (p->p_type == PT_LOAD
   6049 	  && (abfd->flags & D_PAGED) != 0)
   6050 	{
   6051 	  /* p_align in demand paged PT_LOAD segments effectively stores
   6052 	     the maximum page size.  When copying an executable with
   6053 	     objcopy, we set m->p_align from the input file.  Use this
   6054 	     value for maxpagesize rather than bed->maxpagesize, which
   6055 	     may be different.  Note that we use maxpagesize for PT_TLS
   6056 	     segment alignment later in this function, so we are relying
   6057 	     on at least one PT_LOAD segment appearing before a PT_TLS
   6058 	     segment.  */
   6059 	  if (m->p_align_valid)
   6060 	    maxpagesize = m->p_align;
   6061 	  else if (bed->p_align != 0
   6062 		   && (link_info == NULL
   6063 		       || !link_info->maxpagesize_is_set))
   6064 	    /* We will lay out this binary using maxpagesize but set
   6065 	       p->p_align later to the possibly smaller bed->p_align.
   6066 	       The run-time loader will then be able to load this
   6067 	       binary when the system page size is maxpagesize, but if
   6068 	       the system page size is smaller can use p->p_align.
   6069 	       In either case p->p_align will be increased if
   6070 	       necessary to match section alignment.  */
   6071 	    align_pagesize = bed->p_align;
   6072 
   6073 	  p->p_align = maxpagesize;
   6074 	}
   6075       else if (m->p_align_valid)
   6076 	p->p_align = m->p_align;
   6077       else if (m->count == 0)
   6078 	p->p_align = 1 << bed->s->log_file_align;
   6079 
   6080       if (m == phdr_load_seg)
   6081 	off += actual * bed->s->sizeof_phdr;
   6082 
   6083       no_contents = false;
   6084       off_adjust = 0;
   6085       if (p->p_type == PT_LOAD
   6086 	  && m->count > 0)
   6087 	{
   6088 	  bfd_size_type align;  /* Bytes.  */
   6089 	  unsigned int align_power = 0;
   6090 
   6091 	  if (m->p_align_valid)
   6092 	    align = p->p_align;
   6093 	  else
   6094 	    {
   6095 	      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   6096 		{
   6097 		  unsigned int secalign;
   6098 
   6099 		  secalign = bfd_section_alignment (*secpp);
   6100 		  if (secalign > align_power)
   6101 		    align_power = secalign;
   6102 		}
   6103 	      align = (bfd_size_type) 1 << align_power;
   6104 	      /* If a section requires alignment higher than the
   6105 		 minimum p_align value, don't reduce a maxpagesize
   6106 		 p->p_align set earlier in this function.  */
   6107 	      if (align > bed->p_align)
   6108 		align_pagesize = 0;
   6109 	      if (align < maxpagesize)
   6110 		align = maxpagesize;
   6111 	      else
   6112 		{
   6113 		  /* If a section requires alignment higher than the
   6114 		     maximum page size, set p_align to the section
   6115 		     alignment.  */
   6116 		  if ((abfd->flags & D_PAGED) != 0)
   6117 		    p->p_align = align;
   6118 		}
   6119 	    }
   6120 
   6121 	  for (i = 0; i < m->count; i++)
   6122 	    if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   6123 	      /* If we aren't making room for this section, then
   6124 		 it must be SHT_NOBITS regardless of what we've
   6125 		 set via struct bfd_elf_special_section.  */
   6126 	      elf_section_type (m->sections[i]) = SHT_NOBITS;
   6127 
   6128 	  /* Find out whether this segment contains any loadable
   6129 	     sections.  */
   6130 	  no_contents = true;
   6131 	  for (i = 0; i < m->count; i++)
   6132 	    if (elf_section_type (m->sections[i]) != SHT_NOBITS)
   6133 	      {
   6134 		no_contents = false;
   6135 		break;
   6136 	      }
   6137 
   6138 	  off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
   6139 
   6140 	  /* Broken hardware and/or kernel require that files do not
   6141 	     map the same page with different permissions on some hppa
   6142 	     processors.  */
   6143 	  if (j != 0
   6144 	      && (abfd->flags & D_PAGED) != 0
   6145 	      && bed->no_page_alias
   6146 	      && (off & (maxpagesize - 1)) != 0
   6147 	      && ((off & -maxpagesize)
   6148 		  == ((off + off_adjust) & -maxpagesize)))
   6149 	    off_adjust += maxpagesize;
   6150 	  off += off_adjust;
   6151 	  if (no_contents)
   6152 	    {
   6153 	      /* We shouldn't need to align the segment on disk since
   6154 		 the segment doesn't need file space, but the gABI
   6155 		 arguably requires the alignment and glibc ld.so
   6156 		 checks it.  So to comply with the alignment
   6157 		 requirement but not waste file space, we adjust
   6158 		 p_offset for just this segment.  (OFF_ADJUST is
   6159 		 subtracted from OFF later.)  This may put p_offset
   6160 		 past the end of file, but that shouldn't matter.  */
   6161 	    }
   6162 	  else
   6163 	    off_adjust = 0;
   6164 	}
   6165       /* Make sure the .dynamic section is the first section in the
   6166 	 PT_DYNAMIC segment.  */
   6167       else if (p->p_type == PT_DYNAMIC
   6168 	       && m->count > 1
   6169 	       && ! streq (m->sections[0]->name, ".dynamic"))
   6170 	{
   6171 	  _bfd_error_handler
   6172 	    (_("%pB: The first section in the PT_DYNAMIC segment"
   6173 	       " is not the .dynamic section"),
   6174 	     abfd);
   6175 	  bfd_set_error (bfd_error_bad_value);
   6176 	  return false;
   6177 	}
   6178       /* Set the note section type to SHT_NOTE.  */
   6179       else if (p->p_type == PT_NOTE)
   6180 	for (i = 0; i < m->count; i++)
   6181 	  elf_section_type (m->sections[i]) = SHT_NOTE;
   6182 
   6183       if (m->includes_filehdr)
   6184 	{
   6185 	  if (!m->p_flags_valid)
   6186 	    p->p_flags |= PF_R;
   6187 	  p->p_filesz = bed->s->sizeof_ehdr;
   6188 	  p->p_memsz = bed->s->sizeof_ehdr;
   6189 	  if (p->p_type == PT_LOAD)
   6190 	    {
   6191 	      if (m->count > 0)
   6192 		{
   6193 		  if (p->p_vaddr < (bfd_vma) off
   6194 		      || (!m->p_paddr_valid
   6195 			  && p->p_paddr < (bfd_vma) off))
   6196 		    {
   6197 		      _bfd_error_handler
   6198 			(_("%pB: not enough room for program headers,"
   6199 			   " try linking with -N"),
   6200 			 abfd);
   6201 		      bfd_set_error (bfd_error_bad_value);
   6202 		      return false;
   6203 		    }
   6204 		  p->p_vaddr -= off;
   6205 		  if (!m->p_paddr_valid)
   6206 		    p->p_paddr -= off;
   6207 		}
   6208 	    }
   6209 	  else if (sorted_seg_map[0]->includes_filehdr)
   6210 	    {
   6211 	      Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
   6212 	      p->p_vaddr = filehdr->p_vaddr;
   6213 	      if (!m->p_paddr_valid)
   6214 		p->p_paddr = filehdr->p_paddr;
   6215 	    }
   6216 	}
   6217 
   6218       if (m->includes_phdrs)
   6219 	{
   6220 	  if (!m->p_flags_valid)
   6221 	    p->p_flags |= PF_R;
   6222 	  p->p_filesz += actual * bed->s->sizeof_phdr;
   6223 	  p->p_memsz += actual * bed->s->sizeof_phdr;
   6224 	  if (!m->includes_filehdr)
   6225 	    {
   6226 	      if (p->p_type == PT_LOAD)
   6227 		{
   6228 		  p->p_offset = off - actual * bed->s->sizeof_phdr;
   6229 		  elf_elfheader (abfd)->e_phoff = p->p_offset;
   6230 		  if (m->count > 0)
   6231 		    {
   6232 		      p->p_vaddr -= off - p->p_offset;
   6233 		      if (!m->p_paddr_valid)
   6234 			p->p_paddr -= off - p->p_offset;
   6235 		    }
   6236 		}
   6237 	      else if (phdr_load_seg != NULL)
   6238 		{
   6239 		  /* Also set PT_PHDR to match phdr_load_seg.  We've
   6240 		     sorted segments so that phdr_load_seg will
   6241 		     already be set by the code immediately above.  */
   6242 		  Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
   6243 		  bfd_vma phdr_off = 0;  /* Octets.  */
   6244 		  if (phdr_load_seg->includes_filehdr)
   6245 		    phdr_off = bed->s->sizeof_ehdr;
   6246 		  p->p_vaddr = phdr->p_vaddr + phdr_off;
   6247 		  if (!m->p_paddr_valid)
   6248 		    p->p_paddr = phdr->p_paddr + phdr_off;
   6249 		  p->p_offset = phdr->p_offset + phdr_off;
   6250 		}
   6251 	      else
   6252 		p->p_offset = bed->s->sizeof_ehdr;
   6253 	    }
   6254 	}
   6255 
   6256       if (p->p_type == PT_LOAD
   6257 	  || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
   6258 	{
   6259 	  if (!m->includes_filehdr && !m->includes_phdrs)
   6260 	    {
   6261 	      p->p_offset = off;
   6262 	      if (no_contents)
   6263 		{
   6264 		  /* Put meaningless p_offset for PT_LOAD segments
   6265 		     without file contents somewhere within the first
   6266 		     page, in an attempt to not point past EOF.  */
   6267 		  bfd_size_type align = maxpagesize;
   6268 		  if (align < p->p_align)
   6269 		    align = p->p_align;
   6270 		  if (align < 1)
   6271 		    align = 1;
   6272 		  /* Avoid p_offset of zero, which might be wrongly
   6273 		     interpreted as the segment being the first one,
   6274 		     containing the file header.  PR32763.  */
   6275 		  p->p_offset = (off + align - 1) % align + 1;
   6276 		}
   6277 	    }
   6278 	  else
   6279 	    {
   6280 	      file_ptr adjust;  /* Octets.  */
   6281 
   6282 	      adjust = off - (p->p_offset + p->p_filesz);
   6283 	      if (!no_contents)
   6284 		p->p_filesz += adjust;
   6285 	      p->p_memsz += adjust;
   6286 	    }
   6287 	}
   6288 
   6289       if (align_pagesize)
   6290 	p->p_align = align_pagesize;
   6291 
   6292       /* Set up p_filesz, p_memsz, p_align and p_flags from the section
   6293 	 maps.  Set filepos for sections in PT_LOAD segments, and in
   6294 	 core files, for sections in PT_NOTE segments.
   6295 	 assign_file_positions_for_non_load_sections will set filepos
   6296 	 for other sections and update p_filesz for other segments.  */
   6297       for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   6298 	{
   6299 	  asection *sec;
   6300 	  bfd_size_type align;
   6301 	  Elf_Internal_Shdr *this_hdr;
   6302 
   6303 	  sec = *secpp;
   6304 	  this_hdr = &elf_section_data (sec)->this_hdr;
   6305 	  align = (bfd_size_type) 1 << bfd_section_alignment (sec);
   6306 
   6307 	  if ((p->p_type == PT_LOAD
   6308 	       || p->p_type == PT_TLS)
   6309 	      && (this_hdr->sh_type != SHT_NOBITS
   6310 		  || ((this_hdr->sh_flags & SHF_ALLOC) != 0
   6311 		      && ((this_hdr->sh_flags & SHF_TLS) == 0
   6312 			  || p->p_type == PT_TLS))))
   6313 	    {
   6314 	      bfd_vma p_start = p->p_paddr;		/* Octets.  */
   6315 	      bfd_vma p_end = p_start + p->p_memsz;	/* Octets.  */
   6316 	      bfd_vma s_start = sec->lma * opb;		/* Octets.  */
   6317 	      bfd_vma adjust = s_start - p_end;		/* Octets.  */
   6318 
   6319 	      if (adjust != 0
   6320 		  && (s_start < p_end
   6321 		      || p_end < p_start))
   6322 		{
   6323 		  _bfd_error_handler
   6324 		    /* xgettext:c-format */
   6325 		    (_("%pB: section %pA lma %#" PRIx64
   6326 		       " adjusted to %#" PRIx64),
   6327 		     abfd, sec, (uint64_t) s_start / opb,
   6328 		     (uint64_t) p_end / opb);
   6329 		  adjust = 0;
   6330 		  sec->lma = p_end / opb;
   6331 		}
   6332 	      p->p_memsz += adjust;
   6333 
   6334 	      if (p->p_type == PT_LOAD)
   6335 		{
   6336 		  if (this_hdr->sh_type != SHT_NOBITS)
   6337 		    {
   6338 		      off_adjust = 0;
   6339 		      if (p->p_filesz + adjust < p->p_memsz)
   6340 			{
   6341 			  /* We have a PROGBITS section following NOBITS ones.
   6342 			     Allocate file space for the NOBITS section(s).
   6343 			     We don't need to write out the zeros, posix
   6344 			     fseek past the end of data already written
   6345 			     followed by a write at that location is
   6346 			     guaranteed to result in zeros being read
   6347 			     from the gap.  */
   6348 			  adjust = p->p_memsz - p->p_filesz;
   6349 			}
   6350 		    }
   6351 		  /* We only adjust sh_offset in SHT_NOBITS sections
   6352 		     as would seem proper for their address when the
   6353 		     section is first in the segment.  sh_offset
   6354 		     doesn't really have any significance for
   6355 		     SHT_NOBITS anyway, apart from a notional position
   6356 		     relative to other sections.  Historically we
   6357 		     didn't bother with adjusting sh_offset and some
   6358 		     programs depend on it not being adjusted.  See
   6359 		     pr12921 and pr25662.  */
   6360 		  if (this_hdr->sh_type != SHT_NOBITS || i == 0)
   6361 		    {
   6362 		      off += adjust;
   6363 		      if (this_hdr->sh_type == SHT_NOBITS)
   6364 			off_adjust += adjust;
   6365 		    }
   6366 		}
   6367 	      if (this_hdr->sh_type != SHT_NOBITS)
   6368 		p->p_filesz += adjust;
   6369 	    }
   6370 
   6371 	  if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
   6372 	    {
   6373 	      /* The section at i == 0 is the one that actually contains
   6374 		 everything.  */
   6375 	      if (i == 0)
   6376 		{
   6377 		  this_hdr->sh_offset = sec->filepos = off;
   6378 		  off += this_hdr->sh_size;
   6379 		  p->p_filesz = this_hdr->sh_size;
   6380 		  p->p_memsz = 0;
   6381 		  p->p_align = 1;
   6382 		}
   6383 	      else
   6384 		{
   6385 		  /* The rest are fake sections that shouldn't be written.  */
   6386 		  sec->filepos = 0;
   6387 		  sec->size = 0;
   6388 		  sec->flags = 0;
   6389 		  continue;
   6390 		}
   6391 	    }
   6392 	  else
   6393 	    {
   6394 	      if (this_hdr->sh_type == SHT_NOBITS
   6395 		  && (this_hdr->sh_flags & SHF_TLS) != 0
   6396 		  && this_hdr->sh_offset == 0)
   6397 		{
   6398 		  /* Set sh_offset for .tbss sections to their nominal
   6399 		     offset after aligning.  They are not loaded from
   6400 		     disk so the value doesn't really matter, except
   6401 		     when the .tbss section is the first one in a
   6402 		     PT_TLS segment.  In that case it sets the
   6403 		     p_offset for the PT_TLS segment, which according
   6404 		     to the ELF gABI ought to satisfy
   6405 		     p_offset % p_align == p_vaddr % p_align.  */
   6406 		  bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
   6407 							  off, align);
   6408 		  this_hdr->sh_offset = sec->filepos = off + adjust;
   6409 		}
   6410 	      else if (p->p_type == PT_LOAD)
   6411 		{
   6412 		  this_hdr->sh_offset = sec->filepos = off;
   6413 		  if (this_hdr->sh_type != SHT_NOBITS)
   6414 		    off += this_hdr->sh_size;
   6415 		}
   6416 
   6417 	      if (this_hdr->sh_type != SHT_NOBITS)
   6418 		{
   6419 		  p->p_filesz += this_hdr->sh_size;
   6420 		  /* A load section without SHF_ALLOC is something like
   6421 		     a note section in a PT_NOTE segment.  These take
   6422 		     file space but are not loaded into memory.  */
   6423 		  if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   6424 		    p->p_memsz += this_hdr->sh_size;
   6425 		}
   6426 	      else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   6427 		{
   6428 		  if (p->p_type == PT_TLS)
   6429 		    p->p_memsz += this_hdr->sh_size;
   6430 
   6431 		  /* .tbss is special.  It doesn't contribute to p_memsz of
   6432 		     normal segments.  */
   6433 		  else if ((this_hdr->sh_flags & SHF_TLS) == 0)
   6434 		    p->p_memsz += this_hdr->sh_size;
   6435 		}
   6436 
   6437 	      if (align > p->p_align
   6438 		  && !m->p_align_valid
   6439 		  && (p->p_type != PT_LOAD
   6440 		      || (abfd->flags & D_PAGED) == 0))
   6441 		p->p_align = align;
   6442 	    }
   6443 
   6444 	  if (!m->p_flags_valid)
   6445 	    {
   6446 	      p->p_flags |= PF_R;
   6447 	      if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
   6448 		p->p_flags |= PF_X;
   6449 	      if ((this_hdr->sh_flags & SHF_WRITE) != 0)
   6450 		p->p_flags |= PF_W;
   6451 	    }
   6452 	}
   6453 
   6454       off -= off_adjust;
   6455 
   6456       /* PR ld/20815 - Check that the program header segment, if
   6457 	 present, will be loaded into memory.  */
   6458       if (p->p_type == PT_PHDR
   6459 	  && phdr_load_seg == NULL
   6460 	  && !(bed->elf_backend_allow_non_load_phdr != NULL
   6461 	       && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
   6462 	{
   6463 	  /* The fix for this error is usually to edit the linker script being
   6464 	     used and set up the program headers manually.  Either that or
   6465 	     leave room for the headers at the start of the SECTIONS.  */
   6466 	  _bfd_error_handler (_("%pB: error: PHDR segment not covered"
   6467 				" by LOAD segment"),
   6468 			      abfd);
   6469 	  if (link_info == NULL)
   6470 	    return false;
   6471 	  /* Arrange for the linker to exit with an error, deleting
   6472 	     the output file unless --noinhibit-exec is given.  */
   6473 	  link_info->callbacks->info ("%X");
   6474 	}
   6475 
   6476       /* Check that all sections are in a PT_LOAD segment.
   6477 	 Don't check funky gdb generated core files.  */
   6478       if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
   6479 	{
   6480 	  bool check_vma = true;
   6481 
   6482 	  for (i = 1; i < m->count; i++)
   6483 	    if (m->sections[i]->vma == m->sections[i - 1]->vma
   6484 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
   6485 				       ->this_hdr), p) != 0
   6486 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
   6487 				       ->this_hdr), p) != 0)
   6488 	      {
   6489 		/* Looks like we have overlays packed into the segment.  */
   6490 		check_vma = false;
   6491 		break;
   6492 	      }
   6493 
   6494 	  for (i = 0; i < m->count; i++)
   6495 	    {
   6496 	      Elf_Internal_Shdr *this_hdr;
   6497 	      asection *sec;
   6498 
   6499 	      sec = m->sections[i];
   6500 	      this_hdr = &(elf_section_data(sec)->this_hdr);
   6501 	      if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
   6502 		  && !ELF_TBSS_SPECIAL (this_hdr, p))
   6503 		{
   6504 		  _bfd_error_handler
   6505 		    /* xgettext:c-format */
   6506 		    (_("%pB: section `%pA' can't be allocated in segment %u"),
   6507 		     abfd, sec, m->idx);
   6508 		  print_segment_map (m);
   6509 		}
   6510 	    }
   6511 	}
   6512     }
   6513 
   6514   elf_next_file_pos (abfd) = off;
   6515 
   6516   if (link_info != NULL
   6517       && phdr_load_seg != NULL
   6518       && phdr_load_seg->includes_filehdr)
   6519     {
   6520       /* There is a segment that contains both the file headers and the
   6521 	 program headers, so provide a symbol __ehdr_start pointing there.
   6522 	 A program can use this to examine itself robustly.  */
   6523 
   6524       struct elf_link_hash_table *htab = elf_hash_table (link_info);
   6525       struct elf_link_hash_entry *hash = htab->hehdr_start;
   6526 
   6527       /* If the symbol was referenced and not defined, define it.  */
   6528       if (hash != NULL
   6529 	  && (hash->root.type == bfd_link_hash_new
   6530 	      || hash->root.type == bfd_link_hash_undefined
   6531 	      || hash->root.type == bfd_link_hash_undefweak
   6532 	      || hash->root.type == bfd_link_hash_common))
   6533 	{
   6534 	  asection *s = NULL;
   6535 	  bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
   6536 
   6537 	  if (phdr_load_seg->count != 0)
   6538 	    /* The segment contains sections, so use the first one.  */
   6539 	    s = phdr_load_seg->sections[0];
   6540 	  else
   6541 	    /* Use the first (i.e. lowest-addressed) section in any segment.  */
   6542 	    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   6543 	      if (m->p_type == PT_LOAD && m->count != 0)
   6544 		{
   6545 		  s = m->sections[0];
   6546 		  break;
   6547 		}
   6548 
   6549 	  if (s != NULL)
   6550 	    {
   6551 	      hash->root.u.def.value = filehdr_vaddr - s->vma;
   6552 	      hash->root.u.def.section = s;
   6553 	    }
   6554 	  else
   6555 	    {
   6556 	      hash->root.u.def.value = filehdr_vaddr;
   6557 	      hash->root.u.def.section = bfd_abs_section_ptr;
   6558 	    }
   6559 
   6560 	  hash->root.type = bfd_link_hash_defined;
   6561 	  hash->def_regular = 1;
   6562 	  hash->non_elf = 0;
   6563 	}
   6564     }
   6565 
   6566   return true;
   6567 }
   6568 
   6569 /* Determine if a bfd is a debuginfo file.  Unfortunately there
   6570    is no defined method for detecting such files, so we have to
   6571    use heuristics instead.  */
   6572 
   6573 bool
   6574 is_debuginfo_file (bfd *abfd)
   6575 {
   6576   if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   6577     return false;
   6578 
   6579   Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
   6580   Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
   6581   Elf_Internal_Shdr **headerp;
   6582 
   6583   for (headerp = start_headers; headerp < end_headers; headerp ++)
   6584     {
   6585       Elf_Internal_Shdr *header = * headerp;
   6586 
   6587       /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
   6588 	 The only allocated sections are SHT_NOBITS or SHT_NOTES.  */
   6589       if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
   6590 	  && header->sh_type != SHT_NOBITS
   6591 	  && header->sh_type != SHT_NOTE)
   6592 	return false;
   6593     }
   6594 
   6595   return true;
   6596 }
   6597 
   6598 /* Assign file positions for other sections, except for compressed debug
   6599    and sections assigned in _bfd_elf_assign_file_positions_for_non_load.  */
   6600 
   6601 static bool
   6602 assign_file_positions_for_non_load_sections (bfd *abfd,
   6603 					     struct bfd_link_info *link_info)
   6604 {
   6605   elf_backend_data *bed = get_elf_backend_data (abfd);
   6606   Elf_Internal_Shdr **i_shdrpp;
   6607   Elf_Internal_Shdr **hdrpp, **end_hdrpp;
   6608   Elf_Internal_Phdr *phdrs;
   6609   Elf_Internal_Phdr *p;
   6610   struct elf_segment_map *m;
   6611   file_ptr off;
   6612   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   6613   bfd_vma maxpagesize;
   6614 
   6615   if (link_info != NULL)
   6616     maxpagesize = link_info->maxpagesize;
   6617   else
   6618     maxpagesize = bed->maxpagesize;
   6619   i_shdrpp = elf_elfsections (abfd);
   6620   end_hdrpp = i_shdrpp + elf_numsections (abfd);
   6621   off = elf_next_file_pos (abfd);
   6622   for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
   6623     {
   6624       Elf_Internal_Shdr *hdr;
   6625       bfd_vma align;
   6626 
   6627       hdr = *hdrpp;
   6628       if (hdr->bfd_section != NULL
   6629 	  && (hdr->bfd_section->filepos != 0
   6630 	      || (hdr->sh_type == SHT_NOBITS
   6631 		  && hdr->contents == NULL)))
   6632 	BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
   6633       else if ((hdr->sh_flags & SHF_ALLOC) != 0)
   6634 	{
   6635 	  if (hdr->sh_size != 0
   6636 	      /* PR 24717 - debuginfo files are known to be not strictly
   6637 		 compliant with the ELF standard.  In particular they often
   6638 		 have .note.gnu.property sections that are outside of any
   6639 		 loadable segment.  This is not a problem for such files,
   6640 		 so do not warn about them.  */
   6641 	      && ! is_debuginfo_file (abfd))
   6642 	    _bfd_error_handler
   6643 	      /* xgettext:c-format */
   6644 	      (_("%pB: warning: allocated section `%s' not in segment"),
   6645 	       abfd,
   6646 	       (hdr->bfd_section == NULL
   6647 		? "*unknown*"
   6648 		: hdr->bfd_section->name));
   6649 	  /* We don't need to page align empty sections.  */
   6650 	  if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
   6651 	    align = maxpagesize;
   6652 	  else
   6653 	    align = hdr->sh_addralign & -hdr->sh_addralign;
   6654 	  off += vma_page_aligned_bias (hdr->sh_addr, off, align);
   6655 	  off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
   6656 							   bed->s->log_file_align);
   6657 	}
   6658       else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   6659 		&& hdr->bfd_section == NULL)
   6660 	       /* We don't know the offset of these sections yet:
   6661 		  their size has not been decided.  */
   6662 	       || (abfd->is_linker_output
   6663 		   && hdr->bfd_section != NULL
   6664 		   && (hdr->sh_name == -1u
   6665 		       || bfd_section_is_ctf (hdr->bfd_section)))
   6666 	       || hdr == i_shdrpp[elf_onesymtab (abfd)]
   6667 	       || (elf_symtab_shndx_list (abfd) != NULL
   6668 		   && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   6669 	       || hdr == i_shdrpp[elf_strtab_sec (abfd)]
   6670 	       || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
   6671 	hdr->sh_offset = -1;
   6672       else
   6673 	off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
   6674     }
   6675   elf_next_file_pos (abfd) = off;
   6676 
   6677   /* Now that we have set the section file positions, we can set up
   6678      the file positions for the non PT_LOAD segments.  */
   6679   phdrs = elf_tdata (abfd)->phdr;
   6680   for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
   6681     {
   6682       if (p->p_type == PT_GNU_RELRO)
   6683 	{
   6684 	  bfd_vma start, end;  /* Bytes.  */
   6685 	  bool ok;
   6686 
   6687 	  if (link_info != NULL)
   6688 	    {
   6689 	      /* During linking the range of the RELRO segment is passed
   6690 		 in link_info.  Note that there may be padding between
   6691 		 relro_start and the first RELRO section.  */
   6692 	      start = link_info->relro_start;
   6693 	      end = link_info->relro_end;
   6694 	    }
   6695 	  else if (m->count != 0)
   6696 	    {
   6697 	      if (!m->p_size_valid)
   6698 		abort ();
   6699 	      start = m->sections[0]->vma;
   6700 	      end = start + m->p_size / opb;
   6701 	    }
   6702 	  else
   6703 	    {
   6704 	      start = 0;
   6705 	      end = 0;
   6706 	    }
   6707 
   6708 	  ok = false;
   6709 	  if (start < end)
   6710 	    {
   6711 	      struct elf_segment_map *lm;
   6712 	      const Elf_Internal_Phdr *lp;
   6713 	      unsigned int i;
   6714 
   6715 	      /* Find a LOAD segment containing a section in the RELRO
   6716 		 segment.  */
   6717 	      for (lm = elf_seg_map (abfd), lp = phdrs;
   6718 		   lm != NULL;
   6719 		   lm = lm->next, lp++)
   6720 		{
   6721 		  if (lp->p_type == PT_LOAD
   6722 		      && lm->count != 0
   6723 		      && (lm->sections[lm->count - 1]->vma
   6724 			  + (!IS_TBSS (lm->sections[lm->count - 1])
   6725 			     ? lm->sections[lm->count - 1]->size / opb
   6726 			     : 0)) > start
   6727 		      && lm->sections[0]->vma < end)
   6728 		    break;
   6729 		}
   6730 
   6731 	      if (lm != NULL)
   6732 		{
   6733 		  /* Find the section starting the RELRO segment.  */
   6734 		  for (i = 0; i < lm->count; i++)
   6735 		    {
   6736 		      asection *s = lm->sections[i];
   6737 		      if (s->vma >= start
   6738 			  && s->vma < end
   6739 			  && s->size != 0)
   6740 			break;
   6741 		    }
   6742 
   6743 		  if (i < lm->count)
   6744 		    {
   6745 		      p->p_vaddr = lm->sections[i]->vma * opb;
   6746 		      p->p_paddr = lm->sections[i]->lma * opb;
   6747 		      p->p_offset = lm->sections[i]->filepos;
   6748 		      p->p_memsz = end * opb - p->p_vaddr;
   6749 		      p->p_filesz = p->p_memsz;
   6750 
   6751 		      /* The RELRO segment typically ends a few bytes
   6752 			 into .got.plt but other layouts are possible.
   6753 			 In cases where the end does not match any
   6754 			 loaded section (for instance is in file
   6755 			 padding), trim p_filesz back to correspond to
   6756 			 the end of loaded section contents.  */
   6757 		      if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
   6758 			p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
   6759 
   6760 		      /* Preserve the alignment and flags if they are
   6761 			 valid.  The gold linker generates RW/4 for
   6762 			 the PT_GNU_RELRO section.  It is better for
   6763 			 objcopy/strip to honor these attributes
   6764 			 otherwise gdb will choke when using separate
   6765 			 debug files.  */
   6766 		      if (!m->p_align_valid)
   6767 			p->p_align = 1;
   6768 		      if (!m->p_flags_valid)
   6769 			p->p_flags = PF_R;
   6770 		      ok = true;
   6771 		    }
   6772 		}
   6773 	    }
   6774 
   6775 	  if (!ok)
   6776 	    {
   6777 	      if (link_info != NULL)
   6778 		_bfd_error_handler
   6779 		  (_("%pB: warning: unable to allocate any sections"
   6780 		     " to PT_GNU_RELRO segment"),
   6781 		   abfd);
   6782 	      memset (p, 0, sizeof *p);
   6783 	    }
   6784 	}
   6785       else if (p->p_type == PT_GNU_STACK)
   6786 	{
   6787 	  if (m->p_size_valid)
   6788 	    p->p_memsz = m->p_size;
   6789 	}
   6790       else if (m->count != 0)
   6791 	{
   6792 	  unsigned int i;
   6793 
   6794 	  if (p->p_type != PT_LOAD
   6795 	      && (p->p_type != PT_NOTE
   6796 		  || bfd_get_format (abfd) != bfd_core))
   6797 	    {
   6798 	      /* A user specified segment layout may include a PHDR
   6799 		 segment that overlaps with a LOAD segment...  */
   6800 	      if (p->p_type == PT_PHDR)
   6801 		{
   6802 		  m->count = 0;
   6803 		  continue;
   6804 		}
   6805 
   6806 	      if (m->includes_filehdr || m->includes_phdrs)
   6807 		{
   6808 		  /* PR 17512: file: 2195325e.  */
   6809 		  _bfd_error_handler
   6810 		    (_("%pB: error: non-load segment %d includes file header "
   6811 		       "and/or program header"),
   6812 		     abfd, (int) (p - phdrs));
   6813 		  return false;
   6814 		}
   6815 
   6816 	      p->p_filesz = 0;
   6817 	      p->p_offset = m->sections[0]->filepos;
   6818 	      for (i = m->count; i-- != 0;)
   6819 		{
   6820 		  asection *sect = m->sections[i];
   6821 		  Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
   6822 		  if (hdr->sh_type != SHT_NOBITS)
   6823 		    {
   6824 		      p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
   6825 		      /* NB: p_memsz of the loadable PT_NOTE segment
   6826 			 should be the same as p_filesz.  */
   6827 		      if (p->p_type == PT_NOTE
   6828 			  && (hdr->sh_flags & SHF_ALLOC) != 0)
   6829 			p->p_memsz = p->p_filesz;
   6830 		      break;
   6831 		    }
   6832 		}
   6833 	    }
   6834 	}
   6835     }
   6836 
   6837   return true;
   6838 }
   6839 
   6840 static elf_section_list *
   6841 find_section_in_list (unsigned int i, elf_section_list * list)
   6842 {
   6843   for (;list != NULL; list = list->next)
   6844     if (list->ndx == i)
   6845       break;
   6846   return list;
   6847 }
   6848 
   6849 /* Work out the file positions of all the sections.  This is called by
   6850    _bfd_elf_compute_section_file_positions.  All the section sizes and
   6851    VMAs must be known before this is called.
   6852 
   6853    Reloc sections come in two flavours: Those processed specially as
   6854    "side-channel" data attached to a section to which they apply, and
   6855    those that bfd doesn't process as relocations.  The latter sort are
   6856    stored in a normal bfd section by bfd_section_from_shdr.  We don't
   6857    consider the former sort here, unless they form part of the loadable
   6858    image.  Reloc sections not assigned here (and compressed debugging
   6859    sections and CTF sections which nothing else in the file can rely
   6860    upon) will be handled later by assign_file_positions_for_relocs.
   6861 
   6862    We also don't set the positions of the .symtab and .strtab here.  */
   6863 
   6864 static bool
   6865 assign_file_positions_except_relocs (bfd *abfd,
   6866 				     struct bfd_link_info *link_info)
   6867 {
   6868   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   6869   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
   6870   elf_backend_data *bed = get_elf_backend_data (abfd);
   6871   unsigned int alloc;
   6872 
   6873   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
   6874       && bfd_get_format (abfd) != bfd_core)
   6875     {
   6876       Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
   6877       unsigned int num_sec = elf_numsections (abfd);
   6878       Elf_Internal_Shdr **hdrpp;
   6879       unsigned int i;
   6880       file_ptr off;
   6881 
   6882       /* Start after the ELF header.  */
   6883       off = i_ehdrp->e_ehsize;
   6884 
   6885       /* We are not creating an executable, which means that we are
   6886 	 not creating a program header, and that the actual order of
   6887 	 the sections in the file is unimportant.  */
   6888       for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
   6889 	{
   6890 	  Elf_Internal_Shdr *hdr;
   6891 
   6892 	  hdr = *hdrpp;
   6893 	  if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   6894 	       && hdr->bfd_section == NULL)
   6895 	      /* Do not assign offsets for these sections yet: we don't know
   6896 		 their sizes.  */
   6897 	      || (abfd->is_linker_output
   6898 		  && hdr->bfd_section != NULL
   6899 		  && (hdr->sh_name == -1u
   6900 		      || bfd_section_is_ctf (hdr->bfd_section)))
   6901 	      || i == elf_onesymtab (abfd)
   6902 	      || (elf_symtab_shndx_list (abfd) != NULL
   6903 		  && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   6904 	      || i == elf_strtab_sec (abfd)
   6905 	      || i == elf_shstrtab_sec (abfd))
   6906 	    {
   6907 	      hdr->sh_offset = -1;
   6908 	    }
   6909 	  else
   6910 	    /* There shouldn't be a need to effect "capped" file alignment here,
   6911 	       yet at least the Linux kernel's modpost utility was found to be
   6912 	       unhappy without.  While the issue was addressed there, let's be
   6913 	       kind for at least the foreseeable future ...  */
   6914 	    off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
   6915 							     bed->s->log_file_align);
   6916 	}
   6917 
   6918       elf_next_file_pos (abfd) = off;
   6919       elf_program_header_size (abfd) = 0;
   6920     }
   6921   else
   6922     {
   6923       /* Assign file positions for the loaded sections based on the
   6924 	 assignment of sections to segments.  */
   6925       if (!assign_file_positions_for_load_sections (abfd, link_info))
   6926 	return false;
   6927 
   6928       /* And for non-load sections.  */
   6929       if (!assign_file_positions_for_non_load_sections (abfd, link_info))
   6930 	return false;
   6931     }
   6932 
   6933   if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
   6934     return false;
   6935 
   6936   /* Write out the program headers.  */
   6937   alloc = i_ehdrp->e_phnum;
   6938   if (alloc != 0)
   6939     {
   6940       if (link_info != NULL && ! link_info->no_warn_rwx_segments)
   6941 	{
   6942 	  bool warned_tls = false;
   6943 	  bool warned_rwx = false;
   6944 
   6945 	  /* Memory resident segments with non-zero size and RWX
   6946 	     permissions are a security risk, so we generate a warning
   6947 	     here if we are creating any.  */
   6948 	  unsigned int i;
   6949 
   6950 	  for (i = 0; i < alloc; i++)
   6951 	    {
   6952 	      const Elf_Internal_Phdr * phdr = tdata->phdr + i;
   6953 
   6954 	      if (phdr->p_memsz == 0)
   6955 		continue;
   6956 
   6957 	      if (! warned_tls
   6958 		  && phdr->p_type == PT_TLS
   6959 		  && (phdr->p_flags & PF_X))
   6960 		{
   6961 		  if (link_info->warn_is_error_for_rwx_segments)
   6962 		    {
   6963 		      _bfd_error_handler (_("\
   6964 error: %pB has a TLS segment with execute permission"),
   6965 					  abfd);
   6966 		      return false;
   6967 		    }
   6968 
   6969 		  _bfd_error_handler (_("\
   6970 warning: %pB has a TLS segment with execute permission"),
   6971 				      abfd);
   6972 		  if (warned_rwx)
   6973 		    break;
   6974 
   6975 		  warned_tls = true;
   6976 		}
   6977 	      else if (! warned_rwx
   6978 		       && phdr->p_type == PT_LOAD
   6979 		       && ((phdr->p_flags & (PF_R | PF_W | PF_X))
   6980 			   == (PF_R | PF_W | PF_X)))
   6981 		{
   6982 		  if (link_info->warn_is_error_for_rwx_segments)
   6983 		    {
   6984 		      _bfd_error_handler (_("\
   6985 error: %pB has a LOAD segment with RWX permissions"),
   6986 					  abfd);
   6987 		      return false;
   6988 		    }
   6989 
   6990 		  _bfd_error_handler (_("\
   6991 warning: %pB has a LOAD segment with RWX permissions"),
   6992 				      abfd);
   6993 		  if (warned_tls)
   6994 		    break;
   6995 
   6996 		  warned_rwx = true;
   6997 		}
   6998 	    }
   6999 	}
   7000 
   7001       if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
   7002 	  || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
   7003 	return false;
   7004     }
   7005 
   7006   return true;
   7007 }
   7008 
   7009 bool
   7010 _bfd_elf_init_file_header (bfd *abfd,
   7011 			   struct bfd_link_info *info ATTRIBUTE_UNUSED)
   7012 {
   7013   Elf_Internal_Ehdr *i_ehdrp;	/* Elf file header, internal form.  */
   7014   struct elf_strtab_hash *shstrtab;
   7015   elf_backend_data *bed = get_elf_backend_data (abfd);
   7016 
   7017   i_ehdrp = elf_elfheader (abfd);
   7018 
   7019   shstrtab = _bfd_elf_strtab_init ();
   7020   if (shstrtab == NULL)
   7021     return false;
   7022 
   7023   elf_shstrtab (abfd) = shstrtab;
   7024 
   7025   i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
   7026   i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
   7027   i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
   7028   i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
   7029 
   7030   i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
   7031   i_ehdrp->e_ident[EI_DATA] =
   7032     bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
   7033   i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
   7034 
   7035   if ((abfd->flags & DYNAMIC) != 0)
   7036     i_ehdrp->e_type = ET_DYN;
   7037   else if ((abfd->flags & EXEC_P) != 0)
   7038     i_ehdrp->e_type = ET_EXEC;
   7039   else if (bfd_get_format (abfd) == bfd_core)
   7040     i_ehdrp->e_type = ET_CORE;
   7041   else
   7042     i_ehdrp->e_type = ET_REL;
   7043 
   7044   switch (bfd_get_arch (abfd))
   7045     {
   7046     case bfd_arch_unknown:
   7047       i_ehdrp->e_machine = EM_NONE;
   7048       break;
   7049 
   7050       /* There used to be a long list of cases here, each one setting
   7051 	 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
   7052 	 in the corresponding bfd definition.  To avoid duplication,
   7053 	 the switch was removed.  Machines that need special handling
   7054 	 can generally do it in elf_backend_final_write_processing(),
   7055 	 unless they need the information earlier than the final write.
   7056 	 Such need can generally be supplied by replacing the tests for
   7057 	 e_machine with the conditions used to determine it.  */
   7058     default:
   7059       i_ehdrp->e_machine = bed->elf_machine_code;
   7060     }
   7061 
   7062   i_ehdrp->e_version = bed->s->ev_current;
   7063   i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
   7064 
   7065   /* No program header, for now.  */
   7066   i_ehdrp->e_phoff = 0;
   7067   i_ehdrp->e_phentsize = 0;
   7068   i_ehdrp->e_phnum = 0;
   7069 
   7070   /* Each bfd section is section header entry.  */
   7071   i_ehdrp->e_entry = bfd_get_start_address (abfd);
   7072   i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
   7073 
   7074   elf_tdata (abfd)->symtab_hdr.sh_name =
   7075     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
   7076   elf_tdata (abfd)->strtab_hdr.sh_name =
   7077     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
   7078   elf_tdata (abfd)->shstrtab_hdr.sh_name =
   7079     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
   7080   if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
   7081       || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
   7082       || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
   7083     return false;
   7084 
   7085   return true;
   7086 }
   7087 
   7088 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
   7089 
   7090    FIXME: We used to have code here to sort the PT_LOAD segments into
   7091    ascending order, as per the ELF spec.  But this breaks some programs,
   7092    including the Linux kernel.  But really either the spec should be
   7093    changed or the programs updated.  */
   7094 
   7095 bool
   7096 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
   7097 {
   7098   if (link_info != NULL && bfd_link_pie (link_info))
   7099     {
   7100       Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
   7101       unsigned int num_segments = i_ehdrp->e_phnum;
   7102       struct elf_obj_tdata *tdata = elf_tdata (obfd);
   7103       Elf_Internal_Phdr *segment = tdata->phdr;
   7104       Elf_Internal_Phdr *end_segment = &segment[num_segments];
   7105 
   7106       /* Find the lowest p_vaddr in PT_LOAD segments.  */
   7107       bfd_vma p_vaddr = (bfd_vma) -1;
   7108       for (; segment < end_segment; segment++)
   7109 	if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
   7110 	  p_vaddr = segment->p_vaddr;
   7111 
   7112       /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
   7113 	 segments is non-zero.  */
   7114       if (p_vaddr)
   7115 	i_ehdrp->e_type = ET_EXEC;
   7116     }
   7117   return true;
   7118 }
   7119 
   7120 /* Assign file positions for all the reloc sections which are not part
   7121    of the loadable file image, and the file position of section headers.  */
   7122 
   7123 static bool
   7124 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
   7125 {
   7126   file_ptr off;
   7127   Elf_Internal_Shdr **shdrpp, **end_shdrpp;
   7128   Elf_Internal_Shdr *shdrp;
   7129   Elf_Internal_Ehdr *i_ehdrp;
   7130   elf_backend_data *bed = get_elf_backend_data (abfd);
   7131 
   7132   /* Skip non-load sections without section header.  */
   7133   if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
   7134     return true;
   7135 
   7136   off = elf_next_file_pos (abfd);
   7137 
   7138   shdrpp = elf_elfsections (abfd);
   7139   end_shdrpp = shdrpp + elf_numsections (abfd);
   7140   for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
   7141     {
   7142       shdrp = *shdrpp;
   7143       if (shdrp->sh_offset == -1)
   7144 	{
   7145 	  asection *sec = shdrp->bfd_section;
   7146 	  if (sec == NULL
   7147 	      || shdrp->sh_type == SHT_REL
   7148 	      || shdrp->sh_type == SHT_RELA)
   7149 	    ;
   7150 	  else if (bfd_section_is_ctf (sec))
   7151 	    {
   7152 	      /* Update section size and contents.	*/
   7153 	      shdrp->sh_size = sec->size;
   7154 	      shdrp->contents = sec->contents;
   7155 	    }
   7156 	  else if (shdrp->sh_name == -1u)
   7157 	    {
   7158 	      const char *name = sec->name;
   7159 	      struct bfd_elf_section_data *d;
   7160 
   7161 	      /* Compress DWARF debug sections.  */
   7162 	      if (!bfd_compress_section (abfd, sec, shdrp->contents))
   7163 		return false;
   7164 
   7165 	      if (sec->compress_status == COMPRESS_SECTION_DONE
   7166 		  && (abfd->flags & BFD_COMPRESS_GABI) == 0
   7167 		  && name[1] == 'd')
   7168 		{
   7169 		  /* If section is compressed with zlib-gnu, convert
   7170 		     section name from .debug_* to .zdebug_*.  */
   7171 		  char *new_name = bfd_debug_name_to_zdebug (abfd, name);
   7172 		  if (new_name == NULL)
   7173 		    return false;
   7174 		  name = new_name;
   7175 		}
   7176 	      /* Add section name to section name section.  */
   7177 	      shdrp->sh_name
   7178 		= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   7179 						      name, false);
   7180 	      d = elf_section_data (sec);
   7181 
   7182 	      /* Add reloc section name to section name section.  */
   7183 	      if (d->rel.hdr
   7184 		  && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
   7185 						  name, false))
   7186 		return false;
   7187 	      if (d->rela.hdr
   7188 		  && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
   7189 						  name, true))
   7190 		return false;
   7191 
   7192 	      /* Update section size and contents.  */
   7193 	      shdrp->sh_size = sec->size;
   7194 	      shdrp->contents = sec->contents;
   7195 	      sec->contents = NULL;
   7196 	    }
   7197 
   7198 	  off = _bfd_elf_assign_file_position_for_section (shdrp, off,
   7199 		  (abfd->flags & (EXEC_P | DYNAMIC))
   7200 		  || bfd_get_format (abfd) == bfd_core,
   7201 		  bed->s->log_file_align);
   7202 	}
   7203     }
   7204 
   7205   /* Place section name section after DWARF debug sections have been
   7206      compressed.  */
   7207   _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
   7208   shdrp = &elf_tdata (abfd)->shstrtab_hdr;
   7209   shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
   7210   off = _bfd_elf_assign_file_position_for_section (shdrp, off, true, 0);
   7211 
   7212   /* Place the section headers.  */
   7213   i_ehdrp = elf_elfheader (abfd);
   7214   off = BFD_ALIGN (off, 1u << bed->s->log_file_align);
   7215   i_ehdrp->e_shoff = off;
   7216   off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
   7217   elf_next_file_pos (abfd) = off;
   7218 
   7219   return true;
   7220 }
   7221 
   7222 bool
   7223 _bfd_elf_write_object_contents (bfd *abfd)
   7224 {
   7225   elf_backend_data *bed = get_elf_backend_data (abfd);
   7226   Elf_Internal_Shdr **i_shdrp;
   7227   bool failed;
   7228   unsigned int count, num_sec;
   7229   struct elf_obj_tdata *t;
   7230 
   7231   if (! abfd->output_has_begun
   7232       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   7233     return false;
   7234   /* Do not rewrite ELF data when the BFD has been opened for update.
   7235      abfd->output_has_begun was set to TRUE on opening, so creation of
   7236      new sections, and modification of existing section sizes was
   7237      restricted.  This means the ELF header, program headers and
   7238      section headers can't have changed.  If the contents of any
   7239      sections has been modified, then those changes have already been
   7240      written to the BFD.  */
   7241   else if (abfd->direction == both_direction)
   7242     {
   7243       BFD_ASSERT (abfd->output_has_begun);
   7244       return true;
   7245     }
   7246 
   7247   i_shdrp = elf_elfsections (abfd);
   7248 
   7249   failed = false;
   7250   bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
   7251   if (failed)
   7252     return false;
   7253 
   7254   if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
   7255     return false;
   7256 
   7257   /* After writing the headers, we need to write the sections too...  */
   7258   num_sec = elf_numsections (abfd);
   7259   for (count = 1; count < num_sec; count++)
   7260     {
   7261       /* Don't set the sh_name field without section header.  */
   7262       if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
   7263 	i_shdrp[count]->sh_name
   7264 	  = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
   7265 				    i_shdrp[count]->sh_name);
   7266       if (bed->elf_backend_section_processing)
   7267 	if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
   7268 	  return false;
   7269       if (i_shdrp[count]->contents)
   7270 	{
   7271 	  bfd_size_type amt = i_shdrp[count]->sh_size;
   7272 
   7273 	  if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
   7274 	      || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
   7275 	    return false;
   7276 	}
   7277     }
   7278 
   7279   /* Write out the section header names.  */
   7280   t = elf_tdata (abfd);
   7281   if (elf_shstrtab (abfd) != NULL
   7282       && t->shstrtab_hdr.sh_offset != -1
   7283       && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
   7284 	  || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
   7285     return false;
   7286 
   7287   if (!(*bed->elf_backend_final_write_processing) (abfd))
   7288     return false;
   7289 
   7290   if (!bed->s->write_shdrs_and_ehdr (abfd))
   7291     return false;
   7292 
   7293   /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
   7294   if (t->o->build_id.after_write_object_contents != NULL
   7295       && !(*t->o->build_id.after_write_object_contents) (abfd))
   7296     return false;
   7297   if (t->o->package_metadata.after_write_object_contents != NULL
   7298       && !(*t->o->package_metadata.after_write_object_contents) (abfd))
   7299     return false;
   7300 
   7301   return true;
   7302 }
   7303 
   7304 bool
   7305 _bfd_elf_write_corefile_contents (bfd *abfd)
   7306 {
   7307   /* Hopefully this can be done just like an object file.  */
   7308   return _bfd_elf_write_object_contents (abfd);
   7309 }
   7310 
   7311 /* Given a section, search the header to find them.  */
   7312 
   7313 unsigned int
   7314 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
   7315 {
   7316   elf_backend_data *bed;
   7317   unsigned int sec_index;
   7318 
   7319   if (elf_section_data (asect) != NULL
   7320       && elf_section_data (asect)->this_idx != 0)
   7321     return elf_section_data (asect)->this_idx;
   7322 
   7323   if (bfd_is_abs_section (asect))
   7324     sec_index = SHN_ABS;
   7325   else if (bfd_is_com_section (asect))
   7326     sec_index = SHN_COMMON;
   7327   else if (bfd_is_und_section (asect))
   7328     sec_index = SHN_UNDEF;
   7329   else
   7330     sec_index = SHN_BAD;
   7331 
   7332   bed = get_elf_backend_data (abfd);
   7333   if (bed->elf_backend_section_from_bfd_section)
   7334     {
   7335       int retval = sec_index;
   7336 
   7337       if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
   7338 	return retval;
   7339     }
   7340 
   7341   if (sec_index == SHN_BAD)
   7342     bfd_set_error (bfd_error_nonrepresentable_section);
   7343 
   7344   return sec_index;
   7345 }
   7346 
   7347 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
   7348    on error.  */
   7349 
   7350 int
   7351 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
   7352 {
   7353   asymbol *asym_ptr = *asym_ptr_ptr;
   7354   int idx;
   7355   flagword flags = asym_ptr->flags;
   7356 
   7357   /* When gas creates relocations against local labels, it creates its
   7358      own symbol for the section, but does put the symbol into the
   7359      symbol chain, so udata is 0.  When the linker is generating
   7360      relocatable output, this section symbol may be for one of the
   7361      input sections rather than the output section.  */
   7362   if (asym_ptr->udata.i == 0
   7363       && (flags & BSF_SECTION_SYM)
   7364       && asym_ptr->section)
   7365     {
   7366       asection *sec;
   7367 
   7368       sec = asym_ptr->section;
   7369       if (sec->owner != abfd && sec->output_section != NULL)
   7370 	sec = sec->output_section;
   7371       if (sec->owner == abfd
   7372 	  && sec->index < elf_num_section_syms (abfd)
   7373 	  && elf_section_syms (abfd)[sec->index] != NULL)
   7374 	asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
   7375     }
   7376 
   7377   idx = asym_ptr->udata.i;
   7378 
   7379   if (idx == 0)
   7380     {
   7381       /* This case can occur when using --strip-symbol on a symbol
   7382 	 which is used in a relocation entry.  */
   7383       _bfd_error_handler
   7384 	/* xgettext:c-format */
   7385 	(_("%pB: symbol `%s' required but not present"),
   7386 	 abfd, bfd_asymbol_name (asym_ptr));
   7387       bfd_set_error (bfd_error_no_symbols);
   7388       return -1;
   7389     }
   7390 
   7391 #if DEBUG & 4
   7392   {
   7393     fprintf (stderr,
   7394 	     "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
   7395 	     " flags = 0x%.8x\n",
   7396 	     (long) asym_ptr, asym_ptr->name, idx, flags);
   7397     fflush (stderr);
   7398   }
   7399 #endif
   7400 
   7401   return idx;
   7402 }
   7403 
   7404 static inline bfd_vma
   7405 segment_size (Elf_Internal_Phdr *segment)
   7406 {
   7407   return (segment->p_memsz > segment->p_filesz
   7408 	  ? segment->p_memsz : segment->p_filesz);
   7409 }
   7410 
   7411 
   7412 /* Returns the end address of the segment + 1.  */
   7413 static inline bfd_vma
   7414 segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
   7415 {
   7416   return start + segment_size (segment);
   7417 }
   7418 
   7419 static inline bfd_size_type
   7420 section_size (asection *section, Elf_Internal_Phdr *segment)
   7421 {
   7422   if ((section->flags & SEC_HAS_CONTENTS) != 0
   7423       || (section->flags & SEC_THREAD_LOCAL) == 0
   7424       || segment->p_type == PT_TLS)
   7425     return section->size;
   7426   return 0;
   7427 }
   7428 
   7429 /* Returns TRUE if the given section is contained within the given
   7430    segment.  LMA addresses are compared against PADDR when
   7431    USE_VADDR is false, VMA against VADDR when true.  */
   7432 static bool
   7433 is_contained_by (asection *section, Elf_Internal_Phdr *segment,
   7434 		 bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
   7435 		 bool use_vaddr)
   7436 {
   7437   bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
   7438   bfd_vma addr = !use_vaddr ? section->lma : section->vma;
   7439   bfd_vma octet;
   7440   if (_bfd_mul_overflow (addr, opb, &octet))
   7441     return false;
   7442   /* The third and fourth lines below are testing that the section end
   7443      address is within the segment.  It's written this way to avoid
   7444      overflow.  Add seg_addr + section_size to both sides of the
   7445      inequality to make it obvious.  */
   7446   return (octet >= seg_addr
   7447 	  && segment_size (segment) >= section_size (section, segment)
   7448 	  && (octet - seg_addr
   7449 	      <= segment_size (segment) - section_size (section, segment)));
   7450 }
   7451 
   7452 /* Handle PT_NOTE segment.  */
   7453 static bool
   7454 is_note (asection *s, Elf_Internal_Phdr *p)
   7455 {
   7456   return (p->p_type == PT_NOTE
   7457 	  && elf_section_type (s) == SHT_NOTE
   7458 	  && (ufile_ptr) s->filepos >= p->p_offset
   7459 	  && p->p_filesz >= s->size
   7460 	  && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
   7461 }
   7462 
   7463 /* Rewrite program header information.  */
   7464 
   7465 static bool
   7466 rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
   7467 {
   7468   Elf_Internal_Ehdr *iehdr;
   7469   struct elf_segment_map *map;
   7470   struct elf_segment_map *map_first;
   7471   struct elf_segment_map **pointer_to_map;
   7472   Elf_Internal_Phdr *segment;
   7473   asection *section;
   7474   unsigned int i;
   7475   unsigned int num_segments;
   7476   bool phdr_included = false;
   7477   bool p_paddr_valid;
   7478   struct elf_segment_map *phdr_adjust_seg = NULL;
   7479   unsigned int phdr_adjust_num = 0;
   7480   elf_backend_data *bed;
   7481   unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
   7482 
   7483   bed = get_elf_backend_data (ibfd);
   7484   iehdr = elf_elfheader (ibfd);
   7485 
   7486   map_first = NULL;
   7487   pointer_to_map = &map_first;
   7488 
   7489   num_segments = elf_elfheader (ibfd)->e_phnum;
   7490 
   7491   /* The complicated case when p_vaddr is 0 is to handle the Solaris
   7492      linker, which generates a PT_INTERP section with p_vaddr and
   7493      p_memsz set to 0.  */
   7494 #define IS_SOLARIS_PT_INTERP(p, s)					\
   7495   (p->p_vaddr == 0							\
   7496    && p->p_paddr == 0							\
   7497    && p->p_memsz == 0							\
   7498    && p->p_filesz > 0							\
   7499    && (s->flags & SEC_HAS_CONTENTS) != 0				\
   7500    && s->size > 0							\
   7501    && (bfd_vma) s->filepos >= p->p_offset				\
   7502    && ((bfd_vma) s->filepos + s->size					\
   7503        <= p->p_offset + p->p_filesz))
   7504 
   7505   /* Decide if the given section should be included in the given segment.
   7506      A section will be included if:
   7507        1. It is within the address space of the segment -- we use the LMA
   7508 	  if that is set for the segment and the VMA otherwise,
   7509        2. It is an allocated section or a NOTE section in a PT_NOTE
   7510 	  segment.
   7511        3. There is an output section associated with it,
   7512        4. The section has not already been allocated to a previous segment.
   7513        5. PT_GNU_STACK segments do not include any sections.
   7514        6. PT_TLS segment includes only SHF_TLS sections.
   7515        7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
   7516        8. PT_DYNAMIC should not contain empty sections at the beginning
   7517 	  (with the possible exception of .dynamic).  */
   7518 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid)	\
   7519   (((is_contained_by (section, segment, segment->p_paddr,		\
   7520 		      segment->p_vaddr, opb, !paddr_valid)		\
   7521      && (section->flags & SEC_ALLOC) != 0)				\
   7522     || is_note (section, segment))					\
   7523    && segment->p_type != PT_GNU_STACK					\
   7524    && (segment->p_type != PT_TLS					\
   7525        || (section->flags & SEC_THREAD_LOCAL))				\
   7526    && (segment->p_type == PT_LOAD					\
   7527        || segment->p_type == PT_TLS					\
   7528        || (section->flags & SEC_THREAD_LOCAL) == 0)			\
   7529    && (segment->p_type != PT_DYNAMIC					\
   7530        || section_size (section, segment) > 0				\
   7531        || (segment->p_paddr						\
   7532 	   ? segment->p_paddr != section->lma * (opb)			\
   7533 	   : segment->p_vaddr != section->vma * (opb))			\
   7534        || (streq (bfd_section_name (section), ".dynamic")))		\
   7535    && (segment->p_type != PT_LOAD || !section->segment_mark))
   7536 
   7537 /* If the output section of a section in the input segment is NULL,
   7538    it is removed from the corresponding output segment.   */
   7539 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid)	\
   7540   (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid)	\
   7541    && section->output_section != NULL)
   7542 
   7543   /* Returns TRUE iff seg1 starts after the end of seg2.  */
   7544 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)			\
   7545   (seg1->field >= segment_end (seg2, seg2->field))
   7546 
   7547   /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
   7548      their VMA address ranges and their LMA address ranges overlap.
   7549      It is possible to have overlapping VMA ranges without overlapping LMA
   7550      ranges.  RedBoot images for example can have both .data and .bss mapped
   7551      to the same VMA range, but with the .data section mapped to a different
   7552      LMA.  */
   7553 #define SEGMENT_OVERLAPS(seg1, seg2)					\
   7554   (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)			\
   7555 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))			\
   7556    && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)			\
   7557 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
   7558 
   7559   /* Initialise the segment mark field, and discard stupid alignment.  */
   7560   for (section = ibfd->sections; section != NULL; section = section->next)
   7561     {
   7562       asection *o = section->output_section;
   7563       if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
   7564 	o->alignment_power = 0;
   7565       section->segment_mark = false;
   7566     }
   7567 
   7568   /* The Solaris linker creates program headers in which all the
   7569      p_paddr fields are zero.  When we try to objcopy or strip such a
   7570      file, we get confused.  Check for this case, and if we find it
   7571      don't set the p_paddr_valid fields.  */
   7572   p_paddr_valid = false;
   7573   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7574        i < num_segments;
   7575        i++, segment++)
   7576     if (segment->p_paddr != 0)
   7577       {
   7578 	p_paddr_valid = true;
   7579 	break;
   7580       }
   7581 
   7582   /* Scan through the segments specified in the program header
   7583      of the input BFD.  For this first scan we look for overlaps
   7584      in the loadable segments.  These can be created by weird
   7585      parameters to objcopy.  Also, fix some solaris weirdness.  */
   7586   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7587        i < num_segments;
   7588        i++, segment++)
   7589     {
   7590       unsigned int j;
   7591       Elf_Internal_Phdr *segment2;
   7592 
   7593       if (segment->p_type == PT_INTERP)
   7594 	for (section = ibfd->sections; section; section = section->next)
   7595 	  if (IS_SOLARIS_PT_INTERP (segment, section))
   7596 	    {
   7597 	      /* Mininal change so that the normal section to segment
   7598 		 assignment code will work.  */
   7599 	      segment->p_vaddr = section->vma * opb;
   7600 	      break;
   7601 	    }
   7602 
   7603       if (segment->p_type != PT_LOAD)
   7604 	{
   7605 	  /* Remove PT_GNU_RELRO segment.  */
   7606 	  if (segment->p_type == PT_GNU_RELRO)
   7607 	    segment->p_type = PT_NULL;
   7608 	  continue;
   7609 	}
   7610 
   7611       /* Determine if this segment overlaps any previous segments.  */
   7612       for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
   7613 	{
   7614 	  bfd_signed_vma extra_length;
   7615 
   7616 	  if (segment2->p_type != PT_LOAD
   7617 	      || !SEGMENT_OVERLAPS (segment, segment2))
   7618 	    continue;
   7619 
   7620 	  /* Merge the two segments together.  */
   7621 	  if (segment2->p_vaddr < segment->p_vaddr)
   7622 	    {
   7623 	      /* Extend SEGMENT2 to include SEGMENT and then delete
   7624 		 SEGMENT.  */
   7625 	      extra_length = (segment_end (segment, segment->p_vaddr)
   7626 			      - segment_end (segment2, segment2->p_vaddr));
   7627 
   7628 	      if (extra_length > 0)
   7629 		{
   7630 		  segment2->p_memsz += extra_length;
   7631 		  segment2->p_filesz += extra_length;
   7632 		}
   7633 
   7634 	      segment->p_type = PT_NULL;
   7635 
   7636 	      /* Since we have deleted P we must restart the outer loop.  */
   7637 	      i = 0;
   7638 	      segment = elf_tdata (ibfd)->phdr;
   7639 	      break;
   7640 	    }
   7641 	  else
   7642 	    {
   7643 	      /* Extend SEGMENT to include SEGMENT2 and then delete
   7644 		 SEGMENT2.  */
   7645 	      extra_length = (segment_end (segment2, segment2->p_vaddr)
   7646 			      - segment_end (segment, segment->p_vaddr));
   7647 
   7648 	      if (extra_length > 0)
   7649 		{
   7650 		  segment->p_memsz += extra_length;
   7651 		  segment->p_filesz += extra_length;
   7652 		}
   7653 
   7654 	      segment2->p_type = PT_NULL;
   7655 	    }
   7656 	}
   7657     }
   7658 
   7659   /* The second scan attempts to assign sections to segments.  */
   7660   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7661        i < num_segments;
   7662        i++, segment++)
   7663     {
   7664       unsigned int section_count;
   7665       asection **sections;
   7666       asection *output_section;
   7667       unsigned int isec;
   7668       asection *matching_lma;
   7669       asection *suggested_lma;
   7670       unsigned int j;
   7671       size_t amt;
   7672       asection *first_section;
   7673 
   7674       if (segment->p_type == PT_NULL)
   7675 	continue;
   7676 
   7677       first_section = NULL;
   7678       /* Compute how many sections might be placed into this segment.  */
   7679       for (section = ibfd->sections, section_count = 0;
   7680 	   section != NULL;
   7681 	   section = section->next)
   7682 	{
   7683 	  /* Find the first section in the input segment, which may be
   7684 	     removed from the corresponding output segment.   */
   7685 	  if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, p_paddr_valid))
   7686 	    {
   7687 	      if (first_section == NULL)
   7688 		first_section = section;
   7689 	      if (section->output_section != NULL)
   7690 		++section_count;
   7691 	    }
   7692 	}
   7693 
   7694       /* Allocate a segment map big enough to contain
   7695 	 all of the sections we have selected.  */
   7696       amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   7697       amt += section_count * sizeof (asection *);
   7698       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7699       if (map == NULL)
   7700 	return false;
   7701 
   7702       /* Initialise the fields of the segment map.  Default to
   7703 	 using the physical address of the segment in the input BFD.  */
   7704       map->next = NULL;
   7705       map->p_type = segment->p_type;
   7706       map->p_flags = segment->p_flags;
   7707       map->p_flags_valid = 1;
   7708 
   7709       if (map->p_type == PT_LOAD
   7710 	  && (ibfd->flags & D_PAGED) != 0
   7711 	  && maxpagesize > 1
   7712 	  && segment->p_align > 1)
   7713 	{
   7714 	  map->p_align = segment->p_align;
   7715 	  if (segment->p_align > maxpagesize)
   7716 	    map->p_align = maxpagesize;
   7717 	  map->p_align_valid = 1;
   7718 	}
   7719 
   7720       /* If the first section in the input segment is removed, there is
   7721 	 no need to preserve segment physical address in the corresponding
   7722 	 output segment.  */
   7723       if (!first_section || first_section->output_section != NULL)
   7724 	{
   7725 	  map->p_paddr = segment->p_paddr;
   7726 	  map->p_paddr_valid = p_paddr_valid;
   7727 	}
   7728 
   7729       /* Determine if this segment contains the ELF file header
   7730 	 and if it contains the program headers themselves.  */
   7731       map->includes_filehdr = (segment->p_offset == 0
   7732 			       && segment->p_filesz >= iehdr->e_ehsize);
   7733       map->includes_phdrs = 0;
   7734 
   7735       if (!phdr_included || segment->p_type != PT_LOAD)
   7736 	{
   7737 	  map->includes_phdrs =
   7738 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   7739 	     && (segment->p_offset + segment->p_filesz
   7740 		 >= ((bfd_vma) iehdr->e_phoff
   7741 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   7742 
   7743 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   7744 	    phdr_included = true;
   7745 	}
   7746 
   7747       if (section_count == 0)
   7748 	{
   7749 	  /* Special segments, such as the PT_PHDR segment, may contain
   7750 	     no sections, but ordinary, loadable segments should contain
   7751 	     something.  They are allowed by the ELF spec however, so only
   7752 	     a warning is produced.
   7753 	     Don't warn if an empty PT_LOAD contains the program headers.
   7754 	     There is however the valid use case of embedded systems which
   7755 	     have segments with p_filesz of 0 and a p_memsz > 0 to initialize
   7756 	     flash memory with zeros.  No warning is shown for that case.  */
   7757 	  if (segment->p_type == PT_LOAD
   7758 	      && !map->includes_phdrs
   7759 	      && (segment->p_filesz > 0 || segment->p_memsz == 0))
   7760 	    /* xgettext:c-format */
   7761 	    _bfd_error_handler
   7762 	      (_("%pB: warning: empty loadable segment detected"
   7763 		 " at vaddr=%#" PRIx64 ", is this intentional?"),
   7764 	       ibfd, (uint64_t) segment->p_vaddr);
   7765 
   7766 	  map->p_vaddr_offset = segment->p_vaddr / opb;
   7767 	  map->count = 0;
   7768 	  *pointer_to_map = map;
   7769 	  pointer_to_map = &map->next;
   7770 
   7771 	  continue;
   7772 	}
   7773 
   7774       /* Now scan the sections in the input BFD again and attempt
   7775 	 to add their corresponding output sections to the segment map.
   7776 	 The problem here is how to handle an output section which has
   7777 	 been moved (ie had its LMA changed).  There are four possibilities:
   7778 
   7779 	 1. None of the sections have been moved.
   7780 	    In this case we can continue to use the segment LMA from the
   7781 	    input BFD.
   7782 
   7783 	 2. All of the sections have been moved by the same amount.
   7784 	    In this case we can change the segment's LMA to match the LMA
   7785 	    of the first section.
   7786 
   7787 	 3. Some of the sections have been moved, others have not.
   7788 	    In this case those sections which have not been moved can be
   7789 	    placed in the current segment which will have to have its size,
   7790 	    and possibly its LMA changed, and a new segment or segments will
   7791 	    have to be created to contain the other sections.
   7792 
   7793 	 4. The sections have been moved, but not by the same amount.
   7794 	    In this case we can change the segment's LMA to match the LMA
   7795 	    of the first section and we will have to create a new segment
   7796 	    or segments to contain the other sections.
   7797 
   7798 	 In order to save time, we allocate an array to hold the section
   7799 	 pointers that we are interested in.  As these sections get assigned
   7800 	 to a segment, they are removed from this array.  */
   7801 
   7802       amt = section_count * sizeof (asection *);
   7803       sections = (asection **) bfd_malloc (amt);
   7804       if (sections == NULL)
   7805 	return false;
   7806 
   7807       /* Step One: Scan for segment vs section LMA conflicts.
   7808 	 Also add the sections to the section array allocated above.
   7809 	 Also add the sections to the current segment.  In the common
   7810 	 case, where the sections have not been moved, this means that
   7811 	 we have completely filled the segment, and there is nothing
   7812 	 more to do.  */
   7813       isec = 0;
   7814       matching_lma = NULL;
   7815       suggested_lma = NULL;
   7816 
   7817       for (section = first_section, j = 0;
   7818 	   section != NULL;
   7819 	   section = section->next)
   7820 	{
   7821 	  if (INCLUDE_SECTION_IN_SEGMENT (section, segment, opb, p_paddr_valid))
   7822 	    {
   7823 	      output_section = section->output_section;
   7824 
   7825 	      sections[j++] = section;
   7826 
   7827 	      /* The Solaris native linker always sets p_paddr to 0.
   7828 		 We try to catch that case here, and set it to the
   7829 		 correct value.  Note - some backends require that
   7830 		 p_paddr be left as zero.  */
   7831 	      if (!p_paddr_valid
   7832 		  && segment->p_vaddr != 0
   7833 		  && !bed->want_p_paddr_set_to_zero
   7834 		  && isec == 0
   7835 		  && output_section->lma != 0
   7836 		  && (align_power (segment->p_vaddr
   7837 				   + (map->includes_filehdr
   7838 				      ? iehdr->e_ehsize : 0)
   7839 				   + (map->includes_phdrs
   7840 				      ? iehdr->e_phnum * iehdr->e_phentsize
   7841 				      : 0),
   7842 				   output_section->alignment_power * opb)
   7843 		      == (output_section->vma * opb)))
   7844 		map->p_paddr = segment->p_vaddr;
   7845 
   7846 	      /* Match up the physical address of the segment with the
   7847 		 LMA address of the output section.  */
   7848 	      if (is_contained_by (output_section, segment, map->p_paddr,
   7849 				   0, opb, false)
   7850 		  || is_note (section, segment))
   7851 		{
   7852 		  if (matching_lma == NULL
   7853 		      || output_section->lma < matching_lma->lma)
   7854 		    matching_lma = output_section;
   7855 
   7856 		  /* We assume that if the section fits within the segment
   7857 		     then it does not overlap any other section within that
   7858 		     segment.  */
   7859 		  map->sections[isec++] = output_section;
   7860 		}
   7861 	      else if (suggested_lma == NULL)
   7862 		suggested_lma = output_section;
   7863 
   7864 	      if (j == section_count)
   7865 		break;
   7866 	    }
   7867 	}
   7868 
   7869       BFD_ASSERT (j == section_count);
   7870 
   7871       /* Step Two: Adjust the physical address of the current segment,
   7872 	 if necessary.  */
   7873       if (isec == section_count)
   7874 	{
   7875 	  /* All of the sections fitted within the segment as currently
   7876 	     specified.  This is the default case.  Add the segment to
   7877 	     the list of built segments and carry on to process the next
   7878 	     program header in the input BFD.  */
   7879 	  map->count = section_count;
   7880 	  *pointer_to_map = map;
   7881 	  pointer_to_map = &map->next;
   7882 
   7883 	  if (p_paddr_valid
   7884 	      && !bed->want_p_paddr_set_to_zero)
   7885 	    {
   7886 	      bfd_vma hdr_size = 0;
   7887 	      if (map->includes_filehdr)
   7888 		hdr_size = iehdr->e_ehsize;
   7889 	      if (map->includes_phdrs)
   7890 		hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
   7891 
   7892 	      /* Account for padding before the first section in the
   7893 		 segment.  */
   7894 	      map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
   7895 				     - matching_lma->lma);
   7896 	    }
   7897 
   7898 	  free (sections);
   7899 	  continue;
   7900 	}
   7901       else
   7902 	{
   7903 	  /* Change the current segment's physical address to match
   7904 	     the LMA of the first section that fitted, or if no
   7905 	     section fitted, the first section.  */
   7906 	  if (matching_lma == NULL)
   7907 	    matching_lma = suggested_lma;
   7908 
   7909 	  map->p_paddr = matching_lma->lma * opb;
   7910 
   7911 	  /* Offset the segment physical address from the lma
   7912 	     to allow for space taken up by elf headers.  */
   7913 	  if (map->includes_phdrs)
   7914 	    {
   7915 	      map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
   7916 
   7917 	      /* iehdr->e_phnum is just an estimate of the number
   7918 		 of program headers that we will need.  Make a note
   7919 		 here of the number we used and the segment we chose
   7920 		 to hold these headers, so that we can adjust the
   7921 		 offset when we know the correct value.  */
   7922 	      phdr_adjust_num = iehdr->e_phnum;
   7923 	      phdr_adjust_seg = map;
   7924 	    }
   7925 
   7926 	  if (map->includes_filehdr)
   7927 	    {
   7928 	      bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
   7929 	      map->p_paddr -= iehdr->e_ehsize;
   7930 	      /* We've subtracted off the size of headers from the
   7931 		 first section lma, but there may have been some
   7932 		 alignment padding before that section too.  Try to
   7933 		 account for that by adjusting the segment lma down to
   7934 		 the same alignment.  */
   7935 	      if (segment->p_align != 0 && segment->p_align < align)
   7936 		align = segment->p_align;
   7937 	      map->p_paddr &= -(align * opb);
   7938 	    }
   7939 	}
   7940 
   7941       /* Step Three: Loop over the sections again, this time assigning
   7942 	 those that fit to the current segment and removing them from the
   7943 	 sections array; but making sure not to leave large gaps.  Once all
   7944 	 possible sections have been assigned to the current segment it is
   7945 	 added to the list of built segments and if sections still remain
   7946 	 to be assigned, a new segment is constructed before repeating
   7947 	 the loop.  */
   7948       isec = 0;
   7949       do
   7950 	{
   7951 	  map->count = 0;
   7952 	  suggested_lma = NULL;
   7953 
   7954 	  /* Fill the current segment with sections that fit.  */
   7955 	  for (j = 0; j < section_count; j++)
   7956 	    {
   7957 	      section = sections[j];
   7958 
   7959 	      if (section == NULL)
   7960 		continue;
   7961 
   7962 	      output_section = section->output_section;
   7963 
   7964 	      BFD_ASSERT (output_section != NULL);
   7965 
   7966 	      if (is_contained_by (output_section, segment, map->p_paddr,
   7967 				   0, opb, false)
   7968 		  || is_note (section, segment))
   7969 		{
   7970 		  if (map->count == 0)
   7971 		    {
   7972 		      /* If the first section in a segment does not start at
   7973 			 the beginning of the segment, then something is
   7974 			 wrong.  */
   7975 		      if (align_power (map->p_paddr
   7976 				       + (map->includes_filehdr
   7977 					  ? iehdr->e_ehsize : 0)
   7978 				       + (map->includes_phdrs
   7979 					  ? iehdr->e_phnum * iehdr->e_phentsize
   7980 					  : 0),
   7981 				       output_section->alignment_power * opb)
   7982 			  != output_section->lma * opb)
   7983 			goto sorry;
   7984 		    }
   7985 		  else
   7986 		    {
   7987 		      asection *prev_sec;
   7988 
   7989 		      prev_sec = map->sections[map->count - 1];
   7990 
   7991 		      /* If the gap between the end of the previous section
   7992 			 and the start of this section is more than
   7993 			 maxpagesize then we need to start a new segment.  */
   7994 		      if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
   7995 				      maxpagesize)
   7996 			   < BFD_ALIGN (output_section->lma, maxpagesize))
   7997 			  || (prev_sec->lma + prev_sec->size
   7998 			      > output_section->lma))
   7999 			{
   8000 			  if (suggested_lma == NULL)
   8001 			    suggested_lma = output_section;
   8002 
   8003 			  continue;
   8004 			}
   8005 		    }
   8006 
   8007 		  map->sections[map->count++] = output_section;
   8008 		  ++isec;
   8009 		  sections[j] = NULL;
   8010 		  if (segment->p_type == PT_LOAD)
   8011 		    section->segment_mark = true;
   8012 		}
   8013 	      else if (suggested_lma == NULL)
   8014 		suggested_lma = output_section;
   8015 	    }
   8016 
   8017 	  /* PR 23932.  A corrupt input file may contain sections that cannot
   8018 	     be assigned to any segment - because for example they have a
   8019 	     negative size - or segments that do not contain any sections.
   8020 	     But there are also valid reasons why a segment can be empty.
   8021 	     So allow a count of zero.  */
   8022 
   8023 	  /* Add the current segment to the list of built segments.  */
   8024 	  *pointer_to_map = map;
   8025 	  pointer_to_map = &map->next;
   8026 
   8027 	  if (isec < section_count)
   8028 	    {
   8029 	      /* We still have not allocated all of the sections to
   8030 		 segments.  Create a new segment here, initialise it
   8031 		 and carry on looping.  */
   8032 	      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   8033 	      amt += section_count * sizeof (asection *);
   8034 	      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   8035 	      if (map == NULL)
   8036 		{
   8037 		  free (sections);
   8038 		  return false;
   8039 		}
   8040 
   8041 	      /* Initialise the fields of the segment map.  Set the physical
   8042 		 physical address to the LMA of the first section that has
   8043 		 not yet been assigned.  */
   8044 	      map->next = NULL;
   8045 	      map->p_type = segment->p_type;
   8046 	      map->p_flags = segment->p_flags;
   8047 	      map->p_flags_valid = 1;
   8048 	      map->p_paddr = suggested_lma->lma * opb;
   8049 	      map->p_paddr_valid = p_paddr_valid;
   8050 	      map->includes_filehdr = 0;
   8051 	      map->includes_phdrs = 0;
   8052 	    }
   8053 
   8054 	  continue;
   8055 	sorry:
   8056 	  bfd_set_error (bfd_error_sorry);
   8057 	  free (sections);
   8058 	  return false;
   8059 	}
   8060       while (isec < section_count);
   8061 
   8062       free (sections);
   8063     }
   8064 
   8065   elf_seg_map (obfd) = map_first;
   8066 
   8067   /* If we had to estimate the number of program headers that were
   8068      going to be needed, then check our estimate now and adjust
   8069      the offset if necessary.  */
   8070   if (phdr_adjust_seg != NULL)
   8071     {
   8072       unsigned int count;
   8073 
   8074       for (count = 0, map = map_first; map != NULL; map = map->next)
   8075 	count++;
   8076 
   8077       if (count > phdr_adjust_num)
   8078 	phdr_adjust_seg->p_paddr
   8079 	  -= (count - phdr_adjust_num) * iehdr->e_phentsize;
   8080 
   8081       for (map = map_first; map != NULL; map = map->next)
   8082 	if (map->p_type == PT_PHDR)
   8083 	  {
   8084 	    bfd_vma adjust
   8085 	      = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
   8086 	    map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
   8087 	    break;
   8088 	  }
   8089     }
   8090 
   8091 #undef IS_SOLARIS_PT_INTERP
   8092 #undef IS_SECTION_IN_INPUT_SEGMENT
   8093 #undef INCLUDE_SECTION_IN_SEGMENT
   8094 #undef SEGMENT_AFTER_SEGMENT
   8095 #undef SEGMENT_OVERLAPS
   8096   return true;
   8097 }
   8098 
   8099 /* Return true if p_align in the ELF program header in ABFD is valid.  */
   8100 
   8101 static bool
   8102 elf_is_p_align_valid (bfd *abfd)
   8103 {
   8104   unsigned int i;
   8105   Elf_Internal_Phdr *segment;
   8106   unsigned int num_segments;
   8107   elf_backend_data *bed = get_elf_backend_data (abfd);
   8108   bfd_size_type maxpagesize = bed->maxpagesize;
   8109   bfd_size_type p_align = bed->p_align;
   8110 
   8111   /* Return true if the default p_align value isn't set or the maximum
   8112      page size is the same as the minimum page size.  */
   8113   if (p_align == 0 || maxpagesize == bed->minpagesize)
   8114     return true;
   8115 
   8116   /* When the default p_align value is set, p_align may be set to the
   8117      default p_align value while segments are aligned to the maximum
   8118      page size.  In this case, the input p_align will be ignored and
   8119      the maximum page size will be used to align the output segments.  */
   8120   segment = elf_tdata (abfd)->phdr;
   8121   num_segments = elf_elfheader (abfd)->e_phnum;
   8122   for (i = 0; i < num_segments; i++, segment++)
   8123     if (segment->p_type == PT_LOAD
   8124 	&& (segment->p_align != p_align
   8125 	    || vma_page_aligned_bias (segment->p_vaddr,
   8126 				      segment->p_offset,
   8127 				      maxpagesize) != 0))
   8128       return true;
   8129 
   8130   return false;
   8131 }
   8132 
   8133 /* Copy ELF program header information.  */
   8134 
   8135 static bool
   8136 copy_elf_program_header (bfd *ibfd, bfd *obfd)
   8137 {
   8138   Elf_Internal_Ehdr *iehdr;
   8139   struct elf_segment_map *map;
   8140   struct elf_segment_map *map_first;
   8141   struct elf_segment_map **pointer_to_map;
   8142   Elf_Internal_Phdr *segment;
   8143   unsigned int i;
   8144   unsigned int num_segments;
   8145   bool phdr_included = false;
   8146   bool p_paddr_valid;
   8147   bool p_palign_valid;
   8148   unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
   8149 
   8150   iehdr = elf_elfheader (ibfd);
   8151 
   8152   map_first = NULL;
   8153   pointer_to_map = &map_first;
   8154 
   8155   /* If all the segment p_paddr fields are zero, don't set
   8156      map->p_paddr_valid.  */
   8157   p_paddr_valid = false;
   8158   num_segments = elf_elfheader (ibfd)->e_phnum;
   8159   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   8160        i < num_segments;
   8161        i++, segment++)
   8162     if (segment->p_paddr != 0)
   8163       {
   8164 	p_paddr_valid = true;
   8165 	break;
   8166       }
   8167 
   8168   p_palign_valid = elf_is_p_align_valid (ibfd);
   8169 
   8170   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   8171        i < num_segments;
   8172        i++, segment++)
   8173     {
   8174       asection *section;
   8175       unsigned int section_count;
   8176       size_t amt;
   8177       Elf_Internal_Shdr *this_hdr;
   8178       asection *first_section = NULL;
   8179       asection *lowest_section;
   8180 
   8181       /* Compute how many sections are in this segment.  */
   8182       for (section = ibfd->sections, section_count = 0;
   8183 	   section != NULL;
   8184 	   section = section->next)
   8185 	{
   8186 	  this_hdr = &(elf_section_data(section)->this_hdr);
   8187 	  if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   8188 	    {
   8189 	      if (first_section == NULL)
   8190 		first_section = section;
   8191 	      section_count++;
   8192 	    }
   8193 	}
   8194 
   8195       /* Allocate a segment map big enough to contain
   8196 	 all of the sections we have selected.  */
   8197       amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   8198       amt += section_count * sizeof (asection *);
   8199       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   8200       if (map == NULL)
   8201 	return false;
   8202 
   8203       /* Initialize the fields of the output segment map with the
   8204 	 input segment.  */
   8205       map->next = NULL;
   8206       map->p_type = segment->p_type;
   8207       map->p_flags = segment->p_flags;
   8208       map->p_flags_valid = 1;
   8209       map->p_paddr = segment->p_paddr;
   8210       map->p_paddr_valid = p_paddr_valid;
   8211       map->p_align = segment->p_align;
   8212       /* Keep p_align of PT_GNU_STACK for stack alignment.  */
   8213       map->p_align_valid = (map->p_type == PT_GNU_STACK
   8214 			    || p_palign_valid);
   8215       map->p_vaddr_offset = 0;
   8216 
   8217       if (map->p_type == PT_GNU_RELRO
   8218 	  || map->p_type == PT_GNU_STACK)
   8219 	{
   8220 	  /* The PT_GNU_RELRO segment may contain the first a few
   8221 	     bytes in the .got.plt section even if the whole .got.plt
   8222 	     section isn't in the PT_GNU_RELRO segment.  We won't
   8223 	     change the size of the PT_GNU_RELRO segment.
   8224 	     Similarly, PT_GNU_STACK size is significant on uclinux
   8225 	     systems.    */
   8226 	  map->p_size = segment->p_memsz;
   8227 	  map->p_size_valid = 1;
   8228 	}
   8229 
   8230       /* Determine if this segment contains the ELF file header
   8231 	 and if it contains the program headers themselves.  */
   8232       map->includes_filehdr = (segment->p_offset == 0
   8233 			       && segment->p_filesz >= iehdr->e_ehsize);
   8234 
   8235       map->includes_phdrs = 0;
   8236       if (! phdr_included || segment->p_type != PT_LOAD)
   8237 	{
   8238 	  map->includes_phdrs =
   8239 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   8240 	     && (segment->p_offset + segment->p_filesz
   8241 		 >= ((bfd_vma) iehdr->e_phoff
   8242 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   8243 
   8244 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   8245 	    phdr_included = true;
   8246 	}
   8247 
   8248       lowest_section = NULL;
   8249       if (section_count != 0)
   8250 	{
   8251 	  unsigned int isec = 0;
   8252 
   8253 	  for (section = first_section;
   8254 	       section != NULL;
   8255 	       section = section->next)
   8256 	    {
   8257 	      this_hdr = &(elf_section_data(section)->this_hdr);
   8258 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   8259 		{
   8260 		  map->sections[isec++] = section->output_section;
   8261 		  if ((section->flags & SEC_ALLOC) != 0)
   8262 		    {
   8263 		      bfd_vma seg_off;
   8264 
   8265 		      if (lowest_section == NULL
   8266 			  || section->lma < lowest_section->lma)
   8267 			lowest_section = section;
   8268 
   8269 		      /* Section lmas are set up from PT_LOAD header
   8270 			 p_paddr in _bfd_elf_make_section_from_shdr.
   8271 			 If this header has a p_paddr that disagrees
   8272 			 with the section lma, flag the p_paddr as
   8273 			 invalid.  */
   8274 		      if ((section->flags & SEC_LOAD) != 0)
   8275 			seg_off = this_hdr->sh_offset - segment->p_offset;
   8276 		      else
   8277 			seg_off = this_hdr->sh_addr - segment->p_vaddr;
   8278 		      if (section->lma * opb - segment->p_paddr != seg_off)
   8279 			map->p_paddr_valid = false;
   8280 		    }
   8281 		  if (isec == section_count)
   8282 		    break;
   8283 		}
   8284 	    }
   8285 	}
   8286 
   8287       if (section_count == 0)
   8288 	map->p_vaddr_offset = segment->p_vaddr / opb;
   8289       else if (map->p_paddr_valid)
   8290 	{
   8291 	  /* Account for padding before the first section in the segment.  */
   8292 	  bfd_vma hdr_size = 0;
   8293 	  if (map->includes_filehdr)
   8294 	    hdr_size = iehdr->e_ehsize;
   8295 	  if (map->includes_phdrs)
   8296 	    hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
   8297 
   8298 	  map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
   8299 				 - (lowest_section ? lowest_section->lma : 0));
   8300 	}
   8301 
   8302       map->count = section_count;
   8303       *pointer_to_map = map;
   8304       pointer_to_map = &map->next;
   8305     }
   8306 
   8307   elf_seg_map (obfd) = map_first;
   8308   return true;
   8309 }
   8310 
   8311 /* Copy private BFD data.  This copies or rewrites ELF program header
   8312    information.  */
   8313 
   8314 static bool
   8315 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   8316 {
   8317   bfd_vma maxpagesize;
   8318 
   8319   if (elf_tdata (ibfd)->phdr == NULL)
   8320     return true;
   8321 
   8322   if (ibfd->xvec == obfd->xvec)
   8323     {
   8324       /* Check to see if any sections in the input BFD
   8325 	 covered by ELF program header have changed.  */
   8326       Elf_Internal_Phdr *segment;
   8327       asection * section;
   8328       asection * osec;
   8329       asection * prev;
   8330       unsigned int i, num_segments;
   8331       Elf_Internal_Shdr *this_hdr;
   8332       elf_backend_data *bed;
   8333 
   8334       bed = get_elf_backend_data (ibfd);
   8335 
   8336       /* Regenerate the segment map if p_paddr is set to 0.  */
   8337       if (bed->want_p_paddr_set_to_zero)
   8338 	goto rewrite;
   8339 
   8340       /* Initialize the segment mark field.  */
   8341       for (section = obfd->sections; section != NULL;
   8342 	   section = section->next)
   8343 	section->segment_mark = false;
   8344 
   8345       num_segments = elf_elfheader (ibfd)->e_phnum;
   8346       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   8347 	   i < num_segments;
   8348 	   i++, segment++)
   8349 	{
   8350 	  /* PR binutils/3535.  The Solaris linker always sets the p_paddr
   8351 	     and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
   8352 	     which severly confuses things, so always regenerate the segment
   8353 	     map in this case.  */
   8354 	  if (segment->p_paddr == 0
   8355 	      && segment->p_memsz == 0
   8356 	      && (segment->p_type == PT_INTERP
   8357 		  || segment->p_type == PT_DYNAMIC))
   8358 	    goto rewrite;
   8359 
   8360 	  for (section = ibfd->sections, prev = NULL;
   8361 	       section != NULL; section = section->next)
   8362 	    {
   8363 	      /* We mark the output section so that we know it comes
   8364 		 from the input BFD.  */
   8365 	      osec = section->output_section;
   8366 	      if (osec)
   8367 		osec->segment_mark = true;
   8368 
   8369 	      /* Check if this section is covered by the segment.  */
   8370 	      this_hdr = &(elf_section_data(section)->this_hdr);
   8371 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   8372 		{
   8373 		  /* FIXME: Check if its output section is changed or
   8374 		     removed.  What else do we need to check?  */
   8375 		  if (osec == NULL
   8376 		      || section->flags != osec->flags
   8377 		      || section->lma != osec->lma
   8378 		      || section->vma != osec->vma
   8379 		      || section->size != osec->size
   8380 		      || section->rawsize != osec->rawsize
   8381 		      || section->alignment_power != osec->alignment_power)
   8382 		    goto rewrite;
   8383 
   8384 		  /* PR 31450: If this is an allocated section then make sure
   8385 		     that this section's vma to lma relationship is the same
   8386 		     as previous (allocated) section's.  */
   8387 		  if (prev != NULL
   8388 		      && section->flags & SEC_ALLOC
   8389 		      && section->lma - section->vma != prev->lma - prev->vma)
   8390 		    goto rewrite;
   8391 
   8392 		  if (section->flags & SEC_ALLOC)
   8393 		    prev = section;
   8394 		}
   8395 	    }
   8396 	}
   8397 
   8398       /* Check to see if any output section do not come from the
   8399 	 input BFD.  */
   8400       for (section = obfd->sections; section != NULL;
   8401 	   section = section->next)
   8402 	{
   8403 	  if (!section->segment_mark)
   8404 	    goto rewrite;
   8405 	  else
   8406 	    section->segment_mark = false;
   8407 	}
   8408 
   8409       return copy_elf_program_header (ibfd, obfd);
   8410     }
   8411 
   8412  rewrite:
   8413   maxpagesize = 0;
   8414   if (ibfd->xvec == obfd->xvec)
   8415     {
   8416       /* When rewriting program header, set the output maxpagesize to
   8417 	 the maximum alignment of input PT_LOAD segments.  */
   8418       Elf_Internal_Phdr *segment;
   8419       unsigned int i;
   8420       unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
   8421 
   8422       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   8423 	   i < num_segments;
   8424 	   i++, segment++)
   8425 	if (segment->p_type == PT_LOAD
   8426 	    && maxpagesize < segment->p_align)
   8427 	  {
   8428 	    /* PR 17512: file: f17299af.  */
   8429 	    if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
   8430 	      /* xgettext:c-format */
   8431 	      _bfd_error_handler (_("%pB: warning: segment alignment of %#"
   8432 				    PRIx64 " is too large"),
   8433 				  ibfd, (uint64_t) segment->p_align);
   8434 	    else
   8435 	      maxpagesize = segment->p_align;
   8436 	  }
   8437     }
   8438   if (maxpagesize == 0)
   8439     maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
   8440 
   8441   return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
   8442 }
   8443 
   8444 /* Copy private section information from input to output.  This function
   8445    is called both by objcopy where LINK_INFO is NULL, and ld where
   8446    LINK_INFO is non-NULL in the usual case but NULL for a special case
   8447    when dealing with LTO IR or cloning output sections.  */
   8448 
   8449 bool
   8450 _bfd_elf_copy_private_section_data (bfd *ibfd,
   8451 				    asection *isec,
   8452 				    bfd *obfd ATTRIBUTE_UNUSED,
   8453 				    asection *osec,
   8454 				    struct bfd_link_info *link_info)
   8455 {
   8456   if (ibfd->xvec->flavour != bfd_target_elf_flavour)
   8457     return true;
   8458 
   8459   Elf_Internal_Shdr *ihdr = &elf_section_data (isec)->this_hdr;
   8460   Elf_Internal_Shdr *ohdr = &elf_section_data (osec)->this_hdr;
   8461   if (link_info == NULL)
   8462     {
   8463       ohdr->sh_entsize = ihdr->sh_entsize;
   8464 
   8465       if (ihdr->sh_type == SHT_SYMTAB
   8466 	  || ihdr->sh_type == SHT_DYNSYM
   8467 	  || ihdr->sh_type == SHT_GNU_verneed
   8468 	  || ihdr->sh_type == SHT_GNU_verdef)
   8469 	ohdr->sh_info = ihdr->sh_info;
   8470     }
   8471 
   8472   /* If this is a known ABI section, ELF section type and flags may
   8473      have been set up when OSEC was created.  For normal sections we
   8474      allow the user to override the type and flags other than
   8475      SHF_MASKOS and SHF_MASKPROC.  */
   8476   if (elf_section_type (osec) == SHT_PROGBITS
   8477       || elf_section_type (osec) == SHT_NOTE
   8478       || elf_section_type (osec) == SHT_NOBITS)
   8479     elf_section_type (osec) = SHT_NULL;
   8480 
   8481   /* For objcopy and relocatable link, copy the ELF section type from
   8482      the input file if the BFD section flags are the same.  (If they
   8483      are different the user may be doing something like
   8484      "objcopy --set-section-flags .text=alloc,data".)  For a final
   8485      link allow some flags that the linker clears to differ.  */
   8486   bool final_link = (link_info != NULL
   8487 		     && !bfd_link_relocatable (link_info));
   8488   if (elf_section_type (osec) == SHT_NULL
   8489       && (osec->flags == isec->flags
   8490 	  || (final_link
   8491 	      && ((osec->flags ^ isec->flags)
   8492 		  & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
   8493     elf_section_type (osec) = elf_section_type (isec);
   8494 
   8495   elf_section_flags (osec) = elf_section_flags (isec);
   8496   /* Like for type, retain flags for objcopy (yet unlike for type, don't do so
   8497      for relocatable link).  Same heuristic as there: If the BFD section flags
   8498      are different, assume --set-section-flags is in use for the section.
   8499 
   8500      FIXME: Is this correct for all OS/PROC specific flags?  */
   8501   if (link_info != NULL || osec->flags != isec->flags)
   8502     elf_section_flags (osec) &= (SHF_MASKOS | SHF_MASKPROC);
   8503   else
   8504     {
   8505       /* Clear only flags which are set below or elsewhere.  */
   8506       elf_section_flags (osec) &= ~(SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR
   8507 				    | SHF_MERGE | SHF_STRINGS | SHF_LINK_ORDER
   8508 				    | SHF_INFO_LINK | SHF_GROUP | SHF_TLS
   8509 				    | SHF_COMPRESSED);
   8510       if (elf_section_flags (osec) & ~(SHF_MASKOS | SHF_MASKPROC))
   8511 	_bfd_error_handler
   8512 	  (_("%pB:%pA: warning: retaining unknown section flag(s) %#" PRIx64),
   8513 	   ibfd, isec,
   8514 	   (uint64_t) (elf_section_flags (osec)
   8515 		       & ~(SHF_MASKOS | SHF_MASKPROC)));
   8516     }
   8517 
   8518   /* Copy sh_info from input for mbind section.  */
   8519   if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
   8520       && elf_section_flags (isec) & SHF_GNU_MBIND)
   8521     elf_section_data (osec)->this_hdr.sh_info
   8522       = elf_section_data (isec)->this_hdr.sh_info;
   8523 
   8524   /* Set things up for objcopy and relocatable link.  The output
   8525      SHT_GROUP section will have its elf_next_in_group pointing back
   8526      to the input group members.  Ignore linker created group section.
   8527      See elfNN_ia64_object_p in elfxx-ia64.c.  */
   8528   if ((link_info == NULL
   8529        || !link_info->resolve_section_groups)
   8530       && (elf_sec_group (isec) == NULL
   8531 	  || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
   8532     {
   8533       if (elf_section_flags (isec) & SHF_GROUP)
   8534 	elf_section_flags (osec) |= SHF_GROUP;
   8535       elf_next_in_group (osec) = elf_next_in_group (isec);
   8536       elf_section_data (osec)->group = elf_section_data (isec)->group;
   8537     }
   8538 
   8539   /* If not decompress, preserve SHF_COMPRESSED.  */
   8540   if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
   8541     elf_section_flags (osec) |= (elf_section_flags (isec)
   8542 				 & SHF_COMPRESSED);
   8543 
   8544   /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
   8545      don't use the output section of the linked-to section since it
   8546      may be NULL at this point.  */
   8547   if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
   8548     {
   8549       ohdr->sh_flags |= SHF_LINK_ORDER;
   8550       elf_linked_to_section (osec) = elf_linked_to_section (isec);
   8551     }
   8552 
   8553   osec->use_rela_p = isec->use_rela_p;
   8554 
   8555   return true;
   8556 }
   8557 
   8558 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
   8559    necessary if we are removing either the SHT_GROUP section or any of
   8560    the group member sections.  DISCARDED is the value that a section's
   8561    output_section has if the section will be discarded, NULL when this
   8562    function is called from objcopy, bfd_abs_section_ptr when called
   8563    from the linker.  */
   8564 
   8565 bool
   8566 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
   8567 {
   8568   asection *isec;
   8569 
   8570   for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   8571     if (elf_section_type (isec) == SHT_GROUP)
   8572       {
   8573 	asection *first = elf_next_in_group (isec);
   8574 	asection *s = first;
   8575 	bfd_size_type removed = 0;
   8576 
   8577 	while (s != NULL)
   8578 	  {
   8579 	    /* If this member section is being output but the
   8580 	       SHT_GROUP section is not, then clear the group info
   8581 	       set up by _bfd_elf_copy_private_section_data.  */
   8582 	    if (s->output_section != discarded
   8583 		&& isec->output_section == discarded)
   8584 	      {
   8585 		elf_section_flags (s->output_section) &= ~SHF_GROUP;
   8586 		elf_group_name (s->output_section) = NULL;
   8587 	      }
   8588 	    else
   8589 	      {
   8590 		struct bfd_elf_section_data *elf_sec = elf_section_data (s);
   8591 		if (s->output_section == discarded
   8592 		    && isec->output_section != discarded)
   8593 		  {
   8594 		    /* Conversely, if the member section is not being
   8595 		       output but the SHT_GROUP section is, then adjust
   8596 		       its size.  */
   8597 		    removed += 4;
   8598 		    if (elf_sec->rel.hdr != NULL
   8599 			&& (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
   8600 		      removed += 4;
   8601 		    if (elf_sec->rela.hdr != NULL
   8602 			&& (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
   8603 		      removed += 4;
   8604 		  }
   8605 		else
   8606 		  {
   8607 		    /* Also adjust for zero-sized relocation member
   8608 		       section.  */
   8609 		    if (elf_sec->rel.hdr != NULL
   8610 			&& elf_sec->rel.hdr->sh_size == 0)
   8611 		      removed += 4;
   8612 		    if (elf_sec->rela.hdr != NULL
   8613 			&& elf_sec->rela.hdr->sh_size == 0)
   8614 		      removed += 4;
   8615 		  }
   8616 	      }
   8617 	    s = elf_next_in_group (s);
   8618 	    if (s == first)
   8619 	      break;
   8620 	  }
   8621 	if (removed != 0)
   8622 	  {
   8623 	    if (discarded != NULL)
   8624 	      {
   8625 		/* If we've been called for ld -r, then we need to
   8626 		   adjust the input section size.  */
   8627 		if (isec->rawsize == 0)
   8628 		  isec->rawsize = isec->size;
   8629 		isec->size = isec->rawsize - removed;
   8630 		if (isec->size <= 4)
   8631 		  {
   8632 		    isec->size = 0;
   8633 		    isec->flags |= SEC_EXCLUDE;
   8634 		  }
   8635 	      }
   8636 	    else if (isec->output_section != NULL)
   8637 	      {
   8638 		/* Adjust the output section size when called from
   8639 		   objcopy. */
   8640 		isec->output_section->size -= removed;
   8641 		if (isec->output_section->size <= 4)
   8642 		  {
   8643 		    isec->output_section->size = 0;
   8644 		    isec->output_section->flags |= SEC_EXCLUDE;
   8645 		  }
   8646 	      }
   8647 	  }
   8648       }
   8649 
   8650   return true;
   8651 }
   8652 
   8653 /* Copy private header information.  */
   8654 
   8655 bool
   8656 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
   8657 {
   8658   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
   8659     return true;
   8660 
   8661   /* Copy over private BFD data if it has not already been copied.
   8662      This must be done here, rather than in the copy_private_bfd_data
   8663      entry point, because the latter is called after the section
   8664      contents have been set, which means that the program headers have
   8665      already been worked out.  */
   8666   if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
   8667     {
   8668       if (! copy_private_bfd_data (ibfd, obfd))
   8669 	return false;
   8670     }
   8671 
   8672   return _bfd_elf_fixup_group_sections (ibfd, NULL);
   8673 }
   8674 
   8675 /* Copy private symbol information.  If this symbol is in a section
   8676    which we did not map into a BFD section, try to map the section
   8677    index correctly.  We use special macro definitions for the mapped
   8678    section indices; these definitions are interpreted by the
   8679    swap_out_syms function.  */
   8680 
   8681 #define MAP_ONESYMTAB (SHN_HIOS + 1)
   8682 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
   8683 #define MAP_STRTAB    (SHN_HIOS + 3)
   8684 #define MAP_SHSTRTAB  (SHN_HIOS + 4)
   8685 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
   8686 
   8687 bool
   8688 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
   8689 				   asymbol **isymarg,
   8690 				   bfd *obfd ATTRIBUTE_UNUSED,
   8691 				   asymbol **osymarg)
   8692 {
   8693   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
   8694     return true;
   8695 
   8696   elf_symbol_type *isym = elf_symbol_from (*isymarg);
   8697   elf_symbol_type *osym = elf_symbol_from (*osymarg);
   8698   if (isym != NULL
   8699       && isym->internal_elf_sym.st_shndx != 0
   8700       && osym != NULL
   8701       && bfd_is_abs_section (isym->symbol.section))
   8702     {
   8703       unsigned int shndx;
   8704 
   8705       shndx = isym->internal_elf_sym.st_shndx;
   8706       if (shndx == elf_onesymtab (ibfd))
   8707 	shndx = MAP_ONESYMTAB;
   8708       else if (shndx == elf_dynsymtab (ibfd))
   8709 	shndx = MAP_DYNSYMTAB;
   8710       else if (shndx == elf_elfsections (ibfd)[elf_onesymtab (ibfd)]->sh_link)
   8711 	shndx = MAP_STRTAB;
   8712       else if (shndx == elf_elfheader (ibfd)->e_shstrndx)
   8713 	shndx = MAP_SHSTRTAB;
   8714       else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
   8715 	shndx = MAP_SYM_SHNDX;
   8716       osym->internal_elf_sym.st_shndx = shndx;
   8717     }
   8718 
   8719   return true;
   8720 }
   8721 
   8722 /* Swap out the symbols.  */
   8723 
   8724 static bool
   8725 swap_out_syms (bfd *abfd,
   8726 	       struct elf_strtab_hash **sttp,
   8727 	       int relocatable_p,
   8728 	       struct bfd_link_info *info)
   8729 {
   8730   elf_backend_data *bed;
   8731   unsigned int symcount;
   8732   asymbol **syms;
   8733   struct elf_strtab_hash *stt;
   8734   Elf_Internal_Shdr *symtab_hdr;
   8735   Elf_Internal_Shdr *symtab_shndx_hdr;
   8736   Elf_Internal_Shdr *symstrtab_hdr;
   8737   struct elf_sym_strtab *symstrtab;
   8738   bfd_byte *outbound_syms;
   8739   bfd_byte *outbound_shndx;
   8740   unsigned long outbound_syms_index;
   8741   unsigned int idx;
   8742   unsigned int num_locals;
   8743   size_t amt;
   8744   bool name_local_sections;
   8745 
   8746   if (!elf_map_symbols (abfd, &num_locals))
   8747     return false;
   8748 
   8749   /* Dump out the symtabs.  */
   8750   stt = _bfd_elf_strtab_init ();
   8751   if (stt == NULL)
   8752     return false;
   8753 
   8754   bed = get_elf_backend_data (abfd);
   8755   symcount = bfd_get_symcount (abfd);
   8756   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   8757   symtab_hdr->sh_type = SHT_SYMTAB;
   8758   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
   8759   symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
   8760   symtab_hdr->sh_info = num_locals + 1;
   8761   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   8762 
   8763   symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
   8764   symstrtab_hdr->sh_type = SHT_STRTAB;
   8765 
   8766   /* Allocate buffer to swap out the .strtab section.  */
   8767   if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
   8768       || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
   8769     {
   8770       bfd_set_error (bfd_error_no_memory);
   8771       _bfd_elf_strtab_free (stt);
   8772       return false;
   8773     }
   8774 
   8775   if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
   8776       || (outbound_syms = bfd_malloc (amt)) == NULL)
   8777     {
   8778     error_no_mem:
   8779       bfd_set_error (bfd_error_no_memory);
   8780     error_return:
   8781       free (symstrtab);
   8782       _bfd_elf_strtab_free (stt);
   8783       return false;
   8784     }
   8785   symtab_hdr->contents = outbound_syms;
   8786   outbound_syms_index = 0;
   8787 
   8788   outbound_shndx = NULL;
   8789 
   8790   if (elf_symtab_shndx_list (abfd))
   8791     {
   8792       symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
   8793       if (symtab_shndx_hdr->sh_name != 0)
   8794 	{
   8795 	  if (_bfd_mul_overflow (symcount + 1,
   8796 				 sizeof (Elf_External_Sym_Shndx), &amt))
   8797 	    goto error_no_mem;
   8798 	  outbound_shndx =  (bfd_byte *) bfd_zalloc (abfd, amt);
   8799 	  if (outbound_shndx == NULL)
   8800 	    goto error_return;
   8801 
   8802 	  symtab_shndx_hdr->contents = outbound_shndx;
   8803 	  symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
   8804 	  symtab_shndx_hdr->sh_size = amt;
   8805 	  symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
   8806 	  symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
   8807 	}
   8808       /* FIXME: What about any other headers in the list ?  */
   8809     }
   8810 
   8811   /* Now generate the data (for "contents").  */
   8812   {
   8813     /* Fill in zeroth symbol and swap it out.  */
   8814     Elf_Internal_Sym sym;
   8815     sym.st_name = 0;
   8816     sym.st_value = 0;
   8817     sym.st_size = 0;
   8818     sym.st_info = 0;
   8819     sym.st_other = 0;
   8820     sym.st_shndx = SHN_UNDEF;
   8821     sym.st_target_internal = 0;
   8822     symstrtab[outbound_syms_index].sym = sym;
   8823     symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
   8824     outbound_syms_index++;
   8825   }
   8826 
   8827   name_local_sections
   8828     = (bed->elf_backend_name_local_section_symbols
   8829        && bed->elf_backend_name_local_section_symbols (abfd));
   8830 
   8831   syms = bfd_get_outsymbols (abfd);
   8832   for (idx = 0; idx < symcount; idx++)
   8833     {
   8834       Elf_Internal_Sym sym;
   8835 
   8836       flagword flags = syms[idx]->flags;
   8837       if (!name_local_sections
   8838 	  && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
   8839 	{
   8840 	  /* Local section symbols have no name.  */
   8841 	  sym.st_name = 0;
   8842 	}
   8843       else
   8844 	{
   8845 	  /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
   8846 	     to get the final offset for st_name.  */
   8847 	  size_t stridx = _bfd_elf_strtab_add (stt, syms[idx]->name, false);
   8848 	  if (stridx == (size_t) -1)
   8849 	    goto error_return;
   8850 	  sym.st_name = stridx;
   8851 	}
   8852 
   8853       bfd_vma value = syms[idx]->value;
   8854       elf_symbol_type *type_ptr = elf_symbol_from (syms[idx]);
   8855       asection *sec = syms[idx]->section;
   8856 
   8857       if ((flags & BSF_SECTION_SYM) == 0 && bfd_is_com_section (sec))
   8858 	{
   8859 	  /* ELF common symbols put the alignment into the `value' field,
   8860 	     and the size into the `size' field.  This is backwards from
   8861 	     how BFD handles it, so reverse it here.  */
   8862 	  sym.st_size = value;
   8863 	  if (type_ptr == NULL
   8864 	      || type_ptr->internal_elf_sym.st_value == 0)
   8865 	    sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
   8866 	  else
   8867 	    sym.st_value = type_ptr->internal_elf_sym.st_value;
   8868 	  sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   8869 	}
   8870       else
   8871 	{
   8872 	  unsigned int shndx;
   8873 
   8874 	  if (sec->output_section)
   8875 	    {
   8876 	      value += sec->output_offset;
   8877 	      sec = sec->output_section;
   8878 	    }
   8879 
   8880 	  /* Don't add in the section vma for relocatable output.  */
   8881 	  if (! relocatable_p)
   8882 	    value += sec->vma;
   8883 	  sym.st_value = value;
   8884 	  sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
   8885 
   8886 	  if (bfd_is_abs_section (sec)
   8887 	      && type_ptr != NULL
   8888 	      && type_ptr->internal_elf_sym.st_shndx != 0)
   8889 	    {
   8890 	      /* This symbol is in a real ELF section which we did
   8891 		 not create as a BFD section.  Undo the mapping done
   8892 		 by copy_private_symbol_data.  */
   8893 	      shndx = type_ptr->internal_elf_sym.st_shndx;
   8894 	      switch (shndx)
   8895 		{
   8896 		case MAP_ONESYMTAB:
   8897 		  shndx = elf_onesymtab (abfd);
   8898 		  break;
   8899 		case MAP_DYNSYMTAB:
   8900 		  shndx = elf_dynsymtab (abfd);
   8901 		  break;
   8902 		case MAP_STRTAB:
   8903 		  shndx = elf_strtab_sec (abfd);
   8904 		  break;
   8905 		case MAP_SHSTRTAB:
   8906 		  shndx = elf_shstrtab_sec (abfd);
   8907 		  break;
   8908 		case MAP_SYM_SHNDX:
   8909 		  if (elf_symtab_shndx_list (abfd))
   8910 		    shndx = elf_symtab_shndx_list (abfd)->ndx;
   8911 		  break;
   8912 		case SHN_COMMON:
   8913 		case SHN_ABS:
   8914 		  shndx = SHN_ABS;
   8915 		  break;
   8916 		default:
   8917 		  if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
   8918 		    {
   8919 		      if (bed->symbol_section_index)
   8920 			shndx = bed->symbol_section_index (abfd, type_ptr);
   8921 		      /* Otherwise just leave the index alone.  */
   8922 		    }
   8923 		  else
   8924 		    {
   8925 		      if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
   8926 			_bfd_error_handler (_("%pB: \
   8927 Unable to handle section index %x in ELF symbol.  Using ABS instead."),
   8928 					  abfd, shndx);
   8929 		      shndx = SHN_ABS;
   8930 		    }
   8931 		  break;
   8932 		}
   8933 	    }
   8934 	  else
   8935 	    {
   8936 	      shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   8937 
   8938 	      if (shndx == SHN_BAD)
   8939 		{
   8940 		  asection *sec2;
   8941 
   8942 		  /* Writing this would be a hell of a lot easier if
   8943 		     we had some decent documentation on bfd, and
   8944 		     knew what to expect of the library, and what to
   8945 		     demand of applications.  For example, it
   8946 		     appears that `objcopy' might not set the
   8947 		     section of a symbol to be a section that is
   8948 		     actually in the output file.  */
   8949 		  sec2 = bfd_get_section_by_name (abfd, sec->name);
   8950 		  if (sec2 != NULL)
   8951 		    shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
   8952 		  if (shndx == SHN_BAD)
   8953 		    {
   8954 		      /* xgettext:c-format */
   8955 		      _bfd_error_handler
   8956 			(_("unable to find equivalent output section"
   8957 			   " for symbol '%s' from section '%s'"),
   8958 			 syms[idx]->name ? syms[idx]->name : "<Local sym>",
   8959 			 sec->name);
   8960 		      bfd_set_error (bfd_error_invalid_operation);
   8961 		      goto error_return;
   8962 		    }
   8963 		}
   8964 	    }
   8965 
   8966 	  sym.st_shndx = shndx;
   8967 	}
   8968 
   8969       int type;
   8970       if ((flags & BSF_THREAD_LOCAL) != 0)
   8971 	type = STT_TLS;
   8972       else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
   8973 	type = STT_GNU_IFUNC;
   8974       else if ((flags & BSF_FUNCTION) != 0)
   8975 	type = STT_FUNC;
   8976       else if ((flags & BSF_OBJECT) != 0)
   8977 	type = STT_OBJECT;
   8978       else if ((flags & BSF_RELC) != 0)
   8979 	type = STT_RELC;
   8980       else if ((flags & BSF_SRELC) != 0)
   8981 	type = STT_SRELC;
   8982       else
   8983 	type = STT_NOTYPE;
   8984 
   8985       if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
   8986 	type = STT_TLS;
   8987 
   8988       /* Processor-specific types.  */
   8989       if (type_ptr != NULL
   8990 	  && bed->elf_backend_get_symbol_type)
   8991 	type = ((*bed->elf_backend_get_symbol_type)
   8992 		(&type_ptr->internal_elf_sym, type));
   8993 
   8994       if (flags & BSF_SECTION_SYM)
   8995 	{
   8996 	  if (flags & BSF_GLOBAL)
   8997 	    sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   8998 	  else
   8999 	    sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   9000 	}
   9001       else if (bfd_is_com_section (syms[idx]->section))
   9002 	{
   9003 	  if (type != STT_TLS)
   9004 	    {
   9005 	      if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
   9006 		type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
   9007 			? STT_COMMON : STT_OBJECT);
   9008 	      else
   9009 		type = ((flags & BSF_ELF_COMMON) != 0
   9010 			? STT_COMMON : STT_OBJECT);
   9011 	    }
   9012 	  sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
   9013 	}
   9014       else if (bfd_is_und_section (syms[idx]->section))
   9015 	sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
   9016 				    ? STB_WEAK
   9017 				    : STB_GLOBAL),
   9018 				   type);
   9019       else if (flags & BSF_FILE)
   9020 	sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   9021       else
   9022 	{
   9023 	  int bind = STB_LOCAL;
   9024 
   9025 	  if (flags & BSF_LOCAL)
   9026 	    bind = STB_LOCAL;
   9027 	  else if (flags & BSF_GNU_UNIQUE)
   9028 	    bind = STB_GNU_UNIQUE;
   9029 	  else if (flags & BSF_WEAK)
   9030 	    bind = STB_WEAK;
   9031 	  else if (flags & BSF_GLOBAL)
   9032 	    bind = STB_GLOBAL;
   9033 
   9034 	  sym.st_info = ELF_ST_INFO (bind, type);
   9035 	}
   9036 
   9037       if (type_ptr != NULL)
   9038 	{
   9039 	  sym.st_other = type_ptr->internal_elf_sym.st_other;
   9040 	  sym.st_target_internal
   9041 	    = type_ptr->internal_elf_sym.st_target_internal;
   9042 	}
   9043       else
   9044 	{
   9045 	  sym.st_other = 0;
   9046 	  sym.st_target_internal = 0;
   9047 	}
   9048 
   9049       symstrtab[outbound_syms_index].sym = sym;
   9050       symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
   9051       outbound_syms_index++;
   9052     }
   9053 
   9054   /* Finalize the .strtab section.  */
   9055   _bfd_elf_strtab_finalize (stt);
   9056 
   9057   /* Swap out the .strtab section.  */
   9058   for (idx = 0; idx < outbound_syms_index; idx++)
   9059     {
   9060       struct elf_sym_strtab *elfsym = &symstrtab[idx];
   9061       if (elfsym->sym.st_name != 0)
   9062 	elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
   9063 						      elfsym->sym.st_name);
   9064       if (info && info->callbacks->ctf_new_symbol)
   9065 	info->callbacks->ctf_new_symbol (elfsym->dest_index,
   9066 					 &elfsym->sym);
   9067 
   9068       /* Inform the linker of the addition of this symbol.  */
   9069 
   9070       bed->s->swap_symbol_out (abfd, &elfsym->sym,
   9071 			       (outbound_syms
   9072 				+ (elfsym->dest_index
   9073 				   * bed->s->sizeof_sym)),
   9074 			       NPTR_ADD (outbound_shndx,
   9075 					 (elfsym->dest_index
   9076 					  * sizeof (Elf_External_Sym_Shndx))));
   9077     }
   9078   free (symstrtab);
   9079 
   9080   *sttp = stt;
   9081   symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
   9082   symstrtab_hdr->sh_type = SHT_STRTAB;
   9083   symstrtab_hdr->sh_addralign = 1;
   9084 
   9085   return true;
   9086 }
   9087 
   9088 /* Return the number of bytes required to hold the symtab vector.
   9089 
   9090    Note that we base it on the count plus 1, since we will null terminate
   9091    the vector allocated based on this size.  However, the ELF symbol table
   9092    always has a dummy entry as symbol #0, so it ends up even.  */
   9093 
   9094 long
   9095 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
   9096 {
   9097   bfd_size_type symcount;
   9098   long symtab_size;
   9099   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
   9100 
   9101   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   9102   if (symcount > LONG_MAX / sizeof (asymbol *))
   9103     {
   9104       bfd_set_error (bfd_error_file_too_big);
   9105       return -1;
   9106     }
   9107   symtab_size = symcount * (sizeof (asymbol *));
   9108   if (symcount == 0)
   9109     symtab_size = sizeof (asymbol *);
   9110   else if (!bfd_write_p (abfd))
   9111     {
   9112       ufile_ptr filesize = bfd_get_file_size (abfd);
   9113 
   9114       if (filesize != 0 && (unsigned long) symtab_size > filesize)
   9115 	{
   9116 	  bfd_set_error (bfd_error_file_truncated);
   9117 	  return -1;
   9118 	}
   9119     }
   9120 
   9121   return symtab_size;
   9122 }
   9123 
   9124 long
   9125 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
   9126 {
   9127   bfd_size_type symcount;
   9128   long symtab_size;
   9129   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   9130 
   9131   if (elf_dynsymtab (abfd) == 0)
   9132     {
   9133       /* Check if there is dynamic symbol table.  */
   9134       symcount = elf_tdata (abfd)->dt_symtab_count;
   9135       if (symcount)
   9136 	goto compute_symtab_size;
   9137 
   9138       bfd_set_error (bfd_error_invalid_operation);
   9139       return -1;
   9140     }
   9141 
   9142   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   9143   if (symcount > LONG_MAX / sizeof (asymbol *))
   9144     {
   9145       bfd_set_error (bfd_error_file_too_big);
   9146       return -1;
   9147     }
   9148 
   9149  compute_symtab_size:
   9150   symtab_size = symcount * (sizeof (asymbol *));
   9151   if (symcount == 0)
   9152     symtab_size = sizeof (asymbol *);
   9153   else if (!bfd_write_p (abfd))
   9154     {
   9155       ufile_ptr filesize = bfd_get_file_size (abfd);
   9156 
   9157       if (filesize != 0 && (unsigned long) symtab_size > filesize)
   9158 	{
   9159 	  bfd_set_error (bfd_error_file_truncated);
   9160 	  return -1;
   9161 	}
   9162     }
   9163 
   9164   return symtab_size;
   9165 }
   9166 
   9167 long
   9168 _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
   9169 {
   9170   if (asect->reloc_count != 0 && !bfd_write_p (abfd))
   9171     {
   9172       /* Sanity check reloc section size.  */
   9173       ufile_ptr filesize = bfd_get_file_size (abfd);
   9174 
   9175       if (filesize != 0)
   9176 	{
   9177 	  struct bfd_elf_section_data *d = elf_section_data (asect);
   9178 	  bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
   9179 	  bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
   9180 
   9181 	  if (rel_size + rela_size > filesize
   9182 	      || rel_size + rela_size < rel_size)
   9183 	    {
   9184 	      bfd_set_error (bfd_error_file_truncated);
   9185 	      return -1;
   9186 	    }
   9187 	}
   9188     }
   9189 
   9190 #if SIZEOF_LONG == SIZEOF_INT
   9191   if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
   9192     {
   9193       bfd_set_error (bfd_error_file_too_big);
   9194       return -1;
   9195     }
   9196 #endif
   9197   return (asect->reloc_count + 1L) * sizeof (arelent *);
   9198 }
   9199 
   9200 /* Canonicalize the relocs.  */
   9201 
   9202 long
   9203 _bfd_elf_canonicalize_reloc (bfd *abfd,
   9204 			     sec_ptr section,
   9205 			     arelent **relptr,
   9206 			     asymbol **symbols)
   9207 {
   9208   arelent *tblptr;
   9209   unsigned int i;
   9210   elf_backend_data *bed = get_elf_backend_data (abfd);
   9211 
   9212   if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
   9213     return -1;
   9214 
   9215   tblptr = section->relocation;
   9216   for (i = 0; i < section->reloc_count; i++)
   9217     *relptr++ = tblptr++;
   9218 
   9219   *relptr = NULL;
   9220 
   9221   return section->reloc_count;
   9222 }
   9223 
   9224 long
   9225 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
   9226 {
   9227   elf_backend_data *bed = get_elf_backend_data (abfd);
   9228   long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
   9229 
   9230   if (symcount >= 0)
   9231     abfd->symcount = symcount;
   9232   return symcount;
   9233 }
   9234 
   9235 long
   9236 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
   9237 				      asymbol **allocation)
   9238 {
   9239   elf_backend_data *bed = get_elf_backend_data (abfd);
   9240   long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
   9241 
   9242   if (symcount >= 0)
   9243     abfd->dynsymcount = symcount;
   9244   return symcount;
   9245 }
   9246 
   9247 /* Return the size required for the dynamic reloc entries.  Any loadable
   9248    section that was actually installed in the BFD, and has type SHT_REL
   9249    or SHT_RELA, and uses the dynamic symbol table, is considered to be a
   9250    dynamic reloc section.  */
   9251 
   9252 long
   9253 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
   9254 {
   9255   bfd_size_type count, ext_rel_size;
   9256   asection *s;
   9257 
   9258   if (elf_dynsymtab (abfd) == 0)
   9259     {
   9260       bfd_set_error (bfd_error_invalid_operation);
   9261       return -1;
   9262     }
   9263 
   9264   count = 1;
   9265   ext_rel_size = 0;
   9266   for (s = abfd->sections; s != NULL; s = s->next)
   9267     if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   9268 	&& (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   9269 	    || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
   9270 	&& (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
   9271       {
   9272 	ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
   9273 	if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
   9274 	  {
   9275 	    bfd_set_error (bfd_error_file_truncated);
   9276 	    return -1;
   9277 	  }
   9278 	count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
   9279 	if (count > LONG_MAX / sizeof (arelent *))
   9280 	  {
   9281 	    bfd_set_error (bfd_error_file_too_big);
   9282 	    return -1;
   9283 	  }
   9284       }
   9285   if (count > 1 && !bfd_write_p (abfd))
   9286     {
   9287       /* Sanity check reloc section sizes.  */
   9288       ufile_ptr filesize = bfd_get_file_size (abfd);
   9289       if (filesize != 0 && ext_rel_size > filesize)
   9290 	{
   9291 	  bfd_set_error (bfd_error_file_truncated);
   9292 	  return -1;
   9293 	}
   9294     }
   9295   return count * sizeof (arelent *);
   9296 }
   9297 
   9298 /* Canonicalize the dynamic relocation entries.  Note that we return the
   9299    dynamic relocations as a single block, although they are actually
   9300    associated with particular sections; the interface, which was
   9301    designed for SunOS style shared libraries, expects that there is only
   9302    one set of dynamic relocs.  Any loadable section that was actually
   9303    installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
   9304    dynamic symbol table, is considered to be a dynamic reloc section.  */
   9305 
   9306 long
   9307 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
   9308 				     arelent **storage,
   9309 				     asymbol **syms)
   9310 {
   9311   bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
   9312   asection *s;
   9313   long ret;
   9314 
   9315   if (elf_dynsymtab (abfd) == 0)
   9316     {
   9317       bfd_set_error (bfd_error_invalid_operation);
   9318       return -1;
   9319     }
   9320 
   9321   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   9322   ret = 0;
   9323   for (s = abfd->sections; s != NULL; s = s->next)
   9324     {
   9325       if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   9326 	  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   9327 	      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
   9328 	  && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
   9329 	{
   9330 	  arelent *p;
   9331 	  long count, i;
   9332 
   9333 	  if (! (*slurp_relocs) (abfd, s, syms, true))
   9334 	    return -1;
   9335 	  count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
   9336 	  p = s->relocation;
   9337 	  for (i = 0; i < count; i++)
   9338 	    *storage++ = p++;
   9339 	  ret += count;
   9340 	}
   9341     }
   9342 
   9343   *storage = NULL;
   9344 
   9345   return ret;
   9346 }
   9347 
   9348 /* Read in the version information.  */
   9350 
   9351 bool
   9352 _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
   9353 {
   9354   bfd_byte *contents = NULL;
   9355   unsigned int freeidx = 0;
   9356   size_t amt;
   9357   void *contents_addr = NULL;
   9358   size_t contents_size = 0;
   9359 
   9360   if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
   9361     {
   9362       Elf_Internal_Shdr *hdr;
   9363       Elf_External_Verneed *everneed;
   9364       Elf_Internal_Verneed *iverneed;
   9365       unsigned int i;
   9366       bfd_byte *contents_end;
   9367       size_t verneed_count;
   9368       size_t verneed_size;
   9369 
   9370       if (elf_tdata (abfd)->dt_verneed != NULL)
   9371 	{
   9372 	  hdr = NULL;
   9373 	  contents = elf_tdata (abfd)->dt_verneed;
   9374 	  verneed_count = elf_tdata (abfd)->dt_verneed_count;
   9375 	  verneed_size = verneed_count * sizeof (Elf_External_Verneed);
   9376 	}
   9377       else
   9378 	{
   9379 	  hdr = &elf_tdata (abfd)->dynverref_hdr;
   9380 
   9381 	  if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
   9382 	    {
   9383 	    error_return_bad_verref:
   9384 	      _bfd_error_handler
   9385 		(_("%pB: .gnu.version_r invalid entry"), abfd);
   9386 	      bfd_set_error (bfd_error_bad_value);
   9387 	    error_return_verref:
   9388 	      elf_tdata (abfd)->verref = NULL;
   9389 	      elf_tdata (abfd)->cverrefs = 0;
   9390 	      goto error_return;
   9391 	    }
   9392 
   9393 	  if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
   9394 	    goto error_return_verref;
   9395 	  contents_size = hdr->sh_size;
   9396 	  contents = _bfd_mmap_temporary (abfd, contents_size,
   9397 					  &contents_addr, &contents_size);
   9398 	  if (contents == NULL)
   9399 	    goto error_return_verref;
   9400 
   9401 	  verneed_size = hdr->sh_size;
   9402 	  verneed_count = hdr->sh_info;
   9403 	}
   9404 
   9405       if (_bfd_mul_overflow (verneed_count,
   9406 			     sizeof (Elf_Internal_Verneed), &amt))
   9407 	{
   9408 	  bfd_set_error (bfd_error_file_too_big);
   9409 	  goto error_return_verref;
   9410 	}
   9411       if (amt == 0)
   9412 	goto error_return_verref;
   9413       elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
   9414       if (elf_tdata (abfd)->verref == NULL)
   9415 	goto error_return_verref;
   9416 
   9417       BFD_ASSERT (sizeof (Elf_External_Verneed)
   9418 		  == sizeof (Elf_External_Vernaux));
   9419       contents_end = (contents + verneed_size
   9420 		      - sizeof (Elf_External_Verneed));
   9421       everneed = (Elf_External_Verneed *) contents;
   9422       iverneed = elf_tdata (abfd)->verref;
   9423       for (i = 0; i < verneed_count; i++, iverneed++)
   9424 	{
   9425 	  Elf_External_Vernaux *evernaux;
   9426 	  Elf_Internal_Vernaux *ivernaux;
   9427 	  unsigned int j;
   9428 
   9429 	  _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
   9430 
   9431 	  iverneed->vn_bfd = abfd;
   9432 
   9433 	  if (elf_use_dt_symtab_p (abfd))
   9434 	    {
   9435 	      if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz)
   9436 		iverneed->vn_filename
   9437 		  = elf_tdata (abfd)->dt_strtab + iverneed->vn_file;
   9438 	      else
   9439 		iverneed->vn_filename = NULL;
   9440 	    }
   9441 	  else if (hdr == NULL)
   9442 	    goto error_return_bad_verref;
   9443 	  else
   9444 	    iverneed->vn_filename
   9445 	      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   9446 						 iverneed->vn_file);
   9447 	  if (iverneed->vn_filename == NULL)
   9448 	    goto error_return_bad_verref;
   9449 
   9450 	  if (iverneed->vn_cnt == 0)
   9451 	    iverneed->vn_auxptr = NULL;
   9452 	  else
   9453 	    {
   9454 	      if (_bfd_mul_overflow (iverneed->vn_cnt,
   9455 				     sizeof (Elf_Internal_Vernaux), &amt))
   9456 		{
   9457 		  bfd_set_error (bfd_error_file_too_big);
   9458 		  goto error_return_verref;
   9459 		}
   9460 	      iverneed->vn_auxptr = (struct elf_internal_vernaux *)
   9461 		bfd_alloc (abfd, amt);
   9462 	      if (iverneed->vn_auxptr == NULL)
   9463 		goto error_return_verref;
   9464 	    }
   9465 
   9466 	  if (iverneed->vn_aux
   9467 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   9468 	    goto error_return_bad_verref;
   9469 
   9470 	  evernaux = ((Elf_External_Vernaux *)
   9471 		      ((bfd_byte *) everneed + iverneed->vn_aux));
   9472 	  ivernaux = iverneed->vn_auxptr;
   9473 	  for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
   9474 	    {
   9475 	      _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
   9476 
   9477 	      if (elf_use_dt_symtab_p (abfd))
   9478 		{
   9479 		  if (ivernaux->vna_name < elf_tdata (abfd)->dt_strsz)
   9480 		    ivernaux->vna_nodename
   9481 		      = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name;
   9482 		  else
   9483 		    ivernaux->vna_nodename = NULL;
   9484 		}
   9485 	      else if (hdr == NULL)
   9486 		goto error_return_bad_verref;
   9487 	      else
   9488 		ivernaux->vna_nodename
   9489 		  = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   9490 						     ivernaux->vna_name);
   9491 	      if (ivernaux->vna_nodename == NULL)
   9492 		goto error_return_bad_verref;
   9493 
   9494 	      if (ivernaux->vna_other > freeidx)
   9495 		freeidx = ivernaux->vna_other;
   9496 
   9497 	      ivernaux->vna_nextptr = NULL;
   9498 	      if (ivernaux->vna_next == 0)
   9499 		{
   9500 		  iverneed->vn_cnt = j + 1;
   9501 		  break;
   9502 		}
   9503 	      if (j + 1 < iverneed->vn_cnt)
   9504 		ivernaux->vna_nextptr = ivernaux + 1;
   9505 
   9506 	      if (ivernaux->vna_next
   9507 		  > (size_t) (contents_end - (bfd_byte *) evernaux))
   9508 		goto error_return_bad_verref;
   9509 
   9510 	      evernaux = ((Elf_External_Vernaux *)
   9511 			  ((bfd_byte *) evernaux + ivernaux->vna_next));
   9512 	    }
   9513 
   9514 	  iverneed->vn_nextref = NULL;
   9515 	  if (iverneed->vn_next == 0)
   9516 	    break;
   9517 	  if (hdr != NULL && (i + 1 < hdr->sh_info))
   9518 	    iverneed->vn_nextref = iverneed + 1;
   9519 
   9520 	  if (iverneed->vn_next
   9521 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   9522 	    goto error_return_bad_verref;
   9523 
   9524 	  everneed = ((Elf_External_Verneed *)
   9525 		      ((bfd_byte *) everneed + iverneed->vn_next));
   9526 	}
   9527       elf_tdata (abfd)->cverrefs = i;
   9528 
   9529       if (contents != elf_tdata (abfd)->dt_verneed)
   9530 	_bfd_munmap_temporary (contents_addr, contents_size);
   9531       contents = NULL;
   9532       contents_addr = NULL;
   9533     }
   9534 
   9535   if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
   9536     {
   9537       Elf_Internal_Shdr *hdr;
   9538       Elf_External_Verdef *everdef;
   9539       Elf_Internal_Verdef *iverdef;
   9540       Elf_Internal_Verdef *iverdefarr;
   9541       Elf_Internal_Verdef iverdefmem;
   9542       unsigned int i;
   9543       unsigned int maxidx;
   9544       bfd_byte *contents_end_def, *contents_end_aux;
   9545       size_t verdef_count;
   9546       size_t verdef_size;
   9547 
   9548       if (elf_tdata (abfd)->dt_verdef != NULL)
   9549 	{
   9550 	  hdr = NULL;
   9551 	  contents = elf_tdata (abfd)->dt_verdef;
   9552 	  verdef_count = elf_tdata (abfd)->dt_verdef_count;
   9553 	  verdef_size = verdef_count * sizeof (Elf_External_Verdef);
   9554 	}
   9555       else
   9556 	{
   9557 	  hdr = &elf_tdata (abfd)->dynverdef_hdr;
   9558 
   9559 	  if (hdr->sh_size < sizeof (Elf_External_Verdef))
   9560 	    {
   9561 	    error_return_bad_verdef:
   9562 	      _bfd_error_handler
   9563 		(_("%pB: .gnu.version_d invalid entry"), abfd);
   9564 	      bfd_set_error (bfd_error_bad_value);
   9565 	    error_return_verdef:
   9566 	      elf_tdata (abfd)->verdef = NULL;
   9567 	      elf_tdata (abfd)->cverdefs = 0;
   9568 	      goto error_return;
   9569 	    }
   9570 
   9571 	  if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
   9572 	    goto error_return_verdef;
   9573 	  contents_size = hdr->sh_size;
   9574 	  contents = _bfd_mmap_temporary (abfd, contents_size,
   9575 					  &contents_addr, &contents_size);
   9576 	  if (contents == NULL)
   9577 	    goto error_return_verdef;
   9578 
   9579 	  BFD_ASSERT (sizeof (Elf_External_Verdef)
   9580 		      >= sizeof (Elf_External_Verdaux));
   9581 
   9582 	  verdef_count = hdr->sh_info;
   9583 	  verdef_size = hdr->sh_size;
   9584 	}
   9585 
   9586       contents_end_def = (contents + verdef_size
   9587 			  - sizeof (Elf_External_Verdef));
   9588       contents_end_aux = (contents + verdef_size
   9589 			  - sizeof (Elf_External_Verdaux));
   9590 
   9591       /* We know the number of entries in the section but not the maximum
   9592 	 index.  Therefore we have to run through all entries and find
   9593 	 the maximum.  */
   9594       everdef = (Elf_External_Verdef *) contents;
   9595       maxidx = 0;
   9596       for (i = 0; i < verdef_count; ++i)
   9597 	{
   9598 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   9599 
   9600 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
   9601 	    goto error_return_bad_verdef;
   9602 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
   9603 	    maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
   9604 
   9605 	  if (iverdefmem.vd_next == 0)
   9606 	    break;
   9607 
   9608 	  if (iverdefmem.vd_next
   9609 	      > (size_t) (contents_end_def - (bfd_byte *) everdef))
   9610 	    goto error_return_bad_verdef;
   9611 
   9612 	  everdef = ((Elf_External_Verdef *)
   9613 		     ((bfd_byte *) everdef + iverdefmem.vd_next));
   9614 	}
   9615 
   9616       if (default_imported_symver)
   9617 	{
   9618 	  if (freeidx > maxidx)
   9619 	    maxidx = ++freeidx;
   9620 	  else
   9621 	    freeidx = ++maxidx;
   9622 	}
   9623       if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
   9624 	{
   9625 	  bfd_set_error (bfd_error_file_too_big);
   9626 	  goto error_return_verdef;
   9627 	}
   9628 
   9629       if (amt == 0)
   9630 	goto error_return_verdef;
   9631       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
   9632       if (elf_tdata (abfd)->verdef == NULL)
   9633 	goto error_return_verdef;
   9634 
   9635       elf_tdata (abfd)->cverdefs = maxidx;
   9636 
   9637       everdef = (Elf_External_Verdef *) contents;
   9638       iverdefarr = elf_tdata (abfd)->verdef;
   9639       for (i = 0; i < verdef_count; ++i)
   9640 	{
   9641 	  Elf_External_Verdaux *everdaux;
   9642 	  Elf_Internal_Verdaux *iverdaux;
   9643 	  unsigned int j;
   9644 
   9645 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   9646 
   9647 	  if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
   9648 	    goto error_return_bad_verdef;
   9649 
   9650 	  iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
   9651 	  memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
   9652 
   9653 	  iverdef->vd_bfd = abfd;
   9654 
   9655 	  if (iverdef->vd_cnt == 0)
   9656 	    iverdef->vd_auxptr = NULL;
   9657 	  else
   9658 	    {
   9659 	      if (_bfd_mul_overflow (iverdef->vd_cnt,
   9660 				     sizeof (Elf_Internal_Verdaux), &amt))
   9661 		{
   9662 		  bfd_set_error (bfd_error_file_too_big);
   9663 		  goto error_return_verdef;
   9664 		}
   9665 	      iverdef->vd_auxptr = (struct elf_internal_verdaux *)
   9666 		bfd_alloc (abfd, amt);
   9667 	      if (iverdef->vd_auxptr == NULL)
   9668 		goto error_return_verdef;
   9669 	    }
   9670 
   9671 	  if (iverdef->vd_aux
   9672 	      > (size_t) (contents_end_aux - (bfd_byte *) everdef))
   9673 	    goto error_return_bad_verdef;
   9674 
   9675 	  everdaux = ((Elf_External_Verdaux *)
   9676 		      ((bfd_byte *) everdef + iverdef->vd_aux));
   9677 	  iverdaux = iverdef->vd_auxptr;
   9678 	  for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
   9679 	    {
   9680 	      _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
   9681 
   9682 	      if (elf_use_dt_symtab_p (abfd))
   9683 		{
   9684 		  if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz)
   9685 		    iverdaux->vda_nodename
   9686 		      = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name;
   9687 		  else
   9688 		    iverdaux->vda_nodename = NULL;
   9689 		}
   9690 	      else
   9691 		iverdaux->vda_nodename
   9692 		  = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   9693 						     iverdaux->vda_name);
   9694 	      if (iverdaux->vda_nodename == NULL)
   9695 		goto error_return_bad_verdef;
   9696 
   9697 	      iverdaux->vda_nextptr = NULL;
   9698 	      if (iverdaux->vda_next == 0)
   9699 		{
   9700 		  iverdef->vd_cnt = j + 1;
   9701 		  break;
   9702 		}
   9703 	      if (j + 1 < iverdef->vd_cnt)
   9704 		iverdaux->vda_nextptr = iverdaux + 1;
   9705 
   9706 	      if (iverdaux->vda_next
   9707 		  > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
   9708 		goto error_return_bad_verdef;
   9709 
   9710 	      everdaux = ((Elf_External_Verdaux *)
   9711 			  ((bfd_byte *) everdaux + iverdaux->vda_next));
   9712 	    }
   9713 
   9714 	  iverdef->vd_nodename = NULL;
   9715 	  if (iverdef->vd_cnt)
   9716 	    iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
   9717 
   9718 	  iverdef->vd_nextdef = NULL;
   9719 	  if (iverdef->vd_next == 0)
   9720 	    break;
   9721 	  if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
   9722 	    iverdef->vd_nextdef = iverdef + 1;
   9723 
   9724 	  everdef = ((Elf_External_Verdef *)
   9725 		     ((bfd_byte *) everdef + iverdef->vd_next));
   9726 	}
   9727 
   9728       if (contents != elf_tdata (abfd)->dt_verdef)
   9729 	_bfd_munmap_temporary (contents_addr, contents_size);
   9730       contents = NULL;
   9731       contents_addr = NULL;
   9732     }
   9733   else if (default_imported_symver)
   9734     {
   9735       if (freeidx < 3)
   9736 	freeidx = 3;
   9737       else
   9738 	freeidx++;
   9739 
   9740       if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
   9741 	{
   9742 	  bfd_set_error (bfd_error_file_too_big);
   9743 	  goto error_return;
   9744 	}
   9745       if (amt == 0)
   9746 	goto error_return;
   9747       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
   9748       if (elf_tdata (abfd)->verdef == NULL)
   9749 	goto error_return;
   9750 
   9751       elf_tdata (abfd)->cverdefs = freeidx;
   9752     }
   9753 
   9754   /* Create a default version based on the soname.  */
   9755   if (default_imported_symver)
   9756     {
   9757       Elf_Internal_Verdef *iverdef;
   9758       Elf_Internal_Verdaux *iverdaux;
   9759 
   9760       iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
   9761 
   9762       iverdef->vd_version = VER_DEF_CURRENT;
   9763       iverdef->vd_flags = 0;
   9764       iverdef->vd_ndx = freeidx;
   9765       iverdef->vd_cnt = 1;
   9766 
   9767       iverdef->vd_bfd = abfd;
   9768 
   9769       iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
   9770       if (iverdef->vd_nodename == NULL)
   9771 	goto error_return_verdef;
   9772       iverdef->vd_nextdef = NULL;
   9773       iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
   9774 			    bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
   9775       if (iverdef->vd_auxptr == NULL)
   9776 	goto error_return_verdef;
   9777 
   9778       iverdaux = iverdef->vd_auxptr;
   9779       iverdaux->vda_nodename = iverdef->vd_nodename;
   9780     }
   9781 
   9782   return true;
   9783 
   9784  error_return:
   9785   if (contents != elf_tdata (abfd)->dt_verneed
   9786       && contents != elf_tdata (abfd)->dt_verdef)
   9787     _bfd_munmap_temporary (contents_addr, contents_size);
   9788   return false;
   9789 }
   9790 
   9791 asymbol *
   9793 _bfd_elf_make_empty_symbol (bfd *abfd)
   9794 {
   9795   elf_symbol_type *newsym;
   9796 
   9797   newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
   9798   if (!newsym)
   9799     return NULL;
   9800   newsym->symbol.the_bfd = abfd;
   9801   return &newsym->symbol;
   9802 }
   9803 
   9804 void
   9805 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
   9806 			  asymbol *symbol,
   9807 			  symbol_info *ret)
   9808 {
   9809   bfd_symbol_info (symbol, ret);
   9810 }
   9811 
   9812 /* Return whether a symbol name implies a local symbol.  Most targets
   9813    use this function for the is_local_label_name entry point, but some
   9814    override it.  */
   9815 
   9816 bool
   9817 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
   9818 			      const char *name)
   9819 {
   9820   /* Normal local symbols start with ``.L''.  */
   9821   if (name[0] == '.' && name[1] == 'L')
   9822     return true;
   9823 
   9824   /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
   9825      DWARF debugging symbols starting with ``..''.  */
   9826   if (name[0] == '.' && name[1] == '.')
   9827     return true;
   9828 
   9829   /* gcc will sometimes generate symbols beginning with ``_.L_'' when
   9830      emitting DWARF debugging output.  I suspect this is actually a
   9831      small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
   9832      ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
   9833      underscore to be emitted on some ELF targets).  For ease of use,
   9834      we treat such symbols as local.  */
   9835   if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
   9836     return true;
   9837 
   9838   /* Treat assembler generated fake symbols, dollar local labels and
   9839      forward-backward labels (aka local labels) as locals.
   9840      These labels have the form:
   9841 
   9842        L0^A.*				       (fake symbols)
   9843 
   9844        [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
   9845 
   9846      Versions which start with .L will have already been matched above,
   9847      so we only need to match the rest.  */
   9848   if (name[0] == 'L' && ISDIGIT (name[1]))
   9849     {
   9850       bool ret = false;
   9851       const char * p;
   9852       char c;
   9853 
   9854       for (p = name + 2; (c = *p); p++)
   9855 	{
   9856 	  if (c == 1 || c == 2)
   9857 	    {
   9858 	      if (c == 1 && p == name + 2)
   9859 		/* A fake symbol.  */
   9860 		return true;
   9861 
   9862 	      /* FIXME: We are being paranoid here and treating symbols like
   9863 		 L0^Bfoo as if there were non-local, on the grounds that the
   9864 		 assembler will never generate them.  But can any symbol
   9865 		 containing an ASCII value in the range 1-31 ever be anything
   9866 		 other than some kind of local ?  */
   9867 	      ret = true;
   9868 	    }
   9869 
   9870 	  if (! ISDIGIT (c))
   9871 	    {
   9872 	      ret = false;
   9873 	      break;
   9874 	    }
   9875 	}
   9876       return ret;
   9877     }
   9878 
   9879   return false;
   9880 }
   9881 
   9882 alent *
   9883 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
   9884 		     asymbol *symbol ATTRIBUTE_UNUSED)
   9885 {
   9886   abort ();
   9887   return NULL;
   9888 }
   9889 
   9890 bool
   9891 _bfd_elf_set_arch_mach (bfd *abfd,
   9892 			enum bfd_architecture arch,
   9893 			unsigned long machine)
   9894 {
   9895   /* If this isn't the right architecture for this backend, and this
   9896      isn't the generic backend, fail.  */
   9897   if (arch != get_elf_backend_data (abfd)->arch
   9898       && arch != bfd_arch_unknown
   9899       && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
   9900     return false;
   9901 
   9902   return bfd_default_set_arch_mach (abfd, arch, machine);
   9903 }
   9904 
   9905 /* Find the nearest line to a particular section and offset,
   9906    for error reporting.  */
   9907 
   9908 bool
   9909 _bfd_elf_find_nearest_line (bfd *abfd,
   9910 			    asymbol **symbols,
   9911 			    asection *section,
   9912 			    bfd_vma offset,
   9913 			    const char **filename_ptr,
   9914 			    const char **functionname_ptr,
   9915 			    unsigned int *line_ptr,
   9916 			    unsigned int *discriminator_ptr)
   9917 {
   9918   return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
   9919 					      offset, filename_ptr,
   9920 					      functionname_ptr, line_ptr,
   9921 					      discriminator_ptr);
   9922 }
   9923 
   9924 /* Find the nearest line to a particular section and offset,
   9925    for error reporting.  ALT_BFD representing a .gnu_debugaltlink file
   9926    can be optionally specified.  */
   9927 
   9928 bool
   9929 _bfd_elf_find_nearest_line_with_alt (bfd *abfd,
   9930 				     const char *alt_filename,
   9931 				     asymbol **symbols,
   9932 				     asection *section,
   9933 				     bfd_vma offset,
   9934 				     const char **filename_ptr,
   9935 				     const char **functionname_ptr,
   9936 				     unsigned int *line_ptr,
   9937 				     unsigned int *discriminator_ptr)
   9938 {
   9939   bool found;
   9940 
   9941   if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
   9942 					      section, offset, filename_ptr,
   9943 					      functionname_ptr, line_ptr,
   9944 					      discriminator_ptr,
   9945 					      dwarf_debug_sections,
   9946 					      &elf_tdata (abfd)->dwarf2_find_line_info))
   9947     return true;
   9948 
   9949   if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
   9950 				     filename_ptr, functionname_ptr, line_ptr))
   9951     {
   9952       if (!*functionname_ptr)
   9953 	_bfd_elf_find_function (abfd, symbols, section, offset,
   9954 				*filename_ptr ? NULL : filename_ptr,
   9955 				functionname_ptr);
   9956       return true;
   9957     }
   9958 
   9959   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
   9960 					     &found, filename_ptr,
   9961 					     functionname_ptr, line_ptr,
   9962 					     &elf_tdata (abfd)->line_info))
   9963     return false;
   9964   if (found && (*functionname_ptr || *line_ptr))
   9965     return true;
   9966 
   9967   if (symbols == NULL)
   9968     return false;
   9969 
   9970   if (! _bfd_elf_find_function (abfd, symbols, section, offset,
   9971 				filename_ptr, functionname_ptr))
   9972     return false;
   9973 
   9974   *line_ptr = 0;
   9975   return true;
   9976 }
   9977 
   9978 /* Find the line for a symbol.  */
   9979 
   9980 bool
   9981 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
   9982 		    const char **filename_ptr, unsigned int *line_ptr)
   9983 {
   9984   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   9985   return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
   9986 					filename_ptr, NULL, line_ptr, NULL,
   9987 					dwarf_debug_sections,
   9988 					&tdata->dwarf2_find_line_info);
   9989 }
   9990 
   9991 /* After a call to bfd_find_nearest_line, successive calls to
   9992    bfd_find_inliner_info can be used to get source information about
   9993    each level of function inlining that terminated at the address
   9994    passed to bfd_find_nearest_line.  Currently this is only supported
   9995    for DWARF2 with appropriate DWARF3 extensions. */
   9996 
   9997 bool
   9998 _bfd_elf_find_inliner_info (bfd *abfd,
   9999 			    const char **filename_ptr,
   10000 			    const char **functionname_ptr,
   10001 			    unsigned int *line_ptr)
   10002 {
   10003   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   10004   return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
   10005 					functionname_ptr, line_ptr,
   10006 					&tdata->dwarf2_find_line_info);
   10007 }
   10008 
   10009 int
   10010 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
   10011 {
   10012   elf_backend_data *bed = get_elf_backend_data (abfd);
   10013   int ret = bed->s->sizeof_ehdr;
   10014 
   10015   if (!bfd_link_relocatable (info))
   10016     {
   10017       bfd_size_type phdr_size = elf_program_header_size (abfd);
   10018 
   10019       if (phdr_size == (bfd_size_type) -1)
   10020 	{
   10021 	  struct elf_segment_map *m;
   10022 
   10023 	  phdr_size = 0;
   10024 	  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   10025 	    phdr_size += bed->s->sizeof_phdr;
   10026 
   10027 	  if (phdr_size == 0)
   10028 	    phdr_size = get_program_header_size (abfd, info);
   10029 	}
   10030 
   10031       elf_program_header_size (abfd) = phdr_size;
   10032       ret += phdr_size;
   10033     }
   10034 
   10035   return ret;
   10036 }
   10037 
   10038 bool
   10039 _bfd_elf_set_section_contents (bfd *abfd,
   10040 			       sec_ptr section,
   10041 			       const void *location,
   10042 			       file_ptr offset,
   10043 			       bfd_size_type count)
   10044 {
   10045   Elf_Internal_Shdr *hdr;
   10046 
   10047   if (! abfd->output_has_begun
   10048       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   10049     return false;
   10050 
   10051   if (!count)
   10052     return true;
   10053 
   10054   hdr = &elf_section_data (section)->this_hdr;
   10055   if (hdr->sh_offset == (file_ptr) -1)
   10056     {
   10057       unsigned char *contents;
   10058 
   10059       if (bfd_section_is_ctf (section))
   10060 	/* Nothing to do with this section: the contents are generated
   10061 	   later.  */
   10062 	return true;
   10063 
   10064       if ((offset + count) > hdr->sh_size)
   10065 	{
   10066 	  _bfd_error_handler
   10067 	    (_("%pB:%pA: error: attempting to write"
   10068 	       " over the end of the section"),
   10069 	     abfd, section);
   10070 
   10071 	  bfd_set_error (bfd_error_invalid_operation);
   10072 	  return false;
   10073 	}
   10074 
   10075       contents = hdr->contents;
   10076       if (contents == NULL)
   10077 	{
   10078 	  _bfd_error_handler
   10079 	    (_("%pB:%pA: error: attempting to write"
   10080 	       " section into an empty buffer"),
   10081 	     abfd, section);
   10082 
   10083 	  bfd_set_error (bfd_error_invalid_operation);
   10084 	  return false;
   10085 	}
   10086 
   10087       memcpy (contents + offset, location, count);
   10088       return true;
   10089     }
   10090 
   10091   return _bfd_generic_set_section_contents (abfd, section,
   10092 					    location, offset, count);
   10093 }
   10094 
   10095 bool
   10096 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
   10097 			   arelent *cache_ptr ATTRIBUTE_UNUSED,
   10098 			   Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
   10099 {
   10100   abort ();
   10101   return false;
   10102 }
   10103 
   10104 /* Try to convert a non-ELF reloc into an ELF one.  */
   10105 
   10106 bool
   10107 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
   10108 {
   10109   /* Check whether we really have an ELF howto.  */
   10110 
   10111   if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
   10112     {
   10113       bfd_reloc_code_real_type code;
   10114       reloc_howto_type *howto;
   10115 
   10116       /* Alien reloc: Try to determine its type to replace it with an
   10117 	 equivalent ELF reloc.  */
   10118 
   10119       if (areloc->howto->pc_relative)
   10120 	{
   10121 	  switch (areloc->howto->bitsize)
   10122 	    {
   10123 	    case 8:
   10124 	      code = BFD_RELOC_8_PCREL;
   10125 	      break;
   10126 	    case 12:
   10127 	      code = BFD_RELOC_12_PCREL;
   10128 	      break;
   10129 	    case 16:
   10130 	      code = BFD_RELOC_16_PCREL;
   10131 	      break;
   10132 	    case 24:
   10133 	      code = BFD_RELOC_24_PCREL;
   10134 	      break;
   10135 	    case 32:
   10136 	      code = BFD_RELOC_32_PCREL;
   10137 	      break;
   10138 	    case 64:
   10139 	      code = BFD_RELOC_64_PCREL;
   10140 	      break;
   10141 	    default:
   10142 	      goto fail;
   10143 	    }
   10144 
   10145 	  howto = bfd_reloc_type_lookup (abfd, code);
   10146 
   10147 	  if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
   10148 	    {
   10149 	      if (howto->pcrel_offset)
   10150 		areloc->addend += areloc->address;
   10151 	      else
   10152 		areloc->addend -= areloc->address; /* addend is unsigned!! */
   10153 	    }
   10154 	}
   10155       else
   10156 	{
   10157 	  switch (areloc->howto->bitsize)
   10158 	    {
   10159 	    case 8:
   10160 	      code = BFD_RELOC_8;
   10161 	      break;
   10162 	    case 14:
   10163 	      code = BFD_RELOC_14;
   10164 	      break;
   10165 	    case 16:
   10166 	      code = BFD_RELOC_16;
   10167 	      break;
   10168 	    case 26:
   10169 	      code = BFD_RELOC_26;
   10170 	      break;
   10171 	    case 32:
   10172 	      code = BFD_RELOC_32;
   10173 	      break;
   10174 	    case 64:
   10175 	      code = BFD_RELOC_64;
   10176 	      break;
   10177 	    default:
   10178 	      goto fail;
   10179 	    }
   10180 
   10181 	  howto = bfd_reloc_type_lookup (abfd, code);
   10182 	}
   10183 
   10184       if (howto)
   10185 	areloc->howto = howto;
   10186       else
   10187 	goto fail;
   10188     }
   10189 
   10190   return true;
   10191 
   10192  fail:
   10193   /* xgettext:c-format */
   10194   _bfd_error_handler (_("%pB: %s unsupported"),
   10195 		      abfd, areloc->howto->name);
   10196   bfd_set_error (bfd_error_sorry);
   10197   return false;
   10198 }
   10199 
   10200 bool
   10201 _bfd_elf_free_cached_info (bfd *abfd)
   10202 {
   10203   struct elf_obj_tdata *tdata;
   10204 
   10205   if ((bfd_get_format (abfd) == bfd_object
   10206        || bfd_get_format (abfd) == bfd_core)
   10207       && (tdata = elf_tdata (abfd)) != NULL)
   10208     {
   10209       if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
   10210 	_bfd_elf_strtab_free (elf_shstrtab (abfd));
   10211       _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
   10212       _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
   10213       _bfd_stab_cleanup (abfd, &tdata->line_info);
   10214       for (asection *sec = abfd->sections; sec != NULL; sec = sec->next)
   10215 	{
   10216 	  _bfd_elf_munmap_section_contents (sec, sec->contents);
   10217 	  if (!sec->alloced)
   10218 	    {
   10219 	      free (elf_section_data (sec)->this_hdr.contents);
   10220 	      elf_section_data (sec)->this_hdr.contents = NULL;
   10221 	    }
   10222 	  free (elf_section_data (sec)->relocs);
   10223 	  elf_section_data (sec)->relocs = NULL;
   10224 	  if (sec->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
   10225 	    {
   10226 	      struct eh_frame_sec_info *sec_info = sec->sec_info;
   10227 	      free (sec_info->cies);
   10228 	    }
   10229 	}
   10230       free (tdata->symtab_hdr.contents);
   10231       tdata->symtab_hdr.contents = NULL;
   10232     }
   10233 
   10234   return _bfd_generic_bfd_free_cached_info (abfd);
   10235 }
   10236 
   10237 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
   10238    in the relocation's offset.  Thus we cannot allow any sort of sanity
   10239    range-checking to interfere.  There is nothing else to do in processing
   10240    this reloc.  */
   10241 
   10242 bfd_reloc_status_type
   10243 _bfd_elf_rel_vtable_reloc_fn
   10244   (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
   10245    struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
   10246    void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
   10247    bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
   10248 {
   10249   return bfd_reloc_ok;
   10250 }
   10251 
   10252 /* Elf core file support.  Much of this only works on native
   10254    toolchains, since we rely on knowing the
   10255    machine-dependent procfs structure in order to pick
   10256    out details about the corefile.  */
   10257 
   10258 #ifdef HAVE_SYS_PROCFS_H
   10259 # include <sys/procfs.h>
   10260 #endif
   10261 
   10262 /* Return a PID that identifies a "thread" for threaded cores, or the
   10263    PID of the main process for non-threaded cores.  */
   10264 
   10265 static int
   10266 elfcore_make_pid (bfd *abfd)
   10267 {
   10268   int pid;
   10269 
   10270   pid = elf_tdata (abfd)->core->lwpid;
   10271   if (pid == 0)
   10272     pid = elf_tdata (abfd)->core->pid;
   10273 
   10274   return pid;
   10275 }
   10276 
   10277 /* If there isn't a section called NAME, make one, using data from
   10278    SECT.  Note, this function will generate a reference to NAME, so
   10279    you shouldn't deallocate or overwrite it.  */
   10280 
   10281 static bool
   10282 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
   10283 {
   10284   asection *sect2;
   10285 
   10286   if (bfd_get_section_by_name (abfd, name) != NULL)
   10287     return true;
   10288 
   10289   sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
   10290   if (sect2 == NULL)
   10291     return false;
   10292 
   10293   sect2->size = sect->size;
   10294   sect2->filepos = sect->filepos;
   10295   sect2->alignment_power = sect->alignment_power;
   10296   return true;
   10297 }
   10298 
   10299 /* Create a pseudosection containing SIZE bytes at FILEPOS.  This
   10300    actually creates up to two pseudosections:
   10301    - For the single-threaded case, a section named NAME, unless
   10302      such a section already exists.
   10303    - For the multi-threaded case, a section named "NAME/PID", where
   10304      PID is elfcore_make_pid (abfd).
   10305    Both pseudosections have identical contents.  */
   10306 bool
   10307 _bfd_elfcore_make_pseudosection (bfd *abfd,
   10308 				 char *name,
   10309 				 size_t size,
   10310 				 ufile_ptr filepos)
   10311 {
   10312   char buf[100];
   10313   char *threaded_name;
   10314   size_t len;
   10315   asection *sect;
   10316 
   10317   /* Build the section name.  */
   10318 
   10319   sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
   10320   len = strlen (buf) + 1;
   10321   threaded_name = (char *) bfd_alloc (abfd, len);
   10322   if (threaded_name == NULL)
   10323     return false;
   10324   memcpy (threaded_name, buf, len);
   10325 
   10326   sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
   10327 					     SEC_HAS_CONTENTS);
   10328   if (sect == NULL)
   10329     return false;
   10330   sect->size = size;
   10331   sect->filepos = filepos;
   10332   sect->alignment_power = 2;
   10333 
   10334   return elfcore_maybe_make_sect (abfd, name, sect);
   10335 }
   10336 
   10337 static bool
   10338 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
   10339 				size_t offs)
   10340 {
   10341   asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
   10342 						       SEC_HAS_CONTENTS);
   10343 
   10344   if (sect == NULL)
   10345     return false;
   10346 
   10347   sect->size = note->descsz - offs;
   10348   sect->filepos = note->descpos + offs;
   10349   sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   10350 
   10351   return true;
   10352 }
   10353 
   10354 /* prstatus_t exists on:
   10355      solaris 2.5+
   10356      linux 2.[01] + glibc
   10357      unixware 4.2
   10358 */
   10359 
   10360 #if defined (HAVE_PRSTATUS_T)
   10361 
   10362 static bool
   10363 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   10364 {
   10365   size_t size;
   10366   int offset;
   10367 
   10368   if (note->descsz == sizeof (prstatus_t))
   10369     {
   10370       prstatus_t prstat;
   10371 
   10372       size = sizeof (prstat.pr_reg);
   10373       offset   = offsetof (prstatus_t, pr_reg);
   10374       memcpy (&prstat, note->descdata, sizeof (prstat));
   10375 
   10376       /* Do not overwrite the core signal if it
   10377 	 has already been set by another thread.  */
   10378       if (elf_tdata (abfd)->core->signal == 0)
   10379 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   10380       if (elf_tdata (abfd)->core->pid == 0)
   10381 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   10382 
   10383       /* pr_who exists on:
   10384 	 solaris 2.5+
   10385 	 unixware 4.2
   10386 	 pr_who doesn't exist on:
   10387 	 linux 2.[01]
   10388 	 */
   10389 #if defined (HAVE_PRSTATUS_T_PR_WHO)
   10390       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   10391 #else
   10392       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   10393 #endif
   10394     }
   10395 #if defined (HAVE_PRSTATUS32_T)
   10396   else if (note->descsz == sizeof (prstatus32_t))
   10397     {
   10398       /* 64-bit host, 32-bit corefile */
   10399       prstatus32_t prstat;
   10400 
   10401       size = sizeof (prstat.pr_reg);
   10402       offset   = offsetof (prstatus32_t, pr_reg);
   10403       memcpy (&prstat, note->descdata, sizeof (prstat));
   10404 
   10405       /* Do not overwrite the core signal if it
   10406 	 has already been set by another thread.  */
   10407       if (elf_tdata (abfd)->core->signal == 0)
   10408 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   10409       if (elf_tdata (abfd)->core->pid == 0)
   10410 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   10411 
   10412       /* pr_who exists on:
   10413 	 solaris 2.5+
   10414 	 unixware 4.2
   10415 	 pr_who doesn't exist on:
   10416 	 linux 2.[01]
   10417 	 */
   10418 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
   10419       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   10420 #else
   10421       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   10422 #endif
   10423     }
   10424 #endif /* HAVE_PRSTATUS32_T */
   10425   else
   10426     {
   10427       /* Fail - we don't know how to handle any other
   10428 	 note size (ie. data object type).  */
   10429       return true;
   10430     }
   10431 
   10432   /* Make a ".reg/999" section and a ".reg" section.  */
   10433   return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG,
   10434 					  size, note->descpos + offset);
   10435 }
   10436 #endif /* defined (HAVE_PRSTATUS_T) */
   10437 
   10438 /* Create a pseudosection containing the exact contents of NOTE.  */
   10439 static bool
   10440 elfcore_make_note_pseudosection (bfd *abfd,
   10441 				 char *name,
   10442 				 Elf_Internal_Note *note)
   10443 {
   10444   return _bfd_elfcore_make_pseudosection (abfd, name,
   10445 					  note->descsz, note->descpos);
   10446 }
   10447 
   10448 /* There isn't a consistent prfpregset_t across platforms,
   10449    but it doesn't matter, because we don't have to pick this
   10450    data structure apart.  */
   10451 
   10452 static bool
   10453 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
   10454 {
   10455   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
   10456 }
   10457 
   10458 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
   10459    type of NT_PRXFPREG.  Just include the whole note's contents
   10460    literally.  */
   10461 
   10462 static bool
   10463 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
   10464 {
   10465   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XFP, note);
   10466 }
   10467 
   10468 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
   10469    with a note type of NT_X86_XSTATE.  Just include the whole note's
   10470    contents literally.  */
   10471 
   10472 static bool
   10473 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
   10474 {
   10475   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XSTATE, note);
   10476 }
   10477 
   10478 static bool
   10479 elfcore_grok_sspreg (bfd *abfd, Elf_Internal_Note *note)
   10480 {
   10481   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_SSP, note);
   10482 }
   10483 
   10484 /* Linux dumps the XSAVE Layout description in a note named "LINUX"
   10485    with a note type of NT_X86_XSAVE_LAYOUT. */
   10486 static bool
   10487 elfcore_grok_xsave_layout_desc (bfd *abfd, Elf_Internal_Note *note)
   10488 {
   10489   return elfcore_make_note_pseudosection (abfd,
   10490 					  NOTE_PSEUDO_SECTION_XSAVE_LAYOUT,
   10491 					  note);
   10492 }
   10493 
   10494 static bool
   10495 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
   10496 {
   10497   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_VMX, note);
   10498 }
   10499 
   10500 static bool
   10501 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
   10502 {
   10503   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_VSX, note);
   10504 }
   10505 
   10506 static bool
   10507 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
   10508 {
   10509   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TAR, note);
   10510 }
   10511 
   10512 static bool
   10513 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
   10514 {
   10515   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_PPR, note);
   10516 }
   10517 
   10518 static bool
   10519 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
   10520 {
   10521   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_DSCR, note);
   10522 }
   10523 
   10524 static bool
   10525 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
   10526 {
   10527   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_EBB, note);
   10528 }
   10529 
   10530 static bool
   10531 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
   10532 {
   10533   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_PMU, note);
   10534 }
   10535 
   10536 static bool
   10537 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
   10538 {
   10539   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CGPR, note);
   10540 }
   10541 
   10542 static bool
   10543 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
   10544 {
   10545   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CFPR, note);
   10546 }
   10547 
   10548 static bool
   10549 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
   10550 {
   10551   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CVMX, note);
   10552 }
   10553 
   10554 static bool
   10555 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
   10556 {
   10557   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CVSX, note);
   10558 }
   10559 
   10560 static bool
   10561 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
   10562 {
   10563   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_SPR, note);
   10564 }
   10565 
   10566 static bool
   10567 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
   10568 {
   10569   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CTAR, note);
   10570 }
   10571 
   10572 static bool
   10573 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
   10574 {
   10575   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CPPR, note);
   10576 }
   10577 
   10578 static bool
   10579 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
   10580 {
   10581   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CDSCR, note);
   10582 }
   10583 
   10584 static bool
   10585 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
   10586 {
   10587   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_HIGH_GPRS, note);
   10588 }
   10589 
   10590 static bool
   10591 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
   10592 {
   10593   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TIMER, note);
   10594 }
   10595 
   10596 static bool
   10597 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
   10598 {
   10599   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TODCMP, note);
   10600 }
   10601 
   10602 static bool
   10603 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
   10604 {
   10605   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TODPREG, note);
   10606 }
   10607 
   10608 static bool
   10609 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
   10610 {
   10611   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_CTRS, note);
   10612 }
   10613 
   10614 static bool
   10615 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
   10616 {
   10617   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_PREFIX, note);
   10618 }
   10619 
   10620 static bool
   10621 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
   10622 {
   10623   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_LAST_BREAK, note);
   10624 }
   10625 
   10626 static bool
   10627 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
   10628 {
   10629   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_SYSTEM_CALL, note);
   10630 }
   10631 
   10632 static bool
   10633 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
   10634 {
   10635   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TDB, note);
   10636 }
   10637 
   10638 static bool
   10639 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
   10640 {
   10641   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_VXRS_LOW, note);
   10642 }
   10643 
   10644 static bool
   10645 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
   10646 {
   10647   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_VXRS_HIGH, note);
   10648 }
   10649 
   10650 static bool
   10651 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
   10652 {
   10653   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_GS_CB, note);
   10654 }
   10655 
   10656 static bool
   10657 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
   10658 {
   10659   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_GS_BC, note);
   10660 }
   10661 
   10662 static bool
   10663 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
   10664 {
   10665   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_ARM_VFP, note);
   10666 }
   10667 
   10668 static bool
   10669 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
   10670 {
   10671   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_TLS, note);
   10672 }
   10673 
   10674 static bool
   10675 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
   10676 {
   10677   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_HW_BREAK, note);
   10678 }
   10679 
   10680 static bool
   10681 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
   10682 {
   10683   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_HW_WATCH, note);
   10684 }
   10685 
   10686 static bool
   10687 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
   10688 {
   10689   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_SVE, note);
   10690 }
   10691 
   10692 static bool
   10693 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
   10694 {
   10695   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_PAUTH, note);
   10696 }
   10697 
   10698 static bool
   10699 elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
   10700 {
   10701   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_MTE, note);
   10702 }
   10703 
   10704 static bool
   10705 elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note)
   10706 {
   10707   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_SSVE, note);
   10708 }
   10709 
   10710 static bool
   10711 elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note)
   10712 {
   10713   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_ZA, note);
   10714 }
   10715 
   10716 /* Convert NOTE into a bfd_section called ".reg-aarch-zt".  Return TRUE if
   10717    successful, otherwise return FALSE.  */
   10718 
   10719 static bool
   10720 elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note)
   10721 {
   10722   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_ZT, note);
   10723 }
   10724 
   10725 /* Convert NOTE into a bfd_section called ".reg-aarch-gcs".  Return TRUE if
   10726    successful, otherwise return FALSE.  */
   10727 
   10728 static bool
   10729 elfcore_grok_aarch_gcs (bfd *abfd, Elf_Internal_Note *note)
   10730 {
   10731   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_GCS, note);
   10732 }
   10733 
   10734 /* Convert NOTE into the appropriate note pseudo-section for the AArch64 FPMR.
   10735  * Return TRUE if successful, otherwise return FALSE.  */
   10736 
   10737 static bool
   10738 elfcore_grok_aarch_fpmr (bfd *abfd, Elf_Internal_Note *note)
   10739 {
   10740   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_FPMR, note);
   10741 }
   10742 
   10743 static bool
   10744 elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
   10745 {
   10746   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_ARC_V2, note);
   10747 }
   10748 
   10749 /* Convert NOTE into a bfd_section called ".reg-riscv-csr".  Return TRUE if
   10750    successful otherwise, return FALSE.  */
   10751 
   10752 static bool
   10753 elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
   10754 {
   10755   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_RISCV_CSR, note);
   10756 }
   10757 
   10758 /* Convert NOTE into a bfd_section called ".gdb-tdesc".  Return TRUE if
   10759    successful otherwise, return FALSE.  */
   10760 
   10761 static bool
   10762 elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
   10763 {
   10764   return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
   10765 }
   10766 
   10767 static bool
   10768 elfcore_grok_i386_tls (bfd *abfd, Elf_Internal_Note *note)
   10769 {
   10770   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_I386_TLS,
   10771 					  note);
   10772 }
   10773 
   10774 static bool
   10775 elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
   10776 {
   10777   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_CPUCFG, note);
   10778 }
   10779 
   10780 static bool
   10781 elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
   10782 {
   10783   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LBT, note);
   10784 }
   10785 
   10786 static bool
   10787 elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
   10788 {
   10789   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LSX, note);
   10790 }
   10791 
   10792 static bool
   10793 elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
   10794 {
   10795   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LASX, note);
   10796 }
   10797 
   10798 #if defined (HAVE_PRPSINFO_T)
   10799 typedef prpsinfo_t   elfcore_psinfo_t;
   10800 #if defined (HAVE_PRPSINFO32_T)		/* Sparc64 cross Sparc32 */
   10801 typedef prpsinfo32_t elfcore_psinfo32_t;
   10802 #endif
   10803 #endif
   10804 
   10805 #if defined (HAVE_PSINFO_T)
   10806 typedef psinfo_t   elfcore_psinfo_t;
   10807 #if defined (HAVE_PSINFO32_T)		/* Sparc64 cross Sparc32 */
   10808 typedef psinfo32_t elfcore_psinfo32_t;
   10809 #endif
   10810 #endif
   10811 
   10812 /* return a malloc'ed copy of a string at START which is at
   10813    most MAX bytes long, possibly without a terminating '\0'.
   10814    the copy will always have a terminating '\0'.  */
   10815 
   10816 char *
   10817 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
   10818 {
   10819   char *dups;
   10820   char *end = (char *) memchr (start, '\0', max);
   10821   size_t len;
   10822 
   10823   if (end == NULL)
   10824     len = max;
   10825   else
   10826     len = end - start;
   10827 
   10828   dups = (char *) bfd_alloc (abfd, len + 1);
   10829   if (dups == NULL)
   10830     return NULL;
   10831 
   10832   memcpy (dups, start, len);
   10833   dups[len] = '\0';
   10834 
   10835   return dups;
   10836 }
   10837 
   10838 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   10839 static bool
   10840 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   10841 {
   10842   if (note->descsz == sizeof (elfcore_psinfo_t))
   10843     {
   10844       elfcore_psinfo_t psinfo;
   10845 
   10846       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   10847 
   10848 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
   10849       elf_tdata (abfd)->core->pid = psinfo.pr_pid;
   10850 #endif
   10851       elf_tdata (abfd)->core->program
   10852 	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
   10853 				sizeof (psinfo.pr_fname));
   10854 
   10855       elf_tdata (abfd)->core->command
   10856 	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
   10857 				sizeof (psinfo.pr_psargs));
   10858     }
   10859 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   10860   else if (note->descsz == sizeof (elfcore_psinfo32_t))
   10861     {
   10862       /* 64-bit host, 32-bit corefile */
   10863       elfcore_psinfo32_t psinfo;
   10864 
   10865       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   10866 
   10867 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
   10868       elf_tdata (abfd)->core->pid = psinfo.pr_pid;
   10869 #endif
   10870       elf_tdata (abfd)->core->program
   10871 	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
   10872 				sizeof (psinfo.pr_fname));
   10873 
   10874       elf_tdata (abfd)->core->command
   10875 	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
   10876 				sizeof (psinfo.pr_psargs));
   10877     }
   10878 #endif
   10879 
   10880   else
   10881     {
   10882       /* Fail - we don't know how to handle any other
   10883 	 note size (ie. data object type).  */
   10884       return true;
   10885     }
   10886 
   10887   /* Note that for some reason, a spurious space is tacked
   10888      onto the end of the args in some (at least one anyway)
   10889      implementations, so strip it off if it exists.  */
   10890 
   10891   {
   10892     char *command = elf_tdata (abfd)->core->command;
   10893     int n = strlen (command);
   10894 
   10895     if (0 < n && command[n - 1] == ' ')
   10896       command[n - 1] = '\0';
   10897   }
   10898 
   10899   return true;
   10900 }
   10901 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
   10902 
   10903 #if defined (HAVE_PSTATUS_T)
   10904 static bool
   10905 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
   10906 {
   10907   if (note->descsz == sizeof (pstatus_t)
   10908 #if defined (HAVE_PXSTATUS_T)
   10909       || note->descsz == sizeof (pxstatus_t)
   10910 #endif
   10911       )
   10912     {
   10913       pstatus_t pstat;
   10914 
   10915       memcpy (&pstat, note->descdata, sizeof (pstat));
   10916 
   10917       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   10918     }
   10919 #if defined (HAVE_PSTATUS32_T)
   10920   else if (note->descsz == sizeof (pstatus32_t))
   10921     {
   10922       /* 64-bit host, 32-bit corefile */
   10923       pstatus32_t pstat;
   10924 
   10925       memcpy (&pstat, note->descdata, sizeof (pstat));
   10926 
   10927       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   10928     }
   10929 #endif
   10930   /* Could grab some more details from the "representative"
   10931      lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
   10932      NT_LWPSTATUS note, presumably.  */
   10933 
   10934   return true;
   10935 }
   10936 #endif /* defined (HAVE_PSTATUS_T) */
   10937 
   10938 #if defined (HAVE_LWPSTATUS_T)
   10939 static bool
   10940 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
   10941 {
   10942   lwpstatus_t lwpstat;
   10943   char buf[100];
   10944   char *name;
   10945   size_t len;
   10946   asection *sect;
   10947 
   10948   if (note->descsz != sizeof (lwpstat)
   10949 #if defined (HAVE_LWPXSTATUS_T)
   10950       && note->descsz != sizeof (lwpxstatus_t)
   10951 #endif
   10952       )
   10953     return true;
   10954 
   10955   memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
   10956 
   10957   elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
   10958   /* Do not overwrite the core signal if it has already been set by
   10959      another thread.  */
   10960   if (elf_tdata (abfd)->core->signal == 0)
   10961     elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
   10962 
   10963   /* Make a ".reg/999" section.  */
   10964 
   10965   sprintf (buf, NOTE_PSEUDO_SECTION_REG "/%d", elfcore_make_pid (abfd));
   10966   len = strlen (buf) + 1;
   10967   name = bfd_alloc (abfd, len);
   10968   if (name == NULL)
   10969     return false;
   10970   memcpy (name, buf, len);
   10971 
   10972   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10973   if (sect == NULL)
   10974     return false;
   10975 
   10976 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   10977   sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
   10978   sect->filepos = note->descpos
   10979     + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
   10980 #endif
   10981 
   10982 #if defined (HAVE_LWPSTATUS_T_PR_REG)
   10983   sect->size = sizeof (lwpstat.pr_reg);
   10984   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
   10985 #endif
   10986 
   10987   sect->alignment_power = 2;
   10988 
   10989   if (!elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG, sect))
   10990     return false;
   10991 
   10992   /* Make a ".reg2/999" section */
   10993 
   10994   sprintf (buf, NOTE_PSEUDO_SECTION_REG2 "/%d", elfcore_make_pid (abfd));
   10995   len = strlen (buf) + 1;
   10996   name = bfd_alloc (abfd, len);
   10997   if (name == NULL)
   10998     return false;
   10999   memcpy (name, buf, len);
   11000 
   11001   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11002   if (sect == NULL)
   11003     return false;
   11004 
   11005 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   11006   sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
   11007   sect->filepos = note->descpos
   11008     + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
   11009 #endif
   11010 
   11011 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
   11012   sect->size = sizeof (lwpstat.pr_fpreg);
   11013   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
   11014 #endif
   11015 
   11016   sect->alignment_power = 2;
   11017 
   11018   return elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG2, sect);
   11019 }
   11020 #endif /* defined (HAVE_LWPSTATUS_T) */
   11021 
   11022 /* These constants, and the structure offsets used below, are defined by
   11023    Cygwin's core_dump.h */
   11024 #define NOTE_INFO_PROCESS  1
   11025 #define NOTE_INFO_THREAD   2
   11026 #define NOTE_INFO_MODULE   3
   11027 #define NOTE_INFO_MODULE64 4
   11028 
   11029 static bool
   11030 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
   11031 {
   11032   char buf[30];
   11033   char *name;
   11034   size_t len;
   11035   unsigned int name_size;
   11036   asection *sect;
   11037   unsigned int type;
   11038   int is_active_thread;
   11039   bfd_vma base_addr;
   11040 
   11041   if (note->descsz < 4)
   11042     return true;
   11043 
   11044   if (! startswith (note->namedata, "win32"))
   11045     return true;
   11046 
   11047   type = bfd_get_32 (abfd, note->descdata);
   11048 
   11049   static const struct
   11050   {
   11051     const char *type_name;
   11052     unsigned long min_size;
   11053   } size_check[] =
   11054       {
   11055        { "NOTE_INFO_PROCESS", 12 },
   11056        { "NOTE_INFO_THREAD", 12 },
   11057        { "NOTE_INFO_MODULE", 12 },
   11058        { "NOTE_INFO_MODULE64", 16 },
   11059       };
   11060 
   11061   if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
   11062       return true;
   11063 
   11064   if (note->descsz < size_check[type - 1].min_size)
   11065     {
   11066       _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
   11067 			    " is too small"),
   11068 			  abfd, size_check[type - 1].type_name, note->descsz);
   11069       return true;
   11070     }
   11071 
   11072   switch (type)
   11073     {
   11074     case NOTE_INFO_PROCESS:
   11075       /* FIXME: need to add ->core->command.  */
   11076       elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
   11077       elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
   11078       break;
   11079 
   11080     case NOTE_INFO_THREAD:
   11081       /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
   11082 	 structure. */
   11083       /* thread_info.tid */
   11084       sprintf (buf, NOTE_PSEUDO_SECTION_REG "/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
   11085 
   11086       len = strlen (buf) + 1;
   11087       name = (char *) bfd_alloc (abfd, len);
   11088       if (name == NULL)
   11089 	return false;
   11090 
   11091       memcpy (name, buf, len);
   11092 
   11093       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11094       if (sect == NULL)
   11095 	return false;
   11096 
   11097       /* sizeof (thread_info.thread_context) */
   11098       sect->size = note->descsz - 12;
   11099       /* offsetof (thread_info.thread_context) */
   11100       sect->filepos = note->descpos + 12;
   11101       sect->alignment_power = 2;
   11102 
   11103       /* thread_info.is_active_thread */
   11104       is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
   11105 
   11106       if (is_active_thread)
   11107 	if (! elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG, sect))
   11108 	  return false;
   11109       break;
   11110 
   11111     case NOTE_INFO_MODULE:
   11112     case NOTE_INFO_MODULE64:
   11113       /* Make a ".module/xxxxxxxx" section.  */
   11114       if (type == NOTE_INFO_MODULE)
   11115 	{
   11116 	  /* module_info.base_address */
   11117 	  base_addr = bfd_get_32 (abfd, note->descdata + 4);
   11118 	  sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
   11119 	  /* module_info.module_name_size */
   11120 	  name_size = bfd_get_32 (abfd, note->descdata + 8);
   11121 	}
   11122       else /* NOTE_INFO_MODULE64 */
   11123 	{
   11124 	  /* module_info.base_address */
   11125 	  base_addr = bfd_get_64 (abfd, note->descdata + 4);
   11126 	  sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
   11127 	  /* module_info.module_name_size */
   11128 	  name_size = bfd_get_32 (abfd, note->descdata + 12);
   11129 	}
   11130 
   11131       len = strlen (buf) + 1;
   11132       name = (char *) bfd_alloc (abfd, len);
   11133       if (name == NULL)
   11134 	return false;
   11135 
   11136       memcpy (name, buf, len);
   11137 
   11138       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11139 
   11140       if (sect == NULL)
   11141 	return false;
   11142 
   11143       if (note->descsz < 12 + name_size)
   11144 	{
   11145 	  _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
   11146 				" is too small to contain a name of size %u"),
   11147 			      abfd, note->descsz, name_size);
   11148 	  return true;
   11149 	}
   11150 
   11151       sect->size = note->descsz;
   11152       sect->filepos = note->descpos;
   11153       sect->alignment_power = 2;
   11154       break;
   11155 
   11156     default:
   11157       return true;
   11158     }
   11159 
   11160   return true;
   11161 }
   11162 
   11163 static bool
   11164 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
   11165 {
   11166   elf_backend_data *bed = get_elf_backend_data (abfd);
   11167 
   11168   /* Short cut for LINUX notes.  */
   11169   if (note->namesz == 6 /* strlen (NOTE_NAME_LINUX) + 1 */
   11170       && streq (note->namedata, NOTE_NAME_LINUX))
   11171     {
   11172       switch (note->type)
   11173 	{
   11174 	case NT_386_TLS:	return elfcore_grok_i386_tls (abfd, note);
   11175 	case NT_ARC_V2:		return elfcore_grok_arc_v2 (abfd, note);
   11176 	case NT_ARM_FPMR:	return elfcore_grok_aarch_fpmr (abfd, note);
   11177 	case NT_ARM_GCS:	return elfcore_grok_aarch_gcs (abfd, note);
   11178 	case NT_ARM_HW_BREAK:	return elfcore_grok_aarch_hw_break (abfd, note);
   11179 	case NT_ARM_HW_WATCH:	return elfcore_grok_aarch_hw_watch (abfd, note);
   11180 	case NT_ARM_PAC_MASK:	return elfcore_grok_aarch_pauth (abfd, note);
   11181 	case NT_ARM_SSVE:	return elfcore_grok_aarch_ssve (abfd, note);
   11182 	case NT_ARM_SVE:	return elfcore_grok_aarch_sve (abfd, note);
   11183 	case NT_ARM_TAGGED_ADDR_CTRL: return elfcore_grok_aarch_mte (abfd, note);
   11184 	case NT_ARM_TLS:	return elfcore_grok_aarch_tls (abfd, note);
   11185 	case NT_ARM_VFP:	return elfcore_grok_arm_vfp (abfd, note);
   11186 	case NT_ARM_ZA:		return elfcore_grok_aarch_za (abfd, note);
   11187 	case NT_ARM_ZT:		return elfcore_grok_aarch_zt (abfd, note);
   11188 	case NT_LARCH_CPUCFG:	return elfcore_grok_loongarch_cpucfg (abfd, note);
   11189 	case NT_LARCH_LASX:	return elfcore_grok_loongarch_lasx (abfd, note);
   11190 	case NT_LARCH_LBT:	return elfcore_grok_loongarch_lbt (abfd, note);
   11191 	case NT_LARCH_LSX:	return elfcore_grok_loongarch_lsx (abfd, note);
   11192 	case NT_PPC_DSCR:	return elfcore_grok_ppc_dscr (abfd, note);
   11193 	case NT_PPC_EBB:	return elfcore_grok_ppc_ebb (abfd, note);
   11194 	case NT_PPC_PMU:	return elfcore_grok_ppc_pmu (abfd, note);
   11195 	case NT_PPC_PPR:	return elfcore_grok_ppc_ppr (abfd, note);
   11196 	case NT_PPC_TAR:	return elfcore_grok_ppc_tar (abfd, note);
   11197 	case NT_PPC_TM_CDSCR:	return elfcore_grok_ppc_tm_cdscr (abfd, note);
   11198 	case NT_PPC_TM_CFPR:	return elfcore_grok_ppc_tm_cfpr (abfd, note);
   11199 	case NT_PPC_TM_CGPR:	return elfcore_grok_ppc_tm_cgpr (abfd, note);
   11200 	case NT_PPC_TM_CPPR:	return elfcore_grok_ppc_tm_cppr (abfd, note);
   11201 	case NT_PPC_TM_CTAR:	return elfcore_grok_ppc_tm_ctar (abfd, note);
   11202 	case NT_PPC_TM_CVMX:	return elfcore_grok_ppc_tm_cvmx (abfd, note);
   11203 	case NT_PPC_TM_CVSX:	return elfcore_grok_ppc_tm_cvsx (abfd, note);
   11204 	case NT_PPC_TM_SPR:	return elfcore_grok_ppc_tm_spr (abfd, note);
   11205 	case NT_PPC_VMX:	return elfcore_grok_ppc_vmx (abfd, note);
   11206 	case NT_PPC_VSX:	return elfcore_grok_ppc_vsx (abfd, note);
   11207 	case NT_PRXFPREG:	return elfcore_grok_prxfpreg (abfd, note);
   11208 	case NT_S390_CTRS:	return elfcore_grok_s390_ctrs (abfd, note);
   11209 	case NT_S390_GS_BC:	return elfcore_grok_s390_gs_bc (abfd, note);
   11210 	case NT_S390_GS_CB:	return elfcore_grok_s390_gs_cb (abfd, note);
   11211 	case NT_S390_HIGH_GPRS:	return elfcore_grok_s390_high_gprs (abfd, note);
   11212 	case NT_S390_LAST_BREAK:  return elfcore_grok_s390_last_break (abfd, note);
   11213 	case NT_S390_PREFIX:	return elfcore_grok_s390_prefix (abfd, note);
   11214 	case NT_S390_SYSTEM_CALL: return elfcore_grok_s390_system_call (abfd, note);
   11215 	case NT_S390_TDB:	return elfcore_grok_s390_tdb (abfd, note);
   11216 	case NT_S390_TIMER:	return elfcore_grok_s390_timer (abfd, note);
   11217 	case NT_S390_TODCMP:	return elfcore_grok_s390_todcmp (abfd, note);
   11218 	case NT_S390_TODPREG:	return elfcore_grok_s390_todpreg (abfd, note);
   11219 	case NT_S390_VXRS_HIGH:	return elfcore_grok_s390_vxrs_high (abfd, note);
   11220 	case NT_S390_VXRS_LOW:	return elfcore_grok_s390_vxrs_low (abfd, note);
   11221 	case NT_X86_SHSTK:	return elfcore_grok_sspreg (abfd, note);
   11222 	case NT_X86_XSTATE:	return elfcore_grok_xstatereg (abfd, note);
   11223 	case NT_X86_XSAVE_LAYOUT: return elfcore_grok_xsave_layout_desc (abfd, note);
   11224 	default: break;
   11225 	}
   11226     }
   11227 
   11228   switch (note->type)
   11229     {
   11230     default:
   11231       return true;
   11232 
   11233     case NT_PRSTATUS:
   11234       if (bed->elf_backend_grok_prstatus)
   11235 	if ((*bed->elf_backend_grok_prstatus) (abfd, note))
   11236 	  return true;
   11237 #if defined (HAVE_PRSTATUS_T)
   11238       return elfcore_grok_prstatus (abfd, note);
   11239 #else
   11240       return true;
   11241 #endif
   11242 
   11243 #if defined (HAVE_PSTATUS_T)
   11244     case NT_PSTATUS:
   11245       return elfcore_grok_pstatus (abfd, note);
   11246 #endif
   11247 
   11248 #if defined (HAVE_LWPSTATUS_T)
   11249     case NT_LWPSTATUS:
   11250       return elfcore_grok_lwpstatus (abfd, note);
   11251 #endif
   11252 
   11253     case NT_FPREGSET:		/* FIXME: rename to NT_PRFPREG.  */
   11254       return elfcore_grok_prfpreg (abfd, note);
   11255 
   11256     case NT_WIN32PSTATUS:
   11257       return elfcore_grok_win32pstatus (abfd, note);
   11258 
   11259     case NT_GDB_TDESC:
   11260       if (note->namesz == 4 && streq (note->namedata, NOTE_NAME_GDB))
   11261 	return elfcore_grok_gdb_tdesc (abfd, note);
   11262       else
   11263 	return true;
   11264 
   11265     case NT_RISCV_CSR:
   11266       if (note->namesz == 4 && streq (note->namedata, NOTE_NAME_GDB))
   11267 	return elfcore_grok_riscv_csr (abfd, note);
   11268       else
   11269 	return true;
   11270 
   11271     case NT_PRPSINFO:
   11272     case NT_PSINFO:
   11273       if (bed->elf_backend_grok_psinfo)
   11274 	if ((*bed->elf_backend_grok_psinfo) (abfd, note))
   11275 	  return true;
   11276 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   11277       return elfcore_grok_psinfo (abfd, note);
   11278 #else
   11279       return true;
   11280 #endif
   11281 
   11282     case NT_AUXV:
   11283       return elfcore_make_auxv_note_section (abfd, note, 0);
   11284 
   11285     case NT_FILE:
   11286       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
   11287 					      note);
   11288 
   11289     case NT_SIGINFO:
   11290       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
   11291 					      note);
   11292     }
   11293 }
   11294 
   11295 static bool
   11296 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
   11297 {
   11298   struct bfd_build_id* build_id;
   11299 
   11300   if (note->descsz == 0)
   11301     return false;
   11302 
   11303   build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
   11304   if (build_id == NULL)
   11305     return false;
   11306 
   11307   build_id->size = note->descsz;
   11308   memcpy (build_id->data, note->descdata, note->descsz);
   11309   abfd->build_id = build_id;
   11310 
   11311   return true;
   11312 }
   11313 
   11314 static bool
   11315 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
   11316 {
   11317   switch (note->type)
   11318     {
   11319     default:
   11320       return true;
   11321 
   11322     case NT_GNU_PROPERTY_TYPE_0:
   11323       return _bfd_elf_parse_gnu_properties (abfd, note);
   11324 
   11325     case NT_GNU_BUILD_ID:
   11326       return elfobj_grok_gnu_build_id (abfd, note);
   11327     }
   11328 }
   11329 
   11330 static bool
   11331 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
   11332 {
   11333   struct sdt_note *cur =
   11334     (struct sdt_note *) bfd_alloc (abfd,
   11335 				   sizeof (struct sdt_note) + note->descsz);
   11336 
   11337   cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
   11338   cur->size = (bfd_size_type) note->descsz;
   11339   memcpy (cur->data, note->descdata, note->descsz);
   11340 
   11341   elf_tdata (abfd)->sdt_note_head = cur;
   11342 
   11343   return true;
   11344 }
   11345 
   11346 static bool
   11347 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
   11348 {
   11349   switch (note->type)
   11350     {
   11351     case NT_STAPSDT:
   11352       return elfobj_grok_stapsdt_note_1 (abfd, note);
   11353 
   11354     default:
   11355       return true;
   11356     }
   11357 }
   11358 
   11359 static bool
   11360 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
   11361 {
   11362   size_t offset;
   11363 
   11364   switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
   11365     {
   11366     case ELFCLASS32:
   11367       if (note->descsz < 108)
   11368 	return false;
   11369       break;
   11370 
   11371     case ELFCLASS64:
   11372       if (note->descsz < 120)
   11373 	return false;
   11374       break;
   11375 
   11376     default:
   11377       return false;
   11378     }
   11379 
   11380   /* Check for version 1 in pr_version.  */
   11381   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   11382     return false;
   11383 
   11384   offset = 4;
   11385 
   11386   /* Skip over pr_psinfosz. */
   11387   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
   11388     offset += 4;
   11389   else
   11390     {
   11391       offset += 4;	/* Padding before pr_psinfosz. */
   11392       offset += 8;
   11393     }
   11394 
   11395   /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
   11396   elf_tdata (abfd)->core->program
   11397     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
   11398   offset += 17;
   11399 
   11400   /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
   11401   elf_tdata (abfd)->core->command
   11402     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
   11403   offset += 81;
   11404 
   11405   /* Padding before pr_pid.  */
   11406   offset += 2;
   11407 
   11408   /* The pr_pid field was added in version "1a".  */
   11409   if (note->descsz < offset + 4)
   11410     return true;
   11411 
   11412   elf_tdata (abfd)->core->pid
   11413     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11414 
   11415   return true;
   11416 }
   11417 
   11418 static bool
   11419 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
   11420 {
   11421   size_t offset;
   11422   size_t size;
   11423   size_t min_size;
   11424 
   11425   /* Compute offset of pr_getregsz, skipping over pr_statussz.
   11426      Also compute minimum size of this note.  */
   11427   switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
   11428     {
   11429     case ELFCLASS32:
   11430       offset = 4 + 4;
   11431       min_size = offset + (4 * 2) + 4 + 4 + 4;
   11432       break;
   11433 
   11434     case ELFCLASS64:
   11435       offset = 4 + 4 + 8;	/* Includes padding before pr_statussz.  */
   11436       min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
   11437       break;
   11438 
   11439     default:
   11440       return false;
   11441     }
   11442 
   11443   if (note->descsz < min_size)
   11444     return false;
   11445 
   11446   /* Check for version 1 in pr_version.  */
   11447   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   11448     return false;
   11449 
   11450   /* Extract size of pr_reg from pr_gregsetsz.  */
   11451   /* Skip over pr_gregsetsz and pr_fpregsetsz.  */
   11452   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
   11453     {
   11454       size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11455       offset += 4 * 2;
   11456     }
   11457   else
   11458     {
   11459       size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
   11460       offset += 8 * 2;
   11461     }
   11462 
   11463   /* Skip over pr_osreldate.  */
   11464   offset += 4;
   11465 
   11466   /* Read signal from pr_cursig.  */
   11467   if (elf_tdata (abfd)->core->signal == 0)
   11468     elf_tdata (abfd)->core->signal
   11469       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11470   offset += 4;
   11471 
   11472   /* Read TID from pr_pid.  */
   11473   elf_tdata (abfd)->core->lwpid
   11474       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11475   offset += 4;
   11476 
   11477   /* Padding before pr_reg.  */
   11478   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
   11479     offset += 4;
   11480 
   11481   /* Make sure that there is enough data remaining in the note.  */
   11482   if ((note->descsz - offset) < size)
   11483     return false;
   11484 
   11485   /* Make a ".reg/999" section and a ".reg" section.  */
   11486   return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG,
   11487 					  size, note->descpos + offset);
   11488 }
   11489 
   11490 static bool
   11491 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
   11492 {
   11493   elf_backend_data *bed = get_elf_backend_data (abfd);
   11494 
   11495   switch (note->type)
   11496     {
   11497     case NT_PRSTATUS:
   11498       if (bed->elf_backend_grok_freebsd_prstatus)
   11499 	if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
   11500 	  return true;
   11501       return elfcore_grok_freebsd_prstatus (abfd, note);
   11502 
   11503     case NT_FPREGSET:
   11504       return elfcore_grok_prfpreg (abfd, note);
   11505 
   11506     case NT_PRPSINFO:
   11507       return elfcore_grok_freebsd_psinfo (abfd, note);
   11508 
   11509     case NT_FREEBSD_THRMISC:
   11510       return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
   11511 
   11512     case NT_FREEBSD_PROCSTAT_PROC:
   11513       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
   11514 					      note);
   11515 
   11516     case NT_FREEBSD_PROCSTAT_FILES:
   11517       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
   11518 					      note);
   11519 
   11520     case NT_FREEBSD_PROCSTAT_VMMAP:
   11521       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
   11522 					      note);
   11523 
   11524     case NT_FREEBSD_PROCSTAT_AUXV:
   11525       return elfcore_make_auxv_note_section (abfd, note, 4);
   11526 
   11527     case NT_FREEBSD_X86_SEGBASES:
   11528       return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_X86_SEGBASES, note);
   11529 
   11530     case NT_X86_XSTATE:
   11531       return elfcore_grok_xstatereg (abfd, note);
   11532 
   11533     case NT_FREEBSD_PTLWPINFO:
   11534       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
   11535 					      note);
   11536 
   11537     case NT_ARM_TLS:
   11538       return elfcore_grok_aarch_tls (abfd, note);
   11539 
   11540     case NT_ARM_VFP:
   11541       return elfcore_grok_arm_vfp (abfd, note);
   11542 
   11543     default:
   11544       return true;
   11545     }
   11546 }
   11547 
   11548 static bool
   11549 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
   11550 {
   11551   char *cp;
   11552 
   11553   cp = strchr (note->namedata, '@');
   11554   if (cp != NULL)
   11555     {
   11556       *lwpidp = atoi(cp + 1);
   11557       return true;
   11558     }
   11559   return false;
   11560 }
   11561 
   11562 static bool
   11563 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   11564 {
   11565   if (note->descsz <= 0x7c + 31)
   11566     return false;
   11567 
   11568   /* Signal number at offset 0x08. */
   11569   elf_tdata (abfd)->core->signal
   11570     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   11571 
   11572   /* Process ID at offset 0x50. */
   11573   elf_tdata (abfd)->core->pid
   11574     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
   11575 
   11576   /* Command name at 0x7c (max 32 bytes, including nul). */
   11577   elf_tdata (abfd)->core->command
   11578     = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
   11579 
   11580   return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
   11581 					  note);
   11582 }
   11583 
   11584 static bool
   11585 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
   11586 {
   11587   int lwp;
   11588 
   11589   if (elfcore_netbsd_get_lwpid (note, &lwp))
   11590     elf_tdata (abfd)->core->lwpid = lwp;
   11591 
   11592   switch (note->type)
   11593     {
   11594     case NT_NETBSDCORE_PROCINFO:
   11595       /* NetBSD-specific core "procinfo".  Note that we expect to
   11596 	 find this note before any of the others, which is fine,
   11597 	 since the kernel writes this note out first when it
   11598 	 creates a core file.  */
   11599       return elfcore_grok_netbsd_procinfo (abfd, note);
   11600     case NT_NETBSDCORE_AUXV:
   11601       /* NetBSD-specific Elf Auxiliary Vector data. */
   11602       return elfcore_make_auxv_note_section (abfd, note, 4);
   11603     case NT_NETBSDCORE_LWPSTATUS:
   11604       return elfcore_make_note_pseudosection (abfd,
   11605 					      ".note.netbsdcore.lwpstatus",
   11606 					      note);
   11607     default:
   11608       break;
   11609     }
   11610 
   11611   /* As of March 2020 there are no other machine-independent notes
   11612      defined for NetBSD core files.  If the note type is less
   11613      than the start of the machine-dependent note types, we don't
   11614      understand it.  */
   11615 
   11616   if (note->type < NT_NETBSDCORE_FIRSTMACH)
   11617     return true;
   11618 
   11619 
   11620   switch (bfd_get_arch (abfd))
   11621     {
   11622       /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
   11623 	 PT_GETFPREGS == mach+2.  */
   11624 
   11625     case bfd_arch_aarch64:
   11626     case bfd_arch_alpha:
   11627     case bfd_arch_sparc:
   11628       switch (note->type)
   11629 	{
   11630 	case NT_NETBSDCORE_FIRSTMACH+0:
   11631 	  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
   11632 
   11633 	case NT_NETBSDCORE_FIRSTMACH+2:
   11634 	  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
   11635 
   11636 	default:
   11637 	  return true;
   11638 	}
   11639 
   11640       /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
   11641 	 There's also old PT___GETREGS40 == mach + 1 for old reg
   11642 	 structure which lacks GBR.  */
   11643 
   11644     case bfd_arch_sh:
   11645       switch (note->type)
   11646 	{
   11647 	case NT_NETBSDCORE_FIRSTMACH+3:
   11648 	  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
   11649 
   11650 	case NT_NETBSDCORE_FIRSTMACH+5:
   11651 	  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
   11652 
   11653 	default:
   11654 	  return true;
   11655 	}
   11656 
   11657       /* On all other arch's, PT_GETREGS == mach+1 and
   11658 	 PT_GETFPREGS == mach+3.  */
   11659 
   11660     default:
   11661       switch (note->type)
   11662 	{
   11663 	case NT_NETBSDCORE_FIRSTMACH+1:
   11664 	  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
   11665 
   11666 	case NT_NETBSDCORE_FIRSTMACH+3:
   11667 	  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
   11668 
   11669 	default:
   11670 	  return true;
   11671 	}
   11672     }
   11673     /* NOTREACHED */
   11674 }
   11675 
   11676 static bool
   11677 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   11678 {
   11679   if (note->descsz <= 0x48 + 31)
   11680     return false;
   11681 
   11682   /* Signal number at offset 0x08. */
   11683   elf_tdata (abfd)->core->signal
   11684     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   11685 
   11686   /* Process ID at offset 0x20. */
   11687   elf_tdata (abfd)->core->pid
   11688     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
   11689 
   11690   /* Command name at 0x48 (max 32 bytes, including nul). */
   11691   elf_tdata (abfd)->core->command
   11692     = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
   11693 
   11694   return true;
   11695 }
   11696 
   11697 /* Processes Solaris's process status note.
   11698    sig_off ~ offsetof(prstatus_t, pr_cursig)
   11699    pid_off ~ offsetof(prstatus_t, pr_pid)
   11700    lwpid_off ~ offsetof(prstatus_t, pr_who)
   11701    gregset_size ~ sizeof(gregset_t)
   11702    gregset_offset ~ offsetof(prstatus_t, pr_reg)  */
   11703 
   11704 static bool
   11705 elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
   11706 			       int pid_off, int lwpid_off, size_t gregset_size,
   11707 			       size_t gregset_offset)
   11708 {
   11709   asection *sect = NULL;
   11710   elf_tdata (abfd)->core->signal
   11711     = bfd_get_16 (abfd, note->descdata + sig_off);
   11712   elf_tdata (abfd)->core->pid
   11713     = bfd_get_32 (abfd, note->descdata + pid_off);
   11714   elf_tdata (abfd)->core->lwpid
   11715     = bfd_get_32 (abfd, note->descdata + lwpid_off);
   11716 
   11717   sect = bfd_get_section_by_name (abfd, NOTE_PSEUDO_SECTION_REG);
   11718   if (sect != NULL)
   11719     sect->size = gregset_size;
   11720 
   11721   return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, gregset_size,
   11722 					  note->descpos + gregset_offset);
   11723 }
   11724 
   11725 /* Gets program and arguments from a core.
   11726    prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
   11727    comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs)  */
   11728 
   11729 static bool
   11730 elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
   11731 			  int prog_off, int comm_off)
   11732 {
   11733   elf_tdata (abfd)->core->program
   11734     = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
   11735   elf_tdata (abfd)->core->command
   11736     = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
   11737 
   11738   return true;
   11739 }
   11740 
   11741 /* Processes Solaris's LWP status note.
   11742    gregset_size ~ sizeof(gregset_t)
   11743    gregset_off ~ offsetof(lwpstatus_t, pr_reg)
   11744    fpregset_size ~ sizeof(fpregset_t)
   11745    fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg)  */
   11746 
   11747 static bool
   11748 elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
   11749 				size_t gregset_size, int gregset_off,
   11750 				size_t fpregset_size, int fpregset_off)
   11751 {
   11752   asection *sect = NULL;
   11753   char reg2_section_name[16] = { 0 };
   11754 
   11755   (void) snprintf (reg2_section_name, 16, "%s/%i", NOTE_PSEUDO_SECTION_REG2,
   11756 		   elf_tdata (abfd)->core->lwpid);
   11757 
   11758   /* offsetof(lwpstatus_t, pr_lwpid) */
   11759   elf_tdata (abfd)->core->lwpid
   11760     = bfd_get_32 (abfd, note->descdata + 4);
   11761   /* offsetof(lwpstatus_t, pr_cursig) */
   11762   elf_tdata (abfd)->core->signal
   11763     = bfd_get_16 (abfd, note->descdata + 12);
   11764 
   11765   sect = bfd_get_section_by_name (abfd, NOTE_PSEUDO_SECTION_REG);
   11766   if (sect != NULL)
   11767     sect->size = gregset_size;
   11768   else if (!_bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, gregset_size,
   11769 					     note->descpos + gregset_off))
   11770     return false;
   11771 
   11772   sect = bfd_get_section_by_name (abfd, reg2_section_name);
   11773   if (sect != NULL)
   11774     {
   11775       sect->size = fpregset_size;
   11776       sect->filepos = note->descpos + fpregset_off;
   11777       sect->alignment_power = 2;
   11778     }
   11779   else if (!_bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, fpregset_size,
   11780 					     note->descpos + fpregset_off))
   11781     return false;
   11782 
   11783   return true;
   11784 }
   11785 
   11786 static bool
   11787 elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
   11788 {
   11789   if (note == NULL)
   11790     return false;
   11791 
   11792   /* core files are identified as 32- or 64-bit, SPARC or x86,
   11793      by the size of the descsz which matches the sizeof()
   11794      the type appropriate for that note type (e.g., prstatus_t for
   11795      SOLARIS_NT_PRSTATUS) for the corresponding architecture
   11796      on Solaris. The core file bitness may differ from the bitness of
   11797      gdb itself, so fixed values are used instead of sizeof().
   11798      Appropriate fixed offsets are also used to obtain data from
   11799      the note.  */
   11800 
   11801   switch ((int) note->type)
   11802     {
   11803     case SOLARIS_NT_PRSTATUS:
   11804       switch (note->descsz)
   11805 	{
   11806 	case 508: /* sizeof(prstatus_t) SPARC 32-bit */
   11807 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11808 					       136, 216, 308, 152, 356);
   11809 	case 904: /* sizeof(prstatus_t) SPARC 64-bit */
   11810 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11811 					       264, 360, 520, 304, 600);
   11812 	case 432: /* sizeof(prstatus_t) Intel 32-bit */
   11813 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11814 					       136, 216, 308, 76, 356);
   11815 	case 824: /* sizeof(prstatus_t) Intel 64-bit */
   11816 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11817 					       264, 360, 520, 224, 600);
   11818 	default:
   11819 	  return true;
   11820 	}
   11821 
   11822     case SOLARIS_NT_PSINFO:
   11823     case SOLARIS_NT_PRPSINFO:
   11824       switch (note->descsz)
   11825 	{
   11826 	case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
   11827 	  return elfcore_grok_solaris_info(abfd, note, 84, 100);
   11828 	case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
   11829 	  return elfcore_grok_solaris_info(abfd, note, 120, 136);
   11830 	case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
   11831 	  return elfcore_grok_solaris_info(abfd, note, 88, 104);
   11832 	case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
   11833 	  return elfcore_grok_solaris_info(abfd, note, 136, 152);
   11834 	default:
   11835 	  return true;
   11836 	}
   11837 
   11838     case SOLARIS_NT_LWPSTATUS:
   11839       switch (note->descsz)
   11840 	{
   11841 	case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
   11842 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11843 						152, 344, 400, 496);
   11844 	case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
   11845 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11846 						304, 544, 544, 848);
   11847 	case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
   11848 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11849 						76, 344, 380, 420);
   11850 	case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
   11851 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11852 						224, 544, 528, 768);
   11853 	default:
   11854 	  return true;
   11855 	}
   11856 
   11857     case SOLARIS_NT_LWPSINFO:
   11858       /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
   11859       if (note->descsz == 128 || note->descsz == 152)
   11860 	elf_tdata (abfd)->core->lwpid =
   11861 	  bfd_get_32 (abfd, note->descdata + 4);
   11862       break;
   11863 
   11864     default:
   11865       break;
   11866     }
   11867 
   11868   return true;
   11869 }
   11870 
   11871 /* For name starting with "CORE" this may be either a Solaris
   11872    core file or a gdb-generated core file.  Do Solaris-specific
   11873    processing on selected note types first with
   11874    elfcore_grok_solaris_note(), then process the note
   11875    in elfcore_grok_note().  */
   11876 
   11877 static bool
   11878 elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
   11879 {
   11880   if (!elfcore_grok_solaris_note_impl (abfd, note))
   11881     return false;
   11882 
   11883   return elfcore_grok_note (abfd, note);
   11884 }
   11885 
   11886 static bool
   11887 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
   11888 {
   11889   if (note->type == NT_OPENBSD_PROCINFO)
   11890     return elfcore_grok_openbsd_procinfo (abfd, note);
   11891 
   11892   if (note->type == NT_OPENBSD_REGS)
   11893     return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
   11894 
   11895   if (note->type == NT_OPENBSD_FPREGS)
   11896     return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
   11897 
   11898   if (note->type == NT_OPENBSD_XFPREGS)
   11899     return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XFP, note);
   11900 
   11901   if (note->type == NT_OPENBSD_AUXV)
   11902     return elfcore_make_auxv_note_section (abfd, note, 0);
   11903 
   11904   if (note->type == NT_OPENBSD_WCOOKIE)
   11905     {
   11906       asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
   11907 							   SEC_HAS_CONTENTS);
   11908 
   11909       if (sect == NULL)
   11910 	return false;
   11911       sect->size = note->descsz;
   11912       sect->filepos = note->descpos;
   11913       sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   11914 
   11915       return true;
   11916     }
   11917 
   11918   return true;
   11919 }
   11920 
   11921 static bool
   11922 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
   11923 {
   11924   void *ddata = note->descdata;
   11925   char buf[100];
   11926   char *name;
   11927   asection *sect;
   11928   short sig;
   11929   unsigned flags;
   11930 
   11931   if (note->descsz < 16)
   11932     return false;
   11933 
   11934   /* nto_procfs_status 'pid' field is at offset 0.  */
   11935   elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
   11936 
   11937   /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
   11938   *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
   11939 
   11940   /* nto_procfs_status 'flags' field is at offset 8.  */
   11941   flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
   11942 
   11943   /* nto_procfs_status 'what' field is at offset 14.  */
   11944   if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
   11945     {
   11946       elf_tdata (abfd)->core->signal = sig;
   11947       elf_tdata (abfd)->core->lwpid = *tid;
   11948     }
   11949 
   11950   /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
   11951      do not come from signals so we make sure we set the current
   11952      thread just in case.  */
   11953   if (flags & 0x00000080)
   11954     elf_tdata (abfd)->core->lwpid = *tid;
   11955 
   11956   /* Make a ".qnx_core_status/%d" section.  */
   11957   sprintf (buf, ".qnx_core_status/%ld", *tid);
   11958 
   11959   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   11960   if (name == NULL)
   11961     return false;
   11962   strcpy (name, buf);
   11963 
   11964   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11965   if (sect == NULL)
   11966     return false;
   11967 
   11968   sect->size		= note->descsz;
   11969   sect->filepos		= note->descpos;
   11970   sect->alignment_power = 2;
   11971 
   11972   return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
   11973 }
   11974 
   11975 static bool
   11976 elfcore_grok_nto_regs (bfd *abfd,
   11977 		       Elf_Internal_Note *note,
   11978 		       long tid,
   11979 		       char *base)
   11980 {
   11981   char buf[100];
   11982   char *name;
   11983   asection *sect;
   11984 
   11985   /* Make a "(base)/%d" section.  */
   11986   sprintf (buf, "%s/%ld", base, tid);
   11987 
   11988   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   11989   if (name == NULL)
   11990     return false;
   11991   strcpy (name, buf);
   11992 
   11993   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11994   if (sect == NULL)
   11995     return false;
   11996 
   11997   sect->size		= note->descsz;
   11998   sect->filepos		= note->descpos;
   11999   sect->alignment_power = 2;
   12000 
   12001   /* This is the current thread.  */
   12002   if (elf_tdata (abfd)->core->lwpid == tid)
   12003     return elfcore_maybe_make_sect (abfd, base, sect);
   12004 
   12005   return true;
   12006 }
   12007 
   12008 static bool
   12009 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
   12010 {
   12011   /* Every GREG section has a STATUS section before it.  Store the
   12012      tid from the previous call to pass down to the next gregs
   12013      function.  */
   12014   static long tid = 1;
   12015 
   12016   switch (note->type)
   12017     {
   12018     case QNT_CORE_INFO:
   12019       return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
   12020     case QNT_CORE_STATUS:
   12021       return elfcore_grok_nto_status (abfd, note, &tid);
   12022     case QNT_CORE_GREG:
   12023       return elfcore_grok_nto_regs (abfd, note, tid, NOTE_PSEUDO_SECTION_REG);
   12024     case QNT_CORE_FPREG:
   12025       return elfcore_grok_nto_regs (abfd, note, tid, NOTE_PSEUDO_SECTION_REG2);
   12026     default:
   12027       return true;
   12028     }
   12029 }
   12030 
   12031 static bool
   12032 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
   12033 {
   12034   char *name;
   12035   asection *sect;
   12036   size_t len;
   12037 
   12038   /* Use note name as section name.  */
   12039   len = note->namesz;
   12040   name = (char *) bfd_alloc (abfd, len);
   12041   if (name == NULL)
   12042     return false;
   12043   memcpy (name, note->namedata, len);
   12044   name[len - 1] = '\0';
   12045 
   12046   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   12047   if (sect == NULL)
   12048     return false;
   12049 
   12050   sect->size		= note->descsz;
   12051   sect->filepos		= note->descpos;
   12052   sect->alignment_power = 1;
   12053 
   12054   return true;
   12055 }
   12056 
   12057 /* Function: elfcore_write_note
   12058 
   12059    Inputs:
   12060      buffer to hold note, and current size of buffer
   12061      name of note
   12062      type of note
   12063      data for note
   12064      size of data for note
   12065 
   12066    Writes note to end of buffer.  ELF64 notes are written exactly as
   12067    for ELF32, despite the current (as of 2006) ELF gabi specifying
   12068    that they ought to have 8-byte namesz and descsz field, and have
   12069    8-byte alignment.  Other writers, eg. Linux kernel, do the same.
   12070 
   12071    Return:
   12072    Pointer to realloc'd buffer, *BUFSIZ updated.  */
   12073 
   12074 char *
   12075 elfcore_write_note (bfd *abfd,
   12076 		    char *buf,
   12077 		    int *bufsiz,
   12078 		    const char *name,
   12079 		    int type,
   12080 		    const void *input,
   12081 		    int size)
   12082 {
   12083   Elf_External_Note *xnp;
   12084   size_t namesz;
   12085   size_t newspace;
   12086   char *dest;
   12087 
   12088   namesz = 0;
   12089   if (name != NULL)
   12090     namesz = strlen (name) + 1;
   12091 
   12092   newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
   12093 
   12094   buf = (char *) realloc (buf, *bufsiz + newspace);
   12095   if (buf == NULL)
   12096     return buf;
   12097   dest = buf + *bufsiz;
   12098   *bufsiz += newspace;
   12099   xnp = (Elf_External_Note *) dest;
   12100   H_PUT_32 (abfd, namesz, xnp->namesz);
   12101   H_PUT_32 (abfd, size, xnp->descsz);
   12102   H_PUT_32 (abfd, type, xnp->type);
   12103   dest = xnp->name;
   12104   if (name != NULL)
   12105     {
   12106       memcpy (dest, name, namesz);
   12107       dest += namesz;
   12108       while (namesz & 3)
   12109 	{
   12110 	  *dest++ = '\0';
   12111 	  ++namesz;
   12112 	}
   12113     }
   12114   memcpy (dest, input, size);
   12115   dest += size;
   12116   while (size & 3)
   12117     {
   12118       *dest++ = '\0';
   12119       ++size;
   12120     }
   12121   return buf;
   12122 }
   12123 
   12124 /* gcc-8 warns (*) on all the strncpy calls in this function about
   12125    possible string truncation.  The "truncation" is not a bug.  We
   12126    have an external representation of structs with fields that are not
   12127    necessarily NULL terminated and corresponding internal
   12128    representation fields that are one larger so that they can always
   12129    be NULL terminated.
   12130    gcc versions between 4.2 and 4.6 do not allow pragma control of
   12131    diagnostics inside functions, giving a hard error if you try to use
   12132    the finer control available with later versions.
   12133    gcc prior to 4.2 warns about diagnostic push and pop.
   12134    gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
   12135    unless you also add #pragma GCC diagnostic ignored "-Wpragma".
   12136    (*) Depending on your system header files!  */
   12137 #if GCC_VERSION >= 8000
   12138 # pragma GCC diagnostic push
   12139 # pragma GCC diagnostic ignored "-Wstringop-truncation"
   12140 #endif
   12141 char *
   12142 elfcore_write_prpsinfo (bfd  *abfd,
   12143 			char *buf,
   12144 			int  *bufsiz,
   12145 			const char *fname,
   12146 			const char *psargs)
   12147 {
   12148   elf_backend_data *bed = get_elf_backend_data (abfd);
   12149 
   12150   if (bed->elf_backend_write_core_note != NULL)
   12151     {
   12152       char *ret;
   12153       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   12154 						 NT_PRPSINFO, fname, psargs);
   12155       if (ret != NULL)
   12156 	return ret;
   12157     }
   12158 
   12159 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   12160 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   12161   if (bed->s->elfclass == ELFCLASS32)
   12162     {
   12163 #  if defined (HAVE_PSINFO32_T)
   12164       psinfo32_t data;
   12165       int note_type = NT_PSINFO;
   12166 #  else
   12167       prpsinfo32_t data;
   12168       int note_type = NT_PRPSINFO;
   12169 #  endif
   12170 
   12171       memset (&data, 0, sizeof (data));
   12172       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   12173       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   12174       return elfcore_write_note (abfd, buf, bufsiz,
   12175 				 NOTE_NAME_CORE, note_type, &data, sizeof (data));
   12176     }
   12177   else
   12178 # endif
   12179     {
   12180 # if defined (HAVE_PSINFO_T)
   12181       psinfo_t data;
   12182       int note_type = NT_PSINFO;
   12183 # else
   12184       prpsinfo_t data;
   12185       int note_type = NT_PRPSINFO;
   12186 # endif
   12187 
   12188       memset (&data, 0, sizeof (data));
   12189       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   12190       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   12191       return elfcore_write_note (abfd, buf, bufsiz,
   12192 				 NOTE_NAME_CORE, note_type, &data, sizeof (data));
   12193     }
   12194 #endif	/* PSINFO_T or PRPSINFO_T */
   12195 
   12196   free (buf);
   12197   return NULL;
   12198 }
   12199 #if GCC_VERSION >= 8000
   12200 # pragma GCC diagnostic pop
   12201 #endif
   12202 
   12203 char *
   12204 elfcore_write_linux_prpsinfo32
   12205   (bfd *abfd, char *buf, int *bufsiz,
   12206    const struct elf_internal_linux_prpsinfo *prpsinfo)
   12207 {
   12208   if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
   12209     {
   12210       struct elf_external_linux_prpsinfo32_ugid16 data;
   12211 
   12212       swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
   12213       return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE, NT_PRPSINFO,
   12214 				 &data, sizeof (data));
   12215     }
   12216   else
   12217     {
   12218       struct elf_external_linux_prpsinfo32_ugid32 data;
   12219 
   12220       swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
   12221       return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE, NT_PRPSINFO,
   12222 				 &data, sizeof (data));
   12223     }
   12224 }
   12225 
   12226 char *
   12227 elfcore_write_linux_prpsinfo64
   12228   (bfd *abfd, char *buf, int *bufsiz,
   12229    const struct elf_internal_linux_prpsinfo *prpsinfo)
   12230 {
   12231   if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
   12232     {
   12233       struct elf_external_linux_prpsinfo64_ugid16 data;
   12234 
   12235       swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
   12236       return elfcore_write_note (abfd, buf, bufsiz,
   12237 				 NOTE_NAME_CORE, NT_PRPSINFO, &data, sizeof (data));
   12238     }
   12239   else
   12240     {
   12241       struct elf_external_linux_prpsinfo64_ugid32 data;
   12242 
   12243       swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
   12244       return elfcore_write_note (abfd, buf, bufsiz,
   12245 				 NOTE_NAME_CORE, NT_PRPSINFO, &data, sizeof (data));
   12246     }
   12247 }
   12248 
   12249 char *
   12250 elfcore_write_prstatus (bfd *abfd,
   12251 			char *buf,
   12252 			int *bufsiz,
   12253 			long pid,
   12254 			int cursig,
   12255 			const void *gregs)
   12256 {
   12257   elf_backend_data *bed = get_elf_backend_data (abfd);
   12258 
   12259   if (bed->elf_backend_write_core_note != NULL)
   12260     {
   12261       char *ret;
   12262       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   12263 						 NT_PRSTATUS,
   12264 						 pid, cursig, gregs);
   12265       if (ret != NULL)
   12266 	return ret;
   12267     }
   12268 
   12269 #if defined (HAVE_PRSTATUS_T)
   12270 #if defined (HAVE_PRSTATUS32_T)
   12271   if (bed->s->elfclass == ELFCLASS32)
   12272     {
   12273       prstatus32_t prstat;
   12274 
   12275       memset (&prstat, 0, sizeof (prstat));
   12276       prstat.pr_pid = pid;
   12277       prstat.pr_cursig = cursig;
   12278       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   12279       return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
   12280 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   12281     }
   12282   else
   12283 #endif
   12284     {
   12285       prstatus_t prstat;
   12286 
   12287       memset (&prstat, 0, sizeof (prstat));
   12288       prstat.pr_pid = pid;
   12289       prstat.pr_cursig = cursig;
   12290       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   12291       return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
   12292 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   12293     }
   12294 #endif /* HAVE_PRSTATUS_T */
   12295 
   12296   free (buf);
   12297   return NULL;
   12298 }
   12299 
   12300 #if defined (HAVE_LWPSTATUS_T)
   12301 char *
   12302 elfcore_write_lwpstatus (bfd *abfd,
   12303 			 char *buf,
   12304 			 int *bufsiz,
   12305 			 long pid,
   12306 			 int cursig,
   12307 			 const void *gregs)
   12308 {
   12309   lwpstatus_t lwpstat;
   12310 
   12311   memset (&lwpstat, 0, sizeof (lwpstat));
   12312   lwpstat.pr_lwpid  = pid >> 16;
   12313   lwpstat.pr_cursig = cursig;
   12314 #if defined (HAVE_LWPSTATUS_T_PR_REG)
   12315   memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
   12316 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   12317 #if !defined(gregs)
   12318   memcpy (lwpstat.pr_context.uc_mcontext.gregs,
   12319 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
   12320 #else
   12321   memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
   12322 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
   12323 #endif
   12324 #endif
   12325   return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
   12326 			     NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
   12327 }
   12328 #endif /* HAVE_LWPSTATUS_T */
   12329 
   12330 #if defined (HAVE_PSTATUS_T)
   12331 char *
   12332 elfcore_write_pstatus (bfd *abfd,
   12333 		       char *buf,
   12334 		       int *bufsiz,
   12335 		       long pid,
   12336 		       int cursig ATTRIBUTE_UNUSED,
   12337 		       const void *gregs ATTRIBUTE_UNUSED)
   12338 {
   12339 #if defined (HAVE_PSTATUS32_T)
   12340   elf_backend_data *bed = get_elf_backend_data (abfd);
   12341 
   12342   if (bed->s->elfclass == ELFCLASS32)
   12343     {
   12344       pstatus32_t pstat;
   12345 
   12346       memset (&pstat, 0, sizeof (pstat));
   12347       pstat.pr_pid = pid & 0xffff;
   12348       buf = elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
   12349 				NT_PSTATUS, &pstat, sizeof (pstat));
   12350       return buf;
   12351     }
   12352   else
   12353 #endif
   12354     {
   12355       pstatus_t pstat;
   12356 
   12357       memset (&pstat, 0, sizeof (pstat));
   12358       pstat.pr_pid = pid & 0xffff;
   12359       buf = elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
   12360 				NT_PSTATUS, &pstat, sizeof (pstat));
   12361       return buf;
   12362     }
   12363 }
   12364 #endif /* HAVE_PSTATUS_T */
   12365 
   12366 char *
   12367 elfcore_write_prfpreg (bfd *abfd,
   12368 		       char *buf,
   12369 		       int *bufsiz,
   12370 		       const void *fpregs,
   12371 		       int size)
   12372 {
   12373   return elfcore_write_note (abfd, buf, bufsiz,
   12374 			     NOTE_NAME_CORE, NT_FPREGSET, fpregs, size);
   12375 }
   12376 
   12377 char *
   12378 elfcore_write_prxfpreg (bfd *abfd,
   12379 			char *buf,
   12380 			int *bufsiz,
   12381 			const void *xfpregs,
   12382 			int size)
   12383 {
   12384   return elfcore_write_note (abfd, buf, bufsiz,
   12385 			     NOTE_NAME_LINUX, NT_PRXFPREG, xfpregs, size);
   12386 }
   12387 
   12388 char *
   12389 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
   12390 			 const void *xfpregs, int size)
   12391 {
   12392   char *note_name;
   12393 
   12394   if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
   12395     note_name = NOTE_NAME_FREEBSD;
   12396   else
   12397     note_name = NOTE_NAME_LINUX;
   12398 
   12399   return elfcore_write_note (abfd, buf, bufsiz,
   12400 			     note_name, NT_X86_XSTATE, xfpregs, size);
   12401 }
   12402 
   12403 char *
   12404 elfcore_write_xsave_layout (bfd *abfd, char *buf, int *bufsiz,
   12405 			    const void *xsave_layout, int size)
   12406 {
   12407   return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX,
   12408 			     NT_X86_XSAVE_LAYOUT, xsave_layout, size);
   12409 }
   12410 
   12411 static char *
   12412 elfcore_write_sspreg (bfd *abfd, char *buf, int *bufsiz,
   12413 		      const void *ssp, int size)
   12414 {
   12415   return elfcore_write_note (abfd, buf, bufsiz,
   12416 			     NOTE_NAME_LINUX, NT_X86_SHSTK, ssp, size);
   12417 }
   12418 
   12419 char *
   12420 elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
   12421 			    const void *regs, int size)
   12422 {
   12423   return elfcore_write_note (abfd, buf, bufsiz,
   12424 			     NOTE_NAME_FREEBSD, NT_FREEBSD_X86_SEGBASES,
   12425 			     regs, size);
   12426 }
   12427 
   12428 char *
   12429 elfcore_write_i386_tls (bfd *abfd, char *buf, int *bufsiz,
   12430 			    const void *regs, int size)
   12431 {
   12432   return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX, NT_386_TLS,
   12433 			     regs, size);
   12434 }
   12435 
   12436 char *
   12437 elfcore_write_ppc_vmx (bfd *abfd,
   12438 		       char *buf,
   12439 		       int *bufsiz,
   12440 		       const void *ppc_vmx,
   12441 		       int size)
   12442 {
   12443   return elfcore_write_note (abfd, buf, bufsiz,
   12444 			     NOTE_NAME_LINUX, NT_PPC_VMX, ppc_vmx, size);
   12445 }
   12446 
   12447 char *
   12448 elfcore_write_ppc_vsx (bfd *abfd,
   12449 		       char *buf,
   12450 		       int *bufsiz,
   12451 		       const void *ppc_vsx,
   12452 		       int size)
   12453 {
   12454   return elfcore_write_note (abfd, buf, bufsiz,
   12455 			     NOTE_NAME_LINUX, NT_PPC_VSX, ppc_vsx, size);
   12456 }
   12457 
   12458 char *
   12459 elfcore_write_ppc_tar (bfd *abfd,
   12460 		       char *buf,
   12461 		       int *bufsiz,
   12462 		       const void *ppc_tar,
   12463 		       int size)
   12464 {
   12465   return elfcore_write_note (abfd, buf, bufsiz,
   12466 			     NOTE_NAME_LINUX, NT_PPC_TAR, ppc_tar, size);
   12467 }
   12468 
   12469 char *
   12470 elfcore_write_ppc_ppr (bfd *abfd,
   12471 		       char *buf,
   12472 		       int *bufsiz,
   12473 		       const void *ppc_ppr,
   12474 		       int size)
   12475 {
   12476   return elfcore_write_note (abfd, buf, bufsiz,
   12477 			     NOTE_NAME_LINUX, NT_PPC_PPR, ppc_ppr, size);
   12478 }
   12479 
   12480 char *
   12481 elfcore_write_ppc_dscr (bfd *abfd,
   12482 			char *buf,
   12483 			int *bufsiz,
   12484 			const void *ppc_dscr,
   12485 			int size)
   12486 {
   12487   return elfcore_write_note (abfd, buf, bufsiz,
   12488 			     NOTE_NAME_LINUX, NT_PPC_DSCR, ppc_dscr, size);
   12489 }
   12490 
   12491 char *
   12492 elfcore_write_ppc_ebb (bfd *abfd,
   12493 		       char *buf,
   12494 		       int *bufsiz,
   12495 		       const void *ppc_ebb,
   12496 		       int size)
   12497 {
   12498   return elfcore_write_note (abfd, buf, bufsiz,
   12499 			     NOTE_NAME_LINUX, NT_PPC_EBB, ppc_ebb, size);
   12500 }
   12501 
   12502 char *
   12503 elfcore_write_ppc_pmu (bfd *abfd,
   12504 		       char *buf,
   12505 		       int *bufsiz,
   12506 		       const void *ppc_pmu,
   12507 		       int size)
   12508 {
   12509   return elfcore_write_note (abfd, buf, bufsiz,
   12510 			     NOTE_NAME_LINUX, NT_PPC_PMU, ppc_pmu, size);
   12511 }
   12512 
   12513 char *
   12514 elfcore_write_ppc_tm_cgpr (bfd *abfd,
   12515 			   char *buf,
   12516 			   int *bufsiz,
   12517 			   const void *ppc_tm_cgpr,
   12518 			   int size)
   12519 {
   12520   return elfcore_write_note (abfd, buf, bufsiz,
   12521 			     NOTE_NAME_LINUX, NT_PPC_TM_CGPR,
   12522 			     ppc_tm_cgpr, size);
   12523 }
   12524 
   12525 char *
   12526 elfcore_write_ppc_tm_cfpr (bfd *abfd,
   12527 			   char *buf,
   12528 			   int *bufsiz,
   12529 			   const void *ppc_tm_cfpr,
   12530 			   int size)
   12531 {
   12532   return elfcore_write_note (abfd, buf, bufsiz,
   12533 			     NOTE_NAME_LINUX, NT_PPC_TM_CFPR,
   12534 			     ppc_tm_cfpr, size);
   12535 }
   12536 
   12537 char *
   12538 elfcore_write_ppc_tm_cvmx (bfd *abfd,
   12539 			   char *buf,
   12540 			   int *bufsiz,
   12541 			   const void *ppc_tm_cvmx,
   12542 			   int size)
   12543 {
   12544   return elfcore_write_note (abfd, buf, bufsiz,
   12545 			     NOTE_NAME_LINUX, NT_PPC_TM_CVMX,
   12546 			     ppc_tm_cvmx, size);
   12547 }
   12548 
   12549 char *
   12550 elfcore_write_ppc_tm_cvsx (bfd *abfd,
   12551 			   char *buf,
   12552 			   int *bufsiz,
   12553 			   const void *ppc_tm_cvsx,
   12554 			   int size)
   12555 {
   12556   return elfcore_write_note (abfd, buf, bufsiz,
   12557 			     NOTE_NAME_LINUX, NT_PPC_TM_CVSX,
   12558 			     ppc_tm_cvsx, size);
   12559 }
   12560 
   12561 char *
   12562 elfcore_write_ppc_tm_spr (bfd *abfd,
   12563 			  char *buf,
   12564 			  int *bufsiz,
   12565 			  const void *ppc_tm_spr,
   12566 			  int size)
   12567 {
   12568   return elfcore_write_note (abfd, buf, bufsiz,
   12569 			     NOTE_NAME_LINUX, NT_PPC_TM_SPR,
   12570 			     ppc_tm_spr, size);
   12571 }
   12572 
   12573 char *
   12574 elfcore_write_ppc_tm_ctar (bfd *abfd,
   12575 			   char *buf,
   12576 			   int *bufsiz,
   12577 			   const void *ppc_tm_ctar,
   12578 			   int size)
   12579 {
   12580   return elfcore_write_note (abfd, buf, bufsiz,
   12581 			     NOTE_NAME_LINUX, NT_PPC_TM_CTAR,
   12582 			     ppc_tm_ctar, size);
   12583 }
   12584 
   12585 char *
   12586 elfcore_write_ppc_tm_cppr (bfd *abfd,
   12587 			   char *buf,
   12588 			   int *bufsiz,
   12589 			   const void *ppc_tm_cppr,
   12590 			   int size)
   12591 {
   12592   return elfcore_write_note (abfd, buf, bufsiz,
   12593 			     NOTE_NAME_LINUX, NT_PPC_TM_CPPR,
   12594 			     ppc_tm_cppr, size);
   12595 }
   12596 
   12597 char *
   12598 elfcore_write_ppc_tm_cdscr (bfd *abfd,
   12599 			    char *buf,
   12600 			    int *bufsiz,
   12601 			    const void *ppc_tm_cdscr,
   12602 			    int size)
   12603 {
   12604   return elfcore_write_note (abfd, buf, bufsiz,
   12605 			     NOTE_NAME_LINUX, NT_PPC_TM_CDSCR,
   12606 			     ppc_tm_cdscr, size);
   12607 }
   12608 
   12609 static char *
   12610 elfcore_write_s390_high_gprs (bfd *abfd,
   12611 			      char *buf,
   12612 			      int *bufsiz,
   12613 			      const void *s390_high_gprs,
   12614 			      int size)
   12615 {
   12616   return elfcore_write_note (abfd, buf, bufsiz,
   12617 			     NOTE_NAME_LINUX, NT_S390_HIGH_GPRS,
   12618 			     s390_high_gprs, size);
   12619 }
   12620 
   12621 char *
   12622 elfcore_write_s390_timer (bfd *abfd,
   12623 			  char *buf,
   12624 			  int *bufsiz,
   12625 			  const void *s390_timer,
   12626 			  int size)
   12627 {
   12628   return elfcore_write_note (abfd, buf, bufsiz,
   12629 			     NOTE_NAME_LINUX, NT_S390_TIMER,
   12630 			     s390_timer, size);
   12631 }
   12632 
   12633 char *
   12634 elfcore_write_s390_todcmp (bfd *abfd,
   12635 			   char *buf,
   12636 			   int *bufsiz,
   12637 			   const void *s390_todcmp,
   12638 			   int size)
   12639 {
   12640   return elfcore_write_note (abfd, buf, bufsiz,
   12641 			     NOTE_NAME_LINUX, NT_S390_TODCMP,
   12642 			     s390_todcmp, size);
   12643 }
   12644 
   12645 char *
   12646 elfcore_write_s390_todpreg (bfd *abfd,
   12647 			    char *buf,
   12648 			    int *bufsiz,
   12649 			    const void *s390_todpreg,
   12650 			    int size)
   12651 {
   12652   return elfcore_write_note (abfd, buf, bufsiz,
   12653 			     NOTE_NAME_LINUX, NT_S390_TODPREG,
   12654 			     s390_todpreg, size);
   12655 }
   12656 
   12657 char *
   12658 elfcore_write_s390_ctrs (bfd *abfd,
   12659 			 char *buf,
   12660 			 int *bufsiz,
   12661 			 const void *s390_ctrs,
   12662 			 int size)
   12663 {
   12664   return elfcore_write_note (abfd, buf, bufsiz,
   12665 			     NOTE_NAME_LINUX, NT_S390_CTRS, s390_ctrs, size);
   12666 }
   12667 
   12668 char *
   12669 elfcore_write_s390_prefix (bfd *abfd,
   12670 			   char *buf,
   12671 			   int *bufsiz,
   12672 			   const void *s390_prefix,
   12673 			   int size)
   12674 {
   12675   return elfcore_write_note (abfd, buf, bufsiz,
   12676 			     NOTE_NAME_LINUX, NT_S390_PREFIX,
   12677 			     s390_prefix, size);
   12678 }
   12679 
   12680 char *
   12681 elfcore_write_s390_last_break (bfd *abfd,
   12682 			       char *buf,
   12683 			       int *bufsiz,
   12684 			       const void *s390_last_break,
   12685 			       int size)
   12686 {
   12687   return elfcore_write_note (abfd, buf, bufsiz,
   12688 			     NOTE_NAME_LINUX, NT_S390_LAST_BREAK,
   12689 			     s390_last_break, size);
   12690 }
   12691 
   12692 char *
   12693 elfcore_write_s390_system_call (bfd *abfd,
   12694 				char *buf,
   12695 				int *bufsiz,
   12696 				const void *s390_system_call,
   12697 				int size)
   12698 {
   12699   return elfcore_write_note (abfd, buf, bufsiz,
   12700 			     NOTE_NAME_LINUX, NT_S390_SYSTEM_CALL,
   12701 			     s390_system_call, size);
   12702 }
   12703 
   12704 char *
   12705 elfcore_write_s390_tdb (bfd *abfd,
   12706 			char *buf,
   12707 			int *bufsiz,
   12708 			const void *s390_tdb,
   12709 			int size)
   12710 {
   12711   return elfcore_write_note (abfd, buf, bufsiz,
   12712 			     NOTE_NAME_LINUX, NT_S390_TDB, s390_tdb, size);
   12713 }
   12714 
   12715 char *
   12716 elfcore_write_s390_vxrs_low (bfd *abfd,
   12717 			     char *buf,
   12718 			     int *bufsiz,
   12719 			     const void *s390_vxrs_low,
   12720 			     int size)
   12721 {
   12722   return elfcore_write_note (abfd, buf, bufsiz,
   12723 			     NOTE_NAME_LINUX, NT_S390_VXRS_LOW,
   12724 			     s390_vxrs_low, size);
   12725 }
   12726 
   12727 char *
   12728 elfcore_write_s390_vxrs_high (bfd *abfd,
   12729 			     char *buf,
   12730 			     int *bufsiz,
   12731 			     const void *s390_vxrs_high,
   12732 			     int size)
   12733 {
   12734   return elfcore_write_note (abfd, buf, bufsiz,
   12735 			     NOTE_NAME_LINUX, NT_S390_VXRS_HIGH,
   12736 			     s390_vxrs_high, size);
   12737 }
   12738 
   12739 char *
   12740 elfcore_write_s390_gs_cb (bfd *abfd,
   12741 			  char *buf,
   12742 			  int *bufsiz,
   12743 			  const void *s390_gs_cb,
   12744 			  int size)
   12745 {
   12746   return elfcore_write_note (abfd, buf, bufsiz,
   12747 			     NOTE_NAME_LINUX, NT_S390_GS_CB,
   12748 			     s390_gs_cb, size);
   12749 }
   12750 
   12751 char *
   12752 elfcore_write_s390_gs_bc (bfd *abfd,
   12753 			  char *buf,
   12754 			  int *bufsiz,
   12755 			  const void *s390_gs_bc,
   12756 			  int size)
   12757 {
   12758   return elfcore_write_note (abfd, buf, bufsiz,
   12759 			     NOTE_NAME_LINUX, NT_S390_GS_BC,
   12760 			     s390_gs_bc, size);
   12761 }
   12762 
   12763 char *
   12764 elfcore_write_arm_vfp (bfd *abfd,
   12765 		       char *buf,
   12766 		       int *bufsiz,
   12767 		       const void *arm_vfp,
   12768 		       int size)
   12769 {
   12770   return elfcore_write_note (abfd, buf, bufsiz,
   12771 			     NOTE_NAME_LINUX, NT_ARM_VFP,
   12772 			     arm_vfp, size);
   12773 }
   12774 
   12775 char *
   12776 elfcore_write_aarch_tls (bfd *abfd,
   12777 		       char *buf,
   12778 		       int *bufsiz,
   12779 		       const void *aarch_tls,
   12780 		       int size)
   12781 {
   12782   return elfcore_write_note (abfd, buf, bufsiz,
   12783 			     NOTE_NAME_LINUX, NT_ARM_TLS, aarch_tls, size);
   12784 }
   12785 
   12786 char *
   12787 elfcore_write_aarch_hw_break (bfd *abfd,
   12788 			    char *buf,
   12789 			    int *bufsiz,
   12790 			    const void *aarch_hw_break,
   12791 			    int size)
   12792 {
   12793   return elfcore_write_note (abfd, buf, bufsiz,
   12794 			     NOTE_NAME_LINUX, NT_ARM_HW_BREAK,
   12795 			     aarch_hw_break, size);
   12796 }
   12797 
   12798 char *
   12799 elfcore_write_aarch_hw_watch (bfd *abfd,
   12800 			    char *buf,
   12801 			    int *bufsiz,
   12802 			    const void *aarch_hw_watch,
   12803 			    int size)
   12804 {
   12805   return elfcore_write_note (abfd, buf, bufsiz,
   12806 			     NOTE_NAME_LINUX, NT_ARM_HW_WATCH,
   12807 			     aarch_hw_watch, size);
   12808 }
   12809 
   12810 char *
   12811 elfcore_write_aarch_sve (bfd *abfd,
   12812 			 char *buf,
   12813 			 int *bufsiz,
   12814 			 const void *aarch_sve,
   12815 			 int size)
   12816 {
   12817   return elfcore_write_note (abfd, buf, bufsiz,
   12818 			     NOTE_NAME_LINUX, NT_ARM_SVE, aarch_sve, size);
   12819 }
   12820 
   12821 char *
   12822 elfcore_write_aarch_pauth (bfd *abfd,
   12823 			   char *buf,
   12824 			   int *bufsiz,
   12825 			   const void *aarch_pauth,
   12826 			   int size)
   12827 {
   12828   return elfcore_write_note (abfd, buf, bufsiz,
   12829 			     NOTE_NAME_LINUX, NT_ARM_PAC_MASK,
   12830 			     aarch_pauth, size);
   12831 }
   12832 
   12833 char *
   12834 elfcore_write_aarch_mte (bfd *abfd,
   12835 			 char *buf,
   12836 			 int *bufsiz,
   12837 			 const void *aarch_mte,
   12838 			 int size)
   12839 {
   12840   return elfcore_write_note (abfd, buf, bufsiz,
   12841 			     NOTE_NAME_LINUX, NT_ARM_TAGGED_ADDR_CTRL,
   12842 			     aarch_mte, size);
   12843 }
   12844 
   12845 char *
   12846 elfcore_write_aarch_ssve (bfd *abfd,
   12847 			  char *buf,
   12848 			  int *bufsiz,
   12849 			  const void *aarch_ssve,
   12850 			  int size)
   12851 {
   12852   return elfcore_write_note (abfd, buf, bufsiz,
   12853 			     NOTE_NAME_LINUX, NT_ARM_SSVE,
   12854 			     aarch_ssve, size);
   12855 }
   12856 
   12857 char *
   12858 elfcore_write_aarch_za (bfd *abfd,
   12859 			char *buf,
   12860 			int *bufsiz,
   12861 			const void *aarch_za,
   12862 			int size)
   12863 {
   12864   return elfcore_write_note (abfd, buf, bufsiz,
   12865 			     NOTE_NAME_LINUX, NT_ARM_ZA,
   12866 			     aarch_za, size);
   12867 }
   12868 
   12869 /* Write the buffer of zt register values in aarch_zt (length SIZE) into
   12870    the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
   12871    written into.  Return a pointer to the new start of the note buffer, to
   12872    replace BUF which may no longer be valid.  */
   12873 
   12874 char *
   12875 elfcore_write_aarch_zt (bfd *abfd,
   12876 			char *buf,
   12877 			int *bufsiz,
   12878 			const void *aarch_zt,
   12879 			int size)
   12880 {
   12881   return elfcore_write_note (abfd, buf, bufsiz,
   12882 			     NOTE_NAME_LINUX, NT_ARM_ZT,
   12883 			     aarch_zt, size);
   12884 }
   12885 
   12886 /* Write the buffer of GCS register values in AARCH_GCS (length SIZE) into
   12887    the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
   12888    written into.  Return a pointer to the new start of the note buffer, to
   12889    replace BUF which may no longer be valid.  */
   12890 
   12891 static char *
   12892 elfcore_write_aarch_gcs (bfd *abfd, char *buf, int *bufsiz,
   12893 			 const void *aarch_gcs, int size)
   12894 {
   12895   return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX, NT_ARM_GCS,
   12896 			     aarch_gcs, size);
   12897 }
   12898 
   12899 /* Write the buffer of FPMR value in AARCH_FPMR (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 char *
   12905 elfcore_write_aarch_fpmr (bfd *abfd,
   12906 			char *buf,
   12907 			int *bufsiz,
   12908 			const void *aarch_fpmr,
   12909 			int size)
   12910 {
   12911   return elfcore_write_note (abfd, buf, bufsiz,
   12912 			     NOTE_NAME_LINUX, NT_ARM_FPMR, aarch_fpmr, size);
   12913 }
   12914 
   12915 char *
   12916 elfcore_write_arc_v2 (bfd *abfd,
   12917 		      char *buf,
   12918 		      int *bufsiz,
   12919 		      const void *arc_v2,
   12920 		      int size)
   12921 {
   12922   return elfcore_write_note (abfd, buf, bufsiz,
   12923 			     NOTE_NAME_LINUX, NT_ARC_V2, arc_v2, size);
   12924 }
   12925 
   12926 char *
   12927 elfcore_write_loongarch_cpucfg (bfd *abfd,
   12928 				char *buf,
   12929 				int *bufsiz,
   12930 				const void *loongarch_cpucfg,
   12931 				int size)
   12932 {
   12933   return elfcore_write_note (abfd, buf, bufsiz,
   12934 			     NOTE_NAME_LINUX, NT_LARCH_CPUCFG,
   12935 			     loongarch_cpucfg, size);
   12936 }
   12937 
   12938 char *
   12939 elfcore_write_loongarch_lbt (bfd *abfd,
   12940 			     char *buf,
   12941 			     int *bufsiz,
   12942 			     const void *loongarch_lbt,
   12943 			     int size)
   12944 {
   12945   return elfcore_write_note (abfd, buf, bufsiz,
   12946 			     NOTE_NAME_LINUX, NT_LARCH_LBT,
   12947 			     loongarch_lbt, size);
   12948 }
   12949 
   12950 char *
   12951 elfcore_write_loongarch_lsx (bfd *abfd,
   12952 			     char *buf,
   12953 			     int *bufsiz,
   12954 			     const void *loongarch_lsx,
   12955 			     int size)
   12956 {
   12957   return elfcore_write_note (abfd, buf, bufsiz,
   12958 			     NOTE_NAME_LINUX, NT_LARCH_LSX,
   12959 			     loongarch_lsx, size);
   12960 }
   12961 
   12962 char *
   12963 elfcore_write_loongarch_lasx (bfd *abfd,
   12964 			      char *buf,
   12965 			      int *bufsiz,
   12966 			      const void *loongarch_lasx,
   12967 			      int size)
   12968 {
   12969   return elfcore_write_note (abfd, buf, bufsiz,
   12970 			     NOTE_NAME_LINUX, NT_LARCH_LASX,
   12971 			     loongarch_lasx, size);
   12972 }
   12973 
   12974 /* Write the buffer of csr values in CSRS (length SIZE) into the note
   12975    buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
   12976    written into.  Return a pointer to the new start of the note buffer, to
   12977    replace BUF which may no longer be valid.  */
   12978 
   12979 char *
   12980 elfcore_write_riscv_csr (bfd *abfd,
   12981 			 char *buf,
   12982 			 int *bufsiz,
   12983 			 const void *csrs,
   12984 			 int size)
   12985 {
   12986   return elfcore_write_note (abfd, buf, bufsiz,
   12987 			     NOTE_NAME_GDB, NT_RISCV_CSR, csrs, size);
   12988 }
   12989 
   12990 /* Write the target description (a string) pointed to by TDESC, length
   12991    SIZE, into the note buffer BUF, and update *BUFSIZ.  ABFD is the bfd the
   12992    note is being written into.  Return a pointer to the new start of the
   12993    note buffer, to replace BUF which may no longer be valid.  */
   12994 
   12995 char *
   12996 elfcore_write_gdb_tdesc (bfd *abfd,
   12997 			 char *buf,
   12998 			 int *bufsiz,
   12999 			 const void *tdesc,
   13000 			 int size)
   13001 {
   13002   return elfcore_write_note (abfd, buf, bufsiz,
   13003 			     NOTE_NAME_GDB, NT_GDB_TDESC, tdesc, size);
   13004 }
   13005 
   13006 char *
   13007 elfcore_write_register_note (bfd *abfd,
   13008 			     char *buf,
   13009 			     int *bufsiz,
   13010 			     const char *section,
   13011 			     const void *data,
   13012 			     int size)
   13013 {
   13014   static const struct
   13015     {
   13016       const char * section_name;
   13017       char *       (*writer) (bfd *, char *, int *, const void *, int);
   13018     }
   13019   note_writers [] =
   13020     {
   13021       { NOTE_PSEUDO_SECTION_AARCH_FPMR,       elfcore_write_aarch_fpmr},
   13022       { NOTE_PSEUDO_SECTION_AARCH_GCS,        elfcore_write_aarch_gcs},
   13023       { NOTE_PSEUDO_SECTION_AARCH_HW_BREAK,   elfcore_write_aarch_hw_break},
   13024       { NOTE_PSEUDO_SECTION_AARCH_HW_WATCH,   elfcore_write_aarch_hw_watch},
   13025       { NOTE_PSEUDO_SECTION_AARCH_MTE,        elfcore_write_aarch_mte},
   13026       { NOTE_PSEUDO_SECTION_AARCH_PAUTH,      elfcore_write_aarch_pauth},
   13027       { NOTE_PSEUDO_SECTION_AARCH_SSVE,       elfcore_write_aarch_ssve},
   13028       { NOTE_PSEUDO_SECTION_AARCH_SVE,        elfcore_write_aarch_sve},
   13029       { NOTE_PSEUDO_SECTION_AARCH_TLS,        elfcore_write_aarch_tls},
   13030       { NOTE_PSEUDO_SECTION_AARCH_ZA,         elfcore_write_aarch_za},
   13031       { NOTE_PSEUDO_SECTION_AARCH_ZT,         elfcore_write_aarch_zt},
   13032       { NOTE_PSEUDO_SECTION_ARC_V2,           elfcore_write_arc_v2},
   13033       { NOTE_PSEUDO_SECTION_ARM_VFP,          elfcore_write_arm_vfp},
   13034       { NOTE_PSEUDO_SECTION_I386_TLS,         elfcore_write_i386_tls},
   13035       { NOTE_PSEUDO_SECTION_LOONGARCH_CPUCFG, elfcore_write_loongarch_cpucfg},
   13036       { NOTE_PSEUDO_SECTION_LOONGARCH_LASX,   elfcore_write_loongarch_lasx},
   13037       { NOTE_PSEUDO_SECTION_LOONGARCH_LBT,    elfcore_write_loongarch_lbt},
   13038       { NOTE_PSEUDO_SECTION_LOONGARCH_LSX,    elfcore_write_loongarch_lsx},
   13039       { NOTE_PSEUDO_SECTION_PPC_DSCR,         elfcore_write_ppc_dscr},
   13040       { NOTE_PSEUDO_SECTION_PPC_EBB,          elfcore_write_ppc_ebb},
   13041       { NOTE_PSEUDO_SECTION_PPC_PMU,          elfcore_write_ppc_pmu},
   13042       { NOTE_PSEUDO_SECTION_PPC_PPR,          elfcore_write_ppc_ppr},
   13043       { NOTE_PSEUDO_SECTION_PPC_TAR,          elfcore_write_ppc_tar},
   13044       { NOTE_PSEUDO_SECTION_PPC_TM_CDSCR,     elfcore_write_ppc_tm_cdscr},
   13045       { NOTE_PSEUDO_SECTION_PPC_TM_CFPR,      elfcore_write_ppc_tm_cfpr},
   13046       { NOTE_PSEUDO_SECTION_PPC_TM_CGPR,      elfcore_write_ppc_tm_cgpr},
   13047       { NOTE_PSEUDO_SECTION_PPC_TM_CPPR,      elfcore_write_ppc_tm_cppr},
   13048       { NOTE_PSEUDO_SECTION_PPC_TM_CTAR,      elfcore_write_ppc_tm_ctar},
   13049       { NOTE_PSEUDO_SECTION_PPC_TM_CVMX,      elfcore_write_ppc_tm_cvmx},
   13050       { NOTE_PSEUDO_SECTION_PPC_TM_CVSX,      elfcore_write_ppc_tm_cvsx},
   13051       { NOTE_PSEUDO_SECTION_PPC_TM_SPR,       elfcore_write_ppc_tm_spr},
   13052       { NOTE_PSEUDO_SECTION_PPC_VMX,          elfcore_write_ppc_vmx},
   13053       { NOTE_PSEUDO_SECTION_PPC_VSX,          elfcore_write_ppc_vsx},
   13054       { NOTE_PSEUDO_SECTION_REG2,             elfcore_write_prfpreg},
   13055       { NOTE_PSEUDO_SECTION_RISCV_CSR,        elfcore_write_riscv_csr},
   13056       { NOTE_PSEUDO_SECTION_S390_CTRS,        elfcore_write_s390_ctrs},
   13057       { NOTE_PSEUDO_SECTION_S390_GS_BC,       elfcore_write_s390_gs_bc},
   13058       { NOTE_PSEUDO_SECTION_S390_GS_CB,       elfcore_write_s390_gs_cb},
   13059       { NOTE_PSEUDO_SECTION_S390_HIGH_GPRS,   elfcore_write_s390_high_gprs},
   13060       { NOTE_PSEUDO_SECTION_S390_LAST_BREAK,  elfcore_write_s390_last_break},
   13061       { NOTE_PSEUDO_SECTION_S390_PREFIX,      elfcore_write_s390_prefix},
   13062       { NOTE_PSEUDO_SECTION_S390_SYSTEM_CALL, elfcore_write_s390_system_call},
   13063       { NOTE_PSEUDO_SECTION_S390_TDB,         elfcore_write_s390_tdb},
   13064       { NOTE_PSEUDO_SECTION_S390_TIMER,       elfcore_write_s390_timer},
   13065       { NOTE_PSEUDO_SECTION_S390_TODCMP,      elfcore_write_s390_todcmp},
   13066       { NOTE_PSEUDO_SECTION_S390_TODPREG,     elfcore_write_s390_todpreg},
   13067       { NOTE_PSEUDO_SECTION_S390_VXRS_HIGH,   elfcore_write_s390_vxrs_high},
   13068       { NOTE_PSEUDO_SECTION_S390_VXRS_LOW,    elfcore_write_s390_vxrs_low},
   13069       { NOTE_PSEUDO_SECTION_SSP,              elfcore_write_sspreg},
   13070       { NOTE_PSEUDO_SECTION_TDESC,            elfcore_write_gdb_tdesc},
   13071       { NOTE_PSEUDO_SECTION_X86_SEGBASES,     elfcore_write_x86_segbases},
   13072       { NOTE_PSEUDO_SECTION_XFP,              elfcore_write_prxfpreg},
   13073       { NOTE_PSEUDO_SECTION_XSTATE,           elfcore_write_xstatereg},
   13074       { NOTE_PSEUDO_SECTION_XSAVE_LAYOUT,     elfcore_write_xsave_layout} /* NB/ No comma.  */
   13075     };
   13076 
   13077   int i;
   13078 
   13079   for (i = ARRAY_SIZE (note_writers); i--;)
   13080     if (streq (section, note_writers[i].section_name))
   13081       return  note_writers[i].writer (abfd, buf, bufsiz, data, size);
   13082 
   13083   return NULL;
   13084 }
   13085 
   13086 char *
   13087 elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
   13088 			 const void *buf, int bufsiz)
   13089 {
   13090   return elfcore_write_note (obfd, note_data, note_size,
   13091 			     NOTE_NAME_CORE, NT_FILE, buf, bufsiz);
   13092 }
   13093 
   13094 static bool
   13095 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
   13096 		 size_t align)
   13097 {
   13098   char *p;
   13099 
   13100   /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
   13101      gABI specifies that PT_NOTE alignment should be aligned to 4
   13102      bytes for 32-bit objects and to 8 bytes for 64-bit objects.  If
   13103      align is less than 4, we use 4 byte alignment.   */
   13104   if (align < 4)
   13105     align = 4;
   13106   if (align != 4 && align != 8)
   13107     return false;
   13108 
   13109   p = buf;
   13110   while (p < buf + size)
   13111     {
   13112       Elf_External_Note *xnp = (Elf_External_Note *) p;
   13113       Elf_Internal_Note in;
   13114 
   13115       if (offsetof (Elf_External_Note, name) > buf - p + size)
   13116 	return false;
   13117 
   13118       in.type = H_GET_32 (abfd, xnp->type);
   13119 
   13120       in.namesz = H_GET_32 (abfd, xnp->namesz);
   13121       in.namedata = xnp->name;
   13122       if (in.namesz > buf - in.namedata + size)
   13123 	return false;
   13124 
   13125       in.descsz = H_GET_32 (abfd, xnp->descsz);
   13126       in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
   13127       in.descpos = offset + (in.descdata - buf);
   13128       if (in.descsz != 0
   13129 	  && (in.descdata >= buf + size
   13130 	      || in.descsz > buf - in.descdata + size))
   13131 	return false;
   13132 
   13133       switch (bfd_get_format (abfd))
   13134 	{
   13135 	default:
   13136 	  return true;
   13137 
   13138 	case bfd_core:
   13139 	  {
   13140 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
   13141 	    static const struct
   13142 	    {
   13143 	      const char * string;
   13144 	      size_t len;
   13145 	      bool (*func) (bfd *, Elf_Internal_Note *);
   13146 	    }
   13147 	    grokers[] =
   13148 	    {
   13149 	      GROKER_ELEMENT ("", elfcore_grok_note),
   13150 	      GROKER_ELEMENT (NOTE_NAME_FREEBSD, elfcore_grok_freebsd_note),
   13151 	      GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
   13152 	      GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
   13153 	      GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
   13154 	      GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
   13155 	      GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
   13156 	      GROKER_ELEMENT (NOTE_NAME_CORE, elfcore_grok_solaris_note)
   13157 	    };
   13158 #undef GROKER_ELEMENT
   13159 	    int i;
   13160 
   13161 	    for (i = ARRAY_SIZE (grokers); i--;)
   13162 	      {
   13163 		if (in.namesz >= grokers[i].len
   13164 		    && strncmp (in.namedata, grokers[i].string,
   13165 				grokers[i].len) == 0)
   13166 		  {
   13167 		    if (! grokers[i].func (abfd, & in))
   13168 		      return false;
   13169 		    break;
   13170 		  }
   13171 	      }
   13172 	    break;
   13173 	  }
   13174 
   13175 	case bfd_object:
   13176 	  if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
   13177 	    {
   13178 	      if (! elfobj_grok_gnu_note (abfd, &in))
   13179 		return false;
   13180 	    }
   13181 	  else if (in.namesz == sizeof "stapsdt"
   13182 		   && strcmp (in.namedata, "stapsdt") == 0)
   13183 	    {
   13184 	      if (! elfobj_grok_stapsdt_note (abfd, &in))
   13185 		return false;
   13186 	    }
   13187 	  break;
   13188 	}
   13189 
   13190       p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
   13191     }
   13192 
   13193   return true;
   13194 }
   13195 
   13196 bool
   13197 _bfd_elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
   13198 		     size_t align)
   13199 {
   13200   char *buf;
   13201 
   13202   if (size == 0 || (size + 1) == 0)
   13203     return true;
   13204 
   13205   if (bfd_seek (abfd, offset, SEEK_SET) != 0)
   13206     return false;
   13207 
   13208   buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
   13209   if (buf == NULL)
   13210     return false;
   13211 
   13212   /* PR 17512: file: ec08f814
   13213      0-termintate the buffer so that string searches will not overflow.  */
   13214   buf[size] = 0;
   13215 
   13216   if (!elf_parse_notes (abfd, buf, size, offset, align))
   13217     {
   13218       free (buf);
   13219       return false;
   13220     }
   13221 
   13222   free (buf);
   13223   return true;
   13224 }
   13225 
   13226 /* Providing external access to the ELF program header table.  */
   13228 
   13229 /* Return an upper bound on the number of bytes required to store a
   13230    copy of ABFD's program header table entries.  Return -1 if an error
   13231    occurs; bfd_get_error will return an appropriate code.  */
   13232 
   13233 long
   13234 bfd_get_elf_phdr_upper_bound (bfd *abfd)
   13235 {
   13236   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   13237     {
   13238       bfd_set_error (bfd_error_wrong_format);
   13239       return -1;
   13240     }
   13241 
   13242   return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
   13243 }
   13244 
   13245 /* Copy ABFD's program header table entries to *PHDRS.  The entries
   13246    will be stored as an array of Elf_Internal_Phdr structures, as
   13247    defined in include/elf/internal.h.  To find out how large the
   13248    buffer needs to be, call bfd_get_elf_phdr_upper_bound.
   13249 
   13250    Return the number of program header table entries read, or -1 if an
   13251    error occurs; bfd_get_error will return an appropriate code.  */
   13252 
   13253 int
   13254 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
   13255 {
   13256   int num_phdrs;
   13257 
   13258   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   13259     {
   13260       bfd_set_error (bfd_error_wrong_format);
   13261       return -1;
   13262     }
   13263 
   13264   num_phdrs = elf_elfheader (abfd)->e_phnum;
   13265   if (num_phdrs != 0)
   13266     memcpy (phdrs, elf_tdata (abfd)->phdr,
   13267 	    num_phdrs * sizeof (Elf_Internal_Phdr));
   13268 
   13269   return num_phdrs;
   13270 }
   13271 
   13272 enum elf_reloc_type_class
   13273 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   13274 			   const asection *rel_sec ATTRIBUTE_UNUSED,
   13275 			   const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
   13276 {
   13277   return reloc_class_normal;
   13278 }
   13279 
   13280 /* For RELA architectures, return the relocation value for a
   13281    relocation against a local symbol.  */
   13282 
   13283 bfd_vma
   13284 _bfd_elf_rela_local_sym (bfd *abfd,
   13285 			 Elf_Internal_Sym *sym,
   13286 			 asection **psec,
   13287 			 Elf_Internal_Rela *rel)
   13288 {
   13289   asection *sec = *psec;
   13290   bfd_vma relocation;
   13291 
   13292   relocation = (sec->output_section->vma
   13293 		+ sec->output_offset
   13294 		+ sym->st_value);
   13295   if ((sec->flags & SEC_MERGE)
   13296       && ELF_ST_TYPE (sym->st_info) == STT_SECTION
   13297       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
   13298     {
   13299       rel->r_addend =
   13300 	_bfd_merged_section_offset (abfd, psec,
   13301 				    sym->st_value + rel->r_addend);
   13302       if (sec != *psec)
   13303 	{
   13304 	  /* If we have changed the section, and our original section is
   13305 	     marked with SEC_EXCLUDE, it means that the original
   13306 	     SEC_MERGE section has been completely subsumed in some
   13307 	     other SEC_MERGE section.  In this case, we need to leave
   13308 	     some info around for --emit-relocs.  */
   13309 	  if ((sec->flags & SEC_EXCLUDE) != 0)
   13310 	    sec->kept_section = *psec;
   13311 	  sec = *psec;
   13312 	}
   13313       rel->r_addend -= relocation;
   13314       rel->r_addend += sec->output_section->vma + sec->output_offset;
   13315     }
   13316   return relocation;
   13317 }
   13318 
   13319 bfd_vma
   13320 _bfd_elf_rel_local_sym (bfd *abfd,
   13321 			Elf_Internal_Sym *sym,
   13322 			asection **psec,
   13323 			bfd_vma addend)
   13324 {
   13325   asection *sec = *psec;
   13326 
   13327   if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
   13328     return sym->st_value + addend;
   13329 
   13330   return _bfd_merged_section_offset (abfd, psec,
   13331 				     sym->st_value + addend);
   13332 }
   13333 
   13334 /* Adjust an address within a section.  Given OFFSET within SEC, return
   13335    the new offset within the section, based upon changes made to the
   13336    section.  Returns -1 if the offset is now invalid.
   13337    The offset (in abnd out) is in target sized bytes, however big a
   13338    byte may be.  */
   13339 
   13340 bfd_vma
   13341 _bfd_elf_section_offset (bfd *abfd,
   13342 			 struct bfd_link_info *info,
   13343 			 asection *sec,
   13344 			 bfd_vma offset)
   13345 {
   13346   switch (sec->sec_info_type)
   13347     {
   13348     case SEC_INFO_TYPE_STABS:
   13349       return _bfd_stab_section_offset (sec, offset);
   13350 
   13351     case SEC_INFO_TYPE_EH_FRAME:
   13352       return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
   13353 
   13354     case SEC_INFO_TYPE_SFRAME:
   13355       return _bfd_elf_sframe_section_offset (abfd, info, sec, offset);
   13356 
   13357     default:
   13358       if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
   13359 	{
   13360 	  /* Reverse the offset.  */
   13361 	  elf_backend_data *bed = get_elf_backend_data (abfd);
   13362 	  bfd_size_type address_size = bed->s->arch_size / 8;
   13363 
   13364 	  /* address_size and sec->size are in octets.  Convert
   13365 	     to bytes before subtracting the original offset.  */
   13366 	  offset = ((sec->size - address_size)
   13367 		    / bfd_octets_per_byte (abfd, sec) - offset);
   13368 	}
   13369       return offset;
   13370     }
   13371 }
   13372 
   13373 long
   13375 _bfd_elf_get_synthetic_symtab (bfd *abfd,
   13376 			       long symcount ATTRIBUTE_UNUSED,
   13377 			       asymbol **syms ATTRIBUTE_UNUSED,
   13378 			       long dynsymcount,
   13379 			       asymbol **dynsyms,
   13380 			       asymbol **ret)
   13381 {
   13382   elf_backend_data *bed = get_elf_backend_data (abfd);
   13383   asection *relplt;
   13384   asymbol *s;
   13385   const char *relplt_name;
   13386   bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
   13387   arelent *p;
   13388   long count, i, n;
   13389   size_t size;
   13390   Elf_Internal_Shdr *hdr;
   13391   char *names;
   13392   asection *plt;
   13393 
   13394   *ret = NULL;
   13395 
   13396   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
   13397     return 0;
   13398 
   13399   if (dynsymcount <= 0)
   13400     return 0;
   13401 
   13402   if (!bed->plt_sym_val)
   13403     return 0;
   13404 
   13405   relplt_name = bed->relplt_name;
   13406   if (relplt_name == NULL)
   13407     relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
   13408   relplt = bfd_get_section_by_name (abfd, relplt_name);
   13409   if (relplt == NULL)
   13410     return 0;
   13411 
   13412   hdr = &elf_section_data (relplt)->this_hdr;
   13413   if (hdr->sh_link != elf_dynsymtab (abfd)
   13414       || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
   13415     return 0;
   13416 
   13417   plt = bfd_get_section_by_name (abfd, ".plt");
   13418   if (plt == NULL)
   13419     return 0;
   13420 
   13421   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   13422   if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
   13423     return -1;
   13424 
   13425   count = NUM_SHDR_ENTRIES (hdr);
   13426   size = count * sizeof (asymbol);
   13427   p = relplt->relocation;
   13428   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   13429     {
   13430       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
   13431       if (p->addend != 0)
   13432 	{
   13433 #ifdef BFD64
   13434 	  size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
   13435 #else
   13436 	  size += sizeof ("+0x") - 1 + 8;
   13437 #endif
   13438 	}
   13439     }
   13440 
   13441   s = *ret = (asymbol *) bfd_malloc (size);
   13442   if (s == NULL)
   13443     return -1;
   13444 
   13445   names = (char *) (s + count);
   13446   p = relplt->relocation;
   13447   n = 0;
   13448   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   13449     {
   13450       size_t len;
   13451       bfd_vma addr;
   13452 
   13453       addr = bed->plt_sym_val (i, plt, p);
   13454       if (addr == (bfd_vma) -1)
   13455 	continue;
   13456 
   13457       *s = **p->sym_ptr_ptr;
   13458       /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   13459 	 we are defining a symbol, ensure one of them is set.  */
   13460       if ((s->flags & BSF_LOCAL) == 0)
   13461 	s->flags |= BSF_GLOBAL;
   13462       s->flags |= BSF_SYNTHETIC;
   13463       s->section = plt;
   13464       s->value = addr - plt->vma;
   13465       s->name = names;
   13466       s->udata.p = NULL;
   13467       len = strlen ((*p->sym_ptr_ptr)->name);
   13468       memcpy (names, (*p->sym_ptr_ptr)->name, len);
   13469       names += len;
   13470       if (p->addend != 0)
   13471 	{
   13472 	  char buf[30], *a;
   13473 
   13474 	  memcpy (names, "+0x", sizeof ("+0x") - 1);
   13475 	  names += sizeof ("+0x") - 1;
   13476 	  bfd_sprintf_vma (abfd, buf, p->addend);
   13477 	  for (a = buf; *a == '0'; ++a)
   13478 	    ;
   13479 	  len = strlen (a);
   13480 	  memcpy (names, a, len);
   13481 	  names += len;
   13482 	}
   13483       memcpy (names, "@plt", sizeof ("@plt"));
   13484       names += sizeof ("@plt");
   13485       ++s, ++n;
   13486     }
   13487 
   13488   return n;
   13489 }
   13490 
   13491 bool
   13492 _bfd_elf_final_write_processing (bfd *abfd)
   13493 {
   13494   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
   13495   elf_backend_data *bed = get_elf_backend_data (abfd);
   13496 
   13497   if (bed->osabi_exact && i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
   13498     i_ehdrp->e_ident[EI_OSABI] = bed->elf_osabi;
   13499 
   13500   if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_SOLARIS
   13501       || bed->target_os == is_solaris)
   13502     {
   13503       elf_tdata (abfd)->strtab_hdr.sh_flags = SHF_STRINGS;
   13504       elf_tdata (abfd)->shstrtab_hdr.sh_flags = SHF_STRINGS;
   13505     }
   13506 
   13507   /* Set the osabi field to ELFOSABI_GNU if the binary contains
   13508      SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
   13509      or STB_GNU_UNIQUE binding.  */
   13510   if (elf_tdata (abfd)->has_gnu_osabi != 0)
   13511     {
   13512       if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
   13513 	i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
   13514       else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
   13515 	       && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
   13516 	{
   13517 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
   13518 	    _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
   13519 				  "and FreeBSD targets"));
   13520 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
   13521 	    _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
   13522 				  "only by GNU and FreeBSD targets"));
   13523 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
   13524 	    _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
   13525 				  "only by GNU and FreeBSD targets"));
   13526 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
   13527 	    _bfd_error_handler (_("GNU_RETAIN section is supported "
   13528 				  "only by GNU and FreeBSD targets"));
   13529 	  bfd_set_error (bfd_error_sorry);
   13530 	  return false;
   13531 	}
   13532     }
   13533   return true;
   13534 }
   13535 
   13536 
   13537 /* Return TRUE for ELF symbol types that represent functions.
   13538    This is the default version of this function, which is sufficient for
   13539    most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
   13540 
   13541 bool
   13542 _bfd_elf_is_function_type (unsigned int type)
   13543 {
   13544   return (type == STT_FUNC
   13545 	  || type == STT_GNU_IFUNC);
   13546 }
   13547 
   13548 /* If the ELF symbol SYM might be a function in SEC, return the
   13549    function size and set *CODE_OFF to the function's entry point,
   13550    otherwise return zero.  */
   13551 
   13552 bfd_size_type
   13553 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
   13554 			     bfd_vma *code_off)
   13555 {
   13556   bfd_size_type size;
   13557   elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
   13558 
   13559   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
   13560 		     | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
   13561       || sym->section != sec)
   13562     return 0;
   13563 
   13564   size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
   13565 
   13566   /* In theory we should check that the symbol's type satisfies
   13567      _bfd_elf_is_function_type(), but there are some function-like
   13568      symbols which would fail this test.  (eg _start).  Instead
   13569      we check for hidden, local, notype symbols with zero size.
   13570      This type of symbol is generated by the annobin plugin for gcc
   13571      and clang, and should not be considered to be a function symbol.  */
   13572   if (size == 0
   13573       && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
   13574       && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
   13575       && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
   13576     return 0;
   13577 
   13578   *code_off = sym->value;
   13579   /* Do not return 0 for the function's size.  */
   13580   return size ? size : 1;
   13581 }
   13582 
   13583 /* Set to non-zero to enable some debug messages.  */
   13584 #define DEBUG_SECONDARY_RELOCS	 0
   13585 
   13586 /* An internal-to-the-bfd-library only section type
   13587    used to indicate a cached secondary reloc section.  */
   13588 #define SHT_SECONDARY_RELOC	 (SHT_LOOS + SHT_RELA)
   13589 
   13590 /* Create a BFD section to hold a secondary reloc section.  */
   13591 
   13592 bool
   13593 _bfd_elf_init_secondary_reloc_section (bfd * abfd,
   13594 				       Elf_Internal_Shdr *hdr,
   13595 				       const char * name,
   13596 				       unsigned int shindex)
   13597 {
   13598   /* We only support RELA secondary relocs.  */
   13599   if (hdr->sh_type != SHT_RELA)
   13600     return false;
   13601 
   13602 #if DEBUG_SECONDARY_RELOCS
   13603   fprintf (stderr, "secondary reloc section %s encountered\n", name);
   13604 #endif
   13605   hdr->sh_type = SHT_SECONDARY_RELOC;
   13606   return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   13607 }
   13608 
   13609 /* Read in any secondary relocs associated with SEC.  */
   13610 
   13611 bool
   13612 _bfd_elf_slurp_secondary_reloc_section (bfd *       abfd,
   13613 					asection *  sec,
   13614 					asymbol **  symbols,
   13615 					bool dynamic)
   13616 {
   13617   elf_backend_data *ebd = get_elf_backend_data (abfd);
   13618   asection * relsec;
   13619   bool result = true;
   13620   bfd_vma (*r_sym) (bfd_vma);
   13621   ufile_ptr filesize;
   13622 
   13623 #if BFD_DEFAULT_TARGET_SIZE > 32
   13624   if (bfd_arch_bits_per_address (abfd) != 32)
   13625     r_sym = elf64_r_sym;
   13626   else
   13627 #endif
   13628     r_sym = elf32_r_sym;
   13629 
   13630   if (!elf_section_data (sec)->has_secondary_relocs)
   13631     return true;
   13632 
   13633   /* Discover if there are any secondary reloc sections
   13634      associated with SEC.  */
   13635   filesize = bfd_get_file_size (abfd);
   13636   for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
   13637     {
   13638       Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
   13639 
   13640       if (hdr->sh_type == SHT_SECONDARY_RELOC
   13641 	  && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
   13642 	  && (hdr->sh_entsize == ebd->s->sizeof_rel
   13643 	      || hdr->sh_entsize == ebd->s->sizeof_rela))
   13644 	{
   13645 	  bfd_byte * native_relocs;
   13646 	  bfd_byte * native_reloc;
   13647 	  arelent * internal_relocs;
   13648 	  arelent * internal_reloc;
   13649 	  size_t i;
   13650 	  unsigned int entsize;
   13651 	  unsigned int symcount;
   13652 	  bfd_size_type reloc_count;
   13653 	  size_t amt;
   13654 
   13655 	  if (ebd->elf_info_to_howto == NULL)
   13656 	    return false;
   13657 
   13658 #if DEBUG_SECONDARY_RELOCS
   13659 	  fprintf (stderr, "read secondary relocs for %s from %s\n",
   13660 		   sec->name, relsec->name);
   13661 #endif
   13662 	  entsize = hdr->sh_entsize;
   13663 
   13664 	  if (filesize != 0
   13665 	      && ((ufile_ptr) hdr->sh_offset > filesize
   13666 		  || hdr->sh_size > filesize - hdr->sh_offset))
   13667 	    {
   13668 	      bfd_set_error (bfd_error_file_truncated);
   13669 	      result = false;
   13670 	      continue;
   13671 	    }
   13672 
   13673 	  native_relocs = bfd_malloc (hdr->sh_size);
   13674 	  if (native_relocs == NULL)
   13675 	    {
   13676 	      result = false;
   13677 	      continue;
   13678 	    }
   13679 
   13680 	  reloc_count = NUM_SHDR_ENTRIES (hdr);
   13681 	  if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
   13682 	    {
   13683 	      free (native_relocs);
   13684 	      bfd_set_error (bfd_error_file_too_big);
   13685 	      result = false;
   13686 	      continue;
   13687 	    }
   13688 
   13689 	  internal_relocs = (arelent *) bfd_alloc (abfd, amt);
   13690 	  if (internal_relocs == NULL)
   13691 	    {
   13692 	      free (native_relocs);
   13693 	      result = false;
   13694 	      continue;
   13695 	    }
   13696 
   13697 	  if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   13698 	      || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size)
   13699 	    {
   13700 	      free (native_relocs);
   13701 	      /* The internal_relocs will be freed when
   13702 		 the memory for the bfd is released.  */
   13703 	      result = false;
   13704 	      continue;
   13705 	    }
   13706 
   13707 	  if (dynamic)
   13708 	    symcount = bfd_get_dynamic_symcount (abfd);
   13709 	  else
   13710 	    symcount = bfd_get_symcount (abfd);
   13711 
   13712 	  for (i = 0, internal_reloc = internal_relocs,
   13713 		 native_reloc = native_relocs;
   13714 	       i < reloc_count;
   13715 	       i++, internal_reloc++, native_reloc += entsize)
   13716 	    {
   13717 	      bool res;
   13718 	      Elf_Internal_Rela rela;
   13719 
   13720 	      if (entsize == ebd->s->sizeof_rel)
   13721 		ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
   13722 	      else /* entsize == ebd->s->sizeof_rela */
   13723 		ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
   13724 
   13725 	      /* The address of an ELF reloc is section relative for an object
   13726 		 file, and absolute for an executable file or shared library.
   13727 		 The address of a normal BFD reloc is always section relative,
   13728 		 and the address of a dynamic reloc is absolute..  */
   13729 	      if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
   13730 		internal_reloc->address = rela.r_offset;
   13731 	      else
   13732 		internal_reloc->address = rela.r_offset - sec->vma;
   13733 
   13734 	      if (r_sym (rela.r_info) == STN_UNDEF)
   13735 		{
   13736 		  /* FIXME: This and the error case below mean that we
   13737 		     have a symbol on relocs that is not elf_symbol_type.  */
   13738 		  internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
   13739 		}
   13740 	      else if (r_sym (rela.r_info) > symcount)
   13741 		{
   13742 		  _bfd_error_handler
   13743 		    /* xgettext:c-format */
   13744 		    (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
   13745 		     abfd, sec, i, (long) r_sym (rela.r_info));
   13746 		  bfd_set_error (bfd_error_bad_value);
   13747 		  internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
   13748 		  result = false;
   13749 		}
   13750 	      else
   13751 		{
   13752 		  asymbol **ps;
   13753 
   13754 		  ps = symbols + r_sym (rela.r_info) - 1;
   13755 		  internal_reloc->sym_ptr_ptr = ps;
   13756 		  /* Make sure that this symbol is not removed by strip.  */
   13757 		  (*ps)->flags |= BSF_KEEP;
   13758 		}
   13759 
   13760 	      internal_reloc->addend = rela.r_addend;
   13761 
   13762 	      res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
   13763 	      if (! res || internal_reloc->howto == NULL)
   13764 		{
   13765 #if DEBUG_SECONDARY_RELOCS
   13766 		  fprintf (stderr,
   13767 			   "there is no howto associated with reloc %lx\n",
   13768 			   rela.r_info);
   13769 #endif
   13770 		  result = false;
   13771 		}
   13772 	    }
   13773 
   13774 	  free (native_relocs);
   13775 	  /* Store the internal relocs.  */
   13776 	  relsec->sec_info = internal_relocs;
   13777 	}
   13778     }
   13779 
   13780   return result;
   13781 }
   13782 
   13783 /* Set the ELF section header fields of an output secondary reloc section.  */
   13784 
   13785 bool
   13786 _bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
   13787 				      bfd *obfd ATTRIBUTE_UNUSED,
   13788 				      const Elf_Internal_Shdr *isection,
   13789 				      Elf_Internal_Shdr *osection)
   13790 {
   13791   asection * isec;
   13792   asection * osec;
   13793   struct bfd_elf_section_data * esd;
   13794 
   13795   if (isection == NULL)
   13796     return false;
   13797 
   13798   if (isection->sh_type != SHT_SECONDARY_RELOC)
   13799     return true;
   13800 
   13801   isec = isection->bfd_section;
   13802   if (isec == NULL)
   13803     return false;
   13804 
   13805   osec = osection->bfd_section;
   13806   if (osec == NULL)
   13807     return false;
   13808 
   13809   BFD_ASSERT (osec->sec_info == NULL);
   13810   osec->sec_info = isec->sec_info;
   13811   osection->sh_type = SHT_RELA;
   13812   osection->sh_link = elf_onesymtab (obfd);
   13813   if (osection->sh_link == 0)
   13814     {
   13815       /* There is no symbol table - we are hosed...  */
   13816       _bfd_error_handler
   13817 	/* xgettext:c-format */
   13818 	(_("%pB(%pA): link section cannot be set"
   13819 	   " because the output file does not have a symbol table"),
   13820 	obfd, osec);
   13821       bfd_set_error (bfd_error_bad_value);
   13822       return false;
   13823     }
   13824 
   13825   /* Find the output section that corresponds to the isection's
   13826      sh_info link.  */
   13827   if (isection->sh_info == 0
   13828       || isection->sh_info >= elf_numsections (ibfd))
   13829     {
   13830       _bfd_error_handler
   13831 	/* xgettext:c-format */
   13832 	(_("%pB(%pA): info section index is invalid"),
   13833 	obfd, osec);
   13834       bfd_set_error (bfd_error_bad_value);
   13835       return false;
   13836     }
   13837 
   13838   isection = elf_elfsections (ibfd)[isection->sh_info];
   13839 
   13840   if (isection == NULL
   13841       || isection->bfd_section == NULL
   13842       || isection->bfd_section->output_section == NULL)
   13843     {
   13844       _bfd_error_handler
   13845 	/* xgettext:c-format */
   13846 	(_("%pB(%pA): info section index cannot be set"
   13847 	   " because the section is not in the output"),
   13848 	obfd, osec);
   13849       bfd_set_error (bfd_error_bad_value);
   13850       return false;
   13851     }
   13852 
   13853   esd = elf_section_data (isection->bfd_section->output_section);
   13854   BFD_ASSERT (esd != NULL);
   13855   osection->sh_info = esd->this_idx;
   13856   esd->has_secondary_relocs = true;
   13857 #if DEBUG_SECONDARY_RELOCS
   13858   fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
   13859 	   osec->name, osection->sh_link, osection->sh_info);
   13860   fprintf (stderr, "mark section %s as having secondary relocs\n",
   13861 	   bfd_section_name (isection->bfd_section->output_section));
   13862 #endif
   13863 
   13864   return true;
   13865 }
   13866 
   13867 /* Write out a secondary reloc section.
   13868 
   13869    FIXME: Currently this function can result in a serious performance penalty
   13870    for files with secondary relocs and lots of sections.  The proper way to
   13871    fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
   13872    relocs together and then to have this function just walk that chain.  */
   13873 
   13874 bool
   13875 _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
   13876 {
   13877   elf_backend_data *ebd = get_elf_backend_data (abfd);
   13878   bfd_vma addr_offset;
   13879   asection * relsec;
   13880   bfd_vma (*r_info) (bfd_vma, bfd_vma);
   13881   bool result = true;
   13882 
   13883   if (sec == NULL)
   13884     return false;
   13885 
   13886 #if BFD_DEFAULT_TARGET_SIZE > 32
   13887   if (bfd_arch_bits_per_address (abfd) != 32)
   13888     r_info = elf64_r_info;
   13889   else
   13890 #endif
   13891     r_info = elf32_r_info;
   13892 
   13893   /* The address of an ELF reloc is section relative for an object
   13894      file, and absolute for an executable file or shared library.
   13895      The address of a BFD reloc is always section relative.  */
   13896   addr_offset = 0;
   13897   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
   13898     addr_offset = sec->vma;
   13899 
   13900   /* Discover if there are any secondary reloc sections
   13901      associated with SEC.  */
   13902   for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
   13903     {
   13904       const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
   13905       Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
   13906 
   13907       if (hdr->sh_type == SHT_RELA
   13908 	  && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
   13909 	{
   13910 	  asymbol *    last_sym;
   13911 	  int          last_sym_idx;
   13912 	  size_t       reloc_count;
   13913 	  size_t       idx;
   13914 	  bfd_size_type entsize;
   13915 	  arelent *    src_irel;
   13916 	  bfd_byte *   dst_rela;
   13917 
   13918 	  if (hdr->contents != NULL)
   13919 	    {
   13920 	      _bfd_error_handler
   13921 		/* xgettext:c-format */
   13922 		(_("%pB(%pA): error: secondary reloc section processed twice"),
   13923 		 abfd, relsec);
   13924 	      bfd_set_error (bfd_error_bad_value);
   13925 	      result = false;
   13926 	      continue;
   13927 	    }
   13928 
   13929 	  entsize = hdr->sh_entsize;
   13930 	  if (entsize == 0)
   13931 	    {
   13932 	      _bfd_error_handler
   13933 		/* xgettext:c-format */
   13934 		(_("%pB(%pA): error: secondary reloc section"
   13935 		   " has zero sized entries"),
   13936 		 abfd, relsec);
   13937 	      bfd_set_error (bfd_error_bad_value);
   13938 	      result = false;
   13939 	      continue;
   13940 	    }
   13941 	  else if (entsize != ebd->s->sizeof_rel
   13942 		   && entsize != ebd->s->sizeof_rela)
   13943 	    {
   13944 	      _bfd_error_handler
   13945 		/* xgettext:c-format */
   13946 		(_("%pB(%pA): error: secondary reloc section"
   13947 		   " has non-standard sized entries"),
   13948 		 abfd, relsec);
   13949 	      bfd_set_error (bfd_error_bad_value);
   13950 	      result = false;
   13951 	      continue;
   13952 	    }
   13953 
   13954 	  reloc_count = hdr->sh_size / entsize;
   13955 	  hdr->sh_size = entsize * reloc_count;
   13956 	  if (reloc_count == 0)
   13957 	    {
   13958 	      _bfd_error_handler
   13959 		/* xgettext:c-format */
   13960 		(_("%pB(%pA): error: secondary reloc section is empty!"),
   13961 		 abfd, relsec);
   13962 	      bfd_set_error (bfd_error_bad_value);
   13963 	      result = false;
   13964 	      continue;
   13965 	    }
   13966 
   13967 	  hdr->contents = bfd_alloc (abfd, hdr->sh_size);
   13968 	  if (hdr->contents == NULL)
   13969 	    continue;
   13970 	  relsec->alloced = 1;
   13971 
   13972 #if DEBUG_SECONDARY_RELOCS
   13973 	  fprintf (stderr, "write %u secondary relocs for %s from %s\n",
   13974 		   reloc_count, sec->name, relsec->name);
   13975 #endif
   13976 	  last_sym = NULL;
   13977 	  last_sym_idx = 0;
   13978 	  dst_rela = hdr->contents;
   13979 	  src_irel = sec->sec_info;
   13980 	  if (src_irel == NULL)
   13981 	    {
   13982 	      _bfd_error_handler
   13983 		/* xgettext:c-format */
   13984 		(_("%pB(%pA): error: internal relocs missing"
   13985 		   " for secondary reloc section"),
   13986 		 abfd, relsec);
   13987 	      bfd_set_error (bfd_error_bad_value);
   13988 	      result = false;
   13989 	      continue;
   13990 	    }
   13991 
   13992 	  for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
   13993 	    {
   13994 	      Elf_Internal_Rela src_rela;
   13995 	      arelent *ptr;
   13996 	      asymbol *sym;
   13997 	      int n;
   13998 
   13999 	      ptr = src_irel + idx;
   14000 	      if (ptr == NULL)
   14001 		{
   14002 		  _bfd_error_handler
   14003 		    /* xgettext:c-format */
   14004 		    (_("%pB(%pA): error: reloc table entry %zu is empty"),
   14005 		     abfd, relsec, idx);
   14006 		  bfd_set_error (bfd_error_bad_value);
   14007 		  result = false;
   14008 		  break;
   14009 		}
   14010 
   14011 	      if (ptr->sym_ptr_ptr == NULL)
   14012 		{
   14013 		  /* FIXME: Is this an error ? */
   14014 		  n = 0;
   14015 		}
   14016 	      else
   14017 		{
   14018 		  sym = *ptr->sym_ptr_ptr;
   14019 
   14020 		  if (sym == last_sym)
   14021 		    n = last_sym_idx;
   14022 		  else
   14023 		    {
   14024 		      n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
   14025 		      if (n < 0)
   14026 			{
   14027 			  _bfd_error_handler
   14028 			    /* xgettext:c-format */
   14029 			    (_("%pB(%pA): error: secondary reloc %zu"
   14030 			       " references a missing symbol"),
   14031 			     abfd, relsec, idx);
   14032 			  bfd_set_error (bfd_error_bad_value);
   14033 			  result = false;
   14034 			  n = 0;
   14035 			}
   14036 
   14037 		      last_sym = sym;
   14038 		      last_sym_idx = n;
   14039 		    }
   14040 
   14041 		  if (sym->the_bfd != NULL
   14042 		      && sym->the_bfd->xvec != abfd->xvec
   14043 		      && ! _bfd_elf_validate_reloc (abfd, ptr))
   14044 		    {
   14045 		      _bfd_error_handler
   14046 			/* xgettext:c-format */
   14047 			(_("%pB(%pA): error: secondary reloc %zu"
   14048 			   " references a deleted symbol"),
   14049 			 abfd, relsec, idx);
   14050 		      bfd_set_error (bfd_error_bad_value);
   14051 		      result = false;
   14052 		      n = 0;
   14053 		    }
   14054 		}
   14055 
   14056 	      src_rela.r_offset = ptr->address + addr_offset;
   14057 	      if (ptr->howto == NULL)
   14058 		{
   14059 		  _bfd_error_handler
   14060 		    /* xgettext:c-format */
   14061 		    (_("%pB(%pA): error: secondary reloc %zu"
   14062 		       " is of an unknown type"),
   14063 		     abfd, relsec, idx);
   14064 		  bfd_set_error (bfd_error_bad_value);
   14065 		  result = false;
   14066 		  src_rela.r_info = r_info (0, 0);
   14067 		}
   14068 	      else
   14069 		src_rela.r_info = r_info (n, ptr->howto->type);
   14070 	      src_rela.r_addend = ptr->addend;
   14071 
   14072 	      if (entsize == ebd->s->sizeof_rel)
   14073 		ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
   14074 	      else /* entsize == ebd->s->sizeof_rela */
   14075 		ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
   14076 	    }
   14077 	}
   14078     }
   14079 
   14080   return result;
   14081 }
   14082 
   14083 /* Mmap in section contents.  */
   14084 
   14085 static bool
   14086 elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
   14087 {
   14088 #ifdef USE_MMAP
   14089   elf_backend_data *bed = get_elf_backend_data (abfd);
   14090   if (bed->use_mmap
   14091       && sec->compress_status == COMPRESS_SECTION_NONE
   14092       && (sec->flags & SEC_LINKER_CREATED) == 0)
   14093     {
   14094       /* Use mmap only if section size >= the minimum mmap section
   14095 	 size.  */
   14096       size_t readsz = bfd_get_section_limit_octets (abfd, sec);
   14097       size_t allocsz = bfd_get_section_alloc_size (abfd, sec);
   14098       if (readsz == allocsz && readsz >= _bfd_minimum_mmap_size)
   14099 	{
   14100 	  if (sec->contents != NULL)
   14101 	    {
   14102 	      if (!sec->mmapped_p)
   14103 		abort ();
   14104 	      *buf = sec->contents;
   14105 	      return true;
   14106 	    }
   14107 	  if (sec->mmapped_p)
   14108 	    abort ();
   14109 	  sec->mmapped_p = 1;
   14110 
   14111 	  /* We can't use the final link preallocated buffer for mmap.  */
   14112 	  *buf = NULL;
   14113 	}
   14114     }
   14115 #endif
   14116   /* FIXME: We should not get here if sec->alloced is set.  */
   14117   bool ret = bfd_get_full_section_contents (abfd, sec, buf);
   14118   if (ret && sec->mmapped_p)
   14119     *buf = sec->contents;
   14120   return ret;
   14121 }
   14122 
   14123 /* Mmap in section contents.  */
   14124 
   14125 bool
   14126 _bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
   14127 {
   14128   *buf = NULL;
   14129   return elf_mmap_section_contents (abfd, sec, buf);
   14130 }
   14131 
   14132 /* Mmap in the full section contents for the final link.  */
   14133 
   14134 bool
   14135 _bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec,
   14136 				     bfd_byte **buf)
   14137 {
   14138   return elf_mmap_section_contents (abfd, sec, buf);
   14139 }
   14140 
   14141 /* Munmap section contents.  */
   14142 
   14143 void
   14144 _bfd_elf_munmap_section_contents (asection *sec, void *contents)
   14145 {
   14146   /* NB: Since _bfd_elf_munmap_section_contents is called like free,
   14147      CONTENTS may be NULL.  */
   14148   if (contents == NULL)
   14149     return;
   14150 
   14151   if (sec->alloced
   14152       /* What a tangled web we weave with section contents.
   14153 	 FIXME: We shouldn't need to test anything but sec->alloced
   14154 	 here, but there are cases where a buffer is allocated for a
   14155 	 section but then another buffer is malloc'd anyway.  eg.
   14156 	 trace through ld-elf/eh4 testcase on x86_64.  */
   14157       && (sec->contents == contents
   14158 	  || elf_section_data (sec)->this_hdr.contents == contents))
   14159     return;
   14160 
   14161   /* Don't leave pointers to data we are about to munmap or free.  */
   14162   if (sec->contents == contents)
   14163     sec->contents = NULL;
   14164   if (elf_section_data (sec)->this_hdr.contents == contents)
   14165     elf_section_data (sec)->this_hdr.contents = NULL;
   14166 
   14167 #ifdef USE_MMAP
   14168   if (sec->mmapped_p)
   14169     {
   14170       /* When _bfd_elf_mmap_section_contents returns CONTENTS as
   14171 	 malloced, CONTENTS_ADDR is set to NULL.  */
   14172       if (elf_section_data (sec)->contents_addr != NULL)
   14173 	{
   14174 	  /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
   14175 	  if (munmap (elf_section_data (sec)->contents_addr,
   14176 		      elf_section_data (sec)->contents_size) != 0)
   14177 	    abort ();
   14178 	  sec->mmapped_p = 0;
   14179 	  elf_section_data (sec)->contents_addr = NULL;
   14180 	  elf_section_data (sec)->contents_size = 0;
   14181 	  return;
   14182 	}
   14183     }
   14184 #endif
   14185 
   14186   free (contents);
   14187 }
   14188 
   14189 /* Munmap the full section contents for the final link.  */
   14190 
   14191 void
   14192 _bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED)
   14193 {
   14194 #ifdef USE_MMAP
   14195   if (sec->mmapped_p && elf_section_data (sec)->contents_addr != NULL)
   14196     {
   14197       /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as
   14198 	 malloced, CONTENTS_ADDR is set to NULL.  */
   14199       /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
   14200       if (munmap (elf_section_data (sec)->contents_addr,
   14201 		  elf_section_data (sec)->contents_size) != 0)
   14202 	abort ();
   14203       sec->mmapped_p = 0;
   14204       sec->contents = NULL;
   14205       elf_section_data (sec)->this_hdr.contents = NULL;
   14206       elf_section_data (sec)->contents_addr = NULL;
   14207       elf_section_data (sec)->contents_size = 0;
   14208     }
   14209 #endif
   14210 }
   14211