Home | History | Annotate | Line # | Download | only in bfd
elf.c revision 1.17
      1 /* ELF executable support for BFD.
      2 
      3    Copyright (C) 1993-2024 Free Software Foundation, Inc.
      4 
      5    This file is part of BFD, the Binary File Descriptor library.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program; if not, write to the Free Software
     19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     20    MA 02110-1301, USA.  */
     21 
     22 
     23 /*
     24 SECTION
     25 	ELF backends
     26 
     27 	BFD support for ELF formats is being worked on.
     28 	Currently, the best supported back ends are for sparc and i386
     29 	(running svr4 or Solaris 2).
     30 
     31 	Documentation of the internals of the support code still needs
     32 	to be written.  The code is changing quickly enough that we
     33 	haven't bothered yet.  */
     34 
     35 /* For sparc64-cross-sparc32.  */
     36 #define _SYSCALL32
     37 #include "sysdep.h"
     38 #include <limits.h>
     39 #include "bfd.h"
     40 #include "bfdlink.h"
     41 #include "libbfd.h"
     42 #define ARCH_SIZE 0
     43 #include "elf-bfd.h"
     44 #include "libiberty.h"
     45 #include "safe-ctype.h"
     46 #include "elf-linux-core.h"
     47 
     48 #ifdef CORE_HEADER
     49 #include CORE_HEADER
     50 #endif
     51 
     52 static int elf_sort_sections (const void *, const void *);
     53 static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
     54 static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
     55 			   struct bfd_link_info *);
     56 static bool elf_parse_notes (bfd *abfd, char *buf, size_t size,
     57 			     file_ptr offset, size_t align);
     58 
     59 /* Swap version information in and out.  The version information is
     60    currently size independent.  If that ever changes, this code will
     61    need to move into elfcode.h.  */
     62 
     63 /* Swap in a Verdef structure.  */
     64 
     65 void
     66 _bfd_elf_swap_verdef_in (bfd *abfd,
     67 			 const Elf_External_Verdef *src,
     68 			 Elf_Internal_Verdef *dst)
     69 {
     70   dst->vd_version = H_GET_16 (abfd, src->vd_version);
     71   dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
     72   dst->vd_ndx     = H_GET_16 (abfd, src->vd_ndx);
     73   dst->vd_cnt     = H_GET_16 (abfd, src->vd_cnt);
     74   dst->vd_hash    = H_GET_32 (abfd, src->vd_hash);
     75   dst->vd_aux     = H_GET_32 (abfd, src->vd_aux);
     76   dst->vd_next    = H_GET_32 (abfd, src->vd_next);
     77 }
     78 
     79 /* Swap out a Verdef structure.  */
     80 
     81 void
     82 _bfd_elf_swap_verdef_out (bfd *abfd,
     83 			  const Elf_Internal_Verdef *src,
     84 			  Elf_External_Verdef *dst)
     85 {
     86   H_PUT_16 (abfd, src->vd_version, dst->vd_version);
     87   H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
     88   H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
     89   H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
     90   H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
     91   H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
     92   H_PUT_32 (abfd, src->vd_next, dst->vd_next);
     93 }
     94 
     95 /* Swap in a Verdaux structure.  */
     96 
     97 void
     98 _bfd_elf_swap_verdaux_in (bfd *abfd,
     99 			  const Elf_External_Verdaux *src,
    100 			  Elf_Internal_Verdaux *dst)
    101 {
    102   dst->vda_name = H_GET_32 (abfd, src->vda_name);
    103   dst->vda_next = H_GET_32 (abfd, src->vda_next);
    104 }
    105 
    106 /* Swap out a Verdaux structure.  */
    107 
    108 void
    109 _bfd_elf_swap_verdaux_out (bfd *abfd,
    110 			   const Elf_Internal_Verdaux *src,
    111 			   Elf_External_Verdaux *dst)
    112 {
    113   H_PUT_32 (abfd, src->vda_name, dst->vda_name);
    114   H_PUT_32 (abfd, src->vda_next, dst->vda_next);
    115 }
    116 
    117 /* Swap in a Verneed structure.  */
    118 
    119 void
    120 _bfd_elf_swap_verneed_in (bfd *abfd,
    121 			  const Elf_External_Verneed *src,
    122 			  Elf_Internal_Verneed *dst)
    123 {
    124   dst->vn_version = H_GET_16 (abfd, src->vn_version);
    125   dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
    126   dst->vn_file    = H_GET_32 (abfd, src->vn_file);
    127   dst->vn_aux     = H_GET_32 (abfd, src->vn_aux);
    128   dst->vn_next    = H_GET_32 (abfd, src->vn_next);
    129 }
    130 
    131 /* Swap out a Verneed structure.  */
    132 
    133 void
    134 _bfd_elf_swap_verneed_out (bfd *abfd,
    135 			   const Elf_Internal_Verneed *src,
    136 			   Elf_External_Verneed *dst)
    137 {
    138   H_PUT_16 (abfd, src->vn_version, dst->vn_version);
    139   H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
    140   H_PUT_32 (abfd, src->vn_file, dst->vn_file);
    141   H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
    142   H_PUT_32 (abfd, src->vn_next, dst->vn_next);
    143 }
    144 
    145 /* Swap in a Vernaux structure.  */
    146 
    147 void
    148 _bfd_elf_swap_vernaux_in (bfd *abfd,
    149 			  const Elf_External_Vernaux *src,
    150 			  Elf_Internal_Vernaux *dst)
    151 {
    152   dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
    153   dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
    154   dst->vna_other = H_GET_16 (abfd, src->vna_other);
    155   dst->vna_name  = H_GET_32 (abfd, src->vna_name);
    156   dst->vna_next  = H_GET_32 (abfd, src->vna_next);
    157 }
    158 
    159 /* Swap out a Vernaux structure.  */
    160 
    161 void
    162 _bfd_elf_swap_vernaux_out (bfd *abfd,
    163 			   const Elf_Internal_Vernaux *src,
    164 			   Elf_External_Vernaux *dst)
    165 {
    166   H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
    167   H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
    168   H_PUT_16 (abfd, src->vna_other, dst->vna_other);
    169   H_PUT_32 (abfd, src->vna_name, dst->vna_name);
    170   H_PUT_32 (abfd, src->vna_next, dst->vna_next);
    171 }
    172 
    173 /* Swap in a Versym structure.  */
    174 
    175 void
    176 _bfd_elf_swap_versym_in (bfd *abfd,
    177 			 const Elf_External_Versym *src,
    178 			 Elf_Internal_Versym *dst)
    179 {
    180   dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
    181 }
    182 
    183 /* Swap out a Versym structure.  */
    184 
    185 void
    186 _bfd_elf_swap_versym_out (bfd *abfd,
    187 			  const Elf_Internal_Versym *src,
    188 			  Elf_External_Versym *dst)
    189 {
    190   H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
    191 }
    192 
    193 /* Standard ELF hash function.  Do not change this function; you will
    194    cause invalid hash tables to be generated.  */
    195 
    196 unsigned long
    197 bfd_elf_hash (const char *namearg)
    198 {
    199   uint32_t h = 0;
    200 
    201   for (const unsigned char *name = (const unsigned char *) namearg;
    202        *name; name++)
    203     {
    204       h = (h << 4) + *name;
    205       h ^= (h >> 24) & 0xf0;
    206     }
    207   return h & 0x0fffffff;
    208 }
    209 
    210 /* DT_GNU_HASH hash function.  Do not change this function; you will
    211    cause invalid hash tables to be generated.  */
    212 
    213 unsigned long
    214 bfd_elf_gnu_hash (const char *namearg)
    215 {
    216   uint32_t h = 5381;
    217 
    218   for (const unsigned char *name = (const unsigned char *) namearg;
    219        *name; name++)
    220     h = (h << 5) + h + *name;
    221   return h;
    222 }
    223 
    224 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
    225    the object_id field of an elf_obj_tdata field set to OBJECT_ID.  */
    226 bool
    227 bfd_elf_allocate_object (bfd *abfd,
    228 			 size_t object_size,
    229 			 enum elf_target_id object_id)
    230 {
    231   BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
    232   abfd->tdata.any = bfd_zalloc (abfd, object_size);
    233   if (abfd->tdata.any == NULL)
    234     return false;
    235 
    236   elf_object_id (abfd) = object_id;
    237   if (abfd->direction != read_direction)
    238     {
    239       struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
    240       if (o == NULL)
    241 	return false;
    242       elf_tdata (abfd)->o = o;
    243       elf_program_header_size (abfd) = (bfd_size_type) -1;
    244     }
    245   return true;
    246 }
    247 
    248 
    249 bool
    250 bfd_elf_make_object (bfd *abfd)
    251 {
    252   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    253   return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
    254 				  bed->target_id);
    255 }
    256 
    257 bool
    258 bfd_elf_mkcorefile (bfd *abfd)
    259 {
    260   /* I think this can be done just like an object file.  */
    261   if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
    262     return false;
    263   elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
    264   return elf_tdata (abfd)->core != NULL;
    265 }
    266 
    267 char *
    268 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
    269 {
    270   Elf_Internal_Shdr **i_shdrp;
    271   bfd_byte *shstrtab = NULL;
    272   file_ptr offset;
    273   bfd_size_type shstrtabsize;
    274 
    275   i_shdrp = elf_elfsections (abfd);
    276   if (i_shdrp == 0
    277       || shindex >= elf_numsections (abfd)
    278       || i_shdrp[shindex] == 0)
    279     return NULL;
    280 
    281   shstrtab = i_shdrp[shindex]->contents;
    282   if (shstrtab == NULL)
    283     {
    284       /* No cached one, attempt to read, and cache what we read.  */
    285       offset = i_shdrp[shindex]->sh_offset;
    286       shstrtabsize = i_shdrp[shindex]->sh_size;
    287 
    288       /* Allocate and clear an extra byte at the end, to prevent crashes
    289 	 in case the string table is not terminated.  */
    290       if (shstrtabsize + 1 <= 1
    291 	  || bfd_seek (abfd, offset, SEEK_SET) != 0
    292 	  || (shstrtab
    293 	      = _bfd_mmap_readonly_persistent (abfd, shstrtabsize)) == NULL)
    294 	{
    295 	  /* Once we've failed to read it, make sure we don't keep
    296 	     trying.  Otherwise, we'll keep allocating space for
    297 	     the string table over and over.  */
    298 	  i_shdrp[shindex]->sh_size = 0;
    299 	}
    300       else if (shstrtab[shstrtabsize - 1] != '\0')
    301 	{
    302 	  /* It is an error if a string table isn't terminated.  */
    303 	  _bfd_error_handler
    304 	    /* xgettext:c-format */
    305 	    (_("%pB(%pA): string table is corrupt"),
    306 	     abfd, i_shdrp[shindex]->bfd_section);
    307 	  return NULL;
    308 	}
    309       i_shdrp[shindex]->contents = shstrtab;
    310     }
    311   return (char *) shstrtab;
    312 }
    313 
    314 char *
    315 bfd_elf_string_from_elf_section (bfd *abfd,
    316 				 unsigned int shindex,
    317 				 unsigned int strindex)
    318 {
    319   Elf_Internal_Shdr *hdr;
    320 
    321   if (strindex == 0)
    322     return "";
    323 
    324   if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
    325     return NULL;
    326 
    327   hdr = elf_elfsections (abfd)[shindex];
    328 
    329   if (hdr->contents == NULL)
    330     {
    331       if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
    332 	{
    333 	  /* PR 17512: file: f057ec89.  */
    334 	  /* xgettext:c-format */
    335 	  _bfd_error_handler (_("%pB: attempt to load strings from"
    336 				" a non-string section (number %d)"),
    337 			      abfd, shindex);
    338 	  return NULL;
    339 	}
    340 
    341       if (bfd_elf_get_str_section (abfd, shindex) == NULL)
    342 	return NULL;
    343     }
    344   else
    345     {
    346       /* PR 24273: The string section's contents may have already
    347 	 been loaded elsewhere, eg because a corrupt file has the
    348 	 string section index in the ELF header pointing at a group
    349 	 section.  So be paranoid, and test that the last byte of
    350 	 the section is zero.  */
    351       if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
    352 	return NULL;
    353     }
    354 
    355   if (strindex >= hdr->sh_size)
    356     {
    357       unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
    358       _bfd_error_handler
    359 	/* xgettext:c-format */
    360 	(_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
    361 	 abfd, strindex, (uint64_t) hdr->sh_size,
    362 	 (shindex == shstrndx && strindex == hdr->sh_name
    363 	  ? ".shstrtab"
    364 	  : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
    365       return NULL;
    366     }
    367 
    368   return ((char *) hdr->contents) + strindex;
    369 }
    370 
    371 /* Read and convert symbols to internal format.
    372    SYMCOUNT specifies the number of symbols to read, starting from
    373    symbol SYMOFFSET.  If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
    374    are non-NULL, they are used to store the internal symbols, external
    375    symbols, and symbol section index extensions, respectively.
    376    Returns a pointer to the internal symbol buffer (malloced if necessary)
    377    or NULL if there were no symbols or some kind of problem.  */
    378 
    379 Elf_Internal_Sym *
    380 bfd_elf_get_elf_syms (bfd *ibfd,
    381 		      Elf_Internal_Shdr *symtab_hdr,
    382 		      size_t symcount,
    383 		      size_t symoffset,
    384 		      Elf_Internal_Sym *intsym_buf,
    385 		      void *extsym_buf,
    386 		      Elf_External_Sym_Shndx *extshndx_buf)
    387 {
    388   Elf_Internal_Shdr *shndx_hdr;
    389   void *alloc_ext;
    390   const bfd_byte *esym;
    391   Elf_External_Sym_Shndx *alloc_extshndx;
    392   Elf_External_Sym_Shndx *shndx;
    393   Elf_Internal_Sym *alloc_intsym;
    394   Elf_Internal_Sym *isym;
    395   Elf_Internal_Sym *isymend;
    396   const struct elf_backend_data *bed;
    397   size_t extsym_size;
    398   size_t amt;
    399   file_ptr pos;
    400 
    401   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
    402     abort ();
    403 
    404   if (symcount == 0)
    405     return intsym_buf;
    406 
    407   if (elf_use_dt_symtab_p (ibfd))
    408     {
    409       /* Use dynamic symbol table.  */
    410       if (elf_tdata (ibfd)->dt_symtab_count != symcount + symoffset)
    411 	{
    412 	  bfd_set_error (bfd_error_invalid_operation);
    413 	  return NULL;
    414 	}
    415       return elf_tdata (ibfd)->dt_symtab + symoffset;
    416     }
    417 
    418   /* Normal syms might have section extension entries.  */
    419   shndx_hdr = NULL;
    420   if (elf_symtab_shndx_list (ibfd) != NULL)
    421     {
    422       elf_section_list * entry;
    423       Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
    424 
    425       /* Find an index section that is linked to this symtab section.  */
    426       for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
    427 	{
    428 	  /* PR 20063.  */
    429 	  if (entry->hdr.sh_link >= elf_numsections (ibfd))
    430 	    continue;
    431 
    432 	  if (sections[entry->hdr.sh_link] == symtab_hdr)
    433 	    {
    434 	      shndx_hdr = & entry->hdr;
    435 	      break;
    436 	    };
    437 	}
    438 
    439       if (shndx_hdr == NULL)
    440 	{
    441 	  if (symtab_hdr == &elf_symtab_hdr (ibfd))
    442 	    /* Not really accurate, but this was how the old code used
    443 	       to work.  */
    444 	    shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
    445 	  /* Otherwise we do nothing.  The assumption is that
    446 	     the index table will not be needed.  */
    447 	}
    448     }
    449 
    450   /* Read the symbols.  */
    451   alloc_ext = NULL;
    452   alloc_extshndx = NULL;
    453   alloc_intsym = NULL;
    454   bed = get_elf_backend_data (ibfd);
    455   extsym_size = bed->s->sizeof_sym;
    456   if (_bfd_mul_overflow (symcount, extsym_size, &amt))
    457     {
    458       bfd_set_error (bfd_error_file_too_big);
    459       return NULL;
    460     }
    461   pos = symtab_hdr->sh_offset + symoffset * extsym_size;
    462   size_t alloc_ext_size = amt;
    463   if (bfd_seek (ibfd, pos, SEEK_SET) != 0
    464       || !_bfd_mmap_read_temporary (&extsym_buf, &alloc_ext_size,
    465 				    &alloc_ext, ibfd, false))
    466     {
    467       intsym_buf = NULL;
    468       goto out2;
    469     }
    470 
    471   size_t alloc_extshndx_size = 0;
    472   if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
    473     extshndx_buf = NULL;
    474   else
    475     {
    476       if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
    477 	{
    478 	  bfd_set_error (bfd_error_file_too_big);
    479 	  intsym_buf = NULL;
    480 	  goto out1;
    481 	}
    482       alloc_extshndx_size = amt;
    483       pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
    484       if (bfd_seek (ibfd, pos, SEEK_SET) != 0
    485 	  || !_bfd_mmap_read_temporary ((void **) &extshndx_buf,
    486 					&alloc_extshndx_size,
    487 					(void **) &alloc_extshndx,
    488 					ibfd, false))
    489 	{
    490 	  intsym_buf = NULL;
    491 	  goto out1;
    492 	}
    493     }
    494 
    495   if (intsym_buf == NULL)
    496     {
    497       if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
    498 	{
    499 	  bfd_set_error (bfd_error_file_too_big);
    500 	  goto out1;
    501 	}
    502       alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
    503       intsym_buf = alloc_intsym;
    504       if (intsym_buf == NULL)
    505 	goto out1;
    506     }
    507 
    508   /* Convert the symbols to internal form.  */
    509   isymend = intsym_buf + symcount;
    510   for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
    511 	   shndx = extshndx_buf;
    512        isym < isymend;
    513        esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
    514     if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
    515       {
    516 	symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
    517 	/* xgettext:c-format */
    518 	_bfd_error_handler (_("%pB symbol number %lu references"
    519 			      " nonexistent SHT_SYMTAB_SHNDX section"),
    520 			    ibfd, (unsigned long) symoffset);
    521 	free (alloc_intsym);
    522 	intsym_buf = NULL;
    523 	goto out1;
    524       }
    525 
    526  out1:
    527   _bfd_munmap_readonly_temporary (alloc_extshndx, alloc_extshndx_size);
    528  out2:
    529   _bfd_munmap_readonly_temporary (alloc_ext, alloc_ext_size);
    530 
    531   return intsym_buf;
    532 }
    533 
    534 /* Look up a symbol name.  */
    535 const char *
    536 bfd_elf_sym_name (bfd *abfd,
    537 		  Elf_Internal_Shdr *symtab_hdr,
    538 		  Elf_Internal_Sym *isym,
    539 		  asection *sym_sec)
    540 {
    541   const char *name;
    542   unsigned int iname = isym->st_name;
    543   unsigned int shindex = symtab_hdr->sh_link;
    544 
    545   if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
    546       /* Check for a bogus st_shndx to avoid crashing.  */
    547       && isym->st_shndx < elf_numsections (abfd))
    548     {
    549       iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
    550       shindex = elf_elfheader (abfd)->e_shstrndx;
    551     }
    552 
    553   name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
    554   if (name == NULL)
    555     name = "(null)";
    556   else if (sym_sec && *name == '\0')
    557     name = bfd_section_name (sym_sec);
    558 
    559   return name;
    560 }
    561 
    562 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
    563    sections.  The first element is the flags, the rest are section
    564    pointers.  */
    565 
    566 typedef union elf_internal_group {
    567   Elf_Internal_Shdr *shdr;
    568   unsigned int flags;
    569 } Elf_Internal_Group;
    570 
    571 /* Return the name of the group signature symbol.  Why isn't the
    572    signature just a string?  */
    573 
    574 static const char *
    575 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
    576 {
    577   Elf_Internal_Shdr *hdr;
    578   unsigned char esym[sizeof (Elf64_External_Sym)];
    579   Elf_External_Sym_Shndx eshndx;
    580   Elf_Internal_Sym isym;
    581 
    582   /* First we need to ensure the symbol table is available.  Make sure
    583      that it is a symbol table section.  */
    584   if (ghdr->sh_link >= elf_numsections (abfd))
    585     return NULL;
    586   hdr = elf_elfsections (abfd) [ghdr->sh_link];
    587   if (hdr->sh_type != SHT_SYMTAB
    588       || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
    589     return NULL;
    590 
    591   /* Go read the symbol.  */
    592   hdr = &elf_tdata (abfd)->symtab_hdr;
    593   if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
    594 			    &isym, esym, &eshndx) == NULL)
    595     return NULL;
    596 
    597   return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
    598 }
    599 
    600 /* Set next_in_group list pointer, and group name for NEWSECT.  */
    601 
    602 static bool
    603 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
    604 {
    605   unsigned int num_group = elf_tdata (abfd)->num_group;
    606 
    607   /* If num_group is zero, read in all SHT_GROUP sections.  The count
    608      is set to -1 if there are no SHT_GROUP sections.  */
    609   if (num_group == 0)
    610     {
    611       unsigned int i, shnum;
    612 
    613       /* First count the number of groups.  If we have a SHT_GROUP
    614 	 section with just a flag word (ie. sh_size is 4), ignore it.  */
    615       shnum = elf_numsections (abfd);
    616       num_group = 0;
    617 
    618 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize)	\
    619 	(   (shdr)->sh_type == SHT_GROUP		\
    620 	 && (shdr)->sh_size >= minsize			\
    621 	 && (shdr)->sh_entsize == GRP_ENTRY_SIZE	\
    622 	 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
    623 
    624       for (i = 0; i < shnum; i++)
    625 	{
    626 	  Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
    627 
    628 	  if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
    629 	    num_group += 1;
    630 	}
    631 
    632       if (num_group == 0)
    633 	{
    634 	  num_group = (unsigned) -1;
    635 	  elf_tdata (abfd)->num_group = num_group;
    636 	  elf_tdata (abfd)->group_sect_ptr = NULL;
    637 	}
    638       else
    639 	{
    640 	  /* We keep a list of elf section headers for group sections,
    641 	     so we can find them quickly.  */
    642 	  size_t amt;
    643 
    644 	  elf_tdata (abfd)->num_group = num_group;
    645 	  amt = num_group * sizeof (Elf_Internal_Shdr *);
    646 	  elf_tdata (abfd)->group_sect_ptr
    647 	    = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
    648 	  if (elf_tdata (abfd)->group_sect_ptr == NULL)
    649 	    return false;
    650 	  num_group = 0;
    651 
    652 	  for (i = 0; i < shnum; i++)
    653 	    {
    654 	      Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
    655 
    656 	      if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
    657 		{
    658 		  unsigned char *src;
    659 		  Elf_Internal_Group *dest;
    660 
    661 		  /* Make sure the group section has a BFD section
    662 		     attached to it.  */
    663 		  if (!bfd_section_from_shdr (abfd, i))
    664 		    return false;
    665 
    666 		  /* Add to list of sections.  */
    667 		  elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
    668 		  num_group += 1;
    669 
    670 		  /* Read the raw contents.  */
    671 		  BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
    672 		  shdr->contents = NULL;
    673 		  if (_bfd_mul_overflow (shdr->sh_size,
    674 					 sizeof (*dest) / 4, &amt)
    675 		      || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
    676 		      || !(shdr->contents
    677 			   = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
    678 		    {
    679 		      _bfd_error_handler
    680 			/* xgettext:c-format */
    681 			(_("%pB: invalid size field in group section"
    682 			   " header: %#" PRIx64 ""),
    683 			 abfd, (uint64_t) shdr->sh_size);
    684 		      bfd_set_error (bfd_error_bad_value);
    685 		      -- num_group;
    686 		      continue;
    687 		    }
    688 
    689 		  /* Translate raw contents, a flag word followed by an
    690 		     array of elf section indices all in target byte order,
    691 		     to the flag word followed by an array of elf section
    692 		     pointers.  */
    693 		  src = shdr->contents + shdr->sh_size;
    694 		  dest = (Elf_Internal_Group *) (shdr->contents + amt);
    695 
    696 		  while (1)
    697 		    {
    698 		      unsigned int idx;
    699 
    700 		      src -= 4;
    701 		      --dest;
    702 		      idx = H_GET_32 (abfd, src);
    703 		      if (src == shdr->contents)
    704 			{
    705 			  dest->shdr = NULL;
    706 			  dest->flags = idx;
    707 			  if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
    708 			    shdr->bfd_section->flags
    709 			      |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
    710 			  break;
    711 			}
    712 		      if (idx < shnum)
    713 			{
    714 			  dest->shdr = elf_elfsections (abfd)[idx];
    715 			  /* PR binutils/23199: All sections in a
    716 			     section group should be marked with
    717 			     SHF_GROUP.  But some tools generate
    718 			     broken objects without SHF_GROUP.  Fix
    719 			     them up here.  */
    720 			  dest->shdr->sh_flags |= SHF_GROUP;
    721 			}
    722 		      if (idx >= shnum
    723 			  || dest->shdr->sh_type == SHT_GROUP)
    724 			{
    725 			  _bfd_error_handler
    726 			    (_("%pB: invalid entry in SHT_GROUP section [%u]"),
    727 			       abfd, i);
    728 			  dest->shdr = NULL;
    729 			}
    730 		    }
    731 		}
    732 	    }
    733 
    734 	  /* PR 17510: Corrupt binaries might contain invalid groups.  */
    735 	  if (num_group != (unsigned) elf_tdata (abfd)->num_group)
    736 	    {
    737 	      elf_tdata (abfd)->num_group = num_group;
    738 
    739 	      /* If all groups are invalid then fail.  */
    740 	      if (num_group == 0)
    741 		{
    742 		  elf_tdata (abfd)->group_sect_ptr = NULL;
    743 		  elf_tdata (abfd)->num_group = num_group = -1;
    744 		  _bfd_error_handler
    745 		    (_("%pB: no valid group sections found"), abfd);
    746 		  bfd_set_error (bfd_error_bad_value);
    747 		}
    748 	    }
    749 	}
    750     }
    751 
    752   if (num_group != (unsigned) -1)
    753     {
    754       unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
    755       unsigned int j;
    756 
    757       for (j = 0; j < num_group; j++)
    758 	{
    759 	  /* Begin search from previous found group.  */
    760 	  unsigned i = (j + search_offset) % num_group;
    761 
    762 	  Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
    763 	  Elf_Internal_Group *idx;
    764 	  bfd_size_type n_elt;
    765 
    766 	  if (shdr == NULL)
    767 	    continue;
    768 
    769 	  idx = (Elf_Internal_Group *) shdr->contents;
    770 	  if (idx == NULL || shdr->sh_size < 4)
    771 	    {
    772 	      /* See PR 21957 for a reproducer.  */
    773 	      /* xgettext:c-format */
    774 	      _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
    775 				  abfd, shdr->bfd_section);
    776 	      elf_tdata (abfd)->group_sect_ptr[i] = NULL;
    777 	      bfd_set_error (bfd_error_bad_value);
    778 	      return false;
    779 	    }
    780 	  n_elt = shdr->sh_size / 4;
    781 
    782 	  /* Look through this group's sections to see if current
    783 	     section is a member.  */
    784 	  while (--n_elt != 0)
    785 	    if ((++idx)->shdr == hdr)
    786 	      {
    787 		asection *s = NULL;
    788 
    789 		/* We are a member of this group.  Go looking through
    790 		   other members to see if any others are linked via
    791 		   next_in_group.  */
    792 		idx = (Elf_Internal_Group *) shdr->contents;
    793 		n_elt = shdr->sh_size / 4;
    794 		while (--n_elt != 0)
    795 		  if ((++idx)->shdr != NULL
    796 		      && (s = idx->shdr->bfd_section) != NULL
    797 		      && elf_next_in_group (s) != NULL)
    798 		    break;
    799 		if (n_elt != 0)
    800 		  {
    801 		    /* Snarf the group name from other member, and
    802 		       insert current section in circular list.  */
    803 		    elf_group_name (newsect) = elf_group_name (s);
    804 		    elf_next_in_group (newsect) = elf_next_in_group (s);
    805 		    elf_next_in_group (s) = newsect;
    806 		  }
    807 		else
    808 		  {
    809 		    const char *gname;
    810 
    811 		    gname = group_signature (abfd, shdr);
    812 		    if (gname == NULL)
    813 		      return false;
    814 		    elf_group_name (newsect) = gname;
    815 
    816 		    /* Start a circular list with one element.  */
    817 		    elf_next_in_group (newsect) = newsect;
    818 		  }
    819 
    820 		/* If the group section has been created, point to the
    821 		   new member.  */
    822 		if (shdr->bfd_section != NULL)
    823 		  elf_next_in_group (shdr->bfd_section) = newsect;
    824 
    825 		elf_tdata (abfd)->group_search_offset = i;
    826 		j = num_group - 1;
    827 		break;
    828 	      }
    829 	}
    830     }
    831 
    832   if (elf_group_name (newsect) == NULL)
    833     {
    834       /* xgettext:c-format */
    835       _bfd_error_handler (_("%pB: no group info for section '%pA'"),
    836 			  abfd, newsect);
    837       /* PR 29532: Return true here, even though the group info has not been
    838 	 read.  Separate debug info files can have empty group sections, but
    839 	 we do not want this to prevent them from being loaded as otherwise
    840 	 GDB will not be able to use them.  */
    841       return true;
    842     }
    843   return true;
    844 }
    845 
    846 bool
    847 _bfd_elf_setup_sections (bfd *abfd)
    848 {
    849   unsigned int i;
    850   unsigned int num_group = elf_tdata (abfd)->num_group;
    851   bool result = true;
    852   asection *s;
    853 
    854   /* Process SHF_LINK_ORDER.  */
    855   for (s = abfd->sections; s != NULL; s = s->next)
    856     {
    857       Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
    858       if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
    859 	{
    860 	  unsigned int elfsec = this_hdr->sh_link;
    861 	  /* An sh_link value of 0 is now allowed.  It indicates that linked
    862 	     to section has already been discarded, but that the current
    863 	     section has been retained for some other reason.  This linking
    864 	     section is still a candidate for later garbage collection
    865 	     however.  */
    866 	  if (elfsec == 0)
    867 	    {
    868 	      elf_linked_to_section (s) = NULL;
    869 	    }
    870 	  else
    871 	    {
    872 	      asection *linksec = NULL;
    873 
    874 	      if (elfsec < elf_numsections (abfd))
    875 		{
    876 		  this_hdr = elf_elfsections (abfd)[elfsec];
    877 		  linksec = this_hdr->bfd_section;
    878 		}
    879 
    880 	      /* PR 1991, 2008:
    881 		 Some strip/objcopy may leave an incorrect value in
    882 		 sh_link.  We don't want to proceed.  */
    883 	      if (linksec == NULL)
    884 		{
    885 		  _bfd_error_handler
    886 		    /* xgettext:c-format */
    887 		    (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
    888 		     s->owner, elfsec, s);
    889 		  result = false;
    890 		}
    891 
    892 	      elf_linked_to_section (s) = linksec;
    893 	    }
    894 	}
    895       else if (this_hdr->sh_type == SHT_GROUP
    896 	       && elf_next_in_group (s) == NULL)
    897 	{
    898 	  _bfd_error_handler
    899 	    /* xgettext:c-format */
    900 	    (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
    901 	     abfd, elf_section_data (s)->this_idx);
    902 	  result = false;
    903 	}
    904     }
    905 
    906   /* Process section groups.  */
    907   if (num_group == (unsigned) -1)
    908     return result;
    909 
    910   for (i = 0; i < num_group; i++)
    911     {
    912       Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
    913       Elf_Internal_Group *idx;
    914       unsigned int n_elt;
    915 
    916       /* PR binutils/18758: Beware of corrupt binaries with invalid
    917 	 group data.  */
    918       if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
    919 	{
    920 	  _bfd_error_handler
    921 	    /* xgettext:c-format */
    922 	    (_("%pB: section group entry number %u is corrupt"),
    923 	     abfd, i);
    924 	  result = false;
    925 	  continue;
    926 	}
    927 
    928       idx = (Elf_Internal_Group *) shdr->contents;
    929       n_elt = shdr->sh_size / 4;
    930 
    931       while (--n_elt != 0)
    932 	{
    933 	  ++ idx;
    934 
    935 	  if (idx->shdr == NULL)
    936 	    continue;
    937 	  else if (idx->shdr->bfd_section)
    938 	    elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
    939 	  else if (idx->shdr->sh_type != SHT_RELA
    940 		   && idx->shdr->sh_type != SHT_REL)
    941 	    {
    942 	      /* There are some unknown sections in the group.  */
    943 	      _bfd_error_handler
    944 		/* xgettext:c-format */
    945 		(_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
    946 		 abfd,
    947 		 idx->shdr->sh_type,
    948 		 bfd_elf_string_from_elf_section (abfd,
    949 						  (elf_elfheader (abfd)
    950 						   ->e_shstrndx),
    951 						  idx->shdr->sh_name),
    952 		 shdr->bfd_section);
    953 	      result = false;
    954 	    }
    955 	}
    956     }
    957 
    958   return result;
    959 }
    960 
    961 bool
    962 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
    963 {
    964   return elf_next_in_group (sec) != NULL;
    965 }
    966 
    967 const char *
    968 bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
    969 {
    970   if (elf_sec_group (sec) != NULL)
    971     return elf_group_name (sec);
    972   return NULL;
    973 }
    974 
    975 /* Make a BFD section from an ELF section.  We store a pointer to the
    976    BFD section in the bfd_section field of the header.  */
    977 
    978 bool
    979 _bfd_elf_make_section_from_shdr (bfd *abfd,
    980 				 Elf_Internal_Shdr *hdr,
    981 				 const char *name,
    982 				 int shindex)
    983 {
    984   asection *newsect;
    985   flagword flags;
    986   const struct elf_backend_data *bed;
    987   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
    988 
    989   if (hdr->bfd_section != NULL)
    990     return true;
    991 
    992   newsect = bfd_make_section_anyway (abfd, name);
    993   if (newsect == NULL)
    994     return false;
    995 
    996   hdr->bfd_section = newsect;
    997   elf_section_data (newsect)->this_hdr = *hdr;
    998   elf_section_data (newsect)->this_idx = shindex;
    999 
   1000   /* Always use the real type/flags.  */
   1001   elf_section_type (newsect) = hdr->sh_type;
   1002   elf_section_flags (newsect) = hdr->sh_flags;
   1003 
   1004   newsect->filepos = hdr->sh_offset;
   1005 
   1006   flags = SEC_NO_FLAGS;
   1007   if (hdr->sh_type != SHT_NOBITS)
   1008     flags |= SEC_HAS_CONTENTS;
   1009   if (hdr->sh_type == SHT_GROUP)
   1010     flags |= SEC_GROUP;
   1011   if ((hdr->sh_flags & SHF_ALLOC) != 0)
   1012     {
   1013       flags |= SEC_ALLOC;
   1014       if (hdr->sh_type != SHT_NOBITS)
   1015 	flags |= SEC_LOAD;
   1016     }
   1017   if ((hdr->sh_flags & SHF_WRITE) == 0)
   1018     flags |= SEC_READONLY;
   1019   if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
   1020     flags |= SEC_CODE;
   1021   else if ((flags & SEC_LOAD) != 0)
   1022     flags |= SEC_DATA;
   1023   if ((hdr->sh_flags & SHF_MERGE) != 0)
   1024     {
   1025       flags |= SEC_MERGE;
   1026       newsect->entsize = hdr->sh_entsize;
   1027     }
   1028   if ((hdr->sh_flags & SHF_STRINGS) != 0)
   1029     flags |= SEC_STRINGS;
   1030   if (hdr->sh_flags & SHF_GROUP)
   1031     if (!setup_group (abfd, hdr, newsect))
   1032       return false;
   1033   if ((hdr->sh_flags & SHF_TLS) != 0)
   1034     flags |= SEC_THREAD_LOCAL;
   1035   if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
   1036     flags |= SEC_EXCLUDE;
   1037 
   1038   switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
   1039     {
   1040       /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
   1041 	 but binutils as of 2019-07-23 did not set the EI_OSABI header
   1042 	 byte.  */
   1043     case ELFOSABI_GNU:
   1044     case ELFOSABI_FREEBSD:
   1045       if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
   1046 	elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
   1047       /* Fall through */
   1048     case ELFOSABI_NONE:
   1049       if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
   1050 	elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
   1051       break;
   1052     }
   1053 
   1054   if ((flags & SEC_ALLOC) == 0)
   1055     {
   1056       /* The debugging sections appear to be recognized only by name,
   1057 	 not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
   1058       if (name [0] == '.')
   1059 	{
   1060 	  if (startswith (name, ".debug")
   1061 	      || startswith (name, ".gnu.debuglto_.debug_")
   1062 	      || startswith (name, ".gnu.linkonce.wi.")
   1063 	      || startswith (name, ".zdebug"))
   1064 	    flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
   1065 	  else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
   1066 		   || startswith (name, ".note.gnu"))
   1067 	    {
   1068 	      flags |= SEC_ELF_OCTETS;
   1069 	      opb = 1;
   1070 	    }
   1071 	  else if (startswith (name, ".line")
   1072 		   || startswith (name, ".stab")
   1073 		   || strcmp (name, ".gdb_index") == 0)
   1074 	    flags |= SEC_DEBUGGING;
   1075 	}
   1076     }
   1077 
   1078   if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
   1079       || !bfd_set_section_size (newsect, hdr->sh_size)
   1080       || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
   1081 							& -hdr->sh_addralign)))
   1082     return false;
   1083 
   1084   /* As a GNU extension, if the name begins with .gnu.linkonce, we
   1085      only link a single copy of the section.  This is used to support
   1086      g++.  g++ will emit each template expansion in its own section.
   1087      The symbols will be defined as weak, so that multiple definitions
   1088      are permitted.  The GNU linker extension is to actually discard
   1089      all but one of the sections.  */
   1090   if (startswith (name, ".gnu.linkonce")
   1091       && elf_next_in_group (newsect) == NULL)
   1092     flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
   1093 
   1094   if (!bfd_set_section_flags (newsect, flags))
   1095     return false;
   1096 
   1097   bed = get_elf_backend_data (abfd);
   1098   if (bed->elf_backend_section_flags)
   1099     if (!bed->elf_backend_section_flags (hdr))
   1100       return false;
   1101 
   1102   /* We do not parse the PT_NOTE segments as we are interested even in the
   1103      separate debug info files which may have the segments offsets corrupted.
   1104      PT_NOTEs from the core files are currently not parsed using BFD.  */
   1105   if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
   1106     {
   1107       bfd_byte *contents;
   1108 
   1109       if (!_bfd_elf_mmap_section_contents (abfd, newsect, &contents))
   1110 	return false;
   1111 
   1112       elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
   1113 		       hdr->sh_offset, hdr->sh_addralign);
   1114       _bfd_elf_munmap_section_contents (newsect, contents);
   1115     }
   1116 
   1117   if ((newsect->flags & SEC_ALLOC) != 0)
   1118     {
   1119       Elf_Internal_Phdr *phdr;
   1120       unsigned int i, nload;
   1121 
   1122       /* Some ELF linkers produce binaries with all the program header
   1123 	 p_paddr fields zero.  If we have such a binary with more than
   1124 	 one PT_LOAD header, then leave the section lma equal to vma
   1125 	 so that we don't create sections with overlapping lma.  */
   1126       phdr = elf_tdata (abfd)->phdr;
   1127       for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
   1128 	if (phdr->p_paddr != 0)
   1129 	  break;
   1130 	else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
   1131 	  ++nload;
   1132       if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
   1133 	return true;
   1134 
   1135       phdr = elf_tdata (abfd)->phdr;
   1136       for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
   1137 	{
   1138 	  if (((phdr->p_type == PT_LOAD
   1139 		&& (hdr->sh_flags & SHF_TLS) == 0)
   1140 	       || phdr->p_type == PT_TLS)
   1141 	      && ELF_SECTION_IN_SEGMENT (hdr, phdr))
   1142 	    {
   1143 	      if ((newsect->flags & SEC_LOAD) == 0)
   1144 		newsect->lma = (phdr->p_paddr
   1145 				+ hdr->sh_addr - phdr->p_vaddr) / opb;
   1146 	      else
   1147 		/* We used to use the same adjustment for SEC_LOAD
   1148 		   sections, but that doesn't work if the segment
   1149 		   is packed with code from multiple VMAs.
   1150 		   Instead we calculate the section LMA based on
   1151 		   the segment LMA.  It is assumed that the
   1152 		   segment will contain sections with contiguous
   1153 		   LMAs, even if the VMAs are not.  */
   1154 		newsect->lma = (phdr->p_paddr
   1155 				+ hdr->sh_offset - phdr->p_offset) / opb;
   1156 
   1157 	      /* With contiguous segments, we can't tell from file
   1158 		 offsets whether a section with zero size should
   1159 		 be placed at the end of one segment or the
   1160 		 beginning of the next.  Decide based on vaddr.  */
   1161 	      if (hdr->sh_addr >= phdr->p_vaddr
   1162 		  && (hdr->sh_addr + hdr->sh_size
   1163 		      <= phdr->p_vaddr + phdr->p_memsz))
   1164 		break;
   1165 	    }
   1166 	}
   1167     }
   1168 
   1169   /* Compress/decompress DWARF debug sections with names: .debug_*,
   1170      .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set.  */
   1171   if ((newsect->flags & SEC_DEBUGGING) != 0
   1172       && (newsect->flags & SEC_HAS_CONTENTS) != 0
   1173       && (newsect->flags & SEC_ELF_OCTETS) != 0)
   1174     {
   1175       enum { nothing, compress, decompress } action = nothing;
   1176       int compression_header_size;
   1177       bfd_size_type uncompressed_size;
   1178       unsigned int uncompressed_align_power;
   1179       enum compression_type ch_type = ch_none;
   1180       bool compressed
   1181 	= bfd_is_section_compressed_info (abfd, newsect,
   1182 					  &compression_header_size,
   1183 					  &uncompressed_size,
   1184 					  &uncompressed_align_power,
   1185 					  &ch_type);
   1186 
   1187       /* Should we decompress?  */
   1188       if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
   1189 	action = decompress;
   1190 
   1191       /* Should we compress?  Or convert to a different compression?  */
   1192       else if ((abfd->flags & BFD_COMPRESS) != 0
   1193 	       && newsect->size != 0
   1194 	       && compression_header_size >= 0
   1195 	       && uncompressed_size > 0)
   1196 	{
   1197 	  if (!compressed)
   1198 	    action = compress;
   1199 	  else
   1200 	    {
   1201 	      enum compression_type new_ch_type = ch_none;
   1202 	      if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
   1203 		new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
   1204 			       ? ch_compress_zstd : ch_compress_zlib);
   1205 	      if (new_ch_type != ch_type)
   1206 		action = compress;
   1207 	    }
   1208 	}
   1209 
   1210       if (action == compress)
   1211 	{
   1212 	  if (!bfd_init_section_compress_status (abfd, newsect))
   1213 	    {
   1214 	      _bfd_error_handler
   1215 		/* xgettext:c-format */
   1216 		(_("%pB: unable to compress section %s"), abfd, name);
   1217 	      return false;
   1218 	    }
   1219 	}
   1220       else if (action == decompress)
   1221 	{
   1222 	  if (!bfd_init_section_decompress_status (abfd, newsect))
   1223 	    {
   1224 	      _bfd_error_handler
   1225 		/* xgettext:c-format */
   1226 		(_("%pB: unable to decompress section %s"), abfd, name);
   1227 	      return false;
   1228 	    }
   1229 #ifndef HAVE_ZSTD
   1230 	  if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
   1231 	    {
   1232 	      _bfd_error_handler
   1233 		  /* xgettext:c-format */
   1234 		  (_ ("%pB: section %s is compressed with zstd, but BFD "
   1235 		      "is not built with zstd support"),
   1236 		   abfd, name);
   1237 	      newsect->compress_status = COMPRESS_SECTION_NONE;
   1238 	      return false;
   1239 	    }
   1240 #endif
   1241 	  if (abfd->is_linker_input
   1242 	      && name[1] == 'z')
   1243 	    {
   1244 	      /* Rename section from .zdebug_* to .debug_* so that ld
   1245 		 scripts will see this section as a debug section.  */
   1246 	      char *new_name = bfd_zdebug_name_to_debug (abfd, name);
   1247 	      if (new_name == NULL)
   1248 		return false;
   1249 	      bfd_rename_section (newsect, new_name);
   1250 	    }
   1251 	}
   1252     }
   1253 
   1254   return true;
   1255 }
   1256 
   1257 const char *const bfd_elf_section_type_names[] =
   1258 {
   1259   "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
   1260   "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
   1261   "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
   1262 };
   1263 
   1264 /* ELF relocs are against symbols.  If we are producing relocatable
   1265    output, and the reloc is against an external symbol, and nothing
   1266    has given us any additional addend, the resulting reloc will also
   1267    be against the same symbol.  In such a case, we don't want to
   1268    change anything about the way the reloc is handled, since it will
   1269    all be done at final link time.  Rather than put special case code
   1270    into bfd_perform_relocation, all the reloc types use this howto
   1271    function, or should call this function for relocatable output.  */
   1272 
   1273 bfd_reloc_status_type
   1274 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
   1275 		       arelent *reloc_entry,
   1276 		       asymbol *symbol,
   1277 		       void *data ATTRIBUTE_UNUSED,
   1278 		       asection *input_section,
   1279 		       bfd *output_bfd,
   1280 		       char **error_message ATTRIBUTE_UNUSED)
   1281 {
   1282   if (output_bfd != NULL
   1283       && (symbol->flags & BSF_SECTION_SYM) == 0
   1284       && (! reloc_entry->howto->partial_inplace
   1285 	  || reloc_entry->addend == 0))
   1286     {
   1287       reloc_entry->address += input_section->output_offset;
   1288       return bfd_reloc_ok;
   1289     }
   1290 
   1291   /* In some cases the relocation should be treated as output section
   1292      relative, as when linking ELF DWARF into PE COFF.  Many ELF
   1293      targets lack section relative relocations and instead use
   1294      ordinary absolute relocations for references between DWARF
   1295      sections.  That is arguably a bug in those targets but it happens
   1296      to work for the usual case of linking to non-loaded ELF debug
   1297      sections with VMAs forced to zero.  PE COFF on the other hand
   1298      doesn't allow a section VMA of zero.  */
   1299   if (output_bfd == NULL
   1300       && !reloc_entry->howto->pc_relative
   1301       && (symbol->section->flags & SEC_DEBUGGING) != 0
   1302       && (input_section->flags & SEC_DEBUGGING) != 0)
   1303     reloc_entry->addend -= symbol->section->output_section->vma;
   1304 
   1305   return bfd_reloc_continue;
   1306 }
   1307 
   1308 /* Returns TRUE if section A matches section B.
   1310    Names, addresses and links may be different, but everything else
   1311    should be the same.  */
   1312 
   1313 static bool
   1314 section_match (const Elf_Internal_Shdr * a,
   1315 	       const Elf_Internal_Shdr * b)
   1316 {
   1317   if (a->sh_type != b->sh_type
   1318       || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
   1319       || a->sh_addralign != b->sh_addralign
   1320       || a->sh_entsize != b->sh_entsize)
   1321     return false;
   1322   if (a->sh_type == SHT_SYMTAB
   1323       || a->sh_type == SHT_STRTAB)
   1324     return true;
   1325   return a->sh_size == b->sh_size;
   1326 }
   1327 
   1328 /* Find a section in OBFD that has the same characteristics
   1329    as IHEADER.  Return the index of this section or SHN_UNDEF if
   1330    none can be found.  Check's section HINT first, as this is likely
   1331    to be the correct section.  */
   1332 
   1333 static unsigned int
   1334 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
   1335 	   const unsigned int hint)
   1336 {
   1337   Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
   1338   unsigned int i;
   1339 
   1340   BFD_ASSERT (iheader != NULL);
   1341 
   1342   /* See PR 20922 for a reproducer of the NULL test.  */
   1343   if (hint < elf_numsections (obfd)
   1344       && oheaders[hint] != NULL
   1345       && section_match (oheaders[hint], iheader))
   1346     return hint;
   1347 
   1348   for (i = 1; i < elf_numsections (obfd); i++)
   1349     {
   1350       Elf_Internal_Shdr * oheader = oheaders[i];
   1351 
   1352       if (oheader == NULL)
   1353 	continue;
   1354       if (section_match (oheader, iheader))
   1355 	/* FIXME: Do we care if there is a potential for
   1356 	   multiple matches ?  */
   1357 	return i;
   1358     }
   1359 
   1360   return SHN_UNDEF;
   1361 }
   1362 
   1363 /* PR 19938: Attempt to set the ELF section header fields of an OS or
   1364    Processor specific section, based upon a matching input section.
   1365    Returns TRUE upon success, FALSE otherwise.  */
   1366 
   1367 static bool
   1368 copy_special_section_fields (const bfd *ibfd,
   1369 			     bfd *obfd,
   1370 			     const Elf_Internal_Shdr *iheader,
   1371 			     Elf_Internal_Shdr *oheader,
   1372 			     const unsigned int secnum)
   1373 {
   1374   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
   1375   const Elf_Internal_Shdr **iheaders
   1376     = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
   1377   bool changed = false;
   1378   unsigned int sh_link;
   1379 
   1380   if (oheader->sh_type == SHT_NOBITS)
   1381     {
   1382       /* This is a feature for objcopy --only-keep-debug:
   1383 	 When a section's type is changed to NOBITS, we preserve
   1384 	 the sh_link and sh_info fields so that they can be
   1385 	 matched up with the original.
   1386 
   1387 	 Note: Strictly speaking these assignments are wrong.
   1388 	 The sh_link and sh_info fields should point to the
   1389 	 relevent sections in the output BFD, which may not be in
   1390 	 the same location as they were in the input BFD.  But
   1391 	 the whole point of this action is to preserve the
   1392 	 original values of the sh_link and sh_info fields, so
   1393 	 that they can be matched up with the section headers in
   1394 	 the original file.  So strictly speaking we may be
   1395 	 creating an invalid ELF file, but it is only for a file
   1396 	 that just contains debug info and only for sections
   1397 	 without any contents.  */
   1398       if (oheader->sh_link == 0)
   1399 	oheader->sh_link = iheader->sh_link;
   1400       if (oheader->sh_info == 0)
   1401 	oheader->sh_info = iheader->sh_info;
   1402       return true;
   1403     }
   1404 
   1405   /* Allow the target a chance to decide how these fields should be set.  */
   1406   if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
   1407 						    iheader, oheader))
   1408     return true;
   1409 
   1410   /* We have an iheader which might match oheader, and which has non-zero
   1411      sh_info and/or sh_link fields.  Attempt to follow those links and find
   1412      the section in the output bfd which corresponds to the linked section
   1413      in the input bfd.  */
   1414   if (iheader->sh_link != SHN_UNDEF)
   1415     {
   1416       /* See PR 20931 for a reproducer.  */
   1417       if (iheader->sh_link >= elf_numsections (ibfd))
   1418 	{
   1419 	  _bfd_error_handler
   1420 	    /* xgettext:c-format */
   1421 	    (_("%pB: invalid sh_link field (%d) in section number %d"),
   1422 	     ibfd, iheader->sh_link, secnum);
   1423 	  return false;
   1424 	}
   1425 
   1426       sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
   1427       if (sh_link != SHN_UNDEF)
   1428 	{
   1429 	  oheader->sh_link = sh_link;
   1430 	  changed = true;
   1431 	}
   1432       else
   1433 	/* FIXME: Should we install iheader->sh_link
   1434 	   if we could not find a match ?  */
   1435 	_bfd_error_handler
   1436 	  /* xgettext:c-format */
   1437 	  (_("%pB: failed to find link section for section %d"), obfd, secnum);
   1438     }
   1439 
   1440   if (iheader->sh_info)
   1441     {
   1442       /* The sh_info field can hold arbitrary information, but if the
   1443 	 SHF_LINK_INFO flag is set then it should be interpreted as a
   1444 	 section index.  */
   1445       if (iheader->sh_flags & SHF_INFO_LINK)
   1446 	{
   1447 	  sh_link = find_link (obfd, iheaders[iheader->sh_info],
   1448 			       iheader->sh_info);
   1449 	  if (sh_link != SHN_UNDEF)
   1450 	    oheader->sh_flags |= SHF_INFO_LINK;
   1451 	}
   1452       else
   1453 	/* No idea what it means - just copy it.  */
   1454 	sh_link = iheader->sh_info;
   1455 
   1456       if (sh_link != SHN_UNDEF)
   1457 	{
   1458 	  oheader->sh_info = sh_link;
   1459 	  changed = true;
   1460 	}
   1461       else
   1462 	_bfd_error_handler
   1463 	  /* xgettext:c-format */
   1464 	  (_("%pB: failed to find info section for section %d"), obfd, secnum);
   1465     }
   1466 
   1467   return changed;
   1468 }
   1469 
   1470 /* Copy the program header and other data from one object module to
   1471    another.  */
   1472 
   1473 bool
   1474 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   1475 {
   1476   const Elf_Internal_Shdr **iheaders
   1477     = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
   1478   Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
   1479   const struct elf_backend_data *bed;
   1480   unsigned int i;
   1481 
   1482   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   1483     || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   1484     return true;
   1485 
   1486   if (!elf_flags_init (obfd))
   1487     {
   1488       elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
   1489       elf_flags_init (obfd) = true;
   1490     }
   1491 
   1492   elf_gp (obfd) = elf_gp (ibfd);
   1493 
   1494   /* Also copy the EI_OSABI field.  */
   1495   elf_elfheader (obfd)->e_ident[EI_OSABI] =
   1496     elf_elfheader (ibfd)->e_ident[EI_OSABI];
   1497 
   1498   /* If set, copy the EI_ABIVERSION field.  */
   1499   if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
   1500     elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
   1501       = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
   1502 
   1503   /* Copy object attributes.  */
   1504   _bfd_elf_copy_obj_attributes (ibfd, obfd);
   1505 
   1506   if (iheaders == NULL || oheaders == NULL)
   1507     return true;
   1508 
   1509   bed = get_elf_backend_data (obfd);
   1510 
   1511   /* Possibly copy other fields in the section header.  */
   1512   for (i = 1; i < elf_numsections (obfd); i++)
   1513     {
   1514       unsigned int j;
   1515       Elf_Internal_Shdr * oheader = oheaders[i];
   1516 
   1517       /* Ignore ordinary sections.  SHT_NOBITS sections are considered however
   1518 	 because of a special case need for generating separate debug info
   1519 	 files.  See below for more details.  */
   1520       if (oheader == NULL
   1521 	  || (oheader->sh_type != SHT_NOBITS
   1522 	      && oheader->sh_type < SHT_LOOS))
   1523 	continue;
   1524 
   1525       /* Ignore empty sections, and sections whose
   1526 	 fields have already been initialised.  */
   1527       if (oheader->sh_size == 0
   1528 	  || (oheader->sh_info != 0 && oheader->sh_link != 0))
   1529 	continue;
   1530 
   1531       /* Scan for the matching section in the input bfd.
   1532 	 First we try for a direct mapping between the input and
   1533 	 output sections.  */
   1534       for (j = 1; j < elf_numsections (ibfd); j++)
   1535 	{
   1536 	  const Elf_Internal_Shdr * iheader = iheaders[j];
   1537 
   1538 	  if (iheader == NULL)
   1539 	    continue;
   1540 
   1541 	  if (oheader->bfd_section != NULL
   1542 	      && iheader->bfd_section != NULL
   1543 	      && iheader->bfd_section->output_section != NULL
   1544 	      && iheader->bfd_section->output_section == oheader->bfd_section)
   1545 	    {
   1546 	      /* We have found a connection from the input section to
   1547 		 the output section.  Attempt to copy the header fields.
   1548 		 If this fails then do not try any further sections -
   1549 		 there should only be a one-to-one mapping between
   1550 		 input and output.  */
   1551 	      if (!copy_special_section_fields (ibfd, obfd,
   1552 						iheader, oheader, i))
   1553 		j = elf_numsections (ibfd);
   1554 	      break;
   1555 	    }
   1556 	}
   1557 
   1558       if (j < elf_numsections (ibfd))
   1559 	continue;
   1560 
   1561       /* That failed.  So try to deduce the corresponding input section.
   1562 	 Unfortunately we cannot compare names as the output string table
   1563 	 is empty, so instead we check size, address and type.  */
   1564       for (j = 1; j < elf_numsections (ibfd); j++)
   1565 	{
   1566 	  const Elf_Internal_Shdr * iheader = iheaders[j];
   1567 
   1568 	  if (iheader == NULL)
   1569 	    continue;
   1570 
   1571 	  /* Try matching fields in the input section's header.
   1572 	     Since --only-keep-debug turns all non-debug sections into
   1573 	     SHT_NOBITS sections, the output SHT_NOBITS type matches any
   1574 	     input type.  */
   1575 	  if ((oheader->sh_type == SHT_NOBITS
   1576 	       || iheader->sh_type == oheader->sh_type)
   1577 	      && (iheader->sh_flags & ~ SHF_INFO_LINK)
   1578 	      == (oheader->sh_flags & ~ SHF_INFO_LINK)
   1579 	      && iheader->sh_addralign == oheader->sh_addralign
   1580 	      && iheader->sh_entsize == oheader->sh_entsize
   1581 	      && iheader->sh_size == oheader->sh_size
   1582 	      && iheader->sh_addr == oheader->sh_addr
   1583 	      && (iheader->sh_info != oheader->sh_info
   1584 		  || iheader->sh_link != oheader->sh_link))
   1585 	    {
   1586 	      if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
   1587 		break;
   1588 	    }
   1589 	}
   1590 
   1591       if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
   1592 	{
   1593 	  /* Final attempt.  Call the backend copy function
   1594 	     with a NULL input section.  */
   1595 	  (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
   1596 							       NULL, oheader);
   1597 	}
   1598     }
   1599 
   1600   return true;
   1601 }
   1602 
   1603 static const char *
   1604 get_segment_type (unsigned int p_type)
   1605 {
   1606   const char *pt;
   1607   switch (p_type)
   1608     {
   1609     case PT_NULL: pt = "NULL"; break;
   1610     case PT_LOAD: pt = "LOAD"; break;
   1611     case PT_DYNAMIC: pt = "DYNAMIC"; break;
   1612     case PT_INTERP: pt = "INTERP"; break;
   1613     case PT_NOTE: pt = "NOTE"; break;
   1614     case PT_SHLIB: pt = "SHLIB"; break;
   1615     case PT_PHDR: pt = "PHDR"; break;
   1616     case PT_TLS: pt = "TLS"; break;
   1617     case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
   1618     case PT_GNU_STACK: pt = "STACK"; break;
   1619     case PT_GNU_RELRO: pt = "RELRO"; break;
   1620     case PT_GNU_SFRAME: pt = "SFRAME"; break;
   1621     default: pt = NULL; break;
   1622     }
   1623   return pt;
   1624 }
   1625 
   1626 /* Print out the program headers.  */
   1627 
   1628 bool
   1629 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
   1630 {
   1631   FILE *f = (FILE *) farg;
   1632   Elf_Internal_Phdr *p;
   1633   asection *s;
   1634   bfd_byte *dynbuf = NULL;
   1635 
   1636   p = elf_tdata (abfd)->phdr;
   1637   if (p != NULL)
   1638     {
   1639       unsigned int i, c;
   1640 
   1641       fprintf (f, _("\nProgram Header:\n"));
   1642       c = elf_elfheader (abfd)->e_phnum;
   1643       for (i = 0; i < c; i++, p++)
   1644 	{
   1645 	  const char *pt = get_segment_type (p->p_type);
   1646 	  char buf[20];
   1647 
   1648 	  if (pt == NULL)
   1649 	    {
   1650 	      sprintf (buf, "0x%lx", p->p_type);
   1651 	      pt = buf;
   1652 	    }
   1653 	  fprintf (f, "%8s off    0x", pt);
   1654 	  bfd_fprintf_vma (abfd, f, p->p_offset);
   1655 	  fprintf (f, " vaddr 0x");
   1656 	  bfd_fprintf_vma (abfd, f, p->p_vaddr);
   1657 	  fprintf (f, " paddr 0x");
   1658 	  bfd_fprintf_vma (abfd, f, p->p_paddr);
   1659 	  fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
   1660 	  fprintf (f, "         filesz 0x");
   1661 	  bfd_fprintf_vma (abfd, f, p->p_filesz);
   1662 	  fprintf (f, " memsz 0x");
   1663 	  bfd_fprintf_vma (abfd, f, p->p_memsz);
   1664 	  fprintf (f, " flags %c%c%c",
   1665 		   (p->p_flags & PF_R) != 0 ? 'r' : '-',
   1666 		   (p->p_flags & PF_W) != 0 ? 'w' : '-',
   1667 		   (p->p_flags & PF_X) != 0 ? 'x' : '-');
   1668 	  if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
   1669 	    fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
   1670 	  fprintf (f, "\n");
   1671 	}
   1672     }
   1673 
   1674   s = bfd_get_section_by_name (abfd, ".dynamic");
   1675   if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
   1676     {
   1677       unsigned int elfsec;
   1678       unsigned long shlink;
   1679       bfd_byte *extdyn, *extdynend;
   1680       size_t extdynsize;
   1681       void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   1682 
   1683       fprintf (f, _("\nDynamic Section:\n"));
   1684 
   1685       if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
   1686 	goto error_return;
   1687 
   1688       elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   1689       if (elfsec == SHN_BAD)
   1690 	goto error_return;
   1691       shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   1692 
   1693       extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   1694       swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   1695 
   1696       for (extdyn = dynbuf, extdynend = dynbuf + s->size;
   1697 	   (size_t) (extdynend - extdyn) >= extdynsize;
   1698 	   extdyn += extdynsize)
   1699 	{
   1700 	  Elf_Internal_Dyn dyn;
   1701 	  const char *name = "";
   1702 	  char ab[20];
   1703 	  bool stringp;
   1704 	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   1705 
   1706 	  (*swap_dyn_in) (abfd, extdyn, &dyn);
   1707 
   1708 	  if (dyn.d_tag == DT_NULL)
   1709 	    break;
   1710 
   1711 	  stringp = false;
   1712 	  switch (dyn.d_tag)
   1713 	    {
   1714 	    default:
   1715 	      if (bed->elf_backend_get_target_dtag)
   1716 		name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
   1717 
   1718 	      if (!strcmp (name, ""))
   1719 		{
   1720 		  sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
   1721 		  name = ab;
   1722 		}
   1723 	      break;
   1724 
   1725 	    case DT_NEEDED: name = "NEEDED"; stringp = true; break;
   1726 	    case DT_PLTRELSZ: name = "PLTRELSZ"; break;
   1727 	    case DT_PLTGOT: name = "PLTGOT"; break;
   1728 	    case DT_HASH: name = "HASH"; break;
   1729 	    case DT_STRTAB: name = "STRTAB"; break;
   1730 	    case DT_SYMTAB: name = "SYMTAB"; break;
   1731 	    case DT_RELA: name = "RELA"; break;
   1732 	    case DT_RELASZ: name = "RELASZ"; break;
   1733 	    case DT_RELAENT: name = "RELAENT"; break;
   1734 	    case DT_STRSZ: name = "STRSZ"; break;
   1735 	    case DT_SYMENT: name = "SYMENT"; break;
   1736 	    case DT_INIT: name = "INIT"; break;
   1737 	    case DT_FINI: name = "FINI"; break;
   1738 	    case DT_SONAME: name = "SONAME"; stringp = true; break;
   1739 	    case DT_RPATH: name = "RPATH"; stringp = true; break;
   1740 	    case DT_SYMBOLIC: name = "SYMBOLIC"; break;
   1741 	    case DT_REL: name = "REL"; break;
   1742 	    case DT_RELSZ: name = "RELSZ"; break;
   1743 	    case DT_RELENT: name = "RELENT"; break;
   1744 	    case DT_RELR: name = "RELR"; break;
   1745 	    case DT_RELRSZ: name = "RELRSZ"; break;
   1746 	    case DT_RELRENT: name = "RELRENT"; break;
   1747 	    case DT_PLTREL: name = "PLTREL"; break;
   1748 	    case DT_DEBUG: name = "DEBUG"; break;
   1749 	    case DT_TEXTREL: name = "TEXTREL"; break;
   1750 	    case DT_JMPREL: name = "JMPREL"; break;
   1751 	    case DT_BIND_NOW: name = "BIND_NOW"; break;
   1752 	    case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
   1753 	    case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
   1754 	    case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
   1755 	    case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
   1756 	    case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
   1757 	    case DT_FLAGS: name = "FLAGS"; break;
   1758 	    case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
   1759 	    case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
   1760 	    case DT_CHECKSUM: name = "CHECKSUM"; break;
   1761 	    case DT_PLTPADSZ: name = "PLTPADSZ"; break;
   1762 	    case DT_MOVEENT: name = "MOVEENT"; break;
   1763 	    case DT_MOVESZ: name = "MOVESZ"; break;
   1764 	    case DT_FEATURE: name = "FEATURE"; break;
   1765 	    case DT_POSFLAG_1: name = "POSFLAG_1"; break;
   1766 	    case DT_SYMINSZ: name = "SYMINSZ"; break;
   1767 	    case DT_SYMINENT: name = "SYMINENT"; break;
   1768 	    case DT_CONFIG: name = "CONFIG"; stringp = true; break;
   1769 	    case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
   1770 	    case DT_AUDIT: name = "AUDIT"; stringp = true; break;
   1771 	    case DT_PLTPAD: name = "PLTPAD"; break;
   1772 	    case DT_MOVETAB: name = "MOVETAB"; break;
   1773 	    case DT_SYMINFO: name = "SYMINFO"; break;
   1774 	    case DT_RELACOUNT: name = "RELACOUNT"; break;
   1775 	    case DT_RELCOUNT: name = "RELCOUNT"; break;
   1776 	    case DT_FLAGS_1: name = "FLAGS_1"; break;
   1777 	    case DT_VERSYM: name = "VERSYM"; break;
   1778 	    case DT_VERDEF: name = "VERDEF"; break;
   1779 	    case DT_VERDEFNUM: name = "VERDEFNUM"; break;
   1780 	    case DT_VERNEED: name = "VERNEED"; break;
   1781 	    case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
   1782 	    case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
   1783 	    case DT_USED: name = "USED"; break;
   1784 	    case DT_FILTER: name = "FILTER"; stringp = true; break;
   1785 	    case DT_GNU_HASH: name = "GNU_HASH"; break;
   1786 	    }
   1787 
   1788 	  fprintf (f, "  %-20s ", name);
   1789 	  if (! stringp)
   1790 	    {
   1791 	      fprintf (f, "0x");
   1792 	      bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
   1793 	    }
   1794 	  else
   1795 	    {
   1796 	      const char *string;
   1797 	      unsigned int tagv = dyn.d_un.d_val;
   1798 
   1799 	      string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   1800 	      if (string == NULL)
   1801 		goto error_return;
   1802 	      fprintf (f, "%s", string);
   1803 	    }
   1804 	  fprintf (f, "\n");
   1805 	}
   1806 
   1807       _bfd_elf_munmap_section_contents (s, dynbuf);
   1808       dynbuf = NULL;
   1809     }
   1810 
   1811   if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
   1812       || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
   1813     {
   1814       if (! _bfd_elf_slurp_version_tables (abfd, false))
   1815 	return false;
   1816     }
   1817 
   1818   if (elf_dynverdef (abfd) != 0)
   1819     {
   1820       Elf_Internal_Verdef *t;
   1821 
   1822       fprintf (f, _("\nVersion definitions:\n"));
   1823       for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
   1824 	{
   1825 	  fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
   1826 		   t->vd_flags, t->vd_hash,
   1827 		   t->vd_nodename ? t->vd_nodename : "<corrupt>");
   1828 	  if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
   1829 	    {
   1830 	      Elf_Internal_Verdaux *a;
   1831 
   1832 	      fprintf (f, "\t");
   1833 	      for (a = t->vd_auxptr->vda_nextptr;
   1834 		   a != NULL;
   1835 		   a = a->vda_nextptr)
   1836 		fprintf (f, "%s ",
   1837 			 a->vda_nodename ? a->vda_nodename : "<corrupt>");
   1838 	      fprintf (f, "\n");
   1839 	    }
   1840 	}
   1841     }
   1842 
   1843   if (elf_dynverref (abfd) != 0)
   1844     {
   1845       Elf_Internal_Verneed *t;
   1846 
   1847       fprintf (f, _("\nVersion References:\n"));
   1848       for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
   1849 	{
   1850 	  Elf_Internal_Vernaux *a;
   1851 
   1852 	  fprintf (f, _("  required from %s:\n"),
   1853 		   t->vn_filename ? t->vn_filename : "<corrupt>");
   1854 	  for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   1855 	    fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
   1856 		     a->vna_flags, a->vna_other,
   1857 		     a->vna_nodename ? a->vna_nodename : "<corrupt>");
   1858 	}
   1859     }
   1860 
   1861   return true;
   1862 
   1863  error_return:
   1864   _bfd_elf_munmap_section_contents (s, dynbuf);
   1865   return false;
   1866 }
   1867 
   1868 /* Find the file offset corresponding to VMA by using the program
   1869    headers.  */
   1870 
   1871 static file_ptr
   1872 offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma,
   1873 		 size_t size, size_t *max_size_p)
   1874 {
   1875   Elf_Internal_Phdr *seg;
   1876   size_t i;
   1877 
   1878   for (seg = phdrs, i = 0; i < phnum; ++seg, ++i)
   1879     if (seg->p_type == PT_LOAD
   1880 	&& vma >= (seg->p_vaddr & -seg->p_align)
   1881 	&& vma + size <= seg->p_vaddr + seg->p_filesz)
   1882       {
   1883 	if (max_size_p)
   1884 	  *max_size_p = seg->p_vaddr + seg->p_filesz - vma;
   1885 	return vma - seg->p_vaddr + seg->p_offset;
   1886       }
   1887 
   1888   if (max_size_p)
   1889     *max_size_p = 0;
   1890   bfd_set_error (bfd_error_invalid_operation);
   1891   return (file_ptr) -1;
   1892 }
   1893 
   1894 /* Convert hash table to internal form.  */
   1895 
   1896 static bfd_vma *
   1897 get_hash_table_data (bfd *abfd, bfd_size_type number,
   1898 		     unsigned int ent_size, bfd_size_type filesize)
   1899 {
   1900   unsigned char *e_data = NULL;
   1901   bfd_vma *i_data = NULL;
   1902   bfd_size_type size;
   1903   void *e_data_addr;
   1904   size_t e_data_size ATTRIBUTE_UNUSED;
   1905 
   1906   if (ent_size != 4 && ent_size != 8)
   1907     return NULL;
   1908 
   1909   if ((size_t) number != number)
   1910     {
   1911       bfd_set_error (bfd_error_file_too_big);
   1912       return NULL;
   1913     }
   1914 
   1915   size = ent_size * number;
   1916   /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
   1917      attempting to allocate memory when the read is bound to fail.  */
   1918   if (size > filesize
   1919       || number >= ~(size_t) 0 / ent_size
   1920       || number >= ~(size_t) 0 / sizeof (*i_data))
   1921     {
   1922       bfd_set_error (bfd_error_file_too_big);
   1923       return NULL;
   1924     }
   1925 
   1926   e_data = _bfd_mmap_readonly_temporary (abfd, size, &e_data_addr,
   1927 					 &e_data_size);
   1928   if (e_data == NULL)
   1929     return NULL;
   1930 
   1931   i_data = (bfd_vma *) bfd_malloc (number * sizeof (*i_data));
   1932   if (i_data == NULL)
   1933     {
   1934       free (e_data);
   1935       return NULL;
   1936     }
   1937 
   1938   if (ent_size == 4)
   1939     while (number--)
   1940       i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size);
   1941   else
   1942     while (number--)
   1943       i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size);
   1944 
   1945   _bfd_munmap_readonly_temporary (e_data_addr, e_data_size);
   1946   return i_data;
   1947 }
   1948 
   1949 /* Address of .MIPS.xhash section.  FIXME: What is the best way to
   1950    support DT_MIPS_XHASH?  */
   1951 #define DT_MIPS_XHASH	       0x70000036
   1952 
   1953 /* Reconstruct dynamic symbol table from PT_DYNAMIC segment.  */
   1954 
   1955 bool
   1956 _bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr,
   1957 			      Elf_Internal_Phdr *phdrs, size_t phnum,
   1958 			      bfd_size_type filesize)
   1959 {
   1960   bfd_byte *extdyn, *extdynend;
   1961   size_t extdynsize;
   1962   void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   1963   bool (*swap_symbol_in) (bfd *, const void *, const void *,
   1964 			  Elf_Internal_Sym *);
   1965   Elf_Internal_Dyn dyn;
   1966   bfd_vma dt_hash = 0;
   1967   bfd_vma dt_gnu_hash = 0;
   1968   bfd_vma dt_mips_xhash = 0;
   1969   bfd_vma dt_strtab = 0;
   1970   bfd_vma dt_symtab = 0;
   1971   size_t dt_strsz = 0;
   1972   bfd_vma dt_versym = 0;
   1973   bfd_vma dt_verdef = 0;
   1974   bfd_vma dt_verneed = 0;
   1975   bfd_byte *dynbuf = NULL;
   1976   char *strbuf = NULL;
   1977   bfd_vma *gnubuckets = NULL;
   1978   bfd_vma *gnuchains = NULL;
   1979   bfd_vma *mipsxlat = NULL;
   1980   file_ptr saved_filepos, filepos;
   1981   bool res = false;
   1982   size_t amt;
   1983   bfd_byte *esymbuf = NULL, *esym;
   1984   bfd_size_type symcount;
   1985   Elf_Internal_Sym *isymbuf = NULL;
   1986   Elf_Internal_Sym *isym, *isymend;
   1987   bfd_byte *versym = NULL;
   1988   bfd_byte *verdef = NULL;
   1989   bfd_byte *verneed = NULL;
   1990   size_t verdef_size = 0;
   1991   size_t verneed_size = 0;
   1992   size_t extsym_size;
   1993   const struct elf_backend_data *bed;
   1994   void *dynbuf_addr = NULL;
   1995   void *esymbuf_addr = NULL;
   1996   size_t dynbuf_size = 0;
   1997   size_t esymbuf_size = 0;
   1998 
   1999   /* Return TRUE if symbol table is bad.  */
   2000   if (elf_bad_symtab (abfd))
   2001     return true;
   2002 
   2003   /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before.  */
   2004   if (elf_tdata (abfd)->dt_strtab != NULL)
   2005     return true;
   2006 
   2007   bed = get_elf_backend_data (abfd);
   2008 
   2009   /* Save file position for elf_object_p.  */
   2010   saved_filepos = bfd_tell (abfd);
   2011 
   2012   if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0)
   2013     goto error_return;
   2014 
   2015   dynbuf_size = phdr->p_filesz;
   2016   dynbuf = _bfd_mmap_readonly_temporary (abfd, dynbuf_size,
   2017 					 &dynbuf_addr, &dynbuf_size);
   2018   if (dynbuf == NULL)
   2019     goto error_return;
   2020 
   2021   extsym_size = bed->s->sizeof_sym;
   2022   extdynsize = bed->s->sizeof_dyn;
   2023   swap_dyn_in = bed->s->swap_dyn_in;
   2024 
   2025   extdyn = dynbuf;
   2026   if (phdr->p_filesz < extdynsize)
   2027     goto error_return;
   2028   extdynend = extdyn + phdr->p_filesz;
   2029   for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
   2030     {
   2031       swap_dyn_in (abfd, extdyn, &dyn);
   2032 
   2033       if (dyn.d_tag == DT_NULL)
   2034 	break;
   2035 
   2036       switch (dyn.d_tag)
   2037 	{
   2038 	case DT_HASH:
   2039 	  dt_hash = dyn.d_un.d_val;
   2040 	  break;
   2041 	case DT_GNU_HASH:
   2042 	  if (bed->elf_machine_code != EM_MIPS
   2043 	      && bed->elf_machine_code != EM_MIPS_RS3_LE)
   2044 	    dt_gnu_hash = dyn.d_un.d_val;
   2045 	  break;
   2046 	case DT_STRTAB:
   2047 	  dt_strtab = dyn.d_un.d_val;
   2048 	  break;
   2049 	case DT_SYMTAB:
   2050 	  dt_symtab = dyn.d_un.d_val;
   2051 	  break;
   2052 	case DT_STRSZ:
   2053 	  dt_strsz = dyn.d_un.d_val;
   2054 	  break;
   2055 	case DT_SYMENT:
   2056 	  if (dyn.d_un.d_val != extsym_size)
   2057 	    goto error_return;
   2058 	  break;
   2059 	case DT_VERSYM:
   2060 	  dt_versym = dyn.d_un.d_val;
   2061 	  break;
   2062 	case DT_VERDEF:
   2063 	  dt_verdef = dyn.d_un.d_val;
   2064 	  break;
   2065 	case DT_VERNEED:
   2066 	  dt_verneed = dyn.d_un.d_val;
   2067 	  break;
   2068 	default:
   2069 	  if (dyn.d_tag == DT_MIPS_XHASH
   2070 	      && (bed->elf_machine_code == EM_MIPS
   2071 		  || bed->elf_machine_code == EM_MIPS_RS3_LE))
   2072 	    {
   2073 	      dt_gnu_hash = dyn.d_un.d_val;
   2074 	      dt_mips_xhash = dyn.d_un.d_val;
   2075 	    }
   2076 	  break;
   2077 	}
   2078     }
   2079 
   2080   /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
   2081      segment.  */
   2082   if ((!dt_hash && !dt_gnu_hash)
   2083       || !dt_strtab
   2084       || !dt_symtab
   2085       || !dt_strsz)
   2086     goto error_return;
   2087 
   2088   /* Get dynamic string table.  */
   2089   filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL);
   2090   if (filepos == (file_ptr) -1
   2091       || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2092     goto error_return;
   2093 
   2094   /* Dynamic string table must be valid until ABFD is closed.  */
   2095   strbuf = (char *) _bfd_mmap_readonly_persistent (abfd, dt_strsz);
   2096   if (strbuf == NULL)
   2097     goto error_return;
   2098   if (strbuf[dt_strsz - 1] != 0)
   2099     {
   2100       /* It is an error if a string table is't terminated.  */
   2101       _bfd_error_handler
   2102 	/* xgettext:c-format */
   2103 	(_("%pB: DT_STRTAB table is corrupt"), abfd);
   2104       goto error_return;
   2105     }
   2106 
   2107   /* Get the real symbol count from DT_HASH or DT_GNU_HASH.  Prefer
   2108      DT_HASH since it is simpler than DT_GNU_HASH.  */
   2109   if (dt_hash)
   2110     {
   2111       unsigned char nb[16];
   2112       unsigned int hash_ent_size;
   2113 
   2114       switch (bed->elf_machine_code)
   2115 	{
   2116 	case EM_ALPHA:
   2117 	case EM_S390:
   2118 	case EM_S390_OLD:
   2119 	  if (bed->s->elfclass == ELFCLASS64)
   2120 	    {
   2121 	      hash_ent_size = 8;
   2122 	      break;
   2123 	    }
   2124 	  /* FALLTHROUGH */
   2125 	default:
   2126 	  hash_ent_size = 4;
   2127 	  break;
   2128 	}
   2129 
   2130       filepos = offset_from_vma (phdrs, phnum, dt_hash, sizeof (nb),
   2131 				 NULL);
   2132       if (filepos == (file_ptr) -1
   2133 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0
   2134 	  || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size)
   2135 	goto error_return;
   2136 
   2137       /* The number of dynamic symbol table entries equals the number
   2138 	 of chains.  */
   2139       if (hash_ent_size == 8)
   2140 	symcount = bfd_get_64 (abfd, nb + hash_ent_size);
   2141       else
   2142 	symcount = bfd_get_32 (abfd, nb + hash_ent_size);
   2143     }
   2144   else
   2145     {
   2146       /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
   2147 	 bindings are in hash table.  Since in dynamic symbol table,
   2148 	 all symbols with STB_LOCAL binding are placed before symbols
   2149 	 with other bindings and all undefined symbols are placed
   2150 	 before defined ones, the highest symbol index in DT_GNU_HASH
   2151 	 is the highest dynamic symbol table index.  */
   2152       unsigned char nb[16];
   2153       bfd_vma ngnubuckets;
   2154       bfd_vma gnusymidx;
   2155       size_t i, ngnuchains;
   2156       bfd_vma maxchain = 0xffffffff, bitmaskwords;
   2157       bfd_vma buckets_vma;
   2158 
   2159       filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash,
   2160 				 sizeof (nb), NULL);
   2161       if (filepos == (file_ptr) -1
   2162 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0
   2163 	  || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb))
   2164 	goto error_return;
   2165 
   2166       ngnubuckets = bfd_get_32 (abfd, nb);
   2167       gnusymidx = bfd_get_32 (abfd, nb + 4);
   2168       bitmaskwords = bfd_get_32 (abfd, nb + 8);
   2169       buckets_vma = dt_gnu_hash + 16;
   2170       if (bed->s->elfclass == ELFCLASS32)
   2171 	buckets_vma += bitmaskwords * 4;
   2172       else
   2173 	buckets_vma += bitmaskwords * 8;
   2174       filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL);
   2175       if (filepos == (file_ptr) -1
   2176 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2177 	goto error_return;
   2178 
   2179       gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize);
   2180       if (gnubuckets == NULL)
   2181 	goto error_return;
   2182 
   2183       for (i = 0; i < ngnubuckets; i++)
   2184 	if (gnubuckets[i] != 0)
   2185 	  {
   2186 	    if (gnubuckets[i] < gnusymidx)
   2187 	      goto error_return;
   2188 
   2189 	    if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
   2190 	      maxchain = gnubuckets[i];
   2191 	  }
   2192 
   2193       if (maxchain == 0xffffffff)
   2194 	{
   2195 	  symcount = 0;
   2196 	  goto empty_gnu_hash;
   2197 	}
   2198 
   2199       maxchain -= gnusymidx;
   2200       filepos = offset_from_vma (phdrs, phnum,
   2201 				 (buckets_vma +
   2202 				  4 * (ngnubuckets + maxchain)),
   2203 				 4, NULL);
   2204       if (filepos == (file_ptr) -1
   2205 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2206 	goto error_return;
   2207 
   2208       do
   2209 	{
   2210 	  if (bfd_read (nb, 4, abfd) != 4)
   2211 	    goto error_return;
   2212 	  ++maxchain;
   2213 	  if (maxchain == 0)
   2214 	    goto error_return;
   2215 	}
   2216       while ((bfd_get_32 (abfd, nb) & 1) == 0);
   2217 
   2218       filepos = offset_from_vma (phdrs, phnum,
   2219 				 (buckets_vma + 4 * ngnubuckets),
   2220 				 4, NULL);
   2221       if (filepos == (file_ptr) -1
   2222 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2223 	goto error_return;
   2224 
   2225       gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize);
   2226       if (gnuchains == NULL)
   2227 	goto error_return;
   2228       ngnuchains = maxchain;
   2229 
   2230       if (dt_mips_xhash)
   2231 	{
   2232 	  filepos = offset_from_vma (phdrs, phnum,
   2233 				     (buckets_vma
   2234 				      + 4 * (ngnubuckets + maxchain)),
   2235 				     4, NULL);
   2236 	  if (filepos == (file_ptr) -1
   2237 	      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2238 	    goto error_return;
   2239 
   2240 	  mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize);
   2241 	  if (mipsxlat == NULL)
   2242 	    goto error_return;
   2243 	}
   2244 
   2245       symcount = 0;
   2246       for (i = 0; i < ngnubuckets; ++i)
   2247 	if (gnubuckets[i] != 0)
   2248 	  {
   2249 	    bfd_vma si = gnubuckets[i];
   2250 	    bfd_vma off = si - gnusymidx;
   2251 	    do
   2252 	      {
   2253 		if (mipsxlat)
   2254 		  {
   2255 		    if (mipsxlat[off] >= symcount)
   2256 		      symcount = mipsxlat[off] + 1;
   2257 		  }
   2258 		else
   2259 		  {
   2260 		    if (si >= symcount)
   2261 		      symcount = si + 1;
   2262 		  }
   2263 		si++;
   2264 	      }
   2265 	    while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
   2266 	  }
   2267     }
   2268 
   2269   /* Swap in dynamic symbol table.  */
   2270   if (_bfd_mul_overflow (symcount, extsym_size, &amt))
   2271     {
   2272       bfd_set_error (bfd_error_file_too_big);
   2273       goto error_return;
   2274     }
   2275 
   2276   filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL);
   2277   if (filepos == (file_ptr) -1
   2278       || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2279     goto error_return;
   2280   esymbuf_size = amt;
   2281   esymbuf = _bfd_mmap_readonly_temporary (abfd, esymbuf_size,
   2282 					  &esymbuf_addr,
   2283 					  &esymbuf_size);
   2284   if (esymbuf == NULL)
   2285     goto error_return;
   2286 
   2287   if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
   2288     {
   2289       bfd_set_error (bfd_error_file_too_big);
   2290       goto error_return;
   2291     }
   2292 
   2293   /* Dynamic symbol table must be valid until ABFD is closed.  */
   2294   isymbuf = (Elf_Internal_Sym *) bfd_alloc (abfd, amt);
   2295   if (isymbuf == NULL)
   2296     goto error_return;
   2297 
   2298   swap_symbol_in = bed->s->swap_symbol_in;
   2299 
   2300   /* Convert the symbols to internal form.  */
   2301   isymend = isymbuf + symcount;
   2302   for (esym = esymbuf, isym = isymbuf;
   2303        isym < isymend;
   2304        esym += extsym_size, isym++)
   2305     if (!swap_symbol_in (abfd, esym, NULL, isym)
   2306 	|| isym->st_name >= dt_strsz)
   2307       {
   2308 	bfd_set_error (bfd_error_invalid_operation);
   2309 	goto error_return;
   2310       }
   2311 
   2312   if (dt_versym)
   2313     {
   2314       /* Swap in DT_VERSYM.  */
   2315       if (_bfd_mul_overflow (symcount, 2, &amt))
   2316 	{
   2317 	  bfd_set_error (bfd_error_file_too_big);
   2318 	  goto error_return;
   2319 	}
   2320 
   2321       filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL);
   2322       if (filepos == (file_ptr) -1
   2323 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2324 	goto error_return;
   2325 
   2326       /* DT_VERSYM info must be valid until ABFD is closed.  */
   2327       versym = _bfd_mmap_readonly_persistent (abfd, amt);
   2328 
   2329       if (dt_verdef)
   2330 	{
   2331 	  /* Read in DT_VERDEF.  */
   2332 	  filepos = offset_from_vma (phdrs, phnum, dt_verdef,
   2333 				     0, &verdef_size);
   2334 	  if (filepos == (file_ptr) -1
   2335 	      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2336 	    goto error_return;
   2337 
   2338 	  /* DT_VERDEF info must be valid until ABFD is closed.  */
   2339 	  verdef = _bfd_mmap_readonly_persistent (abfd, verdef_size);
   2340 	}
   2341 
   2342       if (dt_verneed)
   2343 	{
   2344 	  /* Read in DT_VERNEED.  */
   2345 	  filepos = offset_from_vma (phdrs, phnum, dt_verneed,
   2346 				     0, &verneed_size);
   2347 	  if (filepos == (file_ptr) -1
   2348 	      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2349 	    goto error_return;
   2350 
   2351 	  /* DT_VERNEED info must be valid until ABFD is closed.  */
   2352 	  verneed = _bfd_mmap_readonly_persistent (abfd, verneed_size);
   2353 	}
   2354     }
   2355 
   2356  empty_gnu_hash:
   2357   elf_tdata (abfd)->dt_strtab = strbuf;
   2358   elf_tdata (abfd)->dt_strsz = dt_strsz;
   2359   elf_tdata (abfd)->dt_symtab = isymbuf;
   2360   elf_tdata (abfd)->dt_symtab_count = symcount;
   2361   elf_tdata (abfd)->dt_versym = versym;
   2362   elf_tdata (abfd)->dt_verdef = verdef;
   2363   elf_tdata (abfd)->dt_verneed = verneed;
   2364   elf_tdata (abfd)->dt_verdef_count
   2365     = verdef_size / sizeof (Elf_External_Verdef);
   2366   elf_tdata (abfd)->dt_verneed_count
   2367     = verneed_size / sizeof (Elf_External_Verneed);
   2368 
   2369   res = true;
   2370 
   2371  error_return:
   2372   /* Restore file position for elf_object_p.  */
   2373   if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0)
   2374     res = false;
   2375   _bfd_munmap_readonly_temporary (dynbuf_addr, dynbuf_size);
   2376   _bfd_munmap_readonly_temporary (esymbuf_addr, esymbuf_size);
   2377   free (gnubuckets);
   2378   free (gnuchains);
   2379   free (mipsxlat);
   2380   return res;
   2381 }
   2382 
   2383 /* Reconstruct section from dynamic symbol.  */
   2384 
   2385 asection *
   2386 _bfd_elf_get_section_from_dynamic_symbol (bfd *abfd,
   2387 					  Elf_Internal_Sym *isym)
   2388 {
   2389   asection *sec;
   2390   flagword flags;
   2391 
   2392   if (!elf_use_dt_symtab_p (abfd))
   2393     return NULL;
   2394 
   2395   flags = SEC_ALLOC | SEC_LOAD;
   2396   switch (ELF_ST_TYPE (isym->st_info))
   2397     {
   2398     case STT_FUNC:
   2399     case STT_GNU_IFUNC:
   2400       sec = bfd_get_section_by_name (abfd, ".text");
   2401       if (sec == NULL)
   2402 	sec = bfd_make_section_with_flags (abfd,
   2403 					   ".text",
   2404 					   flags | SEC_CODE);
   2405       break;
   2406     case STT_COMMON:
   2407       sec = bfd_com_section_ptr;
   2408       break;
   2409     case STT_OBJECT:
   2410       sec = bfd_get_section_by_name (abfd, ".data");
   2411       if (sec == NULL)
   2412 	sec = bfd_make_section_with_flags (abfd,
   2413 					   ".data",
   2414 					   flags | SEC_DATA);
   2415       break;
   2416     case STT_TLS:
   2417       sec = bfd_get_section_by_name (abfd, ".tdata");
   2418       if (sec == NULL)
   2419 	sec = bfd_make_section_with_flags (abfd,
   2420 					   ".tdata",
   2421 					   (flags
   2422 					    | SEC_DATA
   2423 					    | SEC_THREAD_LOCAL));
   2424       break;
   2425     default:
   2426       sec = bfd_abs_section_ptr;
   2427       break;
   2428     }
   2429 
   2430   return sec;
   2431 }
   2432 
   2433 /* Get version name.  If BASE_P is TRUE, return "Base" for VER_FLG_BASE
   2434    and return symbol version for symbol version itself.   */
   2435 
   2436 const char *
   2437 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
   2438 				    bool base_p,
   2439 				    bool *hidden)
   2440 {
   2441   const char *version_string = NULL;
   2442   if ((elf_dynversym (abfd) != 0
   2443        && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
   2444       || (elf_tdata (abfd)->dt_versym != NULL
   2445 	  && (elf_tdata (abfd)->dt_verdef != NULL
   2446 	      || elf_tdata (abfd)->dt_verneed != NULL)))
   2447     {
   2448       unsigned int vernum = ((elf_symbol_type *) symbol)->version;
   2449 
   2450       *hidden = (vernum & VERSYM_HIDDEN) != 0;
   2451       vernum &= VERSYM_VERSION;
   2452 
   2453       if (vernum == 0)
   2454 	version_string = "";
   2455       else if (vernum == 1
   2456 	       && (vernum > elf_tdata (abfd)->cverdefs
   2457 		   || (elf_tdata (abfd)->verdef[0].vd_flags
   2458 		       == VER_FLG_BASE)))
   2459 	version_string = base_p ? "Base" : "";
   2460       else if (vernum <= elf_tdata (abfd)->cverdefs)
   2461 	{
   2462 	  const char *nodename
   2463 	    = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
   2464 	  version_string = "";
   2465 	  if (base_p
   2466 	      || nodename == NULL
   2467 	      || symbol->name == NULL
   2468 	      || strcmp (symbol->name, nodename) != 0)
   2469 	    version_string = nodename;
   2470 	}
   2471       else
   2472 	{
   2473 	  Elf_Internal_Verneed *t;
   2474 
   2475 	  version_string = _("<corrupt>");
   2476 	  for (t = elf_tdata (abfd)->verref;
   2477 	       t != NULL;
   2478 	       t = t->vn_nextref)
   2479 	    {
   2480 	      Elf_Internal_Vernaux *a;
   2481 
   2482 	      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   2483 		{
   2484 		  if (a->vna_other == vernum)
   2485 		    {
   2486 		      *hidden = true;
   2487 		      version_string = a->vna_nodename;
   2488 		      break;
   2489 		    }
   2490 		}
   2491 	    }
   2492 	}
   2493     }
   2494   return version_string;
   2495 }
   2496 
   2497 /* Display ELF-specific fields of a symbol.  */
   2498 
   2499 void
   2500 bfd_elf_print_symbol (bfd *abfd,
   2501 		      void *filep,
   2502 		      asymbol *symbol,
   2503 		      bfd_print_symbol_type how)
   2504 {
   2505   FILE *file = (FILE *) filep;
   2506   switch (how)
   2507     {
   2508     case bfd_print_symbol_name:
   2509       fprintf (file, "%s", symbol->name);
   2510       break;
   2511     case bfd_print_symbol_more:
   2512       fprintf (file, "elf ");
   2513       bfd_fprintf_vma (abfd, file, symbol->value);
   2514       fprintf (file, " %x", symbol->flags);
   2515       break;
   2516     case bfd_print_symbol_all:
   2517       {
   2518 	const char *section_name;
   2519 	const char *name = NULL;
   2520 	const struct elf_backend_data *bed;
   2521 	unsigned char st_other;
   2522 	bfd_vma val;
   2523 	const char *version_string;
   2524 	bool hidden;
   2525 
   2526 	section_name = symbol->section ? symbol->section->name : "(*none*)";
   2527 
   2528 	bed = get_elf_backend_data (abfd);
   2529 	if (bed->elf_backend_print_symbol_all)
   2530 	  name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
   2531 
   2532 	if (name == NULL)
   2533 	  {
   2534 	    name = symbol->name;
   2535 	    bfd_print_symbol_vandf (abfd, file, symbol);
   2536 	  }
   2537 
   2538 	fprintf (file, " %s\t", section_name);
   2539 	/* Print the "other" value for a symbol.  For common symbols,
   2540 	   we've already printed the size; now print the alignment.
   2541 	   For other symbols, we have no specified alignment, and
   2542 	   we've printed the address; now print the size.  */
   2543 	if (symbol->section && bfd_is_com_section (symbol->section))
   2544 	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
   2545 	else
   2546 	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
   2547 	bfd_fprintf_vma (abfd, file, val);
   2548 
   2549 	/* If we have version information, print it.  */
   2550 	version_string = _bfd_elf_get_symbol_version_string (abfd,
   2551 							     symbol,
   2552 							     true,
   2553 							     &hidden);
   2554 	if (version_string)
   2555 	  {
   2556 	    if (!hidden)
   2557 	      fprintf (file, "  %-11s", version_string);
   2558 	    else
   2559 	      {
   2560 		int i;
   2561 
   2562 		fprintf (file, " (%s)", version_string);
   2563 		for (i = 10 - strlen (version_string); i > 0; --i)
   2564 		  putc (' ', file);
   2565 	      }
   2566 	  }
   2567 
   2568 	/* If the st_other field is not zero, print it.  */
   2569 	st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
   2570 
   2571 	switch (st_other)
   2572 	  {
   2573 	  case 0: break;
   2574 	  case STV_INTERNAL:  fprintf (file, " .internal");  break;
   2575 	  case STV_HIDDEN:    fprintf (file, " .hidden");    break;
   2576 	  case STV_PROTECTED: fprintf (file, " .protected"); break;
   2577 	  default:
   2578 	    /* Some other non-defined flags are also present, so print
   2579 	       everything hex.  */
   2580 	    fprintf (file, " 0x%02x", (unsigned int) st_other);
   2581 	  }
   2582 
   2583 	fprintf (file, " %s", name);
   2584       }
   2585       break;
   2586     }
   2587 }
   2588 
   2589 /* ELF .o/exec file reading */
   2591 
   2592 /* Create a new bfd section from an ELF section header.  */
   2593 
   2594 bool
   2595 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
   2596 {
   2597   Elf_Internal_Shdr *hdr;
   2598   Elf_Internal_Ehdr *ehdr;
   2599   const struct elf_backend_data *bed;
   2600   const char *name;
   2601   bool ret = true;
   2602 
   2603   if (shindex >= elf_numsections (abfd))
   2604     return false;
   2605 
   2606   /* PR17512: A corrupt ELF binary might contain a loop of sections via
   2607      sh_link or sh_info.  Detect this here, by refusing to load a
   2608      section that we are already in the process of loading.  */
   2609   if (elf_tdata (abfd)->being_created[shindex])
   2610     {
   2611       _bfd_error_handler
   2612 	(_("%pB: warning: loop in section dependencies detected"), abfd);
   2613       return false;
   2614     }
   2615   elf_tdata (abfd)->being_created[shindex] = true;
   2616 
   2617   hdr = elf_elfsections (abfd)[shindex];
   2618   ehdr = elf_elfheader (abfd);
   2619   name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
   2620 					  hdr->sh_name);
   2621   if (name == NULL)
   2622     goto fail;
   2623 
   2624   bed = get_elf_backend_data (abfd);
   2625   switch (hdr->sh_type)
   2626     {
   2627     case SHT_NULL:
   2628       /* Inactive section. Throw it away.  */
   2629       goto success;
   2630 
   2631     case SHT_PROGBITS:		/* Normal section with contents.  */
   2632     case SHT_NOBITS:		/* .bss section.  */
   2633     case SHT_HASH:		/* .hash section.  */
   2634     case SHT_NOTE:		/* .note section.  */
   2635     case SHT_INIT_ARRAY:	/* .init_array section.  */
   2636     case SHT_FINI_ARRAY:	/* .fini_array section.  */
   2637     case SHT_PREINIT_ARRAY:	/* .preinit_array section.  */
   2638     case SHT_GNU_LIBLIST:	/* .gnu.liblist section.  */
   2639     case SHT_GNU_HASH:		/* .gnu.hash section.  */
   2640       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2641       goto success;
   2642 
   2643     case SHT_DYNAMIC:	/* Dynamic linking information.  */
   2644       if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2645 	goto fail;
   2646 
   2647       if (hdr->sh_link > elf_numsections (abfd))
   2648 	{
   2649 	  /* PR 10478: Accept Solaris binaries with a sh_link field
   2650 	     set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1).  */
   2651 	  switch (bfd_get_arch (abfd))
   2652 	    {
   2653 	    case bfd_arch_i386:
   2654 	    case bfd_arch_sparc:
   2655 	      if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
   2656 		  || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
   2657 		break;
   2658 	      /* Otherwise fall through.  */
   2659 	    default:
   2660 	      goto fail;
   2661 	    }
   2662 	}
   2663       else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
   2664 	goto fail;
   2665       else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
   2666 	{
   2667 	  Elf_Internal_Shdr *dynsymhdr;
   2668 
   2669 	  /* The shared libraries distributed with hpux11 have a bogus
   2670 	     sh_link field for the ".dynamic" section.  Find the
   2671 	     string table for the ".dynsym" section instead.  */
   2672 	  if (elf_dynsymtab (abfd) != 0)
   2673 	    {
   2674 	      dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
   2675 	      hdr->sh_link = dynsymhdr->sh_link;
   2676 	    }
   2677 	  else
   2678 	    {
   2679 	      unsigned int i, num_sec;
   2680 
   2681 	      num_sec = elf_numsections (abfd);
   2682 	      for (i = 1; i < num_sec; i++)
   2683 		{
   2684 		  dynsymhdr = elf_elfsections (abfd)[i];
   2685 		  if (dynsymhdr->sh_type == SHT_DYNSYM)
   2686 		    {
   2687 		      hdr->sh_link = dynsymhdr->sh_link;
   2688 		      break;
   2689 		    }
   2690 		}
   2691 	    }
   2692 	}
   2693       goto success;
   2694 
   2695     case SHT_SYMTAB:		/* A symbol table.  */
   2696       if (elf_onesymtab (abfd) == shindex)
   2697 	goto success;
   2698 
   2699       if (hdr->sh_entsize != bed->s->sizeof_sym)
   2700 	goto fail;
   2701 
   2702       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
   2703 	{
   2704 	  if (hdr->sh_size != 0)
   2705 	    goto fail;
   2706 	  /* Some assemblers erroneously set sh_info to one with a
   2707 	     zero sh_size.  ld sees this as a global symbol count
   2708 	     of (unsigned) -1.  Fix it here.  */
   2709 	  hdr->sh_info = 0;
   2710 	  goto success;
   2711 	}
   2712 
   2713       /* PR 18854: A binary might contain more than one symbol table.
   2714 	 Unusual, but possible.  Warn, but continue.  */
   2715       if (elf_onesymtab (abfd) != 0)
   2716 	{
   2717 	  _bfd_error_handler
   2718 	    /* xgettext:c-format */
   2719 	    (_("%pB: warning: multiple symbol tables detected"
   2720 	       " - ignoring the table in section %u"),
   2721 	     abfd, shindex);
   2722 	  goto success;
   2723 	}
   2724       elf_onesymtab (abfd) = shindex;
   2725       elf_symtab_hdr (abfd) = *hdr;
   2726       elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
   2727       abfd->flags |= HAS_SYMS;
   2728 
   2729       /* Sometimes a shared object will map in the symbol table.  If
   2730 	 SHF_ALLOC is set, and this is a shared object, then we also
   2731 	 treat this section as a BFD section.  We can not base the
   2732 	 decision purely on SHF_ALLOC, because that flag is sometimes
   2733 	 set in a relocatable object file, which would confuse the
   2734 	 linker.  */
   2735       if ((hdr->sh_flags & SHF_ALLOC) != 0
   2736 	  && (abfd->flags & DYNAMIC) != 0
   2737 	  && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2738 						shindex))
   2739 	goto fail;
   2740 
   2741       /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
   2742 	 can't read symbols without that section loaded as well.  It
   2743 	 is most likely specified by the next section header.  */
   2744       {
   2745 	elf_section_list * entry;
   2746 	unsigned int i, num_sec;
   2747 
   2748 	for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
   2749 	  if (entry->hdr.sh_link == shindex)
   2750 	    goto success;
   2751 
   2752 	num_sec = elf_numsections (abfd);
   2753 	for (i = shindex + 1; i < num_sec; i++)
   2754 	  {
   2755 	    Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2756 
   2757 	    if (hdr2->sh_type == SHT_SYMTAB_SHNDX
   2758 		&& hdr2->sh_link == shindex)
   2759 	      break;
   2760 	  }
   2761 
   2762 	if (i == num_sec)
   2763 	  for (i = 1; i < shindex; i++)
   2764 	    {
   2765 	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2766 
   2767 	      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
   2768 		  && hdr2->sh_link == shindex)
   2769 		break;
   2770 	    }
   2771 
   2772 	if (i != shindex)
   2773 	  ret = bfd_section_from_shdr (abfd, i);
   2774 	/* else FIXME: we have failed to find the symbol table.
   2775 	   Should we issue an error?  */
   2776 	goto success;
   2777       }
   2778 
   2779     case SHT_DYNSYM:		/* A dynamic symbol table.  */
   2780       if (elf_dynsymtab (abfd) == shindex)
   2781 	goto success;
   2782 
   2783       if (hdr->sh_entsize != bed->s->sizeof_sym)
   2784 	goto fail;
   2785 
   2786       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
   2787 	{
   2788 	  if (hdr->sh_size != 0)
   2789 	    goto fail;
   2790 
   2791 	  /* Some linkers erroneously set sh_info to one with a
   2792 	     zero sh_size.  ld sees this as a global symbol count
   2793 	     of (unsigned) -1.  Fix it here.  */
   2794 	  hdr->sh_info = 0;
   2795 	  goto success;
   2796 	}
   2797 
   2798       /* PR 18854: A binary might contain more than one dynamic symbol table.
   2799 	 Unusual, but possible.  Warn, but continue.  */
   2800       if (elf_dynsymtab (abfd) != 0)
   2801 	{
   2802 	  _bfd_error_handler
   2803 	    /* xgettext:c-format */
   2804 	    (_("%pB: warning: multiple dynamic symbol tables detected"
   2805 	       " - ignoring the table in section %u"),
   2806 	     abfd, shindex);
   2807 	  goto success;
   2808 	}
   2809       elf_dynsymtab (abfd) = shindex;
   2810       elf_tdata (abfd)->dynsymtab_hdr = *hdr;
   2811       elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   2812       abfd->flags |= HAS_SYMS;
   2813 
   2814       /* Besides being a symbol table, we also treat this as a regular
   2815 	 section, so that objcopy can handle it.  */
   2816       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2817       goto success;
   2818 
   2819     case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections.  */
   2820       {
   2821 	elf_section_list * entry;
   2822 
   2823 	for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
   2824 	  if (entry->ndx == shindex)
   2825 	    goto success;
   2826 
   2827 	entry = bfd_alloc (abfd, sizeof (*entry));
   2828 	if (entry == NULL)
   2829 	  goto fail;
   2830 	entry->ndx = shindex;
   2831 	entry->hdr = * hdr;
   2832 	entry->next = elf_symtab_shndx_list (abfd);
   2833 	elf_symtab_shndx_list (abfd) = entry;
   2834 	elf_elfsections (abfd)[shindex] = & entry->hdr;
   2835 	goto success;
   2836       }
   2837 
   2838     case SHT_STRTAB:		/* A string table.  */
   2839       if (hdr->bfd_section != NULL)
   2840 	goto success;
   2841 
   2842       if (ehdr->e_shstrndx == shindex)
   2843 	{
   2844 	  elf_tdata (abfd)->shstrtab_hdr = *hdr;
   2845 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
   2846 	  goto success;
   2847 	}
   2848 
   2849       if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
   2850 	{
   2851 	symtab_strtab:
   2852 	  elf_tdata (abfd)->strtab_hdr = *hdr;
   2853 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
   2854 	  goto success;
   2855 	}
   2856 
   2857       if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
   2858 	{
   2859 	dynsymtab_strtab:
   2860 	  elf_tdata (abfd)->dynstrtab_hdr = *hdr;
   2861 	  hdr = &elf_tdata (abfd)->dynstrtab_hdr;
   2862 	  elf_elfsections (abfd)[shindex] = hdr;
   2863 	  /* We also treat this as a regular section, so that objcopy
   2864 	     can handle it.  */
   2865 	  ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2866 						 shindex);
   2867 	  goto success;
   2868 	}
   2869 
   2870       /* If the string table isn't one of the above, then treat it as a
   2871 	 regular section.  We need to scan all the headers to be sure,
   2872 	 just in case this strtab section appeared before the above.  */
   2873       if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
   2874 	{
   2875 	  unsigned int i, num_sec;
   2876 
   2877 	  num_sec = elf_numsections (abfd);
   2878 	  for (i = 1; i < num_sec; i++)
   2879 	    {
   2880 	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2881 	      if (hdr2->sh_link == shindex)
   2882 		{
   2883 		  /* Prevent endless recursion on broken objects.  */
   2884 		  if (i == shindex)
   2885 		    goto fail;
   2886 		  if (! bfd_section_from_shdr (abfd, i))
   2887 		    goto fail;
   2888 		  if (elf_onesymtab (abfd) == i)
   2889 		    goto symtab_strtab;
   2890 		  if (elf_dynsymtab (abfd) == i)
   2891 		    goto dynsymtab_strtab;
   2892 		}
   2893 	    }
   2894 	}
   2895       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2896       goto success;
   2897 
   2898     case SHT_REL:
   2899     case SHT_RELA:
   2900     case SHT_RELR:
   2901       /* *These* do a lot of work -- but build no sections!  */
   2902       {
   2903 	asection *target_sect;
   2904 	Elf_Internal_Shdr *hdr2, **p_hdr;
   2905 	unsigned int num_sec = elf_numsections (abfd);
   2906 	struct bfd_elf_section_data *esdt;
   2907 	bfd_size_type size;
   2908 
   2909 	if (hdr->sh_type == SHT_REL)
   2910 	  size = bed->s->sizeof_rel;
   2911 	else if (hdr->sh_type == SHT_RELA)
   2912 	  size = bed->s->sizeof_rela;
   2913 	else
   2914 	  size = bed->s->arch_size / 8;
   2915 	if (hdr->sh_entsize != size)
   2916 	  goto fail;
   2917 
   2918 	/* Check for a bogus link to avoid crashing.  */
   2919 	if (hdr->sh_link >= num_sec)
   2920 	  {
   2921 	    _bfd_error_handler
   2922 	      /* xgettext:c-format */
   2923 	      (_("%pB: invalid link %u for reloc section %s (index %u)"),
   2924 	       abfd, hdr->sh_link, name, shindex);
   2925 	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2926 	    goto success;
   2927 	  }
   2928 
   2929 	/* Get the symbol table.  */
   2930 	if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
   2931 	     || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
   2932 	    && ! bfd_section_from_shdr (abfd, hdr->sh_link))
   2933 	  goto fail;
   2934 
   2935 	/* If this is an alloc section in an executable or shared
   2936 	   library, or the reloc section does not use the main symbol
   2937 	   table we don't treat it as a reloc section.  BFD can't
   2938 	   adequately represent such a section, so at least for now,
   2939 	   we don't try.  We just present it as a normal section.  We
   2940 	   also can't use it as a reloc section if it points to the
   2941 	   null section, an invalid section, another reloc section, or
   2942 	   its sh_link points to the null section.  */
   2943 	if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
   2944 	     && (hdr->sh_flags & SHF_ALLOC) != 0)
   2945 	    || (hdr->sh_flags & SHF_COMPRESSED) != 0
   2946 	    || hdr->sh_type == SHT_RELR
   2947 	    || hdr->sh_link == SHN_UNDEF
   2948 	    || hdr->sh_link != elf_onesymtab (abfd)
   2949 	    || hdr->sh_info == SHN_UNDEF
   2950 	    || hdr->sh_info >= num_sec
   2951 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
   2952 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
   2953 	  {
   2954 	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2955 	    goto success;
   2956 	  }
   2957 
   2958 	if (! bfd_section_from_shdr (abfd, hdr->sh_info))
   2959 	  goto fail;
   2960 
   2961 	target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
   2962 	if (target_sect == NULL)
   2963 	  goto fail;
   2964 
   2965 	esdt = elf_section_data (target_sect);
   2966 	if (hdr->sh_type == SHT_RELA)
   2967 	  p_hdr = &esdt->rela.hdr;
   2968 	else
   2969 	  p_hdr = &esdt->rel.hdr;
   2970 
   2971 	/* PR 17512: file: 0b4f81b7.
   2972 	   Also see PR 24456, for a file which deliberately has two reloc
   2973 	   sections.  */
   2974 	if (*p_hdr != NULL)
   2975 	  {
   2976 	    if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
   2977 	      {
   2978 		_bfd_error_handler
   2979 		  /* xgettext:c-format */
   2980 		  (_("%pB: warning: secondary relocation section '%s' "
   2981 		     "for section %pA found - ignoring"),
   2982 		   abfd, name, target_sect);
   2983 	      }
   2984 	    else
   2985 	      esdt->has_secondary_relocs = true;
   2986 	    goto success;
   2987 	  }
   2988 
   2989 	hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
   2990 	if (hdr2 == NULL)
   2991 	  goto fail;
   2992 	*hdr2 = *hdr;
   2993 	*p_hdr = hdr2;
   2994 	elf_elfsections (abfd)[shindex] = hdr2;
   2995 	target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
   2996 				     * bed->s->int_rels_per_ext_rel);
   2997 	target_sect->flags |= SEC_RELOC;
   2998 	target_sect->relocation = NULL;
   2999 	target_sect->rel_filepos = hdr->sh_offset;
   3000 	/* In the section to which the relocations apply, mark whether
   3001 	   its relocations are of the REL or RELA variety.  */
   3002 	if (hdr->sh_size != 0)
   3003 	  {
   3004 	    if (hdr->sh_type == SHT_RELA)
   3005 	      target_sect->use_rela_p = 1;
   3006 	  }
   3007 	abfd->flags |= HAS_RELOC;
   3008 	goto success;
   3009       }
   3010 
   3011     case SHT_GNU_verdef:
   3012       if (hdr->sh_info != 0)
   3013 	elf_dynverdef (abfd) = shindex;
   3014       elf_tdata (abfd)->dynverdef_hdr = *hdr;
   3015       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   3016       goto success;
   3017 
   3018     case SHT_GNU_versym:
   3019       if (hdr->sh_entsize != sizeof (Elf_External_Versym))
   3020 	goto fail;
   3021 
   3022       elf_dynversym (abfd) = shindex;
   3023       elf_tdata (abfd)->dynversym_hdr = *hdr;
   3024       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   3025       goto success;
   3026 
   3027     case SHT_GNU_verneed:
   3028       if (hdr->sh_info != 0)
   3029 	elf_dynverref (abfd) = shindex;
   3030       elf_tdata (abfd)->dynverref_hdr = *hdr;
   3031       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   3032       goto success;
   3033 
   3034     case SHT_SHLIB:
   3035       goto success;
   3036 
   3037     case SHT_GROUP:
   3038       if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
   3039 	goto fail;
   3040 
   3041       if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   3042 	goto fail;
   3043 
   3044       goto success;
   3045 
   3046     default:
   3047       /* Possibly an attributes section.  */
   3048       if (hdr->sh_type == SHT_GNU_ATTRIBUTES
   3049 	  || hdr->sh_type == bed->obj_attrs_section_type)
   3050 	{
   3051 	  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   3052 	    goto fail;
   3053 	  _bfd_elf_parse_attributes (abfd, hdr);
   3054 	  goto success;
   3055 	}
   3056 
   3057       /* Check for any processor-specific section types.  */
   3058       if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
   3059 	goto success;
   3060 
   3061       if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
   3062 	{
   3063 	  if ((hdr->sh_flags & SHF_ALLOC) != 0)
   3064 	    /* FIXME: How to properly handle allocated section reserved
   3065 	       for applications?  */
   3066 	    _bfd_error_handler
   3067 	      /* xgettext:c-format */
   3068 	      (_("%pB: unknown type [%#x] section `%s'"),
   3069 	       abfd, hdr->sh_type, name);
   3070 	  else
   3071 	    {
   3072 	      /* Allow sections reserved for applications.  */
   3073 	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   3074 	      goto success;
   3075 	    }
   3076 	}
   3077       else if (hdr->sh_type >= SHT_LOPROC
   3078 	       && hdr->sh_type <= SHT_HIPROC)
   3079 	/* FIXME: We should handle this section.  */
   3080 	_bfd_error_handler
   3081 	  /* xgettext:c-format */
   3082 	  (_("%pB: unknown type [%#x] section `%s'"),
   3083 	   abfd, hdr->sh_type, name);
   3084       else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
   3085 	{
   3086 	  /* Unrecognised OS-specific sections.  */
   3087 	  if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
   3088 	    /* SHF_OS_NONCONFORMING indicates that special knowledge is
   3089 	       required to correctly process the section and the file should
   3090 	       be rejected with an error message.  */
   3091 	    _bfd_error_handler
   3092 	      /* xgettext:c-format */
   3093 	      (_("%pB: unknown type [%#x] section `%s'"),
   3094 	       abfd, hdr->sh_type, name);
   3095 	  else
   3096 	    {
   3097 	      /* Otherwise it should be processed.  */
   3098 	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   3099 	      goto success;
   3100 	    }
   3101 	}
   3102       else
   3103 	/* FIXME: We should handle this section.  */
   3104 	_bfd_error_handler
   3105 	  /* xgettext:c-format */
   3106 	  (_("%pB: unknown type [%#x] section `%s'"),
   3107 	   abfd, hdr->sh_type, name);
   3108 
   3109       goto fail;
   3110     }
   3111 
   3112  fail:
   3113   ret = false;
   3114  success:
   3115   elf_tdata (abfd)->being_created[shindex] = false;
   3116   return ret;
   3117 }
   3118 
   3119 /* Return the local symbol specified by ABFD, R_SYMNDX.  */
   3120 
   3121 Elf_Internal_Sym *
   3122 bfd_sym_from_r_symndx (struct sym_cache *cache,
   3123 		       bfd *abfd,
   3124 		       unsigned long r_symndx)
   3125 {
   3126   unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
   3127 
   3128   if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
   3129     {
   3130       Elf_Internal_Shdr *symtab_hdr;
   3131       unsigned char esym[sizeof (Elf64_External_Sym)];
   3132       Elf_External_Sym_Shndx eshndx;
   3133 
   3134       symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   3135       if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
   3136 				&cache->sym[ent], esym, &eshndx) == NULL)
   3137 	return NULL;
   3138 
   3139       if (cache->abfd != abfd)
   3140 	{
   3141 	  memset (cache->indx, -1, sizeof (cache->indx));
   3142 	  cache->abfd = abfd;
   3143 	}
   3144       cache->indx[ent] = r_symndx;
   3145     }
   3146 
   3147   return &cache->sym[ent];
   3148 }
   3149 
   3150 /* Given an ELF section number, retrieve the corresponding BFD
   3151    section.  */
   3152 
   3153 asection *
   3154 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
   3155 {
   3156   if (sec_index >= elf_numsections (abfd))
   3157     return NULL;
   3158   return elf_elfsections (abfd)[sec_index]->bfd_section;
   3159 }
   3160 
   3161 static const struct bfd_elf_special_section special_sections_b[] =
   3162 {
   3163   { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   3164   { NULL,		    0,	0, 0,		 0 }
   3165 };
   3166 
   3167 static const struct bfd_elf_special_section special_sections_c[] =
   3168 {
   3169   { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
   3170   { STRING_COMMA_LEN (".ctf"),	0, SHT_PROGBITS,    0 },
   3171   { NULL,			0, 0, 0,	    0 }
   3172 };
   3173 
   3174 static const struct bfd_elf_special_section special_sections_d[] =
   3175 {
   3176   { STRING_COMMA_LEN (".data"),		-2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   3177   { STRING_COMMA_LEN (".data1"),	 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   3178   /* There are more DWARF sections than these, but they needn't be added here
   3179      unless you have to cope with broken compilers that don't emit section
   3180      attributes or you want to help the user writing assembler.  */
   3181   { STRING_COMMA_LEN (".debug"),	 0, SHT_PROGBITS, 0 },
   3182   { STRING_COMMA_LEN (".debug_line"),	 0, SHT_PROGBITS, 0 },
   3183   { STRING_COMMA_LEN (".debug_info"),	 0, SHT_PROGBITS, 0 },
   3184   { STRING_COMMA_LEN (".debug_abbrev"),	 0, SHT_PROGBITS, 0 },
   3185   { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
   3186   { STRING_COMMA_LEN (".dynamic"),	 0, SHT_DYNAMIC,  SHF_ALLOC },
   3187   { STRING_COMMA_LEN (".dynstr"),	 0, SHT_STRTAB,	  SHF_ALLOC },
   3188   { STRING_COMMA_LEN (".dynsym"),	 0, SHT_DYNSYM,	  SHF_ALLOC },
   3189   { NULL,		       0,	 0, 0,		  0 }
   3190 };
   3191 
   3192 static const struct bfd_elf_special_section special_sections_f[] =
   3193 {
   3194   { STRING_COMMA_LEN (".fini"),	       0, SHT_PROGBITS,	  SHF_ALLOC + SHF_EXECINSTR },
   3195   { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
   3196   { NULL,			   0 , 0, 0,		  0 }
   3197 };
   3198 
   3199 static const struct bfd_elf_special_section special_sections_g[] =
   3200 {
   3201   { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
   3202   { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
   3203   { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
   3204   { STRING_COMMA_LEN (".gnu.lto_"),	  -1, SHT_PROGBITS,    SHF_EXCLUDE },
   3205   { STRING_COMMA_LEN (".got"),		   0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
   3206   { STRING_COMMA_LEN (".gnu.version"),	   0, SHT_GNU_versym,  0 },
   3207   { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
   3208   { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
   3209   { STRING_COMMA_LEN (".gnu.liblist"),	   0, SHT_GNU_LIBLIST, SHF_ALLOC },
   3210   { STRING_COMMA_LEN (".gnu.conflict"),	   0, SHT_RELA,	       SHF_ALLOC },
   3211   { STRING_COMMA_LEN (".gnu.hash"),	   0, SHT_GNU_HASH,    SHF_ALLOC },
   3212   { NULL,			 0,	   0, 0,	       0 }
   3213 };
   3214 
   3215 static const struct bfd_elf_special_section special_sections_h[] =
   3216 {
   3217   { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,	 SHF_ALLOC },
   3218   { NULL,		     0, 0, 0,		 0 }
   3219 };
   3220 
   3221 static const struct bfd_elf_special_section special_sections_i[] =
   3222 {
   3223   { STRING_COMMA_LEN (".init"),	       0, SHT_PROGBITS,	  SHF_ALLOC + SHF_EXECINSTR },
   3224   { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
   3225   { STRING_COMMA_LEN (".interp"),      0, SHT_PROGBITS,	  0 },
   3226   { NULL,		       0,      0, 0,		  0 }
   3227 };
   3228 
   3229 static const struct bfd_elf_special_section special_sections_l[] =
   3230 {
   3231   { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
   3232   { NULL,		     0, 0, 0,		 0 }
   3233 };
   3234 
   3235 static const struct bfd_elf_special_section special_sections_n[] =
   3236 {
   3237   { STRING_COMMA_LEN (".noinit"),	 -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   3238   { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
   3239   { STRING_COMMA_LEN (".note"),		 -1, SHT_NOTE,	   0 },
   3240   { NULL,		     0,		  0, 0,		   0 }
   3241 };
   3242 
   3243 static const struct bfd_elf_special_section special_sections_p[] =
   3244 {
   3245   { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS,	SHF_ALLOC + SHF_WRITE },
   3246   { STRING_COMMA_LEN (".persistent"),	 -2, SHT_PROGBITS,	SHF_ALLOC + SHF_WRITE },
   3247   { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
   3248   { STRING_COMMA_LEN (".plt"),		  0, SHT_PROGBITS,	SHF_ALLOC + SHF_EXECINSTR },
   3249   { NULL,		    0,		  0, 0,			0 }
   3250 };
   3251 
   3252 static const struct bfd_elf_special_section special_sections_r[] =
   3253 {
   3254   { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
   3255   { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
   3256   { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
   3257   { STRING_COMMA_LEN (".rela"),	  -1, SHT_RELA,	    0 },
   3258   { STRING_COMMA_LEN (".rel"),	  -1, SHT_REL,	    0 },
   3259   { NULL,		    0,	   0, 0,	    0 }
   3260 };
   3261 
   3262 static const struct bfd_elf_special_section special_sections_s[] =
   3263 {
   3264   { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
   3265   { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
   3266   { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
   3267   /* See struct bfd_elf_special_section declaration for the semantics of
   3268      this special case where .prefix_length != strlen (.prefix).  */
   3269   { ".stabstr",			5,  3, SHT_STRTAB, 0 },
   3270   { NULL,			0,  0, 0,	   0 }
   3271 };
   3272 
   3273 static const struct bfd_elf_special_section special_sections_t[] =
   3274 {
   3275   { STRING_COMMA_LEN (".text"),	 -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
   3276   { STRING_COMMA_LEN (".tbss"),	 -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
   3277   { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
   3278   { NULL,		      0,  0, 0,		   0 }
   3279 };
   3280 
   3281 static const struct bfd_elf_special_section special_sections_z[] =
   3282 {
   3283   { STRING_COMMA_LEN (".zdebug_line"),	  0, SHT_PROGBITS, 0 },
   3284   { STRING_COMMA_LEN (".zdebug_info"),	  0, SHT_PROGBITS, 0 },
   3285   { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
   3286   { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
   3287   { NULL,		      0,  0, 0,		   0 }
   3288 };
   3289 
   3290 static const struct bfd_elf_special_section * const special_sections[] =
   3291 {
   3292   special_sections_b,		/* 'b' */
   3293   special_sections_c,		/* 'c' */
   3294   special_sections_d,		/* 'd' */
   3295   NULL,				/* 'e' */
   3296   special_sections_f,		/* 'f' */
   3297   special_sections_g,		/* 'g' */
   3298   special_sections_h,		/* 'h' */
   3299   special_sections_i,		/* 'i' */
   3300   NULL,				/* 'j' */
   3301   NULL,				/* 'k' */
   3302   special_sections_l,		/* 'l' */
   3303   NULL,				/* 'm' */
   3304   special_sections_n,		/* 'n' */
   3305   NULL,				/* 'o' */
   3306   special_sections_p,		/* 'p' */
   3307   NULL,				/* 'q' */
   3308   special_sections_r,		/* 'r' */
   3309   special_sections_s,		/* 's' */
   3310   special_sections_t,		/* 't' */
   3311   NULL,				/* 'u' */
   3312   NULL,				/* 'v' */
   3313   NULL,				/* 'w' */
   3314   NULL,				/* 'x' */
   3315   NULL,				/* 'y' */
   3316   special_sections_z		/* 'z' */
   3317 };
   3318 
   3319 const struct bfd_elf_special_section *
   3320 _bfd_elf_get_special_section (const char *name,
   3321 			      const struct bfd_elf_special_section *spec,
   3322 			      unsigned int rela)
   3323 {
   3324   int i;
   3325   int len;
   3326 
   3327   len = strlen (name);
   3328 
   3329   for (i = 0; spec[i].prefix != NULL; i++)
   3330     {
   3331       int suffix_len;
   3332       int prefix_len = spec[i].prefix_length;
   3333 
   3334       if (len < prefix_len)
   3335 	continue;
   3336       if (memcmp (name, spec[i].prefix, prefix_len) != 0)
   3337 	continue;
   3338 
   3339       suffix_len = spec[i].suffix_length;
   3340       if (suffix_len <= 0)
   3341 	{
   3342 	  if (name[prefix_len] != 0)
   3343 	    {
   3344 	      if (suffix_len == 0)
   3345 		continue;
   3346 	      if (name[prefix_len] != '.'
   3347 		  && (suffix_len == -2
   3348 		      || (rela && spec[i].type == SHT_REL)))
   3349 		continue;
   3350 	    }
   3351 	}
   3352       else
   3353 	{
   3354 	  if (len < prefix_len + suffix_len)
   3355 	    continue;
   3356 	  if (memcmp (name + len - suffix_len,
   3357 		      spec[i].prefix + prefix_len,
   3358 		      suffix_len) != 0)
   3359 	    continue;
   3360 	}
   3361       return &spec[i];
   3362     }
   3363 
   3364   return NULL;
   3365 }
   3366 
   3367 const struct bfd_elf_special_section *
   3368 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
   3369 {
   3370   int i;
   3371   const struct bfd_elf_special_section *spec;
   3372   const struct elf_backend_data *bed;
   3373 
   3374   /* See if this is one of the special sections.  */
   3375   if (sec->name == NULL)
   3376     return NULL;
   3377 
   3378   bed = get_elf_backend_data (abfd);
   3379   spec = bed->special_sections;
   3380   if (spec)
   3381     {
   3382       spec = _bfd_elf_get_special_section (sec->name,
   3383 					   bed->special_sections,
   3384 					   sec->use_rela_p);
   3385       if (spec != NULL)
   3386 	return spec;
   3387     }
   3388 
   3389   if (sec->name[0] != '.')
   3390     return NULL;
   3391 
   3392   i = sec->name[1] - 'b';
   3393   if (i < 0 || i > 'z' - 'b')
   3394     return NULL;
   3395 
   3396   spec = special_sections[i];
   3397 
   3398   if (spec == NULL)
   3399     return NULL;
   3400 
   3401   return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
   3402 }
   3403 
   3404 bool
   3405 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
   3406 {
   3407   struct bfd_elf_section_data *sdata;
   3408   const struct elf_backend_data *bed;
   3409   const struct bfd_elf_special_section *ssect;
   3410 
   3411   sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
   3412   if (sdata == NULL)
   3413     {
   3414       sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
   3415 							  sizeof (*sdata));
   3416       if (sdata == NULL)
   3417 	return false;
   3418       sec->used_by_bfd = sdata;
   3419     }
   3420 
   3421   /* Indicate whether or not this section should use RELA relocations.  */
   3422   bed = get_elf_backend_data (abfd);
   3423   sec->use_rela_p = bed->default_use_rela_p;
   3424 
   3425   /* Set up ELF section type and flags for newly created sections, if
   3426      there is an ABI mandated section.  */
   3427   ssect = (*bed->get_sec_type_attr) (abfd, sec);
   3428   if (ssect != NULL)
   3429     {
   3430       elf_section_type (sec) = ssect->type;
   3431       elf_section_flags (sec) = ssect->attr;
   3432     }
   3433 
   3434   return _bfd_generic_new_section_hook (abfd, sec);
   3435 }
   3436 
   3437 /* Create a new bfd section from an ELF program header.
   3438 
   3439    Since program segments have no names, we generate a synthetic name
   3440    of the form segment<NUM>, where NUM is generally the index in the
   3441    program header table.  For segments that are split (see below) we
   3442    generate the names segment<NUM>a and segment<NUM>b.
   3443 
   3444    Note that some program segments may have a file size that is different than
   3445    (less than) the memory size.  All this means is that at execution the
   3446    system must allocate the amount of memory specified by the memory size,
   3447    but only initialize it with the first "file size" bytes read from the
   3448    file.  This would occur for example, with program segments consisting
   3449    of combined data+bss.
   3450 
   3451    To handle the above situation, this routine generates TWO bfd sections
   3452    for the single program segment.  The first has the length specified by
   3453    the file size of the segment, and the second has the length specified
   3454    by the difference between the two sizes.  In effect, the segment is split
   3455    into its initialized and uninitialized parts.  */
   3456 
   3457 bool
   3458 _bfd_elf_make_section_from_phdr (bfd *abfd,
   3459 				 Elf_Internal_Phdr *hdr,
   3460 				 int hdr_index,
   3461 				 const char *type_name)
   3462 {
   3463   asection *newsect;
   3464   char *name;
   3465   char namebuf[64];
   3466   size_t len;
   3467   int split;
   3468   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   3469 
   3470   split = ((hdr->p_memsz > 0)
   3471 	    && (hdr->p_filesz > 0)
   3472 	    && (hdr->p_memsz > hdr->p_filesz));
   3473 
   3474   if (hdr->p_filesz > 0)
   3475     {
   3476       sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
   3477       len = strlen (namebuf) + 1;
   3478       name = (char *) bfd_alloc (abfd, len);
   3479       if (!name)
   3480 	return false;
   3481       memcpy (name, namebuf, len);
   3482       newsect = bfd_make_section (abfd, name);
   3483       if (newsect == NULL)
   3484 	return false;
   3485       newsect->vma = hdr->p_vaddr / opb;
   3486       newsect->lma = hdr->p_paddr / opb;
   3487       newsect->size = hdr->p_filesz;
   3488       newsect->filepos = hdr->p_offset;
   3489       newsect->flags |= SEC_HAS_CONTENTS;
   3490       newsect->alignment_power = bfd_log2 (hdr->p_align);
   3491       if (hdr->p_type == PT_LOAD)
   3492 	{
   3493 	  newsect->flags |= SEC_ALLOC;
   3494 	  newsect->flags |= SEC_LOAD;
   3495 	  if (hdr->p_flags & PF_X)
   3496 	    {
   3497 	      /* FIXME: all we known is that it has execute PERMISSION,
   3498 		 may be data.  */
   3499 	      newsect->flags |= SEC_CODE;
   3500 	    }
   3501 	}
   3502       if (!(hdr->p_flags & PF_W))
   3503 	{
   3504 	  newsect->flags |= SEC_READONLY;
   3505 	}
   3506     }
   3507 
   3508   if (hdr->p_memsz > hdr->p_filesz)
   3509     {
   3510       bfd_vma align;
   3511 
   3512       sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
   3513       len = strlen (namebuf) + 1;
   3514       name = (char *) bfd_alloc (abfd, len);
   3515       if (!name)
   3516 	return false;
   3517       memcpy (name, namebuf, len);
   3518       newsect = bfd_make_section (abfd, name);
   3519       if (newsect == NULL)
   3520 	return false;
   3521       newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
   3522       newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
   3523       newsect->size = hdr->p_memsz - hdr->p_filesz;
   3524       newsect->filepos = hdr->p_offset + hdr->p_filesz;
   3525       align = newsect->vma & -newsect->vma;
   3526       if (align == 0 || align > hdr->p_align)
   3527 	align = hdr->p_align;
   3528       newsect->alignment_power = bfd_log2 (align);
   3529       if (hdr->p_type == PT_LOAD)
   3530 	{
   3531 	  newsect->flags |= SEC_ALLOC;
   3532 	  if (hdr->p_flags & PF_X)
   3533 	    newsect->flags |= SEC_CODE;
   3534 	}
   3535       if (!(hdr->p_flags & PF_W))
   3536 	newsect->flags |= SEC_READONLY;
   3537     }
   3538 
   3539   return true;
   3540 }
   3541 
   3542 static bool
   3543 _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
   3544 {
   3545   /* The return value is ignored.  Build-ids are considered optional.  */
   3546   if (templ->xvec->flavour == bfd_target_elf_flavour)
   3547     return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
   3548       (templ, offset);
   3549   return false;
   3550 }
   3551 
   3552 bool
   3553 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
   3554 {
   3555   const struct elf_backend_data *bed;
   3556 
   3557   switch (hdr->p_type)
   3558     {
   3559     case PT_NULL:
   3560       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
   3561 
   3562     case PT_LOAD:
   3563       if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
   3564 	return false;
   3565       if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
   3566 	_bfd_elf_core_find_build_id (abfd, hdr->p_offset);
   3567       return true;
   3568 
   3569     case PT_DYNAMIC:
   3570       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
   3571 
   3572     case PT_INTERP:
   3573       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
   3574 
   3575     case PT_NOTE:
   3576       if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
   3577 	return false;
   3578       if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
   3579 			    hdr->p_align))
   3580 	return false;
   3581       return true;
   3582 
   3583     case PT_SHLIB:
   3584       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
   3585 
   3586     case PT_PHDR:
   3587       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
   3588 
   3589     case PT_GNU_EH_FRAME:
   3590       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
   3591 					      "eh_frame_hdr");
   3592 
   3593     case PT_GNU_STACK:
   3594       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
   3595 
   3596     case PT_GNU_RELRO:
   3597       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
   3598 
   3599     case PT_GNU_SFRAME:
   3600       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
   3601 					      "sframe");
   3602 
   3603     default:
   3604       /* Check for any processor-specific program segment types.  */
   3605       bed = get_elf_backend_data (abfd);
   3606       return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
   3607     }
   3608 }
   3609 
   3610 /* Return the REL_HDR for SEC, assuming there is only a single one, either
   3611    REL or RELA.  */
   3612 
   3613 Elf_Internal_Shdr *
   3614 _bfd_elf_single_rel_hdr (asection *sec)
   3615 {
   3616   if (elf_section_data (sec)->rel.hdr)
   3617     {
   3618       BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
   3619       return elf_section_data (sec)->rel.hdr;
   3620     }
   3621   else
   3622     return elf_section_data (sec)->rela.hdr;
   3623 }
   3624 
   3625 static bool
   3626 _bfd_elf_set_reloc_sh_name (bfd *abfd,
   3627 			    Elf_Internal_Shdr *rel_hdr,
   3628 			    const char *sec_name,
   3629 			    bool use_rela_p)
   3630 {
   3631   char *name = (char *) bfd_alloc (abfd,
   3632 				   sizeof ".rela" + strlen (sec_name));
   3633   if (name == NULL)
   3634     return false;
   3635 
   3636   sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
   3637   rel_hdr->sh_name =
   3638     (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
   3639 					false);
   3640   if (rel_hdr->sh_name == (unsigned int) -1)
   3641     return false;
   3642 
   3643   return true;
   3644 }
   3645 
   3646 /* Allocate and initialize a section-header for a new reloc section,
   3647    containing relocations against ASECT.  It is stored in RELDATA.  If
   3648    USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
   3649    relocations.  */
   3650 
   3651 static bool
   3652 _bfd_elf_init_reloc_shdr (bfd *abfd,
   3653 			  struct bfd_elf_section_reloc_data *reldata,
   3654 			  const char *sec_name,
   3655 			  bool use_rela_p,
   3656 			  bool delay_st_name_p)
   3657 {
   3658   Elf_Internal_Shdr *rel_hdr;
   3659   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3660 
   3661   BFD_ASSERT (reldata->hdr == NULL);
   3662   rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
   3663   if (rel_hdr == NULL)
   3664     return false;
   3665   reldata->hdr = rel_hdr;
   3666 
   3667   if (delay_st_name_p)
   3668     rel_hdr->sh_name = (unsigned int) -1;
   3669   else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
   3670 					use_rela_p))
   3671     return false;
   3672   rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
   3673   rel_hdr->sh_entsize = (use_rela_p
   3674 			 ? bed->s->sizeof_rela
   3675 			 : bed->s->sizeof_rel);
   3676   rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   3677   rel_hdr->sh_flags = 0;
   3678   rel_hdr->sh_addr = 0;
   3679   rel_hdr->sh_size = 0;
   3680   rel_hdr->sh_offset = 0;
   3681 
   3682   return true;
   3683 }
   3684 
   3685 /* Return the default section type based on the passed in section flags.  */
   3686 
   3687 int
   3688 bfd_elf_get_default_section_type (flagword flags)
   3689 {
   3690   if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
   3691       && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   3692     return SHT_NOBITS;
   3693   return SHT_PROGBITS;
   3694 }
   3695 
   3696 struct fake_section_arg
   3697 {
   3698   struct bfd_link_info *link_info;
   3699   bool failed;
   3700 };
   3701 
   3702 /* Set up an ELF internal section header for a section.  */
   3703 
   3704 static void
   3705 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
   3706 {
   3707   struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
   3708   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3709   struct bfd_elf_section_data *esd = elf_section_data (asect);
   3710   Elf_Internal_Shdr *this_hdr;
   3711   unsigned int sh_type;
   3712   const char *name = asect->name;
   3713   bool delay_st_name_p = false;
   3714   bfd_vma mask;
   3715 
   3716   if (arg->failed)
   3717     {
   3718       /* We already failed; just get out of the bfd_map_over_sections
   3719 	 loop.  */
   3720       return;
   3721     }
   3722 
   3723   this_hdr = &esd->this_hdr;
   3724 
   3725   /* ld: compress DWARF debug sections with names: .debug_*.  */
   3726   if (arg->link_info
   3727       && (abfd->flags & BFD_COMPRESS) != 0
   3728       && (asect->flags & SEC_DEBUGGING) != 0
   3729       && name[1] == 'd'
   3730       && name[6] == '_')
   3731     {
   3732       /* If this section will be compressed, delay adding section
   3733 	 name to section name section after it is compressed in
   3734 	 _bfd_elf_assign_file_positions_for_non_load.  */
   3735       delay_st_name_p = true;
   3736     }
   3737 
   3738   if (delay_st_name_p)
   3739     this_hdr->sh_name = (unsigned int) -1;
   3740   else
   3741     {
   3742       this_hdr->sh_name
   3743 	= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   3744 					      name, false);
   3745       if (this_hdr->sh_name == (unsigned int) -1)
   3746 	{
   3747 	  arg->failed = true;
   3748 	  return;
   3749 	}
   3750     }
   3751 
   3752   /* Don't clear sh_flags. Assembler may set additional bits.  */
   3753 
   3754   if ((asect->flags & SEC_ALLOC) != 0
   3755       || asect->user_set_vma)
   3756     this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
   3757   else
   3758     this_hdr->sh_addr = 0;
   3759 
   3760   this_hdr->sh_offset = 0;
   3761   this_hdr->sh_size = asect->size;
   3762   this_hdr->sh_link = 0;
   3763   /* PR 17512: file: 0eb809fe, 8b0535ee.  */
   3764   if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
   3765     {
   3766       _bfd_error_handler
   3767 	/* xgettext:c-format */
   3768 	(_("%pB: error: alignment power %d of section `%pA' is too big"),
   3769 	 abfd, asect->alignment_power, asect);
   3770       arg->failed = true;
   3771       return;
   3772     }
   3773   /* Set sh_addralign to the highest power of two given by alignment
   3774      consistent with the section VMA.  Linker scripts can force VMA.  */
   3775   mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
   3776   this_hdr->sh_addralign = mask & -mask;
   3777   /* The sh_entsize and sh_info fields may have been set already by
   3778      copy_private_section_data.  */
   3779 
   3780   this_hdr->bfd_section = asect;
   3781   this_hdr->contents = NULL;
   3782 
   3783   /* If the section type is unspecified, we set it based on
   3784      asect->flags.  */
   3785   if (asect->type != 0)
   3786     sh_type = asect->type;
   3787   else if ((asect->flags & SEC_GROUP) != 0)
   3788     sh_type = SHT_GROUP;
   3789   else
   3790     sh_type = bfd_elf_get_default_section_type (asect->flags);
   3791 
   3792   if (this_hdr->sh_type == SHT_NULL)
   3793     this_hdr->sh_type = sh_type;
   3794   else if (this_hdr->sh_type == SHT_NOBITS
   3795 	   && sh_type == SHT_PROGBITS
   3796 	   && (asect->flags & SEC_ALLOC) != 0)
   3797     {
   3798       /* Warn if we are changing a NOBITS section to PROGBITS, but
   3799 	 allow the link to proceed.  This can happen when users link
   3800 	 non-bss input sections to bss output sections, or emit data
   3801 	 to a bss output section via a linker script.  */
   3802       _bfd_error_handler
   3803 	(_("warning: section `%pA' type changed to PROGBITS"), asect);
   3804       this_hdr->sh_type = sh_type;
   3805     }
   3806 
   3807   switch (this_hdr->sh_type)
   3808     {
   3809     default:
   3810       break;
   3811 
   3812     case SHT_STRTAB:
   3813     case SHT_NOTE:
   3814     case SHT_NOBITS:
   3815     case SHT_PROGBITS:
   3816       break;
   3817 
   3818     case SHT_INIT_ARRAY:
   3819     case SHT_FINI_ARRAY:
   3820     case SHT_PREINIT_ARRAY:
   3821       this_hdr->sh_entsize = bed->s->arch_size / 8;
   3822       break;
   3823 
   3824     case SHT_HASH:
   3825       this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
   3826       break;
   3827 
   3828     case SHT_DYNSYM:
   3829       this_hdr->sh_entsize = bed->s->sizeof_sym;
   3830       break;
   3831 
   3832     case SHT_DYNAMIC:
   3833       this_hdr->sh_entsize = bed->s->sizeof_dyn;
   3834       break;
   3835 
   3836     case SHT_RELA:
   3837       if (get_elf_backend_data (abfd)->may_use_rela_p)
   3838 	this_hdr->sh_entsize = bed->s->sizeof_rela;
   3839       break;
   3840 
   3841      case SHT_REL:
   3842       if (get_elf_backend_data (abfd)->may_use_rel_p)
   3843 	this_hdr->sh_entsize = bed->s->sizeof_rel;
   3844       break;
   3845 
   3846      case SHT_GNU_versym:
   3847       this_hdr->sh_entsize = sizeof (Elf_External_Versym);
   3848       break;
   3849 
   3850      case SHT_GNU_verdef:
   3851       this_hdr->sh_entsize = 0;
   3852       /* objcopy or strip will copy over sh_info, but may not set
   3853 	 cverdefs.  The linker will set cverdefs, but sh_info will be
   3854 	 zero.  */
   3855       if (this_hdr->sh_info == 0)
   3856 	this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
   3857       else
   3858 	BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
   3859 		    || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
   3860       break;
   3861 
   3862     case SHT_GNU_verneed:
   3863       this_hdr->sh_entsize = 0;
   3864       /* objcopy or strip will copy over sh_info, but may not set
   3865 	 cverrefs.  The linker will set cverrefs, but sh_info will be
   3866 	 zero.  */
   3867       if (this_hdr->sh_info == 0)
   3868 	this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
   3869       else
   3870 	BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
   3871 		    || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
   3872       break;
   3873 
   3874     case SHT_GROUP:
   3875       this_hdr->sh_entsize = GRP_ENTRY_SIZE;
   3876       break;
   3877 
   3878     case SHT_GNU_HASH:
   3879       this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
   3880       break;
   3881     }
   3882 
   3883   if ((asect->flags & SEC_ALLOC) != 0)
   3884     this_hdr->sh_flags |= SHF_ALLOC;
   3885   if ((asect->flags & SEC_READONLY) == 0)
   3886     this_hdr->sh_flags |= SHF_WRITE;
   3887   if ((asect->flags & SEC_CODE) != 0)
   3888     this_hdr->sh_flags |= SHF_EXECINSTR;
   3889   if ((asect->flags & SEC_MERGE) != 0)
   3890     {
   3891       this_hdr->sh_flags |= SHF_MERGE;
   3892       this_hdr->sh_entsize = asect->entsize;
   3893     }
   3894   if ((asect->flags & SEC_STRINGS) != 0)
   3895     this_hdr->sh_flags |= SHF_STRINGS;
   3896   if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
   3897     this_hdr->sh_flags |= SHF_GROUP;
   3898   if ((asect->flags & SEC_THREAD_LOCAL) != 0)
   3899     {
   3900       this_hdr->sh_flags |= SHF_TLS;
   3901       if (asect->size == 0
   3902 	  && (asect->flags & SEC_HAS_CONTENTS) == 0)
   3903 	{
   3904 	  struct bfd_link_order *o = asect->map_tail.link_order;
   3905 
   3906 	  this_hdr->sh_size = 0;
   3907 	  if (o != NULL)
   3908 	    {
   3909 	      this_hdr->sh_size = o->offset + o->size;
   3910 	      if (this_hdr->sh_size != 0)
   3911 		this_hdr->sh_type = SHT_NOBITS;
   3912 	    }
   3913 	}
   3914     }
   3915   if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
   3916     this_hdr->sh_flags |= SHF_EXCLUDE;
   3917 
   3918   /* If the section has relocs, set up a section header for the
   3919      SHT_REL[A] section.  If two relocation sections are required for
   3920      this section, it is up to the processor-specific back-end to
   3921      create the other.  */
   3922   if ((asect->flags & SEC_RELOC) != 0)
   3923     {
   3924       /* When doing a relocatable link, create both REL and RELA sections if
   3925 	 needed.  */
   3926       if (arg->link_info
   3927 	  /* Do the normal setup if we wouldn't create any sections here.  */
   3928 	  && esd->rel.count + esd->rela.count > 0
   3929 	  && (bfd_link_relocatable (arg->link_info)
   3930 	      || arg->link_info->emitrelocations))
   3931 	{
   3932 	  if (esd->rel.count && esd->rel.hdr == NULL
   3933 	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
   3934 					    false, delay_st_name_p))
   3935 	    {
   3936 	      arg->failed = true;
   3937 	      return;
   3938 	    }
   3939 	  if (esd->rela.count && esd->rela.hdr == NULL
   3940 	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
   3941 					    true, delay_st_name_p))
   3942 	    {
   3943 	      arg->failed = true;
   3944 	      return;
   3945 	    }
   3946 	}
   3947       else if (!_bfd_elf_init_reloc_shdr (abfd,
   3948 					  (asect->use_rela_p
   3949 					   ? &esd->rela : &esd->rel),
   3950 					  name,
   3951 					  asect->use_rela_p,
   3952 					  delay_st_name_p))
   3953 	{
   3954 	  arg->failed = true;
   3955 	  return;
   3956 	}
   3957     }
   3958 
   3959   /* Check for processor-specific section types.  */
   3960   sh_type = this_hdr->sh_type;
   3961   if (bed->elf_backend_fake_sections
   3962       && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
   3963     {
   3964       arg->failed = true;
   3965       return;
   3966     }
   3967 
   3968   if (sh_type == SHT_NOBITS && asect->size != 0)
   3969     {
   3970       /* Don't change the header type from NOBITS if we are being
   3971 	 called for objcopy --only-keep-debug.  */
   3972       this_hdr->sh_type = sh_type;
   3973     }
   3974 }
   3975 
   3976 /* Fill in the contents of a SHT_GROUP section.  Called from
   3977    _bfd_elf_compute_section_file_positions for gas, objcopy, and
   3978    when ELF targets use the generic linker, ld.  Called for ld -r
   3979    from bfd_elf_final_link.  */
   3980 
   3981 void
   3982 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
   3983 {
   3984   bool *failedptr = (bool *) failedptrarg;
   3985   asection *elt, *first;
   3986   unsigned char *loc;
   3987   bool gas;
   3988 
   3989   /* Ignore linker created group section.  See elfNN_ia64_object_p in
   3990      elfxx-ia64.c.  */
   3991   if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
   3992       || sec->size == 0
   3993       || *failedptr)
   3994     return;
   3995 
   3996   if (elf_section_data (sec)->this_hdr.sh_info == 0)
   3997     {
   3998       unsigned long symindx = 0;
   3999 
   4000       /* elf_group_id will have been set up by objcopy and the
   4001 	 generic linker.  */
   4002       if (elf_group_id (sec) != NULL)
   4003 	symindx = elf_group_id (sec)->udata.i;
   4004 
   4005       if (symindx == 0)
   4006 	{
   4007 	  /* If called from the assembler, swap_out_syms will have set up
   4008 	     elf_section_syms.
   4009 	     PR 25699: A corrupt input file could contain bogus group info.  */
   4010 	  if (sec->index >= elf_num_section_syms (abfd)
   4011 	      || elf_section_syms (abfd)[sec->index] == NULL)
   4012 	    {
   4013 	      *failedptr = true;
   4014 	      return;
   4015 	    }
   4016 	  symindx = elf_section_syms (abfd)[sec->index]->udata.i;
   4017 	}
   4018       elf_section_data (sec)->this_hdr.sh_info = symindx;
   4019     }
   4020   else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
   4021     {
   4022       /* The ELF backend linker sets sh_info to -2 when the group
   4023 	 signature symbol is global, and thus the index can't be
   4024 	 set until all local symbols are output.  */
   4025       asection *igroup;
   4026       struct bfd_elf_section_data *sec_data;
   4027       unsigned long symndx;
   4028       unsigned long extsymoff;
   4029       struct elf_link_hash_entry *h;
   4030 
   4031       /* The point of this little dance to the first SHF_GROUP section
   4032 	 then back to the SHT_GROUP section is that this gets us to
   4033 	 the SHT_GROUP in the input object.  */
   4034       igroup = elf_sec_group (elf_next_in_group (sec));
   4035       sec_data = elf_section_data (igroup);
   4036       symndx = sec_data->this_hdr.sh_info;
   4037       extsymoff = 0;
   4038       if (!elf_bad_symtab (igroup->owner))
   4039 	{
   4040 	  Elf_Internal_Shdr *symtab_hdr;
   4041 
   4042 	  symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
   4043 	  extsymoff = symtab_hdr->sh_info;
   4044 	}
   4045       h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
   4046       while (h->root.type == bfd_link_hash_indirect
   4047 	     || h->root.type == bfd_link_hash_warning)
   4048 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4049 
   4050       elf_section_data (sec)->this_hdr.sh_info = h->indx;
   4051     }
   4052 
   4053   /* The contents won't be allocated for "ld -r" or objcopy.  */
   4054   gas = true;
   4055   if (sec->contents == NULL)
   4056     {
   4057       gas = false;
   4058       sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
   4059 
   4060       /* Arrange for the section to be written out.  */
   4061       elf_section_data (sec)->this_hdr.contents = sec->contents;
   4062       if (sec->contents == NULL)
   4063 	{
   4064 	  *failedptr = true;
   4065 	  return;
   4066 	}
   4067     }
   4068 
   4069   loc = sec->contents + sec->size;
   4070 
   4071   /* Get the pointer to the first section in the group that gas
   4072      squirreled away here.  objcopy arranges for this to be set to the
   4073      start of the input section group.  */
   4074   first = elt = elf_next_in_group (sec);
   4075 
   4076   /* First element is a flag word.  Rest of section is elf section
   4077      indices for all the sections of the group.  Write them backwards
   4078      just to keep the group in the same order as given in .section
   4079      directives, not that it matters.  */
   4080   while (elt != NULL)
   4081     {
   4082       asection *s;
   4083 
   4084       s = elt;
   4085       if (!gas)
   4086 	s = s->output_section;
   4087       if (s != NULL
   4088 	  && !bfd_is_abs_section (s))
   4089 	{
   4090 	  struct bfd_elf_section_data *elf_sec = elf_section_data (s);
   4091 	  struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
   4092 
   4093 	  if (elf_sec->rel.hdr != NULL
   4094 	      && (gas
   4095 		  || (input_elf_sec->rel.hdr != NULL
   4096 		      && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
   4097 	    {
   4098 	      elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
   4099 	      loc -= 4;
   4100 	      if (loc == sec->contents)
   4101 		break;
   4102 	      H_PUT_32 (abfd, elf_sec->rel.idx, loc);
   4103 	    }
   4104 	  if (elf_sec->rela.hdr != NULL
   4105 	      && (gas
   4106 		  || (input_elf_sec->rela.hdr != NULL
   4107 		      && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
   4108 	    {
   4109 	      elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
   4110 	      loc -= 4;
   4111 	      if (loc == sec->contents)
   4112 		break;
   4113 	      H_PUT_32 (abfd, elf_sec->rela.idx, loc);
   4114 	    }
   4115 	  loc -= 4;
   4116 	  if (loc == sec->contents)
   4117 	    break;
   4118 	  H_PUT_32 (abfd, elf_sec->this_idx, loc);
   4119 	}
   4120       elt = elf_next_in_group (elt);
   4121       if (elt == first)
   4122 	break;
   4123     }
   4124 
   4125   /* We should always get here with loc == sec->contents + 4, but it is
   4126      possible to craft bogus SHT_GROUP sections that will cause segfaults
   4127      in objcopy without checking loc here and in the loop above.  */
   4128   if (loc == sec->contents)
   4129     BFD_ASSERT (0);
   4130   else
   4131     {
   4132       loc -= 4;
   4133       if (loc != sec->contents)
   4134 	{
   4135 	  BFD_ASSERT (0);
   4136 	  memset (sec->contents + 4, 0, loc - sec->contents);
   4137 	  loc = sec->contents;
   4138 	}
   4139     }
   4140 
   4141   H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
   4142 }
   4143 
   4144 /* Given NAME, the name of a relocation section stripped of its
   4145    .rel/.rela prefix, return the section in ABFD to which the
   4146    relocations apply.  */
   4147 
   4148 asection *
   4149 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
   4150 {
   4151   /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
   4152      section likely apply to .got.plt or .got section.  */
   4153   if (get_elf_backend_data (abfd)->want_got_plt
   4154       && strcmp (name, ".plt") == 0)
   4155     {
   4156       asection *sec;
   4157 
   4158       name = ".got.plt";
   4159       sec = bfd_get_section_by_name (abfd, name);
   4160       if (sec != NULL)
   4161 	return sec;
   4162       name = ".got";
   4163     }
   4164 
   4165   return bfd_get_section_by_name (abfd, name);
   4166 }
   4167 
   4168 /* Return the section to which RELOC_SEC applies.  */
   4169 
   4170 static asection *
   4171 elf_get_reloc_section (asection *reloc_sec)
   4172 {
   4173   const char *name;
   4174   unsigned int type;
   4175   bfd *abfd;
   4176   const struct elf_backend_data *bed;
   4177 
   4178   type = elf_section_data (reloc_sec)->this_hdr.sh_type;
   4179   if (type != SHT_REL && type != SHT_RELA)
   4180     return NULL;
   4181 
   4182   /* We look up the section the relocs apply to by name.  */
   4183   name = reloc_sec->name;
   4184   if (!startswith (name, ".rel"))
   4185     return NULL;
   4186   name += 4;
   4187   if (type == SHT_RELA && *name++ != 'a')
   4188     return NULL;
   4189 
   4190   abfd = reloc_sec->owner;
   4191   bed = get_elf_backend_data (abfd);
   4192   return bed->get_reloc_section (abfd, name);
   4193 }
   4194 
   4195 /* Assign all ELF section numbers.  The dummy first section is handled here
   4196    too.  The link/info pointers for the standard section types are filled
   4197    in here too, while we're at it.  LINK_INFO will be 0 when arriving
   4198    here for gas, objcopy, and when using the generic ELF linker.  */
   4199 
   4200 static bool
   4201 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
   4202 {
   4203   struct elf_obj_tdata *t = elf_tdata (abfd);
   4204   asection *sec;
   4205   unsigned int section_number;
   4206   Elf_Internal_Shdr **i_shdrp;
   4207   struct bfd_elf_section_data *d;
   4208   bool need_symtab;
   4209   size_t amt;
   4210 
   4211   section_number = 1;
   4212 
   4213   _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
   4214 
   4215   /* SHT_GROUP sections are in relocatable files only.  */
   4216   if (link_info == NULL || !link_info->resolve_section_groups)
   4217     {
   4218       size_t reloc_count = 0;
   4219 
   4220       /* Put SHT_GROUP sections first.  */
   4221       for (sec = abfd->sections; sec != NULL; sec = sec->next)
   4222 	{
   4223 	  d = elf_section_data (sec);
   4224 
   4225 	  if (d->this_hdr.sh_type == SHT_GROUP)
   4226 	    {
   4227 	      if (sec->flags & SEC_LINKER_CREATED)
   4228 		{
   4229 		  /* Remove the linker created SHT_GROUP sections.  */
   4230 		  bfd_section_list_remove (abfd, sec);
   4231 		  abfd->section_count--;
   4232 		}
   4233 	      else
   4234 		d->this_idx = section_number++;
   4235 	    }
   4236 
   4237 	  /* Count relocations.  */
   4238 	  reloc_count += sec->reloc_count;
   4239 	}
   4240 
   4241       /* Set/clear HAS_RELOC depending on whether there are relocations.  */
   4242       if (reloc_count == 0)
   4243 	abfd->flags &= ~HAS_RELOC;
   4244       else
   4245 	abfd->flags |= HAS_RELOC;
   4246     }
   4247 
   4248   for (sec = abfd->sections; sec; sec = sec->next)
   4249     {
   4250       d = elf_section_data (sec);
   4251 
   4252       if (d->this_hdr.sh_type != SHT_GROUP)
   4253 	d->this_idx = section_number++;
   4254       if (d->this_hdr.sh_name != (unsigned int) -1)
   4255 	_bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
   4256       if (d->rel.hdr)
   4257 	{
   4258 	  d->rel.idx = section_number++;
   4259 	  if (d->rel.hdr->sh_name != (unsigned int) -1)
   4260 	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
   4261 	}
   4262       else
   4263 	d->rel.idx = 0;
   4264 
   4265       if (d->rela.hdr)
   4266 	{
   4267 	  d->rela.idx = section_number++;
   4268 	  if (d->rela.hdr->sh_name != (unsigned int) -1)
   4269 	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
   4270 	}
   4271       else
   4272 	d->rela.idx = 0;
   4273     }
   4274 
   4275   need_symtab = (bfd_get_symcount (abfd) > 0
   4276 		 || (link_info == NULL
   4277 		     && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
   4278 			 == HAS_RELOC)));
   4279   if (need_symtab)
   4280     {
   4281       elf_onesymtab (abfd) = section_number++;
   4282       _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
   4283       if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
   4284 	{
   4285 	  elf_section_list *entry;
   4286 
   4287 	  BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
   4288 
   4289 	  entry = bfd_zalloc (abfd, sizeof (*entry));
   4290 	  entry->ndx = section_number++;
   4291 	  elf_symtab_shndx_list (abfd) = entry;
   4292 	  entry->hdr.sh_name
   4293 	    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   4294 						  ".symtab_shndx", false);
   4295 	  if (entry->hdr.sh_name == (unsigned int) -1)
   4296 	    return false;
   4297 	}
   4298       elf_strtab_sec (abfd) = section_number++;
   4299       _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
   4300     }
   4301 
   4302   elf_shstrtab_sec (abfd) = section_number++;
   4303   _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
   4304   elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
   4305 
   4306   if (section_number >= SHN_LORESERVE)
   4307     {
   4308       /* xgettext:c-format */
   4309       _bfd_error_handler (_("%pB: too many sections: %u"),
   4310 			  abfd, section_number);
   4311       return false;
   4312     }
   4313 
   4314   elf_numsections (abfd) = section_number;
   4315   elf_elfheader (abfd)->e_shnum = section_number;
   4316 
   4317   /* Set up the list of section header pointers, in agreement with the
   4318      indices.  */
   4319   amt = section_number * sizeof (Elf_Internal_Shdr *);
   4320   i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
   4321   if (i_shdrp == NULL)
   4322     return false;
   4323 
   4324   i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
   4325 						 sizeof (Elf_Internal_Shdr));
   4326   if (i_shdrp[0] == NULL)
   4327     {
   4328       bfd_release (abfd, i_shdrp);
   4329       return false;
   4330     }
   4331 
   4332   elf_elfsections (abfd) = i_shdrp;
   4333 
   4334   i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
   4335   if (need_symtab)
   4336     {
   4337       i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
   4338       if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
   4339 	{
   4340 	  elf_section_list * entry = elf_symtab_shndx_list (abfd);
   4341 	  BFD_ASSERT (entry != NULL);
   4342 	  i_shdrp[entry->ndx] = & entry->hdr;
   4343 	  entry->hdr.sh_link = elf_onesymtab (abfd);
   4344 	}
   4345       i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
   4346       t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
   4347     }
   4348 
   4349   for (sec = abfd->sections; sec; sec = sec->next)
   4350     {
   4351       asection *s;
   4352 
   4353       d = elf_section_data (sec);
   4354 
   4355       i_shdrp[d->this_idx] = &d->this_hdr;
   4356       if (d->rel.idx != 0)
   4357 	i_shdrp[d->rel.idx] = d->rel.hdr;
   4358       if (d->rela.idx != 0)
   4359 	i_shdrp[d->rela.idx] = d->rela.hdr;
   4360 
   4361       /* Fill in the sh_link and sh_info fields while we're at it.  */
   4362 
   4363       /* sh_link of a reloc section is the section index of the symbol
   4364 	 table.  sh_info is the section index of the section to which
   4365 	 the relocation entries apply.  */
   4366       if (d->rel.idx != 0)
   4367 	{
   4368 	  d->rel.hdr->sh_link = elf_onesymtab (abfd);
   4369 	  d->rel.hdr->sh_info = d->this_idx;
   4370 	  d->rel.hdr->sh_flags |= SHF_INFO_LINK;
   4371 	}
   4372       if (d->rela.idx != 0)
   4373 	{
   4374 	  d->rela.hdr->sh_link = elf_onesymtab (abfd);
   4375 	  d->rela.hdr->sh_info = d->this_idx;
   4376 	  d->rela.hdr->sh_flags |= SHF_INFO_LINK;
   4377 	}
   4378 
   4379       /* We need to set up sh_link for SHF_LINK_ORDER.  */
   4380       if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
   4381 	{
   4382 	  s = elf_linked_to_section (sec);
   4383 	  /* We can now have a NULL linked section pointer.
   4384 	     This happens when the sh_link field is 0, which is done
   4385 	     when a linked to section is discarded but the linking
   4386 	     section has been retained for some reason.  */
   4387 	  if (s)
   4388 	    {
   4389 	      /* Check discarded linkonce section.  */
   4390 	      if (discarded_section (s))
   4391 		{
   4392 		  asection *kept;
   4393 		  _bfd_error_handler
   4394 		    /* xgettext:c-format */
   4395 		    (_("%pB: sh_link of section `%pA' points to"
   4396 		       " discarded section `%pA' of `%pB'"),
   4397 		     abfd, d->this_hdr.bfd_section, s, s->owner);
   4398 		  /* Point to the kept section if it has the same
   4399 		     size as the discarded one.  */
   4400 		  kept = _bfd_elf_check_kept_section (s, link_info);
   4401 		  if (kept == NULL)
   4402 		    {
   4403 		      bfd_set_error (bfd_error_bad_value);
   4404 		      return false;
   4405 		    }
   4406 		  s = kept;
   4407 		}
   4408 	      /* Handle objcopy. */
   4409 	      else if (s->output_section == NULL)
   4410 		{
   4411 		  _bfd_error_handler
   4412 		    /* xgettext:c-format */
   4413 		    (_("%pB: sh_link of section `%pA' points to"
   4414 		       " removed section `%pA' of `%pB'"),
   4415 		     abfd, d->this_hdr.bfd_section, s, s->owner);
   4416 		  bfd_set_error (bfd_error_bad_value);
   4417 		  return false;
   4418 		}
   4419 	      s = s->output_section;
   4420 	      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   4421 	    }
   4422 	}
   4423 
   4424       switch (d->this_hdr.sh_type)
   4425 	{
   4426 	case SHT_REL:
   4427 	case SHT_RELA:
   4428 	  /* sh_link is the section index of the symbol table.
   4429 	     sh_info is the section index of the section to which the
   4430 	     relocation entries apply.  */
   4431 	  if (d->this_hdr.sh_link == 0)
   4432 	    {
   4433 	      /* FIXME maybe: If this is a reloc section which we are
   4434 		 treating as a normal section then we likely should
   4435 		 not be assuming its sh_link is .dynsym or .symtab.  */
   4436 	      if ((sec->flags & SEC_ALLOC) != 0)
   4437 		{
   4438 		  s = bfd_get_section_by_name (abfd, ".dynsym");
   4439 		  if (s != NULL)
   4440 		    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   4441 		}
   4442 	      else
   4443 		d->this_hdr.sh_link = elf_onesymtab (abfd);
   4444 	    }
   4445 
   4446 	  s = elf_get_reloc_section (sec);
   4447 	  if (s != NULL)
   4448 	    {
   4449 	      d->this_hdr.sh_info = elf_section_data (s)->this_idx;
   4450 	      d->this_hdr.sh_flags |= SHF_INFO_LINK;
   4451 	    }
   4452 	  break;
   4453 
   4454 	case SHT_STRTAB:
   4455 	  /* We assume that a section named .stab*str is a stabs
   4456 	     string section.  We look for a section with the same name
   4457 	     but without the trailing ``str'', and set its sh_link
   4458 	     field to point to this section.  */
   4459 	  if (startswith (sec->name, ".stab")
   4460 	      && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
   4461 	    {
   4462 	      size_t len;
   4463 	      char *alc;
   4464 
   4465 	      len = strlen (sec->name);
   4466 	      alc = (char *) bfd_malloc (len - 2);
   4467 	      if (alc == NULL)
   4468 		return false;
   4469 	      memcpy (alc, sec->name, len - 3);
   4470 	      alc[len - 3] = '\0';
   4471 	      s = bfd_get_section_by_name (abfd, alc);
   4472 	      free (alc);
   4473 	      if (s != NULL)
   4474 		{
   4475 		  elf_section_data (s)->this_hdr.sh_link = d->this_idx;
   4476 
   4477 		  /* This is a .stab section.  */
   4478 		  elf_section_data (s)->this_hdr.sh_entsize = 12;
   4479 		}
   4480 	    }
   4481 	  break;
   4482 
   4483 	case SHT_DYNAMIC:
   4484 	case SHT_DYNSYM:
   4485 	case SHT_GNU_verneed:
   4486 	case SHT_GNU_verdef:
   4487 	  /* sh_link is the section header index of the string table
   4488 	     used for the dynamic entries, or the symbol table, or the
   4489 	     version strings.  */
   4490 	  s = bfd_get_section_by_name (abfd, ".dynstr");
   4491 	  if (s != NULL)
   4492 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   4493 	  break;
   4494 
   4495 	case SHT_GNU_LIBLIST:
   4496 	  /* sh_link is the section header index of the prelink library
   4497 	     list used for the dynamic entries, or the symbol table, or
   4498 	     the version strings.  */
   4499 	  s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
   4500 					      ? ".dynstr" : ".gnu.libstr"));
   4501 	  if (s != NULL)
   4502 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   4503 	  break;
   4504 
   4505 	case SHT_HASH:
   4506 	case SHT_GNU_HASH:
   4507 	case SHT_GNU_versym:
   4508 	  /* sh_link is the section header index of the symbol table
   4509 	     this hash table or version table is for.  */
   4510 	  s = bfd_get_section_by_name (abfd, ".dynsym");
   4511 	  if (s != NULL)
   4512 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   4513 	  break;
   4514 
   4515 	case SHT_GROUP:
   4516 	  d->this_hdr.sh_link = elf_onesymtab (abfd);
   4517 	}
   4518     }
   4519 
   4520   /* Delay setting sh_name to _bfd_elf_write_object_contents so that
   4521      _bfd_elf_assign_file_positions_for_non_load can convert DWARF
   4522      debug section name from .debug_* to .zdebug_* if needed.  */
   4523 
   4524   return true;
   4525 }
   4526 
   4527 static bool
   4528 sym_is_global (bfd *abfd, asymbol *sym)
   4529 {
   4530   /* If the backend has a special mapping, use it.  */
   4531   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4532   if (bed->elf_backend_sym_is_global)
   4533     return (*bed->elf_backend_sym_is_global) (abfd, sym);
   4534 
   4535   return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
   4536 	  || bfd_is_und_section (bfd_asymbol_section (sym))
   4537 	  || bfd_is_com_section (bfd_asymbol_section (sym)));
   4538 }
   4539 
   4540 /* Filter global symbols of ABFD to include in the import library.  All
   4541    SYMCOUNT symbols of ABFD can be examined from their pointers in
   4542    SYMS.  Pointers of symbols to keep should be stored contiguously at
   4543    the beginning of that array.
   4544 
   4545    Returns the number of symbols to keep.  */
   4546 
   4547 unsigned int
   4548 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
   4549 				asymbol **syms, long symcount)
   4550 {
   4551   long src_count, dst_count = 0;
   4552 
   4553   for (src_count = 0; src_count < symcount; src_count++)
   4554     {
   4555       asymbol *sym = syms[src_count];
   4556       char *name = (char *) bfd_asymbol_name (sym);
   4557       struct bfd_link_hash_entry *h;
   4558 
   4559       if (!sym_is_global (abfd, sym))
   4560 	continue;
   4561 
   4562       h = bfd_link_hash_lookup (info->hash, name, false, false, false);
   4563       if (h == NULL)
   4564 	continue;
   4565       if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
   4566 	continue;
   4567       if (h->linker_def || h->ldscript_def)
   4568 	continue;
   4569 
   4570       syms[dst_count++] = sym;
   4571     }
   4572 
   4573   syms[dst_count] = NULL;
   4574 
   4575   return dst_count;
   4576 }
   4577 
   4578 /* Don't output symbols for sections that are not going to be output,
   4579    that are duplicates or there is no BFD section.  */
   4580 
   4581 static bool
   4582 ignore_sym (asymbol *sym)
   4583 {
   4584   if (sym == NULL)
   4585     return false;
   4586 
   4587   if (sym->section == NULL)
   4588     return true;
   4589 
   4590   if ((sym->flags & BSF_SECTION_SYM) != 0)
   4591     {
   4592       if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
   4593 	return true;
   4594       /* With ld -r on generic elf targets it is possible to have
   4595 	 multiple section symbols in the output for a given section.
   4596 	 We'd like to get rid of all but the first one.  This drops
   4597 	 them if the first input section is non-zero size, but fails
   4598 	 to do so if the first input section is zero sized.  */
   4599       if (sym->section->output_offset != 0)
   4600 	return true;
   4601     }
   4602 
   4603   return discarded_section (sym->section);
   4604 }
   4605 
   4606 /* Map symbol from it's internal number to the external number, moving
   4607    all local symbols to be at the head of the list.  */
   4608 
   4609 static bool
   4610 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
   4611 {
   4612   unsigned int symcount = bfd_get_symcount (abfd);
   4613   asymbol **syms = bfd_get_outsymbols (abfd);
   4614   asymbol **sect_syms;
   4615   unsigned int num_locals = 0;
   4616   unsigned int num_globals = 0;
   4617   unsigned int max_index = 0;
   4618   unsigned int idx;
   4619   asection *asect;
   4620   asymbol **new_syms;
   4621   size_t amt;
   4622 
   4623 #ifdef DEBUG
   4624   fprintf (stderr, "elf_map_symbols\n");
   4625   fflush (stderr);
   4626 #endif
   4627 
   4628   for (asect = abfd->sections; asect; asect = asect->next)
   4629     {
   4630       if (max_index < asect->index)
   4631 	max_index = asect->index;
   4632     }
   4633 
   4634   max_index++;
   4635   amt = max_index * sizeof (asymbol *);
   4636   sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
   4637   if (sect_syms == NULL)
   4638     return false;
   4639   elf_section_syms (abfd) = sect_syms;
   4640   elf_num_section_syms (abfd) = max_index;
   4641 
   4642   /* Init sect_syms entries for any section symbols we have already
   4643      decided to output.  */
   4644   for (idx = 0; idx < symcount; idx++)
   4645     {
   4646       asymbol *sym = syms[idx];
   4647 
   4648       if ((sym->flags & BSF_SECTION_SYM) != 0
   4649 	  && sym->value == 0
   4650 	  && !ignore_sym (sym)
   4651 	  && !bfd_is_abs_section (sym->section))
   4652 	{
   4653 	  asection *sec = sym->section;
   4654 
   4655 	  if (sec->owner != abfd)
   4656 	    sec = sec->output_section;
   4657 
   4658 	  sect_syms[sec->index] = syms[idx];
   4659 	}
   4660     }
   4661 
   4662   /* Classify all of the symbols.  */
   4663   for (idx = 0; idx < symcount; idx++)
   4664     {
   4665       if (ignore_sym (syms[idx]))
   4666 	continue;
   4667       if (sym_is_global (abfd, syms[idx]))
   4668 	num_globals++;
   4669       else
   4670 	num_locals++;
   4671     }
   4672 
   4673   /* We will be adding a section symbol for each normal BFD section.  Most
   4674      sections will already have a section symbol in outsymbols, but
   4675      eg. SHT_GROUP sections will not, and we need the section symbol mapped
   4676      at least in that case.  */
   4677   for (asect = abfd->sections; asect; asect = asect->next)
   4678     {
   4679       asymbol *sym = asect->symbol;
   4680       /* Don't include ignored section symbols.  */
   4681       if (!ignore_sym (sym)
   4682 	  && sect_syms[asect->index] == NULL)
   4683 	{
   4684 	  if (sym_is_global (abfd, asect->symbol))
   4685 	    num_globals++;
   4686 	  else
   4687 	    num_locals++;
   4688 	}
   4689     }
   4690 
   4691   /* Now sort the symbols so the local symbols are first.  */
   4692   amt = (num_locals + num_globals) * sizeof (asymbol *);
   4693   new_syms = (asymbol **) bfd_alloc (abfd, amt);
   4694   if (new_syms == NULL)
   4695     return false;
   4696 
   4697   unsigned int num_globals2 = 0;
   4698   unsigned int num_locals2 = 0;
   4699   for (idx = 0; idx < symcount; idx++)
   4700     {
   4701       asymbol *sym = syms[idx];
   4702       unsigned int i;
   4703 
   4704       if (ignore_sym (sym))
   4705 	continue;
   4706 
   4707       if (sym_is_global (abfd, sym))
   4708 	i = num_locals + num_globals2++;
   4709       else
   4710 	i = num_locals2++;
   4711       new_syms[i] = sym;
   4712       sym->udata.i = i + 1;
   4713     }
   4714   for (asect = abfd->sections; asect; asect = asect->next)
   4715     {
   4716       asymbol *sym = asect->symbol;
   4717       if (!ignore_sym (sym)
   4718 	  && sect_syms[asect->index] == NULL)
   4719 	{
   4720 	  unsigned int i;
   4721 
   4722 	  sect_syms[asect->index] = sym;
   4723 	  if (sym_is_global (abfd, sym))
   4724 	    i = num_locals + num_globals2++;
   4725 	  else
   4726 	    i = num_locals2++;
   4727 	  new_syms[i] = sym;
   4728 	  sym->udata.i = i + 1;
   4729 	}
   4730     }
   4731 
   4732   bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
   4733 
   4734   *pnum_locals = num_locals;
   4735   return true;
   4736 }
   4737 
   4738 /* Align to the maximum file alignment that could be required for any
   4739    ELF data structure.  */
   4740 
   4741 static inline file_ptr
   4742 align_file_position (file_ptr off, int align)
   4743 {
   4744   return (off + align - 1) & ~(align - 1);
   4745 }
   4746 
   4747 /* Assign a file position to a section, optionally aligning to the
   4748    required section alignment.  */
   4749 
   4750 file_ptr
   4751 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
   4752 					   file_ptr offset,
   4753 					   bool align)
   4754 {
   4755   if (align && i_shdrp->sh_addralign > 1)
   4756     offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
   4757   i_shdrp->sh_offset = offset;
   4758   if (i_shdrp->bfd_section != NULL)
   4759     i_shdrp->bfd_section->filepos = offset;
   4760   if (i_shdrp->sh_type != SHT_NOBITS)
   4761     offset += i_shdrp->sh_size;
   4762   return offset;
   4763 }
   4764 
   4765 /* Compute the file positions we are going to put the sections at, and
   4766    otherwise prepare to begin writing out the ELF file.  If LINK_INFO
   4767    is not NULL, this is being called by the ELF backend linker.  */
   4768 
   4769 bool
   4770 _bfd_elf_compute_section_file_positions (bfd *abfd,
   4771 					 struct bfd_link_info *link_info)
   4772 {
   4773   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4774   struct fake_section_arg fsargs;
   4775   bool failed;
   4776   struct elf_strtab_hash *strtab = NULL;
   4777   Elf_Internal_Shdr *shstrtab_hdr;
   4778   bool need_symtab;
   4779 
   4780   if (abfd->output_has_begun)
   4781     return true;
   4782 
   4783   /* Do any elf backend specific processing first.  */
   4784   if (bed->elf_backend_begin_write_processing)
   4785     (*bed->elf_backend_begin_write_processing) (abfd, link_info);
   4786 
   4787   if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
   4788     return false;
   4789 
   4790   fsargs.failed = false;
   4791   fsargs.link_info = link_info;
   4792   bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
   4793   if (fsargs.failed)
   4794     return false;
   4795 
   4796   if (!assign_section_numbers (abfd, link_info))
   4797     return false;
   4798 
   4799   /* The backend linker builds symbol table information itself.  */
   4800   need_symtab = (link_info == NULL
   4801 		 && (bfd_get_symcount (abfd) > 0
   4802 		     || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
   4803 			 == HAS_RELOC)));
   4804   if (need_symtab)
   4805     {
   4806       /* Non-zero if doing a relocatable link.  */
   4807       int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
   4808 
   4809       if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
   4810 	return false;
   4811     }
   4812 
   4813   failed = false;
   4814   if (link_info == NULL)
   4815     {
   4816       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
   4817       if (failed)
   4818 	goto err_free_strtab;
   4819     }
   4820 
   4821   shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
   4822   /* sh_name was set in init_file_header.  */
   4823   shstrtab_hdr->sh_type = SHT_STRTAB;
   4824   shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   4825   shstrtab_hdr->sh_addr = 0;
   4826   /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
   4827   shstrtab_hdr->sh_entsize = 0;
   4828   shstrtab_hdr->sh_link = 0;
   4829   shstrtab_hdr->sh_info = 0;
   4830   /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
   4831   shstrtab_hdr->sh_addralign = 1;
   4832 
   4833   if (!assign_file_positions_except_relocs (abfd, link_info))
   4834     goto err_free_strtab;
   4835 
   4836   if (strtab != NULL)
   4837     {
   4838       file_ptr off;
   4839       Elf_Internal_Shdr *hdr;
   4840 
   4841       off = elf_next_file_pos (abfd);
   4842 
   4843       hdr = & elf_symtab_hdr (abfd);
   4844       off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   4845 
   4846       if (elf_symtab_shndx_list (abfd) != NULL)
   4847 	{
   4848 	  hdr = & elf_symtab_shndx_list (abfd)->hdr;
   4849 	  if (hdr->sh_size != 0)
   4850 	    off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   4851 	  /* FIXME: What about other symtab_shndx sections in the list ?  */
   4852 	}
   4853 
   4854       hdr = &elf_tdata (abfd)->strtab_hdr;
   4855       off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   4856 
   4857       elf_next_file_pos (abfd) = off;
   4858 
   4859       /* Now that we know where the .strtab section goes, write it
   4860 	 out.  */
   4861       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   4862 	  || ! _bfd_elf_strtab_emit (abfd, strtab))
   4863 	goto err_free_strtab;
   4864       _bfd_elf_strtab_free (strtab);
   4865     }
   4866 
   4867   abfd->output_has_begun = true;
   4868   return true;
   4869 
   4870  err_free_strtab:
   4871   if (strtab != NULL)
   4872     _bfd_elf_strtab_free (strtab);
   4873   return false;
   4874 }
   4875 
   4876 /* Retrieve .eh_frame_hdr.  Prior to size_dynamic_sections the
   4877    function effectively returns whether --eh-frame-hdr is given on the
   4878    command line.  After size_dynamic_sections the result reflects
   4879    whether .eh_frame_hdr will actually be output (sizing isn't done
   4880    until ldemul_after_allocation).  */
   4881 
   4882 static asection *
   4883 elf_eh_frame_hdr (const struct bfd_link_info *info)
   4884 {
   4885   if (info != NULL && is_elf_hash_table (info->hash))
   4886     return elf_hash_table (info)->eh_info.hdr_sec;
   4887   return NULL;
   4888 }
   4889 
   4890 /* Make an initial estimate of the size of the program header.  If we
   4891    get the number wrong here, we'll redo section placement.  */
   4892 
   4893 static bfd_size_type
   4894 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
   4895 {
   4896   size_t segs;
   4897   asection *s;
   4898   const struct elf_backend_data *bed;
   4899 
   4900   /* Assume we will need exactly two PT_LOAD segments: one for text
   4901      and one for data.  */
   4902   segs = 2;
   4903 
   4904   s = bfd_get_section_by_name (abfd, ".interp");
   4905   if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
   4906     {
   4907       /* If we have a loadable interpreter section, we need a
   4908 	 PT_INTERP segment.  In this case, assume we also need a
   4909 	 PT_PHDR segment, although that may not be true for all
   4910 	 targets.  */
   4911       segs += 2;
   4912     }
   4913 
   4914   if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
   4915     {
   4916       /* We need a PT_DYNAMIC segment.  */
   4917       ++segs;
   4918     }
   4919 
   4920   if (info != NULL && info->relro)
   4921     {
   4922       /* We need a PT_GNU_RELRO segment.  */
   4923       ++segs;
   4924     }
   4925 
   4926   if (elf_eh_frame_hdr (info))
   4927     {
   4928       /* We need a PT_GNU_EH_FRAME segment.  */
   4929       ++segs;
   4930     }
   4931 
   4932   if (elf_stack_flags (abfd))
   4933     {
   4934       /* We need a PT_GNU_STACK segment.  */
   4935       ++segs;
   4936     }
   4937 
   4938   if (elf_sframe (abfd))
   4939     {
   4940       /* We need a PT_GNU_SFRAME segment.  */
   4941       ++segs;
   4942     }
   4943 
   4944   s = bfd_get_section_by_name (abfd,
   4945 			       NOTE_GNU_PROPERTY_SECTION_NAME);
   4946   if (s != NULL && s->size != 0)
   4947     {
   4948       /* We need a PT_GNU_PROPERTY segment.  */
   4949       ++segs;
   4950     }
   4951 
   4952   for (s = abfd->sections; s != NULL; s = s->next)
   4953     {
   4954       if ((s->flags & SEC_LOAD) != 0
   4955 	  && elf_section_type (s) == SHT_NOTE)
   4956 	{
   4957 	  unsigned int alignment_power;
   4958 	  /* We need a PT_NOTE segment.  */
   4959 	  ++segs;
   4960 	  /* Try to create just one PT_NOTE segment for all adjacent
   4961 	     loadable SHT_NOTE sections.  gABI requires that within a
   4962 	     PT_NOTE segment (and also inside of each SHT_NOTE section)
   4963 	     each note should have the same alignment.  So we check
   4964 	     whether the sections are correctly aligned.  */
   4965 	  alignment_power = s->alignment_power;
   4966 	  while (s->next != NULL
   4967 		 && s->next->alignment_power == alignment_power
   4968 		 && (s->next->flags & SEC_LOAD) != 0
   4969 		 && elf_section_type (s->next) == SHT_NOTE)
   4970 	    s = s->next;
   4971 	}
   4972     }
   4973 
   4974   for (s = abfd->sections; s != NULL; s = s->next)
   4975     {
   4976       if (s->flags & SEC_THREAD_LOCAL)
   4977 	{
   4978 	  /* We need a PT_TLS segment.  */
   4979 	  ++segs;
   4980 	  break;
   4981 	}
   4982     }
   4983 
   4984   bed = get_elf_backend_data (abfd);
   4985 
   4986   if ((abfd->flags & D_PAGED) != 0
   4987       && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
   4988     {
   4989       /* Add a PT_GNU_MBIND segment for each mbind section.  */
   4990       bfd_vma commonpagesize;
   4991       unsigned int page_align_power;
   4992 
   4993       if (info != NULL)
   4994 	commonpagesize = info->commonpagesize;
   4995       else
   4996 	commonpagesize = bed->commonpagesize;
   4997       page_align_power = bfd_log2 (commonpagesize);
   4998       for (s = abfd->sections; s != NULL; s = s->next)
   4999 	if (elf_section_flags (s) & SHF_GNU_MBIND)
   5000 	  {
   5001 	    if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
   5002 	      {
   5003 		_bfd_error_handler
   5004 		  /* xgettext:c-format */
   5005 		  (_("%pB: GNU_MBIND section `%pA' has invalid "
   5006 		     "sh_info field: %d"),
   5007 		   abfd, s, elf_section_data (s)->this_hdr.sh_info);
   5008 		continue;
   5009 	      }
   5010 	    /* Align mbind section to page size.  */
   5011 	    if (s->alignment_power < page_align_power)
   5012 	      s->alignment_power = page_align_power;
   5013 	    segs ++;
   5014 	  }
   5015     }
   5016 
   5017   /* Let the backend count up any program headers it might need.  */
   5018   if (bed->elf_backend_additional_program_headers)
   5019     {
   5020       int a;
   5021 
   5022       a = (*bed->elf_backend_additional_program_headers) (abfd, info);
   5023       if (a == -1)
   5024 	abort ();
   5025       segs += a;
   5026     }
   5027 
   5028   return segs * bed->s->sizeof_phdr;
   5029 }
   5030 
   5031 /* Find the segment that contains the output_section of section.  */
   5032 
   5033 Elf_Internal_Phdr *
   5034 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
   5035 {
   5036   struct elf_segment_map *m;
   5037   Elf_Internal_Phdr *p;
   5038 
   5039   for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
   5040        m != NULL;
   5041        m = m->next, p++)
   5042     {
   5043       int i;
   5044 
   5045       for (i = m->count - 1; i >= 0; i--)
   5046 	if (m->sections[i] == section)
   5047 	  return p;
   5048     }
   5049 
   5050   return NULL;
   5051 }
   5052 
   5053 /* Create a mapping from a set of sections to a program segment.  */
   5054 
   5055 static struct elf_segment_map *
   5056 make_mapping (bfd *abfd,
   5057 	      asection **sections,
   5058 	      unsigned int from,
   5059 	      unsigned int to,
   5060 	      bool phdr)
   5061 {
   5062   struct elf_segment_map *m;
   5063   unsigned int i;
   5064   asection **hdrpp;
   5065   size_t amt;
   5066 
   5067   amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   5068   amt += (to - from) * sizeof (asection *);
   5069   m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5070   if (m == NULL)
   5071     return NULL;
   5072   m->next = NULL;
   5073   m->p_type = PT_LOAD;
   5074   for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
   5075     m->sections[i - from] = *hdrpp;
   5076   m->count = to - from;
   5077 
   5078   if (from == 0 && phdr)
   5079     {
   5080       /* Include the headers in the first PT_LOAD segment.  */
   5081       m->includes_filehdr = 1;
   5082       m->includes_phdrs = 1;
   5083     }
   5084 
   5085   return m;
   5086 }
   5087 
   5088 /* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
   5089    on failure.  */
   5090 
   5091 struct elf_segment_map *
   5092 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
   5093 {
   5094   struct elf_segment_map *m;
   5095 
   5096   m = (struct elf_segment_map *) bfd_zalloc (abfd,
   5097 					     sizeof (struct elf_segment_map));
   5098   if (m == NULL)
   5099     return NULL;
   5100   m->next = NULL;
   5101   m->p_type = PT_DYNAMIC;
   5102   m->count = 1;
   5103   m->sections[0] = dynsec;
   5104 
   5105   return m;
   5106 }
   5107 
   5108 /* Possibly add or remove segments from the segment map.  */
   5109 
   5110 static bool
   5111 elf_modify_segment_map (bfd *abfd,
   5112 			struct bfd_link_info *info,
   5113 			bool remove_empty_load)
   5114 {
   5115   struct elf_segment_map **m;
   5116   const struct elf_backend_data *bed;
   5117 
   5118   /* The placement algorithm assumes that non allocated sections are
   5119      not in PT_LOAD segments.  We ensure this here by removing such
   5120      sections from the segment map.  We also remove excluded
   5121      sections.  Finally, any PT_LOAD segment without sections is
   5122      removed.  */
   5123   m = &elf_seg_map (abfd);
   5124   while (*m)
   5125     {
   5126       unsigned int i, new_count;
   5127 
   5128       for (new_count = 0, i = 0; i < (*m)->count; i++)
   5129 	{
   5130 	  if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
   5131 	      && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
   5132 		  || (*m)->p_type != PT_LOAD))
   5133 	    {
   5134 	      (*m)->sections[new_count] = (*m)->sections[i];
   5135 	      new_count++;
   5136 	    }
   5137 	}
   5138       (*m)->count = new_count;
   5139 
   5140       if (remove_empty_load
   5141 	  && (*m)->p_type == PT_LOAD
   5142 	  && (*m)->count == 0
   5143 	  && !(*m)->includes_phdrs)
   5144 	*m = (*m)->next;
   5145       else
   5146 	m = &(*m)->next;
   5147     }
   5148 
   5149   bed = get_elf_backend_data (abfd);
   5150   if (bed->elf_backend_modify_segment_map != NULL)
   5151     {
   5152       if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
   5153 	return false;
   5154     }
   5155 
   5156   return true;
   5157 }
   5158 
   5159 #define IS_TBSS(s) \
   5160   ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
   5161 
   5162 /* Set up a mapping from BFD sections to program segments.  Update
   5163    NEED_LAYOUT if the section layout is changed.  */
   5164 
   5165 bool
   5166 _bfd_elf_map_sections_to_segments (bfd *abfd,
   5167 				   struct bfd_link_info *info,
   5168 				   bool *need_layout)
   5169 {
   5170   unsigned int count;
   5171   struct elf_segment_map *m;
   5172   asection **sections = NULL;
   5173   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   5174   bool no_user_phdrs;
   5175 
   5176   no_user_phdrs = elf_seg_map (abfd) == NULL;
   5177 
   5178   if (info != NULL)
   5179     {
   5180       info->user_phdrs = !no_user_phdrs;
   5181 
   5182       /* Size the relative relocations if DT_RELR is enabled.  */
   5183       if (info->enable_dt_relr
   5184 	  && need_layout != NULL
   5185 	  && bed->size_relative_relocs
   5186 	  && !bed->size_relative_relocs (info, need_layout))
   5187 	info->callbacks->einfo
   5188 	  (_("%F%P: failed to size relative relocations\n"));
   5189     }
   5190 
   5191   if (no_user_phdrs && bfd_count_sections (abfd) != 0)
   5192     {
   5193       asection *s;
   5194       unsigned int i;
   5195       struct elf_segment_map *mfirst;
   5196       struct elf_segment_map **pm;
   5197       asection *last_hdr;
   5198       bfd_vma last_size;
   5199       unsigned int hdr_index;
   5200       bfd_vma maxpagesize;
   5201       asection **hdrpp;
   5202       bool phdr_in_segment;
   5203       bool writable;
   5204       bool executable;
   5205       unsigned int tls_count = 0;
   5206       asection *first_tls = NULL;
   5207       asection *first_mbind = NULL;
   5208       asection *dynsec, *eh_frame_hdr;
   5209       asection *sframe;
   5210       size_t amt;
   5211       bfd_vma addr_mask, wrap_to = 0;  /* Bytes.  */
   5212       bfd_size_type phdr_size;  /* Octets/bytes.  */
   5213       unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   5214 
   5215       /* Select the allocated sections, and sort them.  */
   5216 
   5217       amt = bfd_count_sections (abfd) * sizeof (asection *);
   5218       sections = (asection **) bfd_malloc (amt);
   5219       if (sections == NULL)
   5220 	goto error_return;
   5221 
   5222       /* Calculate top address, avoiding undefined behaviour of shift
   5223 	 left operator when shift count is equal to size of type
   5224 	 being shifted.  */
   5225       addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
   5226       addr_mask = (addr_mask << 1) + 1;
   5227 
   5228       i = 0;
   5229       for (s = abfd->sections; s != NULL; s = s->next)
   5230 	{
   5231 	  if ((s->flags & SEC_ALLOC) != 0)
   5232 	    {
   5233 	      /* target_index is unused until bfd_elf_final_link
   5234 		 starts output of section symbols.  Use it to make
   5235 		 qsort stable.  */
   5236 	      s->target_index = i;
   5237 	      sections[i] = s;
   5238 	      ++i;
   5239 	      /* A wrapping section potentially clashes with header.  */
   5240 	      if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
   5241 		wrap_to = (s->lma + s->size / opb) & addr_mask;
   5242 	    }
   5243 	}
   5244       BFD_ASSERT (i <= bfd_count_sections (abfd));
   5245       count = i;
   5246 
   5247       qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
   5248 
   5249       phdr_size = elf_program_header_size (abfd);
   5250       if (phdr_size == (bfd_size_type) -1)
   5251 	phdr_size = get_program_header_size (abfd, info);
   5252       phdr_size += bed->s->sizeof_ehdr;
   5253       /* phdr_size is compared to LMA values which are in bytes.  */
   5254       phdr_size /= opb;
   5255       if (info != NULL)
   5256 	maxpagesize = info->maxpagesize;
   5257       else
   5258 	maxpagesize = bed->maxpagesize;
   5259       if (maxpagesize == 0)
   5260 	maxpagesize = 1;
   5261       phdr_in_segment = info != NULL && info->load_phdrs;
   5262       if (count != 0
   5263 	  && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
   5264 	      >= (phdr_size & (maxpagesize - 1))))
   5265 	/* For compatibility with old scripts that may not be using
   5266 	   SIZEOF_HEADERS, add headers when it looks like space has
   5267 	   been left for them.  */
   5268 	phdr_in_segment = true;
   5269 
   5270       /* Build the mapping.  */
   5271       mfirst = NULL;
   5272       pm = &mfirst;
   5273 
   5274       /* If we have a .interp section, then create a PT_PHDR segment for
   5275 	 the program headers and a PT_INTERP segment for the .interp
   5276 	 section.  */
   5277       s = bfd_get_section_by_name (abfd, ".interp");
   5278       if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
   5279 	{
   5280 	  amt = sizeof (struct elf_segment_map);
   5281 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5282 	  if (m == NULL)
   5283 	    goto error_return;
   5284 	  m->next = NULL;
   5285 	  m->p_type = PT_PHDR;
   5286 	  m->p_flags = PF_R;
   5287 	  m->p_flags_valid = 1;
   5288 	  m->includes_phdrs = 1;
   5289 	  phdr_in_segment = true;
   5290 	  *pm = m;
   5291 	  pm = &m->next;
   5292 
   5293 	  amt = sizeof (struct elf_segment_map);
   5294 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5295 	  if (m == NULL)
   5296 	    goto error_return;
   5297 	  m->next = NULL;
   5298 	  m->p_type = PT_INTERP;
   5299 	  m->count = 1;
   5300 	  m->sections[0] = s;
   5301 
   5302 	  *pm = m;
   5303 	  pm = &m->next;
   5304 	}
   5305 
   5306       /* Look through the sections.  We put sections in the same program
   5307 	 segment when the start of the second section can be placed within
   5308 	 a few bytes of the end of the first section.  */
   5309       last_hdr = NULL;
   5310       last_size = 0;
   5311       hdr_index = 0;
   5312       writable = false;
   5313       executable = false;
   5314       dynsec = bfd_get_section_by_name (abfd, ".dynamic");
   5315       if (dynsec != NULL
   5316 	  && (dynsec->flags & SEC_LOAD) == 0)
   5317 	dynsec = NULL;
   5318 
   5319       if ((abfd->flags & D_PAGED) == 0)
   5320 	phdr_in_segment = false;
   5321 
   5322       /* Deal with -Ttext or something similar such that the first section
   5323 	 is not adjacent to the program headers.  This is an
   5324 	 approximation, since at this point we don't know exactly how many
   5325 	 program headers we will need.  */
   5326       if (phdr_in_segment && count > 0)
   5327 	{
   5328 	  bfd_vma phdr_lma;  /* Bytes.  */
   5329 	  bool separate_phdr = false;
   5330 
   5331 	  phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
   5332 	  if (info != NULL
   5333 	      && info->separate_code
   5334 	      && (sections[0]->flags & SEC_CODE) != 0)
   5335 	    {
   5336 	      /* If data sections should be separate from code and
   5337 		 thus not executable, and the first section is
   5338 		 executable then put the file and program headers in
   5339 		 their own PT_LOAD.  */
   5340 	      separate_phdr = true;
   5341 	      if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
   5342 		   == (sections[0]->lma & addr_mask & -maxpagesize)))
   5343 		{
   5344 		  /* The file and program headers are currently on the
   5345 		     same page as the first section.  Put them on the
   5346 		     previous page if we can.  */
   5347 		  if (phdr_lma >= maxpagesize)
   5348 		    phdr_lma -= maxpagesize;
   5349 		  else
   5350 		    separate_phdr = false;
   5351 		}
   5352 	    }
   5353 	  if ((sections[0]->lma & addr_mask) < phdr_lma
   5354 	      || (sections[0]->lma & addr_mask) < phdr_size)
   5355 	    /* If file and program headers would be placed at the end
   5356 	       of memory then it's probably better to omit them.  */
   5357 	    phdr_in_segment = false;
   5358 	  else if (phdr_lma < wrap_to)
   5359 	    /* If a section wraps around to where we'll be placing
   5360 	       file and program headers, then the headers will be
   5361 	       overwritten.  */
   5362 	    phdr_in_segment = false;
   5363 	  else if (separate_phdr)
   5364 	    {
   5365 	      m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
   5366 	      if (m == NULL)
   5367 		goto error_return;
   5368 	      m->p_paddr = phdr_lma * opb;
   5369 	      m->p_vaddr_offset
   5370 		= (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
   5371 	      m->p_paddr_valid = 1;
   5372 	      *pm = m;
   5373 	      pm = &m->next;
   5374 	      phdr_in_segment = false;
   5375 	    }
   5376 	}
   5377 
   5378       for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
   5379 	{
   5380 	  asection *hdr;
   5381 	  bool new_segment;
   5382 
   5383 	  hdr = *hdrpp;
   5384 
   5385 	  /* See if this section and the last one will fit in the same
   5386 	     segment.  */
   5387 
   5388 	  if (last_hdr == NULL)
   5389 	    {
   5390 	      /* If we don't have a segment yet, then we don't need a new
   5391 		 one (we build the last one after this loop).  */
   5392 	      new_segment = false;
   5393 	    }
   5394 	  else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
   5395 	    {
   5396 	      /* If this section has a different relation between the
   5397 		 virtual address and the load address, then we need a new
   5398 		 segment.  */
   5399 	      new_segment = true;
   5400 	    }
   5401 	  else if (hdr->lma < last_hdr->lma + last_size
   5402 		   || last_hdr->lma + last_size < last_hdr->lma)
   5403 	    {
   5404 	      /* If this section has a load address that makes it overlap
   5405 		 the previous section, then we need a new segment.  */
   5406 	      new_segment = true;
   5407 	    }
   5408 	  else if ((abfd->flags & D_PAGED) != 0
   5409 		   && (((last_hdr->lma + last_size - 1) & -maxpagesize)
   5410 		       == (hdr->lma & -maxpagesize)))
   5411 	    {
   5412 	      /* If we are demand paged then we can't map two disk
   5413 		 pages onto the same memory page.  */
   5414 	      new_segment = false;
   5415 	    }
   5416 	  /* In the next test we have to be careful when last_hdr->lma is close
   5417 	     to the end of the address space.  If the aligned address wraps
   5418 	     around to the start of the address space, then there are no more
   5419 	     pages left in memory and it is OK to assume that the current
   5420 	     section can be included in the current segment.  */
   5421 	  else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
   5422 		    + maxpagesize > last_hdr->lma)
   5423 		   && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
   5424 		       + maxpagesize <= hdr->lma))
   5425 	    {
   5426 	      /* If putting this section in this segment would force us to
   5427 		 skip a page in the segment, then we need a new segment.  */
   5428 	      new_segment = true;
   5429 	    }
   5430 	  else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
   5431 		   && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
   5432 	    {
   5433 	      /* We don't want to put a loaded section after a
   5434 		 nonloaded (ie. bss style) section in the same segment
   5435 		 as that will force the non-loaded section to be loaded.
   5436 		 Consider .tbss sections as loaded for this purpose.  */
   5437 	      new_segment = true;
   5438 	    }
   5439 	  else if ((abfd->flags & D_PAGED) == 0)
   5440 	    {
   5441 	      /* If the file is not demand paged, which means that we
   5442 		 don't require the sections to be correctly aligned in the
   5443 		 file, then there is no other reason for a new segment.  */
   5444 	      new_segment = false;
   5445 	    }
   5446 	  else if (info != NULL
   5447 		   && info->separate_code
   5448 		   && executable != ((hdr->flags & SEC_CODE) != 0))
   5449 	    {
   5450 	      new_segment = true;
   5451 	    }
   5452 	  else if (! writable
   5453 		   && (hdr->flags & SEC_READONLY) == 0)
   5454 	    {
   5455 	      /* We don't want to put a writable section in a read only
   5456 		 segment.  */
   5457 	      new_segment = true;
   5458 	    }
   5459 	  else
   5460 	    {
   5461 	      /* Otherwise, we can use the same segment.  */
   5462 	      new_segment = false;
   5463 	    }
   5464 
   5465 	  /* Allow interested parties a chance to override our decision.  */
   5466 	  if (last_hdr != NULL
   5467 	      && info != NULL
   5468 	      && info->callbacks->override_segment_assignment != NULL)
   5469 	    new_segment
   5470 	      = info->callbacks->override_segment_assignment (info, abfd, hdr,
   5471 							      last_hdr,
   5472 							      new_segment);
   5473 
   5474 	  if (! new_segment)
   5475 	    {
   5476 	      if ((hdr->flags & SEC_READONLY) == 0)
   5477 		writable = true;
   5478 	      if ((hdr->flags & SEC_CODE) != 0)
   5479 		executable = true;
   5480 	      last_hdr = hdr;
   5481 	      /* .tbss sections effectively have zero size.  */
   5482 	      last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
   5483 	      continue;
   5484 	    }
   5485 
   5486 	  /* We need a new program segment.  We must create a new program
   5487 	     header holding all the sections from hdr_index until hdr.  */
   5488 
   5489 	  m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
   5490 	  if (m == NULL)
   5491 	    goto error_return;
   5492 
   5493 	  *pm = m;
   5494 	  pm = &m->next;
   5495 
   5496 	  if ((hdr->flags & SEC_READONLY) == 0)
   5497 	    writable = true;
   5498 	  else
   5499 	    writable = false;
   5500 
   5501 	  if ((hdr->flags & SEC_CODE) == 0)
   5502 	    executable = false;
   5503 	  else
   5504 	    executable = true;
   5505 
   5506 	  last_hdr = hdr;
   5507 	  /* .tbss sections effectively have zero size.  */
   5508 	  last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
   5509 	  hdr_index = i;
   5510 	  phdr_in_segment = false;
   5511 	}
   5512 
   5513       /* Create a final PT_LOAD program segment, but not if it's just
   5514 	 for .tbss.  */
   5515       if (last_hdr != NULL
   5516 	  && (i - hdr_index != 1
   5517 	      || !IS_TBSS (last_hdr)))
   5518 	{
   5519 	  m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
   5520 	  if (m == NULL)
   5521 	    goto error_return;
   5522 
   5523 	  *pm = m;
   5524 	  pm = &m->next;
   5525 	}
   5526 
   5527       /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
   5528       if (dynsec != NULL)
   5529 	{
   5530 	  m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
   5531 	  if (m == NULL)
   5532 	    goto error_return;
   5533 	  *pm = m;
   5534 	  pm = &m->next;
   5535 	}
   5536 
   5537       /* For each batch of consecutive loadable SHT_NOTE  sections,
   5538 	 add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
   5539 	 because if we link together nonloadable .note sections and
   5540 	 loadable .note sections, we will generate two .note sections
   5541 	 in the output file.  */
   5542       for (s = abfd->sections; s != NULL; s = s->next)
   5543 	{
   5544 	  if ((s->flags & SEC_LOAD) != 0
   5545 	      && elf_section_type (s) == SHT_NOTE)
   5546 	    {
   5547 	      asection *s2;
   5548 	      unsigned int alignment_power = s->alignment_power;
   5549 
   5550 	      count = 1;
   5551 	      for (s2 = s; s2->next != NULL; s2 = s2->next)
   5552 		{
   5553 		  if (s2->next->alignment_power == alignment_power
   5554 		      && (s2->next->flags & SEC_LOAD) != 0
   5555 		      && elf_section_type (s2->next) == SHT_NOTE
   5556 		      && align_power (s2->lma + s2->size / opb,
   5557 				      alignment_power)
   5558 		      == s2->next->lma)
   5559 		    count++;
   5560 		  else
   5561 		    break;
   5562 		}
   5563 	      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   5564 	      amt += count * sizeof (asection *);
   5565 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5566 	      if (m == NULL)
   5567 		goto error_return;
   5568 	      m->next = NULL;
   5569 	      m->p_type = PT_NOTE;
   5570 	      m->count = count;
   5571 	      while (count > 1)
   5572 		{
   5573 		  m->sections[m->count - count--] = s;
   5574 		  BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   5575 		  s = s->next;
   5576 		}
   5577 	      m->sections[m->count - 1] = s;
   5578 	      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   5579 	      *pm = m;
   5580 	      pm = &m->next;
   5581 	    }
   5582 	  if (s->flags & SEC_THREAD_LOCAL)
   5583 	    {
   5584 	      if (! tls_count)
   5585 		first_tls = s;
   5586 	      tls_count++;
   5587 	    }
   5588 	  if (first_mbind == NULL
   5589 	      && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
   5590 	    first_mbind = s;
   5591 	}
   5592 
   5593       /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
   5594       if (tls_count > 0)
   5595 	{
   5596 	  amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   5597 	  amt += tls_count * sizeof (asection *);
   5598 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5599 	  if (m == NULL)
   5600 	    goto error_return;
   5601 	  m->next = NULL;
   5602 	  m->p_type = PT_TLS;
   5603 	  m->count = tls_count;
   5604 	  /* Mandated PF_R.  */
   5605 	  m->p_flags = PF_R;
   5606 	  m->p_flags_valid = 1;
   5607 	  s = first_tls;
   5608 	  for (i = 0; i < tls_count; ++i)
   5609 	    {
   5610 	      if ((s->flags & SEC_THREAD_LOCAL) == 0)
   5611 		{
   5612 		  _bfd_error_handler
   5613 		    (_("%pB: TLS sections are not adjacent:"), abfd);
   5614 		  s = first_tls;
   5615 		  i = 0;
   5616 		  while (i < tls_count)
   5617 		    {
   5618 		      if ((s->flags & SEC_THREAD_LOCAL) != 0)
   5619 			{
   5620 			  _bfd_error_handler (_("	    TLS: %pA"), s);
   5621 			  i++;
   5622 			}
   5623 		      else
   5624 			_bfd_error_handler (_("	non-TLS: %pA"), s);
   5625 		      s = s->next;
   5626 		    }
   5627 		  bfd_set_error (bfd_error_bad_value);
   5628 		  goto error_return;
   5629 		}
   5630 	      m->sections[i] = s;
   5631 	      s = s->next;
   5632 	    }
   5633 
   5634 	  *pm = m;
   5635 	  pm = &m->next;
   5636 	}
   5637 
   5638       if (first_mbind
   5639 	  && (abfd->flags & D_PAGED) != 0
   5640 	  && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
   5641 	for (s = first_mbind; s != NULL; s = s->next)
   5642 	  if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
   5643 	      && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
   5644 	    {
   5645 	      /* Mandated PF_R.  */
   5646 	      unsigned long p_flags = PF_R;
   5647 	      if ((s->flags & SEC_READONLY) == 0)
   5648 		p_flags |= PF_W;
   5649 	      if ((s->flags & SEC_CODE) != 0)
   5650 		p_flags |= PF_X;
   5651 
   5652 	      amt = sizeof (struct elf_segment_map) + sizeof (asection *);
   5653 	      m = bfd_zalloc (abfd, amt);
   5654 	      if (m == NULL)
   5655 		goto error_return;
   5656 	      m->next = NULL;
   5657 	      m->p_type = (PT_GNU_MBIND_LO
   5658 			   + elf_section_data (s)->this_hdr.sh_info);
   5659 	      m->count = 1;
   5660 	      m->p_flags_valid = 1;
   5661 	      m->sections[0] = s;
   5662 	      m->p_flags = p_flags;
   5663 
   5664 	      *pm = m;
   5665 	      pm = &m->next;
   5666 	    }
   5667 
   5668       s = bfd_get_section_by_name (abfd,
   5669 				   NOTE_GNU_PROPERTY_SECTION_NAME);
   5670       if (s != NULL && s->size != 0)
   5671 	{
   5672 	  amt = sizeof (struct elf_segment_map) + sizeof (asection *);
   5673 	  m = bfd_zalloc (abfd, amt);
   5674 	  if (m == NULL)
   5675 	    goto error_return;
   5676 	  m->next = NULL;
   5677 	  m->p_type = PT_GNU_PROPERTY;
   5678 	  m->count = 1;
   5679 	  m->p_flags_valid = 1;
   5680 	  m->sections[0] = s;
   5681 	  m->p_flags = PF_R;
   5682 	  *pm = m;
   5683 	  pm = &m->next;
   5684 	}
   5685 
   5686       /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
   5687 	 segment.  */
   5688       eh_frame_hdr = elf_eh_frame_hdr (info);
   5689       if (eh_frame_hdr != NULL
   5690 	  && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
   5691 	{
   5692 	  amt = sizeof (struct elf_segment_map);
   5693 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5694 	  if (m == NULL)
   5695 	    goto error_return;
   5696 	  m->next = NULL;
   5697 	  m->p_type = PT_GNU_EH_FRAME;
   5698 	  m->count = 1;
   5699 	  m->sections[0] = eh_frame_hdr->output_section;
   5700 
   5701 	  *pm = m;
   5702 	  pm = &m->next;
   5703 	}
   5704 
   5705       /* If there is a .sframe section, throw in a PT_GNU_SFRAME
   5706 	 segment.  */
   5707       sframe = elf_sframe (abfd);
   5708       if (sframe != NULL
   5709 	  && (sframe->output_section->flags & SEC_LOAD) != 0
   5710 	  && sframe->size != 0)
   5711 	{
   5712 	  amt = sizeof (struct elf_segment_map);
   5713 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5714 	  if (m == NULL)
   5715 	    goto error_return;
   5716 	  m->next = NULL;
   5717 	  m->p_type = PT_GNU_SFRAME;
   5718 	  m->count = 1;
   5719 	  m->sections[0] = sframe->output_section;
   5720 
   5721 	  *pm = m;
   5722 	  pm = &m->next;
   5723 	}
   5724 
   5725       if (elf_stack_flags (abfd))
   5726 	{
   5727 	  amt = sizeof (struct elf_segment_map);
   5728 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5729 	  if (m == NULL)
   5730 	    goto error_return;
   5731 	  m->next = NULL;
   5732 	  m->p_type = PT_GNU_STACK;
   5733 	  m->p_flags = elf_stack_flags (abfd);
   5734 	  m->p_align = bed->stack_align;
   5735 	  m->p_flags_valid = 1;
   5736 	  m->p_align_valid = m->p_align != 0;
   5737 	  if (info->stacksize > 0)
   5738 	    {
   5739 	      m->p_size = info->stacksize;
   5740 	      m->p_size_valid = 1;
   5741 	    }
   5742 
   5743 	  *pm = m;
   5744 	  pm = &m->next;
   5745 	}
   5746 
   5747       if (info != NULL && info->relro)
   5748 	{
   5749 	  for (m = mfirst; m != NULL; m = m->next)
   5750 	    {
   5751 	      if (m->p_type == PT_LOAD
   5752 		  && m->count != 0
   5753 		  && m->sections[0]->vma >= info->relro_start
   5754 		  && m->sections[0]->vma < info->relro_end)
   5755 		{
   5756 		  i = m->count;
   5757 		  while (--i != (unsigned) -1)
   5758 		    {
   5759 		      if (m->sections[i]->size > 0
   5760 			  && (m->sections[i]->flags & SEC_LOAD) != 0
   5761 			  && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
   5762 			break;
   5763 		    }
   5764 
   5765 		  if (i != (unsigned) -1)
   5766 		    break;
   5767 		}
   5768 	    }
   5769 
   5770 	  /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
   5771 	  if (m != NULL)
   5772 	    {
   5773 	      amt = sizeof (struct elf_segment_map);
   5774 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5775 	      if (m == NULL)
   5776 		goto error_return;
   5777 	      m->next = NULL;
   5778 	      m->p_type = PT_GNU_RELRO;
   5779 	      *pm = m;
   5780 	      pm = &m->next;
   5781 	    }
   5782 	}
   5783 
   5784       free (sections);
   5785       elf_seg_map (abfd) = mfirst;
   5786     }
   5787 
   5788   if (!elf_modify_segment_map (abfd, info, no_user_phdrs || info == NULL))
   5789     return false;
   5790 
   5791   for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
   5792     ++count;
   5793   elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
   5794 
   5795   return true;
   5796 
   5797  error_return:
   5798   free (sections);
   5799   return false;
   5800 }
   5801 
   5802 /* Sort sections by address.  */
   5803 
   5804 static int
   5805 elf_sort_sections (const void *arg1, const void *arg2)
   5806 {
   5807   const asection *sec1 = *(const asection **) arg1;
   5808   const asection *sec2 = *(const asection **) arg2;
   5809   bfd_size_type size1, size2;
   5810 
   5811   /* Sort by LMA first, since this is the address used to
   5812      place the section into a segment.  */
   5813   if (sec1->lma < sec2->lma)
   5814     return -1;
   5815   else if (sec1->lma > sec2->lma)
   5816     return 1;
   5817 
   5818   /* Then sort by VMA.  Normally the LMA and the VMA will be
   5819      the same, and this will do nothing.  */
   5820   if (sec1->vma < sec2->vma)
   5821     return -1;
   5822   else if (sec1->vma > sec2->vma)
   5823     return 1;
   5824 
   5825   /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
   5826 
   5827 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
   5828 		  && (x)->size != 0)
   5829 
   5830   if (TOEND (sec1))
   5831     {
   5832       if (!TOEND (sec2))
   5833 	return 1;
   5834     }
   5835   else if (TOEND (sec2))
   5836     return -1;
   5837 
   5838 #undef TOEND
   5839 
   5840   /* Sort by size, to put zero sized sections
   5841      before others at the same address.  */
   5842 
   5843   size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
   5844   size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
   5845 
   5846   if (size1 < size2)
   5847     return -1;
   5848   if (size1 > size2)
   5849     return 1;
   5850 
   5851   return sec1->target_index - sec2->target_index;
   5852 }
   5853 
   5854 /* This qsort comparison functions sorts PT_LOAD segments first and
   5855    by p_paddr, for assign_file_positions_for_load_sections.  */
   5856 
   5857 static int
   5858 elf_sort_segments (const void *arg1, const void *arg2)
   5859 {
   5860   const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
   5861   const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
   5862 
   5863   if (m1->p_type != m2->p_type)
   5864     {
   5865       if (m1->p_type == PT_NULL)
   5866 	return 1;
   5867       if (m2->p_type == PT_NULL)
   5868 	return -1;
   5869       return m1->p_type < m2->p_type ? -1 : 1;
   5870     }
   5871   if (m1->includes_filehdr != m2->includes_filehdr)
   5872     return m1->includes_filehdr ? -1 : 1;
   5873   if (m1->no_sort_lma != m2->no_sort_lma)
   5874     return m1->no_sort_lma ? -1 : 1;
   5875   if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
   5876     {
   5877       bfd_vma lma1, lma2;  /* Octets.  */
   5878       lma1 = 0;
   5879       if (m1->p_paddr_valid)
   5880 	lma1 = m1->p_paddr;
   5881       else if (m1->count != 0)
   5882 	{
   5883 	  unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
   5884 						  m1->sections[0]);
   5885 	  lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
   5886 	}
   5887       lma2 = 0;
   5888       if (m2->p_paddr_valid)
   5889 	lma2 = m2->p_paddr;
   5890       else if (m2->count != 0)
   5891 	{
   5892 	  unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
   5893 						  m2->sections[0]);
   5894 	  lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
   5895 	}
   5896       if (lma1 != lma2)
   5897 	return lma1 < lma2 ? -1 : 1;
   5898     }
   5899   if (m1->idx != m2->idx)
   5900     return m1->idx < m2->idx ? -1 : 1;
   5901   return 0;
   5902 }
   5903 
   5904 /* Ian Lance Taylor writes:
   5905 
   5906    We shouldn't be using % with a negative signed number.  That's just
   5907    not good.  We have to make sure either that the number is not
   5908    negative, or that the number has an unsigned type.  When the types
   5909    are all the same size they wind up as unsigned.  When file_ptr is a
   5910    larger signed type, the arithmetic winds up as signed long long,
   5911    which is wrong.
   5912 
   5913    What we're trying to say here is something like ``increase OFF by
   5914    the least amount that will cause it to be equal to the VMA modulo
   5915    the page size.''  */
   5916 /* In other words, something like:
   5917 
   5918    vma_offset = m->sections[0]->vma % bed->maxpagesize;
   5919    off_offset = off % bed->maxpagesize;
   5920    if (vma_offset < off_offset)
   5921      adjustment = vma_offset + bed->maxpagesize - off_offset;
   5922    else
   5923      adjustment = vma_offset - off_offset;
   5924 
   5925    which can be collapsed into the expression below.  */
   5926 
   5927 static file_ptr
   5928 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
   5929 {
   5930   /* PR binutils/16199: Handle an alignment of zero.  */
   5931   if (maxpagesize == 0)
   5932     maxpagesize = 1;
   5933   return ((vma - off) % maxpagesize);
   5934 }
   5935 
   5936 static void
   5937 print_segment_map (const struct elf_segment_map *m)
   5938 {
   5939   unsigned int j;
   5940   const char *pt = get_segment_type (m->p_type);
   5941   char buf[32];
   5942 
   5943   if (pt == NULL)
   5944     {
   5945       if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
   5946 	sprintf (buf, "LOPROC+%7.7x",
   5947 		 (unsigned int) (m->p_type - PT_LOPROC));
   5948       else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
   5949 	sprintf (buf, "LOOS+%7.7x",
   5950 		 (unsigned int) (m->p_type - PT_LOOS));
   5951       else
   5952 	snprintf (buf, sizeof (buf), "%8.8x",
   5953 		  (unsigned int) m->p_type);
   5954       pt = buf;
   5955     }
   5956   fflush (stdout);
   5957   fprintf (stderr, "%s:", pt);
   5958   for (j = 0; j < m->count; j++)
   5959     fprintf (stderr, " %s", m->sections [j]->name);
   5960   putc ('\n',stderr);
   5961   fflush (stderr);
   5962 }
   5963 
   5964 /* Assign file positions to the sections based on the mapping from
   5965    sections to segments.  This function also sets up some fields in
   5966    the file header.  */
   5967 
   5968 static bool
   5969 assign_file_positions_for_load_sections (bfd *abfd,
   5970 					 struct bfd_link_info *link_info)
   5971 {
   5972   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   5973   struct elf_segment_map *m;
   5974   struct elf_segment_map *phdr_load_seg;
   5975   Elf_Internal_Phdr *phdrs;
   5976   Elf_Internal_Phdr *p;
   5977   file_ptr off;  /* Octets.  */
   5978   bfd_size_type maxpagesize;
   5979   unsigned int alloc, actual;
   5980   unsigned int i, j;
   5981   struct elf_segment_map **sorted_seg_map;
   5982   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   5983 
   5984   if (link_info == NULL
   5985       && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
   5986     return false;
   5987 
   5988   alloc = 0;
   5989   for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   5990     m->idx = alloc++;
   5991 
   5992   if (alloc)
   5993     {
   5994       elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
   5995       elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
   5996     }
   5997   else
   5998     {
   5999       /* PR binutils/12467.  */
   6000       elf_elfheader (abfd)->e_phoff = 0;
   6001       elf_elfheader (abfd)->e_phentsize = 0;
   6002     }
   6003 
   6004   elf_elfheader (abfd)->e_phnum = alloc;
   6005 
   6006   if (elf_program_header_size (abfd) == (bfd_size_type) -1)
   6007     {
   6008       actual = alloc;
   6009       elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
   6010     }
   6011   else
   6012     {
   6013       actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
   6014       BFD_ASSERT (elf_program_header_size (abfd)
   6015 		  == actual * bed->s->sizeof_phdr);
   6016       BFD_ASSERT (actual >= alloc);
   6017     }
   6018 
   6019   if (alloc == 0)
   6020     {
   6021       elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
   6022       return true;
   6023     }
   6024 
   6025   /* We're writing the size in elf_program_header_size (abfd),
   6026      see assign_file_positions_except_relocs, so make sure we have
   6027      that amount allocated, with trailing space cleared.
   6028      The variable alloc contains the computed need, while
   6029      elf_program_header_size (abfd) contains the size used for the
   6030      layout.
   6031      See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
   6032      where the layout is forced to according to a larger size in the
   6033      last iterations for the testcase ld-elf/header.  */
   6034   phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
   6035 			     + alloc * sizeof (*sorted_seg_map)));
   6036   sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
   6037   elf_tdata (abfd)->phdr = phdrs;
   6038   if (phdrs == NULL)
   6039     return false;
   6040 
   6041   for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
   6042     {
   6043       sorted_seg_map[j] = m;
   6044       /* If elf_segment_map is not from map_sections_to_segments, the
   6045 	 sections may not be correctly ordered.  NOTE: sorting should
   6046 	 not be done to the PT_NOTE section of a corefile, which may
   6047 	 contain several pseudo-sections artificially created by bfd.
   6048 	 Sorting these pseudo-sections breaks things badly.  */
   6049       if (m->count > 1
   6050 	  && !(elf_elfheader (abfd)->e_type == ET_CORE
   6051 	       && m->p_type == PT_NOTE))
   6052 	{
   6053 	  for (i = 0; i < m->count; i++)
   6054 	    m->sections[i]->target_index = i;
   6055 	  qsort (m->sections, (size_t) m->count, sizeof (asection *),
   6056 		 elf_sort_sections);
   6057 	}
   6058     }
   6059   if (alloc > 1)
   6060     qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
   6061 	   elf_sort_segments);
   6062 
   6063   maxpagesize = 1;
   6064   if ((abfd->flags & D_PAGED) != 0)
   6065     {
   6066       if (link_info != NULL)
   6067 	maxpagesize = link_info->maxpagesize;
   6068       else
   6069 	maxpagesize = bed->maxpagesize;
   6070     }
   6071 
   6072   /* Sections must map to file offsets past the ELF file header.  */
   6073   off = bed->s->sizeof_ehdr;
   6074   /* And if one of the PT_LOAD headers doesn't include the program
   6075      headers then we'll be mapping program headers in the usual
   6076      position after the ELF file header.  */
   6077   phdr_load_seg = NULL;
   6078   for (j = 0; j < alloc; j++)
   6079     {
   6080       m = sorted_seg_map[j];
   6081       if (m->p_type != PT_LOAD)
   6082 	break;
   6083       if (m->includes_phdrs)
   6084 	{
   6085 	  phdr_load_seg = m;
   6086 	  break;
   6087 	}
   6088     }
   6089   if (phdr_load_seg == NULL)
   6090     off += actual * bed->s->sizeof_phdr;
   6091 
   6092   for (j = 0; j < alloc; j++)
   6093     {
   6094       asection **secpp;
   6095       bfd_vma off_adjust;  /* Octets.  */
   6096       bool no_contents;
   6097       bfd_size_type p_align;
   6098       bool p_align_p;
   6099 
   6100       /* An ELF segment (described by Elf_Internal_Phdr) may contain a
   6101 	 number of sections with contents contributing to both p_filesz
   6102 	 and p_memsz, followed by a number of sections with no contents
   6103 	 that just contribute to p_memsz.  In this loop, OFF tracks next
   6104 	 available file offset for PT_LOAD and PT_NOTE segments.  */
   6105       m = sorted_seg_map[j];
   6106       p = phdrs + m->idx;
   6107       p->p_type = m->p_type;
   6108       p->p_flags = m->p_flags;
   6109       p_align = bed->p_align;
   6110       p_align_p = false;
   6111 
   6112       if (m->count == 0)
   6113 	p->p_vaddr = m->p_vaddr_offset * opb;
   6114       else
   6115 	p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
   6116 
   6117       if (m->p_paddr_valid)
   6118 	p->p_paddr = m->p_paddr;
   6119       else if (m->count == 0)
   6120 	p->p_paddr = 0;
   6121       else
   6122 	p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
   6123 
   6124       if (p->p_type == PT_LOAD
   6125 	  && (abfd->flags & D_PAGED) != 0)
   6126 	{
   6127 	  /* p_align in demand paged PT_LOAD segments effectively stores
   6128 	     the maximum page size.  When copying an executable with
   6129 	     objcopy, we set m->p_align from the input file.  Use this
   6130 	     value for maxpagesize rather than bed->maxpagesize, which
   6131 	     may be different.  Note that we use maxpagesize for PT_TLS
   6132 	     segment alignment later in this function, so we are relying
   6133 	     on at least one PT_LOAD segment appearing before a PT_TLS
   6134 	     segment.  */
   6135 	  if (m->p_align_valid)
   6136 	    maxpagesize = m->p_align;
   6137 	  else if (p_align != 0
   6138 		   && (link_info == NULL
   6139 		       || !link_info->maxpagesize_is_set))
   6140 	    /* Set p_align to the default p_align value while laying
   6141 	       out segments aligning to the maximum page size or the
   6142 	       largest section alignment.  The run-time loader can
   6143 	       align segments to the default p_align value or the
   6144 	       maximum page size, depending on system page size.  */
   6145 	    p_align_p = true;
   6146 
   6147 	  p->p_align = maxpagesize;
   6148 	}
   6149       else if (m->p_align_valid)
   6150 	p->p_align = m->p_align;
   6151       else if (m->count == 0)
   6152 	p->p_align = 1 << bed->s->log_file_align;
   6153 
   6154       if (m == phdr_load_seg)
   6155 	{
   6156 	  if (!m->includes_filehdr)
   6157 	    p->p_offset = off;
   6158 	  off += actual * bed->s->sizeof_phdr;
   6159 	}
   6160 
   6161       no_contents = false;
   6162       off_adjust = 0;
   6163       if (p->p_type == PT_LOAD
   6164 	  && m->count > 0)
   6165 	{
   6166 	  bfd_size_type align;  /* Bytes.  */
   6167 	  unsigned int align_power = 0;
   6168 
   6169 	  if (m->p_align_valid)
   6170 	    align = p->p_align;
   6171 	  else
   6172 	    {
   6173 	      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   6174 		{
   6175 		  unsigned int secalign;
   6176 
   6177 		  secalign = bfd_section_alignment (*secpp);
   6178 		  if (secalign > align_power)
   6179 		    align_power = secalign;
   6180 		}
   6181 	      align = (bfd_size_type) 1 << align_power;
   6182 	      if (align < maxpagesize)
   6183 		{
   6184 		  /* If a section requires alignment higher than the
   6185 		     default p_align value, don't set p_align to the
   6186 		     default p_align value.  */
   6187 		  if (align > p_align)
   6188 		    p_align_p = false;
   6189 		  align = maxpagesize;
   6190 		}
   6191 	      else
   6192 		{
   6193 		  /* If a section requires alignment higher than the
   6194 		     maximum page size, set p_align to the section
   6195 		     alignment.  */
   6196 		  p_align_p = true;
   6197 		  p_align = align;
   6198 		}
   6199 	    }
   6200 
   6201 	  for (i = 0; i < m->count; i++)
   6202 	    if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   6203 	      /* If we aren't making room for this section, then
   6204 		 it must be SHT_NOBITS regardless of what we've
   6205 		 set via struct bfd_elf_special_section.  */
   6206 	      elf_section_type (m->sections[i]) = SHT_NOBITS;
   6207 
   6208 	  /* Find out whether this segment contains any loadable
   6209 	     sections.  */
   6210 	  no_contents = true;
   6211 	  for (i = 0; i < m->count; i++)
   6212 	    if (elf_section_type (m->sections[i]) != SHT_NOBITS)
   6213 	      {
   6214 		no_contents = false;
   6215 		break;
   6216 	      }
   6217 
   6218 	  off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
   6219 
   6220 	  /* Broken hardware and/or kernel require that files do not
   6221 	     map the same page with different permissions on some hppa
   6222 	     processors.  */
   6223 	  if (j != 0
   6224 	      && (abfd->flags & D_PAGED) != 0
   6225 	      && bed->no_page_alias
   6226 	      && (off & (maxpagesize - 1)) != 0
   6227 	      && ((off & -maxpagesize)
   6228 		  == ((off + off_adjust) & -maxpagesize)))
   6229 	    off_adjust += maxpagesize;
   6230 	  off += off_adjust;
   6231 	  if (no_contents)
   6232 	    {
   6233 	      /* We shouldn't need to align the segment on disk since
   6234 		 the segment doesn't need file space, but the gABI
   6235 		 arguably requires the alignment and glibc ld.so
   6236 		 checks it.  So to comply with the alignment
   6237 		 requirement but not waste file space, we adjust
   6238 		 p_offset for just this segment.  (OFF_ADJUST is
   6239 		 subtracted from OFF later.)  This may put p_offset
   6240 		 past the end of file, but that shouldn't matter.  */
   6241 	    }
   6242 	  else
   6243 	    off_adjust = 0;
   6244 	}
   6245       /* Make sure the .dynamic section is the first section in the
   6246 	 PT_DYNAMIC segment.  */
   6247       else if (p->p_type == PT_DYNAMIC
   6248 	       && m->count > 1
   6249 	       && strcmp (m->sections[0]->name, ".dynamic") != 0)
   6250 	{
   6251 	  _bfd_error_handler
   6252 	    (_("%pB: The first section in the PT_DYNAMIC segment"
   6253 	       " is not the .dynamic section"),
   6254 	     abfd);
   6255 	  bfd_set_error (bfd_error_bad_value);
   6256 	  return false;
   6257 	}
   6258       /* Set the note section type to SHT_NOTE.  */
   6259       else if (p->p_type == PT_NOTE)
   6260 	for (i = 0; i < m->count; i++)
   6261 	  elf_section_type (m->sections[i]) = SHT_NOTE;
   6262 
   6263       if (m->includes_filehdr)
   6264 	{
   6265 	  if (!m->p_flags_valid)
   6266 	    p->p_flags |= PF_R;
   6267 	  p->p_filesz = bed->s->sizeof_ehdr;
   6268 	  p->p_memsz = bed->s->sizeof_ehdr;
   6269 	  if (p->p_type == PT_LOAD)
   6270 	    {
   6271 	      if (m->count > 0)
   6272 		{
   6273 		  if (p->p_vaddr < (bfd_vma) off
   6274 		      || (!m->p_paddr_valid
   6275 			  && p->p_paddr < (bfd_vma) off))
   6276 		    {
   6277 		      _bfd_error_handler
   6278 			(_("%pB: not enough room for program headers,"
   6279 			   " try linking with -N"),
   6280 			 abfd);
   6281 		      bfd_set_error (bfd_error_bad_value);
   6282 		      return false;
   6283 		    }
   6284 		  p->p_vaddr -= off;
   6285 		  if (!m->p_paddr_valid)
   6286 		    p->p_paddr -= off;
   6287 		}
   6288 	    }
   6289 	  else if (sorted_seg_map[0]->includes_filehdr)
   6290 	    {
   6291 	      Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
   6292 	      p->p_vaddr = filehdr->p_vaddr;
   6293 	      if (!m->p_paddr_valid)
   6294 		p->p_paddr = filehdr->p_paddr;
   6295 	    }
   6296 	}
   6297 
   6298       if (m->includes_phdrs)
   6299 	{
   6300 	  if (!m->p_flags_valid)
   6301 	    p->p_flags |= PF_R;
   6302 	  p->p_filesz += actual * bed->s->sizeof_phdr;
   6303 	  p->p_memsz += actual * bed->s->sizeof_phdr;
   6304 	  if (!m->includes_filehdr)
   6305 	    {
   6306 	      if (p->p_type == PT_LOAD)
   6307 		{
   6308 		  elf_elfheader (abfd)->e_phoff = p->p_offset;
   6309 		  if (m->count > 0)
   6310 		    {
   6311 		      p->p_vaddr -= off - p->p_offset;
   6312 		      if (!m->p_paddr_valid)
   6313 			p->p_paddr -= off - p->p_offset;
   6314 		    }
   6315 		}
   6316 	      else if (phdr_load_seg != NULL)
   6317 		{
   6318 		  Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
   6319 		  bfd_vma phdr_off = 0;  /* Octets.  */
   6320 		  if (phdr_load_seg->includes_filehdr)
   6321 		    phdr_off = bed->s->sizeof_ehdr;
   6322 		  p->p_vaddr = phdr->p_vaddr + phdr_off;
   6323 		  if (!m->p_paddr_valid)
   6324 		    p->p_paddr = phdr->p_paddr + phdr_off;
   6325 		  p->p_offset = phdr->p_offset + phdr_off;
   6326 		}
   6327 	      else
   6328 		p->p_offset = bed->s->sizeof_ehdr;
   6329 	    }
   6330 	}
   6331 
   6332       if (p->p_type == PT_LOAD
   6333 	  || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
   6334 	{
   6335 	  if (!m->includes_filehdr && !m->includes_phdrs)
   6336 	    {
   6337 	      p->p_offset = off;
   6338 	      if (no_contents)
   6339 		{
   6340 		  /* Put meaningless p_offset for PT_LOAD segments
   6341 		     without file contents somewhere within the first
   6342 		     page, in an attempt to not point past EOF.  */
   6343 		  bfd_size_type align = maxpagesize;
   6344 		  if (align < p->p_align)
   6345 		    align = p->p_align;
   6346 		  if (align < 1)
   6347 		    align = 1;
   6348 		  p->p_offset = off % align;
   6349 		}
   6350 	    }
   6351 	  else
   6352 	    {
   6353 	      file_ptr adjust;  /* Octets.  */
   6354 
   6355 	      adjust = off - (p->p_offset + p->p_filesz);
   6356 	      if (!no_contents)
   6357 		p->p_filesz += adjust;
   6358 	      p->p_memsz += adjust;
   6359 	    }
   6360 	}
   6361 
   6362       /* Set up p_filesz, p_memsz, p_align and p_flags from the section
   6363 	 maps.  Set filepos for sections in PT_LOAD segments, and in
   6364 	 core files, for sections in PT_NOTE segments.
   6365 	 assign_file_positions_for_non_load_sections will set filepos
   6366 	 for other sections and update p_filesz for other segments.  */
   6367       for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   6368 	{
   6369 	  asection *sec;
   6370 	  bfd_size_type align;
   6371 	  Elf_Internal_Shdr *this_hdr;
   6372 
   6373 	  sec = *secpp;
   6374 	  this_hdr = &elf_section_data (sec)->this_hdr;
   6375 	  align = (bfd_size_type) 1 << bfd_section_alignment (sec);
   6376 
   6377 	  if ((p->p_type == PT_LOAD
   6378 	       || p->p_type == PT_TLS)
   6379 	      && (this_hdr->sh_type != SHT_NOBITS
   6380 		  || ((this_hdr->sh_flags & SHF_ALLOC) != 0
   6381 		      && ((this_hdr->sh_flags & SHF_TLS) == 0
   6382 			  || p->p_type == PT_TLS))))
   6383 	    {
   6384 	      bfd_vma p_start = p->p_paddr;		/* Octets.  */
   6385 	      bfd_vma p_end = p_start + p->p_memsz;	/* Octets.  */
   6386 	      bfd_vma s_start = sec->lma * opb;		/* Octets.  */
   6387 	      bfd_vma adjust = s_start - p_end;		/* Octets.  */
   6388 
   6389 	      if (adjust != 0
   6390 		  && (s_start < p_end
   6391 		      || p_end < p_start))
   6392 		{
   6393 		  _bfd_error_handler
   6394 		    /* xgettext:c-format */
   6395 		    (_("%pB: section %pA lma %#" PRIx64
   6396 		       " adjusted to %#" PRIx64),
   6397 		     abfd, sec, (uint64_t) s_start / opb,
   6398 		     (uint64_t) p_end / opb);
   6399 		  adjust = 0;
   6400 		  sec->lma = p_end / opb;
   6401 		}
   6402 	      p->p_memsz += adjust;
   6403 
   6404 	      if (p->p_type == PT_LOAD)
   6405 		{
   6406 		  if (this_hdr->sh_type != SHT_NOBITS)
   6407 		    {
   6408 		      off_adjust = 0;
   6409 		      if (p->p_filesz + adjust < p->p_memsz)
   6410 			{
   6411 			  /* We have a PROGBITS section following NOBITS ones.
   6412 			     Allocate file space for the NOBITS section(s).
   6413 			     We don't need to write out the zeros, posix
   6414 			     fseek past the end of data already written
   6415 			     followed by a write at that location is
   6416 			     guaranteed to result in zeros being read
   6417 			     from the gap.  */
   6418 			  adjust = p->p_memsz - p->p_filesz;
   6419 			}
   6420 		    }
   6421 		  /* We only adjust sh_offset in SHT_NOBITS sections
   6422 		     as would seem proper for their address when the
   6423 		     section is first in the segment.  sh_offset
   6424 		     doesn't really have any significance for
   6425 		     SHT_NOBITS anyway, apart from a notional position
   6426 		     relative to other sections.  Historically we
   6427 		     didn't bother with adjusting sh_offset and some
   6428 		     programs depend on it not being adjusted.  See
   6429 		     pr12921 and pr25662.  */
   6430 		  if (this_hdr->sh_type != SHT_NOBITS || i == 0)
   6431 		    {
   6432 		      off += adjust;
   6433 		      if (this_hdr->sh_type == SHT_NOBITS)
   6434 			off_adjust += adjust;
   6435 		    }
   6436 		}
   6437 	      if (this_hdr->sh_type != SHT_NOBITS)
   6438 		p->p_filesz += adjust;
   6439 	    }
   6440 
   6441 	  if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
   6442 	    {
   6443 	      /* The section at i == 0 is the one that actually contains
   6444 		 everything.  */
   6445 	      if (i == 0)
   6446 		{
   6447 		  this_hdr->sh_offset = sec->filepos = off;
   6448 		  off += this_hdr->sh_size;
   6449 		  p->p_filesz = this_hdr->sh_size;
   6450 		  p->p_memsz = 0;
   6451 		  p->p_align = 1;
   6452 		}
   6453 	      else
   6454 		{
   6455 		  /* The rest are fake sections that shouldn't be written.  */
   6456 		  sec->filepos = 0;
   6457 		  sec->size = 0;
   6458 		  sec->flags = 0;
   6459 		  continue;
   6460 		}
   6461 	    }
   6462 	  else
   6463 	    {
   6464 	      if (p->p_type == PT_LOAD)
   6465 		{
   6466 		  this_hdr->sh_offset = sec->filepos = off;
   6467 		  if (this_hdr->sh_type != SHT_NOBITS)
   6468 		    off += this_hdr->sh_size;
   6469 		}
   6470 	      else if (this_hdr->sh_type == SHT_NOBITS
   6471 		       && (this_hdr->sh_flags & SHF_TLS) != 0
   6472 		       && this_hdr->sh_offset == 0)
   6473 		{
   6474 		  /* This is a .tbss section that didn't get a PT_LOAD.
   6475 		     (See _bfd_elf_map_sections_to_segments "Create a
   6476 		     final PT_LOAD".)  Set sh_offset to the value it
   6477 		     would have if we had created a zero p_filesz and
   6478 		     p_memsz PT_LOAD header for the section.  This
   6479 		     also makes the PT_TLS header have the same
   6480 		     p_offset value.  */
   6481 		  bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
   6482 							  off, align);
   6483 		  this_hdr->sh_offset = sec->filepos = off + adjust;
   6484 		}
   6485 
   6486 	      if (this_hdr->sh_type != SHT_NOBITS)
   6487 		{
   6488 		  p->p_filesz += this_hdr->sh_size;
   6489 		  /* A load section without SHF_ALLOC is something like
   6490 		     a note section in a PT_NOTE segment.  These take
   6491 		     file space but are not loaded into memory.  */
   6492 		  if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   6493 		    p->p_memsz += this_hdr->sh_size;
   6494 		}
   6495 	      else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   6496 		{
   6497 		  if (p->p_type == PT_TLS)
   6498 		    p->p_memsz += this_hdr->sh_size;
   6499 
   6500 		  /* .tbss is special.  It doesn't contribute to p_memsz of
   6501 		     normal segments.  */
   6502 		  else if ((this_hdr->sh_flags & SHF_TLS) == 0)
   6503 		    p->p_memsz += this_hdr->sh_size;
   6504 		}
   6505 
   6506 	      if (align > p->p_align
   6507 		  && !m->p_align_valid
   6508 		  && (p->p_type != PT_LOAD
   6509 		      || (abfd->flags & D_PAGED) == 0))
   6510 		p->p_align = align;
   6511 	    }
   6512 
   6513 	  if (!m->p_flags_valid)
   6514 	    {
   6515 	      p->p_flags |= PF_R;
   6516 	      if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
   6517 		p->p_flags |= PF_X;
   6518 	      if ((this_hdr->sh_flags & SHF_WRITE) != 0)
   6519 		p->p_flags |= PF_W;
   6520 	    }
   6521 	}
   6522 
   6523       off -= off_adjust;
   6524 
   6525       /* PR ld/20815 - Check that the program header segment, if
   6526 	 present, will be loaded into memory.  */
   6527       if (p->p_type == PT_PHDR
   6528 	  && phdr_load_seg == NULL
   6529 	  && !(bed->elf_backend_allow_non_load_phdr != NULL
   6530 	       && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
   6531 	{
   6532 	  /* The fix for this error is usually to edit the linker script being
   6533 	     used and set up the program headers manually.  Either that or
   6534 	     leave room for the headers at the start of the SECTIONS.  */
   6535 	  _bfd_error_handler (_("%pB: error: PHDR segment not covered"
   6536 				" by LOAD segment"),
   6537 			      abfd);
   6538 	  if (link_info == NULL)
   6539 	    return false;
   6540 	  /* Arrange for the linker to exit with an error, deleting
   6541 	     the output file unless --noinhibit-exec is given.  */
   6542 	  link_info->callbacks->info ("%X");
   6543 	}
   6544 
   6545       /* Check that all sections are in a PT_LOAD segment.
   6546 	 Don't check funky gdb generated core files.  */
   6547       if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
   6548 	{
   6549 	  bool check_vma = true;
   6550 
   6551 	  for (i = 1; i < m->count; i++)
   6552 	    if (m->sections[i]->vma == m->sections[i - 1]->vma
   6553 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
   6554 				       ->this_hdr), p) != 0
   6555 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
   6556 				       ->this_hdr), p) != 0)
   6557 	      {
   6558 		/* Looks like we have overlays packed into the segment.  */
   6559 		check_vma = false;
   6560 		break;
   6561 	      }
   6562 
   6563 	  for (i = 0; i < m->count; i++)
   6564 	    {
   6565 	      Elf_Internal_Shdr *this_hdr;
   6566 	      asection *sec;
   6567 
   6568 	      sec = m->sections[i];
   6569 	      this_hdr = &(elf_section_data(sec)->this_hdr);
   6570 	      if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
   6571 		  && !ELF_TBSS_SPECIAL (this_hdr, p))
   6572 		{
   6573 		  _bfd_error_handler
   6574 		    /* xgettext:c-format */
   6575 		    (_("%pB: section `%pA' can't be allocated in segment %d"),
   6576 		     abfd, sec, j);
   6577 		  print_segment_map (m);
   6578 		}
   6579 	    }
   6580 
   6581 	  if (p_align_p)
   6582 	    p->p_align = p_align;
   6583 	}
   6584     }
   6585 
   6586   elf_next_file_pos (abfd) = off;
   6587 
   6588   if (link_info != NULL
   6589       && phdr_load_seg != NULL
   6590       && phdr_load_seg->includes_filehdr)
   6591     {
   6592       /* There is a segment that contains both the file headers and the
   6593 	 program headers, so provide a symbol __ehdr_start pointing there.
   6594 	 A program can use this to examine itself robustly.  */
   6595 
   6596       struct elf_link_hash_entry *hash
   6597 	= elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
   6598 				false, false, true);
   6599       /* If the symbol was referenced and not defined, define it.  */
   6600       if (hash != NULL
   6601 	  && (hash->root.type == bfd_link_hash_new
   6602 	      || hash->root.type == bfd_link_hash_undefined
   6603 	      || hash->root.type == bfd_link_hash_undefweak
   6604 	      || hash->root.type == bfd_link_hash_common))
   6605 	{
   6606 	  asection *s = NULL;
   6607 	  bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
   6608 
   6609 	  if (phdr_load_seg->count != 0)
   6610 	    /* The segment contains sections, so use the first one.  */
   6611 	    s = phdr_load_seg->sections[0];
   6612 	  else
   6613 	    /* Use the first (i.e. lowest-addressed) section in any segment.  */
   6614 	    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   6615 	      if (m->p_type == PT_LOAD && m->count != 0)
   6616 		{
   6617 		  s = m->sections[0];
   6618 		  break;
   6619 		}
   6620 
   6621 	  if (s != NULL)
   6622 	    {
   6623 	      hash->root.u.def.value = filehdr_vaddr - s->vma;
   6624 	      hash->root.u.def.section = s;
   6625 	    }
   6626 	  else
   6627 	    {
   6628 	      hash->root.u.def.value = filehdr_vaddr;
   6629 	      hash->root.u.def.section = bfd_abs_section_ptr;
   6630 	    }
   6631 
   6632 	  hash->root.type = bfd_link_hash_defined;
   6633 	  hash->def_regular = 1;
   6634 	  hash->non_elf = 0;
   6635 	}
   6636     }
   6637 
   6638   return true;
   6639 }
   6640 
   6641 /* Determine if a bfd is a debuginfo file.  Unfortunately there
   6642    is no defined method for detecting such files, so we have to
   6643    use heuristics instead.  */
   6644 
   6645 bool
   6646 is_debuginfo_file (bfd *abfd)
   6647 {
   6648   if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   6649     return false;
   6650 
   6651   Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
   6652   Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
   6653   Elf_Internal_Shdr **headerp;
   6654 
   6655   for (headerp = start_headers; headerp < end_headers; headerp ++)
   6656     {
   6657       Elf_Internal_Shdr *header = * headerp;
   6658 
   6659       /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
   6660 	 The only allocated sections are SHT_NOBITS or SHT_NOTES.  */
   6661       if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
   6662 	  && header->sh_type != SHT_NOBITS
   6663 	  && header->sh_type != SHT_NOTE)
   6664 	return false;
   6665     }
   6666 
   6667   return true;
   6668 }
   6669 
   6670 /* Assign file positions for other sections, except for compressed debug
   6671    and sections assigned in _bfd_elf_assign_file_positions_for_non_load.  */
   6672 
   6673 static bool
   6674 assign_file_positions_for_non_load_sections (bfd *abfd,
   6675 					     struct bfd_link_info *link_info)
   6676 {
   6677   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6678   Elf_Internal_Shdr **i_shdrpp;
   6679   Elf_Internal_Shdr **hdrpp, **end_hdrpp;
   6680   Elf_Internal_Phdr *phdrs;
   6681   Elf_Internal_Phdr *p;
   6682   struct elf_segment_map *m;
   6683   file_ptr off;
   6684   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   6685   bfd_vma maxpagesize;
   6686 
   6687   if (link_info != NULL)
   6688     maxpagesize = link_info->maxpagesize;
   6689   else
   6690     maxpagesize = bed->maxpagesize;
   6691   i_shdrpp = elf_elfsections (abfd);
   6692   end_hdrpp = i_shdrpp + elf_numsections (abfd);
   6693   off = elf_next_file_pos (abfd);
   6694   for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
   6695     {
   6696       Elf_Internal_Shdr *hdr;
   6697       bfd_vma align;
   6698 
   6699       hdr = *hdrpp;
   6700       if (hdr->bfd_section != NULL
   6701 	  && (hdr->bfd_section->filepos != 0
   6702 	      || (hdr->sh_type == SHT_NOBITS
   6703 		  && hdr->contents == NULL)))
   6704 	BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
   6705       else if ((hdr->sh_flags & SHF_ALLOC) != 0)
   6706 	{
   6707 	  if (hdr->sh_size != 0
   6708 	      /* PR 24717 - debuginfo files are known to be not strictly
   6709 		 compliant with the ELF standard.  In particular they often
   6710 		 have .note.gnu.property sections that are outside of any
   6711 		 loadable segment.  This is not a problem for such files,
   6712 		 so do not warn about them.  */
   6713 	      && ! is_debuginfo_file (abfd))
   6714 	    _bfd_error_handler
   6715 	      /* xgettext:c-format */
   6716 	      (_("%pB: warning: allocated section `%s' not in segment"),
   6717 	       abfd,
   6718 	       (hdr->bfd_section == NULL
   6719 		? "*unknown*"
   6720 		: hdr->bfd_section->name));
   6721 	  /* We don't need to page align empty sections.  */
   6722 	  if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
   6723 	    align = maxpagesize;
   6724 	  else
   6725 	    align = hdr->sh_addralign & -hdr->sh_addralign;
   6726 	  off += vma_page_aligned_bias (hdr->sh_addr, off, align);
   6727 	  off = _bfd_elf_assign_file_position_for_section (hdr, off,
   6728 							   false);
   6729 	}
   6730       else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   6731 		&& hdr->bfd_section == NULL)
   6732 	       /* We don't know the offset of these sections yet:
   6733 		  their size has not been decided.  */
   6734 	       || (abfd->is_linker_output
   6735 		   && hdr->bfd_section != NULL
   6736 		   && (hdr->sh_name == -1u
   6737 		       || bfd_section_is_ctf (hdr->bfd_section)))
   6738 	       || hdr == i_shdrpp[elf_onesymtab (abfd)]
   6739 	       || (elf_symtab_shndx_list (abfd) != NULL
   6740 		   && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   6741 	       || hdr == i_shdrpp[elf_strtab_sec (abfd)]
   6742 	       || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
   6743 	hdr->sh_offset = -1;
   6744       else
   6745 	off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   6746     }
   6747   elf_next_file_pos (abfd) = off;
   6748 
   6749   /* Now that we have set the section file positions, we can set up
   6750      the file positions for the non PT_LOAD segments.  */
   6751   phdrs = elf_tdata (abfd)->phdr;
   6752   for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
   6753     {
   6754       if (p->p_type == PT_GNU_RELRO)
   6755 	{
   6756 	  bfd_vma start, end;  /* Bytes.  */
   6757 	  bool ok;
   6758 
   6759 	  if (link_info != NULL)
   6760 	    {
   6761 	      /* During linking the range of the RELRO segment is passed
   6762 		 in link_info.  Note that there may be padding between
   6763 		 relro_start and the first RELRO section.  */
   6764 	      start = link_info->relro_start;
   6765 	      end = link_info->relro_end;
   6766 	    }
   6767 	  else if (m->count != 0)
   6768 	    {
   6769 	      if (!m->p_size_valid)
   6770 		abort ();
   6771 	      start = m->sections[0]->vma;
   6772 	      end = start + m->p_size / opb;
   6773 	    }
   6774 	  else
   6775 	    {
   6776 	      start = 0;
   6777 	      end = 0;
   6778 	    }
   6779 
   6780 	  ok = false;
   6781 	  if (start < end)
   6782 	    {
   6783 	      struct elf_segment_map *lm;
   6784 	      const Elf_Internal_Phdr *lp;
   6785 	      unsigned int i;
   6786 
   6787 	      /* Find a LOAD segment containing a section in the RELRO
   6788 		 segment.  */
   6789 	      for (lm = elf_seg_map (abfd), lp = phdrs;
   6790 		   lm != NULL;
   6791 		   lm = lm->next, lp++)
   6792 		{
   6793 		  if (lp->p_type == PT_LOAD
   6794 		      && lm->count != 0
   6795 		      && (lm->sections[lm->count - 1]->vma
   6796 			  + (!IS_TBSS (lm->sections[lm->count - 1])
   6797 			     ? lm->sections[lm->count - 1]->size / opb
   6798 			     : 0)) > start
   6799 		      && lm->sections[0]->vma < end)
   6800 		    break;
   6801 		}
   6802 
   6803 	      if (lm != NULL)
   6804 		{
   6805 		  /* Find the section starting the RELRO segment.  */
   6806 		  for (i = 0; i < lm->count; i++)
   6807 		    {
   6808 		      asection *s = lm->sections[i];
   6809 		      if (s->vma >= start
   6810 			  && s->vma < end
   6811 			  && s->size != 0)
   6812 			break;
   6813 		    }
   6814 
   6815 		  if (i < lm->count)
   6816 		    {
   6817 		      p->p_vaddr = lm->sections[i]->vma * opb;
   6818 		      p->p_paddr = lm->sections[i]->lma * opb;
   6819 		      p->p_offset = lm->sections[i]->filepos;
   6820 		      p->p_memsz = end * opb - p->p_vaddr;
   6821 		      p->p_filesz = p->p_memsz;
   6822 
   6823 		      /* The RELRO segment typically ends a few bytes
   6824 			 into .got.plt but other layouts are possible.
   6825 			 In cases where the end does not match any
   6826 			 loaded section (for instance is in file
   6827 			 padding), trim p_filesz back to correspond to
   6828 			 the end of loaded section contents.  */
   6829 		      if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
   6830 			p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
   6831 
   6832 		      /* Preserve the alignment and flags if they are
   6833 			 valid.  The gold linker generates RW/4 for
   6834 			 the PT_GNU_RELRO section.  It is better for
   6835 			 objcopy/strip to honor these attributes
   6836 			 otherwise gdb will choke when using separate
   6837 			 debug files.  */
   6838 		      if (!m->p_align_valid)
   6839 			p->p_align = 1;
   6840 		      if (!m->p_flags_valid)
   6841 			p->p_flags = PF_R;
   6842 		      ok = true;
   6843 		    }
   6844 		}
   6845 	    }
   6846 
   6847 	  if (!ok)
   6848 	    {
   6849 	      if (link_info != NULL)
   6850 		_bfd_error_handler
   6851 		  (_("%pB: warning: unable to allocate any sections"
   6852 		     " to PT_GNU_RELRO segment"),
   6853 		   abfd);
   6854 	      memset (p, 0, sizeof *p);
   6855 	    }
   6856 	}
   6857       else if (p->p_type == PT_GNU_STACK)
   6858 	{
   6859 	  if (m->p_size_valid)
   6860 	    p->p_memsz = m->p_size;
   6861 	}
   6862       else if (m->count != 0)
   6863 	{
   6864 	  unsigned int i;
   6865 
   6866 	  if (p->p_type != PT_LOAD
   6867 	      && (p->p_type != PT_NOTE
   6868 		  || bfd_get_format (abfd) != bfd_core))
   6869 	    {
   6870 	      /* A user specified segment layout may include a PHDR
   6871 		 segment that overlaps with a LOAD segment...  */
   6872 	      if (p->p_type == PT_PHDR)
   6873 		{
   6874 		  m->count = 0;
   6875 		  continue;
   6876 		}
   6877 
   6878 	      if (m->includes_filehdr || m->includes_phdrs)
   6879 		{
   6880 		  /* PR 17512: file: 2195325e.  */
   6881 		  _bfd_error_handler
   6882 		    (_("%pB: error: non-load segment %d includes file header "
   6883 		       "and/or program header"),
   6884 		     abfd, (int) (p - phdrs));
   6885 		  return false;
   6886 		}
   6887 
   6888 	      p->p_filesz = 0;
   6889 	      p->p_offset = m->sections[0]->filepos;
   6890 	      for (i = m->count; i-- != 0;)
   6891 		{
   6892 		  asection *sect = m->sections[i];
   6893 		  Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
   6894 		  if (hdr->sh_type != SHT_NOBITS)
   6895 		    {
   6896 		      p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
   6897 		      /* NB: p_memsz of the loadable PT_NOTE segment
   6898 			 should be the same as p_filesz.  */
   6899 		      if (p->p_type == PT_NOTE
   6900 			  && (hdr->sh_flags & SHF_ALLOC) != 0)
   6901 			p->p_memsz = p->p_filesz;
   6902 		      break;
   6903 		    }
   6904 		}
   6905 	    }
   6906 	}
   6907     }
   6908 
   6909   return true;
   6910 }
   6911 
   6912 static elf_section_list *
   6913 find_section_in_list (unsigned int i, elf_section_list * list)
   6914 {
   6915   for (;list != NULL; list = list->next)
   6916     if (list->ndx == i)
   6917       break;
   6918   return list;
   6919 }
   6920 
   6921 /* Work out the file positions of all the sections.  This is called by
   6922    _bfd_elf_compute_section_file_positions.  All the section sizes and
   6923    VMAs must be known before this is called.
   6924 
   6925    Reloc sections come in two flavours: Those processed specially as
   6926    "side-channel" data attached to a section to which they apply, and
   6927    those that bfd doesn't process as relocations.  The latter sort are
   6928    stored in a normal bfd section by bfd_section_from_shdr.  We don't
   6929    consider the former sort here, unless they form part of the loadable
   6930    image.  Reloc sections not assigned here (and compressed debugging
   6931    sections and CTF sections which nothing else in the file can rely
   6932    upon) will be handled later by assign_file_positions_for_relocs.
   6933 
   6934    We also don't set the positions of the .symtab and .strtab here.  */
   6935 
   6936 static bool
   6937 assign_file_positions_except_relocs (bfd *abfd,
   6938 				     struct bfd_link_info *link_info)
   6939 {
   6940   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   6941   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
   6942   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6943   unsigned int alloc;
   6944 
   6945   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
   6946       && bfd_get_format (abfd) != bfd_core)
   6947     {
   6948       Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
   6949       unsigned int num_sec = elf_numsections (abfd);
   6950       Elf_Internal_Shdr **hdrpp;
   6951       unsigned int i;
   6952       file_ptr off;
   6953 
   6954       /* Start after the ELF header.  */
   6955       off = i_ehdrp->e_ehsize;
   6956 
   6957       /* We are not creating an executable, which means that we are
   6958 	 not creating a program header, and that the actual order of
   6959 	 the sections in the file is unimportant.  */
   6960       for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
   6961 	{
   6962 	  Elf_Internal_Shdr *hdr;
   6963 
   6964 	  hdr = *hdrpp;
   6965 	  if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   6966 	       && hdr->bfd_section == NULL)
   6967 	      /* Do not assign offsets for these sections yet: we don't know
   6968 		 their sizes.  */
   6969 	      || (abfd->is_linker_output
   6970 		  && hdr->bfd_section != NULL
   6971 		  && (hdr->sh_name == -1u
   6972 		      || bfd_section_is_ctf (hdr->bfd_section)))
   6973 	      || i == elf_onesymtab (abfd)
   6974 	      || (elf_symtab_shndx_list (abfd) != NULL
   6975 		  && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   6976 	      || i == elf_strtab_sec (abfd)
   6977 	      || i == elf_shstrtab_sec (abfd))
   6978 	    {
   6979 	      hdr->sh_offset = -1;
   6980 	    }
   6981 	  else
   6982 	    off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   6983 	}
   6984 
   6985       elf_next_file_pos (abfd) = off;
   6986       elf_program_header_size (abfd) = 0;
   6987     }
   6988   else
   6989     {
   6990       /* Assign file positions for the loaded sections based on the
   6991 	 assignment of sections to segments.  */
   6992       if (!assign_file_positions_for_load_sections (abfd, link_info))
   6993 	return false;
   6994 
   6995       /* And for non-load sections.  */
   6996       if (!assign_file_positions_for_non_load_sections (abfd, link_info))
   6997 	return false;
   6998     }
   6999 
   7000   if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
   7001     return false;
   7002 
   7003   /* Write out the program headers.  */
   7004   alloc = i_ehdrp->e_phnum;
   7005   if (alloc != 0)
   7006     {
   7007       if (link_info != NULL && ! link_info->no_warn_rwx_segments)
   7008 	{
   7009 	  bool warned_tls = false;
   7010 	  bool warned_rwx = false;
   7011 
   7012 	  /* Memory resident segments with non-zero size and RWX
   7013 	     permissions are a security risk, so we generate a warning
   7014 	     here if we are creating any.  */
   7015 	  unsigned int i;
   7016 
   7017 	  for (i = 0; i < alloc; i++)
   7018 	    {
   7019 	      const Elf_Internal_Phdr * phdr = tdata->phdr + i;
   7020 
   7021 	      if (phdr->p_memsz == 0)
   7022 		continue;
   7023 
   7024 	      if (! warned_tls
   7025 		  && phdr->p_type == PT_TLS
   7026 		  && (phdr->p_flags & PF_X))
   7027 		{
   7028 		  if (link_info->warn_is_error_for_rwx_segments)
   7029 		    {
   7030 		      _bfd_error_handler (_("\
   7031 error: %pB has a TLS segment with execute permission"),
   7032 					  abfd);
   7033 		      return false;
   7034 		    }
   7035 
   7036 		  _bfd_error_handler (_("\
   7037 warning: %pB has a TLS segment with execute permission"),
   7038 				      abfd);
   7039 		  if (warned_rwx)
   7040 		    break;
   7041 
   7042 		  warned_tls = true;
   7043 		}
   7044 	      else if (! warned_rwx
   7045 		       && phdr->p_type == PT_LOAD
   7046 		       && ((phdr->p_flags & (PF_R | PF_W | PF_X))
   7047 			   == (PF_R | PF_W | PF_X)))
   7048 		{
   7049 		  if (link_info->warn_is_error_for_rwx_segments)
   7050 		    {
   7051 		      _bfd_error_handler (_("\
   7052 error: %pB has a LOAD segment with RWX permissions"),
   7053 					  abfd);
   7054 		      return false;
   7055 		    }
   7056 
   7057 		  _bfd_error_handler (_("\
   7058 warning: %pB has a LOAD segment with RWX permissions"),
   7059 				      abfd);
   7060 		  if (warned_tls)
   7061 		    break;
   7062 
   7063 		  warned_rwx = true;
   7064 		}
   7065 	    }
   7066 	}
   7067 
   7068       if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
   7069 	  || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
   7070 	return false;
   7071     }
   7072 
   7073   return true;
   7074 }
   7075 
   7076 bool
   7077 _bfd_elf_init_file_header (bfd *abfd,
   7078 			   struct bfd_link_info *info ATTRIBUTE_UNUSED)
   7079 {
   7080   Elf_Internal_Ehdr *i_ehdrp;	/* Elf file header, internal form.  */
   7081   struct elf_strtab_hash *shstrtab;
   7082   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   7083 
   7084   i_ehdrp = elf_elfheader (abfd);
   7085 
   7086   shstrtab = _bfd_elf_strtab_init ();
   7087   if (shstrtab == NULL)
   7088     return false;
   7089 
   7090   elf_shstrtab (abfd) = shstrtab;
   7091 
   7092   i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
   7093   i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
   7094   i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
   7095   i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
   7096 
   7097   i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
   7098   i_ehdrp->e_ident[EI_DATA] =
   7099     bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
   7100   i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
   7101 
   7102   if ((abfd->flags & DYNAMIC) != 0)
   7103     i_ehdrp->e_type = ET_DYN;
   7104   else if ((abfd->flags & EXEC_P) != 0)
   7105     i_ehdrp->e_type = ET_EXEC;
   7106   else if (bfd_get_format (abfd) == bfd_core)
   7107     i_ehdrp->e_type = ET_CORE;
   7108   else
   7109     i_ehdrp->e_type = ET_REL;
   7110 
   7111   switch (bfd_get_arch (abfd))
   7112     {
   7113     case bfd_arch_unknown:
   7114       i_ehdrp->e_machine = EM_NONE;
   7115       break;
   7116 
   7117       /* There used to be a long list of cases here, each one setting
   7118 	 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
   7119 	 in the corresponding bfd definition.  To avoid duplication,
   7120 	 the switch was removed.  Machines that need special handling
   7121 	 can generally do it in elf_backend_final_write_processing(),
   7122 	 unless they need the information earlier than the final write.
   7123 	 Such need can generally be supplied by replacing the tests for
   7124 	 e_machine with the conditions used to determine it.  */
   7125     default:
   7126       i_ehdrp->e_machine = bed->elf_machine_code;
   7127     }
   7128 
   7129   i_ehdrp->e_version = bed->s->ev_current;
   7130   i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
   7131 
   7132   /* No program header, for now.  */
   7133   i_ehdrp->e_phoff = 0;
   7134   i_ehdrp->e_phentsize = 0;
   7135   i_ehdrp->e_phnum = 0;
   7136 
   7137   /* Each bfd section is section header entry.  */
   7138   i_ehdrp->e_entry = bfd_get_start_address (abfd);
   7139   i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
   7140 
   7141   elf_tdata (abfd)->symtab_hdr.sh_name =
   7142     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
   7143   elf_tdata (abfd)->strtab_hdr.sh_name =
   7144     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
   7145   elf_tdata (abfd)->shstrtab_hdr.sh_name =
   7146     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
   7147   if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
   7148       || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
   7149       || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
   7150     return false;
   7151 
   7152   return true;
   7153 }
   7154 
   7155 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
   7156 
   7157    FIXME: We used to have code here to sort the PT_LOAD segments into
   7158    ascending order, as per the ELF spec.  But this breaks some programs,
   7159    including the Linux kernel.  But really either the spec should be
   7160    changed or the programs updated.  */
   7161 
   7162 bool
   7163 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
   7164 {
   7165   if (link_info != NULL && bfd_link_pie (link_info))
   7166     {
   7167       Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
   7168       unsigned int num_segments = i_ehdrp->e_phnum;
   7169       struct elf_obj_tdata *tdata = elf_tdata (obfd);
   7170       Elf_Internal_Phdr *segment = tdata->phdr;
   7171       Elf_Internal_Phdr *end_segment = &segment[num_segments];
   7172 
   7173       /* Find the lowest p_vaddr in PT_LOAD segments.  */
   7174       bfd_vma p_vaddr = (bfd_vma) -1;
   7175       for (; segment < end_segment; segment++)
   7176 	if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
   7177 	  p_vaddr = segment->p_vaddr;
   7178 
   7179       /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
   7180 	 segments is non-zero.  */
   7181       if (p_vaddr)
   7182 	i_ehdrp->e_type = ET_EXEC;
   7183     }
   7184   return true;
   7185 }
   7186 
   7187 /* Assign file positions for all the reloc sections which are not part
   7188    of the loadable file image, and the file position of section headers.  */
   7189 
   7190 static bool
   7191 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
   7192 {
   7193   file_ptr off;
   7194   Elf_Internal_Shdr **shdrpp, **end_shdrpp;
   7195   Elf_Internal_Shdr *shdrp;
   7196   Elf_Internal_Ehdr *i_ehdrp;
   7197   const struct elf_backend_data *bed;
   7198 
   7199   /* Skip non-load sections without section header.  */
   7200   if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
   7201     return true;
   7202 
   7203   off = elf_next_file_pos (abfd);
   7204 
   7205   shdrpp = elf_elfsections (abfd);
   7206   end_shdrpp = shdrpp + elf_numsections (abfd);
   7207   for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
   7208     {
   7209       shdrp = *shdrpp;
   7210       if (shdrp->sh_offset == -1)
   7211 	{
   7212 	  asection *sec = shdrp->bfd_section;
   7213 	  if (sec == NULL
   7214 	      || shdrp->sh_type == SHT_REL
   7215 	      || shdrp->sh_type == SHT_RELA)
   7216 	    ;
   7217 	  else if (bfd_section_is_ctf (sec))
   7218 	    {
   7219 	      /* Update section size and contents.	*/
   7220 	      shdrp->sh_size = sec->size;
   7221 	      shdrp->contents = sec->contents;
   7222 	    }
   7223 	  else if (shdrp->sh_name == -1u)
   7224 	    {
   7225 	      const char *name = sec->name;
   7226 	      struct bfd_elf_section_data *d;
   7227 
   7228 	      /* Compress DWARF debug sections.  */
   7229 	      if (!bfd_compress_section (abfd, sec, shdrp->contents))
   7230 		return false;
   7231 
   7232 	      if (sec->compress_status == COMPRESS_SECTION_DONE
   7233 		  && (abfd->flags & BFD_COMPRESS_GABI) == 0
   7234 		  && name[1] == 'd')
   7235 		{
   7236 		  /* If section is compressed with zlib-gnu, convert
   7237 		     section name from .debug_* to .zdebug_*.  */
   7238 		  char *new_name = bfd_debug_name_to_zdebug (abfd, name);
   7239 		  if (new_name == NULL)
   7240 		    return false;
   7241 		  name = new_name;
   7242 		}
   7243 	      /* Add section name to section name section.  */
   7244 	      shdrp->sh_name
   7245 		= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   7246 						      name, false);
   7247 	      d = elf_section_data (sec);
   7248 
   7249 	      /* Add reloc section name to section name section.  */
   7250 	      if (d->rel.hdr
   7251 		  && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
   7252 						  name, false))
   7253 		return false;
   7254 	      if (d->rela.hdr
   7255 		  && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
   7256 						  name, true))
   7257 		return false;
   7258 
   7259 	      /* Update section size and contents.  */
   7260 	      shdrp->sh_size = sec->size;
   7261 	      shdrp->contents = sec->contents;
   7262 	      sec->contents = NULL;
   7263 	    }
   7264 
   7265 	  off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
   7266 	}
   7267     }
   7268 
   7269   /* Place section name section after DWARF debug sections have been
   7270      compressed.  */
   7271   _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
   7272   shdrp = &elf_tdata (abfd)->shstrtab_hdr;
   7273   shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
   7274   off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
   7275 
   7276   /* Place the section headers.  */
   7277   i_ehdrp = elf_elfheader (abfd);
   7278   bed = get_elf_backend_data (abfd);
   7279   off = align_file_position (off, 1 << bed->s->log_file_align);
   7280   i_ehdrp->e_shoff = off;
   7281   off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
   7282   elf_next_file_pos (abfd) = off;
   7283 
   7284   return true;
   7285 }
   7286 
   7287 bool
   7288 _bfd_elf_write_object_contents (bfd *abfd)
   7289 {
   7290   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   7291   Elf_Internal_Shdr **i_shdrp;
   7292   bool failed;
   7293   unsigned int count, num_sec;
   7294   struct elf_obj_tdata *t;
   7295 
   7296   if (! abfd->output_has_begun
   7297       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   7298     return false;
   7299   /* Do not rewrite ELF data when the BFD has been opened for update.
   7300      abfd->output_has_begun was set to TRUE on opening, so creation of
   7301      new sections, and modification of existing section sizes was
   7302      restricted.  This means the ELF header, program headers and
   7303      section headers can't have changed.  If the contents of any
   7304      sections has been modified, then those changes have already been
   7305      written to the BFD.  */
   7306   else if (abfd->direction == both_direction)
   7307     {
   7308       BFD_ASSERT (abfd->output_has_begun);
   7309       return true;
   7310     }
   7311 
   7312   i_shdrp = elf_elfsections (abfd);
   7313 
   7314   failed = false;
   7315   bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
   7316   if (failed)
   7317     return false;
   7318 
   7319   if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
   7320     return false;
   7321 
   7322   /* After writing the headers, we need to write the sections too...  */
   7323   num_sec = elf_numsections (abfd);
   7324   for (count = 1; count < num_sec; count++)
   7325     {
   7326       /* Don't set the sh_name field without section header.  */
   7327       if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
   7328 	i_shdrp[count]->sh_name
   7329 	  = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
   7330 				    i_shdrp[count]->sh_name);
   7331       if (bed->elf_backend_section_processing)
   7332 	if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
   7333 	  return false;
   7334       if (i_shdrp[count]->contents)
   7335 	{
   7336 	  bfd_size_type amt = i_shdrp[count]->sh_size;
   7337 
   7338 	  if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
   7339 	      || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
   7340 	    return false;
   7341 	}
   7342     }
   7343 
   7344   /* Write out the section header names.  */
   7345   t = elf_tdata (abfd);
   7346   if (elf_shstrtab (abfd) != NULL
   7347       && t->shstrtab_hdr.sh_offset != -1
   7348       && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
   7349 	  || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
   7350     return false;
   7351 
   7352   if (!(*bed->elf_backend_final_write_processing) (abfd))
   7353     return false;
   7354 
   7355   if (!bed->s->write_shdrs_and_ehdr (abfd))
   7356     return false;
   7357 
   7358   /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
   7359   if (t->o->build_id.after_write_object_contents != NULL
   7360       && !(*t->o->build_id.after_write_object_contents) (abfd))
   7361     return false;
   7362   if (t->o->package_metadata.after_write_object_contents != NULL
   7363       && !(*t->o->package_metadata.after_write_object_contents) (abfd))
   7364     return false;
   7365 
   7366   return true;
   7367 }
   7368 
   7369 bool
   7370 _bfd_elf_write_corefile_contents (bfd *abfd)
   7371 {
   7372   /* Hopefully this can be done just like an object file.  */
   7373   return _bfd_elf_write_object_contents (abfd);
   7374 }
   7375 
   7376 /* Given a section, search the header to find them.  */
   7377 
   7378 unsigned int
   7379 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
   7380 {
   7381   const struct elf_backend_data *bed;
   7382   unsigned int sec_index;
   7383 
   7384   if (elf_section_data (asect) != NULL
   7385       && elf_section_data (asect)->this_idx != 0)
   7386     return elf_section_data (asect)->this_idx;
   7387 
   7388   if (bfd_is_abs_section (asect))
   7389     sec_index = SHN_ABS;
   7390   else if (bfd_is_com_section (asect))
   7391     sec_index = SHN_COMMON;
   7392   else if (bfd_is_und_section (asect))
   7393     sec_index = SHN_UNDEF;
   7394   else
   7395     sec_index = SHN_BAD;
   7396 
   7397   bed = get_elf_backend_data (abfd);
   7398   if (bed->elf_backend_section_from_bfd_section)
   7399     {
   7400       int retval = sec_index;
   7401 
   7402       if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
   7403 	return retval;
   7404     }
   7405 
   7406   if (sec_index == SHN_BAD)
   7407     bfd_set_error (bfd_error_nonrepresentable_section);
   7408 
   7409   return sec_index;
   7410 }
   7411 
   7412 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
   7413    on error.  */
   7414 
   7415 int
   7416 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
   7417 {
   7418   asymbol *asym_ptr = *asym_ptr_ptr;
   7419   int idx;
   7420   flagword flags = asym_ptr->flags;
   7421 
   7422   /* When gas creates relocations against local labels, it creates its
   7423      own symbol for the section, but does put the symbol into the
   7424      symbol chain, so udata is 0.  When the linker is generating
   7425      relocatable output, this section symbol may be for one of the
   7426      input sections rather than the output section.  */
   7427   if (asym_ptr->udata.i == 0
   7428       && (flags & BSF_SECTION_SYM)
   7429       && asym_ptr->section)
   7430     {
   7431       asection *sec;
   7432 
   7433       sec = asym_ptr->section;
   7434       if (sec->owner != abfd && sec->output_section != NULL)
   7435 	sec = sec->output_section;
   7436       if (sec->owner == abfd
   7437 	  && sec->index < elf_num_section_syms (abfd)
   7438 	  && elf_section_syms (abfd)[sec->index] != NULL)
   7439 	asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
   7440     }
   7441 
   7442   idx = asym_ptr->udata.i;
   7443 
   7444   if (idx == 0)
   7445     {
   7446       /* This case can occur when using --strip-symbol on a symbol
   7447 	 which is used in a relocation entry.  */
   7448       _bfd_error_handler
   7449 	/* xgettext:c-format */
   7450 	(_("%pB: symbol `%s' required but not present"),
   7451 	 abfd, bfd_asymbol_name (asym_ptr));
   7452       bfd_set_error (bfd_error_no_symbols);
   7453       return -1;
   7454     }
   7455 
   7456 #if DEBUG & 4
   7457   {
   7458     fprintf (stderr,
   7459 	     "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
   7460 	     " flags = 0x%.8x\n",
   7461 	     (long) asym_ptr, asym_ptr->name, idx, flags);
   7462     fflush (stderr);
   7463   }
   7464 #endif
   7465 
   7466   return idx;
   7467 }
   7468 
   7469 static inline bfd_vma
   7470 segment_size (Elf_Internal_Phdr *segment)
   7471 {
   7472   return (segment->p_memsz > segment->p_filesz
   7473 	  ? segment->p_memsz : segment->p_filesz);
   7474 }
   7475 
   7476 
   7477 /* Returns the end address of the segment + 1.  */
   7478 static inline bfd_vma
   7479 segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
   7480 {
   7481   return start + segment_size (segment);
   7482 }
   7483 
   7484 static inline bfd_size_type
   7485 section_size (asection *section, Elf_Internal_Phdr *segment)
   7486 {
   7487   if ((section->flags & SEC_HAS_CONTENTS) != 0
   7488       || (section->flags & SEC_THREAD_LOCAL) == 0
   7489       || segment->p_type == PT_TLS)
   7490     return section->size;
   7491   return 0;
   7492 }
   7493 
   7494 /* Returns TRUE if the given section is contained within the given
   7495    segment.  LMA addresses are compared against PADDR when
   7496    USE_VADDR is false, VMA against VADDR when true.  */
   7497 static bool
   7498 is_contained_by (asection *section, Elf_Internal_Phdr *segment,
   7499 		 bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
   7500 		 bool use_vaddr)
   7501 {
   7502   bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
   7503   bfd_vma addr = !use_vaddr ? section->lma : section->vma;
   7504   bfd_vma octet;
   7505   if (_bfd_mul_overflow (addr, opb, &octet))
   7506     return false;
   7507   /* The third and fourth lines below are testing that the section end
   7508      address is within the segment.  It's written this way to avoid
   7509      overflow.  Add seg_addr + section_size to both sides of the
   7510      inequality to make it obvious.  */
   7511   return (octet >= seg_addr
   7512 	  && segment_size (segment) >= section_size (section, segment)
   7513 	  && (octet - seg_addr
   7514 	      <= segment_size (segment) - section_size (section, segment)));
   7515 }
   7516 
   7517 /* Handle PT_NOTE segment.  */
   7518 static bool
   7519 is_note (asection *s, Elf_Internal_Phdr *p)
   7520 {
   7521   return (p->p_type == PT_NOTE
   7522 	  && elf_section_type (s) == SHT_NOTE
   7523 	  && (ufile_ptr) s->filepos >= p->p_offset
   7524 	  && p->p_filesz >= s->size
   7525 	  && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
   7526 }
   7527 
   7528 /* Rewrite program header information.  */
   7529 
   7530 static bool
   7531 rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
   7532 {
   7533   Elf_Internal_Ehdr *iehdr;
   7534   struct elf_segment_map *map;
   7535   struct elf_segment_map *map_first;
   7536   struct elf_segment_map **pointer_to_map;
   7537   Elf_Internal_Phdr *segment;
   7538   asection *section;
   7539   unsigned int i;
   7540   unsigned int num_segments;
   7541   bool phdr_included = false;
   7542   bool p_paddr_valid;
   7543   struct elf_segment_map *phdr_adjust_seg = NULL;
   7544   unsigned int phdr_adjust_num = 0;
   7545   const struct elf_backend_data *bed;
   7546   unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
   7547 
   7548   bed = get_elf_backend_data (ibfd);
   7549   iehdr = elf_elfheader (ibfd);
   7550 
   7551   map_first = NULL;
   7552   pointer_to_map = &map_first;
   7553 
   7554   num_segments = elf_elfheader (ibfd)->e_phnum;
   7555 
   7556   /* The complicated case when p_vaddr is 0 is to handle the Solaris
   7557      linker, which generates a PT_INTERP section with p_vaddr and
   7558      p_memsz set to 0.  */
   7559 #define IS_SOLARIS_PT_INTERP(p, s)					\
   7560   (p->p_vaddr == 0							\
   7561    && p->p_paddr == 0							\
   7562    && p->p_memsz == 0							\
   7563    && p->p_filesz > 0							\
   7564    && (s->flags & SEC_HAS_CONTENTS) != 0				\
   7565    && s->size > 0							\
   7566    && (bfd_vma) s->filepos >= p->p_offset				\
   7567    && ((bfd_vma) s->filepos + s->size					\
   7568        <= p->p_offset + p->p_filesz))
   7569 
   7570   /* Decide if the given section should be included in the given segment.
   7571      A section will be included if:
   7572        1. It is within the address space of the segment -- we use the LMA
   7573 	  if that is set for the segment and the VMA otherwise,
   7574        2. It is an allocated section or a NOTE section in a PT_NOTE
   7575 	  segment.
   7576        3. There is an output section associated with it,
   7577        4. The section has not already been allocated to a previous segment.
   7578        5. PT_GNU_STACK segments do not include any sections.
   7579        6. PT_TLS segment includes only SHF_TLS sections.
   7580        7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
   7581        8. PT_DYNAMIC should not contain empty sections at the beginning
   7582 	  (with the possible exception of .dynamic).  */
   7583 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid)	\
   7584   (((is_contained_by (section, segment, segment->p_paddr,		\
   7585 		      segment->p_vaddr, opb, !paddr_valid)		\
   7586      && (section->flags & SEC_ALLOC) != 0)				\
   7587     || is_note (section, segment))					\
   7588    && segment->p_type != PT_GNU_STACK					\
   7589    && (segment->p_type != PT_TLS					\
   7590        || (section->flags & SEC_THREAD_LOCAL))				\
   7591    && (segment->p_type == PT_LOAD					\
   7592        || segment->p_type == PT_TLS					\
   7593        || (section->flags & SEC_THREAD_LOCAL) == 0)			\
   7594    && (segment->p_type != PT_DYNAMIC					\
   7595        || section_size (section, segment) > 0				\
   7596        || (segment->p_paddr						\
   7597 	   ? segment->p_paddr != section->lma * (opb)			\
   7598 	   : segment->p_vaddr != section->vma * (opb))			\
   7599        || (strcmp (bfd_section_name (section), ".dynamic") == 0))	\
   7600    && (segment->p_type != PT_LOAD || !section->segment_mark))
   7601 
   7602 /* If the output section of a section in the input segment is NULL,
   7603    it is removed from the corresponding output segment.   */
   7604 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid)	\
   7605   (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid)	\
   7606    && section->output_section != NULL)
   7607 
   7608   /* Returns TRUE iff seg1 starts after the end of seg2.  */
   7609 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)			\
   7610   (seg1->field >= segment_end (seg2, seg2->field))
   7611 
   7612   /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
   7613      their VMA address ranges and their LMA address ranges overlap.
   7614      It is possible to have overlapping VMA ranges without overlapping LMA
   7615      ranges.  RedBoot images for example can have both .data and .bss mapped
   7616      to the same VMA range, but with the .data section mapped to a different
   7617      LMA.  */
   7618 #define SEGMENT_OVERLAPS(seg1, seg2)					\
   7619   (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)			\
   7620 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))			\
   7621    && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)			\
   7622 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
   7623 
   7624   /* Initialise the segment mark field, and discard stupid alignment.  */
   7625   for (section = ibfd->sections; section != NULL; section = section->next)
   7626     {
   7627       asection *o = section->output_section;
   7628       if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
   7629 	o->alignment_power = 0;
   7630       section->segment_mark = false;
   7631     }
   7632 
   7633   /* The Solaris linker creates program headers in which all the
   7634      p_paddr fields are zero.  When we try to objcopy or strip such a
   7635      file, we get confused.  Check for this case, and if we find it
   7636      don't set the p_paddr_valid fields.  */
   7637   p_paddr_valid = false;
   7638   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7639        i < num_segments;
   7640        i++, segment++)
   7641     if (segment->p_paddr != 0)
   7642       {
   7643 	p_paddr_valid = true;
   7644 	break;
   7645       }
   7646 
   7647   /* Scan through the segments specified in the program header
   7648      of the input BFD.  For this first scan we look for overlaps
   7649      in the loadable segments.  These can be created by weird
   7650      parameters to objcopy.  Also, fix some solaris weirdness.  */
   7651   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7652        i < num_segments;
   7653        i++, segment++)
   7654     {
   7655       unsigned int j;
   7656       Elf_Internal_Phdr *segment2;
   7657 
   7658       if (segment->p_type == PT_INTERP)
   7659 	for (section = ibfd->sections; section; section = section->next)
   7660 	  if (IS_SOLARIS_PT_INTERP (segment, section))
   7661 	    {
   7662 	      /* Mininal change so that the normal section to segment
   7663 		 assignment code will work.  */
   7664 	      segment->p_vaddr = section->vma * opb;
   7665 	      break;
   7666 	    }
   7667 
   7668       if (segment->p_type != PT_LOAD)
   7669 	{
   7670 	  /* Remove PT_GNU_RELRO segment.  */
   7671 	  if (segment->p_type == PT_GNU_RELRO)
   7672 	    segment->p_type = PT_NULL;
   7673 	  continue;
   7674 	}
   7675 
   7676       /* Determine if this segment overlaps any previous segments.  */
   7677       for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
   7678 	{
   7679 	  bfd_signed_vma extra_length;
   7680 
   7681 	  if (segment2->p_type != PT_LOAD
   7682 	      || !SEGMENT_OVERLAPS (segment, segment2))
   7683 	    continue;
   7684 
   7685 	  /* Merge the two segments together.  */
   7686 	  if (segment2->p_vaddr < segment->p_vaddr)
   7687 	    {
   7688 	      /* Extend SEGMENT2 to include SEGMENT and then delete
   7689 		 SEGMENT.  */
   7690 	      extra_length = (segment_end (segment, segment->p_vaddr)
   7691 			      - segment_end (segment2, segment2->p_vaddr));
   7692 
   7693 	      if (extra_length > 0)
   7694 		{
   7695 		  segment2->p_memsz += extra_length;
   7696 		  segment2->p_filesz += extra_length;
   7697 		}
   7698 
   7699 	      segment->p_type = PT_NULL;
   7700 
   7701 	      /* Since we have deleted P we must restart the outer loop.  */
   7702 	      i = 0;
   7703 	      segment = elf_tdata (ibfd)->phdr;
   7704 	      break;
   7705 	    }
   7706 	  else
   7707 	    {
   7708 	      /* Extend SEGMENT to include SEGMENT2 and then delete
   7709 		 SEGMENT2.  */
   7710 	      extra_length = (segment_end (segment2, segment2->p_vaddr)
   7711 			      - segment_end (segment, segment->p_vaddr));
   7712 
   7713 	      if (extra_length > 0)
   7714 		{
   7715 		  segment->p_memsz += extra_length;
   7716 		  segment->p_filesz += extra_length;
   7717 		}
   7718 
   7719 	      segment2->p_type = PT_NULL;
   7720 	    }
   7721 	}
   7722     }
   7723 
   7724   /* The second scan attempts to assign sections to segments.  */
   7725   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7726        i < num_segments;
   7727        i++, segment++)
   7728     {
   7729       unsigned int section_count;
   7730       asection **sections;
   7731       asection *output_section;
   7732       unsigned int isec;
   7733       asection *matching_lma;
   7734       asection *suggested_lma;
   7735       unsigned int j;
   7736       size_t amt;
   7737       asection *first_section;
   7738 
   7739       if (segment->p_type == PT_NULL)
   7740 	continue;
   7741 
   7742       first_section = NULL;
   7743       /* Compute how many sections might be placed into this segment.  */
   7744       for (section = ibfd->sections, section_count = 0;
   7745 	   section != NULL;
   7746 	   section = section->next)
   7747 	{
   7748 	  /* Find the first section in the input segment, which may be
   7749 	     removed from the corresponding output segment.   */
   7750 	  if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, p_paddr_valid))
   7751 	    {
   7752 	      if (first_section == NULL)
   7753 		first_section = section;
   7754 	      if (section->output_section != NULL)
   7755 		++section_count;
   7756 	    }
   7757 	}
   7758 
   7759       /* Allocate a segment map big enough to contain
   7760 	 all of the sections we have selected.  */
   7761       amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   7762       amt += section_count * sizeof (asection *);
   7763       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7764       if (map == NULL)
   7765 	return false;
   7766 
   7767       /* Initialise the fields of the segment map.  Default to
   7768 	 using the physical address of the segment in the input BFD.  */
   7769       map->next = NULL;
   7770       map->p_type = segment->p_type;
   7771       map->p_flags = segment->p_flags;
   7772       map->p_flags_valid = 1;
   7773 
   7774       if (map->p_type == PT_LOAD
   7775 	  && (ibfd->flags & D_PAGED) != 0
   7776 	  && maxpagesize > 1
   7777 	  && segment->p_align > 1)
   7778 	{
   7779 	  map->p_align = segment->p_align;
   7780 	  if (segment->p_align > maxpagesize)
   7781 	    map->p_align = maxpagesize;
   7782 	  map->p_align_valid = 1;
   7783 	}
   7784 
   7785       /* If the first section in the input segment is removed, there is
   7786 	 no need to preserve segment physical address in the corresponding
   7787 	 output segment.  */
   7788       if (!first_section || first_section->output_section != NULL)
   7789 	{
   7790 	  map->p_paddr = segment->p_paddr;
   7791 	  map->p_paddr_valid = p_paddr_valid;
   7792 	}
   7793 
   7794       /* Determine if this segment contains the ELF file header
   7795 	 and if it contains the program headers themselves.  */
   7796       map->includes_filehdr = (segment->p_offset == 0
   7797 			       && segment->p_filesz >= iehdr->e_ehsize);
   7798       map->includes_phdrs = 0;
   7799 
   7800       if (!phdr_included || segment->p_type != PT_LOAD)
   7801 	{
   7802 	  map->includes_phdrs =
   7803 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   7804 	     && (segment->p_offset + segment->p_filesz
   7805 		 >= ((bfd_vma) iehdr->e_phoff
   7806 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   7807 
   7808 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   7809 	    phdr_included = true;
   7810 	}
   7811 
   7812       if (section_count == 0)
   7813 	{
   7814 	  /* Special segments, such as the PT_PHDR segment, may contain
   7815 	     no sections, but ordinary, loadable segments should contain
   7816 	     something.  They are allowed by the ELF spec however, so only
   7817 	     a warning is produced.
   7818 	     Don't warn if an empty PT_LOAD contains the program headers.
   7819 	     There is however the valid use case of embedded systems which
   7820 	     have segments with p_filesz of 0 and a p_memsz > 0 to initialize
   7821 	     flash memory with zeros.  No warning is shown for that case.  */
   7822 	  if (segment->p_type == PT_LOAD
   7823 	      && !map->includes_phdrs
   7824 	      && (segment->p_filesz > 0 || segment->p_memsz == 0))
   7825 	    /* xgettext:c-format */
   7826 	    _bfd_error_handler
   7827 	      (_("%pB: warning: empty loadable segment detected"
   7828 		 " at vaddr=%#" PRIx64 ", is this intentional?"),
   7829 	       ibfd, (uint64_t) segment->p_vaddr);
   7830 
   7831 	  map->p_vaddr_offset = segment->p_vaddr / opb;
   7832 	  map->count = 0;
   7833 	  *pointer_to_map = map;
   7834 	  pointer_to_map = &map->next;
   7835 
   7836 	  continue;
   7837 	}
   7838 
   7839       /* Now scan the sections in the input BFD again and attempt
   7840 	 to add their corresponding output sections to the segment map.
   7841 	 The problem here is how to handle an output section which has
   7842 	 been moved (ie had its LMA changed).  There are four possibilities:
   7843 
   7844 	 1. None of the sections have been moved.
   7845 	    In this case we can continue to use the segment LMA from the
   7846 	    input BFD.
   7847 
   7848 	 2. All of the sections have been moved by the same amount.
   7849 	    In this case we can change the segment's LMA to match the LMA
   7850 	    of the first section.
   7851 
   7852 	 3. Some of the sections have been moved, others have not.
   7853 	    In this case those sections which have not been moved can be
   7854 	    placed in the current segment which will have to have its size,
   7855 	    and possibly its LMA changed, and a new segment or segments will
   7856 	    have to be created to contain the other sections.
   7857 
   7858 	 4. The sections have been moved, but not by the same amount.
   7859 	    In this case we can change the segment's LMA to match the LMA
   7860 	    of the first section and we will have to create a new segment
   7861 	    or segments to contain the other sections.
   7862 
   7863 	 In order to save time, we allocate an array to hold the section
   7864 	 pointers that we are interested in.  As these sections get assigned
   7865 	 to a segment, they are removed from this array.  */
   7866 
   7867       amt = section_count * sizeof (asection *);
   7868       sections = (asection **) bfd_malloc (amt);
   7869       if (sections == NULL)
   7870 	return false;
   7871 
   7872       /* Step One: Scan for segment vs section LMA conflicts.
   7873 	 Also add the sections to the section array allocated above.
   7874 	 Also add the sections to the current segment.  In the common
   7875 	 case, where the sections have not been moved, this means that
   7876 	 we have completely filled the segment, and there is nothing
   7877 	 more to do.  */
   7878       isec = 0;
   7879       matching_lma = NULL;
   7880       suggested_lma = NULL;
   7881 
   7882       for (section = first_section, j = 0;
   7883 	   section != NULL;
   7884 	   section = section->next)
   7885 	{
   7886 	  if (INCLUDE_SECTION_IN_SEGMENT (section, segment, opb, p_paddr_valid))
   7887 	    {
   7888 	      output_section = section->output_section;
   7889 
   7890 	      sections[j++] = section;
   7891 
   7892 	      /* The Solaris native linker always sets p_paddr to 0.
   7893 		 We try to catch that case here, and set it to the
   7894 		 correct value.  Note - some backends require that
   7895 		 p_paddr be left as zero.  */
   7896 	      if (!p_paddr_valid
   7897 		  && segment->p_vaddr != 0
   7898 		  && !bed->want_p_paddr_set_to_zero
   7899 		  && isec == 0
   7900 		  && output_section->lma != 0
   7901 		  && (align_power (segment->p_vaddr
   7902 				   + (map->includes_filehdr
   7903 				      ? iehdr->e_ehsize : 0)
   7904 				   + (map->includes_phdrs
   7905 				      ? iehdr->e_phnum * iehdr->e_phentsize
   7906 				      : 0),
   7907 				   output_section->alignment_power * opb)
   7908 		      == (output_section->vma * opb)))
   7909 		map->p_paddr = segment->p_vaddr;
   7910 
   7911 	      /* Match up the physical address of the segment with the
   7912 		 LMA address of the output section.  */
   7913 	      if (is_contained_by (output_section, segment, map->p_paddr,
   7914 				   0, opb, false)
   7915 		  || is_note (section, segment))
   7916 		{
   7917 		  if (matching_lma == NULL
   7918 		      || output_section->lma < matching_lma->lma)
   7919 		    matching_lma = output_section;
   7920 
   7921 		  /* We assume that if the section fits within the segment
   7922 		     then it does not overlap any other section within that
   7923 		     segment.  */
   7924 		  map->sections[isec++] = output_section;
   7925 		}
   7926 	      else if (suggested_lma == NULL)
   7927 		suggested_lma = output_section;
   7928 
   7929 	      if (j == section_count)
   7930 		break;
   7931 	    }
   7932 	}
   7933 
   7934       BFD_ASSERT (j == section_count);
   7935 
   7936       /* Step Two: Adjust the physical address of the current segment,
   7937 	 if necessary.  */
   7938       if (isec == section_count)
   7939 	{
   7940 	  /* All of the sections fitted within the segment as currently
   7941 	     specified.  This is the default case.  Add the segment to
   7942 	     the list of built segments and carry on to process the next
   7943 	     program header in the input BFD.  */
   7944 	  map->count = section_count;
   7945 	  *pointer_to_map = map;
   7946 	  pointer_to_map = &map->next;
   7947 
   7948 	  if (p_paddr_valid
   7949 	      && !bed->want_p_paddr_set_to_zero)
   7950 	    {
   7951 	      bfd_vma hdr_size = 0;
   7952 	      if (map->includes_filehdr)
   7953 		hdr_size = iehdr->e_ehsize;
   7954 	      if (map->includes_phdrs)
   7955 		hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
   7956 
   7957 	      /* Account for padding before the first section in the
   7958 		 segment.  */
   7959 	      map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
   7960 				     - matching_lma->lma);
   7961 	    }
   7962 
   7963 	  free (sections);
   7964 	  continue;
   7965 	}
   7966       else
   7967 	{
   7968 	  /* Change the current segment's physical address to match
   7969 	     the LMA of the first section that fitted, or if no
   7970 	     section fitted, the first section.  */
   7971 	  if (matching_lma == NULL)
   7972 	    matching_lma = suggested_lma;
   7973 
   7974 	  map->p_paddr = matching_lma->lma * opb;
   7975 
   7976 	  /* Offset the segment physical address from the lma
   7977 	     to allow for space taken up by elf headers.  */
   7978 	  if (map->includes_phdrs)
   7979 	    {
   7980 	      map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
   7981 
   7982 	      /* iehdr->e_phnum is just an estimate of the number
   7983 		 of program headers that we will need.  Make a note
   7984 		 here of the number we used and the segment we chose
   7985 		 to hold these headers, so that we can adjust the
   7986 		 offset when we know the correct value.  */
   7987 	      phdr_adjust_num = iehdr->e_phnum;
   7988 	      phdr_adjust_seg = map;
   7989 	    }
   7990 
   7991 	  if (map->includes_filehdr)
   7992 	    {
   7993 	      bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
   7994 	      map->p_paddr -= iehdr->e_ehsize;
   7995 	      /* We've subtracted off the size of headers from the
   7996 		 first section lma, but there may have been some
   7997 		 alignment padding before that section too.  Try to
   7998 		 account for that by adjusting the segment lma down to
   7999 		 the same alignment.  */
   8000 	      if (segment->p_align != 0 && segment->p_align < align)
   8001 		align = segment->p_align;
   8002 	      map->p_paddr &= -(align * opb);
   8003 	    }
   8004 	}
   8005 
   8006       /* Step Three: Loop over the sections again, this time assigning
   8007 	 those that fit to the current segment and removing them from the
   8008 	 sections array; but making sure not to leave large gaps.  Once all
   8009 	 possible sections have been assigned to the current segment it is
   8010 	 added to the list of built segments and if sections still remain
   8011 	 to be assigned, a new segment is constructed before repeating
   8012 	 the loop.  */
   8013       isec = 0;
   8014       do
   8015 	{
   8016 	  map->count = 0;
   8017 	  suggested_lma = NULL;
   8018 
   8019 	  /* Fill the current segment with sections that fit.  */
   8020 	  for (j = 0; j < section_count; j++)
   8021 	    {
   8022 	      section = sections[j];
   8023 
   8024 	      if (section == NULL)
   8025 		continue;
   8026 
   8027 	      output_section = section->output_section;
   8028 
   8029 	      BFD_ASSERT (output_section != NULL);
   8030 
   8031 	      if (is_contained_by (output_section, segment, map->p_paddr,
   8032 				   0, opb, false)
   8033 		  || is_note (section, segment))
   8034 		{
   8035 		  if (map->count == 0)
   8036 		    {
   8037 		      /* If the first section in a segment does not start at
   8038 			 the beginning of the segment, then something is
   8039 			 wrong.  */
   8040 		      if (align_power (map->p_paddr
   8041 				       + (map->includes_filehdr
   8042 					  ? iehdr->e_ehsize : 0)
   8043 				       + (map->includes_phdrs
   8044 					  ? iehdr->e_phnum * iehdr->e_phentsize
   8045 					  : 0),
   8046 				       output_section->alignment_power * opb)
   8047 			  != output_section->lma * opb)
   8048 			goto sorry;
   8049 		    }
   8050 		  else
   8051 		    {
   8052 		      asection *prev_sec;
   8053 
   8054 		      prev_sec = map->sections[map->count - 1];
   8055 
   8056 		      /* If the gap between the end of the previous section
   8057 			 and the start of this section is more than
   8058 			 maxpagesize then we need to start a new segment.  */
   8059 		      if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
   8060 				      maxpagesize)
   8061 			   < BFD_ALIGN (output_section->lma, maxpagesize))
   8062 			  || (prev_sec->lma + prev_sec->size
   8063 			      > output_section->lma))
   8064 			{
   8065 			  if (suggested_lma == NULL)
   8066 			    suggested_lma = output_section;
   8067 
   8068 			  continue;
   8069 			}
   8070 		    }
   8071 
   8072 		  map->sections[map->count++] = output_section;
   8073 		  ++isec;
   8074 		  sections[j] = NULL;
   8075 		  if (segment->p_type == PT_LOAD)
   8076 		    section->segment_mark = true;
   8077 		}
   8078 	      else if (suggested_lma == NULL)
   8079 		suggested_lma = output_section;
   8080 	    }
   8081 
   8082 	  /* PR 23932.  A corrupt input file may contain sections that cannot
   8083 	     be assigned to any segment - because for example they have a
   8084 	     negative size - or segments that do not contain any sections.
   8085 	     But there are also valid reasons why a segment can be empty.
   8086 	     So allow a count of zero.  */
   8087 
   8088 	  /* Add the current segment to the list of built segments.  */
   8089 	  *pointer_to_map = map;
   8090 	  pointer_to_map = &map->next;
   8091 
   8092 	  if (isec < section_count)
   8093 	    {
   8094 	      /* We still have not allocated all of the sections to
   8095 		 segments.  Create a new segment here, initialise it
   8096 		 and carry on looping.  */
   8097 	      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   8098 	      amt += section_count * sizeof (asection *);
   8099 	      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   8100 	      if (map == NULL)
   8101 		{
   8102 		  free (sections);
   8103 		  return false;
   8104 		}
   8105 
   8106 	      /* Initialise the fields of the segment map.  Set the physical
   8107 		 physical address to the LMA of the first section that has
   8108 		 not yet been assigned.  */
   8109 	      map->next = NULL;
   8110 	      map->p_type = segment->p_type;
   8111 	      map->p_flags = segment->p_flags;
   8112 	      map->p_flags_valid = 1;
   8113 	      map->p_paddr = suggested_lma->lma * opb;
   8114 	      map->p_paddr_valid = p_paddr_valid;
   8115 	      map->includes_filehdr = 0;
   8116 	      map->includes_phdrs = 0;
   8117 	    }
   8118 
   8119 	  continue;
   8120 	sorry:
   8121 	  bfd_set_error (bfd_error_sorry);
   8122 	  free (sections);
   8123 	  return false;
   8124 	}
   8125       while (isec < section_count);
   8126 
   8127       free (sections);
   8128     }
   8129 
   8130   elf_seg_map (obfd) = map_first;
   8131 
   8132   /* If we had to estimate the number of program headers that were
   8133      going to be needed, then check our estimate now and adjust
   8134      the offset if necessary.  */
   8135   if (phdr_adjust_seg != NULL)
   8136     {
   8137       unsigned int count;
   8138 
   8139       for (count = 0, map = map_first; map != NULL; map = map->next)
   8140 	count++;
   8141 
   8142       if (count > phdr_adjust_num)
   8143 	phdr_adjust_seg->p_paddr
   8144 	  -= (count - phdr_adjust_num) * iehdr->e_phentsize;
   8145 
   8146       for (map = map_first; map != NULL; map = map->next)
   8147 	if (map->p_type == PT_PHDR)
   8148 	  {
   8149 	    bfd_vma adjust
   8150 	      = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
   8151 	    map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
   8152 	    break;
   8153 	  }
   8154     }
   8155 
   8156 #undef IS_SOLARIS_PT_INTERP
   8157 #undef IS_SECTION_IN_INPUT_SEGMENT
   8158 #undef INCLUDE_SECTION_IN_SEGMENT
   8159 #undef SEGMENT_AFTER_SEGMENT
   8160 #undef SEGMENT_OVERLAPS
   8161   return true;
   8162 }
   8163 
   8164 /* Return true if p_align in the ELF program header in ABFD is valid.  */
   8165 
   8166 static bool
   8167 elf_is_p_align_valid (bfd *abfd)
   8168 {
   8169   unsigned int i;
   8170   Elf_Internal_Phdr *segment;
   8171   unsigned int num_segments;
   8172   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8173   bfd_size_type maxpagesize = bed->maxpagesize;
   8174   bfd_size_type p_align = bed->p_align;
   8175 
   8176   /* Return true if the default p_align value isn't set or the maximum
   8177      page size is the same as the minimum page size.  */
   8178   if (p_align == 0 || maxpagesize == bed->minpagesize)
   8179     return true;
   8180 
   8181   /* When the default p_align value is set, p_align may be set to the
   8182      default p_align value while segments are aligned to the maximum
   8183      page size.  In this case, the input p_align will be ignored and
   8184      the maximum page size will be used to align the output segments.  */
   8185   segment = elf_tdata (abfd)->phdr;
   8186   num_segments = elf_elfheader (abfd)->e_phnum;
   8187   for (i = 0; i < num_segments; i++, segment++)
   8188     if (segment->p_type == PT_LOAD
   8189 	&& (segment->p_align != p_align
   8190 	    || vma_page_aligned_bias (segment->p_vaddr,
   8191 				      segment->p_offset,
   8192 				      maxpagesize) != 0))
   8193       return true;
   8194 
   8195   return false;
   8196 }
   8197 
   8198 /* Copy ELF program header information.  */
   8199 
   8200 static bool
   8201 copy_elf_program_header (bfd *ibfd, bfd *obfd)
   8202 {
   8203   Elf_Internal_Ehdr *iehdr;
   8204   struct elf_segment_map *map;
   8205   struct elf_segment_map *map_first;
   8206   struct elf_segment_map **pointer_to_map;
   8207   Elf_Internal_Phdr *segment;
   8208   unsigned int i;
   8209   unsigned int num_segments;
   8210   bool phdr_included = false;
   8211   bool p_paddr_valid;
   8212   bool p_palign_valid;
   8213   unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
   8214 
   8215   iehdr = elf_elfheader (ibfd);
   8216 
   8217   map_first = NULL;
   8218   pointer_to_map = &map_first;
   8219 
   8220   /* If all the segment p_paddr fields are zero, don't set
   8221      map->p_paddr_valid.  */
   8222   p_paddr_valid = false;
   8223   num_segments = elf_elfheader (ibfd)->e_phnum;
   8224   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   8225        i < num_segments;
   8226        i++, segment++)
   8227     if (segment->p_paddr != 0)
   8228       {
   8229 	p_paddr_valid = true;
   8230 	break;
   8231       }
   8232 
   8233   p_palign_valid = elf_is_p_align_valid (ibfd);
   8234 
   8235   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   8236        i < num_segments;
   8237        i++, segment++)
   8238     {
   8239       asection *section;
   8240       unsigned int section_count;
   8241       size_t amt;
   8242       Elf_Internal_Shdr *this_hdr;
   8243       asection *first_section = NULL;
   8244       asection *lowest_section;
   8245 
   8246       /* Compute how many sections are in this segment.  */
   8247       for (section = ibfd->sections, section_count = 0;
   8248 	   section != NULL;
   8249 	   section = section->next)
   8250 	{
   8251 	  this_hdr = &(elf_section_data(section)->this_hdr);
   8252 	  if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   8253 	    {
   8254 	      if (first_section == NULL)
   8255 		first_section = section;
   8256 	      section_count++;
   8257 	    }
   8258 	}
   8259 
   8260       /* Allocate a segment map big enough to contain
   8261 	 all of the sections we have selected.  */
   8262       amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   8263       amt += section_count * sizeof (asection *);
   8264       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   8265       if (map == NULL)
   8266 	return false;
   8267 
   8268       /* Initialize the fields of the output segment map with the
   8269 	 input segment.  */
   8270       map->next = NULL;
   8271       map->p_type = segment->p_type;
   8272       map->p_flags = segment->p_flags;
   8273       map->p_flags_valid = 1;
   8274       map->p_paddr = segment->p_paddr;
   8275       map->p_paddr_valid = p_paddr_valid;
   8276       map->p_align = segment->p_align;
   8277       /* Keep p_align of PT_GNU_STACK for stack alignment.  */
   8278       map->p_align_valid = (map->p_type == PT_GNU_STACK
   8279 			    || p_palign_valid);
   8280       map->p_vaddr_offset = 0;
   8281 
   8282       if (map->p_type == PT_GNU_RELRO
   8283 	  || map->p_type == PT_GNU_STACK)
   8284 	{
   8285 	  /* The PT_GNU_RELRO segment may contain the first a few
   8286 	     bytes in the .got.plt section even if the whole .got.plt
   8287 	     section isn't in the PT_GNU_RELRO segment.  We won't
   8288 	     change the size of the PT_GNU_RELRO segment.
   8289 	     Similarly, PT_GNU_STACK size is significant on uclinux
   8290 	     systems.    */
   8291 	  map->p_size = segment->p_memsz;
   8292 	  map->p_size_valid = 1;
   8293 	}
   8294 
   8295       /* Determine if this segment contains the ELF file header
   8296 	 and if it contains the program headers themselves.  */
   8297       map->includes_filehdr = (segment->p_offset == 0
   8298 			       && segment->p_filesz >= iehdr->e_ehsize);
   8299 
   8300       map->includes_phdrs = 0;
   8301       if (! phdr_included || segment->p_type != PT_LOAD)
   8302 	{
   8303 	  map->includes_phdrs =
   8304 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   8305 	     && (segment->p_offset + segment->p_filesz
   8306 		 >= ((bfd_vma) iehdr->e_phoff
   8307 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   8308 
   8309 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   8310 	    phdr_included = true;
   8311 	}
   8312 
   8313       lowest_section = NULL;
   8314       if (section_count != 0)
   8315 	{
   8316 	  unsigned int isec = 0;
   8317 
   8318 	  for (section = first_section;
   8319 	       section != NULL;
   8320 	       section = section->next)
   8321 	    {
   8322 	      this_hdr = &(elf_section_data(section)->this_hdr);
   8323 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   8324 		{
   8325 		  map->sections[isec++] = section->output_section;
   8326 		  if ((section->flags & SEC_ALLOC) != 0)
   8327 		    {
   8328 		      bfd_vma seg_off;
   8329 
   8330 		      if (lowest_section == NULL
   8331 			  || section->lma < lowest_section->lma)
   8332 			lowest_section = section;
   8333 
   8334 		      /* Section lmas are set up from PT_LOAD header
   8335 			 p_paddr in _bfd_elf_make_section_from_shdr.
   8336 			 If this header has a p_paddr that disagrees
   8337 			 with the section lma, flag the p_paddr as
   8338 			 invalid.  */
   8339 		      if ((section->flags & SEC_LOAD) != 0)
   8340 			seg_off = this_hdr->sh_offset - segment->p_offset;
   8341 		      else
   8342 			seg_off = this_hdr->sh_addr - segment->p_vaddr;
   8343 		      if (section->lma * opb - segment->p_paddr != seg_off)
   8344 			map->p_paddr_valid = false;
   8345 		    }
   8346 		  if (isec == section_count)
   8347 		    break;
   8348 		}
   8349 	    }
   8350 	}
   8351 
   8352       if (section_count == 0)
   8353 	map->p_vaddr_offset = segment->p_vaddr / opb;
   8354       else if (map->p_paddr_valid)
   8355 	{
   8356 	  /* Account for padding before the first section in the segment.  */
   8357 	  bfd_vma hdr_size = 0;
   8358 	  if (map->includes_filehdr)
   8359 	    hdr_size = iehdr->e_ehsize;
   8360 	  if (map->includes_phdrs)
   8361 	    hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
   8362 
   8363 	  map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
   8364 				 - (lowest_section ? lowest_section->lma : 0));
   8365 	}
   8366 
   8367       map->count = section_count;
   8368       *pointer_to_map = map;
   8369       pointer_to_map = &map->next;
   8370     }
   8371 
   8372   elf_seg_map (obfd) = map_first;
   8373   return true;
   8374 }
   8375 
   8376 /* Copy private BFD data.  This copies or rewrites ELF program header
   8377    information.  */
   8378 
   8379 static bool
   8380 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   8381 {
   8382   bfd_vma maxpagesize;
   8383 
   8384   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   8385       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   8386     return true;
   8387 
   8388   if (elf_tdata (ibfd)->phdr == NULL)
   8389     return true;
   8390 
   8391   if (ibfd->xvec == obfd->xvec)
   8392     {
   8393       /* Check to see if any sections in the input BFD
   8394 	 covered by ELF program header have changed.  */
   8395       Elf_Internal_Phdr *segment;
   8396       asection * section;
   8397       asection * osec;
   8398       asection * prev;
   8399       unsigned int i, num_segments;
   8400       Elf_Internal_Shdr *this_hdr;
   8401       const struct elf_backend_data *bed;
   8402 
   8403       bed = get_elf_backend_data (ibfd);
   8404 
   8405       /* Regenerate the segment map if p_paddr is set to 0.  */
   8406       if (bed->want_p_paddr_set_to_zero)
   8407 	goto rewrite;
   8408 
   8409       /* Initialize the segment mark field.  */
   8410       for (section = obfd->sections; section != NULL;
   8411 	   section = section->next)
   8412 	section->segment_mark = false;
   8413 
   8414       num_segments = elf_elfheader (ibfd)->e_phnum;
   8415       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   8416 	   i < num_segments;
   8417 	   i++, segment++)
   8418 	{
   8419 	  /* PR binutils/3535.  The Solaris linker always sets the p_paddr
   8420 	     and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
   8421 	     which severly confuses things, so always regenerate the segment
   8422 	     map in this case.  */
   8423 	  if (segment->p_paddr == 0
   8424 	      && segment->p_memsz == 0
   8425 	      && (segment->p_type == PT_INTERP
   8426 		  || segment->p_type == PT_DYNAMIC))
   8427 	    goto rewrite;
   8428 
   8429 	  for (section = ibfd->sections, prev = NULL;
   8430 	       section != NULL; section = section->next)
   8431 	    {
   8432 	      /* We mark the output section so that we know it comes
   8433 		 from the input BFD.  */
   8434 	      osec = section->output_section;
   8435 	      if (osec)
   8436 		osec->segment_mark = true;
   8437 
   8438 	      /* Check if this section is covered by the segment.  */
   8439 	      this_hdr = &(elf_section_data(section)->this_hdr);
   8440 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   8441 		{
   8442 		  /* FIXME: Check if its output section is changed or
   8443 		     removed.  What else do we need to check?  */
   8444 		  if (osec == NULL
   8445 		      || section->flags != osec->flags
   8446 		      || section->lma != osec->lma
   8447 		      || section->vma != osec->vma
   8448 		      || section->size != osec->size
   8449 		      || section->rawsize != osec->rawsize
   8450 		      || section->alignment_power != osec->alignment_power)
   8451 		    goto rewrite;
   8452 
   8453 		  /* PR 31450: If this is an allocated section then make sure
   8454 		     that this section's vma to lma relationship is the same
   8455 		     as previous (allocated) section's.  */
   8456 		  if (prev != NULL
   8457 		      && section->flags & SEC_ALLOC
   8458 		      && section->lma - section->vma != prev->lma - prev->vma)
   8459 		    goto rewrite;
   8460 
   8461 		  if (section->flags & SEC_ALLOC)
   8462 		    prev = section;
   8463 		}
   8464 	    }
   8465 	}
   8466 
   8467       /* Check to see if any output section do not come from the
   8468 	 input BFD.  */
   8469       for (section = obfd->sections; section != NULL;
   8470 	   section = section->next)
   8471 	{
   8472 	  if (!section->segment_mark)
   8473 	    goto rewrite;
   8474 	  else
   8475 	    section->segment_mark = false;
   8476 	}
   8477 
   8478       return copy_elf_program_header (ibfd, obfd);
   8479     }
   8480 
   8481  rewrite:
   8482   maxpagesize = 0;
   8483   if (ibfd->xvec == obfd->xvec)
   8484     {
   8485       /* When rewriting program header, set the output maxpagesize to
   8486 	 the maximum alignment of input PT_LOAD segments.  */
   8487       Elf_Internal_Phdr *segment;
   8488       unsigned int i;
   8489       unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
   8490 
   8491       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   8492 	   i < num_segments;
   8493 	   i++, segment++)
   8494 	if (segment->p_type == PT_LOAD
   8495 	    && maxpagesize < segment->p_align)
   8496 	  {
   8497 	    /* PR 17512: file: f17299af.  */
   8498 	    if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
   8499 	      /* xgettext:c-format */
   8500 	      _bfd_error_handler (_("%pB: warning: segment alignment of %#"
   8501 				    PRIx64 " is too large"),
   8502 				  ibfd, (uint64_t) segment->p_align);
   8503 	    else
   8504 	      maxpagesize = segment->p_align;
   8505 	  }
   8506     }
   8507   if (maxpagesize == 0)
   8508     maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
   8509 
   8510   return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
   8511 }
   8512 
   8513 /* Initialize private output section information from input section.  */
   8514 
   8515 bool
   8516 _bfd_elf_init_private_section_data (bfd *ibfd,
   8517 				    asection *isec,
   8518 				    bfd *obfd,
   8519 				    asection *osec,
   8520 				    struct bfd_link_info *link_info)
   8521 
   8522 {
   8523   Elf_Internal_Shdr *ihdr, *ohdr;
   8524   bool final_link = (link_info != NULL
   8525 		     && !bfd_link_relocatable (link_info));
   8526 
   8527   if (ibfd->xvec->flavour != bfd_target_elf_flavour
   8528       || obfd->xvec->flavour != bfd_target_elf_flavour)
   8529     return true;
   8530 
   8531   BFD_ASSERT (elf_section_data (osec) != NULL);
   8532 
   8533   /* If this is a known ABI section, ELF section type and flags may
   8534      have been set up when OSEC was created.  For normal sections we
   8535      allow the user to override the type and flags other than
   8536      SHF_MASKOS and SHF_MASKPROC.  */
   8537   if (elf_section_type (osec) == SHT_PROGBITS
   8538       || elf_section_type (osec) == SHT_NOTE
   8539       || elf_section_type (osec) == SHT_NOBITS)
   8540     elf_section_type (osec) = SHT_NULL;
   8541   /* For objcopy and relocatable link, copy the ELF section type from
   8542      the input file if the BFD section flags are the same.  (If they
   8543      are different the user may be doing something like
   8544      "objcopy --set-section-flags .text=alloc,data".)  For a final
   8545      link allow some flags that the linker clears to differ.  */
   8546   if (elf_section_type (osec) == SHT_NULL
   8547       && (osec->flags == isec->flags
   8548 	  || (final_link
   8549 	      && ((osec->flags ^ isec->flags)
   8550 		  & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
   8551     elf_section_type (osec) = elf_section_type (isec);
   8552 
   8553   /* FIXME: Is this correct for all OS/PROC specific flags?  */
   8554   elf_section_flags (osec) = (elf_section_flags (isec)
   8555 			      & (SHF_MASKOS | SHF_MASKPROC));
   8556 
   8557   /* Copy sh_info from input for mbind section.  */
   8558   if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
   8559       && elf_section_flags (isec) & SHF_GNU_MBIND)
   8560     elf_section_data (osec)->this_hdr.sh_info
   8561       = elf_section_data (isec)->this_hdr.sh_info;
   8562 
   8563   /* Set things up for objcopy and relocatable link.  The output
   8564      SHT_GROUP section will have its elf_next_in_group pointing back
   8565      to the input group members.  Ignore linker created group section.
   8566      See elfNN_ia64_object_p in elfxx-ia64.c.  */
   8567   if ((link_info == NULL
   8568        || !link_info->resolve_section_groups)
   8569       && (elf_sec_group (isec) == NULL
   8570 	  || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
   8571     {
   8572       if (elf_section_flags (isec) & SHF_GROUP)
   8573 	elf_section_flags (osec) |= SHF_GROUP;
   8574       elf_next_in_group (osec) = elf_next_in_group (isec);
   8575       elf_section_data (osec)->group = elf_section_data (isec)->group;
   8576     }
   8577 
   8578   /* If not decompress, preserve SHF_COMPRESSED.  */
   8579   if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
   8580     elf_section_flags (osec) |= (elf_section_flags (isec)
   8581 				 & SHF_COMPRESSED);
   8582 
   8583   ihdr = &elf_section_data (isec)->this_hdr;
   8584 
   8585   /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
   8586      don't use the output section of the linked-to section since it
   8587      may be NULL at this point.  */
   8588   if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
   8589     {
   8590       ohdr = &elf_section_data (osec)->this_hdr;
   8591       ohdr->sh_flags |= SHF_LINK_ORDER;
   8592       elf_linked_to_section (osec) = elf_linked_to_section (isec);
   8593     }
   8594 
   8595   osec->use_rela_p = isec->use_rela_p;
   8596 
   8597   return true;
   8598 }
   8599 
   8600 /* Copy private section information.  This copies over the entsize
   8601    field, and sometimes the info field.  */
   8602 
   8603 bool
   8604 _bfd_elf_copy_private_section_data (bfd *ibfd,
   8605 				    asection *isec,
   8606 				    bfd *obfd,
   8607 				    asection *osec)
   8608 {
   8609   Elf_Internal_Shdr *ihdr, *ohdr;
   8610 
   8611   if (ibfd->xvec->flavour != bfd_target_elf_flavour
   8612       || obfd->xvec->flavour != bfd_target_elf_flavour)
   8613     return true;
   8614 
   8615   ihdr = &elf_section_data (isec)->this_hdr;
   8616   ohdr = &elf_section_data (osec)->this_hdr;
   8617 
   8618   ohdr->sh_entsize = ihdr->sh_entsize;
   8619 
   8620   if (ihdr->sh_type == SHT_SYMTAB
   8621       || ihdr->sh_type == SHT_DYNSYM
   8622       || ihdr->sh_type == SHT_GNU_verneed
   8623       || ihdr->sh_type == SHT_GNU_verdef)
   8624     ohdr->sh_info = ihdr->sh_info;
   8625 
   8626   return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
   8627 					     NULL);
   8628 }
   8629 
   8630 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
   8631    necessary if we are removing either the SHT_GROUP section or any of
   8632    the group member sections.  DISCARDED is the value that a section's
   8633    output_section has if the section will be discarded, NULL when this
   8634    function is called from objcopy, bfd_abs_section_ptr when called
   8635    from the linker.  */
   8636 
   8637 bool
   8638 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
   8639 {
   8640   asection *isec;
   8641 
   8642   for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   8643     if (elf_section_type (isec) == SHT_GROUP)
   8644       {
   8645 	asection *first = elf_next_in_group (isec);
   8646 	asection *s = first;
   8647 	bfd_size_type removed = 0;
   8648 
   8649 	while (s != NULL)
   8650 	  {
   8651 	    /* If this member section is being output but the
   8652 	       SHT_GROUP section is not, then clear the group info
   8653 	       set up by _bfd_elf_copy_private_section_data.  */
   8654 	    if (s->output_section != discarded
   8655 		&& isec->output_section == discarded)
   8656 	      {
   8657 		elf_section_flags (s->output_section) &= ~SHF_GROUP;
   8658 		elf_group_name (s->output_section) = NULL;
   8659 	      }
   8660 	    else
   8661 	      {
   8662 		struct bfd_elf_section_data *elf_sec = elf_section_data (s);
   8663 		if (s->output_section == discarded
   8664 		    && isec->output_section != discarded)
   8665 		  {
   8666 		    /* Conversely, if the member section is not being
   8667 		       output but the SHT_GROUP section is, then adjust
   8668 		       its size.  */
   8669 		    removed += 4;
   8670 		    if (elf_sec->rel.hdr != NULL
   8671 			&& (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
   8672 		      removed += 4;
   8673 		    if (elf_sec->rela.hdr != NULL
   8674 			&& (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
   8675 		      removed += 4;
   8676 		  }
   8677 		else
   8678 		  {
   8679 		    /* Also adjust for zero-sized relocation member
   8680 		       section.  */
   8681 		    if (elf_sec->rel.hdr != NULL
   8682 			&& elf_sec->rel.hdr->sh_size == 0)
   8683 		      removed += 4;
   8684 		    if (elf_sec->rela.hdr != NULL
   8685 			&& elf_sec->rela.hdr->sh_size == 0)
   8686 		      removed += 4;
   8687 		  }
   8688 	      }
   8689 	    s = elf_next_in_group (s);
   8690 	    if (s == first)
   8691 	      break;
   8692 	  }
   8693 	if (removed != 0)
   8694 	  {
   8695 	    if (discarded != NULL)
   8696 	      {
   8697 		/* If we've been called for ld -r, then we need to
   8698 		   adjust the input section size.  */
   8699 		if (isec->rawsize == 0)
   8700 		  isec->rawsize = isec->size;
   8701 		isec->size = isec->rawsize - removed;
   8702 		if (isec->size <= 4)
   8703 		  {
   8704 		    isec->size = 0;
   8705 		    isec->flags |= SEC_EXCLUDE;
   8706 		  }
   8707 	      }
   8708 	    else if (isec->output_section != NULL)
   8709 	      {
   8710 		/* Adjust the output section size when called from
   8711 		   objcopy. */
   8712 		isec->output_section->size -= removed;
   8713 		if (isec->output_section->size <= 4)
   8714 		  {
   8715 		    isec->output_section->size = 0;
   8716 		    isec->output_section->flags |= SEC_EXCLUDE;
   8717 		  }
   8718 	      }
   8719 	  }
   8720       }
   8721 
   8722   return true;
   8723 }
   8724 
   8725 /* Copy private header information.  */
   8726 
   8727 bool
   8728 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
   8729 {
   8730   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   8731       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   8732     return true;
   8733 
   8734   /* Copy over private BFD data if it has not already been copied.
   8735      This must be done here, rather than in the copy_private_bfd_data
   8736      entry point, because the latter is called after the section
   8737      contents have been set, which means that the program headers have
   8738      already been worked out.  */
   8739   if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
   8740     {
   8741       if (! copy_private_bfd_data (ibfd, obfd))
   8742 	return false;
   8743     }
   8744 
   8745   return _bfd_elf_fixup_group_sections (ibfd, NULL);
   8746 }
   8747 
   8748 /* Copy private symbol information.  If this symbol is in a section
   8749    which we did not map into a BFD section, try to map the section
   8750    index correctly.  We use special macro definitions for the mapped
   8751    section indices; these definitions are interpreted by the
   8752    swap_out_syms function.  */
   8753 
   8754 #define MAP_ONESYMTAB (SHN_HIOS + 1)
   8755 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
   8756 #define MAP_STRTAB    (SHN_HIOS + 3)
   8757 #define MAP_SHSTRTAB  (SHN_HIOS + 4)
   8758 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
   8759 
   8760 bool
   8761 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
   8762 				   asymbol *isymarg,
   8763 				   bfd *obfd,
   8764 				   asymbol *osymarg)
   8765 {
   8766   elf_symbol_type *isym, *osym;
   8767 
   8768   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   8769       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   8770     return true;
   8771 
   8772   isym = elf_symbol_from (isymarg);
   8773   osym = elf_symbol_from (osymarg);
   8774 
   8775   if (isym != NULL
   8776       && isym->internal_elf_sym.st_shndx != 0
   8777       && osym != NULL
   8778       && bfd_is_abs_section (isym->symbol.section))
   8779     {
   8780       unsigned int shndx;
   8781 
   8782       shndx = isym->internal_elf_sym.st_shndx;
   8783       if (shndx == elf_onesymtab (ibfd))
   8784 	shndx = MAP_ONESYMTAB;
   8785       else if (shndx == elf_dynsymtab (ibfd))
   8786 	shndx = MAP_DYNSYMTAB;
   8787       else if (shndx == elf_elfsections (ibfd)[elf_onesymtab (ibfd)]->sh_link)
   8788 	shndx = MAP_STRTAB;
   8789       else if (shndx == elf_elfheader (ibfd)->e_shstrndx)
   8790 	shndx = MAP_SHSTRTAB;
   8791       else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
   8792 	shndx = MAP_SYM_SHNDX;
   8793       osym->internal_elf_sym.st_shndx = shndx;
   8794     }
   8795 
   8796   return true;
   8797 }
   8798 
   8799 /* Swap out the symbols.  */
   8800 
   8801 static bool
   8802 swap_out_syms (bfd *abfd,
   8803 	       struct elf_strtab_hash **sttp,
   8804 	       int relocatable_p,
   8805 	       struct bfd_link_info *info)
   8806 {
   8807   const struct elf_backend_data *bed;
   8808   unsigned int symcount;
   8809   asymbol **syms;
   8810   struct elf_strtab_hash *stt;
   8811   Elf_Internal_Shdr *symtab_hdr;
   8812   Elf_Internal_Shdr *symtab_shndx_hdr;
   8813   Elf_Internal_Shdr *symstrtab_hdr;
   8814   struct elf_sym_strtab *symstrtab;
   8815   bfd_byte *outbound_syms;
   8816   bfd_byte *outbound_shndx;
   8817   unsigned long outbound_syms_index;
   8818   unsigned int idx;
   8819   unsigned int num_locals;
   8820   size_t amt;
   8821   bool name_local_sections;
   8822 
   8823   if (!elf_map_symbols (abfd, &num_locals))
   8824     return false;
   8825 
   8826   /* Dump out the symtabs.  */
   8827   stt = _bfd_elf_strtab_init ();
   8828   if (stt == NULL)
   8829     return false;
   8830 
   8831   bed = get_elf_backend_data (abfd);
   8832   symcount = bfd_get_symcount (abfd);
   8833   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   8834   symtab_hdr->sh_type = SHT_SYMTAB;
   8835   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
   8836   symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
   8837   symtab_hdr->sh_info = num_locals + 1;
   8838   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   8839 
   8840   symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
   8841   symstrtab_hdr->sh_type = SHT_STRTAB;
   8842 
   8843   /* Allocate buffer to swap out the .strtab section.  */
   8844   if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
   8845       || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
   8846     {
   8847       bfd_set_error (bfd_error_no_memory);
   8848       _bfd_elf_strtab_free (stt);
   8849       return false;
   8850     }
   8851 
   8852   if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
   8853       || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
   8854     {
   8855     error_no_mem:
   8856       bfd_set_error (bfd_error_no_memory);
   8857     error_return:
   8858       free (symstrtab);
   8859       _bfd_elf_strtab_free (stt);
   8860       return false;
   8861     }
   8862   symtab_hdr->contents = outbound_syms;
   8863   outbound_syms_index = 0;
   8864 
   8865   outbound_shndx = NULL;
   8866 
   8867   if (elf_symtab_shndx_list (abfd))
   8868     {
   8869       symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
   8870       if (symtab_shndx_hdr->sh_name != 0)
   8871 	{
   8872 	  if (_bfd_mul_overflow (symcount + 1,
   8873 				 sizeof (Elf_External_Sym_Shndx), &amt))
   8874 	    goto error_no_mem;
   8875 	  outbound_shndx =  (bfd_byte *) bfd_zalloc (abfd, amt);
   8876 	  if (outbound_shndx == NULL)
   8877 	    goto error_return;
   8878 
   8879 	  symtab_shndx_hdr->contents = outbound_shndx;
   8880 	  symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
   8881 	  symtab_shndx_hdr->sh_size = amt;
   8882 	  symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
   8883 	  symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
   8884 	}
   8885       /* FIXME: What about any other headers in the list ?  */
   8886     }
   8887 
   8888   /* Now generate the data (for "contents").  */
   8889   {
   8890     /* Fill in zeroth symbol and swap it out.  */
   8891     Elf_Internal_Sym sym;
   8892     sym.st_name = 0;
   8893     sym.st_value = 0;
   8894     sym.st_size = 0;
   8895     sym.st_info = 0;
   8896     sym.st_other = 0;
   8897     sym.st_shndx = SHN_UNDEF;
   8898     sym.st_target_internal = 0;
   8899     symstrtab[outbound_syms_index].sym = sym;
   8900     symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
   8901     outbound_syms_index++;
   8902   }
   8903 
   8904   name_local_sections
   8905     = (bed->elf_backend_name_local_section_symbols
   8906        && bed->elf_backend_name_local_section_symbols (abfd));
   8907 
   8908   syms = bfd_get_outsymbols (abfd);
   8909   for (idx = 0; idx < symcount; idx++)
   8910     {
   8911       Elf_Internal_Sym sym;
   8912 
   8913       flagword flags = syms[idx]->flags;
   8914       if (!name_local_sections
   8915 	  && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
   8916 	{
   8917 	  /* Local section symbols have no name.  */
   8918 	  sym.st_name = (unsigned long) -1;
   8919 	}
   8920       else
   8921 	{
   8922 	  /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
   8923 	     to get the final offset for st_name.  */
   8924 	  sym.st_name
   8925 	    = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
   8926 						   false);
   8927 	  if (sym.st_name == (unsigned long) -1)
   8928 	    goto error_return;
   8929 	}
   8930 
   8931       bfd_vma value = syms[idx]->value;
   8932       elf_symbol_type *type_ptr = elf_symbol_from (syms[idx]);
   8933       asection *sec = syms[idx]->section;
   8934 
   8935       if ((flags & BSF_SECTION_SYM) == 0 && bfd_is_com_section (sec))
   8936 	{
   8937 	  /* ELF common symbols put the alignment into the `value' field,
   8938 	     and the size into the `size' field.  This is backwards from
   8939 	     how BFD handles it, so reverse it here.  */
   8940 	  sym.st_size = value;
   8941 	  if (type_ptr == NULL
   8942 	      || type_ptr->internal_elf_sym.st_value == 0)
   8943 	    sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
   8944 	  else
   8945 	    sym.st_value = type_ptr->internal_elf_sym.st_value;
   8946 	  sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   8947 	}
   8948       else
   8949 	{
   8950 	  unsigned int shndx;
   8951 
   8952 	  if (sec->output_section)
   8953 	    {
   8954 	      value += sec->output_offset;
   8955 	      sec = sec->output_section;
   8956 	    }
   8957 
   8958 	  /* Don't add in the section vma for relocatable output.  */
   8959 	  if (! relocatable_p)
   8960 	    value += sec->vma;
   8961 	  sym.st_value = value;
   8962 	  sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
   8963 
   8964 	  if (bfd_is_abs_section (sec)
   8965 	      && type_ptr != NULL
   8966 	      && type_ptr->internal_elf_sym.st_shndx != 0)
   8967 	    {
   8968 	      /* This symbol is in a real ELF section which we did
   8969 		 not create as a BFD section.  Undo the mapping done
   8970 		 by copy_private_symbol_data.  */
   8971 	      shndx = type_ptr->internal_elf_sym.st_shndx;
   8972 	      switch (shndx)
   8973 		{
   8974 		case MAP_ONESYMTAB:
   8975 		  shndx = elf_onesymtab (abfd);
   8976 		  break;
   8977 		case MAP_DYNSYMTAB:
   8978 		  shndx = elf_dynsymtab (abfd);
   8979 		  break;
   8980 		case MAP_STRTAB:
   8981 		  shndx = elf_strtab_sec (abfd);
   8982 		  break;
   8983 		case MAP_SHSTRTAB:
   8984 		  shndx = elf_shstrtab_sec (abfd);
   8985 		  break;
   8986 		case MAP_SYM_SHNDX:
   8987 		  if (elf_symtab_shndx_list (abfd))
   8988 		    shndx = elf_symtab_shndx_list (abfd)->ndx;
   8989 		  break;
   8990 		case SHN_COMMON:
   8991 		case SHN_ABS:
   8992 		  shndx = SHN_ABS;
   8993 		  break;
   8994 		default:
   8995 		  if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
   8996 		    {
   8997 		      if (bed->symbol_section_index)
   8998 			shndx = bed->symbol_section_index (abfd, type_ptr);
   8999 		      /* Otherwise just leave the index alone.  */
   9000 		    }
   9001 		  else
   9002 		    {
   9003 		      if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
   9004 			_bfd_error_handler (_("%pB: \
   9005 Unable to handle section index %x in ELF symbol.  Using ABS instead."),
   9006 					  abfd, shndx);
   9007 		      shndx = SHN_ABS;
   9008 		    }
   9009 		  break;
   9010 		}
   9011 	    }
   9012 	  else
   9013 	    {
   9014 	      shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   9015 
   9016 	      if (shndx == SHN_BAD)
   9017 		{
   9018 		  asection *sec2;
   9019 
   9020 		  /* Writing this would be a hell of a lot easier if
   9021 		     we had some decent documentation on bfd, and
   9022 		     knew what to expect of the library, and what to
   9023 		     demand of applications.  For example, it
   9024 		     appears that `objcopy' might not set the
   9025 		     section of a symbol to be a section that is
   9026 		     actually in the output file.  */
   9027 		  sec2 = bfd_get_section_by_name (abfd, sec->name);
   9028 		  if (sec2 != NULL)
   9029 		    shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
   9030 		  if (shndx == SHN_BAD)
   9031 		    {
   9032 		      /* xgettext:c-format */
   9033 		      _bfd_error_handler
   9034 			(_("unable to find equivalent output section"
   9035 			   " for symbol '%s' from section '%s'"),
   9036 			 syms[idx]->name ? syms[idx]->name : "<Local sym>",
   9037 			 sec->name);
   9038 		      bfd_set_error (bfd_error_invalid_operation);
   9039 		      goto error_return;
   9040 		    }
   9041 		}
   9042 	    }
   9043 
   9044 	  sym.st_shndx = shndx;
   9045 	}
   9046 
   9047       int type;
   9048       if ((flags & BSF_THREAD_LOCAL) != 0)
   9049 	type = STT_TLS;
   9050       else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
   9051 	type = STT_GNU_IFUNC;
   9052       else if ((flags & BSF_FUNCTION) != 0)
   9053 	type = STT_FUNC;
   9054       else if ((flags & BSF_OBJECT) != 0)
   9055 	type = STT_OBJECT;
   9056       else if ((flags & BSF_RELC) != 0)
   9057 	type = STT_RELC;
   9058       else if ((flags & BSF_SRELC) != 0)
   9059 	type = STT_SRELC;
   9060       else
   9061 	type = STT_NOTYPE;
   9062 
   9063       if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
   9064 	type = STT_TLS;
   9065 
   9066       /* Processor-specific types.  */
   9067       if (type_ptr != NULL
   9068 	  && bed->elf_backend_get_symbol_type)
   9069 	type = ((*bed->elf_backend_get_symbol_type)
   9070 		(&type_ptr->internal_elf_sym, type));
   9071 
   9072       if (flags & BSF_SECTION_SYM)
   9073 	{
   9074 	  if (flags & BSF_GLOBAL)
   9075 	    sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   9076 	  else
   9077 	    sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   9078 	}
   9079       else if (bfd_is_com_section (syms[idx]->section))
   9080 	{
   9081 	  if (type != STT_TLS)
   9082 	    {
   9083 	      if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
   9084 		type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
   9085 			? STT_COMMON : STT_OBJECT);
   9086 	      else
   9087 		type = ((flags & BSF_ELF_COMMON) != 0
   9088 			? STT_COMMON : STT_OBJECT);
   9089 	    }
   9090 	  sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
   9091 	}
   9092       else if (bfd_is_und_section (syms[idx]->section))
   9093 	sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
   9094 				    ? STB_WEAK
   9095 				    : STB_GLOBAL),
   9096 				   type);
   9097       else if (flags & BSF_FILE)
   9098 	sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   9099       else
   9100 	{
   9101 	  int bind = STB_LOCAL;
   9102 
   9103 	  if (flags & BSF_LOCAL)
   9104 	    bind = STB_LOCAL;
   9105 	  else if (flags & BSF_GNU_UNIQUE)
   9106 	    bind = STB_GNU_UNIQUE;
   9107 	  else if (flags & BSF_WEAK)
   9108 	    bind = STB_WEAK;
   9109 	  else if (flags & BSF_GLOBAL)
   9110 	    bind = STB_GLOBAL;
   9111 
   9112 	  sym.st_info = ELF_ST_INFO (bind, type);
   9113 	}
   9114 
   9115       if (type_ptr != NULL)
   9116 	{
   9117 	  sym.st_other = type_ptr->internal_elf_sym.st_other;
   9118 	  sym.st_target_internal
   9119 	    = type_ptr->internal_elf_sym.st_target_internal;
   9120 	}
   9121       else
   9122 	{
   9123 	  sym.st_other = 0;
   9124 	  sym.st_target_internal = 0;
   9125 	}
   9126 
   9127       symstrtab[outbound_syms_index].sym = sym;
   9128       symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
   9129       outbound_syms_index++;
   9130     }
   9131 
   9132   /* Finalize the .strtab section.  */
   9133   _bfd_elf_strtab_finalize (stt);
   9134 
   9135   /* Swap out the .strtab section.  */
   9136   for (idx = 0; idx < outbound_syms_index; idx++)
   9137     {
   9138       struct elf_sym_strtab *elfsym = &symstrtab[idx];
   9139       if (elfsym->sym.st_name == (unsigned long) -1)
   9140 	elfsym->sym.st_name = 0;
   9141       else
   9142 	elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
   9143 						      elfsym->sym.st_name);
   9144       if (info && info->callbacks->ctf_new_symbol)
   9145 	info->callbacks->ctf_new_symbol (elfsym->dest_index,
   9146 					 &elfsym->sym);
   9147 
   9148       /* Inform the linker of the addition of this symbol.  */
   9149 
   9150       bed->s->swap_symbol_out (abfd, &elfsym->sym,
   9151 			       (outbound_syms
   9152 				+ (elfsym->dest_index
   9153 				   * bed->s->sizeof_sym)),
   9154 			       NPTR_ADD (outbound_shndx,
   9155 					 (elfsym->dest_index
   9156 					  * sizeof (Elf_External_Sym_Shndx))));
   9157     }
   9158   free (symstrtab);
   9159 
   9160   *sttp = stt;
   9161   symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
   9162   symstrtab_hdr->sh_type = SHT_STRTAB;
   9163   symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   9164   symstrtab_hdr->sh_addr = 0;
   9165   symstrtab_hdr->sh_entsize = 0;
   9166   symstrtab_hdr->sh_link = 0;
   9167   symstrtab_hdr->sh_info = 0;
   9168   symstrtab_hdr->sh_addralign = 1;
   9169 
   9170   return true;
   9171 }
   9172 
   9173 /* Return the number of bytes required to hold the symtab vector.
   9174 
   9175    Note that we base it on the count plus 1, since we will null terminate
   9176    the vector allocated based on this size.  However, the ELF symbol table
   9177    always has a dummy entry as symbol #0, so it ends up even.  */
   9178 
   9179 long
   9180 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
   9181 {
   9182   bfd_size_type symcount;
   9183   long symtab_size;
   9184   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
   9185 
   9186   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   9187   if (symcount > LONG_MAX / sizeof (asymbol *))
   9188     {
   9189       bfd_set_error (bfd_error_file_too_big);
   9190       return -1;
   9191     }
   9192   symtab_size = symcount * (sizeof (asymbol *));
   9193   if (symcount == 0)
   9194     symtab_size = sizeof (asymbol *);
   9195   else if (!bfd_write_p (abfd))
   9196     {
   9197       ufile_ptr filesize = bfd_get_file_size (abfd);
   9198 
   9199       if (filesize != 0 && (unsigned long) symtab_size > filesize)
   9200 	{
   9201 	  bfd_set_error (bfd_error_file_truncated);
   9202 	  return -1;
   9203 	}
   9204     }
   9205 
   9206   return symtab_size;
   9207 }
   9208 
   9209 long
   9210 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
   9211 {
   9212   bfd_size_type symcount;
   9213   long symtab_size;
   9214   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   9215 
   9216   if (elf_dynsymtab (abfd) == 0)
   9217     {
   9218       /* Check if there is dynamic symbol table.  */
   9219       symcount = elf_tdata (abfd)->dt_symtab_count;
   9220       if (symcount)
   9221 	goto compute_symtab_size;
   9222 
   9223       bfd_set_error (bfd_error_invalid_operation);
   9224       return -1;
   9225     }
   9226 
   9227   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   9228   if (symcount > LONG_MAX / sizeof (asymbol *))
   9229     {
   9230       bfd_set_error (bfd_error_file_too_big);
   9231       return -1;
   9232     }
   9233 
   9234  compute_symtab_size:
   9235   symtab_size = symcount * (sizeof (asymbol *));
   9236   if (symcount == 0)
   9237     symtab_size = sizeof (asymbol *);
   9238   else if (!bfd_write_p (abfd))
   9239     {
   9240       ufile_ptr filesize = bfd_get_file_size (abfd);
   9241 
   9242       if (filesize != 0 && (unsigned long) symtab_size > filesize)
   9243 	{
   9244 	  bfd_set_error (bfd_error_file_truncated);
   9245 	  return -1;
   9246 	}
   9247     }
   9248 
   9249   return symtab_size;
   9250 }
   9251 
   9252 long
   9253 _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
   9254 {
   9255   if (asect->reloc_count != 0 && !bfd_write_p (abfd))
   9256     {
   9257       /* Sanity check reloc section size.  */
   9258       ufile_ptr filesize = bfd_get_file_size (abfd);
   9259 
   9260       if (filesize != 0)
   9261 	{
   9262 	  struct bfd_elf_section_data *d = elf_section_data (asect);
   9263 	  bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
   9264 	  bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
   9265 
   9266 	  if (rel_size + rela_size > filesize
   9267 	      || rel_size + rela_size < rel_size)
   9268 	    {
   9269 	      bfd_set_error (bfd_error_file_truncated);
   9270 	      return -1;
   9271 	    }
   9272 	}
   9273     }
   9274 
   9275 #if SIZEOF_LONG == SIZEOF_INT
   9276   if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
   9277     {
   9278       bfd_set_error (bfd_error_file_too_big);
   9279       return -1;
   9280     }
   9281 #endif
   9282   return (asect->reloc_count + 1L) * sizeof (arelent *);
   9283 }
   9284 
   9285 /* Canonicalize the relocs.  */
   9286 
   9287 long
   9288 _bfd_elf_canonicalize_reloc (bfd *abfd,
   9289 			     sec_ptr section,
   9290 			     arelent **relptr,
   9291 			     asymbol **symbols)
   9292 {
   9293   arelent *tblptr;
   9294   unsigned int i;
   9295   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   9296 
   9297   if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
   9298     return -1;
   9299 
   9300   tblptr = section->relocation;
   9301   for (i = 0; i < section->reloc_count; i++)
   9302     *relptr++ = tblptr++;
   9303 
   9304   *relptr = NULL;
   9305 
   9306   return section->reloc_count;
   9307 }
   9308 
   9309 long
   9310 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
   9311 {
   9312   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   9313   long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
   9314 
   9315   if (symcount >= 0)
   9316     abfd->symcount = symcount;
   9317   return symcount;
   9318 }
   9319 
   9320 long
   9321 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
   9322 				      asymbol **allocation)
   9323 {
   9324   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   9325   long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
   9326 
   9327   if (symcount >= 0)
   9328     abfd->dynsymcount = symcount;
   9329   return symcount;
   9330 }
   9331 
   9332 /* Return the size required for the dynamic reloc entries.  Any loadable
   9333    section that was actually installed in the BFD, and has type SHT_REL
   9334    or SHT_RELA, and uses the dynamic symbol table, is considered to be a
   9335    dynamic reloc section.  */
   9336 
   9337 long
   9338 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
   9339 {
   9340   bfd_size_type count, ext_rel_size;
   9341   asection *s;
   9342 
   9343   if (elf_dynsymtab (abfd) == 0)
   9344     {
   9345       bfd_set_error (bfd_error_invalid_operation);
   9346       return -1;
   9347     }
   9348 
   9349   count = 1;
   9350   ext_rel_size = 0;
   9351   for (s = abfd->sections; s != NULL; s = s->next)
   9352     if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   9353 	&& (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   9354 	    || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
   9355 	&& (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
   9356       {
   9357 	ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
   9358 	if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
   9359 	  {
   9360 	    bfd_set_error (bfd_error_file_truncated);
   9361 	    return -1;
   9362 	  }
   9363 	count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
   9364 	if (count > LONG_MAX / sizeof (arelent *))
   9365 	  {
   9366 	    bfd_set_error (bfd_error_file_too_big);
   9367 	    return -1;
   9368 	  }
   9369       }
   9370   if (count > 1 && !bfd_write_p (abfd))
   9371     {
   9372       /* Sanity check reloc section sizes.  */
   9373       ufile_ptr filesize = bfd_get_file_size (abfd);
   9374       if (filesize != 0 && ext_rel_size > filesize)
   9375 	{
   9376 	  bfd_set_error (bfd_error_file_truncated);
   9377 	  return -1;
   9378 	}
   9379     }
   9380   return count * sizeof (arelent *);
   9381 }
   9382 
   9383 /* Canonicalize the dynamic relocation entries.  Note that we return the
   9384    dynamic relocations as a single block, although they are actually
   9385    associated with particular sections; the interface, which was
   9386    designed for SunOS style shared libraries, expects that there is only
   9387    one set of dynamic relocs.  Any loadable section that was actually
   9388    installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
   9389    dynamic symbol table, is considered to be a dynamic reloc section.  */
   9390 
   9391 long
   9392 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
   9393 				     arelent **storage,
   9394 				     asymbol **syms)
   9395 {
   9396   bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
   9397   asection *s;
   9398   long ret;
   9399 
   9400   if (elf_dynsymtab (abfd) == 0)
   9401     {
   9402       bfd_set_error (bfd_error_invalid_operation);
   9403       return -1;
   9404     }
   9405 
   9406   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   9407   ret = 0;
   9408   for (s = abfd->sections; s != NULL; s = s->next)
   9409     {
   9410       if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   9411 	  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   9412 	      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
   9413 	  && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
   9414 	{
   9415 	  arelent *p;
   9416 	  long count, i;
   9417 
   9418 	  if (! (*slurp_relocs) (abfd, s, syms, true))
   9419 	    return -1;
   9420 	  count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
   9421 	  p = s->relocation;
   9422 	  for (i = 0; i < count; i++)
   9423 	    *storage++ = p++;
   9424 	  ret += count;
   9425 	}
   9426     }
   9427 
   9428   *storage = NULL;
   9429 
   9430   return ret;
   9431 }
   9432 
   9433 /* Read in the version information.  */
   9435 
   9436 bool
   9437 _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
   9438 {
   9439   bfd_byte *contents = NULL;
   9440   unsigned int freeidx = 0;
   9441   size_t amt;
   9442   void *contents_addr = NULL;
   9443   size_t contents_size = 0;
   9444 
   9445   if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
   9446     {
   9447       Elf_Internal_Shdr *hdr;
   9448       Elf_External_Verneed *everneed;
   9449       Elf_Internal_Verneed *iverneed;
   9450       unsigned int i;
   9451       bfd_byte *contents_end;
   9452       size_t verneed_count;
   9453       size_t verneed_size;
   9454 
   9455       if (elf_tdata (abfd)->dt_verneed != NULL)
   9456 	{
   9457 	  hdr = NULL;
   9458 	  contents = elf_tdata (abfd)->dt_verneed;
   9459 	  verneed_count = elf_tdata (abfd)->dt_verneed_count;
   9460 	  verneed_size = verneed_count * sizeof (Elf_External_Verneed);
   9461 	}
   9462       else
   9463 	{
   9464 	  hdr = &elf_tdata (abfd)->dynverref_hdr;
   9465 
   9466 	  if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
   9467 	    {
   9468 	    error_return_bad_verref:
   9469 	      _bfd_error_handler
   9470 		(_("%pB: .gnu.version_r invalid entry"), abfd);
   9471 	      bfd_set_error (bfd_error_bad_value);
   9472 	    error_return_verref:
   9473 	      elf_tdata (abfd)->verref = NULL;
   9474 	      elf_tdata (abfd)->cverrefs = 0;
   9475 	      goto error_return;
   9476 	    }
   9477 
   9478 	  if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
   9479 	    goto error_return_verref;
   9480 	  contents_size = hdr->sh_size;
   9481 	  contents = _bfd_mmap_readonly_temporary (abfd, contents_size,
   9482 						   &contents_addr,
   9483 						   &contents_size);
   9484 	  if (contents == NULL)
   9485 	    goto error_return_verref;
   9486 
   9487 	  verneed_size = hdr->sh_size;
   9488 	  verneed_count = hdr->sh_info;
   9489 	}
   9490 
   9491       if (_bfd_mul_overflow (verneed_count,
   9492 			     sizeof (Elf_Internal_Verneed), &amt))
   9493 	{
   9494 	  bfd_set_error (bfd_error_file_too_big);
   9495 	  goto error_return_verref;
   9496 	}
   9497       if (amt == 0)
   9498 	goto error_return_verref;
   9499       elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
   9500       if (elf_tdata (abfd)->verref == NULL)
   9501 	goto error_return_verref;
   9502 
   9503       BFD_ASSERT (sizeof (Elf_External_Verneed)
   9504 		  == sizeof (Elf_External_Vernaux));
   9505       contents_end = (contents + verneed_size
   9506 		      - sizeof (Elf_External_Verneed));
   9507       everneed = (Elf_External_Verneed *) contents;
   9508       iverneed = elf_tdata (abfd)->verref;
   9509       for (i = 0; i < verneed_count; i++, iverneed++)
   9510 	{
   9511 	  Elf_External_Vernaux *evernaux;
   9512 	  Elf_Internal_Vernaux *ivernaux;
   9513 	  unsigned int j;
   9514 
   9515 	  _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
   9516 
   9517 	  iverneed->vn_bfd = abfd;
   9518 
   9519 	  if (elf_use_dt_symtab_p (abfd))
   9520 	    {
   9521 	      if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz)
   9522 		iverneed->vn_filename
   9523 		  = elf_tdata (abfd)->dt_strtab + iverneed->vn_file;
   9524 	      else
   9525 		iverneed->vn_filename = NULL;
   9526 	    }
   9527 	  else if (hdr == NULL)
   9528 	    goto error_return_bad_verref;
   9529 	  else
   9530 	    iverneed->vn_filename
   9531 	      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   9532 						 iverneed->vn_file);
   9533 	  if (iverneed->vn_filename == NULL)
   9534 	    goto error_return_bad_verref;
   9535 
   9536 	  if (iverneed->vn_cnt == 0)
   9537 	    iverneed->vn_auxptr = NULL;
   9538 	  else
   9539 	    {
   9540 	      if (_bfd_mul_overflow (iverneed->vn_cnt,
   9541 				     sizeof (Elf_Internal_Vernaux), &amt))
   9542 		{
   9543 		  bfd_set_error (bfd_error_file_too_big);
   9544 		  goto error_return_verref;
   9545 		}
   9546 	      iverneed->vn_auxptr = (struct elf_internal_vernaux *)
   9547 		bfd_alloc (abfd, amt);
   9548 	      if (iverneed->vn_auxptr == NULL)
   9549 		goto error_return_verref;
   9550 	    }
   9551 
   9552 	  if (iverneed->vn_aux
   9553 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   9554 	    goto error_return_bad_verref;
   9555 
   9556 	  evernaux = ((Elf_External_Vernaux *)
   9557 		      ((bfd_byte *) everneed + iverneed->vn_aux));
   9558 	  ivernaux = iverneed->vn_auxptr;
   9559 	  for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
   9560 	    {
   9561 	      _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
   9562 
   9563 	      if (elf_use_dt_symtab_p (abfd))
   9564 		{
   9565 		  if (ivernaux->vna_name < elf_tdata (abfd)->dt_strsz)
   9566 		    ivernaux->vna_nodename
   9567 		      = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name;
   9568 		  else
   9569 		    ivernaux->vna_nodename = NULL;
   9570 		}
   9571 	      else if (hdr == NULL)
   9572 		goto error_return_bad_verref;
   9573 	      else
   9574 		ivernaux->vna_nodename
   9575 		  = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   9576 						     ivernaux->vna_name);
   9577 	      if (ivernaux->vna_nodename == NULL)
   9578 		goto error_return_bad_verref;
   9579 
   9580 	      if (ivernaux->vna_other > freeidx)
   9581 		freeidx = ivernaux->vna_other;
   9582 
   9583 	      ivernaux->vna_nextptr = NULL;
   9584 	      if (ivernaux->vna_next == 0)
   9585 		{
   9586 		  iverneed->vn_cnt = j + 1;
   9587 		  break;
   9588 		}
   9589 	      if (j + 1 < iverneed->vn_cnt)
   9590 		ivernaux->vna_nextptr = ivernaux + 1;
   9591 
   9592 	      if (ivernaux->vna_next
   9593 		  > (size_t) (contents_end - (bfd_byte *) evernaux))
   9594 		goto error_return_bad_verref;
   9595 
   9596 	      evernaux = ((Elf_External_Vernaux *)
   9597 			  ((bfd_byte *) evernaux + ivernaux->vna_next));
   9598 	    }
   9599 
   9600 	  iverneed->vn_nextref = NULL;
   9601 	  if (iverneed->vn_next == 0)
   9602 	    break;
   9603 	  if (hdr != NULL && (i + 1 < hdr->sh_info))
   9604 	    iverneed->vn_nextref = iverneed + 1;
   9605 
   9606 	  if (iverneed->vn_next
   9607 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   9608 	    goto error_return_bad_verref;
   9609 
   9610 	  everneed = ((Elf_External_Verneed *)
   9611 		      ((bfd_byte *) everneed + iverneed->vn_next));
   9612 	}
   9613       elf_tdata (abfd)->cverrefs = i;
   9614 
   9615       if (contents != elf_tdata (abfd)->dt_verneed)
   9616 	_bfd_munmap_readonly_temporary (contents_addr, contents_size);
   9617       contents = NULL;
   9618       contents_addr = NULL;
   9619     }
   9620 
   9621   if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
   9622     {
   9623       Elf_Internal_Shdr *hdr;
   9624       Elf_External_Verdef *everdef;
   9625       Elf_Internal_Verdef *iverdef;
   9626       Elf_Internal_Verdef *iverdefarr;
   9627       Elf_Internal_Verdef iverdefmem;
   9628       unsigned int i;
   9629       unsigned int maxidx;
   9630       bfd_byte *contents_end_def, *contents_end_aux;
   9631       size_t verdef_count;
   9632       size_t verdef_size;
   9633 
   9634       if (elf_tdata (abfd)->dt_verdef != NULL)
   9635 	{
   9636 	  hdr = NULL;
   9637 	  contents = elf_tdata (abfd)->dt_verdef;
   9638 	  verdef_count = elf_tdata (abfd)->dt_verdef_count;
   9639 	  verdef_size = verdef_count * sizeof (Elf_External_Verdef);
   9640 	}
   9641       else
   9642 	{
   9643 	  hdr = &elf_tdata (abfd)->dynverdef_hdr;
   9644 
   9645 	  if (hdr->sh_size < sizeof (Elf_External_Verdef))
   9646 	    {
   9647 	    error_return_bad_verdef:
   9648 	      _bfd_error_handler
   9649 		(_("%pB: .gnu.version_d invalid entry"), abfd);
   9650 	      bfd_set_error (bfd_error_bad_value);
   9651 	    error_return_verdef:
   9652 	      elf_tdata (abfd)->verdef = NULL;
   9653 	      elf_tdata (abfd)->cverdefs = 0;
   9654 	      goto error_return;
   9655 	    }
   9656 
   9657 	  if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
   9658 	    goto error_return_verdef;
   9659 	  contents_size = hdr->sh_size;
   9660 	  contents = _bfd_mmap_readonly_temporary (abfd, contents_size,
   9661 						   &contents_addr,
   9662 						   &contents_size);
   9663 	  if (contents == NULL)
   9664 	    goto error_return_verdef;
   9665 
   9666 	  BFD_ASSERT (sizeof (Elf_External_Verdef)
   9667 		      >= sizeof (Elf_External_Verdaux));
   9668 
   9669 	  verdef_count = hdr->sh_info;
   9670 	  verdef_size = hdr->sh_size;
   9671 	}
   9672 
   9673       contents_end_def = (contents + verdef_size
   9674 			  - sizeof (Elf_External_Verdef));
   9675       contents_end_aux = (contents + verdef_size
   9676 			  - sizeof (Elf_External_Verdaux));
   9677 
   9678       /* We know the number of entries in the section but not the maximum
   9679 	 index.  Therefore we have to run through all entries and find
   9680 	 the maximum.  */
   9681       everdef = (Elf_External_Verdef *) contents;
   9682       maxidx = 0;
   9683       for (i = 0; i < verdef_count; ++i)
   9684 	{
   9685 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   9686 
   9687 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
   9688 	    goto error_return_bad_verdef;
   9689 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
   9690 	    maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
   9691 
   9692 	  if (iverdefmem.vd_next == 0)
   9693 	    break;
   9694 
   9695 	  if (iverdefmem.vd_next
   9696 	      > (size_t) (contents_end_def - (bfd_byte *) everdef))
   9697 	    goto error_return_bad_verdef;
   9698 
   9699 	  everdef = ((Elf_External_Verdef *)
   9700 		     ((bfd_byte *) everdef + iverdefmem.vd_next));
   9701 	}
   9702 
   9703       if (default_imported_symver)
   9704 	{
   9705 	  if (freeidx > maxidx)
   9706 	    maxidx = ++freeidx;
   9707 	  else
   9708 	    freeidx = ++maxidx;
   9709 	}
   9710       if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
   9711 	{
   9712 	  bfd_set_error (bfd_error_file_too_big);
   9713 	  goto error_return_verdef;
   9714 	}
   9715 
   9716       if (amt == 0)
   9717 	goto error_return_verdef;
   9718       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
   9719       if (elf_tdata (abfd)->verdef == NULL)
   9720 	goto error_return_verdef;
   9721 
   9722       elf_tdata (abfd)->cverdefs = maxidx;
   9723 
   9724       everdef = (Elf_External_Verdef *) contents;
   9725       iverdefarr = elf_tdata (abfd)->verdef;
   9726       for (i = 0; i < verdef_count; ++i)
   9727 	{
   9728 	  Elf_External_Verdaux *everdaux;
   9729 	  Elf_Internal_Verdaux *iverdaux;
   9730 	  unsigned int j;
   9731 
   9732 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   9733 
   9734 	  if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
   9735 	    goto error_return_bad_verdef;
   9736 
   9737 	  iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
   9738 	  memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
   9739 
   9740 	  iverdef->vd_bfd = abfd;
   9741 
   9742 	  if (iverdef->vd_cnt == 0)
   9743 	    iverdef->vd_auxptr = NULL;
   9744 	  else
   9745 	    {
   9746 	      if (_bfd_mul_overflow (iverdef->vd_cnt,
   9747 				     sizeof (Elf_Internal_Verdaux), &amt))
   9748 		{
   9749 		  bfd_set_error (bfd_error_file_too_big);
   9750 		  goto error_return_verdef;
   9751 		}
   9752 	      iverdef->vd_auxptr = (struct elf_internal_verdaux *)
   9753 		bfd_alloc (abfd, amt);
   9754 	      if (iverdef->vd_auxptr == NULL)
   9755 		goto error_return_verdef;
   9756 	    }
   9757 
   9758 	  if (iverdef->vd_aux
   9759 	      > (size_t) (contents_end_aux - (bfd_byte *) everdef))
   9760 	    goto error_return_bad_verdef;
   9761 
   9762 	  everdaux = ((Elf_External_Verdaux *)
   9763 		      ((bfd_byte *) everdef + iverdef->vd_aux));
   9764 	  iverdaux = iverdef->vd_auxptr;
   9765 	  for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
   9766 	    {
   9767 	      _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
   9768 
   9769 	      if (elf_use_dt_symtab_p (abfd))
   9770 		{
   9771 		  if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz)
   9772 		    iverdaux->vda_nodename
   9773 		      = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name;
   9774 		  else
   9775 		    iverdaux->vda_nodename = NULL;
   9776 		}
   9777 	      else
   9778 		iverdaux->vda_nodename
   9779 		  = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   9780 						     iverdaux->vda_name);
   9781 	      if (iverdaux->vda_nodename == NULL)
   9782 		goto error_return_bad_verdef;
   9783 
   9784 	      iverdaux->vda_nextptr = NULL;
   9785 	      if (iverdaux->vda_next == 0)
   9786 		{
   9787 		  iverdef->vd_cnt = j + 1;
   9788 		  break;
   9789 		}
   9790 	      if (j + 1 < iverdef->vd_cnt)
   9791 		iverdaux->vda_nextptr = iverdaux + 1;
   9792 
   9793 	      if (iverdaux->vda_next
   9794 		  > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
   9795 		goto error_return_bad_verdef;
   9796 
   9797 	      everdaux = ((Elf_External_Verdaux *)
   9798 			  ((bfd_byte *) everdaux + iverdaux->vda_next));
   9799 	    }
   9800 
   9801 	  iverdef->vd_nodename = NULL;
   9802 	  if (iverdef->vd_cnt)
   9803 	    iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
   9804 
   9805 	  iverdef->vd_nextdef = NULL;
   9806 	  if (iverdef->vd_next == 0)
   9807 	    break;
   9808 	  if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
   9809 	    iverdef->vd_nextdef = iverdef + 1;
   9810 
   9811 	  everdef = ((Elf_External_Verdef *)
   9812 		     ((bfd_byte *) everdef + iverdef->vd_next));
   9813 	}
   9814 
   9815       if (contents != elf_tdata (abfd)->dt_verdef)
   9816 	_bfd_munmap_readonly_temporary (contents_addr, contents_size);
   9817       contents = NULL;
   9818       contents_addr = NULL;
   9819     }
   9820   else if (default_imported_symver)
   9821     {
   9822       if (freeidx < 3)
   9823 	freeidx = 3;
   9824       else
   9825 	freeidx++;
   9826 
   9827       if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
   9828 	{
   9829 	  bfd_set_error (bfd_error_file_too_big);
   9830 	  goto error_return;
   9831 	}
   9832       if (amt == 0)
   9833 	goto error_return;
   9834       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
   9835       if (elf_tdata (abfd)->verdef == NULL)
   9836 	goto error_return;
   9837 
   9838       elf_tdata (abfd)->cverdefs = freeidx;
   9839     }
   9840 
   9841   /* Create a default version based on the soname.  */
   9842   if (default_imported_symver)
   9843     {
   9844       Elf_Internal_Verdef *iverdef;
   9845       Elf_Internal_Verdaux *iverdaux;
   9846 
   9847       iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
   9848 
   9849       iverdef->vd_version = VER_DEF_CURRENT;
   9850       iverdef->vd_flags = 0;
   9851       iverdef->vd_ndx = freeidx;
   9852       iverdef->vd_cnt = 1;
   9853 
   9854       iverdef->vd_bfd = abfd;
   9855 
   9856       iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
   9857       if (iverdef->vd_nodename == NULL)
   9858 	goto error_return_verdef;
   9859       iverdef->vd_nextdef = NULL;
   9860       iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
   9861 			    bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
   9862       if (iverdef->vd_auxptr == NULL)
   9863 	goto error_return_verdef;
   9864 
   9865       iverdaux = iverdef->vd_auxptr;
   9866       iverdaux->vda_nodename = iverdef->vd_nodename;
   9867     }
   9868 
   9869   return true;
   9870 
   9871  error_return:
   9872   if (contents != elf_tdata (abfd)->dt_verneed
   9873       && contents != elf_tdata (abfd)->dt_verdef)
   9874     _bfd_munmap_readonly_temporary (contents_addr, contents_size);
   9875   return false;
   9876 }
   9877 
   9878 asymbol *
   9880 _bfd_elf_make_empty_symbol (bfd *abfd)
   9881 {
   9882   elf_symbol_type *newsym;
   9883 
   9884   newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
   9885   if (!newsym)
   9886     return NULL;
   9887   newsym->symbol.the_bfd = abfd;
   9888   return &newsym->symbol;
   9889 }
   9890 
   9891 void
   9892 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
   9893 			  asymbol *symbol,
   9894 			  symbol_info *ret)
   9895 {
   9896   bfd_symbol_info (symbol, ret);
   9897 }
   9898 
   9899 /* Return whether a symbol name implies a local symbol.  Most targets
   9900    use this function for the is_local_label_name entry point, but some
   9901    override it.  */
   9902 
   9903 bool
   9904 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
   9905 			      const char *name)
   9906 {
   9907   /* Normal local symbols start with ``.L''.  */
   9908   if (name[0] == '.' && name[1] == 'L')
   9909     return true;
   9910 
   9911   /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
   9912      DWARF debugging symbols starting with ``..''.  */
   9913   if (name[0] == '.' && name[1] == '.')
   9914     return true;
   9915 
   9916   /* gcc will sometimes generate symbols beginning with ``_.L_'' when
   9917      emitting DWARF debugging output.  I suspect this is actually a
   9918      small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
   9919      ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
   9920      underscore to be emitted on some ELF targets).  For ease of use,
   9921      we treat such symbols as local.  */
   9922   if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
   9923     return true;
   9924 
   9925   /* Treat assembler generated fake symbols, dollar local labels and
   9926      forward-backward labels (aka local labels) as locals.
   9927      These labels have the form:
   9928 
   9929        L0^A.*				       (fake symbols)
   9930 
   9931        [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
   9932 
   9933      Versions which start with .L will have already been matched above,
   9934      so we only need to match the rest.  */
   9935   if (name[0] == 'L' && ISDIGIT (name[1]))
   9936     {
   9937       bool ret = false;
   9938       const char * p;
   9939       char c;
   9940 
   9941       for (p = name + 2; (c = *p); p++)
   9942 	{
   9943 	  if (c == 1 || c == 2)
   9944 	    {
   9945 	      if (c == 1 && p == name + 2)
   9946 		/* A fake symbol.  */
   9947 		return true;
   9948 
   9949 	      /* FIXME: We are being paranoid here and treating symbols like
   9950 		 L0^Bfoo as if there were non-local, on the grounds that the
   9951 		 assembler will never generate them.  But can any symbol
   9952 		 containing an ASCII value in the range 1-31 ever be anything
   9953 		 other than some kind of local ?  */
   9954 	      ret = true;
   9955 	    }
   9956 
   9957 	  if (! ISDIGIT (c))
   9958 	    {
   9959 	      ret = false;
   9960 	      break;
   9961 	    }
   9962 	}
   9963       return ret;
   9964     }
   9965 
   9966   return false;
   9967 }
   9968 
   9969 alent *
   9970 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
   9971 		     asymbol *symbol ATTRIBUTE_UNUSED)
   9972 {
   9973   abort ();
   9974   return NULL;
   9975 }
   9976 
   9977 bool
   9978 _bfd_elf_set_arch_mach (bfd *abfd,
   9979 			enum bfd_architecture arch,
   9980 			unsigned long machine)
   9981 {
   9982   /* If this isn't the right architecture for this backend, and this
   9983      isn't the generic backend, fail.  */
   9984   if (arch != get_elf_backend_data (abfd)->arch
   9985       && arch != bfd_arch_unknown
   9986       && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
   9987     return false;
   9988 
   9989   return bfd_default_set_arch_mach (abfd, arch, machine);
   9990 }
   9991 
   9992 /* Find the nearest line to a particular section and offset,
   9993    for error reporting.  */
   9994 
   9995 bool
   9996 _bfd_elf_find_nearest_line (bfd *abfd,
   9997 			    asymbol **symbols,
   9998 			    asection *section,
   9999 			    bfd_vma offset,
   10000 			    const char **filename_ptr,
   10001 			    const char **functionname_ptr,
   10002 			    unsigned int *line_ptr,
   10003 			    unsigned int *discriminator_ptr)
   10004 {
   10005   return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
   10006 					      offset, filename_ptr,
   10007 					      functionname_ptr, line_ptr,
   10008 					      discriminator_ptr);
   10009 }
   10010 
   10011 /* Find the nearest line to a particular section and offset,
   10012    for error reporting.  ALT_BFD representing a .gnu_debugaltlink file
   10013    can be optionally specified.  */
   10014 
   10015 bool
   10016 _bfd_elf_find_nearest_line_with_alt (bfd *abfd,
   10017 				     const char *alt_filename,
   10018 				     asymbol **symbols,
   10019 				     asection *section,
   10020 				     bfd_vma offset,
   10021 				     const char **filename_ptr,
   10022 				     const char **functionname_ptr,
   10023 				     unsigned int *line_ptr,
   10024 				     unsigned int *discriminator_ptr)
   10025 {
   10026   bool found;
   10027 
   10028   if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
   10029 					      section, offset, filename_ptr,
   10030 					      functionname_ptr, line_ptr,
   10031 					      discriminator_ptr,
   10032 					      dwarf_debug_sections,
   10033 					      &elf_tdata (abfd)->dwarf2_find_line_info))
   10034     return true;
   10035 
   10036   if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
   10037 				     filename_ptr, functionname_ptr, line_ptr))
   10038     {
   10039       if (!*functionname_ptr)
   10040 	_bfd_elf_find_function (abfd, symbols, section, offset,
   10041 				*filename_ptr ? NULL : filename_ptr,
   10042 				functionname_ptr);
   10043       return true;
   10044     }
   10045 
   10046   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
   10047 					     &found, filename_ptr,
   10048 					     functionname_ptr, line_ptr,
   10049 					     &elf_tdata (abfd)->line_info))
   10050     return false;
   10051   if (found && (*functionname_ptr || *line_ptr))
   10052     return true;
   10053 
   10054   if (symbols == NULL)
   10055     return false;
   10056 
   10057   if (! _bfd_elf_find_function (abfd, symbols, section, offset,
   10058 				filename_ptr, functionname_ptr))
   10059     return false;
   10060 
   10061   *line_ptr = 0;
   10062   return true;
   10063 }
   10064 
   10065 /* Find the line for a symbol.  */
   10066 
   10067 bool
   10068 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
   10069 		    const char **filename_ptr, unsigned int *line_ptr)
   10070 {
   10071   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   10072   return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
   10073 					filename_ptr, NULL, line_ptr, NULL,
   10074 					dwarf_debug_sections,
   10075 					&tdata->dwarf2_find_line_info);
   10076 }
   10077 
   10078 /* After a call to bfd_find_nearest_line, successive calls to
   10079    bfd_find_inliner_info can be used to get source information about
   10080    each level of function inlining that terminated at the address
   10081    passed to bfd_find_nearest_line.  Currently this is only supported
   10082    for DWARF2 with appropriate DWARF3 extensions. */
   10083 
   10084 bool
   10085 _bfd_elf_find_inliner_info (bfd *abfd,
   10086 			    const char **filename_ptr,
   10087 			    const char **functionname_ptr,
   10088 			    unsigned int *line_ptr)
   10089 {
   10090   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   10091   return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
   10092 					functionname_ptr, line_ptr,
   10093 					&tdata->dwarf2_find_line_info);
   10094 }
   10095 
   10096 int
   10097 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
   10098 {
   10099   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   10100   int ret = bed->s->sizeof_ehdr;
   10101 
   10102   if (!bfd_link_relocatable (info))
   10103     {
   10104       bfd_size_type phdr_size = elf_program_header_size (abfd);
   10105 
   10106       if (phdr_size == (bfd_size_type) -1)
   10107 	{
   10108 	  struct elf_segment_map *m;
   10109 
   10110 	  phdr_size = 0;
   10111 	  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   10112 	    phdr_size += bed->s->sizeof_phdr;
   10113 
   10114 	  if (phdr_size == 0)
   10115 	    phdr_size = get_program_header_size (abfd, info);
   10116 	}
   10117 
   10118       elf_program_header_size (abfd) = phdr_size;
   10119       ret += phdr_size;
   10120     }
   10121 
   10122   return ret;
   10123 }
   10124 
   10125 bool
   10126 _bfd_elf_set_section_contents (bfd *abfd,
   10127 			       sec_ptr section,
   10128 			       const void *location,
   10129 			       file_ptr offset,
   10130 			       bfd_size_type count)
   10131 {
   10132   Elf_Internal_Shdr *hdr;
   10133 
   10134   if (! abfd->output_has_begun
   10135       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   10136     return false;
   10137 
   10138   if (!count)
   10139     return true;
   10140 
   10141   hdr = &elf_section_data (section)->this_hdr;
   10142   if (hdr->sh_offset == (file_ptr) -1)
   10143     {
   10144       unsigned char *contents;
   10145 
   10146       if (bfd_section_is_ctf (section))
   10147 	/* Nothing to do with this section: the contents are generated
   10148 	   later.  */
   10149 	return true;
   10150 
   10151       if ((offset + count) > hdr->sh_size)
   10152 	{
   10153 	  _bfd_error_handler
   10154 	    (_("%pB:%pA: error: attempting to write"
   10155 	       " over the end of the section"),
   10156 	     abfd, section);
   10157 
   10158 	  bfd_set_error (bfd_error_invalid_operation);
   10159 	  return false;
   10160 	}
   10161 
   10162       contents = hdr->contents;
   10163       if (contents == NULL)
   10164 	{
   10165 	  _bfd_error_handler
   10166 	    (_("%pB:%pA: error: attempting to write"
   10167 	       " section into an empty buffer"),
   10168 	     abfd, section);
   10169 
   10170 	  bfd_set_error (bfd_error_invalid_operation);
   10171 	  return false;
   10172 	}
   10173 
   10174       memcpy (contents + offset, location, count);
   10175       return true;
   10176     }
   10177 
   10178   return _bfd_generic_set_section_contents (abfd, section,
   10179 					    location, offset, count);
   10180 }
   10181 
   10182 bool
   10183 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
   10184 			   arelent *cache_ptr ATTRIBUTE_UNUSED,
   10185 			   Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
   10186 {
   10187   abort ();
   10188   return false;
   10189 }
   10190 
   10191 /* Try to convert a non-ELF reloc into an ELF one.  */
   10192 
   10193 bool
   10194 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
   10195 {
   10196   /* Check whether we really have an ELF howto.  */
   10197 
   10198   if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
   10199     {
   10200       bfd_reloc_code_real_type code;
   10201       reloc_howto_type *howto;
   10202 
   10203       /* Alien reloc: Try to determine its type to replace it with an
   10204 	 equivalent ELF reloc.  */
   10205 
   10206       if (areloc->howto->pc_relative)
   10207 	{
   10208 	  switch (areloc->howto->bitsize)
   10209 	    {
   10210 	    case 8:
   10211 	      code = BFD_RELOC_8_PCREL;
   10212 	      break;
   10213 	    case 12:
   10214 	      code = BFD_RELOC_12_PCREL;
   10215 	      break;
   10216 	    case 16:
   10217 	      code = BFD_RELOC_16_PCREL;
   10218 	      break;
   10219 	    case 24:
   10220 	      code = BFD_RELOC_24_PCREL;
   10221 	      break;
   10222 	    case 32:
   10223 	      code = BFD_RELOC_32_PCREL;
   10224 	      break;
   10225 	    case 64:
   10226 	      code = BFD_RELOC_64_PCREL;
   10227 	      break;
   10228 	    default:
   10229 	      goto fail;
   10230 	    }
   10231 
   10232 	  howto = bfd_reloc_type_lookup (abfd, code);
   10233 
   10234 	  if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
   10235 	    {
   10236 	      if (howto->pcrel_offset)
   10237 		areloc->addend += areloc->address;
   10238 	      else
   10239 		areloc->addend -= areloc->address; /* addend is unsigned!! */
   10240 	    }
   10241 	}
   10242       else
   10243 	{
   10244 	  switch (areloc->howto->bitsize)
   10245 	    {
   10246 	    case 8:
   10247 	      code = BFD_RELOC_8;
   10248 	      break;
   10249 	    case 14:
   10250 	      code = BFD_RELOC_14;
   10251 	      break;
   10252 	    case 16:
   10253 	      code = BFD_RELOC_16;
   10254 	      break;
   10255 	    case 26:
   10256 	      code = BFD_RELOC_26;
   10257 	      break;
   10258 	    case 32:
   10259 	      code = BFD_RELOC_32;
   10260 	      break;
   10261 	    case 64:
   10262 	      code = BFD_RELOC_64;
   10263 	      break;
   10264 	    default:
   10265 	      goto fail;
   10266 	    }
   10267 
   10268 	  howto = bfd_reloc_type_lookup (abfd, code);
   10269 	}
   10270 
   10271       if (howto)
   10272 	areloc->howto = howto;
   10273       else
   10274 	goto fail;
   10275     }
   10276 
   10277   return true;
   10278 
   10279  fail:
   10280   /* xgettext:c-format */
   10281   _bfd_error_handler (_("%pB: %s unsupported"),
   10282 		      abfd, areloc->howto->name);
   10283   bfd_set_error (bfd_error_sorry);
   10284   return false;
   10285 }
   10286 
   10287 bool
   10288 _bfd_elf_free_cached_info (bfd *abfd)
   10289 {
   10290   struct elf_obj_tdata *tdata;
   10291 
   10292   if ((bfd_get_format (abfd) == bfd_object
   10293        || bfd_get_format (abfd) == bfd_core)
   10294       && (tdata = elf_tdata (abfd)) != NULL)
   10295     {
   10296       if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
   10297 	_bfd_elf_strtab_free (elf_shstrtab (abfd));
   10298       _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
   10299       _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
   10300       _bfd_stab_cleanup (abfd, &tdata->line_info);
   10301     }
   10302 
   10303   return _bfd_generic_bfd_free_cached_info (abfd);
   10304 }
   10305 
   10306 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
   10307    in the relocation's offset.  Thus we cannot allow any sort of sanity
   10308    range-checking to interfere.  There is nothing else to do in processing
   10309    this reloc.  */
   10310 
   10311 bfd_reloc_status_type
   10312 _bfd_elf_rel_vtable_reloc_fn
   10313   (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
   10314    struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
   10315    void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
   10316    bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
   10317 {
   10318   return bfd_reloc_ok;
   10319 }
   10320 
   10321 /* Elf core file support.  Much of this only works on native
   10323    toolchains, since we rely on knowing the
   10324    machine-dependent procfs structure in order to pick
   10325    out details about the corefile.  */
   10326 
   10327 #ifdef HAVE_SYS_PROCFS_H
   10328 # include <sys/procfs.h>
   10329 #endif
   10330 
   10331 /* Return a PID that identifies a "thread" for threaded cores, or the
   10332    PID of the main process for non-threaded cores.  */
   10333 
   10334 static int
   10335 elfcore_make_pid (bfd *abfd)
   10336 {
   10337   int pid;
   10338 
   10339   pid = elf_tdata (abfd)->core->lwpid;
   10340   if (pid == 0)
   10341     pid = elf_tdata (abfd)->core->pid;
   10342 
   10343   return pid;
   10344 }
   10345 
   10346 /* If there isn't a section called NAME, make one, using data from
   10347    SECT.  Note, this function will generate a reference to NAME, so
   10348    you shouldn't deallocate or overwrite it.  */
   10349 
   10350 static bool
   10351 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
   10352 {
   10353   asection *sect2;
   10354 
   10355   if (bfd_get_section_by_name (abfd, name) != NULL)
   10356     return true;
   10357 
   10358   sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
   10359   if (sect2 == NULL)
   10360     return false;
   10361 
   10362   sect2->size = sect->size;
   10363   sect2->filepos = sect->filepos;
   10364   sect2->alignment_power = sect->alignment_power;
   10365   return true;
   10366 }
   10367 
   10368 /* Create a pseudosection containing SIZE bytes at FILEPOS.  This
   10369    actually creates up to two pseudosections:
   10370    - For the single-threaded case, a section named NAME, unless
   10371      such a section already exists.
   10372    - For the multi-threaded case, a section named "NAME/PID", where
   10373      PID is elfcore_make_pid (abfd).
   10374    Both pseudosections have identical contents.  */
   10375 bool
   10376 _bfd_elfcore_make_pseudosection (bfd *abfd,
   10377 				 char *name,
   10378 				 size_t size,
   10379 				 ufile_ptr filepos)
   10380 {
   10381   char buf[100];
   10382   char *threaded_name;
   10383   size_t len;
   10384   asection *sect;
   10385 
   10386   /* Build the section name.  */
   10387 
   10388   sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
   10389   len = strlen (buf) + 1;
   10390   threaded_name = (char *) bfd_alloc (abfd, len);
   10391   if (threaded_name == NULL)
   10392     return false;
   10393   memcpy (threaded_name, buf, len);
   10394 
   10395   sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
   10396 					     SEC_HAS_CONTENTS);
   10397   if (sect == NULL)
   10398     return false;
   10399   sect->size = size;
   10400   sect->filepos = filepos;
   10401   sect->alignment_power = 2;
   10402 
   10403   return elfcore_maybe_make_sect (abfd, name, sect);
   10404 }
   10405 
   10406 static bool
   10407 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
   10408 				size_t offs)
   10409 {
   10410   asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
   10411 						       SEC_HAS_CONTENTS);
   10412 
   10413   if (sect == NULL)
   10414     return false;
   10415 
   10416   sect->size = note->descsz - offs;
   10417   sect->filepos = note->descpos + offs;
   10418   sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   10419 
   10420   return true;
   10421 }
   10422 
   10423 /* prstatus_t exists on:
   10424      solaris 2.5+
   10425      linux 2.[01] + glibc
   10426      unixware 4.2
   10427 */
   10428 
   10429 #if defined (HAVE_PRSTATUS_T)
   10430 
   10431 static bool
   10432 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   10433 {
   10434   size_t size;
   10435   int offset;
   10436 
   10437   if (note->descsz == sizeof (prstatus_t))
   10438     {
   10439       prstatus_t prstat;
   10440 
   10441       size = sizeof (prstat.pr_reg);
   10442       offset   = offsetof (prstatus_t, pr_reg);
   10443       memcpy (&prstat, note->descdata, sizeof (prstat));
   10444 
   10445       /* Do not overwrite the core signal if it
   10446 	 has already been set by another thread.  */
   10447       if (elf_tdata (abfd)->core->signal == 0)
   10448 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   10449       if (elf_tdata (abfd)->core->pid == 0)
   10450 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   10451 
   10452       /* pr_who exists on:
   10453 	 solaris 2.5+
   10454 	 unixware 4.2
   10455 	 pr_who doesn't exist on:
   10456 	 linux 2.[01]
   10457 	 */
   10458 #if defined (HAVE_PRSTATUS_T_PR_WHO)
   10459       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   10460 #else
   10461       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   10462 #endif
   10463     }
   10464 #if defined (HAVE_PRSTATUS32_T)
   10465   else if (note->descsz == sizeof (prstatus32_t))
   10466     {
   10467       /* 64-bit host, 32-bit corefile */
   10468       prstatus32_t prstat;
   10469 
   10470       size = sizeof (prstat.pr_reg);
   10471       offset   = offsetof (prstatus32_t, pr_reg);
   10472       memcpy (&prstat, note->descdata, sizeof (prstat));
   10473 
   10474       /* Do not overwrite the core signal if it
   10475 	 has already been set by another thread.  */
   10476       if (elf_tdata (abfd)->core->signal == 0)
   10477 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   10478       if (elf_tdata (abfd)->core->pid == 0)
   10479 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   10480 
   10481       /* pr_who exists on:
   10482 	 solaris 2.5+
   10483 	 unixware 4.2
   10484 	 pr_who doesn't exist on:
   10485 	 linux 2.[01]
   10486 	 */
   10487 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
   10488       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   10489 #else
   10490       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   10491 #endif
   10492     }
   10493 #endif /* HAVE_PRSTATUS32_T */
   10494   else
   10495     {
   10496       /* Fail - we don't know how to handle any other
   10497 	 note size (ie. data object type).  */
   10498       return true;
   10499     }
   10500 
   10501   /* Make a ".reg/999" section and a ".reg" section.  */
   10502   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   10503 					  size, note->descpos + offset);
   10504 }
   10505 #endif /* defined (HAVE_PRSTATUS_T) */
   10506 
   10507 /* Create a pseudosection containing the exact contents of NOTE.  */
   10508 static bool
   10509 elfcore_make_note_pseudosection (bfd *abfd,
   10510 				 char *name,
   10511 				 Elf_Internal_Note *note)
   10512 {
   10513   return _bfd_elfcore_make_pseudosection (abfd, name,
   10514 					  note->descsz, note->descpos);
   10515 }
   10516 
   10517 /* There isn't a consistent prfpregset_t across platforms,
   10518    but it doesn't matter, because we don't have to pick this
   10519    data structure apart.  */
   10520 
   10521 static bool
   10522 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
   10523 {
   10524   return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   10525 }
   10526 
   10527 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
   10528    type of NT_PRXFPREG.  Just include the whole note's contents
   10529    literally.  */
   10530 
   10531 static bool
   10532 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
   10533 {
   10534   return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
   10535 }
   10536 
   10537 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
   10538    with a note type of NT_X86_XSTATE.  Just include the whole note's
   10539    contents literally.  */
   10540 
   10541 static bool
   10542 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
   10543 {
   10544   return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
   10545 }
   10546 
   10547 static bool
   10548 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
   10549 {
   10550   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
   10551 }
   10552 
   10553 static bool
   10554 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
   10555 {
   10556   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
   10557 }
   10558 
   10559 static bool
   10560 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
   10561 {
   10562   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
   10563 }
   10564 
   10565 static bool
   10566 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
   10567 {
   10568   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
   10569 }
   10570 
   10571 static bool
   10572 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
   10573 {
   10574   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
   10575 }
   10576 
   10577 static bool
   10578 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
   10579 {
   10580   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
   10581 }
   10582 
   10583 static bool
   10584 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
   10585 {
   10586   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
   10587 }
   10588 
   10589 static bool
   10590 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
   10591 {
   10592   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
   10593 }
   10594 
   10595 static bool
   10596 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
   10597 {
   10598   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
   10599 }
   10600 
   10601 static bool
   10602 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
   10603 {
   10604   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
   10605 }
   10606 
   10607 static bool
   10608 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
   10609 {
   10610   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
   10611 }
   10612 
   10613 static bool
   10614 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
   10615 {
   10616   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
   10617 }
   10618 
   10619 static bool
   10620 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
   10621 {
   10622   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
   10623 }
   10624 
   10625 static bool
   10626 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
   10627 {
   10628   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
   10629 }
   10630 
   10631 static bool
   10632 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
   10633 {
   10634   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
   10635 }
   10636 
   10637 static bool
   10638 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
   10639 {
   10640   return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
   10641 }
   10642 
   10643 static bool
   10644 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
   10645 {
   10646   return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
   10647 }
   10648 
   10649 static bool
   10650 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
   10651 {
   10652   return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
   10653 }
   10654 
   10655 static bool
   10656 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
   10657 {
   10658   return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
   10659 }
   10660 
   10661 static bool
   10662 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
   10663 {
   10664   return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
   10665 }
   10666 
   10667 static bool
   10668 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
   10669 {
   10670   return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
   10671 }
   10672 
   10673 static bool
   10674 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
   10675 {
   10676   return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
   10677 }
   10678 
   10679 static bool
   10680 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
   10681 {
   10682   return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
   10683 }
   10684 
   10685 static bool
   10686 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
   10687 {
   10688   return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
   10689 }
   10690 
   10691 static bool
   10692 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
   10693 {
   10694   return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
   10695 }
   10696 
   10697 static bool
   10698 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
   10699 {
   10700   return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
   10701 }
   10702 
   10703 static bool
   10704 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
   10705 {
   10706   return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
   10707 }
   10708 
   10709 static bool
   10710 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
   10711 {
   10712   return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
   10713 }
   10714 
   10715 static bool
   10716 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
   10717 {
   10718   return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
   10719 }
   10720 
   10721 static bool
   10722 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
   10723 {
   10724   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
   10725 }
   10726 
   10727 static bool
   10728 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
   10729 {
   10730   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
   10731 }
   10732 
   10733 static bool
   10734 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
   10735 {
   10736   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
   10737 }
   10738 
   10739 static bool
   10740 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
   10741 {
   10742   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
   10743 }
   10744 
   10745 static bool
   10746 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
   10747 {
   10748   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
   10749 }
   10750 
   10751 static bool
   10752 elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
   10753 {
   10754   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
   10755 					  note);
   10756 }
   10757 
   10758 static bool
   10759 elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note)
   10760 {
   10761   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-ssve", note);
   10762 }
   10763 
   10764 static bool
   10765 elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note)
   10766 {
   10767   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-za", note);
   10768 }
   10769 
   10770 /* Convert NOTE into a bfd_section called ".reg-aarch-zt".  Return TRUE if
   10771    successful, otherwise return FALSE.  */
   10772 
   10773 static bool
   10774 elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note)
   10775 {
   10776   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-zt", note);
   10777 }
   10778 
   10779 static bool
   10780 elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
   10781 {
   10782   return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
   10783 }
   10784 
   10785 /* Convert NOTE into a bfd_section called ".reg-riscv-csr".  Return TRUE if
   10786    successful otherwise, return FALSE.  */
   10787 
   10788 static bool
   10789 elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
   10790 {
   10791   return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
   10792 }
   10793 
   10794 /* Convert NOTE into a bfd_section called ".gdb-tdesc".  Return TRUE if
   10795    successful otherwise, return FALSE.  */
   10796 
   10797 static bool
   10798 elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
   10799 {
   10800   return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
   10801 }
   10802 
   10803 static bool
   10804 elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
   10805 {
   10806   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
   10807 }
   10808 
   10809 static bool
   10810 elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
   10811 {
   10812   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
   10813 }
   10814 
   10815 static bool
   10816 elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
   10817 {
   10818   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
   10819 }
   10820 
   10821 static bool
   10822 elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
   10823 {
   10824   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
   10825 }
   10826 
   10827 #if defined (HAVE_PRPSINFO_T)
   10828 typedef prpsinfo_t   elfcore_psinfo_t;
   10829 #if defined (HAVE_PRPSINFO32_T)		/* Sparc64 cross Sparc32 */
   10830 typedef prpsinfo32_t elfcore_psinfo32_t;
   10831 #endif
   10832 #endif
   10833 
   10834 #if defined (HAVE_PSINFO_T)
   10835 typedef psinfo_t   elfcore_psinfo_t;
   10836 #if defined (HAVE_PSINFO32_T)		/* Sparc64 cross Sparc32 */
   10837 typedef psinfo32_t elfcore_psinfo32_t;
   10838 #endif
   10839 #endif
   10840 
   10841 /* return a malloc'ed copy of a string at START which is at
   10842    most MAX bytes long, possibly without a terminating '\0'.
   10843    the copy will always have a terminating '\0'.  */
   10844 
   10845 char *
   10846 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
   10847 {
   10848   char *dups;
   10849   char *end = (char *) memchr (start, '\0', max);
   10850   size_t len;
   10851 
   10852   if (end == NULL)
   10853     len = max;
   10854   else
   10855     len = end - start;
   10856 
   10857   dups = (char *) bfd_alloc (abfd, len + 1);
   10858   if (dups == NULL)
   10859     return NULL;
   10860 
   10861   memcpy (dups, start, len);
   10862   dups[len] = '\0';
   10863 
   10864   return dups;
   10865 }
   10866 
   10867 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   10868 static bool
   10869 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   10870 {
   10871   if (note->descsz == sizeof (elfcore_psinfo_t))
   10872     {
   10873       elfcore_psinfo_t psinfo;
   10874 
   10875       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   10876 
   10877 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
   10878       elf_tdata (abfd)->core->pid = psinfo.pr_pid;
   10879 #endif
   10880       elf_tdata (abfd)->core->program
   10881 	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
   10882 				sizeof (psinfo.pr_fname));
   10883 
   10884       elf_tdata (abfd)->core->command
   10885 	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
   10886 				sizeof (psinfo.pr_psargs));
   10887     }
   10888 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   10889   else if (note->descsz == sizeof (elfcore_psinfo32_t))
   10890     {
   10891       /* 64-bit host, 32-bit corefile */
   10892       elfcore_psinfo32_t psinfo;
   10893 
   10894       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   10895 
   10896 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
   10897       elf_tdata (abfd)->core->pid = psinfo.pr_pid;
   10898 #endif
   10899       elf_tdata (abfd)->core->program
   10900 	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
   10901 				sizeof (psinfo.pr_fname));
   10902 
   10903       elf_tdata (abfd)->core->command
   10904 	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
   10905 				sizeof (psinfo.pr_psargs));
   10906     }
   10907 #endif
   10908 
   10909   else
   10910     {
   10911       /* Fail - we don't know how to handle any other
   10912 	 note size (ie. data object type).  */
   10913       return true;
   10914     }
   10915 
   10916   /* Note that for some reason, a spurious space is tacked
   10917      onto the end of the args in some (at least one anyway)
   10918      implementations, so strip it off if it exists.  */
   10919 
   10920   {
   10921     char *command = elf_tdata (abfd)->core->command;
   10922     int n = strlen (command);
   10923 
   10924     if (0 < n && command[n - 1] == ' ')
   10925       command[n - 1] = '\0';
   10926   }
   10927 
   10928   return true;
   10929 }
   10930 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
   10931 
   10932 #if defined (HAVE_PSTATUS_T)
   10933 static bool
   10934 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
   10935 {
   10936   if (note->descsz == sizeof (pstatus_t)
   10937 #if defined (HAVE_PXSTATUS_T)
   10938       || note->descsz == sizeof (pxstatus_t)
   10939 #endif
   10940       )
   10941     {
   10942       pstatus_t pstat;
   10943 
   10944       memcpy (&pstat, note->descdata, sizeof (pstat));
   10945 
   10946       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   10947     }
   10948 #if defined (HAVE_PSTATUS32_T)
   10949   else if (note->descsz == sizeof (pstatus32_t))
   10950     {
   10951       /* 64-bit host, 32-bit corefile */
   10952       pstatus32_t pstat;
   10953 
   10954       memcpy (&pstat, note->descdata, sizeof (pstat));
   10955 
   10956       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   10957     }
   10958 #endif
   10959   /* Could grab some more details from the "representative"
   10960      lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
   10961      NT_LWPSTATUS note, presumably.  */
   10962 
   10963   return true;
   10964 }
   10965 #endif /* defined (HAVE_PSTATUS_T) */
   10966 
   10967 #if defined (HAVE_LWPSTATUS_T)
   10968 static bool
   10969 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
   10970 {
   10971   lwpstatus_t lwpstat;
   10972   char buf[100];
   10973   char *name;
   10974   size_t len;
   10975   asection *sect;
   10976 
   10977   if (note->descsz != sizeof (lwpstat)
   10978 #if defined (HAVE_LWPXSTATUS_T)
   10979       && note->descsz != sizeof (lwpxstatus_t)
   10980 #endif
   10981       )
   10982     return true;
   10983 
   10984   memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
   10985 
   10986   elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
   10987   /* Do not overwrite the core signal if it has already been set by
   10988      another thread.  */
   10989   if (elf_tdata (abfd)->core->signal == 0)
   10990     elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
   10991 
   10992   /* Make a ".reg/999" section.  */
   10993 
   10994   sprintf (buf, ".reg/%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.gregs);
   11007   sect->filepos = note->descpos
   11008     + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
   11009 #endif
   11010 
   11011 #if defined (HAVE_LWPSTATUS_T_PR_REG)
   11012   sect->size = sizeof (lwpstat.pr_reg);
   11013   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
   11014 #endif
   11015 
   11016   sect->alignment_power = 2;
   11017 
   11018   if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
   11019     return false;
   11020 
   11021   /* Make a ".reg2/999" section */
   11022 
   11023   sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
   11024   len = strlen (buf) + 1;
   11025   name = bfd_alloc (abfd, len);
   11026   if (name == NULL)
   11027     return false;
   11028   memcpy (name, buf, len);
   11029 
   11030   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11031   if (sect == NULL)
   11032     return false;
   11033 
   11034 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   11035   sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
   11036   sect->filepos = note->descpos
   11037     + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
   11038 #endif
   11039 
   11040 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
   11041   sect->size = sizeof (lwpstat.pr_fpreg);
   11042   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
   11043 #endif
   11044 
   11045   sect->alignment_power = 2;
   11046 
   11047   return elfcore_maybe_make_sect (abfd, ".reg2", sect);
   11048 }
   11049 #endif /* defined (HAVE_LWPSTATUS_T) */
   11050 
   11051 /* These constants, and the structure offsets used below, are defined by
   11052    Cygwin's core_dump.h */
   11053 #define NOTE_INFO_PROCESS  1
   11054 #define NOTE_INFO_THREAD   2
   11055 #define NOTE_INFO_MODULE   3
   11056 #define NOTE_INFO_MODULE64 4
   11057 
   11058 static bool
   11059 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
   11060 {
   11061   char buf[30];
   11062   char *name;
   11063   size_t len;
   11064   unsigned int name_size;
   11065   asection *sect;
   11066   unsigned int type;
   11067   int is_active_thread;
   11068   bfd_vma base_addr;
   11069 
   11070   if (note->descsz < 4)
   11071     return true;
   11072 
   11073   if (! startswith (note->namedata, "win32"))
   11074     return true;
   11075 
   11076   type = bfd_get_32 (abfd, note->descdata);
   11077 
   11078   struct
   11079   {
   11080     const char *type_name;
   11081     unsigned long min_size;
   11082   } size_check[] =
   11083       {
   11084        { "NOTE_INFO_PROCESS", 12 },
   11085        { "NOTE_INFO_THREAD", 12 },
   11086        { "NOTE_INFO_MODULE", 12 },
   11087        { "NOTE_INFO_MODULE64", 16 },
   11088       };
   11089 
   11090   if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
   11091       return true;
   11092 
   11093   if (note->descsz < size_check[type - 1].min_size)
   11094     {
   11095       _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
   11096 			    " is too small"),
   11097 			  abfd, size_check[type - 1].type_name, note->descsz);
   11098       return true;
   11099     }
   11100 
   11101   switch (type)
   11102     {
   11103     case NOTE_INFO_PROCESS:
   11104       /* FIXME: need to add ->core->command.  */
   11105       elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
   11106       elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
   11107       break;
   11108 
   11109     case NOTE_INFO_THREAD:
   11110       /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
   11111 	 structure. */
   11112       /* thread_info.tid */
   11113       sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
   11114 
   11115       len = strlen (buf) + 1;
   11116       name = (char *) bfd_alloc (abfd, len);
   11117       if (name == NULL)
   11118 	return false;
   11119 
   11120       memcpy (name, buf, len);
   11121 
   11122       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11123       if (sect == NULL)
   11124 	return false;
   11125 
   11126       /* sizeof (thread_info.thread_context) */
   11127       sect->size = note->descsz - 12;
   11128       /* offsetof (thread_info.thread_context) */
   11129       sect->filepos = note->descpos + 12;
   11130       sect->alignment_power = 2;
   11131 
   11132       /* thread_info.is_active_thread */
   11133       is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
   11134 
   11135       if (is_active_thread)
   11136 	if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
   11137 	  return false;
   11138       break;
   11139 
   11140     case NOTE_INFO_MODULE:
   11141     case NOTE_INFO_MODULE64:
   11142       /* Make a ".module/xxxxxxxx" section.  */
   11143       if (type == NOTE_INFO_MODULE)
   11144 	{
   11145 	  /* module_info.base_address */
   11146 	  base_addr = bfd_get_32 (abfd, note->descdata + 4);
   11147 	  sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
   11148 	  /* module_info.module_name_size */
   11149 	  name_size = bfd_get_32 (abfd, note->descdata + 8);
   11150 	}
   11151       else /* NOTE_INFO_MODULE64 */
   11152 	{
   11153 	  /* module_info.base_address */
   11154 	  base_addr = bfd_get_64 (abfd, note->descdata + 4);
   11155 	  sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
   11156 	  /* module_info.module_name_size */
   11157 	  name_size = bfd_get_32 (abfd, note->descdata + 12);
   11158 	}
   11159 
   11160       len = strlen (buf) + 1;
   11161       name = (char *) bfd_alloc (abfd, len);
   11162       if (name == NULL)
   11163 	return false;
   11164 
   11165       memcpy (name, buf, len);
   11166 
   11167       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11168 
   11169       if (sect == NULL)
   11170 	return false;
   11171 
   11172       if (note->descsz < 12 + name_size)
   11173 	{
   11174 	  _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
   11175 				" is too small to contain a name of size %u"),
   11176 			      abfd, note->descsz, name_size);
   11177 	  return true;
   11178 	}
   11179 
   11180       sect->size = note->descsz;
   11181       sect->filepos = note->descpos;
   11182       sect->alignment_power = 2;
   11183       break;
   11184 
   11185     default:
   11186       return true;
   11187     }
   11188 
   11189   return true;
   11190 }
   11191 
   11192 static bool
   11193 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
   11194 {
   11195   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11196 
   11197   switch (note->type)
   11198     {
   11199     default:
   11200       return true;
   11201 
   11202     case NT_PRSTATUS:
   11203       if (bed->elf_backend_grok_prstatus)
   11204 	if ((*bed->elf_backend_grok_prstatus) (abfd, note))
   11205 	  return true;
   11206 #if defined (HAVE_PRSTATUS_T)
   11207       return elfcore_grok_prstatus (abfd, note);
   11208 #else
   11209       return true;
   11210 #endif
   11211 
   11212 #if defined (HAVE_PSTATUS_T)
   11213     case NT_PSTATUS:
   11214       return elfcore_grok_pstatus (abfd, note);
   11215 #endif
   11216 
   11217 #if defined (HAVE_LWPSTATUS_T)
   11218     case NT_LWPSTATUS:
   11219       return elfcore_grok_lwpstatus (abfd, note);
   11220 #endif
   11221 
   11222     case NT_FPREGSET:		/* FIXME: rename to NT_PRFPREG */
   11223       return elfcore_grok_prfpreg (abfd, note);
   11224 
   11225     case NT_WIN32PSTATUS:
   11226       return elfcore_grok_win32pstatus (abfd, note);
   11227 
   11228     case NT_PRXFPREG:		/* Linux SSE extension */
   11229       if (note->namesz == 6
   11230 	  && strcmp (note->namedata, "LINUX") == 0)
   11231 	return elfcore_grok_prxfpreg (abfd, note);
   11232       else
   11233 	return true;
   11234 
   11235     case NT_X86_XSTATE:		/* Linux XSAVE extension */
   11236       if (note->namesz == 6
   11237 	  && strcmp (note->namedata, "LINUX") == 0)
   11238 	return elfcore_grok_xstatereg (abfd, note);
   11239       else
   11240 	return true;
   11241 
   11242     case NT_PPC_VMX:
   11243       if (note->namesz == 6
   11244 	  && strcmp (note->namedata, "LINUX") == 0)
   11245 	return elfcore_grok_ppc_vmx (abfd, note);
   11246       else
   11247 	return true;
   11248 
   11249     case NT_PPC_VSX:
   11250       if (note->namesz == 6
   11251 	  && strcmp (note->namedata, "LINUX") == 0)
   11252 	return elfcore_grok_ppc_vsx (abfd, note);
   11253       else
   11254 	return true;
   11255 
   11256     case NT_PPC_TAR:
   11257       if (note->namesz == 6
   11258 	  && strcmp (note->namedata, "LINUX") == 0)
   11259 	return elfcore_grok_ppc_tar (abfd, note);
   11260       else
   11261 	return true;
   11262 
   11263     case NT_PPC_PPR:
   11264       if (note->namesz == 6
   11265 	  && strcmp (note->namedata, "LINUX") == 0)
   11266 	return elfcore_grok_ppc_ppr (abfd, note);
   11267       else
   11268 	return true;
   11269 
   11270     case NT_PPC_DSCR:
   11271       if (note->namesz == 6
   11272 	  && strcmp (note->namedata, "LINUX") == 0)
   11273 	return elfcore_grok_ppc_dscr (abfd, note);
   11274       else
   11275 	return true;
   11276 
   11277     case NT_PPC_EBB:
   11278       if (note->namesz == 6
   11279 	  && strcmp (note->namedata, "LINUX") == 0)
   11280 	return elfcore_grok_ppc_ebb (abfd, note);
   11281       else
   11282 	return true;
   11283 
   11284     case NT_PPC_PMU:
   11285       if (note->namesz == 6
   11286 	  && strcmp (note->namedata, "LINUX") == 0)
   11287 	return elfcore_grok_ppc_pmu (abfd, note);
   11288       else
   11289 	return true;
   11290 
   11291     case NT_PPC_TM_CGPR:
   11292       if (note->namesz == 6
   11293 	  && strcmp (note->namedata, "LINUX") == 0)
   11294 	return elfcore_grok_ppc_tm_cgpr (abfd, note);
   11295       else
   11296 	return true;
   11297 
   11298     case NT_PPC_TM_CFPR:
   11299       if (note->namesz == 6
   11300 	  && strcmp (note->namedata, "LINUX") == 0)
   11301 	return elfcore_grok_ppc_tm_cfpr (abfd, note);
   11302       else
   11303 	return true;
   11304 
   11305     case NT_PPC_TM_CVMX:
   11306       if (note->namesz == 6
   11307 	  && strcmp (note->namedata, "LINUX") == 0)
   11308 	return elfcore_grok_ppc_tm_cvmx (abfd, note);
   11309       else
   11310 	return true;
   11311 
   11312     case NT_PPC_TM_CVSX:
   11313       if (note->namesz == 6
   11314 	  && strcmp (note->namedata, "LINUX") == 0)
   11315 	return elfcore_grok_ppc_tm_cvsx (abfd, note);
   11316       else
   11317 	return true;
   11318 
   11319     case NT_PPC_TM_SPR:
   11320       if (note->namesz == 6
   11321 	  && strcmp (note->namedata, "LINUX") == 0)
   11322 	return elfcore_grok_ppc_tm_spr (abfd, note);
   11323       else
   11324 	return true;
   11325 
   11326     case NT_PPC_TM_CTAR:
   11327       if (note->namesz == 6
   11328 	  && strcmp (note->namedata, "LINUX") == 0)
   11329 	return elfcore_grok_ppc_tm_ctar (abfd, note);
   11330       else
   11331 	return true;
   11332 
   11333     case NT_PPC_TM_CPPR:
   11334       if (note->namesz == 6
   11335 	  && strcmp (note->namedata, "LINUX") == 0)
   11336 	return elfcore_grok_ppc_tm_cppr (abfd, note);
   11337       else
   11338 	return true;
   11339 
   11340     case NT_PPC_TM_CDSCR:
   11341       if (note->namesz == 6
   11342 	  && strcmp (note->namedata, "LINUX") == 0)
   11343 	return elfcore_grok_ppc_tm_cdscr (abfd, note);
   11344       else
   11345 	return true;
   11346 
   11347     case NT_S390_HIGH_GPRS:
   11348       if (note->namesz == 6
   11349 	  && strcmp (note->namedata, "LINUX") == 0)
   11350 	return elfcore_grok_s390_high_gprs (abfd, note);
   11351       else
   11352 	return true;
   11353 
   11354     case NT_S390_TIMER:
   11355       if (note->namesz == 6
   11356 	  && strcmp (note->namedata, "LINUX") == 0)
   11357 	return elfcore_grok_s390_timer (abfd, note);
   11358       else
   11359 	return true;
   11360 
   11361     case NT_S390_TODCMP:
   11362       if (note->namesz == 6
   11363 	  && strcmp (note->namedata, "LINUX") == 0)
   11364 	return elfcore_grok_s390_todcmp (abfd, note);
   11365       else
   11366 	return true;
   11367 
   11368     case NT_S390_TODPREG:
   11369       if (note->namesz == 6
   11370 	  && strcmp (note->namedata, "LINUX") == 0)
   11371 	return elfcore_grok_s390_todpreg (abfd, note);
   11372       else
   11373 	return true;
   11374 
   11375     case NT_S390_CTRS:
   11376       if (note->namesz == 6
   11377 	  && strcmp (note->namedata, "LINUX") == 0)
   11378 	return elfcore_grok_s390_ctrs (abfd, note);
   11379       else
   11380 	return true;
   11381 
   11382     case NT_S390_PREFIX:
   11383       if (note->namesz == 6
   11384 	  && strcmp (note->namedata, "LINUX") == 0)
   11385 	return elfcore_grok_s390_prefix (abfd, note);
   11386       else
   11387 	return true;
   11388 
   11389     case NT_S390_LAST_BREAK:
   11390       if (note->namesz == 6
   11391 	  && strcmp (note->namedata, "LINUX") == 0)
   11392 	return elfcore_grok_s390_last_break (abfd, note);
   11393       else
   11394 	return true;
   11395 
   11396     case NT_S390_SYSTEM_CALL:
   11397       if (note->namesz == 6
   11398 	  && strcmp (note->namedata, "LINUX") == 0)
   11399 	return elfcore_grok_s390_system_call (abfd, note);
   11400       else
   11401 	return true;
   11402 
   11403     case NT_S390_TDB:
   11404       if (note->namesz == 6
   11405 	  && strcmp (note->namedata, "LINUX") == 0)
   11406 	return elfcore_grok_s390_tdb (abfd, note);
   11407       else
   11408 	return true;
   11409 
   11410     case NT_S390_VXRS_LOW:
   11411       if (note->namesz == 6
   11412 	  && strcmp (note->namedata, "LINUX") == 0)
   11413 	return elfcore_grok_s390_vxrs_low (abfd, note);
   11414       else
   11415 	return true;
   11416 
   11417     case NT_S390_VXRS_HIGH:
   11418       if (note->namesz == 6
   11419 	  && strcmp (note->namedata, "LINUX") == 0)
   11420 	return elfcore_grok_s390_vxrs_high (abfd, note);
   11421       else
   11422 	return true;
   11423 
   11424     case NT_S390_GS_CB:
   11425       if (note->namesz == 6
   11426 	  && strcmp (note->namedata, "LINUX") == 0)
   11427 	return elfcore_grok_s390_gs_cb (abfd, note);
   11428       else
   11429 	return true;
   11430 
   11431     case NT_S390_GS_BC:
   11432       if (note->namesz == 6
   11433 	  && strcmp (note->namedata, "LINUX") == 0)
   11434 	return elfcore_grok_s390_gs_bc (abfd, note);
   11435       else
   11436 	return true;
   11437 
   11438     case NT_ARC_V2:
   11439       if (note->namesz == 6
   11440 	  && strcmp (note->namedata, "LINUX") == 0)
   11441 	return elfcore_grok_arc_v2 (abfd, note);
   11442       else
   11443 	return true;
   11444 
   11445     case NT_ARM_VFP:
   11446       if (note->namesz == 6
   11447 	  && strcmp (note->namedata, "LINUX") == 0)
   11448 	return elfcore_grok_arm_vfp (abfd, note);
   11449       else
   11450 	return true;
   11451 
   11452     case NT_ARM_TLS:
   11453       if (note->namesz == 6
   11454 	  && strcmp (note->namedata, "LINUX") == 0)
   11455 	return elfcore_grok_aarch_tls (abfd, note);
   11456       else
   11457 	return true;
   11458 
   11459     case NT_ARM_HW_BREAK:
   11460       if (note->namesz == 6
   11461 	  && strcmp (note->namedata, "LINUX") == 0)
   11462 	return elfcore_grok_aarch_hw_break (abfd, note);
   11463       else
   11464 	return true;
   11465 
   11466     case NT_ARM_HW_WATCH:
   11467       if (note->namesz == 6
   11468 	  && strcmp (note->namedata, "LINUX") == 0)
   11469 	return elfcore_grok_aarch_hw_watch (abfd, note);
   11470       else
   11471 	return true;
   11472 
   11473     case NT_ARM_SVE:
   11474       if (note->namesz == 6
   11475 	  && strcmp (note->namedata, "LINUX") == 0)
   11476 	return elfcore_grok_aarch_sve (abfd, note);
   11477       else
   11478 	return true;
   11479 
   11480     case NT_ARM_PAC_MASK:
   11481       if (note->namesz == 6
   11482 	  && strcmp (note->namedata, "LINUX") == 0)
   11483 	return elfcore_grok_aarch_pauth (abfd, note);
   11484       else
   11485 	return true;
   11486 
   11487     case NT_ARM_TAGGED_ADDR_CTRL:
   11488       if (note->namesz == 6
   11489 	  && strcmp (note->namedata, "LINUX") == 0)
   11490 	return elfcore_grok_aarch_mte (abfd, note);
   11491       else
   11492 	return true;
   11493 
   11494     case NT_ARM_SSVE:
   11495       if (note->namesz == 6
   11496 	  && strcmp (note->namedata, "LINUX") == 0)
   11497 	return elfcore_grok_aarch_ssve (abfd, note);
   11498       else
   11499 	return true;
   11500 
   11501     case NT_ARM_ZA:
   11502       if (note->namesz == 6
   11503 	  && strcmp (note->namedata, "LINUX") == 0)
   11504 	return elfcore_grok_aarch_za (abfd, note);
   11505       else
   11506 	return true;
   11507 
   11508     case NT_ARM_ZT:
   11509       if (note->namesz == 6
   11510 	  && strcmp (note->namedata, "LINUX") == 0)
   11511 	return elfcore_grok_aarch_zt (abfd, note);
   11512       else
   11513 	return true;
   11514 
   11515     case NT_GDB_TDESC:
   11516       if (note->namesz == 4
   11517 	  && strcmp (note->namedata, "GDB") == 0)
   11518 	return elfcore_grok_gdb_tdesc (abfd, note);
   11519       else
   11520 	return true;
   11521 
   11522     case NT_RISCV_CSR:
   11523       if (note->namesz == 4
   11524 	  && strcmp (note->namedata, "GDB") == 0)
   11525 	return elfcore_grok_riscv_csr (abfd, note);
   11526       else
   11527 	return true;
   11528 
   11529     case NT_LARCH_CPUCFG:
   11530       if (note->namesz == 6
   11531 	  && strcmp (note->namedata, "LINUX") == 0)
   11532 	return elfcore_grok_loongarch_cpucfg (abfd, note);
   11533       else
   11534 	return true;
   11535 
   11536     case NT_LARCH_LBT:
   11537       if (note->namesz == 6
   11538 	  && strcmp (note->namedata, "LINUX") == 0)
   11539 	return elfcore_grok_loongarch_lbt (abfd, note);
   11540       else
   11541 	return true;
   11542 
   11543     case NT_LARCH_LSX:
   11544       if (note->namesz == 6
   11545 	  && strcmp (note->namedata, "LINUX") == 0)
   11546 	return elfcore_grok_loongarch_lsx (abfd, note);
   11547       else
   11548 	return true;
   11549 
   11550     case NT_LARCH_LASX:
   11551       if (note->namesz == 6
   11552 	  && strcmp (note->namedata, "LINUX") == 0)
   11553 	return elfcore_grok_loongarch_lasx (abfd, note);
   11554       else
   11555 	return true;
   11556 
   11557     case NT_PRPSINFO:
   11558     case NT_PSINFO:
   11559       if (bed->elf_backend_grok_psinfo)
   11560 	if ((*bed->elf_backend_grok_psinfo) (abfd, note))
   11561 	  return true;
   11562 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   11563       return elfcore_grok_psinfo (abfd, note);
   11564 #else
   11565       return true;
   11566 #endif
   11567 
   11568     case NT_AUXV:
   11569       return elfcore_make_auxv_note_section (abfd, note, 0);
   11570 
   11571     case NT_FILE:
   11572       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
   11573 					      note);
   11574 
   11575     case NT_SIGINFO:
   11576       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
   11577 					      note);
   11578 
   11579     }
   11580 }
   11581 
   11582 static bool
   11583 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
   11584 {
   11585   struct bfd_build_id* build_id;
   11586 
   11587   if (note->descsz == 0)
   11588     return false;
   11589 
   11590   build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
   11591   if (build_id == NULL)
   11592     return false;
   11593 
   11594   build_id->size = note->descsz;
   11595   memcpy (build_id->data, note->descdata, note->descsz);
   11596   abfd->build_id = build_id;
   11597 
   11598   return true;
   11599 }
   11600 
   11601 static bool
   11602 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
   11603 {
   11604   switch (note->type)
   11605     {
   11606     default:
   11607       return true;
   11608 
   11609     case NT_GNU_PROPERTY_TYPE_0:
   11610       return _bfd_elf_parse_gnu_properties (abfd, note);
   11611 
   11612     case NT_GNU_BUILD_ID:
   11613       return elfobj_grok_gnu_build_id (abfd, note);
   11614     }
   11615 }
   11616 
   11617 static bool
   11618 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
   11619 {
   11620   struct sdt_note *cur =
   11621     (struct sdt_note *) bfd_alloc (abfd,
   11622 				   sizeof (struct sdt_note) + note->descsz);
   11623 
   11624   cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
   11625   cur->size = (bfd_size_type) note->descsz;
   11626   memcpy (cur->data, note->descdata, note->descsz);
   11627 
   11628   elf_tdata (abfd)->sdt_note_head = cur;
   11629 
   11630   return true;
   11631 }
   11632 
   11633 static bool
   11634 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
   11635 {
   11636   switch (note->type)
   11637     {
   11638     case NT_STAPSDT:
   11639       return elfobj_grok_stapsdt_note_1 (abfd, note);
   11640 
   11641     default:
   11642       return true;
   11643     }
   11644 }
   11645 
   11646 static bool
   11647 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
   11648 {
   11649   size_t offset;
   11650 
   11651   switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
   11652     {
   11653     case ELFCLASS32:
   11654       if (note->descsz < 108)
   11655 	return false;
   11656       break;
   11657 
   11658     case ELFCLASS64:
   11659       if (note->descsz < 120)
   11660 	return false;
   11661       break;
   11662 
   11663     default:
   11664       return false;
   11665     }
   11666 
   11667   /* Check for version 1 in pr_version.  */
   11668   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   11669     return false;
   11670 
   11671   offset = 4;
   11672 
   11673   /* Skip over pr_psinfosz. */
   11674   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
   11675     offset += 4;
   11676   else
   11677     {
   11678       offset += 4;	/* Padding before pr_psinfosz. */
   11679       offset += 8;
   11680     }
   11681 
   11682   /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
   11683   elf_tdata (abfd)->core->program
   11684     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
   11685   offset += 17;
   11686 
   11687   /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
   11688   elf_tdata (abfd)->core->command
   11689     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
   11690   offset += 81;
   11691 
   11692   /* Padding before pr_pid.  */
   11693   offset += 2;
   11694 
   11695   /* The pr_pid field was added in version "1a".  */
   11696   if (note->descsz < offset + 4)
   11697     return true;
   11698 
   11699   elf_tdata (abfd)->core->pid
   11700     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11701 
   11702   return true;
   11703 }
   11704 
   11705 static bool
   11706 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
   11707 {
   11708   size_t offset;
   11709   size_t size;
   11710   size_t min_size;
   11711 
   11712   /* Compute offset of pr_getregsz, skipping over pr_statussz.
   11713      Also compute minimum size of this note.  */
   11714   switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
   11715     {
   11716     case ELFCLASS32:
   11717       offset = 4 + 4;
   11718       min_size = offset + (4 * 2) + 4 + 4 + 4;
   11719       break;
   11720 
   11721     case ELFCLASS64:
   11722       offset = 4 + 4 + 8;	/* Includes padding before pr_statussz.  */
   11723       min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
   11724       break;
   11725 
   11726     default:
   11727       return false;
   11728     }
   11729 
   11730   if (note->descsz < min_size)
   11731     return false;
   11732 
   11733   /* Check for version 1 in pr_version.  */
   11734   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   11735     return false;
   11736 
   11737   /* Extract size of pr_reg from pr_gregsetsz.  */
   11738   /* Skip over pr_gregsetsz and pr_fpregsetsz.  */
   11739   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
   11740     {
   11741       size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11742       offset += 4 * 2;
   11743     }
   11744   else
   11745     {
   11746       size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
   11747       offset += 8 * 2;
   11748     }
   11749 
   11750   /* Skip over pr_osreldate.  */
   11751   offset += 4;
   11752 
   11753   /* Read signal from pr_cursig.  */
   11754   if (elf_tdata (abfd)->core->signal == 0)
   11755     elf_tdata (abfd)->core->signal
   11756       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11757   offset += 4;
   11758 
   11759   /* Read TID from pr_pid.  */
   11760   elf_tdata (abfd)->core->lwpid
   11761       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11762   offset += 4;
   11763 
   11764   /* Padding before pr_reg.  */
   11765   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
   11766     offset += 4;
   11767 
   11768   /* Make sure that there is enough data remaining in the note.  */
   11769   if ((note->descsz - offset) < size)
   11770     return false;
   11771 
   11772   /* Make a ".reg/999" section and a ".reg" section.  */
   11773   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   11774 					  size, note->descpos + offset);
   11775 }
   11776 
   11777 static bool
   11778 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
   11779 {
   11780   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11781 
   11782   switch (note->type)
   11783     {
   11784     case NT_PRSTATUS:
   11785       if (bed->elf_backend_grok_freebsd_prstatus)
   11786 	if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
   11787 	  return true;
   11788       return elfcore_grok_freebsd_prstatus (abfd, note);
   11789 
   11790     case NT_FPREGSET:
   11791       return elfcore_grok_prfpreg (abfd, note);
   11792 
   11793     case NT_PRPSINFO:
   11794       return elfcore_grok_freebsd_psinfo (abfd, note);
   11795 
   11796     case NT_FREEBSD_THRMISC:
   11797       return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
   11798 
   11799     case NT_FREEBSD_PROCSTAT_PROC:
   11800       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
   11801 					      note);
   11802 
   11803     case NT_FREEBSD_PROCSTAT_FILES:
   11804       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
   11805 					      note);
   11806 
   11807     case NT_FREEBSD_PROCSTAT_VMMAP:
   11808       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
   11809 					      note);
   11810 
   11811     case NT_FREEBSD_PROCSTAT_AUXV:
   11812       return elfcore_make_auxv_note_section (abfd, note, 4);
   11813 
   11814     case NT_FREEBSD_X86_SEGBASES:
   11815       return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
   11816 
   11817     case NT_X86_XSTATE:
   11818       return elfcore_grok_xstatereg (abfd, note);
   11819 
   11820     case NT_FREEBSD_PTLWPINFO:
   11821       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
   11822 					      note);
   11823 
   11824     case NT_ARM_TLS:
   11825       return elfcore_grok_aarch_tls (abfd, note);
   11826 
   11827     case NT_ARM_VFP:
   11828       return elfcore_grok_arm_vfp (abfd, note);
   11829 
   11830     default:
   11831       return true;
   11832     }
   11833 }
   11834 
   11835 static bool
   11836 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
   11837 {
   11838   char *cp;
   11839 
   11840   cp = strchr (note->namedata, '@');
   11841   if (cp != NULL)
   11842     {
   11843       *lwpidp = atoi(cp + 1);
   11844       return true;
   11845     }
   11846   return false;
   11847 }
   11848 
   11849 static bool
   11850 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   11851 {
   11852   if (note->descsz <= 0x7c + 31)
   11853     return false;
   11854 
   11855   /* Signal number at offset 0x08. */
   11856   elf_tdata (abfd)->core->signal
   11857     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   11858 
   11859   /* Process ID at offset 0x50. */
   11860   elf_tdata (abfd)->core->pid
   11861     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
   11862 
   11863   /* Command name at 0x7c (max 32 bytes, including nul). */
   11864   elf_tdata (abfd)->core->command
   11865     = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
   11866 
   11867   return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
   11868 					  note);
   11869 }
   11870 
   11871 static bool
   11872 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
   11873 {
   11874   int lwp;
   11875 
   11876   if (elfcore_netbsd_get_lwpid (note, &lwp))
   11877     elf_tdata (abfd)->core->lwpid = lwp;
   11878 
   11879   switch (note->type)
   11880     {
   11881     case NT_NETBSDCORE_PROCINFO:
   11882       /* NetBSD-specific core "procinfo".  Note that we expect to
   11883 	 find this note before any of the others, which is fine,
   11884 	 since the kernel writes this note out first when it
   11885 	 creates a core file.  */
   11886       return elfcore_grok_netbsd_procinfo (abfd, note);
   11887     case NT_NETBSDCORE_AUXV:
   11888       /* NetBSD-specific Elf Auxiliary Vector data. */
   11889       return elfcore_make_auxv_note_section (abfd, note, 0);
   11890     case NT_NETBSDCORE_LWPSTATUS:
   11891       return elfcore_make_note_pseudosection (abfd,
   11892 					      ".note.netbsdcore.lwpstatus",
   11893 					      note);
   11894     default:
   11895       break;
   11896     }
   11897 
   11898   if (note->type == NT_NETBSDCORE_AUXV)
   11899     {
   11900       asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
   11901 							   SEC_HAS_CONTENTS);
   11902 
   11903       if (sect == NULL)
   11904 	return false;
   11905       sect->size = note->descsz;
   11906       sect->filepos = note->descpos;
   11907       sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   11908 
   11909       return true;
   11910     }
   11911 
   11912   /* As of March 2020 there are no other machine-independent notes
   11913      defined for NetBSD core files.  If the note type is less
   11914      than the start of the machine-dependent note types, we don't
   11915      understand it.  */
   11916 
   11917   if (note->type < NT_NETBSDCORE_FIRSTMACH)
   11918     return true;
   11919 
   11920 
   11921   switch (bfd_get_arch (abfd))
   11922     {
   11923       /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
   11924 	 PT_GETFPREGS == mach+2.  */
   11925 
   11926     case bfd_arch_aarch64:
   11927     case bfd_arch_alpha:
   11928     case bfd_arch_riscv:
   11929     case bfd_arch_sparc:
   11930       switch (note->type)
   11931 	{
   11932 	case NT_NETBSDCORE_FIRSTMACH+0:
   11933 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11934 
   11935 	case NT_NETBSDCORE_FIRSTMACH+2:
   11936 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11937 
   11938 	default:
   11939 	  return true;
   11940 	}
   11941 
   11942       /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
   11943 	 There's also old PT___GETREGS40 == mach + 1 for old reg
   11944 	 structure which lacks GBR.  */
   11945 
   11946     case bfd_arch_sh:
   11947       switch (note->type)
   11948 	{
   11949 	case NT_NETBSDCORE_FIRSTMACH+3:
   11950 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11951 
   11952 	case NT_NETBSDCORE_FIRSTMACH+5:
   11953 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11954 
   11955 	default:
   11956 	  return true;
   11957 	}
   11958 
   11959       /* On all other arch's, PT_GETREGS == mach+1 and
   11960 	 PT_GETFPREGS == mach+3.  */
   11961 
   11962     default:
   11963       switch (note->type)
   11964 	{
   11965 	case NT_NETBSDCORE_FIRSTMACH+1:
   11966 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11967 
   11968 	case NT_NETBSDCORE_FIRSTMACH+3:
   11969 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11970 
   11971 	default:
   11972 	  return true;
   11973 	}
   11974     }
   11975     /* NOTREACHED */
   11976 }
   11977 
   11978 static bool
   11979 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   11980 {
   11981   if (note->descsz <= 0x48 + 31)
   11982     return false;
   11983 
   11984   /* Signal number at offset 0x08. */
   11985   elf_tdata (abfd)->core->signal
   11986     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   11987 
   11988   /* Process ID at offset 0x20. */
   11989   elf_tdata (abfd)->core->pid
   11990     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
   11991 
   11992   /* Command name at 0x48 (max 32 bytes, including nul). */
   11993   elf_tdata (abfd)->core->command
   11994     = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
   11995 
   11996   return true;
   11997 }
   11998 
   11999 /* Processes Solaris's process status note.
   12000    sig_off ~ offsetof(prstatus_t, pr_cursig)
   12001    pid_off ~ offsetof(prstatus_t, pr_pid)
   12002    lwpid_off ~ offsetof(prstatus_t, pr_who)
   12003    gregset_size ~ sizeof(gregset_t)
   12004    gregset_offset ~ offsetof(prstatus_t, pr_reg)  */
   12005 
   12006 static bool
   12007 elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
   12008 			       int pid_off, int lwpid_off, size_t gregset_size,
   12009 			       size_t gregset_offset)
   12010 {
   12011   asection *sect = NULL;
   12012   elf_tdata (abfd)->core->signal
   12013     = bfd_get_16 (abfd, note->descdata + sig_off);
   12014   elf_tdata (abfd)->core->pid
   12015     = bfd_get_32 (abfd, note->descdata + pid_off);
   12016   elf_tdata (abfd)->core->lwpid
   12017     = bfd_get_32 (abfd, note->descdata + lwpid_off);
   12018 
   12019   sect = bfd_get_section_by_name (abfd, ".reg");
   12020   if (sect != NULL)
   12021     sect->size = gregset_size;
   12022 
   12023   return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
   12024 					  note->descpos + gregset_offset);
   12025 }
   12026 
   12027 /* Gets program and arguments from a core.
   12028    prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
   12029    comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs)  */
   12030 
   12031 static bool
   12032 elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
   12033 			  int prog_off, int comm_off)
   12034 {
   12035   elf_tdata (abfd)->core->program
   12036     = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
   12037   elf_tdata (abfd)->core->command
   12038     = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
   12039 
   12040   return true;
   12041 }
   12042 
   12043 /* Processes Solaris's LWP status note.
   12044    gregset_size ~ sizeof(gregset_t)
   12045    gregset_off ~ offsetof(lwpstatus_t, pr_reg)
   12046    fpregset_size ~ sizeof(fpregset_t)
   12047    fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg)  */
   12048 
   12049 static bool
   12050 elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
   12051 				size_t gregset_size, int gregset_off,
   12052 				size_t fpregset_size, int fpregset_off)
   12053 {
   12054   asection *sect = NULL;
   12055   char reg2_section_name[16] = { 0 };
   12056 
   12057   (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
   12058 		   elf_tdata (abfd)->core->lwpid);
   12059 
   12060   /* offsetof(lwpstatus_t, pr_lwpid) */
   12061   elf_tdata (abfd)->core->lwpid
   12062     = bfd_get_32 (abfd, note->descdata + 4);
   12063   /* offsetof(lwpstatus_t, pr_cursig) */
   12064   elf_tdata (abfd)->core->signal
   12065     = bfd_get_16 (abfd, note->descdata + 12);
   12066 
   12067   sect = bfd_get_section_by_name (abfd, ".reg");
   12068   if (sect != NULL)
   12069     sect->size = gregset_size;
   12070   else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
   12071 					     note->descpos + gregset_off))
   12072     return false;
   12073 
   12074   sect = bfd_get_section_by_name (abfd, reg2_section_name);
   12075   if (sect != NULL)
   12076     {
   12077       sect->size = fpregset_size;
   12078       sect->filepos = note->descpos + fpregset_off;
   12079       sect->alignment_power = 2;
   12080     }
   12081   else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
   12082 					     note->descpos + fpregset_off))
   12083     return false;
   12084 
   12085   return true;
   12086 }
   12087 
   12088 static bool
   12089 elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
   12090 {
   12091   if (note == NULL)
   12092     return false;
   12093 
   12094   /* core files are identified as 32- or 64-bit, SPARC or x86,
   12095      by the size of the descsz which matches the sizeof()
   12096      the type appropriate for that note type (e.g., prstatus_t for
   12097      SOLARIS_NT_PRSTATUS) for the corresponding architecture
   12098      on Solaris. The core file bitness may differ from the bitness of
   12099      gdb itself, so fixed values are used instead of sizeof().
   12100      Appropriate fixed offsets are also used to obtain data from
   12101      the note.  */
   12102 
   12103   switch ((int) note->type)
   12104     {
   12105     case SOLARIS_NT_PRSTATUS:
   12106       switch (note->descsz)
   12107 	{
   12108 	case 508: /* sizeof(prstatus_t) SPARC 32-bit */
   12109 	  return elfcore_grok_solaris_prstatus(abfd, note,
   12110 					       136, 216, 308, 152, 356);
   12111 	case 904: /* sizeof(prstatus_t) SPARC 64-bit */
   12112 	  return elfcore_grok_solaris_prstatus(abfd, note,
   12113 					       264, 360, 520, 304, 600);
   12114 	case 432: /* sizeof(prstatus_t) Intel 32-bit */
   12115 	  return elfcore_grok_solaris_prstatus(abfd, note,
   12116 					       136, 216, 308, 76, 356);
   12117 	case 824: /* sizeof(prstatus_t) Intel 64-bit */
   12118 	  return elfcore_grok_solaris_prstatus(abfd, note,
   12119 					       264, 360, 520, 224, 600);
   12120 	default:
   12121 	  return true;
   12122 	}
   12123 
   12124     case SOLARIS_NT_PSINFO:
   12125     case SOLARIS_NT_PRPSINFO:
   12126       switch (note->descsz)
   12127 	{
   12128 	case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
   12129 	  return elfcore_grok_solaris_info(abfd, note, 84, 100);
   12130 	case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
   12131 	  return elfcore_grok_solaris_info(abfd, note, 120, 136);
   12132 	case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
   12133 	  return elfcore_grok_solaris_info(abfd, note, 88, 104);
   12134 	case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
   12135 	  return elfcore_grok_solaris_info(abfd, note, 136, 152);
   12136 	default:
   12137 	  return true;
   12138 	}
   12139 
   12140     case SOLARIS_NT_LWPSTATUS:
   12141       switch (note->descsz)
   12142 	{
   12143 	case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
   12144 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   12145 						152, 344, 400, 496);
   12146 	case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
   12147 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   12148 						304, 544, 544, 848);
   12149 	case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
   12150 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   12151 						76, 344, 380, 420);
   12152 	case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
   12153 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   12154 						224, 544, 528, 768);
   12155 	default:
   12156 	  return true;
   12157 	}
   12158 
   12159     case SOLARIS_NT_LWPSINFO:
   12160       /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
   12161       if (note->descsz == 128 || note->descsz == 152)
   12162 	elf_tdata (abfd)->core->lwpid =
   12163 	  bfd_get_32 (abfd, note->descdata + 4);
   12164       break;
   12165 
   12166     default:
   12167       break;
   12168     }
   12169 
   12170   return true;
   12171 }
   12172 
   12173 /* For name starting with "CORE" this may be either a Solaris
   12174    core file or a gdb-generated core file.  Do Solaris-specific
   12175    processing on selected note types first with
   12176    elfcore_grok_solaris_note(), then process the note
   12177    in elfcore_grok_note().  */
   12178 
   12179 static bool
   12180 elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
   12181 {
   12182   if (!elfcore_grok_solaris_note_impl (abfd, note))
   12183     return false;
   12184 
   12185   return elfcore_grok_note (abfd, note);
   12186 }
   12187 
   12188 static bool
   12189 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
   12190 {
   12191   if (note->type == NT_OPENBSD_PROCINFO)
   12192     return elfcore_grok_openbsd_procinfo (abfd, note);
   12193 
   12194   if (note->type == NT_OPENBSD_REGS)
   12195     return elfcore_make_note_pseudosection (abfd, ".reg", note);
   12196 
   12197   if (note->type == NT_OPENBSD_FPREGS)
   12198     return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   12199 
   12200   if (note->type == NT_OPENBSD_XFPREGS)
   12201     return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
   12202 
   12203   if (note->type == NT_OPENBSD_AUXV)
   12204     return elfcore_make_auxv_note_section (abfd, note, 0);
   12205 
   12206   if (note->type == NT_OPENBSD_WCOOKIE)
   12207     {
   12208       asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
   12209 							   SEC_HAS_CONTENTS);
   12210 
   12211       if (sect == NULL)
   12212 	return false;
   12213       sect->size = note->descsz;
   12214       sect->filepos = note->descpos;
   12215       sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   12216 
   12217       return true;
   12218     }
   12219 
   12220   return true;
   12221 }
   12222 
   12223 static bool
   12224 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
   12225 {
   12226   void *ddata = note->descdata;
   12227   char buf[100];
   12228   char *name;
   12229   asection *sect;
   12230   short sig;
   12231   unsigned flags;
   12232 
   12233   if (note->descsz < 16)
   12234     return false;
   12235 
   12236   /* nto_procfs_status 'pid' field is at offset 0.  */
   12237   elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
   12238 
   12239   /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
   12240   *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
   12241 
   12242   /* nto_procfs_status 'flags' field is at offset 8.  */
   12243   flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
   12244 
   12245   /* nto_procfs_status 'what' field is at offset 14.  */
   12246   if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
   12247     {
   12248       elf_tdata (abfd)->core->signal = sig;
   12249       elf_tdata (abfd)->core->lwpid = *tid;
   12250     }
   12251 
   12252   /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
   12253      do not come from signals so we make sure we set the current
   12254      thread just in case.  */
   12255   if (flags & 0x00000080)
   12256     elf_tdata (abfd)->core->lwpid = *tid;
   12257 
   12258   /* Make a ".qnx_core_status/%d" section.  */
   12259   sprintf (buf, ".qnx_core_status/%ld", *tid);
   12260 
   12261   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   12262   if (name == NULL)
   12263     return false;
   12264   strcpy (name, buf);
   12265 
   12266   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   12267   if (sect == NULL)
   12268     return false;
   12269 
   12270   sect->size		= note->descsz;
   12271   sect->filepos		= note->descpos;
   12272   sect->alignment_power = 2;
   12273 
   12274   return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
   12275 }
   12276 
   12277 static bool
   12278 elfcore_grok_nto_regs (bfd *abfd,
   12279 		       Elf_Internal_Note *note,
   12280 		       long tid,
   12281 		       char *base)
   12282 {
   12283   char buf[100];
   12284   char *name;
   12285   asection *sect;
   12286 
   12287   /* Make a "(base)/%d" section.  */
   12288   sprintf (buf, "%s/%ld", base, tid);
   12289 
   12290   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   12291   if (name == NULL)
   12292     return false;
   12293   strcpy (name, buf);
   12294 
   12295   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   12296   if (sect == NULL)
   12297     return false;
   12298 
   12299   sect->size		= note->descsz;
   12300   sect->filepos		= note->descpos;
   12301   sect->alignment_power = 2;
   12302 
   12303   /* This is the current thread.  */
   12304   if (elf_tdata (abfd)->core->lwpid == tid)
   12305     return elfcore_maybe_make_sect (abfd, base, sect);
   12306 
   12307   return true;
   12308 }
   12309 
   12310 static bool
   12311 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
   12312 {
   12313   /* Every GREG section has a STATUS section before it.  Store the
   12314      tid from the previous call to pass down to the next gregs
   12315      function.  */
   12316   static long tid = 1;
   12317 
   12318   switch (note->type)
   12319     {
   12320     case QNT_CORE_INFO:
   12321       return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
   12322     case QNT_CORE_STATUS:
   12323       return elfcore_grok_nto_status (abfd, note, &tid);
   12324     case QNT_CORE_GREG:
   12325       return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
   12326     case QNT_CORE_FPREG:
   12327       return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
   12328     default:
   12329       return true;
   12330     }
   12331 }
   12332 
   12333 static bool
   12334 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
   12335 {
   12336   char *name;
   12337   asection *sect;
   12338   size_t len;
   12339 
   12340   /* Use note name as section name.  */
   12341   len = note->namesz;
   12342   name = (char *) bfd_alloc (abfd, len);
   12343   if (name == NULL)
   12344     return false;
   12345   memcpy (name, note->namedata, len);
   12346   name[len - 1] = '\0';
   12347 
   12348   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   12349   if (sect == NULL)
   12350     return false;
   12351 
   12352   sect->size		= note->descsz;
   12353   sect->filepos		= note->descpos;
   12354   sect->alignment_power = 1;
   12355 
   12356   return true;
   12357 }
   12358 
   12359 /* Function: elfcore_write_note
   12360 
   12361    Inputs:
   12362      buffer to hold note, and current size of buffer
   12363      name of note
   12364      type of note
   12365      data for note
   12366      size of data for note
   12367 
   12368    Writes note to end of buffer.  ELF64 notes are written exactly as
   12369    for ELF32, despite the current (as of 2006) ELF gabi specifying
   12370    that they ought to have 8-byte namesz and descsz field, and have
   12371    8-byte alignment.  Other writers, eg. Linux kernel, do the same.
   12372 
   12373    Return:
   12374    Pointer to realloc'd buffer, *BUFSIZ updated.  */
   12375 
   12376 char *
   12377 elfcore_write_note (bfd *abfd,
   12378 		    char *buf,
   12379 		    int *bufsiz,
   12380 		    const char *name,
   12381 		    int type,
   12382 		    const void *input,
   12383 		    int size)
   12384 {
   12385   Elf_External_Note *xnp;
   12386   size_t namesz;
   12387   size_t newspace;
   12388   char *dest;
   12389 
   12390   namesz = 0;
   12391   if (name != NULL)
   12392     namesz = strlen (name) + 1;
   12393 
   12394   newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
   12395 
   12396   buf = (char *) realloc (buf, *bufsiz + newspace);
   12397   if (buf == NULL)
   12398     return buf;
   12399   dest = buf + *bufsiz;
   12400   *bufsiz += newspace;
   12401   xnp = (Elf_External_Note *) dest;
   12402   H_PUT_32 (abfd, namesz, xnp->namesz);
   12403   H_PUT_32 (abfd, size, xnp->descsz);
   12404   H_PUT_32 (abfd, type, xnp->type);
   12405   dest = xnp->name;
   12406   if (name != NULL)
   12407     {
   12408       memcpy (dest, name, namesz);
   12409       dest += namesz;
   12410       while (namesz & 3)
   12411 	{
   12412 	  *dest++ = '\0';
   12413 	  ++namesz;
   12414 	}
   12415     }
   12416   memcpy (dest, input, size);
   12417   dest += size;
   12418   while (size & 3)
   12419     {
   12420       *dest++ = '\0';
   12421       ++size;
   12422     }
   12423   return buf;
   12424 }
   12425 
   12426 /* gcc-8 warns (*) on all the strncpy calls in this function about
   12427    possible string truncation.  The "truncation" is not a bug.  We
   12428    have an external representation of structs with fields that are not
   12429    necessarily NULL terminated and corresponding internal
   12430    representation fields that are one larger so that they can always
   12431    be NULL terminated.
   12432    gcc versions between 4.2 and 4.6 do not allow pragma control of
   12433    diagnostics inside functions, giving a hard error if you try to use
   12434    the finer control available with later versions.
   12435    gcc prior to 4.2 warns about diagnostic push and pop.
   12436    gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
   12437    unless you also add #pragma GCC diagnostic ignored "-Wpragma".
   12438    (*) Depending on your system header files!  */
   12439 #if GCC_VERSION >= 8000
   12440 # pragma GCC diagnostic push
   12441 # pragma GCC diagnostic ignored "-Wstringop-truncation"
   12442 #endif
   12443 char *
   12444 elfcore_write_prpsinfo (bfd  *abfd,
   12445 			char *buf,
   12446 			int  *bufsiz,
   12447 			const char *fname,
   12448 			const char *psargs)
   12449 {
   12450   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12451 
   12452   if (bed->elf_backend_write_core_note != NULL)
   12453     {
   12454       char *ret;
   12455       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   12456 						 NT_PRPSINFO, fname, psargs);
   12457       if (ret != NULL)
   12458 	return ret;
   12459     }
   12460 
   12461 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   12462 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   12463   if (bed->s->elfclass == ELFCLASS32)
   12464     {
   12465 #  if defined (HAVE_PSINFO32_T)
   12466       psinfo32_t data;
   12467       int note_type = NT_PSINFO;
   12468 #  else
   12469       prpsinfo32_t data;
   12470       int note_type = NT_PRPSINFO;
   12471 #  endif
   12472 
   12473       memset (&data, 0, sizeof (data));
   12474       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   12475       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   12476       return elfcore_write_note (abfd, buf, bufsiz,
   12477 				 "CORE", note_type, &data, sizeof (data));
   12478     }
   12479   else
   12480 # endif
   12481     {
   12482 # if defined (HAVE_PSINFO_T)
   12483       psinfo_t data;
   12484       int note_type = NT_PSINFO;
   12485 # else
   12486       prpsinfo_t data;
   12487       int note_type = NT_PRPSINFO;
   12488 # endif
   12489 
   12490       memset (&data, 0, sizeof (data));
   12491       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   12492       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   12493       return elfcore_write_note (abfd, buf, bufsiz,
   12494 				 "CORE", note_type, &data, sizeof (data));
   12495     }
   12496 #endif	/* PSINFO_T or PRPSINFO_T */
   12497 
   12498   free (buf);
   12499   return NULL;
   12500 }
   12501 #if GCC_VERSION >= 8000
   12502 # pragma GCC diagnostic pop
   12503 #endif
   12504 
   12505 char *
   12506 elfcore_write_linux_prpsinfo32
   12507   (bfd *abfd, char *buf, int *bufsiz,
   12508    const struct elf_internal_linux_prpsinfo *prpsinfo)
   12509 {
   12510   if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
   12511     {
   12512       struct elf_external_linux_prpsinfo32_ugid16 data;
   12513 
   12514       swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
   12515       return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
   12516 				 &data, sizeof (data));
   12517     }
   12518   else
   12519     {
   12520       struct elf_external_linux_prpsinfo32_ugid32 data;
   12521 
   12522       swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
   12523       return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
   12524 				 &data, sizeof (data));
   12525     }
   12526 }
   12527 
   12528 char *
   12529 elfcore_write_linux_prpsinfo64
   12530   (bfd *abfd, char *buf, int *bufsiz,
   12531    const struct elf_internal_linux_prpsinfo *prpsinfo)
   12532 {
   12533   if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
   12534     {
   12535       struct elf_external_linux_prpsinfo64_ugid16 data;
   12536 
   12537       swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
   12538       return elfcore_write_note (abfd, buf, bufsiz,
   12539 				 "CORE", NT_PRPSINFO, &data, sizeof (data));
   12540     }
   12541   else
   12542     {
   12543       struct elf_external_linux_prpsinfo64_ugid32 data;
   12544 
   12545       swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
   12546       return elfcore_write_note (abfd, buf, bufsiz,
   12547 				 "CORE", NT_PRPSINFO, &data, sizeof (data));
   12548     }
   12549 }
   12550 
   12551 char *
   12552 elfcore_write_prstatus (bfd *abfd,
   12553 			char *buf,
   12554 			int *bufsiz,
   12555 			long pid,
   12556 			int cursig,
   12557 			const void *gregs)
   12558 {
   12559   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12560 
   12561   if (bed->elf_backend_write_core_note != NULL)
   12562     {
   12563       char *ret;
   12564       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   12565 						 NT_PRSTATUS,
   12566 						 pid, cursig, gregs);
   12567       if (ret != NULL)
   12568 	return ret;
   12569     }
   12570 
   12571 #if defined (HAVE_PRSTATUS_T)
   12572 #if defined (HAVE_PRSTATUS32_T)
   12573   if (bed->s->elfclass == ELFCLASS32)
   12574     {
   12575       prstatus32_t prstat;
   12576 
   12577       memset (&prstat, 0, sizeof (prstat));
   12578       prstat.pr_pid = pid;
   12579       prstat.pr_cursig = cursig;
   12580       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   12581       return elfcore_write_note (abfd, buf, bufsiz, "CORE",
   12582 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   12583     }
   12584   else
   12585 #endif
   12586     {
   12587       prstatus_t prstat;
   12588 
   12589       memset (&prstat, 0, sizeof (prstat));
   12590       prstat.pr_pid = pid;
   12591       prstat.pr_cursig = cursig;
   12592       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   12593       return elfcore_write_note (abfd, buf, bufsiz, "CORE",
   12594 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   12595     }
   12596 #endif /* HAVE_PRSTATUS_T */
   12597 
   12598   free (buf);
   12599   return NULL;
   12600 }
   12601 
   12602 #if defined (HAVE_LWPSTATUS_T)
   12603 char *
   12604 elfcore_write_lwpstatus (bfd *abfd,
   12605 			 char *buf,
   12606 			 int *bufsiz,
   12607 			 long pid,
   12608 			 int cursig,
   12609 			 const void *gregs)
   12610 {
   12611   lwpstatus_t lwpstat;
   12612   const char *note_name = "CORE";
   12613 
   12614   memset (&lwpstat, 0, sizeof (lwpstat));
   12615   lwpstat.pr_lwpid  = pid >> 16;
   12616   lwpstat.pr_cursig = cursig;
   12617 #if defined (HAVE_LWPSTATUS_T_PR_REG)
   12618   memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
   12619 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   12620 #if !defined(gregs)
   12621   memcpy (lwpstat.pr_context.uc_mcontext.gregs,
   12622 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
   12623 #else
   12624   memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
   12625 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
   12626 #endif
   12627 #endif
   12628   return elfcore_write_note (abfd, buf, bufsiz, note_name,
   12629 			     NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
   12630 }
   12631 #endif /* HAVE_LWPSTATUS_T */
   12632 
   12633 #if defined (HAVE_PSTATUS_T)
   12634 char *
   12635 elfcore_write_pstatus (bfd *abfd,
   12636 		       char *buf,
   12637 		       int *bufsiz,
   12638 		       long pid,
   12639 		       int cursig ATTRIBUTE_UNUSED,
   12640 		       const void *gregs ATTRIBUTE_UNUSED)
   12641 {
   12642   const char *note_name = "CORE";
   12643 #if defined (HAVE_PSTATUS32_T)
   12644   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12645 
   12646   if (bed->s->elfclass == ELFCLASS32)
   12647     {
   12648       pstatus32_t pstat;
   12649 
   12650       memset (&pstat, 0, sizeof (pstat));
   12651       pstat.pr_pid = pid & 0xffff;
   12652       buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
   12653 				NT_PSTATUS, &pstat, sizeof (pstat));
   12654       return buf;
   12655     }
   12656   else
   12657 #endif
   12658     {
   12659       pstatus_t pstat;
   12660 
   12661       memset (&pstat, 0, sizeof (pstat));
   12662       pstat.pr_pid = pid & 0xffff;
   12663       buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
   12664 				NT_PSTATUS, &pstat, sizeof (pstat));
   12665       return buf;
   12666     }
   12667 }
   12668 #endif /* HAVE_PSTATUS_T */
   12669 
   12670 char *
   12671 elfcore_write_prfpreg (bfd *abfd,
   12672 		       char *buf,
   12673 		       int *bufsiz,
   12674 		       const void *fpregs,
   12675 		       int size)
   12676 {
   12677   const char *note_name = "CORE";
   12678   return elfcore_write_note (abfd, buf, bufsiz,
   12679 			     note_name, NT_FPREGSET, fpregs, size);
   12680 }
   12681 
   12682 char *
   12683 elfcore_write_prxfpreg (bfd *abfd,
   12684 			char *buf,
   12685 			int *bufsiz,
   12686 			const void *xfpregs,
   12687 			int size)
   12688 {
   12689   char *note_name = "LINUX";
   12690   return elfcore_write_note (abfd, buf, bufsiz,
   12691 			     note_name, NT_PRXFPREG, xfpregs, size);
   12692 }
   12693 
   12694 char *
   12695 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
   12696 			 const void *xfpregs, int size)
   12697 {
   12698   char *note_name;
   12699   if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
   12700     note_name = "FreeBSD";
   12701   else
   12702     note_name = "LINUX";
   12703   return elfcore_write_note (abfd, buf, bufsiz,
   12704 			     note_name, NT_X86_XSTATE, xfpregs, size);
   12705 }
   12706 
   12707 char *
   12708 elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
   12709 			    const void *regs, int size)
   12710 {
   12711   char *note_name = "FreeBSD";
   12712   return elfcore_write_note (abfd, buf, bufsiz,
   12713 			     note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
   12714 }
   12715 
   12716 char *
   12717 elfcore_write_ppc_vmx (bfd *abfd,
   12718 		       char *buf,
   12719 		       int *bufsiz,
   12720 		       const void *ppc_vmx,
   12721 		       int size)
   12722 {
   12723   char *note_name = "LINUX";
   12724   return elfcore_write_note (abfd, buf, bufsiz,
   12725 			     note_name, NT_PPC_VMX, ppc_vmx, size);
   12726 }
   12727 
   12728 char *
   12729 elfcore_write_ppc_vsx (bfd *abfd,
   12730 		       char *buf,
   12731 		       int *bufsiz,
   12732 		       const void *ppc_vsx,
   12733 		       int size)
   12734 {
   12735   char *note_name = "LINUX";
   12736   return elfcore_write_note (abfd, buf, bufsiz,
   12737 			     note_name, NT_PPC_VSX, ppc_vsx, size);
   12738 }
   12739 
   12740 char *
   12741 elfcore_write_ppc_tar (bfd *abfd,
   12742 		       char *buf,
   12743 		       int *bufsiz,
   12744 		       const void *ppc_tar,
   12745 		       int size)
   12746 {
   12747   char *note_name = "LINUX";
   12748   return elfcore_write_note (abfd, buf, bufsiz,
   12749 			     note_name, NT_PPC_TAR, ppc_tar, size);
   12750 }
   12751 
   12752 char *
   12753 elfcore_write_ppc_ppr (bfd *abfd,
   12754 		       char *buf,
   12755 		       int *bufsiz,
   12756 		       const void *ppc_ppr,
   12757 		       int size)
   12758 {
   12759   char *note_name = "LINUX";
   12760   return elfcore_write_note (abfd, buf, bufsiz,
   12761 			     note_name, NT_PPC_PPR, ppc_ppr, size);
   12762 }
   12763 
   12764 char *
   12765 elfcore_write_ppc_dscr (bfd *abfd,
   12766 			char *buf,
   12767 			int *bufsiz,
   12768 			const void *ppc_dscr,
   12769 			int size)
   12770 {
   12771   char *note_name = "LINUX";
   12772   return elfcore_write_note (abfd, buf, bufsiz,
   12773 			     note_name, NT_PPC_DSCR, ppc_dscr, size);
   12774 }
   12775 
   12776 char *
   12777 elfcore_write_ppc_ebb (bfd *abfd,
   12778 		       char *buf,
   12779 		       int *bufsiz,
   12780 		       const void *ppc_ebb,
   12781 		       int size)
   12782 {
   12783   char *note_name = "LINUX";
   12784   return elfcore_write_note (abfd, buf, bufsiz,
   12785 			     note_name, NT_PPC_EBB, ppc_ebb, size);
   12786 }
   12787 
   12788 char *
   12789 elfcore_write_ppc_pmu (bfd *abfd,
   12790 		       char *buf,
   12791 		       int *bufsiz,
   12792 		       const void *ppc_pmu,
   12793 		       int size)
   12794 {
   12795   char *note_name = "LINUX";
   12796   return elfcore_write_note (abfd, buf, bufsiz,
   12797 			     note_name, NT_PPC_PMU, ppc_pmu, size);
   12798 }
   12799 
   12800 char *
   12801 elfcore_write_ppc_tm_cgpr (bfd *abfd,
   12802 			   char *buf,
   12803 			   int *bufsiz,
   12804 			   const void *ppc_tm_cgpr,
   12805 			   int size)
   12806 {
   12807   char *note_name = "LINUX";
   12808   return elfcore_write_note (abfd, buf, bufsiz,
   12809 			     note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
   12810 }
   12811 
   12812 char *
   12813 elfcore_write_ppc_tm_cfpr (bfd *abfd,
   12814 			   char *buf,
   12815 			   int *bufsiz,
   12816 			   const void *ppc_tm_cfpr,
   12817 			   int size)
   12818 {
   12819   char *note_name = "LINUX";
   12820   return elfcore_write_note (abfd, buf, bufsiz,
   12821 			     note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
   12822 }
   12823 
   12824 char *
   12825 elfcore_write_ppc_tm_cvmx (bfd *abfd,
   12826 			   char *buf,
   12827 			   int *bufsiz,
   12828 			   const void *ppc_tm_cvmx,
   12829 			   int size)
   12830 {
   12831   char *note_name = "LINUX";
   12832   return elfcore_write_note (abfd, buf, bufsiz,
   12833 			     note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
   12834 }
   12835 
   12836 char *
   12837 elfcore_write_ppc_tm_cvsx (bfd *abfd,
   12838 			   char *buf,
   12839 			   int *bufsiz,
   12840 			   const void *ppc_tm_cvsx,
   12841 			   int size)
   12842 {
   12843   char *note_name = "LINUX";
   12844   return elfcore_write_note (abfd, buf, bufsiz,
   12845 			     note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
   12846 }
   12847 
   12848 char *
   12849 elfcore_write_ppc_tm_spr (bfd *abfd,
   12850 			  char *buf,
   12851 			  int *bufsiz,
   12852 			  const void *ppc_tm_spr,
   12853 			  int size)
   12854 {
   12855   char *note_name = "LINUX";
   12856   return elfcore_write_note (abfd, buf, bufsiz,
   12857 			     note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
   12858 }
   12859 
   12860 char *
   12861 elfcore_write_ppc_tm_ctar (bfd *abfd,
   12862 			   char *buf,
   12863 			   int *bufsiz,
   12864 			   const void *ppc_tm_ctar,
   12865 			   int size)
   12866 {
   12867   char *note_name = "LINUX";
   12868   return elfcore_write_note (abfd, buf, bufsiz,
   12869 			     note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
   12870 }
   12871 
   12872 char *
   12873 elfcore_write_ppc_tm_cppr (bfd *abfd,
   12874 			   char *buf,
   12875 			   int *bufsiz,
   12876 			   const void *ppc_tm_cppr,
   12877 			   int size)
   12878 {
   12879   char *note_name = "LINUX";
   12880   return elfcore_write_note (abfd, buf, bufsiz,
   12881 			     note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
   12882 }
   12883 
   12884 char *
   12885 elfcore_write_ppc_tm_cdscr (bfd *abfd,
   12886 			    char *buf,
   12887 			    int *bufsiz,
   12888 			    const void *ppc_tm_cdscr,
   12889 			    int size)
   12890 {
   12891   char *note_name = "LINUX";
   12892   return elfcore_write_note (abfd, buf, bufsiz,
   12893 			     note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
   12894 }
   12895 
   12896 static char *
   12897 elfcore_write_s390_high_gprs (bfd *abfd,
   12898 			      char *buf,
   12899 			      int *bufsiz,
   12900 			      const void *s390_high_gprs,
   12901 			      int size)
   12902 {
   12903   char *note_name = "LINUX";
   12904   return elfcore_write_note (abfd, buf, bufsiz,
   12905 			     note_name, NT_S390_HIGH_GPRS,
   12906 			     s390_high_gprs, size);
   12907 }
   12908 
   12909 char *
   12910 elfcore_write_s390_timer (bfd *abfd,
   12911 			  char *buf,
   12912 			  int *bufsiz,
   12913 			  const void *s390_timer,
   12914 			  int size)
   12915 {
   12916   char *note_name = "LINUX";
   12917   return elfcore_write_note (abfd, buf, bufsiz,
   12918 			     note_name, NT_S390_TIMER, s390_timer, size);
   12919 }
   12920 
   12921 char *
   12922 elfcore_write_s390_todcmp (bfd *abfd,
   12923 			   char *buf,
   12924 			   int *bufsiz,
   12925 			   const void *s390_todcmp,
   12926 			   int size)
   12927 {
   12928   char *note_name = "LINUX";
   12929   return elfcore_write_note (abfd, buf, bufsiz,
   12930 			     note_name, NT_S390_TODCMP, s390_todcmp, size);
   12931 }
   12932 
   12933 char *
   12934 elfcore_write_s390_todpreg (bfd *abfd,
   12935 			    char *buf,
   12936 			    int *bufsiz,
   12937 			    const void *s390_todpreg,
   12938 			    int size)
   12939 {
   12940   char *note_name = "LINUX";
   12941   return elfcore_write_note (abfd, buf, bufsiz,
   12942 			     note_name, NT_S390_TODPREG, s390_todpreg, size);
   12943 }
   12944 
   12945 char *
   12946 elfcore_write_s390_ctrs (bfd *abfd,
   12947 			 char *buf,
   12948 			 int *bufsiz,
   12949 			 const void *s390_ctrs,
   12950 			 int size)
   12951 {
   12952   char *note_name = "LINUX";
   12953   return elfcore_write_note (abfd, buf, bufsiz,
   12954 			     note_name, NT_S390_CTRS, s390_ctrs, size);
   12955 }
   12956 
   12957 char *
   12958 elfcore_write_s390_prefix (bfd *abfd,
   12959 			   char *buf,
   12960 			   int *bufsiz,
   12961 			   const void *s390_prefix,
   12962 			   int size)
   12963 {
   12964   char *note_name = "LINUX";
   12965   return elfcore_write_note (abfd, buf, bufsiz,
   12966 			     note_name, NT_S390_PREFIX, s390_prefix, size);
   12967 }
   12968 
   12969 char *
   12970 elfcore_write_s390_last_break (bfd *abfd,
   12971 			       char *buf,
   12972 			       int *bufsiz,
   12973 			       const void *s390_last_break,
   12974 			       int size)
   12975 {
   12976   char *note_name = "LINUX";
   12977   return elfcore_write_note (abfd, buf, bufsiz,
   12978 			     note_name, NT_S390_LAST_BREAK,
   12979 			     s390_last_break, size);
   12980 }
   12981 
   12982 char *
   12983 elfcore_write_s390_system_call (bfd *abfd,
   12984 				char *buf,
   12985 				int *bufsiz,
   12986 				const void *s390_system_call,
   12987 				int size)
   12988 {
   12989   char *note_name = "LINUX";
   12990   return elfcore_write_note (abfd, buf, bufsiz,
   12991 			     note_name, NT_S390_SYSTEM_CALL,
   12992 			     s390_system_call, size);
   12993 }
   12994 
   12995 char *
   12996 elfcore_write_s390_tdb (bfd *abfd,
   12997 			char *buf,
   12998 			int *bufsiz,
   12999 			const void *s390_tdb,
   13000 			int size)
   13001 {
   13002   char *note_name = "LINUX";
   13003   return elfcore_write_note (abfd, buf, bufsiz,
   13004 			     note_name, NT_S390_TDB, s390_tdb, size);
   13005 }
   13006 
   13007 char *
   13008 elfcore_write_s390_vxrs_low (bfd *abfd,
   13009 			     char *buf,
   13010 			     int *bufsiz,
   13011 			     const void *s390_vxrs_low,
   13012 			     int size)
   13013 {
   13014   char *note_name = "LINUX";
   13015   return elfcore_write_note (abfd, buf, bufsiz,
   13016 			     note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
   13017 }
   13018 
   13019 char *
   13020 elfcore_write_s390_vxrs_high (bfd *abfd,
   13021 			     char *buf,
   13022 			     int *bufsiz,
   13023 			     const void *s390_vxrs_high,
   13024 			     int size)
   13025 {
   13026   char *note_name = "LINUX";
   13027   return elfcore_write_note (abfd, buf, bufsiz,
   13028 			     note_name, NT_S390_VXRS_HIGH,
   13029 			     s390_vxrs_high, size);
   13030 }
   13031 
   13032 char *
   13033 elfcore_write_s390_gs_cb (bfd *abfd,
   13034 			  char *buf,
   13035 			  int *bufsiz,
   13036 			  const void *s390_gs_cb,
   13037 			  int size)
   13038 {
   13039   char *note_name = "LINUX";
   13040   return elfcore_write_note (abfd, buf, bufsiz,
   13041 			     note_name, NT_S390_GS_CB,
   13042 			     s390_gs_cb, size);
   13043 }
   13044 
   13045 char *
   13046 elfcore_write_s390_gs_bc (bfd *abfd,
   13047 			  char *buf,
   13048 			  int *bufsiz,
   13049 			  const void *s390_gs_bc,
   13050 			  int size)
   13051 {
   13052   char *note_name = "LINUX";
   13053   return elfcore_write_note (abfd, buf, bufsiz,
   13054 			     note_name, NT_S390_GS_BC,
   13055 			     s390_gs_bc, size);
   13056 }
   13057 
   13058 char *
   13059 elfcore_write_arm_vfp (bfd *abfd,
   13060 		       char *buf,
   13061 		       int *bufsiz,
   13062 		       const void *arm_vfp,
   13063 		       int size)
   13064 {
   13065   char *note_name = "LINUX";
   13066   return elfcore_write_note (abfd, buf, bufsiz,
   13067 			     note_name, NT_ARM_VFP, arm_vfp, size);
   13068 }
   13069 
   13070 char *
   13071 elfcore_write_aarch_tls (bfd *abfd,
   13072 		       char *buf,
   13073 		       int *bufsiz,
   13074 		       const void *aarch_tls,
   13075 		       int size)
   13076 {
   13077   char *note_name = "LINUX";
   13078   return elfcore_write_note (abfd, buf, bufsiz,
   13079 			     note_name, NT_ARM_TLS, aarch_tls, size);
   13080 }
   13081 
   13082 char *
   13083 elfcore_write_aarch_hw_break (bfd *abfd,
   13084 			    char *buf,
   13085 			    int *bufsiz,
   13086 			    const void *aarch_hw_break,
   13087 			    int size)
   13088 {
   13089   char *note_name = "LINUX";
   13090   return elfcore_write_note (abfd, buf, bufsiz,
   13091 			     note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
   13092 }
   13093 
   13094 char *
   13095 elfcore_write_aarch_hw_watch (bfd *abfd,
   13096 			    char *buf,
   13097 			    int *bufsiz,
   13098 			    const void *aarch_hw_watch,
   13099 			    int size)
   13100 {
   13101   char *note_name = "LINUX";
   13102   return elfcore_write_note (abfd, buf, bufsiz,
   13103 			     note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
   13104 }
   13105 
   13106 char *
   13107 elfcore_write_aarch_sve (bfd *abfd,
   13108 			 char *buf,
   13109 			 int *bufsiz,
   13110 			 const void *aarch_sve,
   13111 			 int size)
   13112 {
   13113   char *note_name = "LINUX";
   13114   return elfcore_write_note (abfd, buf, bufsiz,
   13115 			     note_name, NT_ARM_SVE, aarch_sve, size);
   13116 }
   13117 
   13118 char *
   13119 elfcore_write_aarch_pauth (bfd *abfd,
   13120 			   char *buf,
   13121 			   int *bufsiz,
   13122 			   const void *aarch_pauth,
   13123 			   int size)
   13124 {
   13125   char *note_name = "LINUX";
   13126   return elfcore_write_note (abfd, buf, bufsiz,
   13127 			     note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
   13128 }
   13129 
   13130 char *
   13131 elfcore_write_aarch_mte (bfd *abfd,
   13132 				      char *buf,
   13133 				      int *bufsiz,
   13134 				      const void *aarch_mte,
   13135 				      int size)
   13136 {
   13137   char *note_name = "LINUX";
   13138   return elfcore_write_note (abfd, buf, bufsiz,
   13139 			     note_name, NT_ARM_TAGGED_ADDR_CTRL,
   13140 			     aarch_mte,
   13141 			     size);
   13142 }
   13143 
   13144 char *
   13145 elfcore_write_aarch_ssve (bfd *abfd,
   13146 			  char *buf,
   13147 			  int *bufsiz,
   13148 			  const void *aarch_ssve,
   13149 			  int size)
   13150 {
   13151   char *note_name = "LINUX";
   13152   return elfcore_write_note (abfd, buf, bufsiz,
   13153 			     note_name, NT_ARM_SSVE,
   13154 			     aarch_ssve,
   13155 			     size);
   13156 }
   13157 
   13158 char *
   13159 elfcore_write_aarch_za (bfd *abfd,
   13160 			char *buf,
   13161 			int *bufsiz,
   13162 			const void *aarch_za,
   13163 			int size)
   13164 {
   13165   char *note_name = "LINUX";
   13166   return elfcore_write_note (abfd, buf, bufsiz,
   13167 			     note_name, NT_ARM_ZA,
   13168 			     aarch_za,
   13169 			     size);
   13170 }
   13171 
   13172 /* Write the buffer of zt register values in aarch_zt (length SIZE) into
   13173    the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
   13174    written into.  Return a pointer to the new start of the note buffer, to
   13175    replace BUF which may no longer be valid.  */
   13176 
   13177 char *
   13178 elfcore_write_aarch_zt (bfd *abfd,
   13179 			char *buf,
   13180 			int *bufsiz,
   13181 			const void *aarch_zt,
   13182 			int size)
   13183 {
   13184   char *note_name = "LINUX";
   13185   return elfcore_write_note (abfd, buf, bufsiz,
   13186 			     note_name, NT_ARM_ZT,
   13187 			     aarch_zt,
   13188 			     size);
   13189 }
   13190 
   13191 char *
   13192 elfcore_write_arc_v2 (bfd *abfd,
   13193 		      char *buf,
   13194 		      int *bufsiz,
   13195 		      const void *arc_v2,
   13196 		      int size)
   13197 {
   13198   char *note_name = "LINUX";
   13199   return elfcore_write_note (abfd, buf, bufsiz,
   13200 			     note_name, NT_ARC_V2, arc_v2, size);
   13201 }
   13202 
   13203 char *
   13204 elfcore_write_loongarch_cpucfg (bfd *abfd,
   13205 				char *buf,
   13206 				int *bufsiz,
   13207 				const void *loongarch_cpucfg,
   13208 				int size)
   13209 {
   13210   char *note_name = "LINUX";
   13211   return elfcore_write_note (abfd, buf, bufsiz,
   13212 			     note_name, NT_LARCH_CPUCFG,
   13213 			     loongarch_cpucfg, size);
   13214 }
   13215 
   13216 char *
   13217 elfcore_write_loongarch_lbt (bfd *abfd,
   13218 			     char *buf,
   13219 			     int *bufsiz,
   13220 			     const void *loongarch_lbt,
   13221 			     int size)
   13222 {
   13223   char *note_name = "LINUX";
   13224   return elfcore_write_note (abfd, buf, bufsiz,
   13225 			     note_name, NT_LARCH_LBT, loongarch_lbt, size);
   13226 }
   13227 
   13228 char *
   13229 elfcore_write_loongarch_lsx (bfd *abfd,
   13230 			     char *buf,
   13231 			     int *bufsiz,
   13232 			     const void *loongarch_lsx,
   13233 			     int size)
   13234 {
   13235   char *note_name = "LINUX";
   13236   return elfcore_write_note (abfd, buf, bufsiz,
   13237 			     note_name, NT_LARCH_LSX, loongarch_lsx, size);
   13238 }
   13239 
   13240 char *
   13241 elfcore_write_loongarch_lasx (bfd *abfd,
   13242 			      char *buf,
   13243 			      int *bufsiz,
   13244 			      const void *loongarch_lasx,
   13245 			      int size)
   13246 {
   13247   char *note_name = "LINUX";
   13248   return elfcore_write_note (abfd, buf, bufsiz,
   13249 			     note_name, NT_LARCH_LASX, loongarch_lasx, size);
   13250 }
   13251 
   13252 /* Write the buffer of csr values in CSRS (length SIZE) into the note
   13253    buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
   13254    written into.  Return a pointer to the new start of the note buffer, to
   13255    replace BUF which may no longer be valid.  */
   13256 
   13257 char *
   13258 elfcore_write_riscv_csr (bfd *abfd,
   13259 			 char *buf,
   13260 			 int *bufsiz,
   13261 			 const void *csrs,
   13262 			 int size)
   13263 {
   13264   const char *note_name = "GDB";
   13265   return elfcore_write_note (abfd, buf, bufsiz,
   13266 			     note_name, NT_RISCV_CSR, csrs, size);
   13267 }
   13268 
   13269 /* Write the target description (a string) pointed to by TDESC, length
   13270    SIZE, into the note buffer BUF, and update *BUFSIZ.  ABFD is the bfd the
   13271    note is being written into.  Return a pointer to the new start of the
   13272    note buffer, to replace BUF which may no longer be valid.  */
   13273 
   13274 char *
   13275 elfcore_write_gdb_tdesc (bfd *abfd,
   13276 			 char *buf,
   13277 			 int *bufsiz,
   13278 			 const void *tdesc,
   13279 			 int size)
   13280 {
   13281   const char *note_name = "GDB";
   13282   return elfcore_write_note (abfd, buf, bufsiz,
   13283 			     note_name, NT_GDB_TDESC, tdesc, size);
   13284 }
   13285 
   13286 char *
   13287 elfcore_write_register_note (bfd *abfd,
   13288 			     char *buf,
   13289 			     int *bufsiz,
   13290 			     const char *section,
   13291 			     const void *data,
   13292 			     int size)
   13293 {
   13294   if (strcmp (section, ".reg2") == 0)
   13295     return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
   13296   if (strcmp (section, ".reg-xfp") == 0)
   13297     return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
   13298   if (strcmp (section, ".reg-xstate") == 0)
   13299     return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
   13300   if (strcmp (section, ".reg-x86-segbases") == 0)
   13301     return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
   13302   if (strcmp (section, ".reg-ppc-vmx") == 0)
   13303     return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
   13304   if (strcmp (section, ".reg-ppc-vsx") == 0)
   13305     return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
   13306   if (strcmp (section, ".reg-ppc-tar") == 0)
   13307     return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
   13308   if (strcmp (section, ".reg-ppc-ppr") == 0)
   13309     return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
   13310   if (strcmp (section, ".reg-ppc-dscr") == 0)
   13311     return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
   13312   if (strcmp (section, ".reg-ppc-ebb") == 0)
   13313     return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
   13314   if (strcmp (section, ".reg-ppc-pmu") == 0)
   13315     return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
   13316   if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
   13317     return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
   13318   if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
   13319     return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
   13320   if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
   13321     return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
   13322   if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
   13323     return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
   13324   if (strcmp (section, ".reg-ppc-tm-spr") == 0)
   13325     return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
   13326   if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
   13327     return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
   13328   if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
   13329     return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
   13330   if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
   13331     return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
   13332   if (strcmp (section, ".reg-s390-high-gprs") == 0)
   13333     return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
   13334   if (strcmp (section, ".reg-s390-timer") == 0)
   13335     return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
   13336   if (strcmp (section, ".reg-s390-todcmp") == 0)
   13337     return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
   13338   if (strcmp (section, ".reg-s390-todpreg") == 0)
   13339     return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
   13340   if (strcmp (section, ".reg-s390-ctrs") == 0)
   13341     return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
   13342   if (strcmp (section, ".reg-s390-prefix") == 0)
   13343     return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
   13344   if (strcmp (section, ".reg-s390-last-break") == 0)
   13345     return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
   13346   if (strcmp (section, ".reg-s390-system-call") == 0)
   13347     return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
   13348   if (strcmp (section, ".reg-s390-tdb") == 0)
   13349     return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
   13350   if (strcmp (section, ".reg-s390-vxrs-low") == 0)
   13351     return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
   13352   if (strcmp (section, ".reg-s390-vxrs-high") == 0)
   13353     return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
   13354   if (strcmp (section, ".reg-s390-gs-cb") == 0)
   13355     return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
   13356   if (strcmp (section, ".reg-s390-gs-bc") == 0)
   13357     return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
   13358   if (strcmp (section, ".reg-arm-vfp") == 0)
   13359     return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
   13360   if (strcmp (section, ".reg-aarch-tls") == 0)
   13361     return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
   13362   if (strcmp (section, ".reg-aarch-hw-break") == 0)
   13363     return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
   13364   if (strcmp (section, ".reg-aarch-hw-watch") == 0)
   13365     return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
   13366   if (strcmp (section, ".reg-aarch-sve") == 0)
   13367     return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
   13368   if (strcmp (section, ".reg-aarch-pauth") == 0)
   13369     return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
   13370   if (strcmp (section, ".reg-aarch-mte") == 0)
   13371     return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
   13372   if (strcmp (section, ".reg-aarch-ssve") == 0)
   13373     return elfcore_write_aarch_ssve (abfd, buf, bufsiz, data, size);
   13374   if (strcmp (section, ".reg-aarch-za") == 0)
   13375     return elfcore_write_aarch_za (abfd, buf, bufsiz, data, size);
   13376   if (strcmp (section, ".reg-aarch-zt") == 0)
   13377     return elfcore_write_aarch_zt (abfd, buf, bufsiz, data, size);
   13378   if (strcmp (section, ".reg-arc-v2") == 0)
   13379     return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
   13380   if (strcmp (section, ".gdb-tdesc") == 0)
   13381     return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
   13382   if (strcmp (section, ".reg-riscv-csr") == 0)
   13383     return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
   13384   if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
   13385     return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
   13386   if (strcmp (section, ".reg-loongarch-lbt") == 0)
   13387     return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
   13388   if (strcmp (section, ".reg-loongarch-lsx") == 0)
   13389     return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
   13390   if (strcmp (section, ".reg-loongarch-lasx") == 0)
   13391     return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
   13392   return NULL;
   13393 }
   13394 
   13395 char *
   13396 elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
   13397 			 const void *buf, int bufsiz)
   13398 {
   13399   return elfcore_write_note (obfd, note_data, note_size,
   13400 			     "CORE", NT_FILE, buf, bufsiz);
   13401 }
   13402 
   13403 static bool
   13404 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
   13405 		 size_t align)
   13406 {
   13407   char *p;
   13408 
   13409   /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
   13410      gABI specifies that PT_NOTE alignment should be aligned to 4
   13411      bytes for 32-bit objects and to 8 bytes for 64-bit objects.  If
   13412      align is less than 4, we use 4 byte alignment.   */
   13413   if (align < 4)
   13414     align = 4;
   13415   if (align != 4 && align != 8)
   13416     return false;
   13417 
   13418   p = buf;
   13419   while (p < buf + size)
   13420     {
   13421       Elf_External_Note *xnp = (Elf_External_Note *) p;
   13422       Elf_Internal_Note in;
   13423 
   13424       if (offsetof (Elf_External_Note, name) > buf - p + size)
   13425 	return false;
   13426 
   13427       in.type = H_GET_32 (abfd, xnp->type);
   13428 
   13429       in.namesz = H_GET_32 (abfd, xnp->namesz);
   13430       in.namedata = xnp->name;
   13431       if (in.namesz > buf - in.namedata + size)
   13432 	return false;
   13433 
   13434       in.descsz = H_GET_32 (abfd, xnp->descsz);
   13435       in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
   13436       in.descpos = offset + (in.descdata - buf);
   13437       if (in.descsz != 0
   13438 	  && (in.descdata >= buf + size
   13439 	      || in.descsz > buf - in.descdata + size))
   13440 	return false;
   13441 
   13442       switch (bfd_get_format (abfd))
   13443 	{
   13444 	default:
   13445 	  return true;
   13446 
   13447 	case bfd_core:
   13448 	  {
   13449 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
   13450 	    struct
   13451 	    {
   13452 	      const char * string;
   13453 	      size_t len;
   13454 	      bool (*func) (bfd *, Elf_Internal_Note *);
   13455 	    }
   13456 	    grokers[] =
   13457 	    {
   13458 	      GROKER_ELEMENT ("", elfcore_grok_note),
   13459 	      GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
   13460 	      GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
   13461 	      GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
   13462 	      GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
   13463 	      GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
   13464 	      GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
   13465 	      GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
   13466 	    };
   13467 #undef GROKER_ELEMENT
   13468 	    int i;
   13469 
   13470 	    for (i = ARRAY_SIZE (grokers); i--;)
   13471 	      {
   13472 		if (in.namesz >= grokers[i].len
   13473 		    && strncmp (in.namedata, grokers[i].string,
   13474 				grokers[i].len) == 0)
   13475 		  {
   13476 		    if (! grokers[i].func (abfd, & in))
   13477 		      return false;
   13478 		    break;
   13479 		  }
   13480 	      }
   13481 	    break;
   13482 	  }
   13483 
   13484 	case bfd_object:
   13485 	  if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
   13486 	    {
   13487 	      if (! elfobj_grok_gnu_note (abfd, &in))
   13488 		return false;
   13489 	    }
   13490 	  else if (in.namesz == sizeof "stapsdt"
   13491 		   && strcmp (in.namedata, "stapsdt") == 0)
   13492 	    {
   13493 	      if (! elfobj_grok_stapsdt_note (abfd, &in))
   13494 		return false;
   13495 	    }
   13496 	  break;
   13497 	}
   13498 
   13499       p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
   13500     }
   13501 
   13502   return true;
   13503 }
   13504 
   13505 bool
   13506 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
   13507 		size_t align)
   13508 {
   13509   char *buf;
   13510 
   13511   if (size == 0 || (size + 1) == 0)
   13512     return true;
   13513 
   13514   if (bfd_seek (abfd, offset, SEEK_SET) != 0)
   13515     return false;
   13516 
   13517   buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
   13518   if (buf == NULL)
   13519     return false;
   13520 
   13521   /* PR 17512: file: ec08f814
   13522      0-termintate the buffer so that string searches will not overflow.  */
   13523   buf[size] = 0;
   13524 
   13525   if (!elf_parse_notes (abfd, buf, size, offset, align))
   13526     {
   13527       free (buf);
   13528       return false;
   13529     }
   13530 
   13531   free (buf);
   13532   return true;
   13533 }
   13534 
   13535 /* Providing external access to the ELF program header table.  */
   13537 
   13538 /* Return an upper bound on the number of bytes required to store a
   13539    copy of ABFD's program header table entries.  Return -1 if an error
   13540    occurs; bfd_get_error will return an appropriate code.  */
   13541 
   13542 long
   13543 bfd_get_elf_phdr_upper_bound (bfd *abfd)
   13544 {
   13545   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   13546     {
   13547       bfd_set_error (bfd_error_wrong_format);
   13548       return -1;
   13549     }
   13550 
   13551   return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
   13552 }
   13553 
   13554 /* Copy ABFD's program header table entries to *PHDRS.  The entries
   13555    will be stored as an array of Elf_Internal_Phdr structures, as
   13556    defined in include/elf/internal.h.  To find out how large the
   13557    buffer needs to be, call bfd_get_elf_phdr_upper_bound.
   13558 
   13559    Return the number of program header table entries read, or -1 if an
   13560    error occurs; bfd_get_error will return an appropriate code.  */
   13561 
   13562 int
   13563 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
   13564 {
   13565   int num_phdrs;
   13566 
   13567   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   13568     {
   13569       bfd_set_error (bfd_error_wrong_format);
   13570       return -1;
   13571     }
   13572 
   13573   num_phdrs = elf_elfheader (abfd)->e_phnum;
   13574   if (num_phdrs != 0)
   13575     memcpy (phdrs, elf_tdata (abfd)->phdr,
   13576 	    num_phdrs * sizeof (Elf_Internal_Phdr));
   13577 
   13578   return num_phdrs;
   13579 }
   13580 
   13581 enum elf_reloc_type_class
   13582 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   13583 			   const asection *rel_sec ATTRIBUTE_UNUSED,
   13584 			   const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
   13585 {
   13586   return reloc_class_normal;
   13587 }
   13588 
   13589 /* For RELA architectures, return the relocation value for a
   13590    relocation against a local symbol.  */
   13591 
   13592 bfd_vma
   13593 _bfd_elf_rela_local_sym (bfd *abfd,
   13594 			 Elf_Internal_Sym *sym,
   13595 			 asection **psec,
   13596 			 Elf_Internal_Rela *rel)
   13597 {
   13598   asection *sec = *psec;
   13599   bfd_vma relocation;
   13600 
   13601   relocation = (sec->output_section->vma
   13602 		+ sec->output_offset
   13603 		+ sym->st_value);
   13604   if ((sec->flags & SEC_MERGE)
   13605       && ELF_ST_TYPE (sym->st_info) == STT_SECTION
   13606       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
   13607     {
   13608       rel->r_addend =
   13609 	_bfd_merged_section_offset (abfd, psec,
   13610 				    elf_section_data (sec)->sec_info,
   13611 				    sym->st_value + rel->r_addend);
   13612       if (sec != *psec)
   13613 	{
   13614 	  /* If we have changed the section, and our original section is
   13615 	     marked with SEC_EXCLUDE, it means that the original
   13616 	     SEC_MERGE section has been completely subsumed in some
   13617 	     other SEC_MERGE section.  In this case, we need to leave
   13618 	     some info around for --emit-relocs.  */
   13619 	  if ((sec->flags & SEC_EXCLUDE) != 0)
   13620 	    sec->kept_section = *psec;
   13621 	  sec = *psec;
   13622 	}
   13623       rel->r_addend -= relocation;
   13624       rel->r_addend += sec->output_section->vma + sec->output_offset;
   13625     }
   13626   return relocation;
   13627 }
   13628 
   13629 bfd_vma
   13630 _bfd_elf_rel_local_sym (bfd *abfd,
   13631 			Elf_Internal_Sym *sym,
   13632 			asection **psec,
   13633 			bfd_vma addend)
   13634 {
   13635   asection *sec = *psec;
   13636 
   13637   if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
   13638     return sym->st_value + addend;
   13639 
   13640   return _bfd_merged_section_offset (abfd, psec,
   13641 				     elf_section_data (sec)->sec_info,
   13642 				     sym->st_value + addend);
   13643 }
   13644 
   13645 /* Adjust an address within a section.  Given OFFSET within SEC, return
   13646    the new offset within the section, based upon changes made to the
   13647    section.  Returns -1 if the offset is now invalid.
   13648    The offset (in abnd out) is in target sized bytes, however big a
   13649    byte may be.  */
   13650 
   13651 bfd_vma
   13652 _bfd_elf_section_offset (bfd *abfd,
   13653 			 struct bfd_link_info *info,
   13654 			 asection *sec,
   13655 			 bfd_vma offset)
   13656 {
   13657   switch (sec->sec_info_type)
   13658     {
   13659     case SEC_INFO_TYPE_STABS:
   13660       return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
   13661 				       offset);
   13662     case SEC_INFO_TYPE_EH_FRAME:
   13663       return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
   13664 
   13665     default:
   13666       if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
   13667 	{
   13668 	  /* Reverse the offset.  */
   13669 	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   13670 	  bfd_size_type address_size = bed->s->arch_size / 8;
   13671 
   13672 	  /* address_size and sec->size are in octets.  Convert
   13673 	     to bytes before subtracting the original offset.  */
   13674 	  offset = ((sec->size - address_size)
   13675 		    / bfd_octets_per_byte (abfd, sec) - offset);
   13676 	}
   13677       return offset;
   13678     }
   13679 }
   13680 
   13681 long
   13683 _bfd_elf_get_synthetic_symtab (bfd *abfd,
   13684 			       long symcount ATTRIBUTE_UNUSED,
   13685 			       asymbol **syms ATTRIBUTE_UNUSED,
   13686 			       long dynsymcount,
   13687 			       asymbol **dynsyms,
   13688 			       asymbol **ret)
   13689 {
   13690   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   13691   asection *relplt;
   13692   asymbol *s;
   13693   const char *relplt_name;
   13694   bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
   13695   arelent *p;
   13696   long count, i, n;
   13697   size_t size;
   13698   Elf_Internal_Shdr *hdr;
   13699   char *names;
   13700   asection *plt;
   13701 
   13702   *ret = NULL;
   13703 
   13704   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
   13705     return 0;
   13706 
   13707   if (dynsymcount <= 0)
   13708     return 0;
   13709 
   13710   if (!bed->plt_sym_val)
   13711     return 0;
   13712 
   13713   relplt_name = bed->relplt_name;
   13714   if (relplt_name == NULL)
   13715     relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
   13716   relplt = bfd_get_section_by_name (abfd, relplt_name);
   13717   if (relplt == NULL)
   13718     return 0;
   13719 
   13720   hdr = &elf_section_data (relplt)->this_hdr;
   13721   if (hdr->sh_link != elf_dynsymtab (abfd)
   13722       || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
   13723     return 0;
   13724 
   13725   plt = bfd_get_section_by_name (abfd, ".plt");
   13726   if (plt == NULL)
   13727     return 0;
   13728 
   13729   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   13730   if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
   13731     return -1;
   13732 
   13733   count = NUM_SHDR_ENTRIES (hdr);
   13734   size = count * sizeof (asymbol);
   13735   p = relplt->relocation;
   13736   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   13737     {
   13738       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
   13739       if (p->addend != 0)
   13740 	{
   13741 #ifdef BFD64
   13742 	  size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
   13743 #else
   13744 	  size += sizeof ("+0x") - 1 + 8;
   13745 #endif
   13746 	}
   13747     }
   13748 
   13749   s = *ret = (asymbol *) bfd_malloc (size);
   13750   if (s == NULL)
   13751     return -1;
   13752 
   13753   names = (char *) (s + count);
   13754   p = relplt->relocation;
   13755   n = 0;
   13756   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   13757     {
   13758       size_t len;
   13759       bfd_vma addr;
   13760 
   13761       addr = bed->plt_sym_val (i, plt, p);
   13762       if (addr == (bfd_vma) -1)
   13763 	continue;
   13764 
   13765       *s = **p->sym_ptr_ptr;
   13766       /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   13767 	 we are defining a symbol, ensure one of them is set.  */
   13768       if ((s->flags & BSF_LOCAL) == 0)
   13769 	s->flags |= BSF_GLOBAL;
   13770       s->flags |= BSF_SYNTHETIC;
   13771       s->section = plt;
   13772       s->value = addr - plt->vma;
   13773       s->name = names;
   13774       s->udata.p = NULL;
   13775       len = strlen ((*p->sym_ptr_ptr)->name);
   13776       memcpy (names, (*p->sym_ptr_ptr)->name, len);
   13777       names += len;
   13778       if (p->addend != 0)
   13779 	{
   13780 	  char buf[30], *a;
   13781 
   13782 	  memcpy (names, "+0x", sizeof ("+0x") - 1);
   13783 	  names += sizeof ("+0x") - 1;
   13784 	  bfd_sprintf_vma (abfd, buf, p->addend);
   13785 	  for (a = buf; *a == '0'; ++a)
   13786 	    ;
   13787 	  len = strlen (a);
   13788 	  memcpy (names, a, len);
   13789 	  names += len;
   13790 	}
   13791       memcpy (names, "@plt", sizeof ("@plt"));
   13792       names += sizeof ("@plt");
   13793       ++s, ++n;
   13794     }
   13795 
   13796   return n;
   13797 }
   13798 
   13799 /* It is only used by x86-64 so far.
   13800    ??? This repeats *COM* id of zero.  sec->id is supposed to be unique,
   13801    but current usage would allow all of _bfd_std_section to be zero.  */
   13802 static const asymbol lcomm_sym
   13803   = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
   13804 asection _bfd_elf_large_com_section
   13805   = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
   13806 		      "LARGE_COMMON", 0, SEC_IS_COMMON);
   13807 
   13808 bool
   13809 _bfd_elf_final_write_processing (bfd *abfd)
   13810 {
   13811   Elf_Internal_Ehdr *i_ehdrp;	/* ELF file header, internal form.  */
   13812 
   13813   i_ehdrp = elf_elfheader (abfd);
   13814 
   13815   if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
   13816     i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
   13817 
   13818   /* Set the osabi field to ELFOSABI_GNU if the binary contains
   13819      SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
   13820      or STB_GNU_UNIQUE binding.  */
   13821   if (elf_tdata (abfd)->has_gnu_osabi != 0)
   13822     {
   13823       if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
   13824 	i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
   13825       else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
   13826 	       && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
   13827 	{
   13828 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
   13829 	    _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
   13830 				  "and FreeBSD targets"));
   13831 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
   13832 	    _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
   13833 				  "only by GNU and FreeBSD targets"));
   13834 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
   13835 	    _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
   13836 				  "only by GNU and FreeBSD targets"));
   13837 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
   13838 	    _bfd_error_handler (_("GNU_RETAIN section is supported "
   13839 				  "only by GNU and FreeBSD targets"));
   13840 	  bfd_set_error (bfd_error_sorry);
   13841 	  return false;
   13842 	}
   13843     }
   13844   return true;
   13845 }
   13846 
   13847 
   13848 /* Return TRUE for ELF symbol types that represent functions.
   13849    This is the default version of this function, which is sufficient for
   13850    most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
   13851 
   13852 bool
   13853 _bfd_elf_is_function_type (unsigned int type)
   13854 {
   13855   return (type == STT_FUNC
   13856 	  || type == STT_GNU_IFUNC);
   13857 }
   13858 
   13859 /* If the ELF symbol SYM might be a function in SEC, return the
   13860    function size and set *CODE_OFF to the function's entry point,
   13861    otherwise return zero.  */
   13862 
   13863 bfd_size_type
   13864 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
   13865 			     bfd_vma *code_off)
   13866 {
   13867   bfd_size_type size;
   13868   elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
   13869 
   13870   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
   13871 		     | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
   13872       || sym->section != sec)
   13873     return 0;
   13874 
   13875   size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
   13876 
   13877   /* In theory we should check that the symbol's type satisfies
   13878      _bfd_elf_is_function_type(), but there are some function-like
   13879      symbols which would fail this test.  (eg _start).  Instead
   13880      we check for hidden, local, notype symbols with zero size.
   13881      This type of symbol is generated by the annobin plugin for gcc
   13882      and clang, and should not be considered to be a function symbol.  */
   13883   if (size == 0
   13884       && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
   13885       && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
   13886       && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
   13887     return 0;
   13888 
   13889   *code_off = sym->value;
   13890   /* Do not return 0 for the function's size.  */
   13891   return size ? size : 1;
   13892 }
   13893 
   13894 /* Set to non-zero to enable some debug messages.  */
   13895 #define DEBUG_SECONDARY_RELOCS	 0
   13896 
   13897 /* An internal-to-the-bfd-library only section type
   13898    used to indicate a cached secondary reloc section.  */
   13899 #define SHT_SECONDARY_RELOC	 (SHT_LOOS + SHT_RELA)
   13900 
   13901 /* Create a BFD section to hold a secondary reloc section.  */
   13902 
   13903 bool
   13904 _bfd_elf_init_secondary_reloc_section (bfd * abfd,
   13905 				       Elf_Internal_Shdr *hdr,
   13906 				       const char * name,
   13907 				       unsigned int shindex)
   13908 {
   13909   /* We only support RELA secondary relocs.  */
   13910   if (hdr->sh_type != SHT_RELA)
   13911     return false;
   13912 
   13913 #if DEBUG_SECONDARY_RELOCS
   13914   fprintf (stderr, "secondary reloc section %s encountered\n", name);
   13915 #endif
   13916   hdr->sh_type = SHT_SECONDARY_RELOC;
   13917   return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   13918 }
   13919 
   13920 /* Read in any secondary relocs associated with SEC.  */
   13921 
   13922 bool
   13923 _bfd_elf_slurp_secondary_reloc_section (bfd *       abfd,
   13924 					asection *  sec,
   13925 					asymbol **  symbols,
   13926 					bool dynamic)
   13927 {
   13928   const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
   13929   asection * relsec;
   13930   bool result = true;
   13931   bfd_vma (*r_sym) (bfd_vma);
   13932   ufile_ptr filesize;
   13933 
   13934 #if BFD_DEFAULT_TARGET_SIZE > 32
   13935   if (bfd_arch_bits_per_address (abfd) != 32)
   13936     r_sym = elf64_r_sym;
   13937   else
   13938 #endif
   13939     r_sym = elf32_r_sym;
   13940 
   13941   if (!elf_section_data (sec)->has_secondary_relocs)
   13942     return true;
   13943 
   13944   /* Discover if there are any secondary reloc sections
   13945      associated with SEC.  */
   13946   filesize = bfd_get_file_size (abfd);
   13947   for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
   13948     {
   13949       Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
   13950 
   13951       if (hdr->sh_type == SHT_SECONDARY_RELOC
   13952 	  && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
   13953 	  && (hdr->sh_entsize == ebd->s->sizeof_rel
   13954 	      || hdr->sh_entsize == ebd->s->sizeof_rela))
   13955 	{
   13956 	  bfd_byte * native_relocs;
   13957 	  bfd_byte * native_reloc;
   13958 	  arelent * internal_relocs;
   13959 	  arelent * internal_reloc;
   13960 	  size_t i;
   13961 	  unsigned int entsize;
   13962 	  unsigned int symcount;
   13963 	  bfd_size_type reloc_count;
   13964 	  size_t amt;
   13965 
   13966 	  if (ebd->elf_info_to_howto == NULL)
   13967 	    return false;
   13968 
   13969 #if DEBUG_SECONDARY_RELOCS
   13970 	  fprintf (stderr, "read secondary relocs for %s from %s\n",
   13971 		   sec->name, relsec->name);
   13972 #endif
   13973 	  entsize = hdr->sh_entsize;
   13974 
   13975 	  if (filesize != 0
   13976 	      && ((ufile_ptr) hdr->sh_offset > filesize
   13977 		  || hdr->sh_size > filesize - hdr->sh_offset))
   13978 	    {
   13979 	      bfd_set_error (bfd_error_file_truncated);
   13980 	      result = false;
   13981 	      continue;
   13982 	    }
   13983 
   13984 	  native_relocs = bfd_malloc (hdr->sh_size);
   13985 	  if (native_relocs == NULL)
   13986 	    {
   13987 	      result = false;
   13988 	      continue;
   13989 	    }
   13990 
   13991 	  reloc_count = NUM_SHDR_ENTRIES (hdr);
   13992 	  if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
   13993 	    {
   13994 	      free (native_relocs);
   13995 	      bfd_set_error (bfd_error_file_too_big);
   13996 	      result = false;
   13997 	      continue;
   13998 	    }
   13999 
   14000 	  internal_relocs = (arelent *) bfd_alloc (abfd, amt);
   14001 	  if (internal_relocs == NULL)
   14002 	    {
   14003 	      free (native_relocs);
   14004 	      result = false;
   14005 	      continue;
   14006 	    }
   14007 
   14008 	  if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   14009 	      || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size)
   14010 	    {
   14011 	      free (native_relocs);
   14012 	      /* The internal_relocs will be freed when
   14013 		 the memory for the bfd is released.  */
   14014 	      result = false;
   14015 	      continue;
   14016 	    }
   14017 
   14018 	  if (dynamic)
   14019 	    symcount = bfd_get_dynamic_symcount (abfd);
   14020 	  else
   14021 	    symcount = bfd_get_symcount (abfd);
   14022 
   14023 	  for (i = 0, internal_reloc = internal_relocs,
   14024 		 native_reloc = native_relocs;
   14025 	       i < reloc_count;
   14026 	       i++, internal_reloc++, native_reloc += entsize)
   14027 	    {
   14028 	      bool res;
   14029 	      Elf_Internal_Rela rela;
   14030 
   14031 	      if (entsize == ebd->s->sizeof_rel)
   14032 		ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
   14033 	      else /* entsize == ebd->s->sizeof_rela */
   14034 		ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
   14035 
   14036 	      /* The address of an ELF reloc is section relative for an object
   14037 		 file, and absolute for an executable file or shared library.
   14038 		 The address of a normal BFD reloc is always section relative,
   14039 		 and the address of a dynamic reloc is absolute..  */
   14040 	      if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
   14041 		internal_reloc->address = rela.r_offset;
   14042 	      else
   14043 		internal_reloc->address = rela.r_offset - sec->vma;
   14044 
   14045 	      if (r_sym (rela.r_info) == STN_UNDEF)
   14046 		{
   14047 		  /* FIXME: This and the error case below mean that we
   14048 		     have a symbol on relocs that is not elf_symbol_type.  */
   14049 		  internal_reloc->sym_ptr_ptr =
   14050 		    bfd_abs_section_ptr->symbol_ptr_ptr;
   14051 		}
   14052 	      else if (r_sym (rela.r_info) > symcount)
   14053 		{
   14054 		  _bfd_error_handler
   14055 		    /* xgettext:c-format */
   14056 		    (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
   14057 		     abfd, sec, i, (long) r_sym (rela.r_info));
   14058 		  bfd_set_error (bfd_error_bad_value);
   14059 		  internal_reloc->sym_ptr_ptr =
   14060 		    bfd_abs_section_ptr->symbol_ptr_ptr;
   14061 		  result = false;
   14062 		}
   14063 	      else
   14064 		{
   14065 		  asymbol **ps;
   14066 
   14067 		  ps = symbols + r_sym (rela.r_info) - 1;
   14068 		  internal_reloc->sym_ptr_ptr = ps;
   14069 		  /* Make sure that this symbol is not removed by strip.  */
   14070 		  (*ps)->flags |= BSF_KEEP;
   14071 		}
   14072 
   14073 	      internal_reloc->addend = rela.r_addend;
   14074 
   14075 	      res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
   14076 	      if (! res || internal_reloc->howto == NULL)
   14077 		{
   14078 #if DEBUG_SECONDARY_RELOCS
   14079 		  fprintf (stderr,
   14080 			   "there is no howto associated with reloc %lx\n",
   14081 			   rela.r_info);
   14082 #endif
   14083 		  result = false;
   14084 		}
   14085 	    }
   14086 
   14087 	  free (native_relocs);
   14088 	  /* Store the internal relocs.  */
   14089 	  elf_section_data (relsec)->sec_info = internal_relocs;
   14090 	}
   14091     }
   14092 
   14093   return result;
   14094 }
   14095 
   14096 /* Set the ELF section header fields of an output secondary reloc section.  */
   14097 
   14098 bool
   14099 _bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
   14100 				      bfd *obfd ATTRIBUTE_UNUSED,
   14101 				      const Elf_Internal_Shdr *isection,
   14102 				      Elf_Internal_Shdr *osection)
   14103 {
   14104   asection * isec;
   14105   asection * osec;
   14106   struct bfd_elf_section_data * esd;
   14107 
   14108   if (isection == NULL)
   14109     return false;
   14110 
   14111   if (isection->sh_type != SHT_SECONDARY_RELOC)
   14112     return true;
   14113 
   14114   isec = isection->bfd_section;
   14115   if (isec == NULL)
   14116     return false;
   14117 
   14118   osec = osection->bfd_section;
   14119   if (osec == NULL)
   14120     return false;
   14121 
   14122   esd = elf_section_data (osec);
   14123   BFD_ASSERT (esd->sec_info == NULL);
   14124   esd->sec_info = elf_section_data (isec)->sec_info;
   14125   osection->sh_type = SHT_RELA;
   14126   osection->sh_link = elf_onesymtab (obfd);
   14127   if (osection->sh_link == 0)
   14128     {
   14129       /* There is no symbol table - we are hosed...  */
   14130       _bfd_error_handler
   14131 	/* xgettext:c-format */
   14132 	(_("%pB(%pA): link section cannot be set"
   14133 	   " because the output file does not have a symbol table"),
   14134 	obfd, osec);
   14135       bfd_set_error (bfd_error_bad_value);
   14136       return false;
   14137     }
   14138 
   14139   /* Find the output section that corresponds to the isection's
   14140      sh_info link.  */
   14141   if (isection->sh_info == 0
   14142       || isection->sh_info >= elf_numsections (ibfd))
   14143     {
   14144       _bfd_error_handler
   14145 	/* xgettext:c-format */
   14146 	(_("%pB(%pA): info section index is invalid"),
   14147 	obfd, osec);
   14148       bfd_set_error (bfd_error_bad_value);
   14149       return false;
   14150     }
   14151 
   14152   isection = elf_elfsections (ibfd)[isection->sh_info];
   14153 
   14154   if (isection == NULL
   14155       || isection->bfd_section == NULL
   14156       || isection->bfd_section->output_section == NULL)
   14157     {
   14158       _bfd_error_handler
   14159 	/* xgettext:c-format */
   14160 	(_("%pB(%pA): info section index cannot be set"
   14161 	   " because the section is not in the output"),
   14162 	obfd, osec);
   14163       bfd_set_error (bfd_error_bad_value);
   14164       return false;
   14165     }
   14166 
   14167   esd = elf_section_data (isection->bfd_section->output_section);
   14168   BFD_ASSERT (esd != NULL);
   14169   osection->sh_info = esd->this_idx;
   14170   esd->has_secondary_relocs = true;
   14171 #if DEBUG_SECONDARY_RELOCS
   14172   fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
   14173 	   osec->name, osection->sh_link, osection->sh_info);
   14174   fprintf (stderr, "mark section %s as having secondary relocs\n",
   14175 	   bfd_section_name (isection->bfd_section->output_section));
   14176 #endif
   14177 
   14178   return true;
   14179 }
   14180 
   14181 /* Write out a secondary reloc section.
   14182 
   14183    FIXME: Currently this function can result in a serious performance penalty
   14184    for files with secondary relocs and lots of sections.  The proper way to
   14185    fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
   14186    relocs together and then to have this function just walk that chain.  */
   14187 
   14188 bool
   14189 _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
   14190 {
   14191   const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
   14192   bfd_vma addr_offset;
   14193   asection * relsec;
   14194   bfd_vma (*r_info) (bfd_vma, bfd_vma);
   14195   bool result = true;
   14196 
   14197   if (sec == NULL)
   14198     return false;
   14199 
   14200 #if BFD_DEFAULT_TARGET_SIZE > 32
   14201   if (bfd_arch_bits_per_address (abfd) != 32)
   14202     r_info = elf64_r_info;
   14203   else
   14204 #endif
   14205     r_info = elf32_r_info;
   14206 
   14207   /* The address of an ELF reloc is section relative for an object
   14208      file, and absolute for an executable file or shared library.
   14209      The address of a BFD reloc is always section relative.  */
   14210   addr_offset = 0;
   14211   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
   14212     addr_offset = sec->vma;
   14213 
   14214   /* Discover if there are any secondary reloc sections
   14215      associated with SEC.  */
   14216   for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
   14217     {
   14218       const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
   14219       Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
   14220 
   14221       if (hdr->sh_type == SHT_RELA
   14222 	  && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
   14223 	{
   14224 	  asymbol *    last_sym;
   14225 	  int          last_sym_idx;
   14226 	  size_t       reloc_count;
   14227 	  size_t       idx;
   14228 	  bfd_size_type entsize;
   14229 	  arelent *    src_irel;
   14230 	  bfd_byte *   dst_rela;
   14231 
   14232 	  if (hdr->contents != NULL)
   14233 	    {
   14234 	      _bfd_error_handler
   14235 		/* xgettext:c-format */
   14236 		(_("%pB(%pA): error: secondary reloc section processed twice"),
   14237 		 abfd, relsec);
   14238 	      bfd_set_error (bfd_error_bad_value);
   14239 	      result = false;
   14240 	      continue;
   14241 	    }
   14242 
   14243 	  entsize = hdr->sh_entsize;
   14244 	  if (entsize == 0)
   14245 	    {
   14246 	      _bfd_error_handler
   14247 		/* xgettext:c-format */
   14248 		(_("%pB(%pA): error: secondary reloc section"
   14249 		   " has zero sized entries"),
   14250 		 abfd, relsec);
   14251 	      bfd_set_error (bfd_error_bad_value);
   14252 	      result = false;
   14253 	      continue;
   14254 	    }
   14255 	  else if (entsize != ebd->s->sizeof_rel
   14256 		   && entsize != ebd->s->sizeof_rela)
   14257 	    {
   14258 	      _bfd_error_handler
   14259 		/* xgettext:c-format */
   14260 		(_("%pB(%pA): error: secondary reloc section"
   14261 		   " has non-standard sized entries"),
   14262 		 abfd, relsec);
   14263 	      bfd_set_error (bfd_error_bad_value);
   14264 	      result = false;
   14265 	      continue;
   14266 	    }
   14267 
   14268 	  reloc_count = hdr->sh_size / entsize;
   14269 	  hdr->sh_size = entsize * reloc_count;
   14270 	  if (reloc_count == 0)
   14271 	    {
   14272 	      _bfd_error_handler
   14273 		/* xgettext:c-format */
   14274 		(_("%pB(%pA): error: secondary reloc section is empty!"),
   14275 		 abfd, relsec);
   14276 	      bfd_set_error (bfd_error_bad_value);
   14277 	      result = false;
   14278 	      continue;
   14279 	    }
   14280 
   14281 	  hdr->contents = bfd_alloc (abfd, hdr->sh_size);
   14282 	  if (hdr->contents == NULL)
   14283 	    continue;
   14284 
   14285 #if DEBUG_SECONDARY_RELOCS
   14286 	  fprintf (stderr, "write %u secondary relocs for %s from %s\n",
   14287 		   reloc_count, sec->name, relsec->name);
   14288 #endif
   14289 	  last_sym = NULL;
   14290 	  last_sym_idx = 0;
   14291 	  dst_rela = hdr->contents;
   14292 	  src_irel = (arelent *) esd->sec_info;
   14293 	  if (src_irel == NULL)
   14294 	    {
   14295 	      _bfd_error_handler
   14296 		/* xgettext:c-format */
   14297 		(_("%pB(%pA): error: internal relocs missing"
   14298 		   " for secondary reloc section"),
   14299 		 abfd, relsec);
   14300 	      bfd_set_error (bfd_error_bad_value);
   14301 	      result = false;
   14302 	      continue;
   14303 	    }
   14304 
   14305 	  for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
   14306 	    {
   14307 	      Elf_Internal_Rela src_rela;
   14308 	      arelent *ptr;
   14309 	      asymbol *sym;
   14310 	      int n;
   14311 
   14312 	      ptr = src_irel + idx;
   14313 	      if (ptr == NULL)
   14314 		{
   14315 		  _bfd_error_handler
   14316 		    /* xgettext:c-format */
   14317 		    (_("%pB(%pA): error: reloc table entry %zu is empty"),
   14318 		     abfd, relsec, idx);
   14319 		  bfd_set_error (bfd_error_bad_value);
   14320 		  result = false;
   14321 		  break;
   14322 		}
   14323 
   14324 	      if (ptr->sym_ptr_ptr == NULL)
   14325 		{
   14326 		  /* FIXME: Is this an error ? */
   14327 		  n = 0;
   14328 		}
   14329 	      else
   14330 		{
   14331 		  sym = *ptr->sym_ptr_ptr;
   14332 
   14333 		  if (sym == last_sym)
   14334 		    n = last_sym_idx;
   14335 		  else
   14336 		    {
   14337 		      n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
   14338 		      if (n < 0)
   14339 			{
   14340 			  _bfd_error_handler
   14341 			    /* xgettext:c-format */
   14342 			    (_("%pB(%pA): error: secondary reloc %zu"
   14343 			       " references a missing symbol"),
   14344 			     abfd, relsec, idx);
   14345 			  bfd_set_error (bfd_error_bad_value);
   14346 			  result = false;
   14347 			  n = 0;
   14348 			}
   14349 
   14350 		      last_sym = sym;
   14351 		      last_sym_idx = n;
   14352 		    }
   14353 
   14354 		  if (sym->the_bfd != NULL
   14355 		      && sym->the_bfd->xvec != abfd->xvec
   14356 		      && ! _bfd_elf_validate_reloc (abfd, ptr))
   14357 		    {
   14358 		      _bfd_error_handler
   14359 			/* xgettext:c-format */
   14360 			(_("%pB(%pA): error: secondary reloc %zu"
   14361 			   " references a deleted symbol"),
   14362 			 abfd, relsec, idx);
   14363 		      bfd_set_error (bfd_error_bad_value);
   14364 		      result = false;
   14365 		      n = 0;
   14366 		    }
   14367 		}
   14368 
   14369 	      src_rela.r_offset = ptr->address + addr_offset;
   14370 	      if (ptr->howto == NULL)
   14371 		{
   14372 		  _bfd_error_handler
   14373 		    /* xgettext:c-format */
   14374 		    (_("%pB(%pA): error: secondary reloc %zu"
   14375 		       " is of an unknown type"),
   14376 		     abfd, relsec, idx);
   14377 		  bfd_set_error (bfd_error_bad_value);
   14378 		  result = false;
   14379 		  src_rela.r_info = r_info (0, 0);
   14380 		}
   14381 	      else
   14382 		src_rela.r_info = r_info (n, ptr->howto->type);
   14383 	      src_rela.r_addend = ptr->addend;
   14384 
   14385 	      if (entsize == ebd->s->sizeof_rel)
   14386 		ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
   14387 	      else /* entsize == ebd->s->sizeof_rela */
   14388 		ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
   14389 	    }
   14390 	}
   14391     }
   14392 
   14393   return result;
   14394 }
   14395 
   14396 /* Mmap in section contents.  If FINAL_LINK is false, set *BUF to NULL
   14397    before calling bfd_get_full_section_contents.  */
   14398 
   14399 static bool
   14400 elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf,
   14401 			   bool final_link)
   14402 {
   14403 #ifdef USE_MMAP
   14404   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   14405   if (bed->use_mmap
   14406       && sec->compress_status == COMPRESS_SECTION_NONE
   14407       && (sec->flags & SEC_LINKER_CREATED) == 0)
   14408     {
   14409       /* Use mmap only if section size >= the minimum mmap section
   14410 	 size.  */
   14411       size_t readsz = bfd_get_section_limit_octets (abfd, sec);
   14412       size_t allocsz = bfd_get_section_alloc_size (abfd, sec);
   14413       if (readsz == allocsz && readsz >= _bfd_minimum_mmap_size)
   14414 	{
   14415 	  if (sec->contents != NULL)
   14416 	    {
   14417 	      if (!sec->mmapped_p)
   14418 		abort ();
   14419 	      *buf = sec->contents;
   14420 	      return true;
   14421 	    }
   14422 	  if (sec->mmapped_p)
   14423 	    abort ();
   14424 	  sec->mmapped_p = 1;
   14425 
   14426 	  /* Never use the preallocated buffer if mmapp is used.  */
   14427 	  *buf = NULL;
   14428 	}
   14429     }
   14430 #endif
   14431   /* NB: When this is called from elf_link_input_bfd, FINAL_LINK is
   14432      true.  If FINAL_LINK is false, *BUF is set to the preallocated
   14433      buffer if USE_MMAP is undefined and *BUF is set to NULL if
   14434      USE_MMAP is defined.  */
   14435   if (!final_link)
   14436     *buf = NULL;
   14437   bool ret = bfd_get_full_section_contents (abfd, sec, buf);
   14438   if (ret && sec->mmapped_p)
   14439     *buf = sec->contents;
   14440   return ret;
   14441 }
   14442 
   14443 /* Mmap in section contents.  */
   14444 
   14445 bool
   14446 _bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
   14447 {
   14448   return elf_mmap_section_contents (abfd, sec, buf, false);
   14449 }
   14450 
   14451 /* Mmap in the full section contents for the final link.  */
   14452 
   14453 bool
   14454 _bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec,
   14455 				     bfd_byte **buf)
   14456 {
   14457   return elf_mmap_section_contents (abfd, sec, buf, true);
   14458 }
   14459 
   14460 /* Munmap section contents.  */
   14461 
   14462 void
   14463 _bfd_elf_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED,
   14464 				  void *contents)
   14465 {
   14466   /* NB: Since _bfd_elf_munmap_section_contents is called like free,
   14467      CONTENTS may be NULL.  */
   14468   if (contents == NULL)
   14469     return;
   14470 
   14471 #ifdef USE_MMAP
   14472   if (sec->mmapped_p)
   14473     {
   14474       /* _bfd_elf_mmap_section_contents may return the previously
   14475 	 mapped section contents.  Munmap the section contents only
   14476 	 if they haven't been cached.  */
   14477       if (elf_section_data (sec)->this_hdr.contents == contents)
   14478 	return;
   14479 
   14480       /* When _bfd_elf_mmap_section_contents returns CONTENTS as
   14481 	 malloced, CONTENTS_ADDR is set to NULL.  */
   14482       if (elf_section_data (sec)->contents_addr != NULL)
   14483 	{
   14484 	  /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
   14485 	  if (munmap (elf_section_data (sec)->contents_addr,
   14486 		      elf_section_data (sec)->contents_size) != 0)
   14487 	    abort ();
   14488 	  sec->mmapped_p = 0;
   14489 	  sec->contents = NULL;
   14490 	  elf_section_data (sec)->contents_addr = NULL;
   14491 	  elf_section_data (sec)->contents_size = 0;
   14492 	  return;
   14493 	}
   14494     }
   14495 #endif
   14496 
   14497   free (contents);
   14498 }
   14499 
   14500 /* Munmap the full section contents for the final link.  */
   14501 
   14502 void
   14503 _bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED)
   14504 {
   14505 #ifdef USE_MMAP
   14506   if (sec->mmapped_p && elf_section_data (sec)->contents_addr != NULL)
   14507     {
   14508       /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as
   14509 	 malloced, CONTENTS_ADDR is set to NULL.  */
   14510       /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
   14511       if (munmap (elf_section_data (sec)->contents_addr,
   14512 		  elf_section_data (sec)->contents_size) != 0)
   14513 	abort ();
   14514       sec->mmapped_p = 0;
   14515       sec->contents = NULL;
   14516       elf_section_data (sec)->contents_addr = NULL;
   14517       elf_section_data (sec)->contents_size = 0;
   14518     }
   14519 #endif
   14520 }
   14521