Home | History | Annotate | Line # | Download | only in bfd
elf.c revision 1.9.2.1
      1 /* ELF executable support for BFD.
      2 
      3    Copyright (C) 1993-2018 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 "bfd.h"
     39 #include "bfdlink.h"
     40 #include "libbfd.h"
     41 #define ARCH_SIZE 0
     42 #include "elf-bfd.h"
     43 #include "libiberty.h"
     44 #include "safe-ctype.h"
     45 #include "elf-linux-core.h"
     46 
     47 #ifdef CORE_HEADER
     48 #include CORE_HEADER
     49 #endif
     50 
     51 static int elf_sort_sections (const void *, const void *);
     52 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
     53 static bfd_boolean prep_headers (bfd *);
     54 static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ;
     55 static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type,
     56 				   size_t align) ;
     57 static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
     58 				    file_ptr offset, size_t align);
     59 
     60 /* Swap version information in and out.  The version information is
     61    currently size independent.  If that ever changes, this code will
     62    need to move into elfcode.h.  */
     63 
     64 /* Swap in a Verdef structure.  */
     65 
     66 void
     67 _bfd_elf_swap_verdef_in (bfd *abfd,
     68 			 const Elf_External_Verdef *src,
     69 			 Elf_Internal_Verdef *dst)
     70 {
     71   dst->vd_version = H_GET_16 (abfd, src->vd_version);
     72   dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
     73   dst->vd_ndx     = H_GET_16 (abfd, src->vd_ndx);
     74   dst->vd_cnt     = H_GET_16 (abfd, src->vd_cnt);
     75   dst->vd_hash    = H_GET_32 (abfd, src->vd_hash);
     76   dst->vd_aux     = H_GET_32 (abfd, src->vd_aux);
     77   dst->vd_next    = H_GET_32 (abfd, src->vd_next);
     78 }
     79 
     80 /* Swap out a Verdef structure.  */
     81 
     82 void
     83 _bfd_elf_swap_verdef_out (bfd *abfd,
     84 			  const Elf_Internal_Verdef *src,
     85 			  Elf_External_Verdef *dst)
     86 {
     87   H_PUT_16 (abfd, src->vd_version, dst->vd_version);
     88   H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
     89   H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
     90   H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
     91   H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
     92   H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
     93   H_PUT_32 (abfd, src->vd_next, dst->vd_next);
     94 }
     95 
     96 /* Swap in a Verdaux structure.  */
     97 
     98 void
     99 _bfd_elf_swap_verdaux_in (bfd *abfd,
    100 			  const Elf_External_Verdaux *src,
    101 			  Elf_Internal_Verdaux *dst)
    102 {
    103   dst->vda_name = H_GET_32 (abfd, src->vda_name);
    104   dst->vda_next = H_GET_32 (abfd, src->vda_next);
    105 }
    106 
    107 /* Swap out a Verdaux structure.  */
    108 
    109 void
    110 _bfd_elf_swap_verdaux_out (bfd *abfd,
    111 			   const Elf_Internal_Verdaux *src,
    112 			   Elf_External_Verdaux *dst)
    113 {
    114   H_PUT_32 (abfd, src->vda_name, dst->vda_name);
    115   H_PUT_32 (abfd, src->vda_next, dst->vda_next);
    116 }
    117 
    118 /* Swap in a Verneed structure.  */
    119 
    120 void
    121 _bfd_elf_swap_verneed_in (bfd *abfd,
    122 			  const Elf_External_Verneed *src,
    123 			  Elf_Internal_Verneed *dst)
    124 {
    125   dst->vn_version = H_GET_16 (abfd, src->vn_version);
    126   dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
    127   dst->vn_file    = H_GET_32 (abfd, src->vn_file);
    128   dst->vn_aux     = H_GET_32 (abfd, src->vn_aux);
    129   dst->vn_next    = H_GET_32 (abfd, src->vn_next);
    130 }
    131 
    132 /* Swap out a Verneed structure.  */
    133 
    134 void
    135 _bfd_elf_swap_verneed_out (bfd *abfd,
    136 			   const Elf_Internal_Verneed *src,
    137 			   Elf_External_Verneed *dst)
    138 {
    139   H_PUT_16 (abfd, src->vn_version, dst->vn_version);
    140   H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
    141   H_PUT_32 (abfd, src->vn_file, dst->vn_file);
    142   H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
    143   H_PUT_32 (abfd, src->vn_next, dst->vn_next);
    144 }
    145 
    146 /* Swap in a Vernaux structure.  */
    147 
    148 void
    149 _bfd_elf_swap_vernaux_in (bfd *abfd,
    150 			  const Elf_External_Vernaux *src,
    151 			  Elf_Internal_Vernaux *dst)
    152 {
    153   dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
    154   dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
    155   dst->vna_other = H_GET_16 (abfd, src->vna_other);
    156   dst->vna_name  = H_GET_32 (abfd, src->vna_name);
    157   dst->vna_next  = H_GET_32 (abfd, src->vna_next);
    158 }
    159 
    160 /* Swap out a Vernaux structure.  */
    161 
    162 void
    163 _bfd_elf_swap_vernaux_out (bfd *abfd,
    164 			   const Elf_Internal_Vernaux *src,
    165 			   Elf_External_Vernaux *dst)
    166 {
    167   H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
    168   H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
    169   H_PUT_16 (abfd, src->vna_other, dst->vna_other);
    170   H_PUT_32 (abfd, src->vna_name, dst->vna_name);
    171   H_PUT_32 (abfd, src->vna_next, dst->vna_next);
    172 }
    173 
    174 /* Swap in a Versym structure.  */
    175 
    176 void
    177 _bfd_elf_swap_versym_in (bfd *abfd,
    178 			 const Elf_External_Versym *src,
    179 			 Elf_Internal_Versym *dst)
    180 {
    181   dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
    182 }
    183 
    184 /* Swap out a Versym structure.  */
    185 
    186 void
    187 _bfd_elf_swap_versym_out (bfd *abfd,
    188 			  const Elf_Internal_Versym *src,
    189 			  Elf_External_Versym *dst)
    190 {
    191   H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
    192 }
    193 
    194 /* Standard ELF hash function.  Do not change this function; you will
    195    cause invalid hash tables to be generated.  */
    196 
    197 unsigned long
    198 bfd_elf_hash (const char *namearg)
    199 {
    200   const unsigned char *name = (const unsigned char *) namearg;
    201   unsigned long h = 0;
    202   unsigned long g;
    203   int ch;
    204 
    205   while ((ch = *name++) != '\0')
    206     {
    207       h = (h << 4) + ch;
    208       if ((g = (h & 0xf0000000)) != 0)
    209 	{
    210 	  h ^= g >> 24;
    211 	  /* The ELF ABI says `h &= ~g', but this is equivalent in
    212 	     this case and on some machines one insn instead of two.  */
    213 	  h ^= g;
    214 	}
    215     }
    216   return h & 0xffffffff;
    217 }
    218 
    219 /* DT_GNU_HASH hash function.  Do not change this function; you will
    220    cause invalid hash tables to be generated.  */
    221 
    222 unsigned long
    223 bfd_elf_gnu_hash (const char *namearg)
    224 {
    225   const unsigned char *name = (const unsigned char *) namearg;
    226   unsigned long h = 5381;
    227   unsigned char ch;
    228 
    229   while ((ch = *name++) != '\0')
    230     h = (h << 5) + h + ch;
    231   return h & 0xffffffff;
    232 }
    233 
    234 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
    235    the object_id field of an elf_obj_tdata field set to OBJECT_ID.  */
    236 bfd_boolean
    237 bfd_elf_allocate_object (bfd *abfd,
    238 			 size_t object_size,
    239 			 enum elf_target_id object_id)
    240 {
    241   BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
    242   abfd->tdata.any = bfd_zalloc (abfd, object_size);
    243   if (abfd->tdata.any == NULL)
    244     return FALSE;
    245 
    246   elf_object_id (abfd) = object_id;
    247   if (abfd->direction != read_direction)
    248     {
    249       struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
    250       if (o == NULL)
    251 	return FALSE;
    252       elf_tdata (abfd)->o = o;
    253       elf_program_header_size (abfd) = (bfd_size_type) -1;
    254     }
    255   return TRUE;
    256 }
    257 
    258 
    259 bfd_boolean
    260 bfd_elf_make_object (bfd *abfd)
    261 {
    262   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    263   return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
    264 				  bed->target_id);
    265 }
    266 
    267 bfd_boolean
    268 bfd_elf_mkcorefile (bfd *abfd)
    269 {
    270   /* I think this can be done just like an object file.  */
    271   if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
    272     return FALSE;
    273   elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
    274   return elf_tdata (abfd)->core != NULL;
    275 }
    276 
    277 static char *
    278 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
    279 {
    280   Elf_Internal_Shdr **i_shdrp;
    281   bfd_byte *shstrtab = NULL;
    282   file_ptr offset;
    283   bfd_size_type shstrtabsize;
    284 
    285   i_shdrp = elf_elfsections (abfd);
    286   if (i_shdrp == 0
    287       || shindex >= elf_numsections (abfd)
    288       || i_shdrp[shindex] == 0)
    289     return NULL;
    290 
    291   shstrtab = i_shdrp[shindex]->contents;
    292   if (shstrtab == NULL)
    293     {
    294       /* No cached one, attempt to read, and cache what we read.  */
    295       offset = i_shdrp[shindex]->sh_offset;
    296       shstrtabsize = i_shdrp[shindex]->sh_size;
    297 
    298       /* Allocate and clear an extra byte at the end, to prevent crashes
    299 	 in case the string table is not terminated.  */
    300       if (shstrtabsize + 1 <= 1
    301 	  || shstrtabsize > bfd_get_file_size (abfd)
    302 	  || bfd_seek (abfd, offset, SEEK_SET) != 0
    303 	  || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL)
    304 	shstrtab = NULL;
    305       else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
    306 	{
    307 	  if (bfd_get_error () != bfd_error_system_call)
    308 	    bfd_set_error (bfd_error_file_truncated);
    309 	  bfd_release (abfd, shstrtab);
    310 	  shstrtab = NULL;
    311 	  /* Once we've failed to read it, make sure we don't keep
    312 	     trying.  Otherwise, we'll keep allocating space for
    313 	     the string table over and over.  */
    314 	  i_shdrp[shindex]->sh_size = 0;
    315 	}
    316       else
    317 	shstrtab[shstrtabsize] = '\0';
    318       i_shdrp[shindex]->contents = shstrtab;
    319     }
    320   return (char *) shstrtab;
    321 }
    322 
    323 char *
    324 bfd_elf_string_from_elf_section (bfd *abfd,
    325 				 unsigned int shindex,
    326 				 unsigned int strindex)
    327 {
    328   Elf_Internal_Shdr *hdr;
    329 
    330   if (strindex == 0)
    331     return "";
    332 
    333   if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
    334     return NULL;
    335 
    336   hdr = elf_elfsections (abfd)[shindex];
    337 
    338   if (hdr->contents == NULL)
    339     {
    340       if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
    341 	{
    342 	  /* PR 17512: file: f057ec89.  */
    343 	  /* xgettext:c-format */
    344 	  _bfd_error_handler (_("%pB: attempt to load strings from"
    345 				" a non-string section (number %d)"),
    346 			      abfd, shindex);
    347 	  return NULL;
    348 	}
    349 
    350       if (bfd_elf_get_str_section (abfd, shindex) == NULL)
    351 	return NULL;
    352     }
    353 
    354   if (strindex >= hdr->sh_size)
    355     {
    356       unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
    357       _bfd_error_handler
    358 	/* xgettext:c-format */
    359 	(_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
    360 	 abfd, strindex, (uint64_t) hdr->sh_size,
    361 	 (shindex == shstrndx && strindex == hdr->sh_name
    362 	  ? ".shstrtab"
    363 	  : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
    364       return NULL;
    365     }
    366 
    367   return ((char *) hdr->contents) + strindex;
    368 }
    369 
    370 /* Read and convert symbols to internal format.
    371    SYMCOUNT specifies the number of symbols to read, starting from
    372    symbol SYMOFFSET.  If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
    373    are non-NULL, they are used to store the internal symbols, external
    374    symbols, and symbol section index extensions, respectively.
    375    Returns a pointer to the internal symbol buffer (malloced if necessary)
    376    or NULL if there were no symbols or some kind of problem.  */
    377 
    378 Elf_Internal_Sym *
    379 bfd_elf_get_elf_syms (bfd *ibfd,
    380 		      Elf_Internal_Shdr *symtab_hdr,
    381 		      size_t symcount,
    382 		      size_t symoffset,
    383 		      Elf_Internal_Sym *intsym_buf,
    384 		      void *extsym_buf,
    385 		      Elf_External_Sym_Shndx *extshndx_buf)
    386 {
    387   Elf_Internal_Shdr *shndx_hdr;
    388   void *alloc_ext;
    389   const bfd_byte *esym;
    390   Elf_External_Sym_Shndx *alloc_extshndx;
    391   Elf_External_Sym_Shndx *shndx;
    392   Elf_Internal_Sym *alloc_intsym;
    393   Elf_Internal_Sym *isym;
    394   Elf_Internal_Sym *isymend;
    395   const struct elf_backend_data *bed;
    396   size_t extsym_size;
    397   bfd_size_type amt;
    398   file_ptr pos;
    399 
    400   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
    401     abort ();
    402 
    403   if (symcount == 0)
    404     return intsym_buf;
    405 
    406   /* Normal syms might have section extension entries.  */
    407   shndx_hdr = NULL;
    408   if (elf_symtab_shndx_list (ibfd) != NULL)
    409     {
    410       elf_section_list * entry;
    411       Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
    412 
    413       /* Find an index section that is linked to this symtab section.  */
    414       for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
    415 	{
    416 	  /* PR 20063.  */
    417 	  if (entry->hdr.sh_link >= elf_numsections (ibfd))
    418 	    continue;
    419 
    420 	  if (sections[entry->hdr.sh_link] == symtab_hdr)
    421 	    {
    422 	      shndx_hdr = & entry->hdr;
    423 	      break;
    424 	    };
    425 	}
    426 
    427       if (shndx_hdr == NULL)
    428 	{
    429 	  if (symtab_hdr == & elf_symtab_hdr (ibfd))
    430 	    /* Not really accurate, but this was how the old code used to work.  */
    431 	    shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
    432 	  /* Otherwise we do nothing.  The assumption is that
    433 	     the index table will not be needed.  */
    434 	}
    435     }
    436 
    437   /* Read the symbols.  */
    438   alloc_ext = NULL;
    439   alloc_extshndx = NULL;
    440   alloc_intsym = NULL;
    441   bed = get_elf_backend_data (ibfd);
    442   extsym_size = bed->s->sizeof_sym;
    443   amt = (bfd_size_type) symcount * extsym_size;
    444   pos = symtab_hdr->sh_offset + symoffset * extsym_size;
    445   if (extsym_buf == NULL)
    446     {
    447       alloc_ext = bfd_malloc2 (symcount, extsym_size);
    448       extsym_buf = alloc_ext;
    449     }
    450   if (extsym_buf == NULL
    451       || bfd_seek (ibfd, pos, SEEK_SET) != 0
    452       || bfd_bread (extsym_buf, amt, ibfd) != amt)
    453     {
    454       intsym_buf = NULL;
    455       goto out;
    456     }
    457 
    458   if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
    459     extshndx_buf = NULL;
    460   else
    461     {
    462       amt = (bfd_size_type) symcount * sizeof (Elf_External_Sym_Shndx);
    463       pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
    464       if (extshndx_buf == NULL)
    465 	{
    466 	  alloc_extshndx = (Elf_External_Sym_Shndx *)
    467 	      bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx));
    468 	  extshndx_buf = alloc_extshndx;
    469 	}
    470       if (extshndx_buf == NULL
    471 	  || bfd_seek (ibfd, pos, SEEK_SET) != 0
    472 	  || bfd_bread (extshndx_buf, amt, ibfd) != amt)
    473 	{
    474 	  intsym_buf = NULL;
    475 	  goto out;
    476 	}
    477     }
    478 
    479   if (intsym_buf == NULL)
    480     {
    481       alloc_intsym = (Elf_Internal_Sym *)
    482 	  bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
    483       intsym_buf = alloc_intsym;
    484       if (intsym_buf == NULL)
    485 	goto out;
    486     }
    487 
    488   /* Convert the symbols to internal form.  */
    489   isymend = intsym_buf + symcount;
    490   for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
    491 	   shndx = extshndx_buf;
    492        isym < isymend;
    493        esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
    494     if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
    495       {
    496 	symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
    497 	/* xgettext:c-format */
    498 	_bfd_error_handler (_("%pB symbol number %lu references"
    499 			      " nonexistent SHT_SYMTAB_SHNDX section"),
    500 			    ibfd, (unsigned long) symoffset);
    501 	if (alloc_intsym != NULL)
    502 	  free (alloc_intsym);
    503 	intsym_buf = NULL;
    504 	goto out;
    505       }
    506 
    507  out:
    508   if (alloc_ext != NULL)
    509     free (alloc_ext);
    510   if (alloc_extshndx != NULL)
    511     free (alloc_extshndx);
    512 
    513   return intsym_buf;
    514 }
    515 
    516 /* Look up a symbol name.  */
    517 const char *
    518 bfd_elf_sym_name (bfd *abfd,
    519 		  Elf_Internal_Shdr *symtab_hdr,
    520 		  Elf_Internal_Sym *isym,
    521 		  asection *sym_sec)
    522 {
    523   const char *name;
    524   unsigned int iname = isym->st_name;
    525   unsigned int shindex = symtab_hdr->sh_link;
    526 
    527   if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
    528       /* Check for a bogus st_shndx to avoid crashing.  */
    529       && isym->st_shndx < elf_numsections (abfd))
    530     {
    531       iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
    532       shindex = elf_elfheader (abfd)->e_shstrndx;
    533     }
    534 
    535   name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
    536   if (name == NULL)
    537     name = "(null)";
    538   else if (sym_sec && *name == '\0')
    539     name = bfd_section_name (abfd, sym_sec);
    540 
    541   return name;
    542 }
    543 
    544 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
    545    sections.  The first element is the flags, the rest are section
    546    pointers.  */
    547 
    548 typedef union elf_internal_group {
    549   Elf_Internal_Shdr *shdr;
    550   unsigned int flags;
    551 } Elf_Internal_Group;
    552 
    553 /* Return the name of the group signature symbol.  Why isn't the
    554    signature just a string?  */
    555 
    556 static const char *
    557 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
    558 {
    559   Elf_Internal_Shdr *hdr;
    560   unsigned char esym[sizeof (Elf64_External_Sym)];
    561   Elf_External_Sym_Shndx eshndx;
    562   Elf_Internal_Sym isym;
    563 
    564   /* First we need to ensure the symbol table is available.  Make sure
    565      that it is a symbol table section.  */
    566   if (ghdr->sh_link >= elf_numsections (abfd))
    567     return NULL;
    568   hdr = elf_elfsections (abfd) [ghdr->sh_link];
    569   if (hdr->sh_type != SHT_SYMTAB
    570       || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
    571     return NULL;
    572 
    573   /* Go read the symbol.  */
    574   hdr = &elf_tdata (abfd)->symtab_hdr;
    575   if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
    576 			    &isym, esym, &eshndx) == NULL)
    577     return NULL;
    578 
    579   return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
    580 }
    581 
    582 /* Set next_in_group list pointer, and group name for NEWSECT.  */
    583 
    584 static bfd_boolean
    585 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
    586 {
    587   unsigned int num_group = elf_tdata (abfd)->num_group;
    588 
    589   /* If num_group is zero, read in all SHT_GROUP sections.  The count
    590      is set to -1 if there are no SHT_GROUP sections.  */
    591   if (num_group == 0)
    592     {
    593       unsigned int i, shnum;
    594 
    595       /* First count the number of groups.  If we have a SHT_GROUP
    596 	 section with just a flag word (ie. sh_size is 4), ignore it.  */
    597       shnum = elf_numsections (abfd);
    598       num_group = 0;
    599 
    600 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize)	\
    601 	(   (shdr)->sh_type == SHT_GROUP		\
    602 	 && (shdr)->sh_size >= minsize			\
    603 	 && (shdr)->sh_entsize == GRP_ENTRY_SIZE	\
    604 	 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
    605 
    606       for (i = 0; i < shnum; i++)
    607 	{
    608 	  Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
    609 
    610 	  if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
    611 	    num_group += 1;
    612 	}
    613 
    614       if (num_group == 0)
    615 	{
    616 	  num_group = (unsigned) -1;
    617 	  elf_tdata (abfd)->num_group = num_group;
    618 	  elf_tdata (abfd)->group_sect_ptr = NULL;
    619 	}
    620       else
    621 	{
    622 	  /* We keep a list of elf section headers for group sections,
    623 	     so we can find them quickly.  */
    624 	  bfd_size_type amt;
    625 
    626 	  elf_tdata (abfd)->num_group = num_group;
    627 	  elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
    628 	      bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
    629 	  if (elf_tdata (abfd)->group_sect_ptr == NULL)
    630 	    return FALSE;
    631 	  memset (elf_tdata (abfd)->group_sect_ptr, 0,
    632 		  num_group * sizeof (Elf_Internal_Shdr *));
    633 	  num_group = 0;
    634 
    635 	  for (i = 0; i < shnum; i++)
    636 	    {
    637 	      Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
    638 
    639 	      if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
    640 		{
    641 		  unsigned char *src;
    642 		  Elf_Internal_Group *dest;
    643 
    644 		  /* Make sure the group section has a BFD section
    645 		     attached to it.  */
    646 		  if (!bfd_section_from_shdr (abfd, i))
    647 		    return FALSE;
    648 
    649 		  /* Add to list of sections.  */
    650 		  elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
    651 		  num_group += 1;
    652 
    653 		  /* Read the raw contents.  */
    654 		  BFD_ASSERT (sizeof (*dest) >= 4);
    655 		  amt = shdr->sh_size * sizeof (*dest) / 4;
    656 		  shdr->contents = (unsigned char *)
    657 		      bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
    658 		  /* PR binutils/4110: Handle corrupt group headers.  */
    659 		  if (shdr->contents == NULL)
    660 		    {
    661 		      _bfd_error_handler
    662 			/* xgettext:c-format */
    663 			(_("%pB: corrupt size field in group section"
    664 			   " header: %#" PRIx64),
    665 			 abfd, (uint64_t) shdr->sh_size);
    666 		      bfd_set_error (bfd_error_bad_value);
    667 		      -- num_group;
    668 		      continue;
    669 		    }
    670 
    671 		  memset (shdr->contents, 0, amt);
    672 
    673 		  if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
    674 		      || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
    675 			  != shdr->sh_size))
    676 		    {
    677 		      _bfd_error_handler
    678 			/* xgettext:c-format */
    679 			(_("%pB: invalid size field in group section"
    680 			   " header: %#" PRIx64 ""),
    681 			 abfd, (uint64_t) shdr->sh_size);
    682 		      bfd_set_error (bfd_error_bad_value);
    683 		      -- num_group;
    684 		      /* PR 17510: If the group contents are even
    685 			 partially corrupt, do not allow any of the
    686 			 contents to be used.  */
    687 		      memset (shdr->contents, 0, amt);
    688 		      continue;
    689 		    }
    690 
    691 		  /* Translate raw contents, a flag word followed by an
    692 		     array of elf section indices all in target byte order,
    693 		     to the flag word followed by an array of elf section
    694 		     pointers.  */
    695 		  src = shdr->contents + shdr->sh_size;
    696 		  dest = (Elf_Internal_Group *) (shdr->contents + amt);
    697 
    698 		  while (1)
    699 		    {
    700 		      unsigned int idx;
    701 
    702 		      src -= 4;
    703 		      --dest;
    704 		      idx = H_GET_32 (abfd, src);
    705 		      if (src == shdr->contents)
    706 			{
    707 			  dest->flags = idx;
    708 			  if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
    709 			    shdr->bfd_section->flags
    710 			      |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
    711 			  break;
    712 			}
    713 		      if (idx < shnum)
    714 			{
    715 			  dest->shdr = elf_elfsections (abfd)[idx];
    716 			  /* PR binutils/23199: All sections in a
    717 			     section group should be marked with
    718 			     SHF_GROUP.  But some tools generate
    719 			     broken objects without SHF_GROUP.  Fix
    720 			     them up here.  */
    721 			  dest->shdr->sh_flags |= SHF_GROUP;
    722 			}
    723 		      if (idx >= shnum
    724 			  || dest->shdr->sh_type == SHT_GROUP)
    725 			{
    726 			  _bfd_error_handler
    727 			    (_("%pB: invalid entry in SHT_GROUP section [%u]"),
    728 			       abfd, i);
    729 			  dest->shdr = NULL;
    730 			}
    731 		    }
    732 		}
    733 	    }
    734 
    735 	  /* PR 17510: Corrupt binaries might contain invalid groups.  */
    736 	  if (num_group != (unsigned) elf_tdata (abfd)->num_group)
    737 	    {
    738 	      elf_tdata (abfd)->num_group = num_group;
    739 
    740 	      /* If all groups are invalid then fail.  */
    741 	      if (num_group == 0)
    742 		{
    743 		  elf_tdata (abfd)->group_sect_ptr = NULL;
    744 		  elf_tdata (abfd)->num_group = num_group = -1;
    745 		  _bfd_error_handler
    746 		    (_("%pB: no valid group sections found"), abfd);
    747 		  bfd_set_error (bfd_error_bad_value);
    748 		}
    749 	    }
    750 	}
    751     }
    752 
    753   if (num_group != (unsigned) -1)
    754     {
    755       unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
    756       unsigned int j;
    757 
    758       for (j = 0; j < num_group; j++)
    759 	{
    760 	  /* Begin search from previous found group.  */
    761 	  unsigned i = (j + search_offset) % num_group;
    762 
    763 	  Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
    764 	  Elf_Internal_Group *idx;
    765 	  bfd_size_type n_elt;
    766 
    767 	  if (shdr == NULL)
    768 	    continue;
    769 
    770 	  idx = (Elf_Internal_Group *) shdr->contents;
    771 	  if (idx == NULL || shdr->sh_size < 4)
    772 	    {
    773 	      /* See PR 21957 for a reproducer.  */
    774 	      /* xgettext:c-format */
    775 	      _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
    776 				  abfd, shdr->bfd_section);
    777 	      elf_tdata (abfd)->group_sect_ptr[i] = NULL;
    778 	      bfd_set_error (bfd_error_bad_value);
    779 	      return FALSE;
    780 	    }
    781 	  n_elt = shdr->sh_size / 4;
    782 
    783 	  /* Look through this group's sections to see if current
    784 	     section is a member.  */
    785 	  while (--n_elt != 0)
    786 	    if ((++idx)->shdr == hdr)
    787 	      {
    788 		asection *s = NULL;
    789 
    790 		/* We are a member of this group.  Go looking through
    791 		   other members to see if any others are linked via
    792 		   next_in_group.  */
    793 		idx = (Elf_Internal_Group *) shdr->contents;
    794 		n_elt = shdr->sh_size / 4;
    795 		while (--n_elt != 0)
    796 		  if ((++idx)->shdr != NULL
    797 		      && (s = idx->shdr->bfd_section) != NULL
    798 		      && elf_next_in_group (s) != NULL)
    799 		    break;
    800 		if (n_elt != 0)
    801 		  {
    802 		    /* Snarf the group name from other member, and
    803 		       insert current section in circular list.  */
    804 		    elf_group_name (newsect) = elf_group_name (s);
    805 		    elf_next_in_group (newsect) = elf_next_in_group (s);
    806 		    elf_next_in_group (s) = newsect;
    807 		  }
    808 		else
    809 		  {
    810 		    const char *gname;
    811 
    812 		    gname = group_signature (abfd, shdr);
    813 		    if (gname == NULL)
    814 		      return FALSE;
    815 		    elf_group_name (newsect) = gname;
    816 
    817 		    /* Start a circular list with one element.  */
    818 		    elf_next_in_group (newsect) = newsect;
    819 		  }
    820 
    821 		/* If the group section has been created, point to the
    822 		   new member.  */
    823 		if (shdr->bfd_section != NULL)
    824 		  elf_next_in_group (shdr->bfd_section) = newsect;
    825 
    826 		elf_tdata (abfd)->group_search_offset = i;
    827 		j = num_group - 1;
    828 		break;
    829 	      }
    830 	}
    831     }
    832 
    833   if (elf_group_name (newsect) == NULL)
    834     {
    835       /* xgettext:c-format */
    836       _bfd_error_handler (_("%pB: no group info for section '%pA'"),
    837 			  abfd, newsect);
    838       return FALSE;
    839     }
    840   return TRUE;
    841 }
    842 
    843 bfd_boolean
    844 _bfd_elf_setup_sections (bfd *abfd)
    845 {
    846   unsigned int i;
    847   unsigned int num_group = elf_tdata (abfd)->num_group;
    848   bfd_boolean result = TRUE;
    849   asection *s;
    850 
    851   /* Process SHF_LINK_ORDER.  */
    852   for (s = abfd->sections; s != NULL; s = s->next)
    853     {
    854       Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
    855       if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
    856 	{
    857 	  unsigned int elfsec = this_hdr->sh_link;
    858 	  /* FIXME: The old Intel compiler and old strip/objcopy may
    859 	     not set the sh_link or sh_info fields.  Hence we could
    860 	     get the situation where elfsec is 0.  */
    861 	  if (elfsec == 0)
    862 	    {
    863 	      const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    864 	      if (bed->link_order_error_handler)
    865 		bed->link_order_error_handler
    866 		  /* xgettext:c-format */
    867 		  (_("%pB: warning: sh_link not set for section `%pA'"),
    868 		   abfd, s);
    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 group data.  */
    917       if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
    918 	{
    919 	  _bfd_error_handler
    920 	    /* xgettext:c-format */
    921 	    (_("%pB: section group entry number %u is corrupt"),
    922 	     abfd, i);
    923 	  result = FALSE;
    924 	  continue;
    925 	}
    926 
    927       idx = (Elf_Internal_Group *) shdr->contents;
    928       n_elt = shdr->sh_size / 4;
    929 
    930       while (--n_elt != 0)
    931 	{
    932 	  ++ idx;
    933 
    934 	  if (idx->shdr == NULL)
    935 	    continue;
    936 	  else if (idx->shdr->bfd_section)
    937 	    elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
    938 	  else if (idx->shdr->sh_type != SHT_RELA
    939 		   && idx->shdr->sh_type != SHT_REL)
    940 	    {
    941 	      /* There are some unknown sections in the group.  */
    942 	      _bfd_error_handler
    943 		/* xgettext:c-format */
    944 		(_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
    945 		 abfd,
    946 		 idx->shdr->sh_type,
    947 		 bfd_elf_string_from_elf_section (abfd,
    948 						  (elf_elfheader (abfd)
    949 						   ->e_shstrndx),
    950 						  idx->shdr->sh_name),
    951 		 shdr->bfd_section);
    952 	      result = FALSE;
    953 	    }
    954 	}
    955     }
    956 
    957   return result;
    958 }
    959 
    960 bfd_boolean
    961 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
    962 {
    963   return elf_next_in_group (sec) != NULL;
    964 }
    965 
    966 static char *
    967 convert_debug_to_zdebug (bfd *abfd, const char *name)
    968 {
    969   unsigned int len = strlen (name);
    970   char *new_name = bfd_alloc (abfd, len + 2);
    971   if (new_name == NULL)
    972     return NULL;
    973   new_name[0] = '.';
    974   new_name[1] = 'z';
    975   memcpy (new_name + 2, name + 1, len);
    976   return new_name;
    977 }
    978 
    979 static char *
    980 convert_zdebug_to_debug (bfd *abfd, const char *name)
    981 {
    982   unsigned int len = strlen (name);
    983   char *new_name = bfd_alloc (abfd, len);
    984   if (new_name == NULL)
    985     return NULL;
    986   new_name[0] = '.';
    987   memcpy (new_name + 1, name + 2, len - 1);
    988   return new_name;
    989 }
    990 
    991 /* Make a BFD section from an ELF section.  We store a pointer to the
    992    BFD section in the bfd_section field of the header.  */
    993 
    994 bfd_boolean
    995 _bfd_elf_make_section_from_shdr (bfd *abfd,
    996 				 Elf_Internal_Shdr *hdr,
    997 				 const char *name,
    998 				 int shindex)
    999 {
   1000   asection *newsect;
   1001   flagword flags;
   1002   const struct elf_backend_data *bed;
   1003 
   1004   if (hdr->bfd_section != NULL)
   1005     return TRUE;
   1006 
   1007   newsect = bfd_make_section_anyway (abfd, name);
   1008   if (newsect == NULL)
   1009     return FALSE;
   1010 
   1011   hdr->bfd_section = newsect;
   1012   elf_section_data (newsect)->this_hdr = *hdr;
   1013   elf_section_data (newsect)->this_idx = shindex;
   1014 
   1015   /* Always use the real type/flags.  */
   1016   elf_section_type (newsect) = hdr->sh_type;
   1017   elf_section_flags (newsect) = hdr->sh_flags;
   1018 
   1019   newsect->filepos = hdr->sh_offset;
   1020 
   1021   if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
   1022       || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
   1023       || ! bfd_set_section_alignment (abfd, newsect,
   1024 				      bfd_log2 (hdr->sh_addralign)))
   1025     return FALSE;
   1026 
   1027   flags = SEC_NO_FLAGS;
   1028   if (hdr->sh_type != SHT_NOBITS)
   1029     flags |= SEC_HAS_CONTENTS;
   1030   if (hdr->sh_type == SHT_GROUP)
   1031     flags |= SEC_GROUP;
   1032   if ((hdr->sh_flags & SHF_ALLOC) != 0)
   1033     {
   1034       flags |= SEC_ALLOC;
   1035       if (hdr->sh_type != SHT_NOBITS)
   1036 	flags |= SEC_LOAD;
   1037     }
   1038   if ((hdr->sh_flags & SHF_WRITE) == 0)
   1039     flags |= SEC_READONLY;
   1040   if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
   1041     flags |= SEC_CODE;
   1042   else if ((flags & SEC_LOAD) != 0)
   1043     flags |= SEC_DATA;
   1044   if ((hdr->sh_flags & SHF_MERGE) != 0)
   1045     {
   1046       flags |= SEC_MERGE;
   1047       newsect->entsize = hdr->sh_entsize;
   1048     }
   1049   if ((hdr->sh_flags & SHF_STRINGS) != 0)
   1050     flags |= SEC_STRINGS;
   1051   if (hdr->sh_flags & SHF_GROUP)
   1052     if (!setup_group (abfd, hdr, newsect))
   1053       return FALSE;
   1054   if ((hdr->sh_flags & SHF_TLS) != 0)
   1055     flags |= SEC_THREAD_LOCAL;
   1056   if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
   1057     flags |= SEC_EXCLUDE;
   1058 
   1059   if ((flags & SEC_ALLOC) == 0)
   1060     {
   1061       /* The debugging sections appear to be recognized only by name,
   1062 	 not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
   1063       if (name [0] == '.')
   1064 	{
   1065 	  const char *p;
   1066 	  int n;
   1067 	  if (name[1] == 'd')
   1068 	    p = ".debug", n = 6;
   1069 	  else if (name[1] == 'g' && name[2] == 'n')
   1070 	    p = ".gnu.linkonce.wi.", n = 17;
   1071 	  else if (name[1] == 'g' && name[2] == 'd')
   1072 	    p = ".gdb_index", n = 11; /* yes we really do mean 11.  */
   1073 	  else if (name[1] == 'l')
   1074 	    p = ".line", n = 5;
   1075 	  else if (name[1] == 's')
   1076 	    p = ".stab", n = 5;
   1077 	  else if (name[1] == 'z')
   1078 	    p = ".zdebug", n = 7;
   1079 	  else
   1080 	    p = NULL, n = 0;
   1081 	  if (p != NULL && strncmp (name, p, n) == 0)
   1082 	    flags |= SEC_DEBUGGING;
   1083 	}
   1084     }
   1085 
   1086   /* As a GNU extension, if the name begins with .gnu.linkonce, we
   1087      only link a single copy of the section.  This is used to support
   1088      g++.  g++ will emit each template expansion in its own section.
   1089      The symbols will be defined as weak, so that multiple definitions
   1090      are permitted.  The GNU linker extension is to actually discard
   1091      all but one of the sections.  */
   1092   if (CONST_STRNEQ (name, ".gnu.linkonce")
   1093       && elf_next_in_group (newsect) == NULL)
   1094     flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
   1095 
   1096   bed = get_elf_backend_data (abfd);
   1097   if (bed->elf_backend_section_flags)
   1098     if (! bed->elf_backend_section_flags (&flags, hdr))
   1099       return FALSE;
   1100 
   1101   if (! bfd_set_section_flags (abfd, newsect, flags))
   1102     return FALSE;
   1103 
   1104   /* We do not parse the PT_NOTE segments as we are interested even in the
   1105      separate debug info files which may have the segments offsets corrupted.
   1106      PT_NOTEs from the core files are currently not parsed using BFD.  */
   1107   if (hdr->sh_type == SHT_NOTE)
   1108     {
   1109       bfd_byte *contents;
   1110 
   1111       if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
   1112 	return FALSE;
   1113 
   1114       elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
   1115 		       hdr->sh_offset, hdr->sh_addralign);
   1116       free (contents);
   1117     }
   1118 
   1119   if ((flags & SEC_ALLOC) != 0)
   1120     {
   1121       Elf_Internal_Phdr *phdr;
   1122       unsigned int i, nload;
   1123 
   1124       /* Some ELF linkers produce binaries with all the program header
   1125 	 p_paddr fields zero.  If we have such a binary with more than
   1126 	 one PT_LOAD header, then leave the section lma equal to vma
   1127 	 so that we don't create sections with overlapping lma.  */
   1128       phdr = elf_tdata (abfd)->phdr;
   1129       for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
   1130 	if (phdr->p_paddr != 0)
   1131 	  break;
   1132 	else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
   1133 	  ++nload;
   1134       if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
   1135 	return TRUE;
   1136 
   1137       phdr = elf_tdata (abfd)->phdr;
   1138       for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
   1139 	{
   1140 	  if (((phdr->p_type == PT_LOAD
   1141 		&& (hdr->sh_flags & SHF_TLS) == 0)
   1142 	       || phdr->p_type == PT_TLS)
   1143 	      && ELF_SECTION_IN_SEGMENT (hdr, phdr))
   1144 	    {
   1145 	      if ((flags & SEC_LOAD) == 0)
   1146 		newsect->lma = (phdr->p_paddr
   1147 				+ hdr->sh_addr - phdr->p_vaddr);
   1148 	      else
   1149 		/* We used to use the same adjustment for SEC_LOAD
   1150 		   sections, but that doesn't work if the segment
   1151 		   is packed with code from multiple VMAs.
   1152 		   Instead we calculate the section LMA based on
   1153 		   the segment LMA.  It is assumed that the
   1154 		   segment will contain sections with contiguous
   1155 		   LMAs, even if the VMAs are not.  */
   1156 		newsect->lma = (phdr->p_paddr
   1157 				+ hdr->sh_offset - phdr->p_offset);
   1158 
   1159 	      /* With contiguous segments, we can't tell from file
   1160 		 offsets whether a section with zero size should
   1161 		 be placed at the end of one segment or the
   1162 		 beginning of the next.  Decide based on vaddr.  */
   1163 	      if (hdr->sh_addr >= phdr->p_vaddr
   1164 		  && (hdr->sh_addr + hdr->sh_size
   1165 		      <= phdr->p_vaddr + phdr->p_memsz))
   1166 		break;
   1167 	    }
   1168 	}
   1169     }
   1170 
   1171   /* Compress/decompress DWARF debug sections with names: .debug_* and
   1172      .zdebug_*, after the section flags is set.  */
   1173   if ((flags & SEC_DEBUGGING)
   1174       && ((name[1] == 'd' && name[6] == '_')
   1175 	  || (name[1] == 'z' && name[7] == '_')))
   1176     {
   1177       enum { nothing, compress, decompress } action = nothing;
   1178       int compression_header_size;
   1179       bfd_size_type uncompressed_size;
   1180       bfd_boolean compressed
   1181 	= bfd_is_section_compressed_with_header (abfd, newsect,
   1182 						 &compression_header_size,
   1183 						 &uncompressed_size);
   1184 
   1185       if (compressed)
   1186 	{
   1187 	  /* Compressed section.  Check if we should decompress.  */
   1188 	  if ((abfd->flags & BFD_DECOMPRESS))
   1189 	    action = decompress;
   1190 	}
   1191 
   1192       /* Compress the uncompressed section or convert from/to .zdebug*
   1193 	 section.  Check if we should compress.  */
   1194       if (action == nothing)
   1195 	{
   1196 	  if (newsect->size != 0
   1197 	      && (abfd->flags & BFD_COMPRESS)
   1198 	      && compression_header_size >= 0
   1199 	      && uncompressed_size > 0
   1200 	      && (!compressed
   1201 		  || ((compression_header_size > 0)
   1202 		      != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
   1203 	    action = compress;
   1204 	  else
   1205 	    return TRUE;
   1206 	}
   1207 
   1208       if (action == compress)
   1209 	{
   1210 	  if (!bfd_init_section_compress_status (abfd, newsect))
   1211 	    {
   1212 	      _bfd_error_handler
   1213 		/* xgettext:c-format */
   1214 		(_("%pB: unable to initialize compress status for section %s"),
   1215 		 abfd, name);
   1216 	      return FALSE;
   1217 	    }
   1218 	}
   1219       else
   1220 	{
   1221 	  if (!bfd_init_section_decompress_status (abfd, newsect))
   1222 	    {
   1223 	      _bfd_error_handler
   1224 		/* xgettext:c-format */
   1225 		(_("%pB: unable to initialize decompress status for section %s"),
   1226 		 abfd, name);
   1227 	      return FALSE;
   1228 	    }
   1229 	}
   1230 
   1231       if (abfd->is_linker_input)
   1232 	{
   1233 	  if (name[1] == 'z'
   1234 	      && (action == decompress
   1235 		  || (action == compress
   1236 		      && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
   1237 	    {
   1238 	      /* Convert section name from .zdebug_* to .debug_* so
   1239 		 that linker will consider this section as a debug
   1240 		 section.  */
   1241 	      char *new_name = convert_zdebug_to_debug (abfd, name);
   1242 	      if (new_name == NULL)
   1243 		return FALSE;
   1244 	      bfd_rename_section (abfd, newsect, new_name);
   1245 	    }
   1246 	}
   1247       else
   1248 	/* For objdump, don't rename the section.  For objcopy, delay
   1249 	   section rename to elf_fake_sections.  */
   1250 	newsect->flags |= SEC_ELF_RENAME;
   1251     }
   1252 
   1253   return TRUE;
   1254 }
   1255 
   1256 const char *const bfd_elf_section_type_names[] =
   1257 {
   1258   "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
   1259   "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
   1260   "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
   1261 };
   1262 
   1263 /* ELF relocs are against symbols.  If we are producing relocatable
   1264    output, and the reloc is against an external symbol, and nothing
   1265    has given us any additional addend, the resulting reloc will also
   1266    be against the same symbol.  In such a case, we don't want to
   1267    change anything about the way the reloc is handled, since it will
   1268    all be done at final link time.  Rather than put special case code
   1269    into bfd_perform_relocation, all the reloc types use this howto
   1270    function.  It just short circuits the reloc if producing
   1271    relocatable output against an external symbol.  */
   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   return bfd_reloc_continue;
   1292 }
   1293 
   1294 /* Returns TRUE if section A matches section B.
   1296    Names, addresses and links may be different, but everything else
   1297    should be the same.  */
   1298 
   1299 static bfd_boolean
   1300 section_match (const Elf_Internal_Shdr * a,
   1301 	       const Elf_Internal_Shdr * b)
   1302 {
   1303   return
   1304     a->sh_type	       == b->sh_type
   1305     && (a->sh_flags & ~ SHF_INFO_LINK)
   1306     == (b->sh_flags & ~ SHF_INFO_LINK)
   1307     && a->sh_addralign == b->sh_addralign
   1308     && a->sh_size      == b->sh_size
   1309     && a->sh_entsize   == b->sh_entsize
   1310     /* FIXME: Check sh_addr ?  */
   1311     ;
   1312 }
   1313 
   1314 /* Find a section in OBFD that has the same characteristics
   1315    as IHEADER.  Return the index of this section or SHN_UNDEF if
   1316    none can be found.  Check's section HINT first, as this is likely
   1317    to be the correct section.  */
   1318 
   1319 static unsigned int
   1320 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
   1321 	   const unsigned int hint)
   1322 {
   1323   Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
   1324   unsigned int i;
   1325 
   1326   BFD_ASSERT (iheader != NULL);
   1327 
   1328   /* See PR 20922 for a reproducer of the NULL test.  */
   1329   if (hint < elf_numsections (obfd)
   1330       && oheaders[hint] != NULL
   1331       && section_match (oheaders[hint], iheader))
   1332     return hint;
   1333 
   1334   for (i = 1; i < elf_numsections (obfd); i++)
   1335     {
   1336       Elf_Internal_Shdr * oheader = oheaders[i];
   1337 
   1338       if (oheader == NULL)
   1339 	continue;
   1340       if (section_match (oheader, iheader))
   1341 	/* FIXME: Do we care if there is a potential for
   1342 	   multiple matches ?  */
   1343 	return i;
   1344     }
   1345 
   1346   return SHN_UNDEF;
   1347 }
   1348 
   1349 /* PR 19938: Attempt to set the ELF section header fields of an OS or
   1350    Processor specific section, based upon a matching input section.
   1351    Returns TRUE upon success, FALSE otherwise.  */
   1352 
   1353 static bfd_boolean
   1354 copy_special_section_fields (const bfd *ibfd,
   1355 			     bfd *obfd,
   1356 			     const Elf_Internal_Shdr *iheader,
   1357 			     Elf_Internal_Shdr *oheader,
   1358 			     const unsigned int secnum)
   1359 {
   1360   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
   1361   const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
   1362   bfd_boolean changed = FALSE;
   1363   unsigned int sh_link;
   1364 
   1365   if (oheader->sh_type == SHT_NOBITS)
   1366     {
   1367       /* This is a feature for objcopy --only-keep-debug:
   1368 	 When a section's type is changed to NOBITS, we preserve
   1369 	 the sh_link and sh_info fields so that they can be
   1370 	 matched up with the original.
   1371 
   1372 	 Note: Strictly speaking these assignments are wrong.
   1373 	 The sh_link and sh_info fields should point to the
   1374 	 relevent sections in the output BFD, which may not be in
   1375 	 the same location as they were in the input BFD.  But
   1376 	 the whole point of this action is to preserve the
   1377 	 original values of the sh_link and sh_info fields, so
   1378 	 that they can be matched up with the section headers in
   1379 	 the original file.  So strictly speaking we may be
   1380 	 creating an invalid ELF file, but it is only for a file
   1381 	 that just contains debug info and only for sections
   1382 	 without any contents.  */
   1383       if (oheader->sh_link == 0)
   1384 	oheader->sh_link = iheader->sh_link;
   1385       if (oheader->sh_info == 0)
   1386 	oheader->sh_info = iheader->sh_info;
   1387       return TRUE;
   1388     }
   1389 
   1390   /* Allow the target a chance to decide how these fields should be set.  */
   1391   if (bed->elf_backend_copy_special_section_fields != NULL
   1392       && bed->elf_backend_copy_special_section_fields
   1393       (ibfd, obfd, iheader, oheader))
   1394     return TRUE;
   1395 
   1396   /* We have an iheader which might match oheader, and which has non-zero
   1397      sh_info and/or sh_link fields.  Attempt to follow those links and find
   1398      the section in the output bfd which corresponds to the linked section
   1399      in the input bfd.  */
   1400   if (iheader->sh_link != SHN_UNDEF)
   1401     {
   1402       /* See PR 20931 for a reproducer.  */
   1403       if (iheader->sh_link >= elf_numsections (ibfd))
   1404 	{
   1405 	  _bfd_error_handler
   1406 	    /* xgettext:c-format */
   1407 	    (_("%pB: invalid sh_link field (%d) in section number %d"),
   1408 	     ibfd, iheader->sh_link, secnum);
   1409 	  return FALSE;
   1410 	}
   1411 
   1412       sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
   1413       if (sh_link != SHN_UNDEF)
   1414 	{
   1415 	  oheader->sh_link = sh_link;
   1416 	  changed = TRUE;
   1417 	}
   1418       else
   1419 	/* FIXME: Should we install iheader->sh_link
   1420 	   if we could not find a match ?  */
   1421 	_bfd_error_handler
   1422 	  /* xgettext:c-format */
   1423 	  (_("%pB: failed to find link section for section %d"), obfd, secnum);
   1424     }
   1425 
   1426   if (iheader->sh_info)
   1427     {
   1428       /* The sh_info field can hold arbitrary information, but if the
   1429 	 SHF_LINK_INFO flag is set then it should be interpreted as a
   1430 	 section index.  */
   1431       if (iheader->sh_flags & SHF_INFO_LINK)
   1432 	{
   1433 	  sh_link = find_link (obfd, iheaders[iheader->sh_info],
   1434 			       iheader->sh_info);
   1435 	  if (sh_link != SHN_UNDEF)
   1436 	    oheader->sh_flags |= SHF_INFO_LINK;
   1437 	}
   1438       else
   1439 	/* No idea what it means - just copy it.  */
   1440 	sh_link = iheader->sh_info;
   1441 
   1442       if (sh_link != SHN_UNDEF)
   1443 	{
   1444 	  oheader->sh_info = sh_link;
   1445 	  changed = TRUE;
   1446 	}
   1447       else
   1448 	_bfd_error_handler
   1449 	  /* xgettext:c-format */
   1450 	  (_("%pB: failed to find info section for section %d"), obfd, secnum);
   1451     }
   1452 
   1453   return changed;
   1454 }
   1455 
   1456 /* Copy the program header and other data from one object module to
   1457    another.  */
   1458 
   1459 bfd_boolean
   1460 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   1461 {
   1462   const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
   1463   Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
   1464   const struct elf_backend_data *bed;
   1465   unsigned int i;
   1466 
   1467   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   1468     || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   1469     return TRUE;
   1470 
   1471   if (!elf_flags_init (obfd))
   1472     {
   1473       elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
   1474       elf_flags_init (obfd) = TRUE;
   1475     }
   1476 
   1477   elf_gp (obfd) = elf_gp (ibfd);
   1478 
   1479   /* Also copy the EI_OSABI field.  */
   1480   elf_elfheader (obfd)->e_ident[EI_OSABI] =
   1481     elf_elfheader (ibfd)->e_ident[EI_OSABI];
   1482 
   1483   /* If set, copy the EI_ABIVERSION field.  */
   1484   if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
   1485     elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
   1486       = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
   1487 
   1488   /* Copy object attributes.  */
   1489   _bfd_elf_copy_obj_attributes (ibfd, obfd);
   1490 
   1491   if (iheaders == NULL || oheaders == NULL)
   1492     return TRUE;
   1493 
   1494   bed = get_elf_backend_data (obfd);
   1495 
   1496   /* Possibly copy other fields in the section header.  */
   1497   for (i = 1; i < elf_numsections (obfd); i++)
   1498     {
   1499       unsigned int j;
   1500       Elf_Internal_Shdr * oheader = oheaders[i];
   1501 
   1502       /* Ignore ordinary sections.  SHT_NOBITS sections are considered however
   1503 	 because of a special case need for generating separate debug info
   1504 	 files.  See below for more details.  */
   1505       if (oheader == NULL
   1506 	  || (oheader->sh_type != SHT_NOBITS
   1507 	      && oheader->sh_type < SHT_LOOS))
   1508 	continue;
   1509 
   1510       /* Ignore empty sections, and sections whose
   1511 	 fields have already been initialised.  */
   1512       if (oheader->sh_size == 0
   1513 	  || (oheader->sh_info != 0 && oheader->sh_link != 0))
   1514 	continue;
   1515 
   1516       /* Scan for the matching section in the input bfd.
   1517 	 First we try for a direct mapping between the input and output sections.  */
   1518       for (j = 1; j < elf_numsections (ibfd); j++)
   1519 	{
   1520 	  const Elf_Internal_Shdr * iheader = iheaders[j];
   1521 
   1522 	  if (iheader == NULL)
   1523 	    continue;
   1524 
   1525 	  if (oheader->bfd_section != NULL
   1526 	      && iheader->bfd_section != NULL
   1527 	      && iheader->bfd_section->output_section != NULL
   1528 	      && iheader->bfd_section->output_section == oheader->bfd_section)
   1529 	    {
   1530 	      /* We have found a connection from the input section to the
   1531 		 output section.  Attempt to copy the header fields.  If
   1532 		 this fails then do not try any further sections - there
   1533 		 should only be a one-to-one mapping between input and output. */
   1534 	      if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
   1535 		j = elf_numsections (ibfd);
   1536 	      break;
   1537 	    }
   1538 	}
   1539 
   1540       if (j < elf_numsections (ibfd))
   1541 	continue;
   1542 
   1543       /* That failed.  So try to deduce the corresponding input section.
   1544 	 Unfortunately we cannot compare names as the output string table
   1545 	 is empty, so instead we check size, address and type.  */
   1546       for (j = 1; j < elf_numsections (ibfd); j++)
   1547 	{
   1548 	  const Elf_Internal_Shdr * iheader = iheaders[j];
   1549 
   1550 	  if (iheader == NULL)
   1551 	    continue;
   1552 
   1553 	  /* Try matching fields in the input section's header.
   1554 	     Since --only-keep-debug turns all non-debug sections into
   1555 	     SHT_NOBITS sections, the output SHT_NOBITS type matches any
   1556 	     input type.  */
   1557 	  if ((oheader->sh_type == SHT_NOBITS
   1558 	       || iheader->sh_type == oheader->sh_type)
   1559 	      && (iheader->sh_flags & ~ SHF_INFO_LINK)
   1560 	      == (oheader->sh_flags & ~ SHF_INFO_LINK)
   1561 	      && iheader->sh_addralign == oheader->sh_addralign
   1562 	      && iheader->sh_entsize == oheader->sh_entsize
   1563 	      && iheader->sh_size == oheader->sh_size
   1564 	      && iheader->sh_addr == oheader->sh_addr
   1565 	      && (iheader->sh_info != oheader->sh_info
   1566 		  || iheader->sh_link != oheader->sh_link))
   1567 	    {
   1568 	      if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
   1569 		break;
   1570 	    }
   1571 	}
   1572 
   1573       if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
   1574 	{
   1575 	  /* Final attempt.  Call the backend copy function
   1576 	     with a NULL input section.  */
   1577 	  if (bed->elf_backend_copy_special_section_fields != NULL)
   1578 	    bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader);
   1579 	}
   1580     }
   1581 
   1582   return TRUE;
   1583 }
   1584 
   1585 static const char *
   1586 get_segment_type (unsigned int p_type)
   1587 {
   1588   const char *pt;
   1589   switch (p_type)
   1590     {
   1591     case PT_NULL: pt = "NULL"; break;
   1592     case PT_LOAD: pt = "LOAD"; break;
   1593     case PT_DYNAMIC: pt = "DYNAMIC"; break;
   1594     case PT_INTERP: pt = "INTERP"; break;
   1595     case PT_NOTE: pt = "NOTE"; break;
   1596     case PT_SHLIB: pt = "SHLIB"; break;
   1597     case PT_PHDR: pt = "PHDR"; break;
   1598     case PT_TLS: pt = "TLS"; break;
   1599     case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
   1600     case PT_GNU_STACK: pt = "STACK"; break;
   1601     case PT_GNU_RELRO: pt = "RELRO"; break;
   1602     default: pt = NULL; break;
   1603     }
   1604   return pt;
   1605 }
   1606 
   1607 /* Print out the program headers.  */
   1608 
   1609 bfd_boolean
   1610 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
   1611 {
   1612   FILE *f = (FILE *) farg;
   1613   Elf_Internal_Phdr *p;
   1614   asection *s;
   1615   bfd_byte *dynbuf = NULL;
   1616 
   1617   p = elf_tdata (abfd)->phdr;
   1618   if (p != NULL)
   1619     {
   1620       unsigned int i, c;
   1621 
   1622       fprintf (f, _("\nProgram Header:\n"));
   1623       c = elf_elfheader (abfd)->e_phnum;
   1624       for (i = 0; i < c; i++, p++)
   1625 	{
   1626 	  const char *pt = get_segment_type (p->p_type);
   1627 	  char buf[20];
   1628 
   1629 	  if (pt == NULL)
   1630 	    {
   1631 	      sprintf (buf, "0x%lx", p->p_type);
   1632 	      pt = buf;
   1633 	    }
   1634 	  fprintf (f, "%8s off    0x", pt);
   1635 	  bfd_fprintf_vma (abfd, f, p->p_offset);
   1636 	  fprintf (f, " vaddr 0x");
   1637 	  bfd_fprintf_vma (abfd, f, p->p_vaddr);
   1638 	  fprintf (f, " paddr 0x");
   1639 	  bfd_fprintf_vma (abfd, f, p->p_paddr);
   1640 	  fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
   1641 	  fprintf (f, "         filesz 0x");
   1642 	  bfd_fprintf_vma (abfd, f, p->p_filesz);
   1643 	  fprintf (f, " memsz 0x");
   1644 	  bfd_fprintf_vma (abfd, f, p->p_memsz);
   1645 	  fprintf (f, " flags %c%c%c",
   1646 		   (p->p_flags & PF_R) != 0 ? 'r' : '-',
   1647 		   (p->p_flags & PF_W) != 0 ? 'w' : '-',
   1648 		   (p->p_flags & PF_X) != 0 ? 'x' : '-');
   1649 	  if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
   1650 	    fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
   1651 	  fprintf (f, "\n");
   1652 	}
   1653     }
   1654 
   1655   s = bfd_get_section_by_name (abfd, ".dynamic");
   1656   if (s != NULL)
   1657     {
   1658       unsigned int elfsec;
   1659       unsigned long shlink;
   1660       bfd_byte *extdyn, *extdynend;
   1661       size_t extdynsize;
   1662       void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   1663 
   1664       fprintf (f, _("\nDynamic Section:\n"));
   1665 
   1666       if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
   1667 	goto error_return;
   1668 
   1669       elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   1670       if (elfsec == SHN_BAD)
   1671 	goto error_return;
   1672       shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   1673 
   1674       extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   1675       swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   1676 
   1677       extdyn = dynbuf;
   1678       /* PR 17512: file: 6f427532.  */
   1679       if (s->size < extdynsize)
   1680 	goto error_return;
   1681       extdynend = extdyn + s->size;
   1682       /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
   1683 	 Fix range check.  */
   1684       for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
   1685 	{
   1686 	  Elf_Internal_Dyn dyn;
   1687 	  const char *name = "";
   1688 	  char ab[20];
   1689 	  bfd_boolean stringp;
   1690 	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   1691 
   1692 	  (*swap_dyn_in) (abfd, extdyn, &dyn);
   1693 
   1694 	  if (dyn.d_tag == DT_NULL)
   1695 	    break;
   1696 
   1697 	  stringp = FALSE;
   1698 	  switch (dyn.d_tag)
   1699 	    {
   1700 	    default:
   1701 	      if (bed->elf_backend_get_target_dtag)
   1702 		name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
   1703 
   1704 	      if (!strcmp (name, ""))
   1705 		{
   1706 		  sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
   1707 		  name = ab;
   1708 		}
   1709 	      break;
   1710 
   1711 	    case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
   1712 	    case DT_PLTRELSZ: name = "PLTRELSZ"; break;
   1713 	    case DT_PLTGOT: name = "PLTGOT"; break;
   1714 	    case DT_HASH: name = "HASH"; break;
   1715 	    case DT_STRTAB: name = "STRTAB"; break;
   1716 	    case DT_SYMTAB: name = "SYMTAB"; break;
   1717 	    case DT_RELA: name = "RELA"; break;
   1718 	    case DT_RELASZ: name = "RELASZ"; break;
   1719 	    case DT_RELAENT: name = "RELAENT"; break;
   1720 	    case DT_STRSZ: name = "STRSZ"; break;
   1721 	    case DT_SYMENT: name = "SYMENT"; break;
   1722 	    case DT_INIT: name = "INIT"; break;
   1723 	    case DT_FINI: name = "FINI"; break;
   1724 	    case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
   1725 	    case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
   1726 	    case DT_SYMBOLIC: name = "SYMBOLIC"; break;
   1727 	    case DT_REL: name = "REL"; break;
   1728 	    case DT_RELSZ: name = "RELSZ"; break;
   1729 	    case DT_RELENT: name = "RELENT"; break;
   1730 	    case DT_PLTREL: name = "PLTREL"; break;
   1731 	    case DT_DEBUG: name = "DEBUG"; break;
   1732 	    case DT_TEXTREL: name = "TEXTREL"; break;
   1733 	    case DT_JMPREL: name = "JMPREL"; break;
   1734 	    case DT_BIND_NOW: name = "BIND_NOW"; break;
   1735 	    case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
   1736 	    case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
   1737 	    case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
   1738 	    case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
   1739 	    case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
   1740 	    case DT_FLAGS: name = "FLAGS"; break;
   1741 	    case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
   1742 	    case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
   1743 	    case DT_CHECKSUM: name = "CHECKSUM"; break;
   1744 	    case DT_PLTPADSZ: name = "PLTPADSZ"; break;
   1745 	    case DT_MOVEENT: name = "MOVEENT"; break;
   1746 	    case DT_MOVESZ: name = "MOVESZ"; break;
   1747 	    case DT_FEATURE: name = "FEATURE"; break;
   1748 	    case DT_POSFLAG_1: name = "POSFLAG_1"; break;
   1749 	    case DT_SYMINSZ: name = "SYMINSZ"; break;
   1750 	    case DT_SYMINENT: name = "SYMINENT"; break;
   1751 	    case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
   1752 	    case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
   1753 	    case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
   1754 	    case DT_PLTPAD: name = "PLTPAD"; break;
   1755 	    case DT_MOVETAB: name = "MOVETAB"; break;
   1756 	    case DT_SYMINFO: name = "SYMINFO"; break;
   1757 	    case DT_RELACOUNT: name = "RELACOUNT"; break;
   1758 	    case DT_RELCOUNT: name = "RELCOUNT"; break;
   1759 	    case DT_FLAGS_1: name = "FLAGS_1"; break;
   1760 	    case DT_VERSYM: name = "VERSYM"; break;
   1761 	    case DT_VERDEF: name = "VERDEF"; break;
   1762 	    case DT_VERDEFNUM: name = "VERDEFNUM"; break;
   1763 	    case DT_VERNEED: name = "VERNEED"; break;
   1764 	    case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
   1765 	    case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
   1766 	    case DT_USED: name = "USED"; break;
   1767 	    case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
   1768 	    case DT_GNU_HASH: name = "GNU_HASH"; break;
   1769 	    }
   1770 
   1771 	  fprintf (f, "  %-20s ", name);
   1772 	  if (! stringp)
   1773 	    {
   1774 	      fprintf (f, "0x");
   1775 	      bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
   1776 	    }
   1777 	  else
   1778 	    {
   1779 	      const char *string;
   1780 	      unsigned int tagv = dyn.d_un.d_val;
   1781 
   1782 	      string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   1783 	      if (string == NULL)
   1784 		goto error_return;
   1785 	      fprintf (f, "%s", string);
   1786 	    }
   1787 	  fprintf (f, "\n");
   1788 	}
   1789 
   1790       free (dynbuf);
   1791       dynbuf = NULL;
   1792     }
   1793 
   1794   if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
   1795       || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
   1796     {
   1797       if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
   1798 	return FALSE;
   1799     }
   1800 
   1801   if (elf_dynverdef (abfd) != 0)
   1802     {
   1803       Elf_Internal_Verdef *t;
   1804 
   1805       fprintf (f, _("\nVersion definitions:\n"));
   1806       for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
   1807 	{
   1808 	  fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
   1809 		   t->vd_flags, t->vd_hash,
   1810 		   t->vd_nodename ? t->vd_nodename : "<corrupt>");
   1811 	  if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
   1812 	    {
   1813 	      Elf_Internal_Verdaux *a;
   1814 
   1815 	      fprintf (f, "\t");
   1816 	      for (a = t->vd_auxptr->vda_nextptr;
   1817 		   a != NULL;
   1818 		   a = a->vda_nextptr)
   1819 		fprintf (f, "%s ",
   1820 			 a->vda_nodename ? a->vda_nodename : "<corrupt>");
   1821 	      fprintf (f, "\n");
   1822 	    }
   1823 	}
   1824     }
   1825 
   1826   if (elf_dynverref (abfd) != 0)
   1827     {
   1828       Elf_Internal_Verneed *t;
   1829 
   1830       fprintf (f, _("\nVersion References:\n"));
   1831       for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
   1832 	{
   1833 	  Elf_Internal_Vernaux *a;
   1834 
   1835 	  fprintf (f, _("  required from %s:\n"),
   1836 		   t->vn_filename ? t->vn_filename : "<corrupt>");
   1837 	  for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   1838 	    fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
   1839 		     a->vna_flags, a->vna_other,
   1840 		     a->vna_nodename ? a->vna_nodename : "<corrupt>");
   1841 	}
   1842     }
   1843 
   1844   return TRUE;
   1845 
   1846  error_return:
   1847   if (dynbuf != NULL)
   1848     free (dynbuf);
   1849   return FALSE;
   1850 }
   1851 
   1852 /* Get version string.  */
   1853 
   1854 const char *
   1855 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
   1856 				    bfd_boolean *hidden)
   1857 {
   1858   const char *version_string = NULL;
   1859   if (elf_dynversym (abfd) != 0
   1860       && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
   1861     {
   1862       unsigned int vernum = ((elf_symbol_type *) symbol)->version;
   1863 
   1864       *hidden = (vernum & VERSYM_HIDDEN) != 0;
   1865       vernum &= VERSYM_VERSION;
   1866 
   1867       if (vernum == 0)
   1868 	version_string = "";
   1869       else if (vernum == 1
   1870 	       && (vernum > elf_tdata (abfd)->cverdefs
   1871 		   || (elf_tdata (abfd)->verdef[0].vd_flags
   1872 		       == VER_FLG_BASE)))
   1873 	version_string = "Base";
   1874       else if (vernum <= elf_tdata (abfd)->cverdefs)
   1875 	version_string =
   1876 	  elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
   1877       else
   1878 	{
   1879 	  Elf_Internal_Verneed *t;
   1880 
   1881 	  version_string = "";
   1882 	  for (t = elf_tdata (abfd)->verref;
   1883 	       t != NULL;
   1884 	       t = t->vn_nextref)
   1885 	    {
   1886 	      Elf_Internal_Vernaux *a;
   1887 
   1888 	      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   1889 		{
   1890 		  if (a->vna_other == vernum)
   1891 		    {
   1892 		      version_string = a->vna_nodename;
   1893 		      break;
   1894 		    }
   1895 		}
   1896 	    }
   1897 	}
   1898     }
   1899   return version_string;
   1900 }
   1901 
   1902 /* Display ELF-specific fields of a symbol.  */
   1903 
   1904 void
   1905 bfd_elf_print_symbol (bfd *abfd,
   1906 		      void *filep,
   1907 		      asymbol *symbol,
   1908 		      bfd_print_symbol_type how)
   1909 {
   1910   FILE *file = (FILE *) filep;
   1911   switch (how)
   1912     {
   1913     case bfd_print_symbol_name:
   1914       fprintf (file, "%s", symbol->name);
   1915       break;
   1916     case bfd_print_symbol_more:
   1917       fprintf (file, "elf ");
   1918       bfd_fprintf_vma (abfd, file, symbol->value);
   1919       fprintf (file, " %x", symbol->flags);
   1920       break;
   1921     case bfd_print_symbol_all:
   1922       {
   1923 	const char *section_name;
   1924 	const char *name = NULL;
   1925 	const struct elf_backend_data *bed;
   1926 	unsigned char st_other;
   1927 	bfd_vma val;
   1928 	const char *version_string;
   1929 	bfd_boolean hidden;
   1930 
   1931 	section_name = symbol->section ? symbol->section->name : "(*none*)";
   1932 
   1933 	bed = get_elf_backend_data (abfd);
   1934 	if (bed->elf_backend_print_symbol_all)
   1935 	  name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
   1936 
   1937 	if (name == NULL)
   1938 	  {
   1939 	    name = symbol->name;
   1940 	    bfd_print_symbol_vandf (abfd, file, symbol);
   1941 	  }
   1942 
   1943 	fprintf (file, " %s\t", section_name);
   1944 	/* Print the "other" value for a symbol.  For common symbols,
   1945 	   we've already printed the size; now print the alignment.
   1946 	   For other symbols, we have no specified alignment, and
   1947 	   we've printed the address; now print the size.  */
   1948 	if (symbol->section && bfd_is_com_section (symbol->section))
   1949 	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
   1950 	else
   1951 	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
   1952 	bfd_fprintf_vma (abfd, file, val);
   1953 
   1954 	/* If we have version information, print it.  */
   1955 	version_string = _bfd_elf_get_symbol_version_string (abfd,
   1956 							     symbol,
   1957 							     &hidden);
   1958 	if (version_string)
   1959 	  {
   1960 	    if (!hidden)
   1961 	      fprintf (file, "  %-11s", version_string);
   1962 	    else
   1963 	      {
   1964 		int i;
   1965 
   1966 		fprintf (file, " (%s)", version_string);
   1967 		for (i = 10 - strlen (version_string); i > 0; --i)
   1968 		  putc (' ', file);
   1969 	      }
   1970 	  }
   1971 
   1972 	/* If the st_other field is not zero, print it.  */
   1973 	st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
   1974 
   1975 	switch (st_other)
   1976 	  {
   1977 	  case 0: break;
   1978 	  case STV_INTERNAL:  fprintf (file, " .internal");  break;
   1979 	  case STV_HIDDEN:    fprintf (file, " .hidden");    break;
   1980 	  case STV_PROTECTED: fprintf (file, " .protected"); break;
   1981 	  default:
   1982 	    /* Some other non-defined flags are also present, so print
   1983 	       everything hex.  */
   1984 	    fprintf (file, " 0x%02x", (unsigned int) st_other);
   1985 	  }
   1986 
   1987 	fprintf (file, " %s", name);
   1988       }
   1989       break;
   1990     }
   1991 }
   1992 
   1993 /* ELF .o/exec file reading */
   1995 
   1996 /* Create a new bfd section from an ELF section header.  */
   1997 
   1998 bfd_boolean
   1999 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
   2000 {
   2001   Elf_Internal_Shdr *hdr;
   2002   Elf_Internal_Ehdr *ehdr;
   2003   const struct elf_backend_data *bed;
   2004   const char *name;
   2005   bfd_boolean ret = TRUE;
   2006   static bfd_boolean * sections_being_created = NULL;
   2007   static bfd * sections_being_created_abfd = NULL;
   2008   static unsigned int nesting = 0;
   2009 
   2010   if (shindex >= elf_numsections (abfd))
   2011     return FALSE;
   2012 
   2013   if (++ nesting > 3)
   2014     {
   2015       /* PR17512: A corrupt ELF binary might contain a recursive group of
   2016 	 sections, with each the string indicies pointing to the next in the
   2017 	 loop.  Detect this here, by refusing to load a section that we are
   2018 	 already in the process of loading.  We only trigger this test if
   2019 	 we have nested at least three sections deep as normal ELF binaries
   2020 	 can expect to recurse at least once.
   2021 
   2022 	 FIXME: It would be better if this array was attached to the bfd,
   2023 	 rather than being held in a static pointer.  */
   2024 
   2025       if (sections_being_created_abfd != abfd)
   2026 	sections_being_created = NULL;
   2027       if (sections_being_created == NULL)
   2028 	{
   2029 	  /* FIXME: It would be more efficient to attach this array to the bfd somehow.  */
   2030 	  sections_being_created = (bfd_boolean *)
   2031 	    bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean));
   2032 	  sections_being_created_abfd = abfd;
   2033 	}
   2034       if (sections_being_created [shindex])
   2035 	{
   2036 	  _bfd_error_handler
   2037 	    (_("%pB: warning: loop in section dependencies detected"), abfd);
   2038 	  return FALSE;
   2039 	}
   2040       sections_being_created [shindex] = TRUE;
   2041     }
   2042 
   2043   hdr = elf_elfsections (abfd)[shindex];
   2044   ehdr = elf_elfheader (abfd);
   2045   name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
   2046 					  hdr->sh_name);
   2047   if (name == NULL)
   2048     goto fail;
   2049 
   2050   bed = get_elf_backend_data (abfd);
   2051   switch (hdr->sh_type)
   2052     {
   2053     case SHT_NULL:
   2054       /* Inactive section. Throw it away.  */
   2055       goto success;
   2056 
   2057     case SHT_PROGBITS:		/* Normal section with contents.  */
   2058     case SHT_NOBITS:		/* .bss section.  */
   2059     case SHT_HASH:		/* .hash section.  */
   2060     case SHT_NOTE:		/* .note section.  */
   2061     case SHT_INIT_ARRAY:	/* .init_array section.  */
   2062     case SHT_FINI_ARRAY:	/* .fini_array section.  */
   2063     case SHT_PREINIT_ARRAY:	/* .preinit_array section.  */
   2064     case SHT_GNU_LIBLIST:	/* .gnu.liblist section.  */
   2065     case SHT_GNU_HASH:		/* .gnu.hash section.  */
   2066       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2067       goto success;
   2068 
   2069     case SHT_DYNAMIC:	/* Dynamic linking information.  */
   2070       if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2071 	goto fail;
   2072 
   2073       if (hdr->sh_link > elf_numsections (abfd))
   2074 	{
   2075 	  /* PR 10478: Accept Solaris binaries with a sh_link
   2076 	     field set to SHN_BEFORE or SHN_AFTER.  */
   2077 	  switch (bfd_get_arch (abfd))
   2078 	    {
   2079 	    case bfd_arch_i386:
   2080 	    case bfd_arch_sparc:
   2081 	      if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
   2082 		  || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
   2083 		break;
   2084 	      /* Otherwise fall through.  */
   2085 	    default:
   2086 	      goto fail;
   2087 	    }
   2088 	}
   2089       else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
   2090 	goto fail;
   2091       else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
   2092 	{
   2093 	  Elf_Internal_Shdr *dynsymhdr;
   2094 
   2095 	  /* The shared libraries distributed with hpux11 have a bogus
   2096 	     sh_link field for the ".dynamic" section.  Find the
   2097 	     string table for the ".dynsym" section instead.  */
   2098 	  if (elf_dynsymtab (abfd) != 0)
   2099 	    {
   2100 	      dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
   2101 	      hdr->sh_link = dynsymhdr->sh_link;
   2102 	    }
   2103 	  else
   2104 	    {
   2105 	      unsigned int i, num_sec;
   2106 
   2107 	      num_sec = elf_numsections (abfd);
   2108 	      for (i = 1; i < num_sec; i++)
   2109 		{
   2110 		  dynsymhdr = elf_elfsections (abfd)[i];
   2111 		  if (dynsymhdr->sh_type == SHT_DYNSYM)
   2112 		    {
   2113 		      hdr->sh_link = dynsymhdr->sh_link;
   2114 		      break;
   2115 		    }
   2116 		}
   2117 	    }
   2118 	}
   2119       goto success;
   2120 
   2121     case SHT_SYMTAB:		/* A symbol table.  */
   2122       if (elf_onesymtab (abfd) == shindex)
   2123 	goto success;
   2124 
   2125       if (hdr->sh_entsize != bed->s->sizeof_sym)
   2126 	goto fail;
   2127 
   2128       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
   2129 	{
   2130 	  if (hdr->sh_size != 0)
   2131 	    goto fail;
   2132 	  /* Some assemblers erroneously set sh_info to one with a
   2133 	     zero sh_size.  ld sees this as a global symbol count
   2134 	     of (unsigned) -1.  Fix it here.  */
   2135 	  hdr->sh_info = 0;
   2136 	  goto success;
   2137 	}
   2138 
   2139       /* PR 18854: A binary might contain more than one symbol table.
   2140 	 Unusual, but possible.  Warn, but continue.  */
   2141       if (elf_onesymtab (abfd) != 0)
   2142 	{
   2143 	  _bfd_error_handler
   2144 	    /* xgettext:c-format */
   2145 	    (_("%pB: warning: multiple symbol tables detected"
   2146 	       " - ignoring the table in section %u"),
   2147 	     abfd, shindex);
   2148 	  goto success;
   2149 	}
   2150       elf_onesymtab (abfd) = shindex;
   2151       elf_symtab_hdr (abfd) = *hdr;
   2152       elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
   2153       abfd->flags |= HAS_SYMS;
   2154 
   2155       /* Sometimes a shared object will map in the symbol table.  If
   2156 	 SHF_ALLOC is set, and this is a shared object, then we also
   2157 	 treat this section as a BFD section.  We can not base the
   2158 	 decision purely on SHF_ALLOC, because that flag is sometimes
   2159 	 set in a relocatable object file, which would confuse the
   2160 	 linker.  */
   2161       if ((hdr->sh_flags & SHF_ALLOC) != 0
   2162 	  && (abfd->flags & DYNAMIC) != 0
   2163 	  && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2164 						shindex))
   2165 	goto fail;
   2166 
   2167       /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
   2168 	 can't read symbols without that section loaded as well.  It
   2169 	 is most likely specified by the next section header.  */
   2170       {
   2171 	elf_section_list * entry;
   2172 	unsigned int i, num_sec;
   2173 
   2174 	for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
   2175 	  if (entry->hdr.sh_link == shindex)
   2176 	    goto success;
   2177 
   2178 	num_sec = elf_numsections (abfd);
   2179 	for (i = shindex + 1; i < num_sec; i++)
   2180 	  {
   2181 	    Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2182 
   2183 	    if (hdr2->sh_type == SHT_SYMTAB_SHNDX
   2184 		&& hdr2->sh_link == shindex)
   2185 	      break;
   2186 	  }
   2187 
   2188 	if (i == num_sec)
   2189 	  for (i = 1; i < shindex; i++)
   2190 	    {
   2191 	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2192 
   2193 	      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
   2194 		  && hdr2->sh_link == shindex)
   2195 		break;
   2196 	    }
   2197 
   2198 	if (i != shindex)
   2199 	  ret = bfd_section_from_shdr (abfd, i);
   2200 	/* else FIXME: we have failed to find the symbol table - should we issue an error ? */
   2201 	goto success;
   2202       }
   2203 
   2204     case SHT_DYNSYM:		/* A dynamic symbol table.  */
   2205       if (elf_dynsymtab (abfd) == shindex)
   2206 	goto success;
   2207 
   2208       if (hdr->sh_entsize != bed->s->sizeof_sym)
   2209 	goto fail;
   2210 
   2211       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
   2212 	{
   2213 	  if (hdr->sh_size != 0)
   2214 	    goto fail;
   2215 
   2216 	  /* Some linkers erroneously set sh_info to one with a
   2217 	     zero sh_size.  ld sees this as a global symbol count
   2218 	     of (unsigned) -1.  Fix it here.  */
   2219 	  hdr->sh_info = 0;
   2220 	  goto success;
   2221 	}
   2222 
   2223       /* PR 18854: A binary might contain more than one dynamic symbol table.
   2224 	 Unusual, but possible.  Warn, but continue.  */
   2225       if (elf_dynsymtab (abfd) != 0)
   2226 	{
   2227 	  _bfd_error_handler
   2228 	    /* xgettext:c-format */
   2229 	    (_("%pB: warning: multiple dynamic symbol tables detected"
   2230 	       " - ignoring the table in section %u"),
   2231 	     abfd, shindex);
   2232 	  goto success;
   2233 	}
   2234       elf_dynsymtab (abfd) = shindex;
   2235       elf_tdata (abfd)->dynsymtab_hdr = *hdr;
   2236       elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   2237       abfd->flags |= HAS_SYMS;
   2238 
   2239       /* Besides being a symbol table, we also treat this as a regular
   2240 	 section, so that objcopy can handle it.  */
   2241       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2242       goto success;
   2243 
   2244     case SHT_SYMTAB_SHNDX:	/* Symbol section indices when >64k sections.  */
   2245       {
   2246 	elf_section_list * entry;
   2247 
   2248 	for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
   2249 	  if (entry->ndx == shindex)
   2250 	    goto success;
   2251 
   2252 	entry = bfd_alloc (abfd, sizeof * entry);
   2253 	if (entry == NULL)
   2254 	  goto fail;
   2255 	entry->ndx = shindex;
   2256 	entry->hdr = * hdr;
   2257 	entry->next = elf_symtab_shndx_list (abfd);
   2258 	elf_symtab_shndx_list (abfd) = entry;
   2259 	elf_elfsections (abfd)[shindex] = & entry->hdr;
   2260 	goto success;
   2261       }
   2262 
   2263     case SHT_STRTAB:		/* A string table.  */
   2264       if (hdr->bfd_section != NULL)
   2265 	goto success;
   2266 
   2267       if (ehdr->e_shstrndx == shindex)
   2268 	{
   2269 	  elf_tdata (abfd)->shstrtab_hdr = *hdr;
   2270 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
   2271 	  goto success;
   2272 	}
   2273 
   2274       if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
   2275 	{
   2276 	symtab_strtab:
   2277 	  elf_tdata (abfd)->strtab_hdr = *hdr;
   2278 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
   2279 	  goto success;
   2280 	}
   2281 
   2282       if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
   2283 	{
   2284 	dynsymtab_strtab:
   2285 	  elf_tdata (abfd)->dynstrtab_hdr = *hdr;
   2286 	  hdr = &elf_tdata (abfd)->dynstrtab_hdr;
   2287 	  elf_elfsections (abfd)[shindex] = hdr;
   2288 	  /* We also treat this as a regular section, so that objcopy
   2289 	     can handle it.  */
   2290 	  ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2291 						 shindex);
   2292 	  goto success;
   2293 	}
   2294 
   2295       /* If the string table isn't one of the above, then treat it as a
   2296 	 regular section.  We need to scan all the headers to be sure,
   2297 	 just in case this strtab section appeared before the above.  */
   2298       if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
   2299 	{
   2300 	  unsigned int i, num_sec;
   2301 
   2302 	  num_sec = elf_numsections (abfd);
   2303 	  for (i = 1; i < num_sec; i++)
   2304 	    {
   2305 	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2306 	      if (hdr2->sh_link == shindex)
   2307 		{
   2308 		  /* Prevent endless recursion on broken objects.  */
   2309 		  if (i == shindex)
   2310 		    goto fail;
   2311 		  if (! bfd_section_from_shdr (abfd, i))
   2312 		    goto fail;
   2313 		  if (elf_onesymtab (abfd) == i)
   2314 		    goto symtab_strtab;
   2315 		  if (elf_dynsymtab (abfd) == i)
   2316 		    goto dynsymtab_strtab;
   2317 		}
   2318 	    }
   2319 	}
   2320       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2321       goto success;
   2322 
   2323     case SHT_REL:
   2324     case SHT_RELA:
   2325       /* *These* do a lot of work -- but build no sections!  */
   2326       {
   2327 	asection *target_sect;
   2328 	Elf_Internal_Shdr *hdr2, **p_hdr;
   2329 	unsigned int num_sec = elf_numsections (abfd);
   2330 	struct bfd_elf_section_data *esdt;
   2331 
   2332 	if (hdr->sh_entsize
   2333 	    != (bfd_size_type) (hdr->sh_type == SHT_REL
   2334 				? bed->s->sizeof_rel : bed->s->sizeof_rela))
   2335 	  goto fail;
   2336 
   2337 	/* Check for a bogus link to avoid crashing.  */
   2338 	if (hdr->sh_link >= num_sec)
   2339 	  {
   2340 	    _bfd_error_handler
   2341 	      /* xgettext:c-format */
   2342 	      (_("%pB: invalid link %u for reloc section %s (index %u)"),
   2343 	       abfd, hdr->sh_link, name, shindex);
   2344 	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2345 						   shindex);
   2346 	    goto success;
   2347 	  }
   2348 
   2349 	/* For some incomprehensible reason Oracle distributes
   2350 	   libraries for Solaris in which some of the objects have
   2351 	   bogus sh_link fields.  It would be nice if we could just
   2352 	   reject them, but, unfortunately, some people need to use
   2353 	   them.  We scan through the section headers; if we find only
   2354 	   one suitable symbol table, we clobber the sh_link to point
   2355 	   to it.  I hope this doesn't break anything.
   2356 
   2357 	   Don't do it on executable nor shared library.  */
   2358 	if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
   2359 	    && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
   2360 	    && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
   2361 	  {
   2362 	    unsigned int scan;
   2363 	    int found;
   2364 
   2365 	    found = 0;
   2366 	    for (scan = 1; scan < num_sec; scan++)
   2367 	      {
   2368 		if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
   2369 		    || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
   2370 		  {
   2371 		    if (found != 0)
   2372 		      {
   2373 			found = 0;
   2374 			break;
   2375 		      }
   2376 		    found = scan;
   2377 		  }
   2378 	      }
   2379 	    if (found != 0)
   2380 	      hdr->sh_link = found;
   2381 	  }
   2382 
   2383 	/* Get the symbol table.  */
   2384 	if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
   2385 	     || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
   2386 	    && ! bfd_section_from_shdr (abfd, hdr->sh_link))
   2387 	  goto fail;
   2388 
   2389 	/* If this reloc section does not use the main symbol table we
   2390 	   don't treat it as a reloc section.  BFD can't adequately
   2391 	   represent such a section, so at least for now, we don't
   2392 	   try.  We just present it as a normal section.  We also
   2393 	   can't use it as a reloc section if it points to the null
   2394 	   section, an invalid section, another reloc section, or its
   2395 	   sh_link points to the null section.  */
   2396 	if (hdr->sh_link != elf_onesymtab (abfd)
   2397 	    || hdr->sh_link == SHN_UNDEF
   2398 	    || hdr->sh_info == SHN_UNDEF
   2399 	    || hdr->sh_info >= num_sec
   2400 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
   2401 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
   2402 	  {
   2403 	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2404 						   shindex);
   2405 	    goto success;
   2406 	  }
   2407 
   2408 	if (! bfd_section_from_shdr (abfd, hdr->sh_info))
   2409 	  goto fail;
   2410 
   2411 	target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
   2412 	if (target_sect == NULL)
   2413 	  goto fail;
   2414 
   2415 	esdt = elf_section_data (target_sect);
   2416 	if (hdr->sh_type == SHT_RELA)
   2417 	  p_hdr = &esdt->rela.hdr;
   2418 	else
   2419 	  p_hdr = &esdt->rel.hdr;
   2420 
   2421 	/* PR 17512: file: 0b4f81b7.  */
   2422 	if (*p_hdr != NULL)
   2423 	  goto fail;
   2424 	hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
   2425 	if (hdr2 == NULL)
   2426 	  goto fail;
   2427 	*hdr2 = *hdr;
   2428 	*p_hdr = hdr2;
   2429 	elf_elfsections (abfd)[shindex] = hdr2;
   2430 	target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
   2431 				     * bed->s->int_rels_per_ext_rel);
   2432 	target_sect->flags |= SEC_RELOC;
   2433 	target_sect->relocation = NULL;
   2434 	target_sect->rel_filepos = hdr->sh_offset;
   2435 	/* In the section to which the relocations apply, mark whether
   2436 	   its relocations are of the REL or RELA variety.  */
   2437 	if (hdr->sh_size != 0)
   2438 	  {
   2439 	    if (hdr->sh_type == SHT_RELA)
   2440 	      target_sect->use_rela_p = 1;
   2441 	  }
   2442 	abfd->flags |= HAS_RELOC;
   2443 	goto success;
   2444       }
   2445 
   2446     case SHT_GNU_verdef:
   2447       elf_dynverdef (abfd) = shindex;
   2448       elf_tdata (abfd)->dynverdef_hdr = *hdr;
   2449       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2450       goto success;
   2451 
   2452     case SHT_GNU_versym:
   2453       if (hdr->sh_entsize != sizeof (Elf_External_Versym))
   2454 	goto fail;
   2455 
   2456       elf_dynversym (abfd) = shindex;
   2457       elf_tdata (abfd)->dynversym_hdr = *hdr;
   2458       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2459       goto success;
   2460 
   2461     case SHT_GNU_verneed:
   2462       elf_dynverref (abfd) = shindex;
   2463       elf_tdata (abfd)->dynverref_hdr = *hdr;
   2464       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2465       goto success;
   2466 
   2467     case SHT_SHLIB:
   2468       goto success;
   2469 
   2470     case SHT_GROUP:
   2471       if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
   2472 	goto fail;
   2473 
   2474       if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2475 	goto fail;
   2476 
   2477       goto success;
   2478 
   2479     default:
   2480       /* Possibly an attributes section.  */
   2481       if (hdr->sh_type == SHT_GNU_ATTRIBUTES
   2482 	  || hdr->sh_type == bed->obj_attrs_section_type)
   2483 	{
   2484 	  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2485 	    goto fail;
   2486 	  _bfd_elf_parse_attributes (abfd, hdr);
   2487 	  goto success;
   2488 	}
   2489 
   2490       /* Check for any processor-specific section types.  */
   2491       if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
   2492 	goto success;
   2493 
   2494       if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
   2495 	{
   2496 	  if ((hdr->sh_flags & SHF_ALLOC) != 0)
   2497 	    /* FIXME: How to properly handle allocated section reserved
   2498 	       for applications?  */
   2499 	    _bfd_error_handler
   2500 	      /* xgettext:c-format */
   2501 	      (_("%pB: unknown type [%#x] section `%s'"),
   2502 	       abfd, hdr->sh_type, name);
   2503 	  else
   2504 	    {
   2505 	      /* Allow sections reserved for applications.  */
   2506 	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2507 						     shindex);
   2508 	      goto success;
   2509 	    }
   2510 	}
   2511       else if (hdr->sh_type >= SHT_LOPROC
   2512 	       && hdr->sh_type <= SHT_HIPROC)
   2513 	/* FIXME: We should handle this section.  */
   2514 	_bfd_error_handler
   2515 	  /* xgettext:c-format */
   2516 	  (_("%pB: unknown type [%#x] section `%s'"),
   2517 	   abfd, hdr->sh_type, name);
   2518       else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
   2519 	{
   2520 	  /* Unrecognised OS-specific sections.  */
   2521 	  if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
   2522 	    /* SHF_OS_NONCONFORMING indicates that special knowledge is
   2523 	       required to correctly process the section and the file should
   2524 	       be rejected with an error message.  */
   2525 	    _bfd_error_handler
   2526 	      /* xgettext:c-format */
   2527 	      (_("%pB: unknown type [%#x] section `%s'"),
   2528 	       abfd, hdr->sh_type, name);
   2529 	  else
   2530 	    {
   2531 	      /* Otherwise it should be processed.  */
   2532 	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2533 	      goto success;
   2534 	    }
   2535 	}
   2536       else
   2537 	/* FIXME: We should handle this section.  */
   2538 	_bfd_error_handler
   2539 	  /* xgettext:c-format */
   2540 	  (_("%pB: unknown type [%#x] section `%s'"),
   2541 	   abfd, hdr->sh_type, name);
   2542 
   2543       goto fail;
   2544     }
   2545 
   2546  fail:
   2547   ret = FALSE;
   2548  success:
   2549   if (sections_being_created && sections_being_created_abfd == abfd)
   2550     sections_being_created [shindex] = FALSE;
   2551   if (-- nesting == 0)
   2552     {
   2553       sections_being_created = NULL;
   2554       sections_being_created_abfd = abfd;
   2555     }
   2556   return ret;
   2557 }
   2558 
   2559 /* Return the local symbol specified by ABFD, R_SYMNDX.  */
   2560 
   2561 Elf_Internal_Sym *
   2562 bfd_sym_from_r_symndx (struct sym_cache *cache,
   2563 		       bfd *abfd,
   2564 		       unsigned long r_symndx)
   2565 {
   2566   unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
   2567 
   2568   if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
   2569     {
   2570       Elf_Internal_Shdr *symtab_hdr;
   2571       unsigned char esym[sizeof (Elf64_External_Sym)];
   2572       Elf_External_Sym_Shndx eshndx;
   2573 
   2574       symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   2575       if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
   2576 				&cache->sym[ent], esym, &eshndx) == NULL)
   2577 	return NULL;
   2578 
   2579       if (cache->abfd != abfd)
   2580 	{
   2581 	  memset (cache->indx, -1, sizeof (cache->indx));
   2582 	  cache->abfd = abfd;
   2583 	}
   2584       cache->indx[ent] = r_symndx;
   2585     }
   2586 
   2587   return &cache->sym[ent];
   2588 }
   2589 
   2590 /* Given an ELF section number, retrieve the corresponding BFD
   2591    section.  */
   2592 
   2593 asection *
   2594 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
   2595 {
   2596   if (sec_index >= elf_numsections (abfd))
   2597     return NULL;
   2598   return elf_elfsections (abfd)[sec_index]->bfd_section;
   2599 }
   2600 
   2601 static const struct bfd_elf_special_section special_sections_b[] =
   2602 {
   2603   { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   2604   { NULL,		    0,	0, 0,		 0 }
   2605 };
   2606 
   2607 static const struct bfd_elf_special_section special_sections_c[] =
   2608 {
   2609   { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
   2610   { NULL,			0, 0, 0,	    0 }
   2611 };
   2612 
   2613 static const struct bfd_elf_special_section special_sections_d[] =
   2614 {
   2615   { STRING_COMMA_LEN (".data"),		-2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2616   { STRING_COMMA_LEN (".data1"),	 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2617   /* There are more DWARF sections than these, but they needn't be added here
   2618      unless you have to cope with broken compilers that don't emit section
   2619      attributes or you want to help the user writing assembler.  */
   2620   { STRING_COMMA_LEN (".debug"),	 0, SHT_PROGBITS, 0 },
   2621   { STRING_COMMA_LEN (".debug_line"),	 0, SHT_PROGBITS, 0 },
   2622   { STRING_COMMA_LEN (".debug_info"),	 0, SHT_PROGBITS, 0 },
   2623   { STRING_COMMA_LEN (".debug_abbrev"),	 0, SHT_PROGBITS, 0 },
   2624   { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
   2625   { STRING_COMMA_LEN (".dynamic"),	 0, SHT_DYNAMIC,  SHF_ALLOC },
   2626   { STRING_COMMA_LEN (".dynstr"),	 0, SHT_STRTAB,	  SHF_ALLOC },
   2627   { STRING_COMMA_LEN (".dynsym"),	 0, SHT_DYNSYM,	  SHF_ALLOC },
   2628   { NULL,		       0,	 0, 0,		  0 }
   2629 };
   2630 
   2631 static const struct bfd_elf_special_section special_sections_f[] =
   2632 {
   2633   { STRING_COMMA_LEN (".fini"),	       0, SHT_PROGBITS,	  SHF_ALLOC + SHF_EXECINSTR },
   2634   { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
   2635   { NULL,			   0 , 0, 0,		  0 }
   2636 };
   2637 
   2638 static const struct bfd_elf_special_section special_sections_g[] =
   2639 {
   2640   { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
   2641   { STRING_COMMA_LEN (".gnu.lto_"),	  -1, SHT_PROGBITS,    SHF_EXCLUDE },
   2642   { STRING_COMMA_LEN (".got"),		   0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
   2643   { STRING_COMMA_LEN (".gnu.version"),	   0, SHT_GNU_versym,  0 },
   2644   { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
   2645   { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
   2646   { STRING_COMMA_LEN (".gnu.liblist"),	   0, SHT_GNU_LIBLIST, SHF_ALLOC },
   2647   { STRING_COMMA_LEN (".gnu.conflict"),	   0, SHT_RELA,	       SHF_ALLOC },
   2648   { STRING_COMMA_LEN (".gnu.hash"),	   0, SHT_GNU_HASH,    SHF_ALLOC },
   2649   { NULL,			 0,	   0, 0,	       0 }
   2650 };
   2651 
   2652 static const struct bfd_elf_special_section special_sections_h[] =
   2653 {
   2654   { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,	 SHF_ALLOC },
   2655   { NULL,		     0, 0, 0,		 0 }
   2656 };
   2657 
   2658 static const struct bfd_elf_special_section special_sections_i[] =
   2659 {
   2660   { STRING_COMMA_LEN (".init"),	       0, SHT_PROGBITS,	  SHF_ALLOC + SHF_EXECINSTR },
   2661   { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
   2662   { STRING_COMMA_LEN (".interp"),      0, SHT_PROGBITS,	  0 },
   2663   { NULL,		       0,      0, 0,		  0 }
   2664 };
   2665 
   2666 static const struct bfd_elf_special_section special_sections_l[] =
   2667 {
   2668   { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
   2669   { NULL,		     0, 0, 0,		 0 }
   2670 };
   2671 
   2672 static const struct bfd_elf_special_section special_sections_n[] =
   2673 {
   2674   { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
   2675   { STRING_COMMA_LEN (".note"),		 -1, SHT_NOTE,	   0 },
   2676   { NULL,		     0,		  0, 0,		   0 }
   2677 };
   2678 
   2679 static const struct bfd_elf_special_section special_sections_p[] =
   2680 {
   2681   { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
   2682   { STRING_COMMA_LEN (".plt"),		  0, SHT_PROGBITS,	SHF_ALLOC + SHF_EXECINSTR },
   2683   { NULL,		    0,		  0, 0,			0 }
   2684 };
   2685 
   2686 static const struct bfd_elf_special_section special_sections_r[] =
   2687 {
   2688   { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
   2689   { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
   2690   { STRING_COMMA_LEN (".rela"),	  -1, SHT_RELA,	    0 },
   2691   { STRING_COMMA_LEN (".rel"),	  -1, SHT_REL,	    0 },
   2692   { NULL,		    0,	   0, 0,	    0 }
   2693 };
   2694 
   2695 static const struct bfd_elf_special_section special_sections_s[] =
   2696 {
   2697   { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
   2698   { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
   2699   { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
   2700   /* See struct bfd_elf_special_section declaration for the semantics of
   2701      this special case where .prefix_length != strlen (.prefix).  */
   2702   { ".stabstr",			5,  3, SHT_STRTAB, 0 },
   2703   { NULL,			0,  0, 0,	   0 }
   2704 };
   2705 
   2706 static const struct bfd_elf_special_section special_sections_t[] =
   2707 {
   2708   { STRING_COMMA_LEN (".text"),	 -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
   2709   { STRING_COMMA_LEN (".tbss"),	 -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
   2710   { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
   2711   { NULL,		      0,  0, 0,		   0 }
   2712 };
   2713 
   2714 static const struct bfd_elf_special_section special_sections_z[] =
   2715 {
   2716   { STRING_COMMA_LEN (".zdebug_line"),	  0, SHT_PROGBITS, 0 },
   2717   { STRING_COMMA_LEN (".zdebug_info"),	  0, SHT_PROGBITS, 0 },
   2718   { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
   2719   { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
   2720   { NULL,		      0,  0, 0,		   0 }
   2721 };
   2722 
   2723 static const struct bfd_elf_special_section * const special_sections[] =
   2724 {
   2725   special_sections_b,		/* 'b' */
   2726   special_sections_c,		/* 'c' */
   2727   special_sections_d,		/* 'd' */
   2728   NULL,				/* 'e' */
   2729   special_sections_f,		/* 'f' */
   2730   special_sections_g,		/* 'g' */
   2731   special_sections_h,		/* 'h' */
   2732   special_sections_i,		/* 'i' */
   2733   NULL,				/* 'j' */
   2734   NULL,				/* 'k' */
   2735   special_sections_l,		/* 'l' */
   2736   NULL,				/* 'm' */
   2737   special_sections_n,		/* 'n' */
   2738   NULL,				/* 'o' */
   2739   special_sections_p,		/* 'p' */
   2740   NULL,				/* 'q' */
   2741   special_sections_r,		/* 'r' */
   2742   special_sections_s,		/* 's' */
   2743   special_sections_t,		/* 't' */
   2744   NULL,				/* 'u' */
   2745   NULL,				/* 'v' */
   2746   NULL,				/* 'w' */
   2747   NULL,				/* 'x' */
   2748   NULL,				/* 'y' */
   2749   special_sections_z		/* 'z' */
   2750 };
   2751 
   2752 const struct bfd_elf_special_section *
   2753 _bfd_elf_get_special_section (const char *name,
   2754 			      const struct bfd_elf_special_section *spec,
   2755 			      unsigned int rela)
   2756 {
   2757   int i;
   2758   int len;
   2759 
   2760   len = strlen (name);
   2761 
   2762   for (i = 0; spec[i].prefix != NULL; i++)
   2763     {
   2764       int suffix_len;
   2765       int prefix_len = spec[i].prefix_length;
   2766 
   2767       if (len < prefix_len)
   2768 	continue;
   2769       if (memcmp (name, spec[i].prefix, prefix_len) != 0)
   2770 	continue;
   2771 
   2772       suffix_len = spec[i].suffix_length;
   2773       if (suffix_len <= 0)
   2774 	{
   2775 	  if (name[prefix_len] != 0)
   2776 	    {
   2777 	      if (suffix_len == 0)
   2778 		continue;
   2779 	      if (name[prefix_len] != '.'
   2780 		  && (suffix_len == -2
   2781 		      || (rela && spec[i].type == SHT_REL)))
   2782 		continue;
   2783 	    }
   2784 	}
   2785       else
   2786 	{
   2787 	  if (len < prefix_len + suffix_len)
   2788 	    continue;
   2789 	  if (memcmp (name + len - suffix_len,
   2790 		      spec[i].prefix + prefix_len,
   2791 		      suffix_len) != 0)
   2792 	    continue;
   2793 	}
   2794       return &spec[i];
   2795     }
   2796 
   2797   return NULL;
   2798 }
   2799 
   2800 const struct bfd_elf_special_section *
   2801 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
   2802 {
   2803   int i;
   2804   const struct bfd_elf_special_section *spec;
   2805   const struct elf_backend_data *bed;
   2806 
   2807   /* See if this is one of the special sections.  */
   2808   if (sec->name == NULL)
   2809     return NULL;
   2810 
   2811   bed = get_elf_backend_data (abfd);
   2812   spec = bed->special_sections;
   2813   if (spec)
   2814     {
   2815       spec = _bfd_elf_get_special_section (sec->name,
   2816 					   bed->special_sections,
   2817 					   sec->use_rela_p);
   2818       if (spec != NULL)
   2819 	return spec;
   2820     }
   2821 
   2822   if (sec->name[0] != '.')
   2823     return NULL;
   2824 
   2825   i = sec->name[1] - 'b';
   2826   if (i < 0 || i > 'z' - 'b')
   2827     return NULL;
   2828 
   2829   spec = special_sections[i];
   2830 
   2831   if (spec == NULL)
   2832     return NULL;
   2833 
   2834   return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
   2835 }
   2836 
   2837 bfd_boolean
   2838 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
   2839 {
   2840   struct bfd_elf_section_data *sdata;
   2841   const struct elf_backend_data *bed;
   2842   const struct bfd_elf_special_section *ssect;
   2843 
   2844   sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
   2845   if (sdata == NULL)
   2846     {
   2847       sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
   2848 							  sizeof (*sdata));
   2849       if (sdata == NULL)
   2850 	return FALSE;
   2851       sec->used_by_bfd = sdata;
   2852     }
   2853 
   2854   /* Indicate whether or not this section should use RELA relocations.  */
   2855   bed = get_elf_backend_data (abfd);
   2856   sec->use_rela_p = bed->default_use_rela_p;
   2857 
   2858   /* When we read a file, we don't need to set ELF section type and
   2859      flags.  They will be overridden in _bfd_elf_make_section_from_shdr
   2860      anyway.  We will set ELF section type and flags for all linker
   2861      created sections.  If user specifies BFD section flags, we will
   2862      set ELF section type and flags based on BFD section flags in
   2863      elf_fake_sections.  Special handling for .init_array/.fini_array
   2864      output sections since they may contain .ctors/.dtors input
   2865      sections.  We don't want _bfd_elf_init_private_section_data to
   2866      copy ELF section type from .ctors/.dtors input sections.  */
   2867   if (abfd->direction != read_direction
   2868       || (sec->flags & SEC_LINKER_CREATED) != 0)
   2869     {
   2870       ssect = (*bed->get_sec_type_attr) (abfd, sec);
   2871       if (ssect != NULL
   2872 	  && (!sec->flags
   2873 	      || (sec->flags & SEC_LINKER_CREATED) != 0
   2874 	      || ssect->type == SHT_INIT_ARRAY
   2875 	      || ssect->type == SHT_FINI_ARRAY))
   2876 	{
   2877 	  elf_section_type (sec) = ssect->type;
   2878 	  elf_section_flags (sec) = ssect->attr;
   2879 	}
   2880     }
   2881 
   2882   return _bfd_generic_new_section_hook (abfd, sec);
   2883 }
   2884 
   2885 /* Create a new bfd section from an ELF program header.
   2886 
   2887    Since program segments have no names, we generate a synthetic name
   2888    of the form segment<NUM>, where NUM is generally the index in the
   2889    program header table.  For segments that are split (see below) we
   2890    generate the names segment<NUM>a and segment<NUM>b.
   2891 
   2892    Note that some program segments may have a file size that is different than
   2893    (less than) the memory size.  All this means is that at execution the
   2894    system must allocate the amount of memory specified by the memory size,
   2895    but only initialize it with the first "file size" bytes read from the
   2896    file.  This would occur for example, with program segments consisting
   2897    of combined data+bss.
   2898 
   2899    To handle the above situation, this routine generates TWO bfd sections
   2900    for the single program segment.  The first has the length specified by
   2901    the file size of the segment, and the second has the length specified
   2902    by the difference between the two sizes.  In effect, the segment is split
   2903    into its initialized and uninitialized parts.
   2904 
   2905  */
   2906 
   2907 bfd_boolean
   2908 _bfd_elf_make_section_from_phdr (bfd *abfd,
   2909 				 Elf_Internal_Phdr *hdr,
   2910 				 int hdr_index,
   2911 				 const char *type_name)
   2912 {
   2913   asection *newsect;
   2914   char *name;
   2915   char namebuf[64];
   2916   size_t len;
   2917   int split;
   2918 
   2919   split = ((hdr->p_memsz > 0)
   2920 	    && (hdr->p_filesz > 0)
   2921 	    && (hdr->p_memsz > hdr->p_filesz));
   2922 
   2923   if (hdr->p_filesz > 0)
   2924     {
   2925       sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
   2926       len = strlen (namebuf) + 1;
   2927       name = (char *) bfd_alloc (abfd, len);
   2928       if (!name)
   2929 	return FALSE;
   2930       memcpy (name, namebuf, len);
   2931       newsect = bfd_make_section (abfd, name);
   2932       if (newsect == NULL)
   2933 	return FALSE;
   2934       newsect->vma = hdr->p_vaddr;
   2935       newsect->lma = hdr->p_paddr;
   2936       newsect->size = hdr->p_filesz;
   2937       newsect->filepos = hdr->p_offset;
   2938       newsect->flags |= SEC_HAS_CONTENTS;
   2939       newsect->alignment_power = bfd_log2 (hdr->p_align);
   2940       if (hdr->p_type == PT_LOAD)
   2941 	{
   2942 	  newsect->flags |= SEC_ALLOC;
   2943 	  newsect->flags |= SEC_LOAD;
   2944 	  if (hdr->p_flags & PF_X)
   2945 	    {
   2946 	      /* FIXME: all we known is that it has execute PERMISSION,
   2947 		 may be data.  */
   2948 	      newsect->flags |= SEC_CODE;
   2949 	    }
   2950 	}
   2951       if (!(hdr->p_flags & PF_W))
   2952 	{
   2953 	  newsect->flags |= SEC_READONLY;
   2954 	}
   2955     }
   2956 
   2957   if (hdr->p_memsz > hdr->p_filesz)
   2958     {
   2959       bfd_vma align;
   2960 
   2961       sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
   2962       len = strlen (namebuf) + 1;
   2963       name = (char *) bfd_alloc (abfd, len);
   2964       if (!name)
   2965 	return FALSE;
   2966       memcpy (name, namebuf, len);
   2967       newsect = bfd_make_section (abfd, name);
   2968       if (newsect == NULL)
   2969 	return FALSE;
   2970       newsect->vma = hdr->p_vaddr + hdr->p_filesz;
   2971       newsect->lma = hdr->p_paddr + hdr->p_filesz;
   2972       newsect->size = hdr->p_memsz - hdr->p_filesz;
   2973       newsect->filepos = hdr->p_offset + hdr->p_filesz;
   2974       align = newsect->vma & -newsect->vma;
   2975       if (align == 0 || align > hdr->p_align)
   2976 	align = hdr->p_align;
   2977       newsect->alignment_power = bfd_log2 (align);
   2978       if (hdr->p_type == PT_LOAD)
   2979 	{
   2980 	  /* Hack for gdb.  Segments that have not been modified do
   2981 	     not have their contents written to a core file, on the
   2982 	     assumption that a debugger can find the contents in the
   2983 	     executable.  We flag this case by setting the fake
   2984 	     section size to zero.  Note that "real" bss sections will
   2985 	     always have their contents dumped to the core file.  */
   2986 	  if (bfd_get_format (abfd) == bfd_core)
   2987 	    newsect->size = 0;
   2988 	  newsect->flags |= SEC_ALLOC;
   2989 	  if (hdr->p_flags & PF_X)
   2990 	    newsect->flags |= SEC_CODE;
   2991 	}
   2992       if (!(hdr->p_flags & PF_W))
   2993 	newsect->flags |= SEC_READONLY;
   2994     }
   2995 
   2996   return TRUE;
   2997 }
   2998 
   2999 bfd_boolean
   3000 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
   3001 {
   3002   const struct elf_backend_data *bed;
   3003 
   3004   switch (hdr->p_type)
   3005     {
   3006     case PT_NULL:
   3007       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
   3008 
   3009     case PT_LOAD:
   3010       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
   3011 
   3012     case PT_DYNAMIC:
   3013       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
   3014 
   3015     case PT_INTERP:
   3016       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
   3017 
   3018     case PT_NOTE:
   3019       if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
   3020 	return FALSE;
   3021       if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
   3022 			    hdr->p_align))
   3023 	return FALSE;
   3024       return TRUE;
   3025 
   3026     case PT_SHLIB:
   3027       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
   3028 
   3029     case PT_PHDR:
   3030       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
   3031 
   3032     case PT_GNU_EH_FRAME:
   3033       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
   3034 					      "eh_frame_hdr");
   3035 
   3036     case PT_GNU_STACK:
   3037       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
   3038 
   3039     case PT_GNU_RELRO:
   3040       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
   3041 
   3042     default:
   3043       /* Check for any processor-specific program segment types.  */
   3044       bed = get_elf_backend_data (abfd);
   3045       return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
   3046     }
   3047 }
   3048 
   3049 /* Return the REL_HDR for SEC, assuming there is only a single one, either
   3050    REL or RELA.  */
   3051 
   3052 Elf_Internal_Shdr *
   3053 _bfd_elf_single_rel_hdr (asection *sec)
   3054 {
   3055   if (elf_section_data (sec)->rel.hdr)
   3056     {
   3057       BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
   3058       return elf_section_data (sec)->rel.hdr;
   3059     }
   3060   else
   3061     return elf_section_data (sec)->rela.hdr;
   3062 }
   3063 
   3064 static bfd_boolean
   3065 _bfd_elf_set_reloc_sh_name (bfd *abfd,
   3066 			    Elf_Internal_Shdr *rel_hdr,
   3067 			    const char *sec_name,
   3068 			    bfd_boolean use_rela_p)
   3069 {
   3070   char *name = (char *) bfd_alloc (abfd,
   3071 				   sizeof ".rela" + strlen (sec_name));
   3072   if (name == NULL)
   3073     return FALSE;
   3074 
   3075   sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
   3076   rel_hdr->sh_name =
   3077     (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
   3078 					FALSE);
   3079   if (rel_hdr->sh_name == (unsigned int) -1)
   3080     return FALSE;
   3081 
   3082   return TRUE;
   3083 }
   3084 
   3085 /* Allocate and initialize a section-header for a new reloc section,
   3086    containing relocations against ASECT.  It is stored in RELDATA.  If
   3087    USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
   3088    relocations.  */
   3089 
   3090 static bfd_boolean
   3091 _bfd_elf_init_reloc_shdr (bfd *abfd,
   3092 			  struct bfd_elf_section_reloc_data *reldata,
   3093 			  const char *sec_name,
   3094 			  bfd_boolean use_rela_p,
   3095 			  bfd_boolean delay_st_name_p)
   3096 {
   3097   Elf_Internal_Shdr *rel_hdr;
   3098   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3099 
   3100   BFD_ASSERT (reldata->hdr == NULL);
   3101   rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
   3102   reldata->hdr = rel_hdr;
   3103 
   3104   if (delay_st_name_p)
   3105     rel_hdr->sh_name = (unsigned int) -1;
   3106   else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
   3107 					use_rela_p))
   3108     return FALSE;
   3109   rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
   3110   rel_hdr->sh_entsize = (use_rela_p
   3111 			 ? bed->s->sizeof_rela
   3112 			 : bed->s->sizeof_rel);
   3113   rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   3114   rel_hdr->sh_flags = 0;
   3115   rel_hdr->sh_addr = 0;
   3116   rel_hdr->sh_size = 0;
   3117   rel_hdr->sh_offset = 0;
   3118 
   3119   return TRUE;
   3120 }
   3121 
   3122 /* Return the default section type based on the passed in section flags.  */
   3123 
   3124 int
   3125 bfd_elf_get_default_section_type (flagword flags)
   3126 {
   3127   if ((flags & SEC_ALLOC) != 0
   3128       && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   3129     return SHT_NOBITS;
   3130   return SHT_PROGBITS;
   3131 }
   3132 
   3133 struct fake_section_arg
   3134 {
   3135   struct bfd_link_info *link_info;
   3136   bfd_boolean failed;
   3137 };
   3138 
   3139 /* Set up an ELF internal section header for a section.  */
   3140 
   3141 static void
   3142 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
   3143 {
   3144   struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
   3145   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3146   struct bfd_elf_section_data *esd = elf_section_data (asect);
   3147   Elf_Internal_Shdr *this_hdr;
   3148   unsigned int sh_type;
   3149   const char *name = asect->name;
   3150   bfd_boolean delay_st_name_p = FALSE;
   3151 
   3152   if (arg->failed)
   3153     {
   3154       /* We already failed; just get out of the bfd_map_over_sections
   3155 	 loop.  */
   3156       return;
   3157     }
   3158 
   3159   this_hdr = &esd->this_hdr;
   3160 
   3161   if (arg->link_info)
   3162     {
   3163       /* ld: compress DWARF debug sections with names: .debug_*.  */
   3164       if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
   3165 	  && (asect->flags & SEC_DEBUGGING)
   3166 	  && name[1] == 'd'
   3167 	  && name[6] == '_')
   3168 	{
   3169 	  /* Set SEC_ELF_COMPRESS to indicate this section should be
   3170 	     compressed.  */
   3171 	  asect->flags |= SEC_ELF_COMPRESS;
   3172 
   3173 	  /* If this section will be compressed, delay adding section
   3174 	     name to section name section after it is compressed in
   3175 	     _bfd_elf_assign_file_positions_for_non_load.  */
   3176 	  delay_st_name_p = TRUE;
   3177 	}
   3178     }
   3179   else if ((asect->flags & SEC_ELF_RENAME))
   3180     {
   3181       /* objcopy: rename output DWARF debug section.  */
   3182       if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
   3183 	{
   3184 	  /* When we decompress or compress with SHF_COMPRESSED,
   3185 	     convert section name from .zdebug_* to .debug_* if
   3186 	     needed.  */
   3187 	  if (name[1] == 'z')
   3188 	    {
   3189 	      char *new_name = convert_zdebug_to_debug (abfd, name);
   3190 	      if (new_name == NULL)
   3191 		{
   3192 		  arg->failed = TRUE;
   3193 		  return;
   3194 		}
   3195 	      name = new_name;
   3196 	    }
   3197 	}
   3198       else if (asect->compress_status == COMPRESS_SECTION_DONE)
   3199 	{
   3200 	  /* PR binutils/18087: Compression does not always make a
   3201 	     section smaller.  So only rename the section when
   3202 	     compression has actually taken place.  If input section
   3203 	     name is .zdebug_*, we should never compress it again.  */
   3204 	  char *new_name = convert_debug_to_zdebug (abfd, name);
   3205 	  if (new_name == NULL)
   3206 	    {
   3207 	      arg->failed = TRUE;
   3208 	      return;
   3209 	    }
   3210 	  BFD_ASSERT (name[1] != 'z');
   3211 	  name = new_name;
   3212 	}
   3213     }
   3214 
   3215   if (delay_st_name_p)
   3216     this_hdr->sh_name = (unsigned int) -1;
   3217   else
   3218     {
   3219       this_hdr->sh_name
   3220 	= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   3221 					      name, FALSE);
   3222       if (this_hdr->sh_name == (unsigned int) -1)
   3223 	{
   3224 	  arg->failed = TRUE;
   3225 	  return;
   3226 	}
   3227     }
   3228 
   3229   /* Don't clear sh_flags. Assembler may set additional bits.  */
   3230 
   3231   if ((asect->flags & SEC_ALLOC) != 0
   3232       || asect->user_set_vma)
   3233     this_hdr->sh_addr = asect->vma;
   3234   else
   3235     this_hdr->sh_addr = 0;
   3236 
   3237   this_hdr->sh_offset = 0;
   3238   this_hdr->sh_size = asect->size;
   3239   this_hdr->sh_link = 0;
   3240   /* PR 17512: file: 0eb809fe, 8b0535ee.  */
   3241   if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
   3242     {
   3243       _bfd_error_handler
   3244 	/* xgettext:c-format */
   3245 	(_("%pB: error: alignment power %d of section `%pA' is too big"),
   3246 	 abfd, asect->alignment_power, asect);
   3247       arg->failed = TRUE;
   3248       return;
   3249     }
   3250   this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
   3251   /* The sh_entsize and sh_info fields may have been set already by
   3252      copy_private_section_data.  */
   3253 
   3254   this_hdr->bfd_section = asect;
   3255   this_hdr->contents = NULL;
   3256 
   3257   /* If the section type is unspecified, we set it based on
   3258      asect->flags.  */
   3259   if ((asect->flags & SEC_GROUP) != 0)
   3260     sh_type = SHT_GROUP;
   3261   else
   3262     sh_type = bfd_elf_get_default_section_type (asect->flags);
   3263 
   3264   if (this_hdr->sh_type == SHT_NULL)
   3265     this_hdr->sh_type = sh_type;
   3266   else if (this_hdr->sh_type == SHT_NOBITS
   3267 	   && sh_type == SHT_PROGBITS
   3268 	   && (asect->flags & SEC_ALLOC) != 0)
   3269     {
   3270       /* Warn if we are changing a NOBITS section to PROGBITS, but
   3271 	 allow the link to proceed.  This can happen when users link
   3272 	 non-bss input sections to bss output sections, or emit data
   3273 	 to a bss output section via a linker script.  */
   3274       _bfd_error_handler
   3275 	(_("warning: section `%pA' type changed to PROGBITS"), asect);
   3276       this_hdr->sh_type = sh_type;
   3277     }
   3278 
   3279   switch (this_hdr->sh_type)
   3280     {
   3281     default:
   3282       break;
   3283 
   3284     case SHT_STRTAB:
   3285     case SHT_NOTE:
   3286     case SHT_NOBITS:
   3287     case SHT_PROGBITS:
   3288       break;
   3289 
   3290     case SHT_INIT_ARRAY:
   3291     case SHT_FINI_ARRAY:
   3292     case SHT_PREINIT_ARRAY:
   3293       this_hdr->sh_entsize = bed->s->arch_size / 8;
   3294       break;
   3295 
   3296     case SHT_HASH:
   3297       this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
   3298       break;
   3299 
   3300     case SHT_DYNSYM:
   3301       this_hdr->sh_entsize = bed->s->sizeof_sym;
   3302       break;
   3303 
   3304     case SHT_DYNAMIC:
   3305       this_hdr->sh_entsize = bed->s->sizeof_dyn;
   3306       break;
   3307 
   3308     case SHT_RELA:
   3309       if (get_elf_backend_data (abfd)->may_use_rela_p)
   3310 	this_hdr->sh_entsize = bed->s->sizeof_rela;
   3311       break;
   3312 
   3313      case SHT_REL:
   3314       if (get_elf_backend_data (abfd)->may_use_rel_p)
   3315 	this_hdr->sh_entsize = bed->s->sizeof_rel;
   3316       break;
   3317 
   3318      case SHT_GNU_versym:
   3319       this_hdr->sh_entsize = sizeof (Elf_External_Versym);
   3320       break;
   3321 
   3322      case SHT_GNU_verdef:
   3323       this_hdr->sh_entsize = 0;
   3324       /* objcopy or strip will copy over sh_info, but may not set
   3325 	 cverdefs.  The linker will set cverdefs, but sh_info will be
   3326 	 zero.  */
   3327       if (this_hdr->sh_info == 0)
   3328 	this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
   3329       else
   3330 	BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
   3331 		    || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
   3332       break;
   3333 
   3334     case SHT_GNU_verneed:
   3335       this_hdr->sh_entsize = 0;
   3336       /* objcopy or strip will copy over sh_info, but may not set
   3337 	 cverrefs.  The linker will set cverrefs, but sh_info will be
   3338 	 zero.  */
   3339       if (this_hdr->sh_info == 0)
   3340 	this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
   3341       else
   3342 	BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
   3343 		    || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
   3344       break;
   3345 
   3346     case SHT_GROUP:
   3347       this_hdr->sh_entsize = GRP_ENTRY_SIZE;
   3348       break;
   3349 
   3350     case SHT_GNU_HASH:
   3351       this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
   3352       break;
   3353     }
   3354 
   3355   if ((asect->flags & SEC_ALLOC) != 0)
   3356     this_hdr->sh_flags |= SHF_ALLOC;
   3357   if ((asect->flags & SEC_READONLY) == 0)
   3358     this_hdr->sh_flags |= SHF_WRITE;
   3359   if ((asect->flags & SEC_CODE) != 0)
   3360     this_hdr->sh_flags |= SHF_EXECINSTR;
   3361   if ((asect->flags & SEC_MERGE) != 0)
   3362     {
   3363       this_hdr->sh_flags |= SHF_MERGE;
   3364       this_hdr->sh_entsize = asect->entsize;
   3365     }
   3366   if ((asect->flags & SEC_STRINGS) != 0)
   3367     this_hdr->sh_flags |= SHF_STRINGS;
   3368   if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
   3369     this_hdr->sh_flags |= SHF_GROUP;
   3370   if ((asect->flags & SEC_THREAD_LOCAL) != 0)
   3371     {
   3372       this_hdr->sh_flags |= SHF_TLS;
   3373       if (asect->size == 0
   3374 	  && (asect->flags & SEC_HAS_CONTENTS) == 0)
   3375 	{
   3376 	  struct bfd_link_order *o = asect->map_tail.link_order;
   3377 
   3378 	  this_hdr->sh_size = 0;
   3379 	  if (o != NULL)
   3380 	    {
   3381 	      this_hdr->sh_size = o->offset + o->size;
   3382 	      if (this_hdr->sh_size != 0)
   3383 		this_hdr->sh_type = SHT_NOBITS;
   3384 	    }
   3385 	}
   3386     }
   3387   if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
   3388     this_hdr->sh_flags |= SHF_EXCLUDE;
   3389 
   3390   /* If the section has relocs, set up a section header for the
   3391      SHT_REL[A] section.  If two relocation sections are required for
   3392      this section, it is up to the processor-specific back-end to
   3393      create the other.  */
   3394   if ((asect->flags & SEC_RELOC) != 0)
   3395     {
   3396       /* When doing a relocatable link, create both REL and RELA sections if
   3397 	 needed.  */
   3398       if (arg->link_info
   3399 	  /* Do the normal setup if we wouldn't create any sections here.  */
   3400 	  && esd->rel.count + esd->rela.count > 0
   3401 	  && (bfd_link_relocatable (arg->link_info)
   3402 	      || arg->link_info->emitrelocations))
   3403 	{
   3404 	  if (esd->rel.count && esd->rel.hdr == NULL
   3405 	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
   3406 					    FALSE, delay_st_name_p))
   3407 	    {
   3408 	      arg->failed = TRUE;
   3409 	      return;
   3410 	    }
   3411 	  if (esd->rela.count && esd->rela.hdr == NULL
   3412 	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
   3413 					    TRUE, delay_st_name_p))
   3414 	    {
   3415 	      arg->failed = TRUE;
   3416 	      return;
   3417 	    }
   3418 	}
   3419       else if (!_bfd_elf_init_reloc_shdr (abfd,
   3420 					  (asect->use_rela_p
   3421 					   ? &esd->rela : &esd->rel),
   3422 					  name,
   3423 					  asect->use_rela_p,
   3424 					  delay_st_name_p))
   3425 	{
   3426 	  arg->failed = TRUE;
   3427 	  return;
   3428 	}
   3429     }
   3430 
   3431   /* Check for processor-specific section types.  */
   3432   sh_type = this_hdr->sh_type;
   3433   if (bed->elf_backend_fake_sections
   3434       && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
   3435     {
   3436       arg->failed = TRUE;
   3437       return;
   3438     }
   3439 
   3440   if (sh_type == SHT_NOBITS && asect->size != 0)
   3441     {
   3442       /* Don't change the header type from NOBITS if we are being
   3443 	 called for objcopy --only-keep-debug.  */
   3444       this_hdr->sh_type = sh_type;
   3445     }
   3446 }
   3447 
   3448 /* Fill in the contents of a SHT_GROUP section.  Called from
   3449    _bfd_elf_compute_section_file_positions for gas, objcopy, and
   3450    when ELF targets use the generic linker, ld.  Called for ld -r
   3451    from bfd_elf_final_link.  */
   3452 
   3453 void
   3454 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
   3455 {
   3456   bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
   3457   asection *elt, *first;
   3458   unsigned char *loc;
   3459   bfd_boolean gas;
   3460 
   3461   /* Ignore linker created group section.  See elfNN_ia64_object_p in
   3462      elfxx-ia64.c.  */
   3463   if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
   3464       || *failedptr)
   3465     return;
   3466 
   3467   if (elf_section_data (sec)->this_hdr.sh_info == 0)
   3468     {
   3469       unsigned long symindx = 0;
   3470 
   3471       /* elf_group_id will have been set up by objcopy and the
   3472 	 generic linker.  */
   3473       if (elf_group_id (sec) != NULL)
   3474 	symindx = elf_group_id (sec)->udata.i;
   3475 
   3476       if (symindx == 0)
   3477 	{
   3478 	  /* If called from the assembler, swap_out_syms will have set up
   3479 	     elf_section_syms.  */
   3480 	  BFD_ASSERT (elf_section_syms (abfd) != NULL);
   3481 	  symindx = elf_section_syms (abfd)[sec->index]->udata.i;
   3482 	}
   3483       elf_section_data (sec)->this_hdr.sh_info = symindx;
   3484     }
   3485   else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
   3486     {
   3487       /* The ELF backend linker sets sh_info to -2 when the group
   3488 	 signature symbol is global, and thus the index can't be
   3489 	 set until all local symbols are output.  */
   3490       asection *igroup;
   3491       struct bfd_elf_section_data *sec_data;
   3492       unsigned long symndx;
   3493       unsigned long extsymoff;
   3494       struct elf_link_hash_entry *h;
   3495 
   3496       /* The point of this little dance to the first SHF_GROUP section
   3497 	 then back to the SHT_GROUP section is that this gets us to
   3498 	 the SHT_GROUP in the input object.  */
   3499       igroup = elf_sec_group (elf_next_in_group (sec));
   3500       sec_data = elf_section_data (igroup);
   3501       symndx = sec_data->this_hdr.sh_info;
   3502       extsymoff = 0;
   3503       if (!elf_bad_symtab (igroup->owner))
   3504 	{
   3505 	  Elf_Internal_Shdr *symtab_hdr;
   3506 
   3507 	  symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
   3508 	  extsymoff = symtab_hdr->sh_info;
   3509 	}
   3510       h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
   3511       while (h->root.type == bfd_link_hash_indirect
   3512 	     || h->root.type == bfd_link_hash_warning)
   3513 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3514 
   3515       elf_section_data (sec)->this_hdr.sh_info = h->indx;
   3516     }
   3517 
   3518   /* The contents won't be allocated for "ld -r" or objcopy.  */
   3519   gas = TRUE;
   3520   if (sec->contents == NULL)
   3521     {
   3522       gas = FALSE;
   3523       sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
   3524 
   3525       /* Arrange for the section to be written out.  */
   3526       elf_section_data (sec)->this_hdr.contents = sec->contents;
   3527       if (sec->contents == NULL)
   3528 	{
   3529 	  *failedptr = TRUE;
   3530 	  return;
   3531 	}
   3532     }
   3533 
   3534   loc = sec->contents + sec->size;
   3535 
   3536   /* Get the pointer to the first section in the group that gas
   3537      squirreled away here.  objcopy arranges for this to be set to the
   3538      start of the input section group.  */
   3539   first = elt = elf_next_in_group (sec);
   3540 
   3541   /* First element is a flag word.  Rest of section is elf section
   3542      indices for all the sections of the group.  Write them backwards
   3543      just to keep the group in the same order as given in .section
   3544      directives, not that it matters.  */
   3545   while (elt != NULL)
   3546     {
   3547       asection *s;
   3548 
   3549       s = elt;
   3550       if (!gas)
   3551 	s = s->output_section;
   3552       if (s != NULL
   3553 	  && !bfd_is_abs_section (s))
   3554 	{
   3555 	  struct bfd_elf_section_data *elf_sec = elf_section_data (s);
   3556 	  struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
   3557 
   3558 	  if (elf_sec->rel.hdr != NULL
   3559 	      && (gas
   3560 		  || (input_elf_sec->rel.hdr != NULL
   3561 		      && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
   3562 	    {
   3563 	      elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
   3564 	      loc -= 4;
   3565 	      H_PUT_32 (abfd, elf_sec->rel.idx, loc);
   3566 	    }
   3567 	  if (elf_sec->rela.hdr != NULL
   3568 	      && (gas
   3569 		  || (input_elf_sec->rela.hdr != NULL
   3570 		      && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
   3571 	    {
   3572 	      elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
   3573 	      loc -= 4;
   3574 	      H_PUT_32 (abfd, elf_sec->rela.idx, loc);
   3575 	    }
   3576 	  loc -= 4;
   3577 	  H_PUT_32 (abfd, elf_sec->this_idx, loc);
   3578 	}
   3579       elt = elf_next_in_group (elt);
   3580       if (elt == first)
   3581 	break;
   3582     }
   3583 
   3584   loc -= 4;
   3585   BFD_ASSERT (loc == sec->contents);
   3586 
   3587   H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
   3588 }
   3589 
   3590 /* Given NAME, the name of a relocation section stripped of its
   3591    .rel/.rela prefix, return the section in ABFD to which the
   3592    relocations apply.  */
   3593 
   3594 asection *
   3595 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
   3596 {
   3597   /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
   3598      section likely apply to .got.plt or .got section.  */
   3599   if (get_elf_backend_data (abfd)->want_got_plt
   3600       && strcmp (name, ".plt") == 0)
   3601     {
   3602       asection *sec;
   3603 
   3604       name = ".got.plt";
   3605       sec = bfd_get_section_by_name (abfd, name);
   3606       if (sec != NULL)
   3607 	return sec;
   3608       name = ".got";
   3609     }
   3610 
   3611   return bfd_get_section_by_name (abfd, name);
   3612 }
   3613 
   3614 /* Return the section to which RELOC_SEC applies.  */
   3615 
   3616 static asection *
   3617 elf_get_reloc_section (asection *reloc_sec)
   3618 {
   3619   const char *name;
   3620   unsigned int type;
   3621   bfd *abfd;
   3622   const struct elf_backend_data *bed;
   3623 
   3624   type = elf_section_data (reloc_sec)->this_hdr.sh_type;
   3625   if (type != SHT_REL && type != SHT_RELA)
   3626     return NULL;
   3627 
   3628   /* We look up the section the relocs apply to by name.  */
   3629   name = reloc_sec->name;
   3630   if (strncmp (name, ".rel", 4) != 0)
   3631     return NULL;
   3632   name += 4;
   3633   if (type == SHT_RELA && *name++ != 'a')
   3634     return NULL;
   3635 
   3636   abfd = reloc_sec->owner;
   3637   bed = get_elf_backend_data (abfd);
   3638   return bed->get_reloc_section (abfd, name);
   3639 }
   3640 
   3641 /* Assign all ELF section numbers.  The dummy first section is handled here
   3642    too.  The link/info pointers for the standard section types are filled
   3643    in here too, while we're at it.  */
   3644 
   3645 static bfd_boolean
   3646 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
   3647 {
   3648   struct elf_obj_tdata *t = elf_tdata (abfd);
   3649   asection *sec;
   3650   unsigned int section_number;
   3651   Elf_Internal_Shdr **i_shdrp;
   3652   struct bfd_elf_section_data *d;
   3653   bfd_boolean need_symtab;
   3654 
   3655   section_number = 1;
   3656 
   3657   _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
   3658 
   3659   /* SHT_GROUP sections are in relocatable files only.  */
   3660   if (link_info == NULL || !link_info->resolve_section_groups)
   3661     {
   3662       size_t reloc_count = 0;
   3663 
   3664       /* Put SHT_GROUP sections first.  */
   3665       for (sec = abfd->sections; sec != NULL; sec = sec->next)
   3666 	{
   3667 	  d = elf_section_data (sec);
   3668 
   3669 	  if (d->this_hdr.sh_type == SHT_GROUP)
   3670 	    {
   3671 	      if (sec->flags & SEC_LINKER_CREATED)
   3672 		{
   3673 		  /* Remove the linker created SHT_GROUP sections.  */
   3674 		  bfd_section_list_remove (abfd, sec);
   3675 		  abfd->section_count--;
   3676 		}
   3677 	      else
   3678 		d->this_idx = section_number++;
   3679 	    }
   3680 
   3681 	  /* Count relocations.  */
   3682 	  reloc_count += sec->reloc_count;
   3683 	}
   3684 
   3685       /* Clear HAS_RELOC if there are no relocations.  */
   3686       if (reloc_count == 0)
   3687 	abfd->flags &= ~HAS_RELOC;
   3688     }
   3689 
   3690   for (sec = abfd->sections; sec; sec = sec->next)
   3691     {
   3692       d = elf_section_data (sec);
   3693 
   3694       if (d->this_hdr.sh_type != SHT_GROUP)
   3695 	d->this_idx = section_number++;
   3696       if (d->this_hdr.sh_name != (unsigned int) -1)
   3697 	_bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
   3698       if (d->rel.hdr)
   3699 	{
   3700 	  d->rel.idx = section_number++;
   3701 	  if (d->rel.hdr->sh_name != (unsigned int) -1)
   3702 	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
   3703 	}
   3704       else
   3705 	d->rel.idx = 0;
   3706 
   3707       if (d->rela.hdr)
   3708 	{
   3709 	  d->rela.idx = section_number++;
   3710 	  if (d->rela.hdr->sh_name != (unsigned int) -1)
   3711 	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
   3712 	}
   3713       else
   3714 	d->rela.idx = 0;
   3715     }
   3716 
   3717   need_symtab = (bfd_get_symcount (abfd) > 0
   3718 		|| (link_info == NULL
   3719 		    && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
   3720 			== HAS_RELOC)));
   3721   if (need_symtab)
   3722     {
   3723       elf_onesymtab (abfd) = section_number++;
   3724       _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
   3725       if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
   3726 	{
   3727 	  elf_section_list * entry;
   3728 
   3729 	  BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
   3730 
   3731 	  entry = bfd_zalloc (abfd, sizeof * entry);
   3732 	  entry->ndx = section_number++;
   3733 	  elf_symtab_shndx_list (abfd) = entry;
   3734 	  entry->hdr.sh_name
   3735 	    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   3736 						  ".symtab_shndx", FALSE);
   3737 	  if (entry->hdr.sh_name == (unsigned int) -1)
   3738 	    return FALSE;
   3739 	}
   3740       elf_strtab_sec (abfd) = section_number++;
   3741       _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
   3742     }
   3743 
   3744   elf_shstrtab_sec (abfd) = section_number++;
   3745   _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
   3746   elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
   3747 
   3748   if (section_number >= SHN_LORESERVE)
   3749     {
   3750       /* xgettext:c-format */
   3751       _bfd_error_handler (_("%pB: too many sections: %u"),
   3752 			  abfd, section_number);
   3753       return FALSE;
   3754     }
   3755 
   3756   elf_numsections (abfd) = section_number;
   3757   elf_elfheader (abfd)->e_shnum = section_number;
   3758 
   3759   /* Set up the list of section header pointers, in agreement with the
   3760      indices.  */
   3761   i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
   3762 						sizeof (Elf_Internal_Shdr *));
   3763   if (i_shdrp == NULL)
   3764     return FALSE;
   3765 
   3766   i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
   3767 						 sizeof (Elf_Internal_Shdr));
   3768   if (i_shdrp[0] == NULL)
   3769     {
   3770       bfd_release (abfd, i_shdrp);
   3771       return FALSE;
   3772     }
   3773 
   3774   elf_elfsections (abfd) = i_shdrp;
   3775 
   3776   i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
   3777   if (need_symtab)
   3778     {
   3779       i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
   3780       if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
   3781 	{
   3782 	  elf_section_list * entry = elf_symtab_shndx_list (abfd);
   3783 	  BFD_ASSERT (entry != NULL);
   3784 	  i_shdrp[entry->ndx] = & entry->hdr;
   3785 	  entry->hdr.sh_link = elf_onesymtab (abfd);
   3786 	}
   3787       i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
   3788       t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
   3789     }
   3790 
   3791   for (sec = abfd->sections; sec; sec = sec->next)
   3792     {
   3793       asection *s;
   3794 
   3795       d = elf_section_data (sec);
   3796 
   3797       i_shdrp[d->this_idx] = &d->this_hdr;
   3798       if (d->rel.idx != 0)
   3799 	i_shdrp[d->rel.idx] = d->rel.hdr;
   3800       if (d->rela.idx != 0)
   3801 	i_shdrp[d->rela.idx] = d->rela.hdr;
   3802 
   3803       /* Fill in the sh_link and sh_info fields while we're at it.  */
   3804 
   3805       /* sh_link of a reloc section is the section index of the symbol
   3806 	 table.  sh_info is the section index of the section to which
   3807 	 the relocation entries apply.  */
   3808       if (d->rel.idx != 0)
   3809 	{
   3810 	  d->rel.hdr->sh_link = elf_onesymtab (abfd);
   3811 	  d->rel.hdr->sh_info = d->this_idx;
   3812 	  d->rel.hdr->sh_flags |= SHF_INFO_LINK;
   3813 	}
   3814       if (d->rela.idx != 0)
   3815 	{
   3816 	  d->rela.hdr->sh_link = elf_onesymtab (abfd);
   3817 	  d->rela.hdr->sh_info = d->this_idx;
   3818 	  d->rela.hdr->sh_flags |= SHF_INFO_LINK;
   3819 	}
   3820 
   3821       /* We need to set up sh_link for SHF_LINK_ORDER.  */
   3822       if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
   3823 	{
   3824 	  s = elf_linked_to_section (sec);
   3825 	  if (s)
   3826 	    {
   3827 	      /* elf_linked_to_section points to the input section.  */
   3828 	      if (link_info != NULL)
   3829 		{
   3830 		  /* Check discarded linkonce section.  */
   3831 		  if (discarded_section (s))
   3832 		    {
   3833 		      asection *kept;
   3834 		      _bfd_error_handler
   3835 			/* xgettext:c-format */
   3836 			(_("%pB: sh_link of section `%pA' points to"
   3837 			   " discarded section `%pA' of `%pB'"),
   3838 			 abfd, d->this_hdr.bfd_section,
   3839 			 s, s->owner);
   3840 		      /* Point to the kept section if it has the same
   3841 			 size as the discarded one.  */
   3842 		      kept = _bfd_elf_check_kept_section (s, link_info);
   3843 		      if (kept == NULL)
   3844 			{
   3845 			  bfd_set_error (bfd_error_bad_value);
   3846 			  return FALSE;
   3847 			}
   3848 		      s = kept;
   3849 		    }
   3850 
   3851 		  s = s->output_section;
   3852 		  BFD_ASSERT (s != NULL);
   3853 		}
   3854 	      else
   3855 		{
   3856 		  /* Handle objcopy. */
   3857 		  if (s->output_section == NULL)
   3858 		    {
   3859 		      _bfd_error_handler
   3860 			/* xgettext:c-format */
   3861 			(_("%pB: sh_link of section `%pA' points to"
   3862 			   " removed section `%pA' of `%pB'"),
   3863 			 abfd, d->this_hdr.bfd_section, s, s->owner);
   3864 		      bfd_set_error (bfd_error_bad_value);
   3865 		      return FALSE;
   3866 		    }
   3867 		  s = s->output_section;
   3868 		}
   3869 	      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3870 	    }
   3871 	  else
   3872 	    {
   3873 	      /* PR 290:
   3874 		 The Intel C compiler generates SHT_IA_64_UNWIND with
   3875 		 SHF_LINK_ORDER.  But it doesn't set the sh_link or
   3876 		 sh_info fields.  Hence we could get the situation
   3877 		 where s is NULL.  */
   3878 	      const struct elf_backend_data *bed
   3879 		= get_elf_backend_data (abfd);
   3880 	      if (bed->link_order_error_handler)
   3881 		bed->link_order_error_handler
   3882 		  /* xgettext:c-format */
   3883 		  (_("%pB: warning: sh_link not set for section `%pA'"),
   3884 		   abfd, sec);
   3885 	    }
   3886 	}
   3887 
   3888       switch (d->this_hdr.sh_type)
   3889 	{
   3890 	case SHT_REL:
   3891 	case SHT_RELA:
   3892 	  /* A reloc section which we are treating as a normal BFD
   3893 	     section.  sh_link is the section index of the symbol
   3894 	     table.  sh_info is the section index of the section to
   3895 	     which the relocation entries apply.  We assume that an
   3896 	     allocated reloc section uses the dynamic symbol table.
   3897 	     FIXME: How can we be sure?  */
   3898 	  s = bfd_get_section_by_name (abfd, ".dynsym");
   3899 	  if (s != NULL)
   3900 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3901 
   3902 	  s = elf_get_reloc_section (sec);
   3903 	  if (s != NULL)
   3904 	    {
   3905 	      d->this_hdr.sh_info = elf_section_data (s)->this_idx;
   3906 	      d->this_hdr.sh_flags |= SHF_INFO_LINK;
   3907 	    }
   3908 	  break;
   3909 
   3910 	case SHT_STRTAB:
   3911 	  /* We assume that a section named .stab*str is a stabs
   3912 	     string section.  We look for a section with the same name
   3913 	     but without the trailing ``str'', and set its sh_link
   3914 	     field to point to this section.  */
   3915 	  if (CONST_STRNEQ (sec->name, ".stab")
   3916 	      && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
   3917 	    {
   3918 	      size_t len;
   3919 	      char *alc;
   3920 
   3921 	      len = strlen (sec->name);
   3922 	      alc = (char *) bfd_malloc (len - 2);
   3923 	      if (alc == NULL)
   3924 		return FALSE;
   3925 	      memcpy (alc, sec->name, len - 3);
   3926 	      alc[len - 3] = '\0';
   3927 	      s = bfd_get_section_by_name (abfd, alc);
   3928 	      free (alc);
   3929 	      if (s != NULL)
   3930 		{
   3931 		  elf_section_data (s)->this_hdr.sh_link = d->this_idx;
   3932 
   3933 		  /* This is a .stab section.  */
   3934 		  if (elf_section_data (s)->this_hdr.sh_entsize == 0)
   3935 		    elf_section_data (s)->this_hdr.sh_entsize
   3936 		      = 4 + 2 * bfd_get_arch_size (abfd) / 8;
   3937 		}
   3938 	    }
   3939 	  break;
   3940 
   3941 	case SHT_DYNAMIC:
   3942 	case SHT_DYNSYM:
   3943 	case SHT_GNU_verneed:
   3944 	case SHT_GNU_verdef:
   3945 	  /* sh_link is the section header index of the string table
   3946 	     used for the dynamic entries, or the symbol table, or the
   3947 	     version strings.  */
   3948 	  s = bfd_get_section_by_name (abfd, ".dynstr");
   3949 	  if (s != NULL)
   3950 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3951 	  break;
   3952 
   3953 	case SHT_GNU_LIBLIST:
   3954 	  /* sh_link is the section header index of the prelink library
   3955 	     list used for the dynamic entries, or the symbol table, or
   3956 	     the version strings.  */
   3957 	  s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
   3958 					     ? ".dynstr" : ".gnu.libstr");
   3959 	  if (s != NULL)
   3960 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3961 	  break;
   3962 
   3963 	case SHT_HASH:
   3964 	case SHT_GNU_HASH:
   3965 	case SHT_GNU_versym:
   3966 	  /* sh_link is the section header index of the symbol table
   3967 	     this hash table or version table is for.  */
   3968 	  s = bfd_get_section_by_name (abfd, ".dynsym");
   3969 	  if (s != NULL)
   3970 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3971 	  break;
   3972 
   3973 	case SHT_GROUP:
   3974 	  d->this_hdr.sh_link = elf_onesymtab (abfd);
   3975 	}
   3976     }
   3977 
   3978   /* Delay setting sh_name to _bfd_elf_write_object_contents so that
   3979      _bfd_elf_assign_file_positions_for_non_load can convert DWARF
   3980      debug section name from .debug_* to .zdebug_* if needed.  */
   3981 
   3982   return TRUE;
   3983 }
   3984 
   3985 static bfd_boolean
   3986 sym_is_global (bfd *abfd, asymbol *sym)
   3987 {
   3988   /* If the backend has a special mapping, use it.  */
   3989   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3990   if (bed->elf_backend_sym_is_global)
   3991     return (*bed->elf_backend_sym_is_global) (abfd, sym);
   3992 
   3993   return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
   3994 	  || bfd_is_und_section (bfd_get_section (sym))
   3995 	  || bfd_is_com_section (bfd_get_section (sym)));
   3996 }
   3997 
   3998 /* Filter global symbols of ABFD to include in the import library.  All
   3999    SYMCOUNT symbols of ABFD can be examined from their pointers in
   4000    SYMS.  Pointers of symbols to keep should be stored contiguously at
   4001    the beginning of that array.
   4002 
   4003    Returns the number of symbols to keep.  */
   4004 
   4005 unsigned int
   4006 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
   4007 				asymbol **syms, long symcount)
   4008 {
   4009   long src_count, dst_count = 0;
   4010 
   4011   for (src_count = 0; src_count < symcount; src_count++)
   4012     {
   4013       asymbol *sym = syms[src_count];
   4014       char *name = (char *) bfd_asymbol_name (sym);
   4015       struct bfd_link_hash_entry *h;
   4016 
   4017       if (!sym_is_global (abfd, sym))
   4018 	continue;
   4019 
   4020       h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
   4021       if (h == NULL)
   4022 	continue;
   4023       if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
   4024 	continue;
   4025       if (h->linker_def || h->ldscript_def)
   4026 	continue;
   4027 
   4028       syms[dst_count++] = sym;
   4029     }
   4030 
   4031   syms[dst_count] = NULL;
   4032 
   4033   return dst_count;
   4034 }
   4035 
   4036 /* Don't output section symbols for sections that are not going to be
   4037    output, that are duplicates or there is no BFD section.  */
   4038 
   4039 static bfd_boolean
   4040 ignore_section_sym (bfd *abfd, asymbol *sym)
   4041 {
   4042   elf_symbol_type *type_ptr;
   4043 
   4044   if (sym == NULL)
   4045     return FALSE;
   4046 
   4047   if ((sym->flags & BSF_SECTION_SYM) == 0)
   4048     return FALSE;
   4049 
   4050   if (sym->section == NULL)
   4051     return TRUE;
   4052 
   4053   type_ptr = elf_symbol_from (abfd, sym);
   4054   return ((type_ptr != NULL
   4055 	   && type_ptr->internal_elf_sym.st_shndx != 0
   4056 	   && bfd_is_abs_section (sym->section))
   4057 	  || !(sym->section->owner == abfd
   4058 	       || (sym->section->output_section != NULL
   4059 		   && sym->section->output_section->owner == abfd
   4060 		   && sym->section->output_offset == 0)
   4061 	       || bfd_is_abs_section (sym->section)));
   4062 }
   4063 
   4064 /* Map symbol from it's internal number to the external number, moving
   4065    all local symbols to be at the head of the list.  */
   4066 
   4067 static bfd_boolean
   4068 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
   4069 {
   4070   unsigned int symcount = bfd_get_symcount (abfd);
   4071   asymbol **syms = bfd_get_outsymbols (abfd);
   4072   asymbol **sect_syms;
   4073   unsigned int num_locals = 0;
   4074   unsigned int num_globals = 0;
   4075   unsigned int num_locals2 = 0;
   4076   unsigned int num_globals2 = 0;
   4077   unsigned int max_index = 0;
   4078   unsigned int idx;
   4079   asection *asect;
   4080   asymbol **new_syms;
   4081 
   4082 #ifdef DEBUG
   4083   fprintf (stderr, "elf_map_symbols\n");
   4084   fflush (stderr);
   4085 #endif
   4086 
   4087   for (asect = abfd->sections; asect; asect = asect->next)
   4088     {
   4089       if (max_index < asect->index)
   4090 	max_index = asect->index;
   4091     }
   4092 
   4093   max_index++;
   4094   sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
   4095   if (sect_syms == NULL)
   4096     return FALSE;
   4097   elf_section_syms (abfd) = sect_syms;
   4098   elf_num_section_syms (abfd) = max_index;
   4099 
   4100   /* Init sect_syms entries for any section symbols we have already
   4101      decided to output.  */
   4102   for (idx = 0; idx < symcount; idx++)
   4103     {
   4104       asymbol *sym = syms[idx];
   4105 
   4106       if ((sym->flags & BSF_SECTION_SYM) != 0
   4107 	  && sym->value == 0
   4108 	  && !ignore_section_sym (abfd, sym)
   4109 	  && !bfd_is_abs_section (sym->section))
   4110 	{
   4111 	  asection *sec = sym->section;
   4112 
   4113 	  if (sec->owner != abfd)
   4114 	    sec = sec->output_section;
   4115 
   4116 	  sect_syms[sec->index] = syms[idx];
   4117 	}
   4118     }
   4119 
   4120   /* Classify all of the symbols.  */
   4121   for (idx = 0; idx < symcount; idx++)
   4122     {
   4123       if (sym_is_global (abfd, syms[idx]))
   4124 	num_globals++;
   4125       else if (!ignore_section_sym (abfd, syms[idx]))
   4126 	num_locals++;
   4127     }
   4128 
   4129   /* We will be adding a section symbol for each normal BFD section.  Most
   4130      sections will already have a section symbol in outsymbols, but
   4131      eg. SHT_GROUP sections will not, and we need the section symbol mapped
   4132      at least in that case.  */
   4133   for (asect = abfd->sections; asect; asect = asect->next)
   4134     {
   4135       if (sect_syms[asect->index] == NULL)
   4136 	{
   4137 	  if (!sym_is_global (abfd, asect->symbol))
   4138 	    num_locals++;
   4139 	  else
   4140 	    num_globals++;
   4141 	}
   4142     }
   4143 
   4144   /* Now sort the symbols so the local symbols are first.  */
   4145   new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
   4146 				      sizeof (asymbol *));
   4147 
   4148   if (new_syms == NULL)
   4149     return FALSE;
   4150 
   4151   for (idx = 0; idx < symcount; idx++)
   4152     {
   4153       asymbol *sym = syms[idx];
   4154       unsigned int i;
   4155 
   4156       if (sym_is_global (abfd, sym))
   4157 	i = num_locals + num_globals2++;
   4158       else if (!ignore_section_sym (abfd, sym))
   4159 	i = num_locals2++;
   4160       else
   4161 	continue;
   4162       new_syms[i] = sym;
   4163       sym->udata.i = i + 1;
   4164     }
   4165   for (asect = abfd->sections; asect; asect = asect->next)
   4166     {
   4167       if (sect_syms[asect->index] == NULL)
   4168 	{
   4169 	  asymbol *sym = asect->symbol;
   4170 	  unsigned int i;
   4171 
   4172 	  sect_syms[asect->index] = sym;
   4173 	  if (!sym_is_global (abfd, sym))
   4174 	    i = num_locals2++;
   4175 	  else
   4176 	    i = num_locals + num_globals2++;
   4177 	  new_syms[i] = sym;
   4178 	  sym->udata.i = i + 1;
   4179 	}
   4180     }
   4181 
   4182   bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
   4183 
   4184   *pnum_locals = num_locals;
   4185   return TRUE;
   4186 }
   4187 
   4188 /* Align to the maximum file alignment that could be required for any
   4189    ELF data structure.  */
   4190 
   4191 static inline file_ptr
   4192 align_file_position (file_ptr off, int align)
   4193 {
   4194   return (off + align - 1) & ~(align - 1);
   4195 }
   4196 
   4197 /* Assign a file position to a section, optionally aligning to the
   4198    required section alignment.  */
   4199 
   4200 file_ptr
   4201 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
   4202 					   file_ptr offset,
   4203 					   bfd_boolean align)
   4204 {
   4205   if (align && i_shdrp->sh_addralign > 1)
   4206     offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
   4207   i_shdrp->sh_offset = offset;
   4208   if (i_shdrp->bfd_section != NULL)
   4209     i_shdrp->bfd_section->filepos = offset;
   4210   if (i_shdrp->sh_type != SHT_NOBITS)
   4211     offset += i_shdrp->sh_size;
   4212   return offset;
   4213 }
   4214 
   4215 /* Compute the file positions we are going to put the sections at, and
   4216    otherwise prepare to begin writing out the ELF file.  If LINK_INFO
   4217    is not NULL, this is being called by the ELF backend linker.  */
   4218 
   4219 bfd_boolean
   4220 _bfd_elf_compute_section_file_positions (bfd *abfd,
   4221 					 struct bfd_link_info *link_info)
   4222 {
   4223   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4224   struct fake_section_arg fsargs;
   4225   bfd_boolean failed;
   4226   struct elf_strtab_hash *strtab = NULL;
   4227   Elf_Internal_Shdr *shstrtab_hdr;
   4228   bfd_boolean need_symtab;
   4229 
   4230   if (abfd->output_has_begun)
   4231     return TRUE;
   4232 
   4233   /* Do any elf backend specific processing first.  */
   4234   if (bed->elf_backend_begin_write_processing)
   4235     (*bed->elf_backend_begin_write_processing) (abfd, link_info);
   4236 
   4237   if (! prep_headers (abfd))
   4238     return FALSE;
   4239 
   4240   /* Post process the headers if necessary.  */
   4241   (*bed->elf_backend_post_process_headers) (abfd, link_info);
   4242 
   4243   fsargs.failed = FALSE;
   4244   fsargs.link_info = link_info;
   4245   bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
   4246   if (fsargs.failed)
   4247     return FALSE;
   4248 
   4249   if (!assign_section_numbers (abfd, link_info))
   4250     return FALSE;
   4251 
   4252   /* The backend linker builds symbol table information itself.  */
   4253   need_symtab = (link_info == NULL
   4254 		 && (bfd_get_symcount (abfd) > 0
   4255 		     || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
   4256 			 == HAS_RELOC)));
   4257   if (need_symtab)
   4258     {
   4259       /* Non-zero if doing a relocatable link.  */
   4260       int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
   4261 
   4262       if (! swap_out_syms (abfd, &strtab, relocatable_p))
   4263 	return FALSE;
   4264     }
   4265 
   4266   failed = FALSE;
   4267   if (link_info == NULL)
   4268     {
   4269       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
   4270       if (failed)
   4271 	return FALSE;
   4272     }
   4273 
   4274   shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
   4275   /* sh_name was set in prep_headers.  */
   4276   shstrtab_hdr->sh_type = SHT_STRTAB;
   4277   shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   4278   shstrtab_hdr->sh_addr = 0;
   4279   /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
   4280   shstrtab_hdr->sh_entsize = 0;
   4281   shstrtab_hdr->sh_link = 0;
   4282   shstrtab_hdr->sh_info = 0;
   4283   /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
   4284   shstrtab_hdr->sh_addralign = 1;
   4285 
   4286   if (!assign_file_positions_except_relocs (abfd, link_info))
   4287     return FALSE;
   4288 
   4289   if (need_symtab)
   4290     {
   4291       file_ptr off;
   4292       Elf_Internal_Shdr *hdr;
   4293 
   4294       off = elf_next_file_pos (abfd);
   4295 
   4296       hdr = & elf_symtab_hdr (abfd);
   4297       off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
   4298 
   4299       if (elf_symtab_shndx_list (abfd) != NULL)
   4300 	{
   4301 	  hdr = & elf_symtab_shndx_list (abfd)->hdr;
   4302 	  if (hdr->sh_size != 0)
   4303 	    off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
   4304 	  /* FIXME: What about other symtab_shndx sections in the list ?  */
   4305 	}
   4306 
   4307       hdr = &elf_tdata (abfd)->strtab_hdr;
   4308       off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
   4309 
   4310       elf_next_file_pos (abfd) = off;
   4311 
   4312       /* Now that we know where the .strtab section goes, write it
   4313 	 out.  */
   4314       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   4315 	  || ! _bfd_elf_strtab_emit (abfd, strtab))
   4316 	return FALSE;
   4317       _bfd_elf_strtab_free (strtab);
   4318     }
   4319 
   4320   abfd->output_has_begun = TRUE;
   4321 
   4322   return TRUE;
   4323 }
   4324 
   4325 /* Make an initial estimate of the size of the program header.  If we
   4326    get the number wrong here, we'll redo section placement.  */
   4327 
   4328 static bfd_size_type
   4329 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
   4330 {
   4331   size_t segs;
   4332   asection *s, *s2;
   4333   const struct elf_backend_data *bed;
   4334 
   4335   /* Assume we will need exactly two PT_LOAD segments: one for text
   4336      and one for data.  */
   4337   segs = 2;
   4338 
   4339   s = bfd_get_section_by_name (abfd, ".interp");
   4340   s2 = bfd_get_section_by_name (abfd, ".dynamic");
   4341   if (s != NULL && (s->flags & SEC_LOAD) != 0)
   4342     {
   4343       ++segs;
   4344     }
   4345 
   4346   if (s2 != NULL && (s2->flags & SEC_LOAD) != 0)
   4347     {
   4348       /* We need a PT_DYNAMIC segment.  */
   4349       ++segs;
   4350     }
   4351 
   4352   if ((s != NULL && (s->flags & SEC_LOAD) != 0) ||
   4353       (s2 != NULL && (s2->flags & SEC_LOAD) != 0))
   4354     {
   4355       /*
   4356        * If either a PT_INTERP or PT_DYNAMIC segment is created,
   4357        * also create a PT_PHDR segment.
   4358        */
   4359       ++segs;
   4360     }
   4361 
   4362   if (info != NULL && info->relro)
   4363     {
   4364       /* We need a PT_GNU_RELRO segment.  */
   4365       ++segs;
   4366     }
   4367 
   4368   if (elf_eh_frame_hdr (abfd))
   4369     {
   4370       /* We need a PT_GNU_EH_FRAME segment.  */
   4371       ++segs;
   4372     }
   4373 
   4374   if (elf_stack_flags (abfd))
   4375     {
   4376       /* We need a PT_GNU_STACK segment.  */
   4377       ++segs;
   4378     }
   4379 
   4380   for (s = abfd->sections; s != NULL; s = s->next)
   4381     {
   4382       if ((s->flags & SEC_LOAD) != 0
   4383 	  && CONST_STRNEQ (s->name, ".note"))
   4384 	{
   4385 	  /* We need a PT_NOTE segment.  */
   4386 	  ++segs;
   4387 	  /* Try to create just one PT_NOTE segment
   4388 	     for all adjacent loadable .note* sections.
   4389 	     gABI requires that within a PT_NOTE segment
   4390 	     (and also inside of each SHT_NOTE section)
   4391 	     each note is padded to a multiple of 4 size,
   4392 	     so we check whether the sections are correctly
   4393 	     aligned.  */
   4394 	  if (s->alignment_power == 2)
   4395 	    while (s->next != NULL
   4396 		   && s->next->alignment_power == 2
   4397 		   && (s->next->flags & SEC_LOAD) != 0
   4398 		   && CONST_STRNEQ (s->next->name, ".note"))
   4399 	      s = s->next;
   4400 	}
   4401     }
   4402 
   4403   for (s = abfd->sections; s != NULL; s = s->next)
   4404     {
   4405       if (s->flags & SEC_THREAD_LOCAL)
   4406 	{
   4407 	  /* We need a PT_TLS segment.  */
   4408 	  ++segs;
   4409 	  break;
   4410 	}
   4411     }
   4412 
   4413   bed = get_elf_backend_data (abfd);
   4414 
   4415  if ((abfd->flags & D_PAGED) != 0)
   4416    {
   4417      /* Add a PT_GNU_MBIND segment for each mbind section.  */
   4418      unsigned int page_align_power = bfd_log2 (bed->commonpagesize);
   4419      for (s = abfd->sections; s != NULL; s = s->next)
   4420        if (elf_section_flags (s) & SHF_GNU_MBIND)
   4421 	 {
   4422 	   if (elf_section_data (s)->this_hdr.sh_info
   4423 	       > PT_GNU_MBIND_NUM)
   4424 	     {
   4425 	       _bfd_error_handler
   4426 		 /* xgettext:c-format */
   4427 		 (_("%pB: GNU_MBIN section `%pA' has invalid sh_info field: %d"),
   4428 		     abfd, s, elf_section_data (s)->this_hdr.sh_info);
   4429 	       continue;
   4430 	     }
   4431 	   /* Align mbind section to page size.  */
   4432 	   if (s->alignment_power < page_align_power)
   4433 	     s->alignment_power = page_align_power;
   4434 	   segs ++;
   4435 	 }
   4436    }
   4437 
   4438  /* Let the backend count up any program headers it might need.  */
   4439  if (bed->elf_backend_additional_program_headers)
   4440     {
   4441       int a;
   4442 
   4443       a = (*bed->elf_backend_additional_program_headers) (abfd, info);
   4444       if (a == -1)
   4445 	abort ();
   4446       segs += a;
   4447     }
   4448 
   4449   return segs * bed->s->sizeof_phdr;
   4450 }
   4451 
   4452 /* Find the segment that contains the output_section of section.  */
   4453 
   4454 Elf_Internal_Phdr *
   4455 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
   4456 {
   4457   struct elf_segment_map *m;
   4458   Elf_Internal_Phdr *p;
   4459 
   4460   for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
   4461        m != NULL;
   4462        m = m->next, p++)
   4463     {
   4464       int i;
   4465 
   4466       for (i = m->count - 1; i >= 0; i--)
   4467 	if (m->sections[i] == section)
   4468 	  return p;
   4469     }
   4470 
   4471   return NULL;
   4472 }
   4473 
   4474 /* Create a mapping from a set of sections to a program segment.  */
   4475 
   4476 static struct elf_segment_map *
   4477 make_mapping (bfd *abfd,
   4478 	      asection **sections,
   4479 	      unsigned int from,
   4480 	      unsigned int to,
   4481 	      bfd_boolean phdr)
   4482 {
   4483   struct elf_segment_map *m;
   4484   unsigned int i;
   4485   asection **hdrpp;
   4486   bfd_size_type amt;
   4487 
   4488   amt = sizeof (struct elf_segment_map);
   4489   amt += (to - from - 1) * sizeof (asection *);
   4490   m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4491   if (m == NULL)
   4492     return NULL;
   4493   m->next = NULL;
   4494   m->p_type = PT_LOAD;
   4495   for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
   4496     m->sections[i - from] = *hdrpp;
   4497   m->count = to - from;
   4498 
   4499   if (from == 0 && phdr)
   4500     {
   4501       /* Include the headers in the first PT_LOAD segment.  */
   4502       m->includes_filehdr = 1;
   4503       m->includes_phdrs = 1;
   4504     }
   4505 
   4506   return m;
   4507 }
   4508 
   4509 /* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
   4510    on failure.  */
   4511 
   4512 struct elf_segment_map *
   4513 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
   4514 {
   4515   struct elf_segment_map *m;
   4516 
   4517   m = (struct elf_segment_map *) bfd_zalloc (abfd,
   4518 					     sizeof (struct elf_segment_map));
   4519   if (m == NULL)
   4520     return NULL;
   4521   m->next = NULL;
   4522   m->p_type = PT_DYNAMIC;
   4523   m->count = 1;
   4524   m->sections[0] = dynsec;
   4525 
   4526   return m;
   4527 }
   4528 
   4529 /* Possibly add or remove segments from the segment map.  */
   4530 
   4531 static bfd_boolean
   4532 elf_modify_segment_map (bfd *abfd,
   4533 			struct bfd_link_info *info,
   4534 			bfd_boolean remove_empty_load)
   4535 {
   4536   struct elf_segment_map **m;
   4537   const struct elf_backend_data *bed;
   4538 
   4539   /* The placement algorithm assumes that non allocated sections are
   4540      not in PT_LOAD segments.  We ensure this here by removing such
   4541      sections from the segment map.  We also remove excluded
   4542      sections.  Finally, any PT_LOAD segment without sections is
   4543      removed.  */
   4544   m = &elf_seg_map (abfd);
   4545   while (*m)
   4546     {
   4547       unsigned int i, new_count;
   4548 
   4549       for (new_count = 0, i = 0; i < (*m)->count; i++)
   4550 	{
   4551 	  if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
   4552 	      && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
   4553 		  || (*m)->p_type != PT_LOAD))
   4554 	    {
   4555 	      (*m)->sections[new_count] = (*m)->sections[i];
   4556 	      new_count++;
   4557 	    }
   4558 	}
   4559       (*m)->count = new_count;
   4560 
   4561       if (remove_empty_load
   4562 	  && (*m)->p_type == PT_LOAD
   4563 	  && (*m)->count == 0
   4564 	  && !(*m)->includes_phdrs)
   4565 	*m = (*m)->next;
   4566       else
   4567 	m = &(*m)->next;
   4568     }
   4569 
   4570   bed = get_elf_backend_data (abfd);
   4571   if (bed->elf_backend_modify_segment_map != NULL)
   4572     {
   4573       if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
   4574 	return FALSE;
   4575     }
   4576 
   4577   return TRUE;
   4578 }
   4579 
   4580 #define IS_TBSS(s) \
   4581   ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
   4582 
   4583 /* Set up a mapping from BFD sections to program segments.  */
   4584 
   4585 bfd_boolean
   4586 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
   4587 {
   4588   unsigned int count;
   4589   struct elf_segment_map *m;
   4590   asection **sections = NULL;
   4591   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4592   bfd_boolean no_user_phdrs;
   4593 
   4594   no_user_phdrs = elf_seg_map (abfd) == NULL;
   4595 
   4596   if (info != NULL)
   4597     info->user_phdrs = !no_user_phdrs;
   4598 
   4599   if (no_user_phdrs && bfd_count_sections (abfd) != 0)
   4600     {
   4601       asection *s;
   4602       unsigned int i;
   4603       struct elf_segment_map *mfirst;
   4604       struct elf_segment_map **pm;
   4605       asection *last_hdr;
   4606       bfd_vma last_size;
   4607       unsigned int phdr_index;
   4608       bfd_vma maxpagesize;
   4609       asection **hdrpp;
   4610       bfd_boolean phdr_in_segment = TRUE;
   4611       bfd_boolean writable;
   4612       bfd_boolean executable;
   4613       int tls_count = 0;
   4614       asection *first_tls = NULL;
   4615       asection *first_mbind = NULL;
   4616       asection *dynsec, *eh_frame_hdr;
   4617       bfd_size_type amt;
   4618       bfd_vma addr_mask, wrap_to = 0;
   4619       bfd_boolean linker_created_pt_phdr_segment = FALSE;
   4620 
   4621       /* Select the allocated sections, and sort them.  */
   4622 
   4623       sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
   4624 					    sizeof (asection *));
   4625       if (sections == NULL)
   4626 	goto error_return;
   4627 
   4628       /* Calculate top address, avoiding undefined behaviour of shift
   4629 	 left operator when shift count is equal to size of type
   4630 	 being shifted.  */
   4631       addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
   4632       addr_mask = (addr_mask << 1) + 1;
   4633 
   4634       i = 0;
   4635       for (s = abfd->sections; s != NULL; s = s->next)
   4636 	{
   4637 	  if ((s->flags & SEC_ALLOC) != 0)
   4638 	    {
   4639 	      sections[i] = s;
   4640 	      ++i;
   4641 	      /* A wrapping section potentially clashes with header.  */
   4642 	      if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
   4643 		wrap_to = (s->lma + s->size) & addr_mask;
   4644 	    }
   4645 	}
   4646       BFD_ASSERT (i <= bfd_count_sections (abfd));
   4647       count = i;
   4648 
   4649       qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
   4650 
   4651       /* Build the mapping.  */
   4652 
   4653       mfirst = NULL;
   4654       pm = &mfirst;
   4655 
   4656       /* If we have a .interp section, then create a PT_PHDR segment for
   4657 	 the program headers and a PT_INTERP segment for the .interp
   4658 	 section.  */
   4659       s = bfd_get_section_by_name (abfd, ".interp");
   4660       if (s != NULL && (s->flags & SEC_LOAD) == 0)
   4661 	s = NULL;
   4662       dynsec = bfd_get_section_by_name (abfd, ".dynamic");
   4663       if (dynsec != NULL && (dynsec->flags & SEC_LOAD) == 0)
   4664 	dynsec = NULL;
   4665 
   4666       if (s != NULL || dynsec != NULL)
   4667 	{
   4668 	  amt = sizeof (struct elf_segment_map);
   4669 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4670 	  if (m == NULL)
   4671 	    goto error_return;
   4672 	  m->next = NULL;
   4673 	  m->p_type = PT_PHDR;
   4674 	  m->p_flags = PF_R;
   4675 	  m->p_flags_valid = 1;
   4676 	  m->includes_phdrs = 1;
   4677 	  linker_created_pt_phdr_segment = TRUE;
   4678 	  *pm = m;
   4679 	  pm = &m->next;
   4680 	}
   4681 
   4682       if (s != NULL)
   4683 	{
   4684 	  amt = sizeof (struct elf_segment_map);
   4685 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4686 	  if (m == NULL)
   4687 	    goto error_return;
   4688 	  m->next = NULL;
   4689 	  m->p_type = PT_INTERP;
   4690 	  m->count = 1;
   4691 	  m->sections[0] = s;
   4692 
   4693 	  *pm = m;
   4694 	  pm = &m->next;
   4695 	}
   4696 
   4697       /* Look through the sections.  We put sections in the same program
   4698 	 segment when the start of the second section can be placed within
   4699 	 a few bytes of the end of the first section.  */
   4700       last_hdr = NULL;
   4701       last_size = 0;
   4702       phdr_index = 0;
   4703       maxpagesize = bed->maxpagesize;
   4704       /* PR 17512: file: c8455299.
   4705 	 Avoid divide-by-zero errors later on.
   4706 	 FIXME: Should we abort if the maxpagesize is zero ?  */
   4707       if (maxpagesize == 0)
   4708 	maxpagesize = 1;
   4709       writable = FALSE;
   4710       executable = FALSE;
   4711 
   4712       /* Deal with -Ttext or something similar such that the first section
   4713 	 is not adjacent to the program headers.  This is an
   4714 	 approximation, since at this point we don't know exactly how many
   4715 	 program headers we will need.  */
   4716       if (count > 0)
   4717 	{
   4718 	  bfd_size_type phdr_size = elf_program_header_size (abfd);
   4719 
   4720 	  if (phdr_size == (bfd_size_type) -1)
   4721 	    phdr_size = get_program_header_size (abfd, info);
   4722 	  phdr_size += bed->s->sizeof_ehdr;
   4723 	  if ((abfd->flags & D_PAGED) == 0
   4724 	      || (sections[0]->lma & addr_mask) < phdr_size
   4725 	      || ((sections[0]->lma & addr_mask) % maxpagesize
   4726 		  < phdr_size % maxpagesize)
   4727 	      || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to)
   4728 	    {
   4729 	      /* PR 20815: The ELF standard says that a PT_PHDR segment, if
   4730 		 present, must be included as part of the memory image of the
   4731 		 program.  Ie it must be part of a PT_LOAD segment as well.
   4732 		 If we have had to create our own PT_PHDR segment, but it is
   4733 		 not going to be covered by the first PT_LOAD segment, then
   4734 		 force the inclusion if we can...  */
   4735 	      if ((abfd->flags & D_PAGED) != 0
   4736 		  && linker_created_pt_phdr_segment)
   4737 		phdr_in_segment = TRUE;
   4738 	      else
   4739 		phdr_in_segment = FALSE;
   4740 	    }
   4741 	}
   4742 
   4743       for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
   4744 	{
   4745 	  asection *hdr;
   4746 	  bfd_boolean new_segment;
   4747 
   4748 	  hdr = *hdrpp;
   4749 
   4750 	  /* See if this section and the last one will fit in the same
   4751 	     segment.  */
   4752 
   4753 	  if (last_hdr == NULL)
   4754 	    {
   4755 	      /* If we don't have a segment yet, then we don't need a new
   4756 		 one (we build the last one after this loop).  */
   4757 	      new_segment = FALSE;
   4758 	    }
   4759 	  else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
   4760 	    {
   4761 	      /* If this section has a different relation between the
   4762 		 virtual address and the load address, then we need a new
   4763 		 segment.  */
   4764 	      new_segment = TRUE;
   4765 	    }
   4766 	  else if (hdr->lma < last_hdr->lma + last_size
   4767 		   || last_hdr->lma + last_size < last_hdr->lma)
   4768 	    {
   4769 	      /* If this section has a load address that makes it overlap
   4770 		 the previous section, then we need a new segment.  */
   4771 	      new_segment = TRUE;
   4772 	    }
   4773 	  else if ((abfd->flags & D_PAGED) != 0
   4774 		   && (((last_hdr->lma + last_size - 1) & -maxpagesize)
   4775 		       == (hdr->lma & -maxpagesize)))
   4776 	    {
   4777 	      /* If we are demand paged then we can't map two disk
   4778 		 pages onto the same memory page.  */
   4779 	      new_segment = FALSE;
   4780 	    }
   4781 	  /* In the next test we have to be careful when last_hdr->lma is close
   4782 	     to the end of the address space.  If the aligned address wraps
   4783 	     around to the start of the address space, then there are no more
   4784 	     pages left in memory and it is OK to assume that the current
   4785 	     section can be included in the current segment.  */
   4786 	  else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
   4787 		    + maxpagesize > last_hdr->lma)
   4788 		   && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
   4789 		       + maxpagesize <= hdr->lma))
   4790 	    {
   4791 	      /* If putting this section in this segment would force us to
   4792 		 skip a page in the segment, then we need a new segment.  */
   4793 	      new_segment = TRUE;
   4794 	    }
   4795 	  else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
   4796 		   && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
   4797 	    {
   4798 	      /* We don't want to put a loaded section after a
   4799 		 nonloaded (ie. bss style) section in the same segment
   4800 		 as that will force the non-loaded section to be loaded.
   4801 		 Consider .tbss sections as loaded for this purpose.  */
   4802 	      new_segment = TRUE;
   4803 	    }
   4804 	  else if ((abfd->flags & D_PAGED) == 0)
   4805 	    {
   4806 	      /* If the file is not demand paged, which means that we
   4807 		 don't require the sections to be correctly aligned in the
   4808 		 file, then there is no other reason for a new segment.  */
   4809 	      new_segment = FALSE;
   4810 	    }
   4811 	  else if (info != NULL
   4812 		   && info->separate_code
   4813 		   && executable != ((hdr->flags & SEC_CODE) != 0))
   4814 	    {
   4815 	      new_segment = TRUE;
   4816 	    }
   4817 	  else if (! writable
   4818 		   && (hdr->flags & SEC_READONLY) == 0)
   4819 	    {
   4820 	      /* We don't want to put a writable section in a read only
   4821 		 segment.  */
   4822 	      new_segment = TRUE;
   4823 	    }
   4824 	  else
   4825 	    {
   4826 	      /* Otherwise, we can use the same segment.  */
   4827 	      new_segment = FALSE;
   4828 	    }
   4829 
   4830 	  /* Allow interested parties a chance to override our decision.  */
   4831 	  if (last_hdr != NULL
   4832 	      && info != NULL
   4833 	      && info->callbacks->override_segment_assignment != NULL)
   4834 	    new_segment
   4835 	      = info->callbacks->override_segment_assignment (info, abfd, hdr,
   4836 							      last_hdr,
   4837 							      new_segment);
   4838 
   4839 	  if (! new_segment)
   4840 	    {
   4841 	      if ((hdr->flags & SEC_READONLY) == 0)
   4842 		writable = TRUE;
   4843 	      if ((hdr->flags & SEC_CODE) != 0)
   4844 		executable = TRUE;
   4845 	      last_hdr = hdr;
   4846 	      /* .tbss sections effectively have zero size.  */
   4847 	      last_size = !IS_TBSS (hdr) ? hdr->size : 0;
   4848 	      continue;
   4849 	    }
   4850 
   4851 	  /* We need a new program segment.  We must create a new program
   4852 	     header holding all the sections from phdr_index until hdr.  */
   4853 
   4854 	  m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
   4855 	  if (m == NULL)
   4856 	    goto error_return;
   4857 
   4858 	  *pm = m;
   4859 	  pm = &m->next;
   4860 
   4861 	  if ((hdr->flags & SEC_READONLY) == 0)
   4862 	    writable = TRUE;
   4863 	  else
   4864 	    writable = FALSE;
   4865 
   4866 	  if ((hdr->flags & SEC_CODE) == 0)
   4867 	    executable = FALSE;
   4868 	  else
   4869 	    executable = TRUE;
   4870 
   4871 	  last_hdr = hdr;
   4872 	  /* .tbss sections effectively have zero size.  */
   4873 	  last_size = !IS_TBSS (hdr) ? hdr->size : 0;
   4874 	  phdr_index = i;
   4875 	  phdr_in_segment = FALSE;
   4876 	}
   4877 
   4878       /* Create a final PT_LOAD program segment, but not if it's just
   4879 	 for .tbss.  */
   4880       if (last_hdr != NULL
   4881 	  && (i - phdr_index != 1
   4882 	      || !IS_TBSS (last_hdr)))
   4883 	{
   4884 	  m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
   4885 	  if (m == NULL)
   4886 	    goto error_return;
   4887 
   4888 	  *pm = m;
   4889 	  pm = &m->next;
   4890 	}
   4891 
   4892       /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
   4893       if (dynsec != NULL)
   4894 	{
   4895 	  m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
   4896 	  if (m == NULL)
   4897 	    goto error_return;
   4898 	  *pm = m;
   4899 	  pm = &m->next;
   4900 	}
   4901 
   4902       /* For each batch of consecutive loadable .note sections,
   4903 	 add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
   4904 	 because if we link together nonloadable .note sections and
   4905 	 loadable .note sections, we will generate two .note sections
   4906 	 in the output file.  FIXME: Using names for section types is
   4907 	 bogus anyhow.  */
   4908       for (s = abfd->sections; s != NULL; s = s->next)
   4909 	{
   4910 	  if ((s->flags & SEC_LOAD) != 0
   4911 	      && CONST_STRNEQ (s->name, ".note"))
   4912 	    {
   4913 	      asection *s2;
   4914 
   4915 	      count = 1;
   4916 	      amt = sizeof (struct elf_segment_map);
   4917 	      if (s->alignment_power == 2)
   4918 		for (s2 = s; s2->next != NULL; s2 = s2->next)
   4919 		  {
   4920 		    if (s2->next->alignment_power == 2
   4921 			&& (s2->next->flags & SEC_LOAD) != 0
   4922 			&& CONST_STRNEQ (s2->next->name, ".note")
   4923 			&& align_power (s2->lma + s2->size, 2)
   4924 			   == s2->next->lma)
   4925 		      count++;
   4926 		    else
   4927 		      break;
   4928 		  }
   4929 	      amt += (count - 1) * sizeof (asection *);
   4930 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4931 	      if (m == NULL)
   4932 		goto error_return;
   4933 	      m->next = NULL;
   4934 	      m->p_type = PT_NOTE;
   4935 	      m->count = count;
   4936 	      while (count > 1)
   4937 		{
   4938 		  m->sections[m->count - count--] = s;
   4939 		  BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   4940 		  s = s->next;
   4941 		}
   4942 	      m->sections[m->count - 1] = s;
   4943 	      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   4944 	      *pm = m;
   4945 	      pm = &m->next;
   4946 	    }
   4947 	  if (s->flags & SEC_THREAD_LOCAL)
   4948 	    {
   4949 	      if (! tls_count)
   4950 		first_tls = s;
   4951 	      tls_count++;
   4952 	    }
   4953 	  if (first_mbind == NULL
   4954 	      && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
   4955 	    first_mbind = s;
   4956 	}
   4957 
   4958       /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
   4959       if (tls_count > 0)
   4960 	{
   4961 	  amt = sizeof (struct elf_segment_map);
   4962 	  amt += (tls_count - 1) * sizeof (asection *);
   4963 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4964 	  if (m == NULL)
   4965 	    goto error_return;
   4966 	  m->next = NULL;
   4967 	  m->p_type = PT_TLS;
   4968 	  m->count = tls_count;
   4969 	  /* Mandated PF_R.  */
   4970 	  m->p_flags = PF_R;
   4971 	  m->p_flags_valid = 1;
   4972 	  s = first_tls;
   4973 	  for (i = 0; i < (unsigned int) tls_count; ++i)
   4974 	    {
   4975 	      if ((s->flags & SEC_THREAD_LOCAL) == 0)
   4976 		{
   4977 		  _bfd_error_handler
   4978 		    (_("%pB: TLS sections are not adjacent:"), abfd);
   4979 		  s = first_tls;
   4980 		  i = 0;
   4981 		  while (i < (unsigned int) tls_count)
   4982 		    {
   4983 		      if ((s->flags & SEC_THREAD_LOCAL) != 0)
   4984 			{
   4985 			  _bfd_error_handler (_("	    TLS: %pA"), s);
   4986 			  i++;
   4987 			}
   4988 		      else
   4989 			_bfd_error_handler (_("	non-TLS: %pA"), s);
   4990 		      s = s->next;
   4991 		    }
   4992 		  bfd_set_error (bfd_error_bad_value);
   4993 		  goto error_return;
   4994 		}
   4995 	      m->sections[i] = s;
   4996 	      s = s->next;
   4997 	    }
   4998 
   4999 	  *pm = m;
   5000 	  pm = &m->next;
   5001 	}
   5002 
   5003       if (first_mbind && (abfd->flags & D_PAGED) != 0)
   5004 	for (s = first_mbind; s != NULL; s = s->next)
   5005 	  if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
   5006 	      && (elf_section_data (s)->this_hdr.sh_info
   5007 		  <= PT_GNU_MBIND_NUM))
   5008 	    {
   5009 	      /* Mandated PF_R.  */
   5010 	      unsigned long p_flags = PF_R;
   5011 	      if ((s->flags & SEC_READONLY) == 0)
   5012 		p_flags |= PF_W;
   5013 	      if ((s->flags & SEC_CODE) != 0)
   5014 		p_flags |= PF_X;
   5015 
   5016 	      amt = sizeof (struct elf_segment_map) + sizeof (asection *);
   5017 	      m = bfd_zalloc (abfd, amt);
   5018 	      if (m == NULL)
   5019 		goto error_return;
   5020 	      m->next = NULL;
   5021 	      m->p_type = (PT_GNU_MBIND_LO
   5022 			   + elf_section_data (s)->this_hdr.sh_info);
   5023 	      m->count = 1;
   5024 	      m->p_flags_valid = 1;
   5025 	      m->sections[0] = s;
   5026 	      m->p_flags = p_flags;
   5027 
   5028 	      *pm = m;
   5029 	      pm = &m->next;
   5030 	    }
   5031 
   5032       /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
   5033 	 segment.  */
   5034       eh_frame_hdr = elf_eh_frame_hdr (abfd);
   5035       if (eh_frame_hdr != NULL
   5036 	  && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
   5037 	{
   5038 	  amt = sizeof (struct elf_segment_map);
   5039 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5040 	  if (m == NULL)
   5041 	    goto error_return;
   5042 	  m->next = NULL;
   5043 	  m->p_type = PT_GNU_EH_FRAME;
   5044 	  m->count = 1;
   5045 	  m->sections[0] = eh_frame_hdr->output_section;
   5046 
   5047 	  *pm = m;
   5048 	  pm = &m->next;
   5049 	}
   5050 
   5051       if (elf_stack_flags (abfd))
   5052 	{
   5053 	  amt = sizeof (struct elf_segment_map);
   5054 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5055 	  if (m == NULL)
   5056 	    goto error_return;
   5057 	  m->next = NULL;
   5058 	  m->p_type = PT_GNU_STACK;
   5059 	  m->p_flags = elf_stack_flags (abfd);
   5060 	  m->p_align = bed->stack_align;
   5061 	  m->p_flags_valid = 1;
   5062 	  m->p_align_valid = m->p_align != 0;
   5063 	  if (info->stacksize > 0)
   5064 	    {
   5065 	      m->p_size = info->stacksize;
   5066 	      m->p_size_valid = 1;
   5067 	    }
   5068 
   5069 	  *pm = m;
   5070 	  pm = &m->next;
   5071 	}
   5072 
   5073       if (info != NULL && info->relro)
   5074 	{
   5075 	  for (m = mfirst; m != NULL; m = m->next)
   5076 	    {
   5077 	      if (m->p_type == PT_LOAD
   5078 		  && m->count != 0
   5079 		  && m->sections[0]->vma >= info->relro_start
   5080 		  && m->sections[0]->vma < info->relro_end)
   5081 		{
   5082 		  i = m->count;
   5083 		  while (--i != (unsigned) -1)
   5084 		    if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
   5085 			== (SEC_LOAD | SEC_HAS_CONTENTS))
   5086 		      break;
   5087 
   5088 		  if (i != (unsigned) -1)
   5089 		    break;
   5090 		}
   5091 	    }
   5092 
   5093 	  /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
   5094 	  if (m != NULL)
   5095 	    {
   5096 	      amt = sizeof (struct elf_segment_map);
   5097 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5098 	      if (m == NULL)
   5099 		goto error_return;
   5100 	      m->next = NULL;
   5101 	      m->p_type = PT_GNU_RELRO;
   5102 	      *pm = m;
   5103 	      pm = &m->next;
   5104 	    }
   5105 	}
   5106 
   5107       free (sections);
   5108       elf_seg_map (abfd) = mfirst;
   5109     }
   5110 
   5111   if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
   5112     return FALSE;
   5113 
   5114   for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
   5115     ++count;
   5116   elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
   5117 
   5118   return TRUE;
   5119 
   5120  error_return:
   5121   if (sections != NULL)
   5122     free (sections);
   5123   return FALSE;
   5124 }
   5125 
   5126 /* Sort sections by address.  */
   5127 
   5128 static int
   5129 elf_sort_sections (const void *arg1, const void *arg2)
   5130 {
   5131   const asection *sec1 = *(const asection **) arg1;
   5132   const asection *sec2 = *(const asection **) arg2;
   5133   bfd_size_type size1, size2;
   5134 
   5135   /* Sort by LMA first, since this is the address used to
   5136      place the section into a segment.  */
   5137   if (sec1->lma < sec2->lma)
   5138     return -1;
   5139   else if (sec1->lma > sec2->lma)
   5140     return 1;
   5141 
   5142   /* Then sort by VMA.  Normally the LMA and the VMA will be
   5143      the same, and this will do nothing.  */
   5144   if (sec1->vma < sec2->vma)
   5145     return -1;
   5146   else if (sec1->vma > sec2->vma)
   5147     return 1;
   5148 
   5149   /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
   5150 
   5151 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
   5152 
   5153   if (TOEND (sec1))
   5154     {
   5155       if (TOEND (sec2))
   5156 	{
   5157 	  /* If the indicies are the same, do not return 0
   5158 	     here, but continue to try the next comparison.  */
   5159 	  if (sec1->target_index - sec2->target_index != 0)
   5160 	    return sec1->target_index - sec2->target_index;
   5161 	}
   5162       else
   5163 	return 1;
   5164     }
   5165   else if (TOEND (sec2))
   5166     return -1;
   5167 
   5168 #undef TOEND
   5169 
   5170   /* Sort by size, to put zero sized sections
   5171      before others at the same address.  */
   5172 
   5173   size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
   5174   size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
   5175 
   5176   if (size1 < size2)
   5177     return -1;
   5178   if (size1 > size2)
   5179     return 1;
   5180 
   5181   return sec1->target_index - sec2->target_index;
   5182 }
   5183 
   5184 /* Ian Lance Taylor writes:
   5185 
   5186    We shouldn't be using % with a negative signed number.  That's just
   5187    not good.  We have to make sure either that the number is not
   5188    negative, or that the number has an unsigned type.  When the types
   5189    are all the same size they wind up as unsigned.  When file_ptr is a
   5190    larger signed type, the arithmetic winds up as signed long long,
   5191    which is wrong.
   5192 
   5193    What we're trying to say here is something like ``increase OFF by
   5194    the least amount that will cause it to be equal to the VMA modulo
   5195    the page size.''  */
   5196 /* In other words, something like:
   5197 
   5198    vma_offset = m->sections[0]->vma % bed->maxpagesize;
   5199    off_offset = off % bed->maxpagesize;
   5200    if (vma_offset < off_offset)
   5201      adjustment = vma_offset + bed->maxpagesize - off_offset;
   5202    else
   5203      adjustment = vma_offset - off_offset;
   5204 
   5205    which can be collapsed into the expression below.  */
   5206 
   5207 static file_ptr
   5208 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
   5209 {
   5210   /* PR binutils/16199: Handle an alignment of zero.  */
   5211   if (maxpagesize == 0)
   5212     maxpagesize = 1;
   5213   return ((vma - off) % maxpagesize);
   5214 }
   5215 
   5216 static void
   5217 print_segment_map (const struct elf_segment_map *m)
   5218 {
   5219   unsigned int j;
   5220   const char *pt = get_segment_type (m->p_type);
   5221   char buf[32];
   5222 
   5223   if (pt == NULL)
   5224     {
   5225       if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
   5226 	sprintf (buf, "LOPROC+%7.7x",
   5227 		 (unsigned int) (m->p_type - PT_LOPROC));
   5228       else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
   5229 	sprintf (buf, "LOOS+%7.7x",
   5230 		 (unsigned int) (m->p_type - PT_LOOS));
   5231       else
   5232 	snprintf (buf, sizeof (buf), "%8.8x",
   5233 		  (unsigned int) m->p_type);
   5234       pt = buf;
   5235     }
   5236   fflush (stdout);
   5237   fprintf (stderr, "%s:", pt);
   5238   for (j = 0; j < m->count; j++)
   5239     fprintf (stderr, " %s", m->sections [j]->name);
   5240   putc ('\n',stderr);
   5241   fflush (stderr);
   5242 }
   5243 
   5244 static bfd_boolean
   5245 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
   5246 {
   5247   void *buf;
   5248   bfd_boolean ret;
   5249 
   5250   if (bfd_seek (abfd, pos, SEEK_SET) != 0)
   5251     return FALSE;
   5252   buf = bfd_zmalloc (len);
   5253   if (buf == NULL)
   5254     return FALSE;
   5255   ret = bfd_bwrite (buf, len, abfd) == len;
   5256   free (buf);
   5257   return ret;
   5258 }
   5259 
   5260 /* Assign file positions to the sections based on the mapping from
   5261    sections to segments.  This function also sets up some fields in
   5262    the file header.  */
   5263 
   5264 static bfd_boolean
   5265 assign_file_positions_for_load_sections (bfd *abfd,
   5266 					 struct bfd_link_info *link_info)
   5267 {
   5268   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   5269   struct elf_segment_map *m;
   5270   Elf_Internal_Phdr *phdrs;
   5271   Elf_Internal_Phdr *p;
   5272   file_ptr off;
   5273   bfd_size_type maxpagesize;
   5274   unsigned int pt_load_count = 0;
   5275   unsigned int alloc;
   5276   unsigned int i, j;
   5277   bfd_vma header_pad = 0;
   5278 
   5279   if (link_info == NULL
   5280       && !_bfd_elf_map_sections_to_segments (abfd, link_info))
   5281     return FALSE;
   5282 
   5283   alloc = 0;
   5284   for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   5285     {
   5286       ++alloc;
   5287       if (m->header_size)
   5288 	header_pad = m->header_size;
   5289     }
   5290 
   5291   if (alloc)
   5292     {
   5293       elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
   5294       elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
   5295     }
   5296   else
   5297     {
   5298       /* PR binutils/12467.  */
   5299       elf_elfheader (abfd)->e_phoff = 0;
   5300       elf_elfheader (abfd)->e_phentsize = 0;
   5301     }
   5302 
   5303   elf_elfheader (abfd)->e_phnum = alloc;
   5304 
   5305   if (elf_program_header_size (abfd) == (bfd_size_type) -1)
   5306     elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
   5307   else
   5308     BFD_ASSERT (elf_program_header_size (abfd)
   5309 		>= alloc * bed->s->sizeof_phdr);
   5310 
   5311   if (alloc == 0)
   5312     {
   5313       elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
   5314       return TRUE;
   5315     }
   5316 
   5317   /* We're writing the size in elf_program_header_size (abfd),
   5318      see assign_file_positions_except_relocs, so make sure we have
   5319      that amount allocated, with trailing space cleared.
   5320      The variable alloc contains the computed need, while
   5321      elf_program_header_size (abfd) contains the size used for the
   5322      layout.
   5323      See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
   5324      where the layout is forced to according to a larger size in the
   5325      last iterations for the testcase ld-elf/header.  */
   5326   BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
   5327 	      == 0);
   5328   phdrs = (Elf_Internal_Phdr *)
   5329      bfd_zalloc2 (abfd,
   5330 		  (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
   5331 		  sizeof (Elf_Internal_Phdr));
   5332   elf_tdata (abfd)->phdr = phdrs;
   5333   if (phdrs == NULL)
   5334     return FALSE;
   5335 
   5336   maxpagesize = 1;
   5337   if ((abfd->flags & D_PAGED) != 0)
   5338     maxpagesize = bed->maxpagesize;
   5339 
   5340   off = bed->s->sizeof_ehdr;
   5341   off += alloc * bed->s->sizeof_phdr;
   5342   if (header_pad < (bfd_vma) off)
   5343     header_pad = 0;
   5344   else
   5345     header_pad -= off;
   5346   off += header_pad;
   5347 
   5348   for (m = elf_seg_map (abfd), p = phdrs, j = 0;
   5349        m != NULL;
   5350        m = m->next, p++, j++)
   5351     {
   5352       asection **secpp;
   5353       bfd_vma off_adjust;
   5354       bfd_boolean no_contents;
   5355 
   5356       /* If elf_segment_map is not from map_sections_to_segments, the
   5357 	 sections may not be correctly ordered.  NOTE: sorting should
   5358 	 not be done to the PT_NOTE section of a corefile, which may
   5359 	 contain several pseudo-sections artificially created by bfd.
   5360 	 Sorting these pseudo-sections breaks things badly.  */
   5361       if (m->count > 1
   5362 	  && !(elf_elfheader (abfd)->e_type == ET_CORE
   5363 	       && m->p_type == PT_NOTE))
   5364 	qsort (m->sections, (size_t) m->count, sizeof (asection *),
   5365 	       elf_sort_sections);
   5366 
   5367       /* An ELF segment (described by Elf_Internal_Phdr) may contain a
   5368 	 number of sections with contents contributing to both p_filesz
   5369 	 and p_memsz, followed by a number of sections with no contents
   5370 	 that just contribute to p_memsz.  In this loop, OFF tracks next
   5371 	 available file offset for PT_LOAD and PT_NOTE segments.  */
   5372       p->p_type = m->p_type;
   5373       p->p_flags = m->p_flags;
   5374 
   5375       if (m->count == 0)
   5376 	p->p_vaddr = 0;
   5377       else
   5378 	p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
   5379 
   5380       if (m->p_paddr_valid)
   5381 	p->p_paddr = m->p_paddr;
   5382       else if (m->count == 0)
   5383 	p->p_paddr = 0;
   5384       else
   5385 	p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
   5386 
   5387       if (p->p_type == PT_LOAD
   5388 	  && (abfd->flags & D_PAGED) != 0)
   5389 	{
   5390 	  /* p_align in demand paged PT_LOAD segments effectively stores
   5391 	     the maximum page size.  When copying an executable with
   5392 	     objcopy, we set m->p_align from the input file.  Use this
   5393 	     value for maxpagesize rather than bed->maxpagesize, which
   5394 	     may be different.  Note that we use maxpagesize for PT_TLS
   5395 	     segment alignment later in this function, so we are relying
   5396 	     on at least one PT_LOAD segment appearing before a PT_TLS
   5397 	     segment.  */
   5398 	  if (m->p_align_valid)
   5399 	    maxpagesize = m->p_align;
   5400 
   5401 	  p->p_align = maxpagesize;
   5402 	  pt_load_count += 1;
   5403 	}
   5404       else if (m->p_align_valid)
   5405 	p->p_align = m->p_align;
   5406       else if (m->count == 0)
   5407 	p->p_align = 1 << bed->s->log_file_align;
   5408       else
   5409 	p->p_align = 0;
   5410 
   5411       no_contents = FALSE;
   5412       off_adjust = 0;
   5413       if (p->p_type == PT_LOAD
   5414 	  && m->count > 0)
   5415 	{
   5416 	  bfd_size_type align;
   5417 	  unsigned int align_power = 0;
   5418 
   5419 	  if (m->p_align_valid)
   5420 	    align = p->p_align;
   5421 	  else
   5422 	    {
   5423 	      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   5424 		{
   5425 		  unsigned int secalign;
   5426 
   5427 		  secalign = bfd_get_section_alignment (abfd, *secpp);
   5428 		  if (secalign > align_power)
   5429 		    align_power = secalign;
   5430 		}
   5431 	      align = (bfd_size_type) 1 << align_power;
   5432 	      if (align < maxpagesize)
   5433 		align = maxpagesize;
   5434 	    }
   5435 
   5436 	  for (i = 0; i < m->count; i++)
   5437 	    if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   5438 	      /* If we aren't making room for this section, then
   5439 		 it must be SHT_NOBITS regardless of what we've
   5440 		 set via struct bfd_elf_special_section.  */
   5441 	      elf_section_type (m->sections[i]) = SHT_NOBITS;
   5442 
   5443 	  /* Find out whether this segment contains any loadable
   5444 	     sections.  */
   5445 	  no_contents = TRUE;
   5446 	  for (i = 0; i < m->count; i++)
   5447 	    if (elf_section_type (m->sections[i]) != SHT_NOBITS)
   5448 	      {
   5449 		no_contents = FALSE;
   5450 		break;
   5451 	      }
   5452 
   5453 	  off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
   5454 
   5455 	  /* Broken hardware and/or kernel require that files do not
   5456 	     map the same page with different permissions on some hppa
   5457 	     processors.  */
   5458 	  if (pt_load_count > 1
   5459 	      && bed->no_page_alias
   5460 	      && (off & (maxpagesize - 1)) != 0
   5461 	      && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize))
   5462 	    off_adjust += maxpagesize;
   5463 	  off += off_adjust;
   5464 	  if (no_contents)
   5465 	    {
   5466 	      /* We shouldn't need to align the segment on disk since
   5467 		 the segment doesn't need file space, but the gABI
   5468 		 arguably requires the alignment and glibc ld.so
   5469 		 checks it.  So to comply with the alignment
   5470 		 requirement but not waste file space, we adjust
   5471 		 p_offset for just this segment.  (OFF_ADJUST is
   5472 		 subtracted from OFF later.)  This may put p_offset
   5473 		 past the end of file, but that shouldn't matter.  */
   5474 	    }
   5475 	  else
   5476 	    off_adjust = 0;
   5477 	}
   5478       /* Make sure the .dynamic section is the first section in the
   5479 	 PT_DYNAMIC segment.  */
   5480       else if (p->p_type == PT_DYNAMIC
   5481 	       && m->count > 1
   5482 	       && strcmp (m->sections[0]->name, ".dynamic") != 0)
   5483 	{
   5484 	  _bfd_error_handler
   5485 	    (_("%pB: The first section in the PT_DYNAMIC segment"
   5486 	       " is not the .dynamic section"),
   5487 	     abfd);
   5488 	  bfd_set_error (bfd_error_bad_value);
   5489 	  return FALSE;
   5490 	}
   5491       /* Set the note section type to SHT_NOTE.  */
   5492       else if (p->p_type == PT_NOTE)
   5493 	for (i = 0; i < m->count; i++)
   5494 	  elf_section_type (m->sections[i]) = SHT_NOTE;
   5495 
   5496       p->p_offset = 0;
   5497       p->p_filesz = 0;
   5498       p->p_memsz = 0;
   5499 
   5500       if (m->includes_filehdr)
   5501 	{
   5502 	  if (!m->p_flags_valid)
   5503 	    p->p_flags |= PF_R;
   5504 	  p->p_filesz = bed->s->sizeof_ehdr;
   5505 	  p->p_memsz = bed->s->sizeof_ehdr;
   5506 	  if (m->count > 0)
   5507 	    {
   5508 	      if (p->p_vaddr < (bfd_vma) off
   5509 		  || (!m->p_paddr_valid
   5510 		      && p->p_paddr < (bfd_vma) off))
   5511 		{
   5512 		  _bfd_error_handler
   5513 		    (_("%pB: not enough room for program headers,"
   5514 		       " try linking with -N"),
   5515 		     abfd);
   5516 		  bfd_set_error (bfd_error_bad_value);
   5517 		  return FALSE;
   5518 		}
   5519 
   5520 	      p->p_vaddr -= off;
   5521 	      if (!m->p_paddr_valid)
   5522 		p->p_paddr -= off;
   5523 	    }
   5524 	}
   5525 
   5526       if (m->includes_phdrs)
   5527 	{
   5528 	  if (!m->p_flags_valid)
   5529 	    p->p_flags |= PF_R;
   5530 
   5531 	  if (!m->includes_filehdr)
   5532 	    {
   5533 	      p->p_offset = bed->s->sizeof_ehdr;
   5534 
   5535 	      if (m->count > 0)
   5536 		{
   5537 		  p->p_vaddr -= off - p->p_offset;
   5538 		  if (!m->p_paddr_valid)
   5539 		    p->p_paddr -= off - p->p_offset;
   5540 		}
   5541 	    }
   5542 
   5543 	  p->p_filesz += alloc * bed->s->sizeof_phdr;
   5544 	  p->p_memsz += alloc * bed->s->sizeof_phdr;
   5545 	  if (m->count)
   5546 	    {
   5547 	      p->p_filesz += header_pad;
   5548 	      p->p_memsz += header_pad;
   5549 	    }
   5550 	}
   5551 
   5552       if (p->p_type == PT_LOAD
   5553 	  || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
   5554 	{
   5555 	  if (!m->includes_filehdr && !m->includes_phdrs)
   5556 	    p->p_offset = off;
   5557 	  else
   5558 	    {
   5559 	      file_ptr adjust;
   5560 
   5561 	      adjust = off - (p->p_offset + p->p_filesz);
   5562 	      if (!no_contents)
   5563 		p->p_filesz += adjust;
   5564 	      p->p_memsz += adjust;
   5565 	    }
   5566 	}
   5567 
   5568       /* Set up p_filesz, p_memsz, p_align and p_flags from the section
   5569 	 maps.  Set filepos for sections in PT_LOAD segments, and in
   5570 	 core files, for sections in PT_NOTE segments.
   5571 	 assign_file_positions_for_non_load_sections will set filepos
   5572 	 for other sections and update p_filesz for other segments.  */
   5573       for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   5574 	{
   5575 	  asection *sec;
   5576 	  bfd_size_type align;
   5577 	  Elf_Internal_Shdr *this_hdr;
   5578 
   5579 	  sec = *secpp;
   5580 	  this_hdr = &elf_section_data (sec)->this_hdr;
   5581 	  align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
   5582 
   5583 	  if ((p->p_type == PT_LOAD
   5584 	       || p->p_type == PT_TLS)
   5585 	      && (this_hdr->sh_type != SHT_NOBITS
   5586 		  || ((this_hdr->sh_flags & SHF_ALLOC) != 0
   5587 		      && ((this_hdr->sh_flags & SHF_TLS) == 0
   5588 			  || p->p_type == PT_TLS))))
   5589 	    {
   5590 	      bfd_vma p_start = p->p_paddr;
   5591 	      bfd_vma p_end = p_start + p->p_memsz;
   5592 	      bfd_vma s_start = sec->lma;
   5593 	      bfd_vma adjust = s_start - p_end;
   5594 
   5595 	      if (adjust != 0
   5596 		  && (s_start < p_end
   5597 		      || p_end < p_start))
   5598 		{
   5599 		  _bfd_error_handler
   5600 		    /* xgettext:c-format */
   5601 		    (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
   5602 		     abfd, sec, (uint64_t) s_start, (uint64_t) p_end);
   5603 		  adjust = 0;
   5604 		  sec->lma = p_end;
   5605 		}
   5606 	      p->p_memsz += adjust;
   5607 
   5608 	      if (this_hdr->sh_type != SHT_NOBITS)
   5609 		{
   5610 		  if (p->p_filesz + adjust < p->p_memsz)
   5611 		    {
   5612 		      /* We have a PROGBITS section following NOBITS ones.
   5613 			 Allocate file space for the NOBITS section(s) and
   5614 			 zero it.  */
   5615 		      adjust = p->p_memsz - p->p_filesz;
   5616 		      if (!write_zeros (abfd, off, adjust))
   5617 			return FALSE;
   5618 		    }
   5619 		  off += adjust;
   5620 		  p->p_filesz += adjust;
   5621 		}
   5622 	    }
   5623 
   5624 	  if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
   5625 	    {
   5626 	      /* The section at i == 0 is the one that actually contains
   5627 		 everything.  */
   5628 	      if (i == 0)
   5629 		{
   5630 		  this_hdr->sh_offset = sec->filepos = off;
   5631 		  off += this_hdr->sh_size;
   5632 		  p->p_filesz = this_hdr->sh_size;
   5633 		  p->p_memsz = 0;
   5634 		  p->p_align = 1;
   5635 		}
   5636 	      else
   5637 		{
   5638 		  /* The rest are fake sections that shouldn't be written.  */
   5639 		  sec->filepos = 0;
   5640 		  sec->size = 0;
   5641 		  sec->flags = 0;
   5642 		  continue;
   5643 		}
   5644 	    }
   5645 	  else
   5646 	    {
   5647 	      if (p->p_type == PT_LOAD)
   5648 		{
   5649 		  this_hdr->sh_offset = sec->filepos = off;
   5650 		  if (this_hdr->sh_type != SHT_NOBITS)
   5651 		    off += this_hdr->sh_size;
   5652 		}
   5653 	      else if (this_hdr->sh_type == SHT_NOBITS
   5654 		       && (this_hdr->sh_flags & SHF_TLS) != 0
   5655 		       && this_hdr->sh_offset == 0)
   5656 		{
   5657 		  /* This is a .tbss section that didn't get a PT_LOAD.
   5658 		     (See _bfd_elf_map_sections_to_segments "Create a
   5659 		     final PT_LOAD".)  Set sh_offset to the value it
   5660 		     would have if we had created a zero p_filesz and
   5661 		     p_memsz PT_LOAD header for the section.  This
   5662 		     also makes the PT_TLS header have the same
   5663 		     p_offset value.  */
   5664 		  bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
   5665 							  off, align);
   5666 		  this_hdr->sh_offset = sec->filepos = off + adjust;
   5667 		}
   5668 
   5669 	      if (this_hdr->sh_type != SHT_NOBITS)
   5670 		{
   5671 		  p->p_filesz += this_hdr->sh_size;
   5672 		  /* A load section without SHF_ALLOC is something like
   5673 		     a note section in a PT_NOTE segment.  These take
   5674 		     file space but are not loaded into memory.  */
   5675 		  if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   5676 		    p->p_memsz += this_hdr->sh_size;
   5677 		}
   5678 	      else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   5679 		{
   5680 		  if (p->p_type == PT_TLS)
   5681 		    p->p_memsz += this_hdr->sh_size;
   5682 
   5683 		  /* .tbss is special.  It doesn't contribute to p_memsz of
   5684 		     normal segments.  */
   5685 		  else if ((this_hdr->sh_flags & SHF_TLS) == 0)
   5686 		    p->p_memsz += this_hdr->sh_size;
   5687 		}
   5688 
   5689 	      if (align > p->p_align
   5690 		  && !m->p_align_valid
   5691 		  && (p->p_type != PT_LOAD
   5692 		      || (abfd->flags & D_PAGED) == 0))
   5693 		p->p_align = align;
   5694 	    }
   5695 
   5696 	  if (!m->p_flags_valid)
   5697 	    {
   5698 	      p->p_flags |= PF_R;
   5699 	      if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
   5700 		p->p_flags |= PF_X;
   5701 	      if ((this_hdr->sh_flags & SHF_WRITE) != 0)
   5702 		p->p_flags |= PF_W;
   5703 	    }
   5704 	}
   5705 
   5706       off -= off_adjust;
   5707 
   5708       /* Check that all sections are in a PT_LOAD segment.
   5709 	 Don't check funky gdb generated core files.  */
   5710       if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
   5711 	{
   5712 	  bfd_boolean check_vma = TRUE;
   5713 
   5714 	  for (i = 1; i < m->count; i++)
   5715 	    if (m->sections[i]->vma == m->sections[i - 1]->vma
   5716 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
   5717 				       ->this_hdr), p) != 0
   5718 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
   5719 				       ->this_hdr), p) != 0)
   5720 	      {
   5721 		/* Looks like we have overlays packed into the segment.  */
   5722 		check_vma = FALSE;
   5723 		break;
   5724 	      }
   5725 
   5726 	  for (i = 0; i < m->count; i++)
   5727 	    {
   5728 	      Elf_Internal_Shdr *this_hdr;
   5729 	      asection *sec;
   5730 
   5731 	      sec = m->sections[i];
   5732 	      this_hdr = &(elf_section_data(sec)->this_hdr);
   5733 	      if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
   5734 		  && !ELF_TBSS_SPECIAL (this_hdr, p))
   5735 		{
   5736 		  _bfd_error_handler
   5737 		    /* xgettext:c-format */
   5738 		    (_("%pB: section `%pA' can't be allocated in segment %d"),
   5739 		     abfd, sec, j);
   5740 		  print_segment_map (m);
   5741 		}
   5742 	    }
   5743 	}
   5744     }
   5745 
   5746   elf_next_file_pos (abfd) = off;
   5747   return TRUE;
   5748 }
   5749 
   5750 /* Assign file positions for the other sections.  */
   5751 
   5752 static bfd_boolean
   5753 assign_file_positions_for_non_load_sections (bfd *abfd,
   5754 					     struct bfd_link_info *link_info)
   5755 {
   5756   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   5757   Elf_Internal_Shdr **i_shdrpp;
   5758   Elf_Internal_Shdr **hdrpp, **end_hdrpp;
   5759   Elf_Internal_Phdr *phdrs;
   5760   Elf_Internal_Phdr *p;
   5761   struct elf_segment_map *m;
   5762   struct elf_segment_map *hdrs_segment;
   5763   bfd_vma filehdr_vaddr, filehdr_paddr;
   5764   bfd_vma phdrs_vaddr, phdrs_paddr;
   5765   file_ptr off;
   5766   unsigned int count;
   5767 
   5768   i_shdrpp = elf_elfsections (abfd);
   5769   end_hdrpp = i_shdrpp + elf_numsections (abfd);
   5770   off = elf_next_file_pos (abfd);
   5771   for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
   5772     {
   5773       Elf_Internal_Shdr *hdr;
   5774 
   5775       hdr = *hdrpp;
   5776       if (hdr->bfd_section != NULL
   5777 	  && (hdr->bfd_section->filepos != 0
   5778 	      || (hdr->sh_type == SHT_NOBITS
   5779 		  && hdr->contents == NULL)))
   5780 	BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
   5781       else if ((hdr->sh_flags & SHF_ALLOC) != 0)
   5782 	{
   5783 	  if (hdr->sh_size != 0)
   5784 	    _bfd_error_handler
   5785 	      /* xgettext:c-format */
   5786 	      (_("%pB: warning: allocated section `%s' not in segment"),
   5787 	       abfd,
   5788 	       (hdr->bfd_section == NULL
   5789 		? "*unknown*"
   5790 		: hdr->bfd_section->name));
   5791 	  /* We don't need to page align empty sections.  */
   5792 	  if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
   5793 	    off += vma_page_aligned_bias (hdr->sh_addr, off,
   5794 					  bed->maxpagesize);
   5795 	  else
   5796 	    off += vma_page_aligned_bias (hdr->sh_addr, off,
   5797 					  hdr->sh_addralign);
   5798 	  off = _bfd_elf_assign_file_position_for_section (hdr, off,
   5799 							   FALSE);
   5800 	}
   5801       else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   5802 		&& hdr->bfd_section == NULL)
   5803 	       || (hdr->bfd_section != NULL
   5804 		   && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
   5805 		   /* Compress DWARF debug sections.  */
   5806 	       || hdr == i_shdrpp[elf_onesymtab (abfd)]
   5807 	       || (elf_symtab_shndx_list (abfd) != NULL
   5808 		   && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   5809 	       || hdr == i_shdrpp[elf_strtab_sec (abfd)]
   5810 	       || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
   5811 	hdr->sh_offset = -1;
   5812       else
   5813 	off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
   5814     }
   5815 
   5816   /* Now that we have set the section file positions, we can set up
   5817      the file positions for the non PT_LOAD segments.  */
   5818   count = 0;
   5819   filehdr_vaddr = 0;
   5820   filehdr_paddr = 0;
   5821   phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
   5822   phdrs_paddr = 0;
   5823   hdrs_segment = NULL;
   5824   phdrs = elf_tdata (abfd)->phdr;
   5825   for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
   5826     {
   5827       ++count;
   5828       if (p->p_type != PT_LOAD)
   5829 	continue;
   5830 
   5831       if (m->includes_filehdr)
   5832 	{
   5833 	  filehdr_vaddr = p->p_vaddr;
   5834 	  filehdr_paddr = p->p_paddr;
   5835 	}
   5836       if (m->includes_phdrs)
   5837 	{
   5838 	  phdrs_vaddr = p->p_vaddr;
   5839 	  phdrs_paddr = p->p_paddr;
   5840 	  if (m->includes_filehdr)
   5841 	    {
   5842 	      hdrs_segment = m;
   5843 	      phdrs_vaddr += bed->s->sizeof_ehdr;
   5844 	      phdrs_paddr += bed->s->sizeof_ehdr;
   5845 	    }
   5846 	}
   5847     }
   5848 
   5849   if (hdrs_segment != NULL && link_info != NULL)
   5850     {
   5851       /* There is a segment that contains both the file headers and the
   5852 	 program headers, so provide a symbol __ehdr_start pointing there.
   5853 	 A program can use this to examine itself robustly.  */
   5854 
   5855       struct elf_link_hash_entry *hash
   5856 	= elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
   5857 				FALSE, FALSE, TRUE);
   5858       /* If the symbol was referenced and not defined, define it.  */
   5859       if (hash != NULL
   5860 	  && (hash->root.type == bfd_link_hash_new
   5861 	      || hash->root.type == bfd_link_hash_undefined
   5862 	      || hash->root.type == bfd_link_hash_undefweak
   5863 	      || hash->root.type == bfd_link_hash_common))
   5864 	{
   5865 	  asection *s = NULL;
   5866 	  if (hdrs_segment->count != 0)
   5867 	    /* The segment contains sections, so use the first one.  */
   5868 	    s = hdrs_segment->sections[0];
   5869 	  else
   5870 	    /* Use the first (i.e. lowest-addressed) section in any segment.  */
   5871 	    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   5872 	      if (m->count != 0)
   5873 		{
   5874 		  s = m->sections[0];
   5875 		  break;
   5876 		}
   5877 
   5878 	  if (s != NULL)
   5879 	    {
   5880 	      hash->root.u.def.value = filehdr_vaddr - s->vma;
   5881 	      hash->root.u.def.section = s;
   5882 	    }
   5883 	  else
   5884 	    {
   5885 	      hash->root.u.def.value = filehdr_vaddr;
   5886 	      hash->root.u.def.section = bfd_abs_section_ptr;
   5887 	    }
   5888 
   5889 	  hash->root.type = bfd_link_hash_defined;
   5890 	  hash->def_regular = 1;
   5891 	  hash->non_elf = 0;
   5892 	}
   5893     }
   5894 
   5895   for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
   5896     {
   5897       if (p->p_type == PT_GNU_RELRO)
   5898 	{
   5899 	  bfd_vma start, end;
   5900 	  bfd_boolean ok;
   5901 
   5902 	  if (link_info != NULL)
   5903 	    {
   5904 	      /* During linking the range of the RELRO segment is passed
   5905 		 in link_info.  Note that there may be padding between
   5906 		 relro_start and the first RELRO section.  */
   5907 	      start = link_info->relro_start;
   5908 	      end = link_info->relro_end;
   5909 	    }
   5910 	  else if (m->count != 0)
   5911 	    {
   5912 	      if (!m->p_size_valid)
   5913 		abort ();
   5914 	      start = m->sections[0]->vma;
   5915 	      end = start + m->p_size;
   5916 	    }
   5917 	  else
   5918 	    {
   5919 	      start = 0;
   5920 	      end = 0;
   5921 	    }
   5922 
   5923 	  ok = FALSE;
   5924 	  if (start < end)
   5925 	    {
   5926 	      struct elf_segment_map *lm;
   5927 	      const Elf_Internal_Phdr *lp;
   5928 	      unsigned int i;
   5929 
   5930 	      /* Find a LOAD segment containing a section in the RELRO
   5931 		 segment.  */
   5932 	      for (lm = elf_seg_map (abfd), lp = phdrs;
   5933 		   lm != NULL;
   5934 		   lm = lm->next, lp++)
   5935 		{
   5936 		  if (lp->p_type == PT_LOAD
   5937 		      && lm->count != 0
   5938 		      && (lm->sections[lm->count - 1]->vma
   5939 			  + (!IS_TBSS (lm->sections[lm->count - 1])
   5940 			     ? lm->sections[lm->count - 1]->size
   5941 			     : 0)) > start
   5942 		      && lm->sections[0]->vma < end)
   5943 		    break;
   5944 		}
   5945 
   5946 	      if (lm != NULL)
   5947 		{
   5948 		  /* Find the section starting the RELRO segment.  */
   5949 		  for (i = 0; i < lm->count; i++)
   5950 		    {
   5951 		      asection *s = lm->sections[i];
   5952 		      if (s->vma >= start
   5953 			  && s->vma < end
   5954 			  && s->size != 0)
   5955 			break;
   5956 		    }
   5957 
   5958 		  if (i < lm->count)
   5959 		    {
   5960 		      p->p_vaddr = lm->sections[i]->vma;
   5961 		      p->p_paddr = lm->sections[i]->lma;
   5962 		      p->p_offset = lm->sections[i]->filepos;
   5963 		      p->p_memsz = end - p->p_vaddr;
   5964 		      p->p_filesz = p->p_memsz;
   5965 
   5966 		      /* The RELRO segment typically ends a few bytes
   5967 			 into .got.plt but other layouts are possible.
   5968 			 In cases where the end does not match any
   5969 			 loaded section (for instance is in file
   5970 			 padding), trim p_filesz back to correspond to
   5971 			 the end of loaded section contents.  */
   5972 		      if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
   5973 			p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
   5974 
   5975 		      /* Preserve the alignment and flags if they are
   5976 			 valid.  The gold linker generates RW/4 for
   5977 			 the PT_GNU_RELRO section.  It is better for
   5978 			 objcopy/strip to honor these attributes
   5979 			 otherwise gdb will choke when using separate
   5980 			 debug files.  */
   5981 		      if (!m->p_align_valid)
   5982 			p->p_align = 1;
   5983 		      if (!m->p_flags_valid)
   5984 			p->p_flags = PF_R;
   5985 		      ok = TRUE;
   5986 		    }
   5987 		}
   5988 	    }
   5989 	  if (link_info != NULL)
   5990 	    BFD_ASSERT (ok);
   5991 	  if (!ok)
   5992 	    memset (p, 0, sizeof *p);
   5993 	}
   5994       else if (p->p_type == PT_GNU_STACK)
   5995 	{
   5996 	  if (m->p_size_valid)
   5997 	    p->p_memsz = m->p_size;
   5998 	}
   5999       else if (m->count != 0)
   6000 	{
   6001 	  unsigned int i;
   6002 
   6003 	  if (p->p_type != PT_LOAD
   6004 	      && (p->p_type != PT_NOTE
   6005 		  || bfd_get_format (abfd) != bfd_core))
   6006 	    {
   6007 	      /* A user specified segment layout may include a PHDR
   6008 		 segment that overlaps with a LOAD segment...  */
   6009 	      if (p->p_type == PT_PHDR)
   6010 		{
   6011 		  m->count = 0;
   6012 		  continue;
   6013 		}
   6014 
   6015 	      if (m->includes_filehdr || m->includes_phdrs)
   6016 		{
   6017 		  /* PR 17512: file: 2195325e.  */
   6018 		  _bfd_error_handler
   6019 		    (_("%pB: error: non-load segment %d includes file header "
   6020 		       "and/or program header"),
   6021 		     abfd, (int) (p - phdrs));
   6022 		  return FALSE;
   6023 		}
   6024 
   6025 	      p->p_filesz = 0;
   6026 	      p->p_offset = m->sections[0]->filepos;
   6027 	      for (i = m->count; i-- != 0;)
   6028 		{
   6029 		  asection *sect = m->sections[i];
   6030 		  Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
   6031 		  if (hdr->sh_type != SHT_NOBITS)
   6032 		    {
   6033 		      p->p_filesz = (sect->filepos - m->sections[0]->filepos
   6034 				     + hdr->sh_size);
   6035 		      break;
   6036 		    }
   6037 		}
   6038 	    }
   6039 	}
   6040       else if (m->includes_filehdr)
   6041 	{
   6042 	  p->p_vaddr = filehdr_vaddr;
   6043 	  if (! m->p_paddr_valid)
   6044 	    p->p_paddr = filehdr_paddr;
   6045 	}
   6046       else if (m->includes_phdrs)
   6047 	{
   6048 	  p->p_vaddr = phdrs_vaddr;
   6049 	  if (! m->p_paddr_valid)
   6050 	    p->p_paddr = phdrs_paddr;
   6051 	}
   6052     }
   6053 
   6054   elf_next_file_pos (abfd) = off;
   6055 
   6056   return TRUE;
   6057 }
   6058 
   6059 static elf_section_list *
   6060 find_section_in_list (unsigned int i, elf_section_list * list)
   6061 {
   6062   for (;list != NULL; list = list->next)
   6063     if (list->ndx == i)
   6064       break;
   6065   return list;
   6066 }
   6067 
   6068 /* Work out the file positions of all the sections.  This is called by
   6069    _bfd_elf_compute_section_file_positions.  All the section sizes and
   6070    VMAs must be known before this is called.
   6071 
   6072    Reloc sections come in two flavours: Those processed specially as
   6073    "side-channel" data attached to a section to which they apply, and
   6074    those that bfd doesn't process as relocations.  The latter sort are
   6075    stored in a normal bfd section by bfd_section_from_shdr.   We don't
   6076    consider the former sort here, unless they form part of the loadable
   6077    image.  Reloc sections not assigned here will be handled later by
   6078    assign_file_positions_for_relocs.
   6079 
   6080    We also don't set the positions of the .symtab and .strtab here.  */
   6081 
   6082 static bfd_boolean
   6083 assign_file_positions_except_relocs (bfd *abfd,
   6084 				     struct bfd_link_info *link_info)
   6085 {
   6086   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   6087   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
   6088   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6089 
   6090   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
   6091       && bfd_get_format (abfd) != bfd_core)
   6092     {
   6093       Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
   6094       unsigned int num_sec = elf_numsections (abfd);
   6095       Elf_Internal_Shdr **hdrpp;
   6096       unsigned int i;
   6097       file_ptr off;
   6098 
   6099       /* Start after the ELF header.  */
   6100       off = i_ehdrp->e_ehsize;
   6101 
   6102       /* We are not creating an executable, which means that we are
   6103 	 not creating a program header, and that the actual order of
   6104 	 the sections in the file is unimportant.  */
   6105       for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
   6106 	{
   6107 	  Elf_Internal_Shdr *hdr;
   6108 
   6109 	  hdr = *hdrpp;
   6110 	  if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   6111 	       && hdr->bfd_section == NULL)
   6112 	      || (hdr->bfd_section != NULL
   6113 		  && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
   6114 		  /* Compress DWARF debug sections.  */
   6115 	      || i == elf_onesymtab (abfd)
   6116 	      || (elf_symtab_shndx_list (abfd) != NULL
   6117 		  && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   6118 	      || i == elf_strtab_sec (abfd)
   6119 	      || i == elf_shstrtab_sec (abfd))
   6120 	    {
   6121 	      hdr->sh_offset = -1;
   6122 	    }
   6123 	  else
   6124 	    off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
   6125 	}
   6126 
   6127       elf_next_file_pos (abfd) = off;
   6128     }
   6129   else
   6130     {
   6131       unsigned int alloc;
   6132 
   6133       /* Assign file positions for the loaded sections based on the
   6134 	 assignment of sections to segments.  */
   6135       if (!assign_file_positions_for_load_sections (abfd, link_info))
   6136 	return FALSE;
   6137 
   6138       /* And for non-load sections.  */
   6139       if (!assign_file_positions_for_non_load_sections (abfd, link_info))
   6140 	return FALSE;
   6141 
   6142       if (bed->elf_backend_modify_program_headers != NULL)
   6143 	{
   6144 	  if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
   6145 	    return FALSE;
   6146 	}
   6147 
   6148       /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.  */
   6149       if (link_info != NULL && bfd_link_pie (link_info))
   6150 	{
   6151 	  unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
   6152 	  Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
   6153 	  Elf_Internal_Phdr *end_segment = &segment[num_segments];
   6154 
   6155 	  /* Find the lowest p_vaddr in PT_LOAD segments.  */
   6156 	  bfd_vma p_vaddr = (bfd_vma) -1;
   6157 	  for (; segment < end_segment; segment++)
   6158 	    if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
   6159 	      p_vaddr = segment->p_vaddr;
   6160 
   6161 	  /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
   6162 	     segments is non-zero.  */
   6163 	  if (p_vaddr)
   6164 	    i_ehdrp->e_type = ET_EXEC;
   6165 	}
   6166 
   6167       /* Write out the program headers.  */
   6168       alloc = elf_elfheader (abfd)->e_phnum;
   6169       if (alloc == 0)
   6170 	return TRUE;
   6171 
   6172       /* PR ld/20815 - Check that the program header segment, if present, will
   6173 	 be loaded into memory.  FIXME: The check below is not sufficient as
   6174 	 really all PT_LOAD segments should be checked before issuing an error
   6175 	 message.  Plus the PHDR segment does not have to be the first segment
   6176 	 in the program header table.  But this version of the check should
   6177 	 catch all real world use cases.
   6178 
   6179 	 FIXME: We used to have code here to sort the PT_LOAD segments into
   6180 	 ascending order, as per the ELF spec.  But this breaks some programs,
   6181 	 including the Linux kernel.  But really either the spec should be
   6182 	 changed or the programs updated.  */
   6183       if (alloc > 1
   6184 	  && tdata->phdr[0].p_type == PT_PHDR
   6185 	  && (bed->elf_backend_allow_non_load_phdr == NULL
   6186 	      || !bed->elf_backend_allow_non_load_phdr (abfd, tdata->phdr,
   6187 							alloc))
   6188 	  && tdata->phdr[1].p_type == PT_LOAD
   6189 	  && (tdata->phdr[1].p_vaddr > tdata->phdr[0].p_vaddr
   6190 	      || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz
   6191 		  < tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz)))
   6192 	{
   6193 	  /* The fix for this error is usually to edit the linker script being
   6194 	     used and set up the program headers manually.  Either that or
   6195 	     leave room for the headers at the start of the SECTIONS.  */
   6196 	  _bfd_error_handler (_("%pB: error: PHDR segment not covered"
   6197 				" by LOAD segment"),
   6198 			      abfd);
   6199 	  return FALSE;
   6200 	}
   6201 
   6202       if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
   6203 	  || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
   6204 	return FALSE;
   6205     }
   6206 
   6207   return TRUE;
   6208 }
   6209 
   6210 static bfd_boolean
   6211 prep_headers (bfd *abfd)
   6212 {
   6213   Elf_Internal_Ehdr *i_ehdrp;	/* Elf file header, internal form.  */
   6214   struct elf_strtab_hash *shstrtab;
   6215   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6216 
   6217   i_ehdrp = elf_elfheader (abfd);
   6218 
   6219   shstrtab = _bfd_elf_strtab_init ();
   6220   if (shstrtab == NULL)
   6221     return FALSE;
   6222 
   6223   elf_shstrtab (abfd) = shstrtab;
   6224 
   6225   i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
   6226   i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
   6227   i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
   6228   i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
   6229 
   6230   i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
   6231   i_ehdrp->e_ident[EI_DATA] =
   6232     bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
   6233   i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
   6234 
   6235   if ((abfd->flags & DYNAMIC) != 0)
   6236     i_ehdrp->e_type = ET_DYN;
   6237   else if ((abfd->flags & EXEC_P) != 0)
   6238     i_ehdrp->e_type = ET_EXEC;
   6239   else if (bfd_get_format (abfd) == bfd_core)
   6240     i_ehdrp->e_type = ET_CORE;
   6241   else
   6242     i_ehdrp->e_type = ET_REL;
   6243 
   6244   switch (bfd_get_arch (abfd))
   6245     {
   6246     case bfd_arch_unknown:
   6247       i_ehdrp->e_machine = EM_NONE;
   6248       break;
   6249 
   6250       /* There used to be a long list of cases here, each one setting
   6251 	 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
   6252 	 in the corresponding bfd definition.  To avoid duplication,
   6253 	 the switch was removed.  Machines that need special handling
   6254 	 can generally do it in elf_backend_final_write_processing(),
   6255 	 unless they need the information earlier than the final write.
   6256 	 Such need can generally be supplied by replacing the tests for
   6257 	 e_machine with the conditions used to determine it.  */
   6258     default:
   6259       i_ehdrp->e_machine = bed->elf_machine_code;
   6260     }
   6261 
   6262   i_ehdrp->e_version = bed->s->ev_current;
   6263   i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
   6264 
   6265   /* No program header, for now.  */
   6266   i_ehdrp->e_phoff = 0;
   6267   i_ehdrp->e_phentsize = 0;
   6268   i_ehdrp->e_phnum = 0;
   6269 
   6270   /* Each bfd section is section header entry.  */
   6271   i_ehdrp->e_entry = bfd_get_start_address (abfd);
   6272   i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
   6273 
   6274   /* If we're building an executable, we'll need a program header table.  */
   6275   if (abfd->flags & EXEC_P)
   6276     /* It all happens later.  */
   6277     ;
   6278   else
   6279     {
   6280       i_ehdrp->e_phentsize = 0;
   6281       i_ehdrp->e_phoff = 0;
   6282     }
   6283 
   6284   elf_tdata (abfd)->symtab_hdr.sh_name =
   6285     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
   6286   elf_tdata (abfd)->strtab_hdr.sh_name =
   6287     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
   6288   elf_tdata (abfd)->shstrtab_hdr.sh_name =
   6289     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
   6290   if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
   6291       || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
   6292       || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
   6293     return FALSE;
   6294 
   6295   return TRUE;
   6296 }
   6297 
   6298 /* Assign file positions for all the reloc sections which are not part
   6299    of the loadable file image, and the file position of section headers.  */
   6300 
   6301 static bfd_boolean
   6302 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
   6303 {
   6304   file_ptr off;
   6305   Elf_Internal_Shdr **shdrpp, **end_shdrpp;
   6306   Elf_Internal_Shdr *shdrp;
   6307   Elf_Internal_Ehdr *i_ehdrp;
   6308   const struct elf_backend_data *bed;
   6309 
   6310   off = elf_next_file_pos (abfd);
   6311 
   6312   shdrpp = elf_elfsections (abfd);
   6313   end_shdrpp = shdrpp + elf_numsections (abfd);
   6314   for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
   6315     {
   6316       shdrp = *shdrpp;
   6317       if (shdrp->sh_offset == -1)
   6318 	{
   6319 	  asection *sec = shdrp->bfd_section;
   6320 	  bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
   6321 				|| shdrp->sh_type == SHT_RELA);
   6322 	  if (is_rel
   6323 	      || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
   6324 	    {
   6325 	      if (!is_rel)
   6326 		{
   6327 		  const char *name = sec->name;
   6328 		  struct bfd_elf_section_data *d;
   6329 
   6330 		  /* Compress DWARF debug sections.  */
   6331 		  if (!bfd_compress_section (abfd, sec,
   6332 					     shdrp->contents))
   6333 		    return FALSE;
   6334 
   6335 		  if (sec->compress_status == COMPRESS_SECTION_DONE
   6336 		      && (abfd->flags & BFD_COMPRESS_GABI) == 0)
   6337 		    {
   6338 		      /* If section is compressed with zlib-gnu, convert
   6339 			 section name from .debug_* to .zdebug_*.  */
   6340 		      char *new_name
   6341 			= convert_debug_to_zdebug (abfd, name);
   6342 		      if (new_name == NULL)
   6343 			return FALSE;
   6344 		      name = new_name;
   6345 		    }
   6346 		  /* Add section name to section name section.  */
   6347 		  if (shdrp->sh_name != (unsigned int) -1)
   6348 		    abort ();
   6349 		  shdrp->sh_name
   6350 		    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   6351 							  name, FALSE);
   6352 		  d = elf_section_data (sec);
   6353 
   6354 		  /* Add reloc section name to section name section.  */
   6355 		  if (d->rel.hdr
   6356 		      && !_bfd_elf_set_reloc_sh_name (abfd,
   6357 						      d->rel.hdr,
   6358 						      name, FALSE))
   6359 		    return FALSE;
   6360 		  if (d->rela.hdr
   6361 		      && !_bfd_elf_set_reloc_sh_name (abfd,
   6362 						      d->rela.hdr,
   6363 						      name, TRUE))
   6364 		    return FALSE;
   6365 
   6366 		  /* Update section size and contents.  */
   6367 		  shdrp->sh_size = sec->size;
   6368 		  shdrp->contents = sec->contents;
   6369 		  shdrp->bfd_section->contents = NULL;
   6370 		}
   6371 	      off = _bfd_elf_assign_file_position_for_section (shdrp,
   6372 							       off,
   6373 							       TRUE);
   6374 	    }
   6375 	}
   6376     }
   6377 
   6378   /* Place section name section after DWARF debug sections have been
   6379      compressed.  */
   6380   _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
   6381   shdrp = &elf_tdata (abfd)->shstrtab_hdr;
   6382   shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
   6383   off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
   6384 
   6385   /* Place the section headers.  */
   6386   i_ehdrp = elf_elfheader (abfd);
   6387   bed = get_elf_backend_data (abfd);
   6388   off = align_file_position (off, 1 << bed->s->log_file_align);
   6389   i_ehdrp->e_shoff = off;
   6390   off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
   6391   elf_next_file_pos (abfd) = off;
   6392 
   6393   return TRUE;
   6394 }
   6395 
   6396 bfd_boolean
   6397 _bfd_elf_write_object_contents (bfd *abfd)
   6398 {
   6399   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6400   Elf_Internal_Shdr **i_shdrp;
   6401   bfd_boolean failed;
   6402   unsigned int count, num_sec;
   6403   struct elf_obj_tdata *t;
   6404 
   6405   if (! abfd->output_has_begun
   6406       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   6407     return FALSE;
   6408 
   6409   i_shdrp = elf_elfsections (abfd);
   6410 
   6411   failed = FALSE;
   6412   bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
   6413   if (failed)
   6414     return FALSE;
   6415 
   6416   if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
   6417     return FALSE;
   6418 
   6419   /* After writing the headers, we need to write the sections too...  */
   6420   num_sec = elf_numsections (abfd);
   6421   for (count = 1; count < num_sec; count++)
   6422     {
   6423       i_shdrp[count]->sh_name
   6424 	= _bfd_elf_strtab_offset (elf_shstrtab (abfd),
   6425 				  i_shdrp[count]->sh_name);
   6426       if (bed->elf_backend_section_processing)
   6427 	if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
   6428 	  return FALSE;
   6429       if (i_shdrp[count]->contents)
   6430 	{
   6431 	  bfd_size_type amt = i_shdrp[count]->sh_size;
   6432 
   6433 	  if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
   6434 	      || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
   6435 	    return FALSE;
   6436 	}
   6437     }
   6438 
   6439   /* Write out the section header names.  */
   6440   t = elf_tdata (abfd);
   6441   if (elf_shstrtab (abfd) != NULL
   6442       && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
   6443 	  || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
   6444     return FALSE;
   6445 
   6446   if (bed->elf_backend_final_write_processing)
   6447     (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd));
   6448 
   6449   if (!bed->s->write_shdrs_and_ehdr (abfd))
   6450     return FALSE;
   6451 
   6452   /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
   6453   if (t->o->build_id.after_write_object_contents != NULL)
   6454     return (*t->o->build_id.after_write_object_contents) (abfd);
   6455 
   6456   return TRUE;
   6457 }
   6458 
   6459 bfd_boolean
   6460 _bfd_elf_write_corefile_contents (bfd *abfd)
   6461 {
   6462   /* Hopefully this can be done just like an object file.  */
   6463   return _bfd_elf_write_object_contents (abfd);
   6464 }
   6465 
   6466 /* Given a section, search the header to find them.  */
   6467 
   6468 unsigned int
   6469 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
   6470 {
   6471   const struct elf_backend_data *bed;
   6472   unsigned int sec_index;
   6473 
   6474   if (elf_section_data (asect) != NULL
   6475       && elf_section_data (asect)->this_idx != 0)
   6476     return elf_section_data (asect)->this_idx;
   6477 
   6478   if (bfd_is_abs_section (asect))
   6479     sec_index = SHN_ABS;
   6480   else if (bfd_is_com_section (asect))
   6481     sec_index = SHN_COMMON;
   6482   else if (bfd_is_und_section (asect))
   6483     sec_index = SHN_UNDEF;
   6484   else
   6485     sec_index = SHN_BAD;
   6486 
   6487   bed = get_elf_backend_data (abfd);
   6488   if (bed->elf_backend_section_from_bfd_section)
   6489     {
   6490       int retval = sec_index;
   6491 
   6492       if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
   6493 	return retval;
   6494     }
   6495 
   6496   if (sec_index == SHN_BAD)
   6497     bfd_set_error (bfd_error_nonrepresentable_section);
   6498 
   6499   return sec_index;
   6500 }
   6501 
   6502 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
   6503    on error.  */
   6504 
   6505 int
   6506 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
   6507 {
   6508   asymbol *asym_ptr = *asym_ptr_ptr;
   6509   int idx;
   6510   flagword flags = asym_ptr->flags;
   6511 
   6512   /* When gas creates relocations against local labels, it creates its
   6513      own symbol for the section, but does put the symbol into the
   6514      symbol chain, so udata is 0.  When the linker is generating
   6515      relocatable output, this section symbol may be for one of the
   6516      input sections rather than the output section.  */
   6517   if (asym_ptr->udata.i == 0
   6518       && (flags & BSF_SECTION_SYM)
   6519       && asym_ptr->section)
   6520     {
   6521       asection *sec;
   6522       int indx;
   6523 
   6524       sec = asym_ptr->section;
   6525       if (sec->owner != abfd && sec->output_section != NULL)
   6526 	sec = sec->output_section;
   6527       if (sec->owner == abfd
   6528 	  && (indx = sec->index) < elf_num_section_syms (abfd)
   6529 	  && elf_section_syms (abfd)[indx] != NULL)
   6530 	asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
   6531     }
   6532 
   6533   idx = asym_ptr->udata.i;
   6534 
   6535   if (idx == 0)
   6536     {
   6537       /* This case can occur when using --strip-symbol on a symbol
   6538 	 which is used in a relocation entry.  */
   6539       _bfd_error_handler
   6540 	/* xgettext:c-format */
   6541 	(_("%pB: symbol `%s' required but not present"),
   6542 	 abfd, bfd_asymbol_name (asym_ptr));
   6543       bfd_set_error (bfd_error_no_symbols);
   6544       return -1;
   6545     }
   6546 
   6547 #if DEBUG & 4
   6548   {
   6549     fprintf (stderr,
   6550 	     "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
   6551 	     (long) asym_ptr, asym_ptr->name, idx, flags);
   6552     fflush (stderr);
   6553   }
   6554 #endif
   6555 
   6556   return idx;
   6557 }
   6558 
   6559 /* Rewrite program header information.  */
   6560 
   6561 static bfd_boolean
   6562 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
   6563 {
   6564   Elf_Internal_Ehdr *iehdr;
   6565   struct elf_segment_map *map;
   6566   struct elf_segment_map *map_first;
   6567   struct elf_segment_map **pointer_to_map;
   6568   Elf_Internal_Phdr *segment;
   6569   asection *section;
   6570   unsigned int i;
   6571   unsigned int num_segments;
   6572   bfd_boolean phdr_included = FALSE;
   6573   bfd_boolean p_paddr_valid;
   6574   bfd_vma maxpagesize;
   6575   struct elf_segment_map *phdr_adjust_seg = NULL;
   6576   unsigned int phdr_adjust_num = 0;
   6577   const struct elf_backend_data *bed;
   6578 
   6579   bed = get_elf_backend_data (ibfd);
   6580   iehdr = elf_elfheader (ibfd);
   6581 
   6582   map_first = NULL;
   6583   pointer_to_map = &map_first;
   6584 
   6585   num_segments = elf_elfheader (ibfd)->e_phnum;
   6586   maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
   6587 
   6588   /* Returns the end address of the segment + 1.  */
   6589 #define SEGMENT_END(segment, start)					\
   6590   (start + (segment->p_memsz > segment->p_filesz			\
   6591 	    ? segment->p_memsz : segment->p_filesz))
   6592 
   6593 #define SECTION_SIZE(section, segment)					\
   6594   (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL))		\
   6595     != SEC_THREAD_LOCAL || segment->p_type == PT_TLS)			\
   6596    ? section->size : 0)
   6597 
   6598   /* Returns TRUE if the given section is contained within
   6599      the given segment.  VMA addresses are compared.  */
   6600 #define IS_CONTAINED_BY_VMA(section, segment)				\
   6601   (section->vma >= segment->p_vaddr					\
   6602    && (section->vma + SECTION_SIZE (section, segment)			\
   6603        <= (SEGMENT_END (segment, segment->p_vaddr))))
   6604 
   6605   /* Returns TRUE if the given section is contained within
   6606      the given segment.  LMA addresses are compared.  */
   6607 #define IS_CONTAINED_BY_LMA(section, segment, base)			\
   6608   (section->lma >= base							\
   6609    && (section->lma + SECTION_SIZE (section, segment)			\
   6610        <= SEGMENT_END (segment, base)))
   6611 
   6612   /* Handle PT_NOTE segment.  */
   6613 #define IS_NOTE(p, s)							\
   6614   (p->p_type == PT_NOTE							\
   6615    && elf_section_type (s) == SHT_NOTE					\
   6616    && (bfd_vma) s->filepos >= p->p_offset				\
   6617    && ((bfd_vma) s->filepos + s->size					\
   6618        <= p->p_offset + p->p_filesz))
   6619 
   6620   /* Special case: corefile "NOTE" section containing regs, prpsinfo
   6621      etc.  */
   6622 #define IS_COREFILE_NOTE(p, s)						\
   6623   (IS_NOTE (p, s)							\
   6624    && bfd_get_format (ibfd) == bfd_core					\
   6625    && s->vma == 0							\
   6626    && s->lma == 0)
   6627 
   6628   /* The complicated case when p_vaddr is 0 is to handle the Solaris
   6629      linker, which generates a PT_INTERP section with p_vaddr and
   6630      p_memsz set to 0.  */
   6631 #define IS_SOLARIS_PT_INTERP(p, s)					\
   6632   (p->p_vaddr == 0							\
   6633    && p->p_paddr == 0							\
   6634    && p->p_memsz == 0							\
   6635    && p->p_filesz > 0							\
   6636    && (s->flags & SEC_HAS_CONTENTS) != 0				\
   6637    && s->size > 0							\
   6638    && (bfd_vma) s->filepos >= p->p_offset				\
   6639    && ((bfd_vma) s->filepos + s->size					\
   6640        <= p->p_offset + p->p_filesz))
   6641 
   6642   /* Decide if the given section should be included in the given segment.
   6643      A section will be included if:
   6644        1. It is within the address space of the segment -- we use the LMA
   6645 	  if that is set for the segment and the VMA otherwise,
   6646        2. It is an allocated section or a NOTE section in a PT_NOTE
   6647 	  segment.
   6648        3. There is an output section associated with it,
   6649        4. The section has not already been allocated to a previous segment.
   6650        5. PT_GNU_STACK segments do not include any sections.
   6651        6. PT_TLS segment includes only SHF_TLS sections.
   6652        7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
   6653        8. PT_DYNAMIC should not contain empty sections at the beginning
   6654 	  (with the possible exception of .dynamic).  */
   6655 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed)		\
   6656   ((((segment->p_paddr							\
   6657       ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr)	\
   6658       : IS_CONTAINED_BY_VMA (section, segment))				\
   6659      && (section->flags & SEC_ALLOC) != 0)				\
   6660     || IS_NOTE (segment, section))					\
   6661    && segment->p_type != PT_GNU_STACK					\
   6662    && (segment->p_type != PT_TLS					\
   6663        || (section->flags & SEC_THREAD_LOCAL))				\
   6664    && (segment->p_type == PT_LOAD					\
   6665        || segment->p_type == PT_TLS					\
   6666        || (section->flags & SEC_THREAD_LOCAL) == 0)			\
   6667    && (segment->p_type != PT_DYNAMIC					\
   6668        || SECTION_SIZE (section, segment) > 0				\
   6669        || (segment->p_paddr						\
   6670 	   ? segment->p_paddr != section->lma				\
   6671 	   : segment->p_vaddr != section->vma)				\
   6672        || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic")	\
   6673 	   == 0))							\
   6674    && (segment->p_type != PT_LOAD || !section->segment_mark))
   6675 
   6676 /* If the output section of a section in the input segment is NULL,
   6677    it is removed from the corresponding output segment.   */
   6678 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed)		\
   6679   (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)		\
   6680    && section->output_section != NULL)
   6681 
   6682   /* Returns TRUE iff seg1 starts after the end of seg2.  */
   6683 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)			\
   6684   (seg1->field >= SEGMENT_END (seg2, seg2->field))
   6685 
   6686   /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
   6687      their VMA address ranges and their LMA address ranges overlap.
   6688      It is possible to have overlapping VMA ranges without overlapping LMA
   6689      ranges.  RedBoot images for example can have both .data and .bss mapped
   6690      to the same VMA range, but with the .data section mapped to a different
   6691      LMA.  */
   6692 #define SEGMENT_OVERLAPS(seg1, seg2)					\
   6693   (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)			\
   6694 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))			\
   6695    && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)			\
   6696 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
   6697 
   6698   /* Initialise the segment mark field.  */
   6699   for (section = ibfd->sections; section != NULL; section = section->next)
   6700     section->segment_mark = FALSE;
   6701 
   6702   /* The Solaris linker creates program headers in which all the
   6703      p_paddr fields are zero.  When we try to objcopy or strip such a
   6704      file, we get confused.  Check for this case, and if we find it
   6705      don't set the p_paddr_valid fields.  */
   6706   p_paddr_valid = FALSE;
   6707   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   6708        i < num_segments;
   6709        i++, segment++)
   6710     if (segment->p_paddr != 0)
   6711       {
   6712 	p_paddr_valid = TRUE;
   6713 	break;
   6714       }
   6715 
   6716   /* Scan through the segments specified in the program header
   6717      of the input BFD.  For this first scan we look for overlaps
   6718      in the loadable segments.  These can be created by weird
   6719      parameters to objcopy.  Also, fix some solaris weirdness.  */
   6720   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   6721        i < num_segments;
   6722        i++, segment++)
   6723     {
   6724       unsigned int j;
   6725       Elf_Internal_Phdr *segment2;
   6726 
   6727       if (segment->p_type == PT_INTERP)
   6728 	for (section = ibfd->sections; section; section = section->next)
   6729 	  if (IS_SOLARIS_PT_INTERP (segment, section))
   6730 	    {
   6731 	      /* Mininal change so that the normal section to segment
   6732 		 assignment code will work.  */
   6733 	      segment->p_vaddr = section->vma;
   6734 	      break;
   6735 	    }
   6736 
   6737       if (segment->p_type != PT_LOAD)
   6738 	{
   6739 	  /* Remove PT_GNU_RELRO segment.  */
   6740 	  if (segment->p_type == PT_GNU_RELRO)
   6741 	    segment->p_type = PT_NULL;
   6742 	  continue;
   6743 	}
   6744 
   6745       /* Determine if this segment overlaps any previous segments.  */
   6746       for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
   6747 	{
   6748 	  bfd_signed_vma extra_length;
   6749 
   6750 	  if (segment2->p_type != PT_LOAD
   6751 	      || !SEGMENT_OVERLAPS (segment, segment2))
   6752 	    continue;
   6753 
   6754 	  /* Merge the two segments together.  */
   6755 	  if (segment2->p_vaddr < segment->p_vaddr)
   6756 	    {
   6757 	      /* Extend SEGMENT2 to include SEGMENT and then delete
   6758 		 SEGMENT.  */
   6759 	      extra_length = (SEGMENT_END (segment, segment->p_vaddr)
   6760 			      - SEGMENT_END (segment2, segment2->p_vaddr));
   6761 
   6762 	      if (extra_length > 0)
   6763 		{
   6764 		  segment2->p_memsz += extra_length;
   6765 		  segment2->p_filesz += extra_length;
   6766 		}
   6767 
   6768 	      segment->p_type = PT_NULL;
   6769 
   6770 	      /* Since we have deleted P we must restart the outer loop.  */
   6771 	      i = 0;
   6772 	      segment = elf_tdata (ibfd)->phdr;
   6773 	      break;
   6774 	    }
   6775 	  else
   6776 	    {
   6777 	      /* Extend SEGMENT to include SEGMENT2 and then delete
   6778 		 SEGMENT2.  */
   6779 	      extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
   6780 			      - SEGMENT_END (segment, segment->p_vaddr));
   6781 
   6782 	      if (extra_length > 0)
   6783 		{
   6784 		  segment->p_memsz += extra_length;
   6785 		  segment->p_filesz += extra_length;
   6786 		}
   6787 
   6788 	      segment2->p_type = PT_NULL;
   6789 	    }
   6790 	}
   6791     }
   6792 
   6793   /* The second scan attempts to assign sections to segments.  */
   6794   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   6795        i < num_segments;
   6796        i++, segment++)
   6797     {
   6798       unsigned int section_count;
   6799       asection **sections;
   6800       asection *output_section;
   6801       unsigned int isec;
   6802       asection *matching_lma;
   6803       asection *suggested_lma;
   6804       unsigned int j;
   6805       bfd_size_type amt;
   6806       asection *first_section;
   6807 
   6808       if (segment->p_type == PT_NULL)
   6809 	continue;
   6810 
   6811       first_section = NULL;
   6812       /* Compute how many sections might be placed into this segment.  */
   6813       for (section = ibfd->sections, section_count = 0;
   6814 	   section != NULL;
   6815 	   section = section->next)
   6816 	{
   6817 	  /* Find the first section in the input segment, which may be
   6818 	     removed from the corresponding output segment.   */
   6819 	  if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
   6820 	    {
   6821 	      if (first_section == NULL)
   6822 		first_section = section;
   6823 	      if (section->output_section != NULL)
   6824 		++section_count;
   6825 	    }
   6826 	}
   6827 
   6828       /* Allocate a segment map big enough to contain
   6829 	 all of the sections we have selected.  */
   6830       amt = sizeof (struct elf_segment_map);
   6831       amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
   6832       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   6833       if (map == NULL)
   6834 	return FALSE;
   6835 
   6836       /* Initialise the fields of the segment map.  Default to
   6837 	 using the physical address of the segment in the input BFD.  */
   6838       map->next = NULL;
   6839       map->p_type = segment->p_type;
   6840       map->p_flags = segment->p_flags;
   6841       map->p_flags_valid = 1;
   6842 
   6843       /* If the first section in the input segment is removed, there is
   6844 	 no need to preserve segment physical address in the corresponding
   6845 	 output segment.  */
   6846       if (!first_section || first_section->output_section != NULL)
   6847 	{
   6848 	  map->p_paddr = segment->p_paddr;
   6849 	  map->p_paddr_valid = p_paddr_valid;
   6850 	}
   6851 
   6852       /* Determine if this segment contains the ELF file header
   6853 	 and if it contains the program headers themselves.  */
   6854       map->includes_filehdr = (segment->p_offset == 0
   6855 			       && segment->p_filesz >= iehdr->e_ehsize);
   6856       map->includes_phdrs = 0;
   6857 
   6858       if (!phdr_included || segment->p_type != PT_LOAD)
   6859 	{
   6860 	  map->includes_phdrs =
   6861 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   6862 	     && (segment->p_offset + segment->p_filesz
   6863 		 >= ((bfd_vma) iehdr->e_phoff
   6864 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   6865 
   6866 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   6867 	    phdr_included = TRUE;
   6868 	}
   6869 
   6870       if (section_count == 0)
   6871 	{
   6872 	  /* Special segments, such as the PT_PHDR segment, may contain
   6873 	     no sections, but ordinary, loadable segments should contain
   6874 	     something.  They are allowed by the ELF spec however, so only
   6875 	     a warning is produced.
   6876 	     There is however the valid use case of embedded systems which
   6877 	     have segments with p_filesz of 0 and a p_memsz > 0 to initialize
   6878 	     flash memory with zeros.  No warning is shown for that case.  */
   6879 	  if (segment->p_type == PT_LOAD
   6880 	      && (segment->p_filesz > 0 || segment->p_memsz == 0))
   6881 	    /* xgettext:c-format */
   6882 	    _bfd_error_handler
   6883 	      (_("%pB: warning: empty loadable segment detected"
   6884 		 " at vaddr=%#" PRIx64 ", is this intentional?"),
   6885 	       ibfd, (uint64_t) segment->p_vaddr);
   6886 
   6887 	  map->count = 0;
   6888 	  *pointer_to_map = map;
   6889 	  pointer_to_map = &map->next;
   6890 
   6891 	  continue;
   6892 	}
   6893 
   6894       /* Now scan the sections in the input BFD again and attempt
   6895 	 to add their corresponding output sections to the segment map.
   6896 	 The problem here is how to handle an output section which has
   6897 	 been moved (ie had its LMA changed).  There are four possibilities:
   6898 
   6899 	 1. None of the sections have been moved.
   6900 	    In this case we can continue to use the segment LMA from the
   6901 	    input BFD.
   6902 
   6903 	 2. All of the sections have been moved by the same amount.
   6904 	    In this case we can change the segment's LMA to match the LMA
   6905 	    of the first section.
   6906 
   6907 	 3. Some of the sections have been moved, others have not.
   6908 	    In this case those sections which have not been moved can be
   6909 	    placed in the current segment which will have to have its size,
   6910 	    and possibly its LMA changed, and a new segment or segments will
   6911 	    have to be created to contain the other sections.
   6912 
   6913 	 4. The sections have been moved, but not by the same amount.
   6914 	    In this case we can change the segment's LMA to match the LMA
   6915 	    of the first section and we will have to create a new segment
   6916 	    or segments to contain the other sections.
   6917 
   6918 	 In order to save time, we allocate an array to hold the section
   6919 	 pointers that we are interested in.  As these sections get assigned
   6920 	 to a segment, they are removed from this array.  */
   6921 
   6922       sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
   6923       if (sections == NULL)
   6924 	return FALSE;
   6925 
   6926       /* Step One: Scan for segment vs section LMA conflicts.
   6927 	 Also add the sections to the section array allocated above.
   6928 	 Also add the sections to the current segment.  In the common
   6929 	 case, where the sections have not been moved, this means that
   6930 	 we have completely filled the segment, and there is nothing
   6931 	 more to do.  */
   6932       isec = 0;
   6933       matching_lma = NULL;
   6934       suggested_lma = NULL;
   6935 
   6936       for (section = first_section, j = 0;
   6937 	   section != NULL;
   6938 	   section = section->next)
   6939 	{
   6940 	  if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
   6941 	    {
   6942 	      output_section = section->output_section;
   6943 
   6944 	      sections[j++] = section;
   6945 
   6946 	      /* The Solaris native linker always sets p_paddr to 0.
   6947 		 We try to catch that case here, and set it to the
   6948 		 correct value.  Note - some backends require that
   6949 		 p_paddr be left as zero.  */
   6950 	      if (!p_paddr_valid
   6951 		  && segment->p_vaddr != 0
   6952 		  && !bed->want_p_paddr_set_to_zero
   6953 		  && isec == 0
   6954 		  && output_section->lma != 0
   6955 		  && (align_power (segment->p_vaddr
   6956 				   + (map->includes_filehdr
   6957 				      ? iehdr->e_ehsize : 0)
   6958 				   + (map->includes_phdrs
   6959 				      ? iehdr->e_phnum * iehdr->e_phentsize
   6960 				      : 0),
   6961 				   output_section->alignment_power)
   6962 		      == output_section->vma))
   6963 		map->p_paddr = segment->p_vaddr;
   6964 
   6965 	      /* Match up the physical address of the segment with the
   6966 		 LMA address of the output section.  */
   6967 	      if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
   6968 		  || IS_COREFILE_NOTE (segment, section)
   6969 		  || (bed->want_p_paddr_set_to_zero
   6970 		      && IS_CONTAINED_BY_VMA (output_section, segment)))
   6971 		{
   6972 		  if (matching_lma == NULL
   6973 		      || output_section->lma < matching_lma->lma)
   6974 		    matching_lma = output_section;
   6975 
   6976 		  /* We assume that if the section fits within the segment
   6977 		     then it does not overlap any other section within that
   6978 		     segment.  */
   6979 		  map->sections[isec++] = output_section;
   6980 		}
   6981 	      else if (suggested_lma == NULL)
   6982 		suggested_lma = output_section;
   6983 
   6984 	      if (j == section_count)
   6985 		break;
   6986 	    }
   6987 	}
   6988 
   6989       BFD_ASSERT (j == section_count);
   6990 
   6991       /* Step Two: Adjust the physical address of the current segment,
   6992 	 if necessary.  */
   6993       if (isec == section_count)
   6994 	{
   6995 	  /* All of the sections fitted within the segment as currently
   6996 	     specified.  This is the default case.  Add the segment to
   6997 	     the list of built segments and carry on to process the next
   6998 	     program header in the input BFD.  */
   6999 	  map->count = section_count;
   7000 	  *pointer_to_map = map;
   7001 	  pointer_to_map = &map->next;
   7002 
   7003 	  if (p_paddr_valid
   7004 	      && !bed->want_p_paddr_set_to_zero
   7005 	      && matching_lma->lma != map->p_paddr
   7006 	      && !map->includes_filehdr
   7007 	      && !map->includes_phdrs)
   7008 	    /* There is some padding before the first section in the
   7009 	       segment.  So, we must account for that in the output
   7010 	       segment's vma.  */
   7011 	    map->p_vaddr_offset = matching_lma->lma - map->p_paddr;
   7012 
   7013 	  free (sections);
   7014 	  continue;
   7015 	}
   7016       else
   7017 	{
   7018 	  /* Change the current segment's physical address to match
   7019 	     the LMA of the first section that fitted, or if no
   7020 	     section fitted, the first section.  */
   7021 	  if (matching_lma == NULL)
   7022 	    matching_lma = suggested_lma;
   7023 
   7024 	  map->p_paddr = matching_lma->lma;
   7025 
   7026 	  /* Offset the segment physical address from the lma
   7027 	     to allow for space taken up by elf headers.  */
   7028 	  if (map->includes_phdrs)
   7029 	    {
   7030 	      map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
   7031 
   7032 	      /* iehdr->e_phnum is just an estimate of the number
   7033 		 of program headers that we will need.  Make a note
   7034 		 here of the number we used and the segment we chose
   7035 		 to hold these headers, so that we can adjust the
   7036 		 offset when we know the correct value.  */
   7037 	      phdr_adjust_num = iehdr->e_phnum;
   7038 	      phdr_adjust_seg = map;
   7039 	    }
   7040 
   7041 	  if (map->includes_filehdr)
   7042 	    {
   7043 	      bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
   7044 	      map->p_paddr -= iehdr->e_ehsize;
   7045 	      /* We've subtracted off the size of headers from the
   7046 		 first section lma, but there may have been some
   7047 		 alignment padding before that section too.  Try to
   7048 		 account for that by adjusting the segment lma down to
   7049 		 the same alignment.  */
   7050 	      if (segment->p_align != 0 && segment->p_align < align)
   7051 		align = segment->p_align;
   7052 	      map->p_paddr &= -align;
   7053 	    }
   7054 	}
   7055 
   7056       /* Step Three: Loop over the sections again, this time assigning
   7057 	 those that fit to the current segment and removing them from the
   7058 	 sections array; but making sure not to leave large gaps.  Once all
   7059 	 possible sections have been assigned to the current segment it is
   7060 	 added to the list of built segments and if sections still remain
   7061 	 to be assigned, a new segment is constructed before repeating
   7062 	 the loop.  */
   7063       isec = 0;
   7064       do
   7065 	{
   7066 	  map->count = 0;
   7067 	  suggested_lma = NULL;
   7068 
   7069 	  /* Fill the current segment with sections that fit.  */
   7070 	  for (j = 0; j < section_count; j++)
   7071 	    {
   7072 	      section = sections[j];
   7073 
   7074 	      if (section == NULL)
   7075 		continue;
   7076 
   7077 	      output_section = section->output_section;
   7078 
   7079 	      BFD_ASSERT (output_section != NULL);
   7080 
   7081 	      if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
   7082 		  || IS_COREFILE_NOTE (segment, section))
   7083 		{
   7084 		  if (map->count == 0)
   7085 		    {
   7086 		      /* If the first section in a segment does not start at
   7087 			 the beginning of the segment, then something is
   7088 			 wrong.  */
   7089 		      if (align_power (map->p_paddr
   7090 				       + (map->includes_filehdr
   7091 					  ? iehdr->e_ehsize : 0)
   7092 				       + (map->includes_phdrs
   7093 					  ? iehdr->e_phnum * iehdr->e_phentsize
   7094 					  : 0),
   7095 				       output_section->alignment_power)
   7096 			  != output_section->lma)
   7097 			abort ();
   7098 		    }
   7099 		  else
   7100 		    {
   7101 		      asection *prev_sec;
   7102 
   7103 		      prev_sec = map->sections[map->count - 1];
   7104 
   7105 		      /* If the gap between the end of the previous section
   7106 			 and the start of this section is more than
   7107 			 maxpagesize then we need to start a new segment.  */
   7108 		      if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
   7109 				      maxpagesize)
   7110 			   < BFD_ALIGN (output_section->lma, maxpagesize))
   7111 			  || (prev_sec->lma + prev_sec->size
   7112 			      > output_section->lma))
   7113 			{
   7114 			  if (suggested_lma == NULL)
   7115 			    suggested_lma = output_section;
   7116 
   7117 			  continue;
   7118 			}
   7119 		    }
   7120 
   7121 		  map->sections[map->count++] = output_section;
   7122 		  ++isec;
   7123 		  sections[j] = NULL;
   7124 		  if (segment->p_type == PT_LOAD)
   7125 		    section->segment_mark = TRUE;
   7126 		}
   7127 	      else if (suggested_lma == NULL)
   7128 		suggested_lma = output_section;
   7129 	    }
   7130 
   7131 	  BFD_ASSERT (map->count > 0);
   7132 
   7133 	  /* Add the current segment to the list of built segments.  */
   7134 	  *pointer_to_map = map;
   7135 	  pointer_to_map = &map->next;
   7136 
   7137 	  if (isec < section_count)
   7138 	    {
   7139 	      /* We still have not allocated all of the sections to
   7140 		 segments.  Create a new segment here, initialise it
   7141 		 and carry on looping.  */
   7142 	      amt = sizeof (struct elf_segment_map);
   7143 	      amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
   7144 	      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7145 	      if (map == NULL)
   7146 		{
   7147 		  free (sections);
   7148 		  return FALSE;
   7149 		}
   7150 
   7151 	      /* Initialise the fields of the segment map.  Set the physical
   7152 		 physical address to the LMA of the first section that has
   7153 		 not yet been assigned.  */
   7154 	      map->next = NULL;
   7155 	      map->p_type = segment->p_type;
   7156 	      map->p_flags = segment->p_flags;
   7157 	      map->p_flags_valid = 1;
   7158 	      map->p_paddr = suggested_lma->lma;
   7159 	      map->p_paddr_valid = p_paddr_valid;
   7160 	      map->includes_filehdr = 0;
   7161 	      map->includes_phdrs = 0;
   7162 	    }
   7163 	}
   7164       while (isec < section_count);
   7165 
   7166       free (sections);
   7167     }
   7168 
   7169   elf_seg_map (obfd) = map_first;
   7170 
   7171   /* If we had to estimate the number of program headers that were
   7172      going to be needed, then check our estimate now and adjust
   7173      the offset if necessary.  */
   7174   if (phdr_adjust_seg != NULL)
   7175     {
   7176       unsigned int count;
   7177 
   7178       for (count = 0, map = map_first; map != NULL; map = map->next)
   7179 	count++;
   7180 
   7181       if (count > phdr_adjust_num)
   7182 	phdr_adjust_seg->p_paddr
   7183 	  -= (count - phdr_adjust_num) * iehdr->e_phentsize;
   7184 
   7185       for (map = map_first; map != NULL; map = map->next)
   7186 	if (map->p_type == PT_PHDR)
   7187 	  {
   7188 	    bfd_vma adjust
   7189 	      = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
   7190 	    map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
   7191 	    break;
   7192 	  }
   7193     }
   7194 
   7195 #undef SEGMENT_END
   7196 #undef SECTION_SIZE
   7197 #undef IS_CONTAINED_BY_VMA
   7198 #undef IS_CONTAINED_BY_LMA
   7199 #undef IS_NOTE
   7200 #undef IS_COREFILE_NOTE
   7201 #undef IS_SOLARIS_PT_INTERP
   7202 #undef IS_SECTION_IN_INPUT_SEGMENT
   7203 #undef INCLUDE_SECTION_IN_SEGMENT
   7204 #undef SEGMENT_AFTER_SEGMENT
   7205 #undef SEGMENT_OVERLAPS
   7206   return TRUE;
   7207 }
   7208 
   7209 /* Copy ELF program header information.  */
   7210 
   7211 static bfd_boolean
   7212 copy_elf_program_header (bfd *ibfd, bfd *obfd)
   7213 {
   7214   Elf_Internal_Ehdr *iehdr;
   7215   struct elf_segment_map *map;
   7216   struct elf_segment_map *map_first;
   7217   struct elf_segment_map **pointer_to_map;
   7218   Elf_Internal_Phdr *segment;
   7219   unsigned int i;
   7220   unsigned int num_segments;
   7221   bfd_boolean phdr_included = FALSE;
   7222   bfd_boolean p_paddr_valid;
   7223 
   7224   iehdr = elf_elfheader (ibfd);
   7225 
   7226   map_first = NULL;
   7227   pointer_to_map = &map_first;
   7228 
   7229   /* If all the segment p_paddr fields are zero, don't set
   7230      map->p_paddr_valid.  */
   7231   p_paddr_valid = FALSE;
   7232   num_segments = elf_elfheader (ibfd)->e_phnum;
   7233   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7234        i < num_segments;
   7235        i++, segment++)
   7236     if (segment->p_paddr != 0)
   7237       {
   7238 	p_paddr_valid = TRUE;
   7239 	break;
   7240       }
   7241 
   7242   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7243        i < num_segments;
   7244        i++, segment++)
   7245     {
   7246       asection *section;
   7247       unsigned int section_count;
   7248       bfd_size_type amt;
   7249       Elf_Internal_Shdr *this_hdr;
   7250       asection *first_section = NULL;
   7251       asection *lowest_section;
   7252 
   7253       /* Compute how many sections are in this segment.  */
   7254       for (section = ibfd->sections, section_count = 0;
   7255 	   section != NULL;
   7256 	   section = section->next)
   7257 	{
   7258 	  this_hdr = &(elf_section_data(section)->this_hdr);
   7259 	  if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   7260 	    {
   7261 	      if (first_section == NULL)
   7262 		first_section = section;
   7263 	      section_count++;
   7264 	    }
   7265 	}
   7266 
   7267       /* Allocate a segment map big enough to contain
   7268 	 all of the sections we have selected.  */
   7269       amt = sizeof (struct elf_segment_map);
   7270       if (section_count != 0)
   7271 	amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
   7272       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7273       if (map == NULL)
   7274 	return FALSE;
   7275 
   7276       /* Initialize the fields of the output segment map with the
   7277 	 input segment.  */
   7278       map->next = NULL;
   7279       map->p_type = segment->p_type;
   7280       map->p_flags = segment->p_flags;
   7281       map->p_flags_valid = 1;
   7282       map->p_paddr = segment->p_paddr;
   7283       map->p_paddr_valid = p_paddr_valid;
   7284       map->p_align = segment->p_align;
   7285       map->p_align_valid = 1;
   7286       map->p_vaddr_offset = 0;
   7287 
   7288       if (map->p_type == PT_GNU_RELRO
   7289 	  || map->p_type == PT_GNU_STACK)
   7290 	{
   7291 	  /* The PT_GNU_RELRO segment may contain the first a few
   7292 	     bytes in the .got.plt section even if the whole .got.plt
   7293 	     section isn't in the PT_GNU_RELRO segment.  We won't
   7294 	     change the size of the PT_GNU_RELRO segment.
   7295 	     Similarly, PT_GNU_STACK size is significant on uclinux
   7296 	     systems.    */
   7297 	  map->p_size = segment->p_memsz;
   7298 	  map->p_size_valid = 1;
   7299 	}
   7300 
   7301       /* Determine if this segment contains the ELF file header
   7302 	 and if it contains the program headers themselves.  */
   7303       map->includes_filehdr = (segment->p_offset == 0
   7304 			       && segment->p_filesz >= iehdr->e_ehsize);
   7305 
   7306       map->includes_phdrs = 0;
   7307       if (! phdr_included || segment->p_type != PT_LOAD)
   7308 	{
   7309 	  map->includes_phdrs =
   7310 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   7311 	     && (segment->p_offset + segment->p_filesz
   7312 		 >= ((bfd_vma) iehdr->e_phoff
   7313 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   7314 
   7315 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   7316 	    phdr_included = TRUE;
   7317 	}
   7318 
   7319       lowest_section = NULL;
   7320       if (section_count != 0)
   7321 	{
   7322 	  unsigned int isec = 0;
   7323 
   7324 	  for (section = first_section;
   7325 	       section != NULL;
   7326 	       section = section->next)
   7327 	    {
   7328 	      this_hdr = &(elf_section_data(section)->this_hdr);
   7329 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   7330 		{
   7331 		  map->sections[isec++] = section->output_section;
   7332 		  if ((section->flags & SEC_ALLOC) != 0)
   7333 		    {
   7334 		      bfd_vma seg_off;
   7335 
   7336 		      if (lowest_section == NULL
   7337 			  || section->lma < lowest_section->lma)
   7338 			lowest_section = section;
   7339 
   7340 		      /* Section lmas are set up from PT_LOAD header
   7341 			 p_paddr in _bfd_elf_make_section_from_shdr.
   7342 			 If this header has a p_paddr that disagrees
   7343 			 with the section lma, flag the p_paddr as
   7344 			 invalid.  */
   7345 		      if ((section->flags & SEC_LOAD) != 0)
   7346 			seg_off = this_hdr->sh_offset - segment->p_offset;
   7347 		      else
   7348 			seg_off = this_hdr->sh_addr - segment->p_vaddr;
   7349 		      if (section->lma - segment->p_paddr != seg_off)
   7350 			map->p_paddr_valid = FALSE;
   7351 		    }
   7352 		  if (isec == section_count)
   7353 		    break;
   7354 		}
   7355 	    }
   7356 	}
   7357 
   7358       if (map->includes_filehdr && lowest_section != NULL)
   7359 	/* We need to keep the space used by the headers fixed.  */
   7360 	map->header_size = lowest_section->vma - segment->p_vaddr;
   7361 
   7362       if (!map->includes_phdrs
   7363 	  && !map->includes_filehdr
   7364 	  && map->p_paddr_valid)
   7365 	/* There is some other padding before the first section.  */
   7366 	map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
   7367 			       - segment->p_paddr);
   7368 
   7369       map->count = section_count;
   7370       *pointer_to_map = map;
   7371       pointer_to_map = &map->next;
   7372     }
   7373 
   7374   elf_seg_map (obfd) = map_first;
   7375   return TRUE;
   7376 }
   7377 
   7378 /* Copy private BFD data.  This copies or rewrites ELF program header
   7379    information.  */
   7380 
   7381 static bfd_boolean
   7382 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   7383 {
   7384   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   7385       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   7386     return TRUE;
   7387 
   7388   if (elf_tdata (ibfd)->phdr == NULL)
   7389     return TRUE;
   7390 
   7391   if (ibfd->xvec == obfd->xvec)
   7392     {
   7393       /* Check to see if any sections in the input BFD
   7394 	 covered by ELF program header have changed.  */
   7395       Elf_Internal_Phdr *segment;
   7396       asection *section, *osec;
   7397       unsigned int i, num_segments;
   7398       Elf_Internal_Shdr *this_hdr;
   7399       const struct elf_backend_data *bed;
   7400 
   7401       bed = get_elf_backend_data (ibfd);
   7402 
   7403       /* Regenerate the segment map if p_paddr is set to 0.  */
   7404       if (bed->want_p_paddr_set_to_zero)
   7405 	goto rewrite;
   7406 
   7407       /* Initialize the segment mark field.  */
   7408       for (section = obfd->sections; section != NULL;
   7409 	   section = section->next)
   7410 	section->segment_mark = FALSE;
   7411 
   7412       num_segments = elf_elfheader (ibfd)->e_phnum;
   7413       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7414 	   i < num_segments;
   7415 	   i++, segment++)
   7416 	{
   7417 	  /* PR binutils/3535.  The Solaris linker always sets the p_paddr
   7418 	     and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
   7419 	     which severly confuses things, so always regenerate the segment
   7420 	     map in this case.  */
   7421 	  if (segment->p_paddr == 0
   7422 	      && segment->p_memsz == 0
   7423 	      && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
   7424 	    goto rewrite;
   7425 
   7426 	  for (section = ibfd->sections;
   7427 	       section != NULL; section = section->next)
   7428 	    {
   7429 	      /* We mark the output section so that we know it comes
   7430 		 from the input BFD.  */
   7431 	      osec = section->output_section;
   7432 	      if (osec)
   7433 		osec->segment_mark = TRUE;
   7434 
   7435 	      /* Check if this section is covered by the segment.  */
   7436 	      this_hdr = &(elf_section_data(section)->this_hdr);
   7437 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   7438 		{
   7439 		  /* FIXME: Check if its output section is changed or
   7440 		     removed.  What else do we need to check?  */
   7441 		  if (osec == NULL
   7442 		      || section->flags != osec->flags
   7443 		      || section->lma != osec->lma
   7444 		      || section->vma != osec->vma
   7445 		      || section->size != osec->size
   7446 		      || section->rawsize != osec->rawsize
   7447 		      || section->alignment_power != osec->alignment_power)
   7448 		    goto rewrite;
   7449 		}
   7450 	    }
   7451 	}
   7452 
   7453       /* Check to see if any output section do not come from the
   7454 	 input BFD.  */
   7455       for (section = obfd->sections; section != NULL;
   7456 	   section = section->next)
   7457 	{
   7458 	  if (!section->segment_mark)
   7459 	    goto rewrite;
   7460 	  else
   7461 	    section->segment_mark = FALSE;
   7462 	}
   7463 
   7464       return copy_elf_program_header (ibfd, obfd);
   7465     }
   7466 
   7467 rewrite:
   7468   if (ibfd->xvec == obfd->xvec)
   7469     {
   7470       /* When rewriting program header, set the output maxpagesize to
   7471 	 the maximum alignment of input PT_LOAD segments.  */
   7472       Elf_Internal_Phdr *segment;
   7473       unsigned int i;
   7474       unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
   7475       bfd_vma maxpagesize = 0;
   7476 
   7477       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7478 	   i < num_segments;
   7479 	   i++, segment++)
   7480 	if (segment->p_type == PT_LOAD
   7481 	    && maxpagesize < segment->p_align)
   7482 	  {
   7483 	    /* PR 17512: file: f17299af.  */
   7484 	    if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
   7485 	      /* xgettext:c-format */
   7486 	      _bfd_error_handler (_("%pB: warning: segment alignment of %#"
   7487 				    PRIx64 " is too large"),
   7488 				  ibfd, (uint64_t) segment->p_align);
   7489 	    else
   7490 	      maxpagesize = segment->p_align;
   7491 	  }
   7492 
   7493       if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
   7494 	bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
   7495     }
   7496 
   7497   return rewrite_elf_program_header (ibfd, obfd);
   7498 }
   7499 
   7500 /* Initialize private output section information from input section.  */
   7501 
   7502 bfd_boolean
   7503 _bfd_elf_init_private_section_data (bfd *ibfd,
   7504 				    asection *isec,
   7505 				    bfd *obfd,
   7506 				    asection *osec,
   7507 				    struct bfd_link_info *link_info)
   7508 
   7509 {
   7510   Elf_Internal_Shdr *ihdr, *ohdr;
   7511   bfd_boolean final_link = (link_info != NULL
   7512 			    && !bfd_link_relocatable (link_info));
   7513 
   7514   if (ibfd->xvec->flavour != bfd_target_elf_flavour
   7515       || obfd->xvec->flavour != bfd_target_elf_flavour)
   7516     return TRUE;
   7517 
   7518   BFD_ASSERT (elf_section_data (osec) != NULL);
   7519 
   7520   /* For objcopy and relocatable link, don't copy the output ELF
   7521      section type from input if the output BFD section flags have been
   7522      set to something different.  For a final link allow some flags
   7523      that the linker clears to differ.  */
   7524   if (elf_section_type (osec) == SHT_NULL
   7525       && (osec->flags == isec->flags
   7526 	  || (final_link
   7527 	      && ((osec->flags ^ isec->flags)
   7528 		  & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
   7529     elf_section_type (osec) = elf_section_type (isec);
   7530 
   7531   /* FIXME: Is this correct for all OS/PROC specific flags?  */
   7532   elf_section_flags (osec) |= (elf_section_flags (isec)
   7533 			       & (SHF_MASKOS | SHF_MASKPROC));
   7534 
   7535   /* Copy sh_info from input for mbind section.  */
   7536   if (elf_section_flags (isec) & SHF_GNU_MBIND)
   7537     elf_section_data (osec)->this_hdr.sh_info
   7538       = elf_section_data (isec)->this_hdr.sh_info;
   7539 
   7540   /* Set things up for objcopy and relocatable link.  The output
   7541      SHT_GROUP section will have its elf_next_in_group pointing back
   7542      to the input group members.  Ignore linker created group section.
   7543      See elfNN_ia64_object_p in elfxx-ia64.c.  */
   7544   if ((link_info == NULL
   7545        || !link_info->resolve_section_groups)
   7546       && (elf_sec_group (isec) == NULL
   7547 	  || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
   7548     {
   7549       if (elf_section_flags (isec) & SHF_GROUP)
   7550 	elf_section_flags (osec) |= SHF_GROUP;
   7551       elf_next_in_group (osec) = elf_next_in_group (isec);
   7552       elf_section_data (osec)->group = elf_section_data (isec)->group;
   7553     }
   7554 
   7555   /* If not decompress, preserve SHF_COMPRESSED.  */
   7556   if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
   7557     elf_section_flags (osec) |= (elf_section_flags (isec)
   7558 				 & SHF_COMPRESSED);
   7559 
   7560   ihdr = &elf_section_data (isec)->this_hdr;
   7561 
   7562   /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
   7563      don't use the output section of the linked-to section since it
   7564      may be NULL at this point.  */
   7565   if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
   7566     {
   7567       ohdr = &elf_section_data (osec)->this_hdr;
   7568       ohdr->sh_flags |= SHF_LINK_ORDER;
   7569       elf_linked_to_section (osec) = elf_linked_to_section (isec);
   7570     }
   7571 
   7572   osec->use_rela_p = isec->use_rela_p;
   7573 
   7574   return TRUE;
   7575 }
   7576 
   7577 /* Copy private section information.  This copies over the entsize
   7578    field, and sometimes the info field.  */
   7579 
   7580 bfd_boolean
   7581 _bfd_elf_copy_private_section_data (bfd *ibfd,
   7582 				    asection *isec,
   7583 				    bfd *obfd,
   7584 				    asection *osec)
   7585 {
   7586   Elf_Internal_Shdr *ihdr, *ohdr;
   7587 
   7588   if (ibfd->xvec->flavour != bfd_target_elf_flavour
   7589       || obfd->xvec->flavour != bfd_target_elf_flavour)
   7590     return TRUE;
   7591 
   7592   ihdr = &elf_section_data (isec)->this_hdr;
   7593   ohdr = &elf_section_data (osec)->this_hdr;
   7594 
   7595   ohdr->sh_entsize = ihdr->sh_entsize;
   7596 
   7597   if (ihdr->sh_type == SHT_SYMTAB
   7598       || ihdr->sh_type == SHT_DYNSYM
   7599       || ihdr->sh_type == SHT_GNU_verneed
   7600       || ihdr->sh_type == SHT_GNU_verdef)
   7601     ohdr->sh_info = ihdr->sh_info;
   7602 
   7603   return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
   7604 					     NULL);
   7605 }
   7606 
   7607 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
   7608    necessary if we are removing either the SHT_GROUP section or any of
   7609    the group member sections.  DISCARDED is the value that a section's
   7610    output_section has if the section will be discarded, NULL when this
   7611    function is called from objcopy, bfd_abs_section_ptr when called
   7612    from the linker.  */
   7613 
   7614 bfd_boolean
   7615 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
   7616 {
   7617   asection *isec;
   7618 
   7619   for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   7620     if (elf_section_type (isec) == SHT_GROUP)
   7621       {
   7622 	asection *first = elf_next_in_group (isec);
   7623 	asection *s = first;
   7624 	bfd_size_type removed = 0;
   7625 
   7626 	while (s != NULL)
   7627 	  {
   7628 	    /* If this member section is being output but the
   7629 	       SHT_GROUP section is not, then clear the group info
   7630 	       set up by _bfd_elf_copy_private_section_data.  */
   7631 	    if (s->output_section != discarded
   7632 		&& isec->output_section == discarded)
   7633 	      {
   7634 		elf_section_flags (s->output_section) &= ~SHF_GROUP;
   7635 		elf_group_name (s->output_section) = NULL;
   7636 	      }
   7637 	    /* Conversely, if the member section is not being output
   7638 	       but the SHT_GROUP section is, then adjust its size.  */
   7639 	    else if (s->output_section == discarded
   7640 		     && isec->output_section != discarded)
   7641 	      {
   7642 		struct bfd_elf_section_data *elf_sec = elf_section_data (s);
   7643 		removed += 4;
   7644 		if (elf_sec->rel.hdr != NULL
   7645 		    && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
   7646 		  removed += 4;
   7647 		if (elf_sec->rela.hdr != NULL
   7648 		    && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
   7649 		  removed += 4;
   7650 	      }
   7651 	    s = elf_next_in_group (s);
   7652 	    if (s == first)
   7653 	      break;
   7654 	  }
   7655 	if (removed != 0)
   7656 	  {
   7657 	    if (discarded != NULL)
   7658 	      {
   7659 		/* If we've been called for ld -r, then we need to
   7660 		   adjust the input section size.  */
   7661 		if (isec->rawsize == 0)
   7662 		  isec->rawsize = isec->size;
   7663 		isec->size = isec->rawsize - removed;
   7664 		if (isec->size <= 4)
   7665 		  {
   7666 		    isec->size = 0;
   7667 		    isec->flags |= SEC_EXCLUDE;
   7668 		  }
   7669 	      }
   7670 	    else
   7671 	      {
   7672 		/* Adjust the output section size when called from
   7673 		   objcopy. */
   7674 		isec->output_section->size -= removed;
   7675 		if (isec->output_section->size <= 4)
   7676 		  {
   7677 		    isec->output_section->size = 0;
   7678 		    isec->output_section->flags |= SEC_EXCLUDE;
   7679 		  }
   7680 	      }
   7681 	  }
   7682       }
   7683 
   7684   return TRUE;
   7685 }
   7686 
   7687 /* Copy private header information.  */
   7688 
   7689 bfd_boolean
   7690 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
   7691 {
   7692   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   7693       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   7694     return TRUE;
   7695 
   7696   /* Copy over private BFD data if it has not already been copied.
   7697      This must be done here, rather than in the copy_private_bfd_data
   7698      entry point, because the latter is called after the section
   7699      contents have been set, which means that the program headers have
   7700      already been worked out.  */
   7701   if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
   7702     {
   7703       if (! copy_private_bfd_data (ibfd, obfd))
   7704 	return FALSE;
   7705     }
   7706 
   7707   return _bfd_elf_fixup_group_sections (ibfd, NULL);
   7708 }
   7709 
   7710 /* Copy private symbol information.  If this symbol is in a section
   7711    which we did not map into a BFD section, try to map the section
   7712    index correctly.  We use special macro definitions for the mapped
   7713    section indices; these definitions are interpreted by the
   7714    swap_out_syms function.  */
   7715 
   7716 #define MAP_ONESYMTAB (SHN_HIOS + 1)
   7717 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
   7718 #define MAP_STRTAB    (SHN_HIOS + 3)
   7719 #define MAP_SHSTRTAB  (SHN_HIOS + 4)
   7720 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
   7721 
   7722 bfd_boolean
   7723 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
   7724 				   asymbol *isymarg,
   7725 				   bfd *obfd,
   7726 				   asymbol *osymarg)
   7727 {
   7728   elf_symbol_type *isym, *osym;
   7729 
   7730   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   7731       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   7732     return TRUE;
   7733 
   7734   isym = elf_symbol_from (ibfd, isymarg);
   7735   osym = elf_symbol_from (obfd, osymarg);
   7736 
   7737   if (isym != NULL
   7738       && isym->internal_elf_sym.st_shndx != 0
   7739       && osym != NULL
   7740       && bfd_is_abs_section (isym->symbol.section))
   7741     {
   7742       unsigned int shndx;
   7743 
   7744       shndx = isym->internal_elf_sym.st_shndx;
   7745       if (shndx == elf_onesymtab (ibfd))
   7746 	shndx = MAP_ONESYMTAB;
   7747       else if (shndx == elf_dynsymtab (ibfd))
   7748 	shndx = MAP_DYNSYMTAB;
   7749       else if (shndx == elf_strtab_sec (ibfd))
   7750 	shndx = MAP_STRTAB;
   7751       else if (shndx == elf_shstrtab_sec (ibfd))
   7752 	shndx = MAP_SHSTRTAB;
   7753       else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
   7754 	shndx = MAP_SYM_SHNDX;
   7755       osym->internal_elf_sym.st_shndx = shndx;
   7756     }
   7757 
   7758   return TRUE;
   7759 }
   7760 
   7761 /* Swap out the symbols.  */
   7762 
   7763 static bfd_boolean
   7764 swap_out_syms (bfd *abfd,
   7765 	       struct elf_strtab_hash **sttp,
   7766 	       int relocatable_p)
   7767 {
   7768   const struct elf_backend_data *bed;
   7769   int symcount;
   7770   asymbol **syms;
   7771   struct elf_strtab_hash *stt;
   7772   Elf_Internal_Shdr *symtab_hdr;
   7773   Elf_Internal_Shdr *symtab_shndx_hdr;
   7774   Elf_Internal_Shdr *symstrtab_hdr;
   7775   struct elf_sym_strtab *symstrtab;
   7776   bfd_byte *outbound_syms;
   7777   bfd_byte *outbound_shndx;
   7778   unsigned long outbound_syms_index;
   7779   unsigned long outbound_shndx_index;
   7780   int idx;
   7781   unsigned int num_locals;
   7782   bfd_size_type amt;
   7783   bfd_boolean name_local_sections;
   7784 
   7785   if (!elf_map_symbols (abfd, &num_locals))
   7786     return FALSE;
   7787 
   7788   /* Dump out the symtabs.  */
   7789   stt = _bfd_elf_strtab_init ();
   7790   if (stt == NULL)
   7791     return FALSE;
   7792 
   7793   bed = get_elf_backend_data (abfd);
   7794   symcount = bfd_get_symcount (abfd);
   7795   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   7796   symtab_hdr->sh_type = SHT_SYMTAB;
   7797   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
   7798   symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
   7799   symtab_hdr->sh_info = num_locals + 1;
   7800   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   7801 
   7802   symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
   7803   symstrtab_hdr->sh_type = SHT_STRTAB;
   7804 
   7805   /* Allocate buffer to swap out the .strtab section.  */
   7806   symstrtab = (struct elf_sym_strtab *) bfd_malloc ((symcount + 1)
   7807 						    * sizeof (*symstrtab));
   7808   if (symstrtab == NULL)
   7809     {
   7810       _bfd_elf_strtab_free (stt);
   7811       return FALSE;
   7812     }
   7813 
   7814   outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
   7815 					   bed->s->sizeof_sym);
   7816   if (outbound_syms == NULL)
   7817     {
   7818 error_return:
   7819       _bfd_elf_strtab_free (stt);
   7820       free (symstrtab);
   7821       return FALSE;
   7822     }
   7823   symtab_hdr->contents = outbound_syms;
   7824   outbound_syms_index = 0;
   7825 
   7826   outbound_shndx = NULL;
   7827   outbound_shndx_index = 0;
   7828 
   7829   if (elf_symtab_shndx_list (abfd))
   7830     {
   7831       symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
   7832       if (symtab_shndx_hdr->sh_name != 0)
   7833 	{
   7834 	  amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
   7835 	  outbound_shndx =  (bfd_byte *)
   7836 	    bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
   7837 	  if (outbound_shndx == NULL)
   7838 	    goto error_return;
   7839 
   7840 	  symtab_shndx_hdr->contents = outbound_shndx;
   7841 	  symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
   7842 	  symtab_shndx_hdr->sh_size = amt;
   7843 	  symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
   7844 	  symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
   7845 	}
   7846       /* FIXME: What about any other headers in the list ?  */
   7847     }
   7848 
   7849   /* Now generate the data (for "contents").  */
   7850   {
   7851     /* Fill in zeroth symbol and swap it out.  */
   7852     Elf_Internal_Sym sym;
   7853     sym.st_name = 0;
   7854     sym.st_value = 0;
   7855     sym.st_size = 0;
   7856     sym.st_info = 0;
   7857     sym.st_other = 0;
   7858     sym.st_shndx = SHN_UNDEF;
   7859     sym.st_target_internal = 0;
   7860     symstrtab[0].sym = sym;
   7861     symstrtab[0].dest_index = outbound_syms_index;
   7862     symstrtab[0].destshndx_index = outbound_shndx_index;
   7863     outbound_syms_index++;
   7864     if (outbound_shndx != NULL)
   7865       outbound_shndx_index++;
   7866   }
   7867 
   7868   name_local_sections
   7869     = (bed->elf_backend_name_local_section_symbols
   7870        && bed->elf_backend_name_local_section_symbols (abfd));
   7871 
   7872   syms = bfd_get_outsymbols (abfd);
   7873   for (idx = 0; idx < symcount;)
   7874     {
   7875       Elf_Internal_Sym sym;
   7876       bfd_vma value = syms[idx]->value;
   7877       elf_symbol_type *type_ptr;
   7878       flagword flags = syms[idx]->flags;
   7879       int type;
   7880 
   7881       if (!name_local_sections
   7882 	  && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
   7883 	{
   7884 	  /* Local section symbols have no name.  */
   7885 	  sym.st_name = (unsigned long) -1;
   7886 	}
   7887       else
   7888 	{
   7889 	  /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
   7890 	     to get the final offset for st_name.  */
   7891 	  sym.st_name
   7892 	    = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
   7893 						   FALSE);
   7894 	  if (sym.st_name == (unsigned long) -1)
   7895 	    goto error_return;
   7896 	}
   7897 
   7898       type_ptr = elf_symbol_from (abfd, syms[idx]);
   7899 
   7900       if ((flags & BSF_SECTION_SYM) == 0
   7901 	  && bfd_is_com_section (syms[idx]->section))
   7902 	{
   7903 	  /* ELF common symbols put the alignment into the `value' field,
   7904 	     and the size into the `size' field.  This is backwards from
   7905 	     how BFD handles it, so reverse it here.  */
   7906 	  sym.st_size = value;
   7907 	  if (type_ptr == NULL
   7908 	      || type_ptr->internal_elf_sym.st_value == 0)
   7909 	    sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
   7910 	  else
   7911 	    sym.st_value = type_ptr->internal_elf_sym.st_value;
   7912 	  sym.st_shndx = _bfd_elf_section_from_bfd_section
   7913 	    (abfd, syms[idx]->section);
   7914 	}
   7915       else
   7916 	{
   7917 	  asection *sec = syms[idx]->section;
   7918 	  unsigned int shndx;
   7919 
   7920 	  if (sec->output_section)
   7921 	    {
   7922 	      value += sec->output_offset;
   7923 	      sec = sec->output_section;
   7924 	    }
   7925 
   7926 	  /* Don't add in the section vma for relocatable output.  */
   7927 	  if (! relocatable_p)
   7928 	    value += sec->vma;
   7929 	  sym.st_value = value;
   7930 	  sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
   7931 
   7932 	  if (bfd_is_abs_section (sec)
   7933 	      && type_ptr != NULL
   7934 	      && type_ptr->internal_elf_sym.st_shndx != 0)
   7935 	    {
   7936 	      /* This symbol is in a real ELF section which we did
   7937 		 not create as a BFD section.  Undo the mapping done
   7938 		 by copy_private_symbol_data.  */
   7939 	      shndx = type_ptr->internal_elf_sym.st_shndx;
   7940 	      switch (shndx)
   7941 		{
   7942 		case MAP_ONESYMTAB:
   7943 		  shndx = elf_onesymtab (abfd);
   7944 		  break;
   7945 		case MAP_DYNSYMTAB:
   7946 		  shndx = elf_dynsymtab (abfd);
   7947 		  break;
   7948 		case MAP_STRTAB:
   7949 		  shndx = elf_strtab_sec (abfd);
   7950 		  break;
   7951 		case MAP_SHSTRTAB:
   7952 		  shndx = elf_shstrtab_sec (abfd);
   7953 		  break;
   7954 		case MAP_SYM_SHNDX:
   7955 		  if (elf_symtab_shndx_list (abfd))
   7956 		    shndx = elf_symtab_shndx_list (abfd)->ndx;
   7957 		  break;
   7958 		default:
   7959 		  shndx = SHN_ABS;
   7960 		  break;
   7961 		}
   7962 	    }
   7963 	  else
   7964 	    {
   7965 	      shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   7966 
   7967 	      if (shndx == SHN_BAD)
   7968 		{
   7969 		  asection *sec2;
   7970 
   7971 		  /* Writing this would be a hell of a lot easier if
   7972 		     we had some decent documentation on bfd, and
   7973 		     knew what to expect of the library, and what to
   7974 		     demand of applications.  For example, it
   7975 		     appears that `objcopy' might not set the
   7976 		     section of a symbol to be a section that is
   7977 		     actually in the output file.  */
   7978 		  sec2 = bfd_get_section_by_name (abfd, sec->name);
   7979 		  if (sec2 != NULL)
   7980 		    shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
   7981 		  if (shndx == SHN_BAD)
   7982 		    {
   7983 		      /* xgettext:c-format */
   7984 		      _bfd_error_handler
   7985 			(_("unable to find equivalent output section"
   7986 			   " for symbol '%s' from section '%s'"),
   7987 			 syms[idx]->name ? syms[idx]->name : "<Local sym>",
   7988 			 sec->name);
   7989 		      bfd_set_error (bfd_error_invalid_operation);
   7990 		      goto error_return;
   7991 		    }
   7992 		}
   7993 	    }
   7994 
   7995 	  sym.st_shndx = shndx;
   7996 	}
   7997 
   7998       if ((flags & BSF_THREAD_LOCAL) != 0)
   7999 	type = STT_TLS;
   8000       else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
   8001 	type = STT_GNU_IFUNC;
   8002       else if ((flags & BSF_FUNCTION) != 0)
   8003 	type = STT_FUNC;
   8004       else if ((flags & BSF_OBJECT) != 0)
   8005 	type = STT_OBJECT;
   8006       else if ((flags & BSF_RELC) != 0)
   8007 	type = STT_RELC;
   8008       else if ((flags & BSF_SRELC) != 0)
   8009 	type = STT_SRELC;
   8010       else
   8011 	type = STT_NOTYPE;
   8012 
   8013       if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
   8014 	type = STT_TLS;
   8015 
   8016       /* Processor-specific types.  */
   8017       if (type_ptr != NULL
   8018 	  && bed->elf_backend_get_symbol_type)
   8019 	type = ((*bed->elf_backend_get_symbol_type)
   8020 		(&type_ptr->internal_elf_sym, type));
   8021 
   8022       if (flags & BSF_SECTION_SYM)
   8023 	{
   8024 	  if (flags & BSF_GLOBAL)
   8025 	    sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   8026 	  else
   8027 	    sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   8028 	}
   8029       else if (bfd_is_com_section (syms[idx]->section))
   8030 	{
   8031 	  if (type != STT_TLS)
   8032 	    {
   8033 	      if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
   8034 		type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
   8035 			? STT_COMMON : STT_OBJECT);
   8036 	      else
   8037 		type = ((flags & BSF_ELF_COMMON) != 0
   8038 			? STT_COMMON : STT_OBJECT);
   8039 	    }
   8040 	  sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
   8041 	}
   8042       else if (bfd_is_und_section (syms[idx]->section))
   8043 	sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
   8044 				    ? STB_WEAK
   8045 				    : STB_GLOBAL),
   8046 				   type);
   8047       else if (flags & BSF_FILE)
   8048 	sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   8049       else
   8050 	{
   8051 	  int bind = STB_LOCAL;
   8052 
   8053 	  if (flags & BSF_LOCAL)
   8054 	    bind = STB_LOCAL;
   8055 	  else if (flags & BSF_GNU_UNIQUE)
   8056 	    bind = STB_GNU_UNIQUE;
   8057 	  else if (flags & BSF_WEAK)
   8058 	    bind = STB_WEAK;
   8059 	  else if (flags & BSF_GLOBAL)
   8060 	    bind = STB_GLOBAL;
   8061 
   8062 	  sym.st_info = ELF_ST_INFO (bind, type);
   8063 	}
   8064 
   8065       if (type_ptr != NULL)
   8066 	{
   8067 	  sym.st_other = type_ptr->internal_elf_sym.st_other;
   8068 	  sym.st_target_internal
   8069 	    = type_ptr->internal_elf_sym.st_target_internal;
   8070 	}
   8071       else
   8072 	{
   8073 	  sym.st_other = 0;
   8074 	  sym.st_target_internal = 0;
   8075 	}
   8076 
   8077       idx++;
   8078       symstrtab[idx].sym = sym;
   8079       symstrtab[idx].dest_index = outbound_syms_index;
   8080       symstrtab[idx].destshndx_index = outbound_shndx_index;
   8081 
   8082       outbound_syms_index++;
   8083       if (outbound_shndx != NULL)
   8084 	outbound_shndx_index++;
   8085     }
   8086 
   8087   /* Finalize the .strtab section.  */
   8088   _bfd_elf_strtab_finalize (stt);
   8089 
   8090   /* Swap out the .strtab section.  */
   8091   for (idx = 0; idx <= symcount; idx++)
   8092     {
   8093       struct elf_sym_strtab *elfsym = &symstrtab[idx];
   8094       if (elfsym->sym.st_name == (unsigned long) -1)
   8095 	elfsym->sym.st_name = 0;
   8096       else
   8097 	elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
   8098 						      elfsym->sym.st_name);
   8099       bed->s->swap_symbol_out (abfd, &elfsym->sym,
   8100 			       (outbound_syms
   8101 				+ (elfsym->dest_index
   8102 				   * bed->s->sizeof_sym)),
   8103 			       (outbound_shndx
   8104 				+ (elfsym->destshndx_index
   8105 				   * sizeof (Elf_External_Sym_Shndx))));
   8106     }
   8107   free (symstrtab);
   8108 
   8109   *sttp = stt;
   8110   symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
   8111   symstrtab_hdr->sh_type = SHT_STRTAB;
   8112   symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   8113   symstrtab_hdr->sh_addr = 0;
   8114   symstrtab_hdr->sh_entsize = 0;
   8115   symstrtab_hdr->sh_link = 0;
   8116   symstrtab_hdr->sh_info = 0;
   8117   symstrtab_hdr->sh_addralign = 1;
   8118 
   8119   return TRUE;
   8120 }
   8121 
   8122 /* Return the number of bytes required to hold the symtab vector.
   8123 
   8124    Note that we base it on the count plus 1, since we will null terminate
   8125    the vector allocated based on this size.  However, the ELF symbol table
   8126    always has a dummy entry as symbol #0, so it ends up even.  */
   8127 
   8128 long
   8129 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
   8130 {
   8131   long symcount;
   8132   long symtab_size;
   8133   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
   8134 
   8135   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   8136   symtab_size = (symcount + 1) * (sizeof (asymbol *));
   8137   if (symcount > 0)
   8138     symtab_size -= sizeof (asymbol *);
   8139 
   8140   return symtab_size;
   8141 }
   8142 
   8143 long
   8144 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
   8145 {
   8146   long symcount;
   8147   long symtab_size;
   8148   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   8149 
   8150   if (elf_dynsymtab (abfd) == 0)
   8151     {
   8152       bfd_set_error (bfd_error_invalid_operation);
   8153       return -1;
   8154     }
   8155 
   8156   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   8157   symtab_size = (symcount + 1) * (sizeof (asymbol *));
   8158   if (symcount > 0)
   8159     symtab_size -= sizeof (asymbol *);
   8160 
   8161   return symtab_size;
   8162 }
   8163 
   8164 long
   8165 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
   8166 				sec_ptr asect)
   8167 {
   8168   return (asect->reloc_count + 1) * sizeof (arelent *);
   8169 }
   8170 
   8171 /* Canonicalize the relocs.  */
   8172 
   8173 long
   8174 _bfd_elf_canonicalize_reloc (bfd *abfd,
   8175 			     sec_ptr section,
   8176 			     arelent **relptr,
   8177 			     asymbol **symbols)
   8178 {
   8179   arelent *tblptr;
   8180   unsigned int i;
   8181   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8182 
   8183   if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
   8184     return -1;
   8185 
   8186   tblptr = section->relocation;
   8187   for (i = 0; i < section->reloc_count; i++)
   8188     *relptr++ = tblptr++;
   8189 
   8190   *relptr = NULL;
   8191 
   8192   return section->reloc_count;
   8193 }
   8194 
   8195 long
   8196 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
   8197 {
   8198   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8199   long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
   8200 
   8201   if (symcount >= 0)
   8202     bfd_get_symcount (abfd) = symcount;
   8203   return symcount;
   8204 }
   8205 
   8206 long
   8207 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
   8208 				      asymbol **allocation)
   8209 {
   8210   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8211   long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
   8212 
   8213   if (symcount >= 0)
   8214     bfd_get_dynamic_symcount (abfd) = symcount;
   8215   return symcount;
   8216 }
   8217 
   8218 /* Return the size required for the dynamic reloc entries.  Any loadable
   8219    section that was actually installed in the BFD, and has type SHT_REL
   8220    or SHT_RELA, and uses the dynamic symbol table, is considered to be a
   8221    dynamic reloc section.  */
   8222 
   8223 long
   8224 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
   8225 {
   8226   long ret;
   8227   asection *s;
   8228 
   8229   if (elf_dynsymtab (abfd) == 0)
   8230     {
   8231       bfd_set_error (bfd_error_invalid_operation);
   8232       return -1;
   8233     }
   8234 
   8235   ret = sizeof (arelent *);
   8236   for (s = abfd->sections; s != NULL; s = s->next)
   8237     if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   8238 	&& (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   8239 	    || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
   8240       ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
   8241 	      * sizeof (arelent *));
   8242 
   8243   return ret;
   8244 }
   8245 
   8246 /* Canonicalize the dynamic relocation entries.  Note that we return the
   8247    dynamic relocations as a single block, although they are actually
   8248    associated with particular sections; the interface, which was
   8249    designed for SunOS style shared libraries, expects that there is only
   8250    one set of dynamic relocs.  Any loadable section that was actually
   8251    installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
   8252    dynamic symbol table, is considered to be a dynamic reloc section.  */
   8253 
   8254 long
   8255 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
   8256 				     arelent **storage,
   8257 				     asymbol **syms)
   8258 {
   8259   bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
   8260   asection *s;
   8261   long ret;
   8262 
   8263   if (elf_dynsymtab (abfd) == 0)
   8264     {
   8265       bfd_set_error (bfd_error_invalid_operation);
   8266       return -1;
   8267     }
   8268 
   8269   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   8270   ret = 0;
   8271   for (s = abfd->sections; s != NULL; s = s->next)
   8272     {
   8273       if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   8274 	  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   8275 	      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
   8276 	{
   8277 	  arelent *p;
   8278 	  long count, i;
   8279 
   8280 	  if (! (*slurp_relocs) (abfd, s, syms, TRUE))
   8281 	    return -1;
   8282 	  count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
   8283 	  p = s->relocation;
   8284 	  for (i = 0; i < count; i++)
   8285 	    *storage++ = p++;
   8286 	  ret += count;
   8287 	}
   8288     }
   8289 
   8290   *storage = NULL;
   8291 
   8292   return ret;
   8293 }
   8294 
   8295 /* Read in the version information.  */
   8297 
   8298 bfd_boolean
   8299 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
   8300 {
   8301   bfd_byte *contents = NULL;
   8302   unsigned int freeidx = 0;
   8303 
   8304   if (elf_dynverref (abfd) != 0)
   8305     {
   8306       Elf_Internal_Shdr *hdr;
   8307       Elf_External_Verneed *everneed;
   8308       Elf_Internal_Verneed *iverneed;
   8309       unsigned int i;
   8310       bfd_byte *contents_end;
   8311 
   8312       hdr = &elf_tdata (abfd)->dynverref_hdr;
   8313 
   8314       if (hdr->sh_info == 0
   8315 	  || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
   8316 	{
   8317 error_return_bad_verref:
   8318 	  _bfd_error_handler
   8319 	    (_("%pB: .gnu.version_r invalid entry"), abfd);
   8320 	  bfd_set_error (bfd_error_bad_value);
   8321 error_return_verref:
   8322 	  elf_tdata (abfd)->verref = NULL;
   8323 	  elf_tdata (abfd)->cverrefs = 0;
   8324 	  goto error_return;
   8325 	}
   8326 
   8327       contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
   8328       if (contents == NULL)
   8329 	goto error_return_verref;
   8330 
   8331       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   8332 	  || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
   8333 	goto error_return_verref;
   8334 
   8335       elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
   8336 	bfd_alloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
   8337 
   8338       if (elf_tdata (abfd)->verref == NULL)
   8339 	goto error_return_verref;
   8340 
   8341       BFD_ASSERT (sizeof (Elf_External_Verneed)
   8342 		  == sizeof (Elf_External_Vernaux));
   8343       contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
   8344       everneed = (Elf_External_Verneed *) contents;
   8345       iverneed = elf_tdata (abfd)->verref;
   8346       for (i = 0; i < hdr->sh_info; i++, iverneed++)
   8347 	{
   8348 	  Elf_External_Vernaux *evernaux;
   8349 	  Elf_Internal_Vernaux *ivernaux;
   8350 	  unsigned int j;
   8351 
   8352 	  _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
   8353 
   8354 	  iverneed->vn_bfd = abfd;
   8355 
   8356 	  iverneed->vn_filename =
   8357 	    bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   8358 					     iverneed->vn_file);
   8359 	  if (iverneed->vn_filename == NULL)
   8360 	    goto error_return_bad_verref;
   8361 
   8362 	  if (iverneed->vn_cnt == 0)
   8363 	    iverneed->vn_auxptr = NULL;
   8364 	  else
   8365 	    {
   8366 	      iverneed->vn_auxptr = (struct elf_internal_vernaux *)
   8367 		  bfd_alloc2 (abfd, iverneed->vn_cnt,
   8368 			      sizeof (Elf_Internal_Vernaux));
   8369 	      if (iverneed->vn_auxptr == NULL)
   8370 		goto error_return_verref;
   8371 	    }
   8372 
   8373 	  if (iverneed->vn_aux
   8374 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   8375 	    goto error_return_bad_verref;
   8376 
   8377 	  evernaux = ((Elf_External_Vernaux *)
   8378 		      ((bfd_byte *) everneed + iverneed->vn_aux));
   8379 	  ivernaux = iverneed->vn_auxptr;
   8380 	  for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
   8381 	    {
   8382 	      _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
   8383 
   8384 	      ivernaux->vna_nodename =
   8385 		bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   8386 						 ivernaux->vna_name);
   8387 	      if (ivernaux->vna_nodename == NULL)
   8388 		goto error_return_bad_verref;
   8389 
   8390 	      if (ivernaux->vna_other > freeidx)
   8391 		freeidx = ivernaux->vna_other;
   8392 
   8393 	      ivernaux->vna_nextptr = NULL;
   8394 	      if (ivernaux->vna_next == 0)
   8395 		{
   8396 		  iverneed->vn_cnt = j + 1;
   8397 		  break;
   8398 		}
   8399 	      if (j + 1 < iverneed->vn_cnt)
   8400 		ivernaux->vna_nextptr = ivernaux + 1;
   8401 
   8402 	      if (ivernaux->vna_next
   8403 		  > (size_t) (contents_end - (bfd_byte *) evernaux))
   8404 		goto error_return_bad_verref;
   8405 
   8406 	      evernaux = ((Elf_External_Vernaux *)
   8407 			  ((bfd_byte *) evernaux + ivernaux->vna_next));
   8408 	    }
   8409 
   8410 	  iverneed->vn_nextref = NULL;
   8411 	  if (iverneed->vn_next == 0)
   8412 	    break;
   8413 	  if (i + 1 < hdr->sh_info)
   8414 	    iverneed->vn_nextref = iverneed + 1;
   8415 
   8416 	  if (iverneed->vn_next
   8417 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   8418 	    goto error_return_bad_verref;
   8419 
   8420 	  everneed = ((Elf_External_Verneed *)
   8421 		      ((bfd_byte *) everneed + iverneed->vn_next));
   8422 	}
   8423       elf_tdata (abfd)->cverrefs = i;
   8424 
   8425       free (contents);
   8426       contents = NULL;
   8427     }
   8428 
   8429   if (elf_dynverdef (abfd) != 0)
   8430     {
   8431       Elf_Internal_Shdr *hdr;
   8432       Elf_External_Verdef *everdef;
   8433       Elf_Internal_Verdef *iverdef;
   8434       Elf_Internal_Verdef *iverdefarr;
   8435       Elf_Internal_Verdef iverdefmem;
   8436       unsigned int i;
   8437       unsigned int maxidx;
   8438       bfd_byte *contents_end_def, *contents_end_aux;
   8439 
   8440       hdr = &elf_tdata (abfd)->dynverdef_hdr;
   8441 
   8442       if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
   8443 	{
   8444 	error_return_bad_verdef:
   8445 	  _bfd_error_handler
   8446 	    (_("%pB: .gnu.version_d invalid entry"), abfd);
   8447 	  bfd_set_error (bfd_error_bad_value);
   8448 	error_return_verdef:
   8449 	  elf_tdata (abfd)->verdef = NULL;
   8450 	  elf_tdata (abfd)->cverdefs = 0;
   8451 	  goto error_return;
   8452 	}
   8453 
   8454       contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
   8455       if (contents == NULL)
   8456 	goto error_return_verdef;
   8457       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   8458 	  || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
   8459 	goto error_return_verdef;
   8460 
   8461       BFD_ASSERT (sizeof (Elf_External_Verdef)
   8462 		  >= sizeof (Elf_External_Verdaux));
   8463       contents_end_def = contents + hdr->sh_size
   8464 			 - sizeof (Elf_External_Verdef);
   8465       contents_end_aux = contents + hdr->sh_size
   8466 			 - sizeof (Elf_External_Verdaux);
   8467 
   8468       /* We know the number of entries in the section but not the maximum
   8469 	 index.  Therefore we have to run through all entries and find
   8470 	 the maximum.  */
   8471       everdef = (Elf_External_Verdef *) contents;
   8472       maxidx = 0;
   8473       for (i = 0; i < hdr->sh_info; ++i)
   8474 	{
   8475 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   8476 
   8477 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
   8478 	    goto error_return_bad_verdef;
   8479 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
   8480 	    maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
   8481 
   8482 	  if (iverdefmem.vd_next == 0)
   8483 	    break;
   8484 
   8485 	  if (iverdefmem.vd_next
   8486 	      > (size_t) (contents_end_def - (bfd_byte *) everdef))
   8487 	    goto error_return_bad_verdef;
   8488 
   8489 	  everdef = ((Elf_External_Verdef *)
   8490 		     ((bfd_byte *) everdef + iverdefmem.vd_next));
   8491 	}
   8492 
   8493       if (default_imported_symver)
   8494 	{
   8495 	  if (freeidx > maxidx)
   8496 	    maxidx = ++freeidx;
   8497 	  else
   8498 	    freeidx = ++maxidx;
   8499 	}
   8500 
   8501       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
   8502 	bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
   8503       if (elf_tdata (abfd)->verdef == NULL)
   8504 	goto error_return_verdef;
   8505 
   8506       elf_tdata (abfd)->cverdefs = maxidx;
   8507 
   8508       everdef = (Elf_External_Verdef *) contents;
   8509       iverdefarr = elf_tdata (abfd)->verdef;
   8510       for (i = 0; i < hdr->sh_info; i++)
   8511 	{
   8512 	  Elf_External_Verdaux *everdaux;
   8513 	  Elf_Internal_Verdaux *iverdaux;
   8514 	  unsigned int j;
   8515 
   8516 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   8517 
   8518 	  if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
   8519 	    goto error_return_bad_verdef;
   8520 
   8521 	  iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
   8522 	  memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
   8523 
   8524 	  iverdef->vd_bfd = abfd;
   8525 
   8526 	  if (iverdef->vd_cnt == 0)
   8527 	    iverdef->vd_auxptr = NULL;
   8528 	  else
   8529 	    {
   8530 	      iverdef->vd_auxptr = (struct elf_internal_verdaux *)
   8531 		  bfd_alloc2 (abfd, iverdef->vd_cnt,
   8532 			      sizeof (Elf_Internal_Verdaux));
   8533 	      if (iverdef->vd_auxptr == NULL)
   8534 		goto error_return_verdef;
   8535 	    }
   8536 
   8537 	  if (iverdef->vd_aux
   8538 	      > (size_t) (contents_end_aux - (bfd_byte *) everdef))
   8539 	    goto error_return_bad_verdef;
   8540 
   8541 	  everdaux = ((Elf_External_Verdaux *)
   8542 		      ((bfd_byte *) everdef + iverdef->vd_aux));
   8543 	  iverdaux = iverdef->vd_auxptr;
   8544 	  for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
   8545 	    {
   8546 	      _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
   8547 
   8548 	      iverdaux->vda_nodename =
   8549 		bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   8550 						 iverdaux->vda_name);
   8551 	      if (iverdaux->vda_nodename == NULL)
   8552 		goto error_return_bad_verdef;
   8553 
   8554 	      iverdaux->vda_nextptr = NULL;
   8555 	      if (iverdaux->vda_next == 0)
   8556 		{
   8557 		  iverdef->vd_cnt = j + 1;
   8558 		  break;
   8559 		}
   8560 	      if (j + 1 < iverdef->vd_cnt)
   8561 		iverdaux->vda_nextptr = iverdaux + 1;
   8562 
   8563 	      if (iverdaux->vda_next
   8564 		  > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
   8565 		goto error_return_bad_verdef;
   8566 
   8567 	      everdaux = ((Elf_External_Verdaux *)
   8568 			  ((bfd_byte *) everdaux + iverdaux->vda_next));
   8569 	    }
   8570 
   8571 	  iverdef->vd_nodename = NULL;
   8572 	  if (iverdef->vd_cnt)
   8573 	    iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
   8574 
   8575 	  iverdef->vd_nextdef = NULL;
   8576 	  if (iverdef->vd_next == 0)
   8577 	    break;
   8578 	  if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
   8579 	    iverdef->vd_nextdef = iverdef + 1;
   8580 
   8581 	  everdef = ((Elf_External_Verdef *)
   8582 		     ((bfd_byte *) everdef + iverdef->vd_next));
   8583 	}
   8584 
   8585       free (contents);
   8586       contents = NULL;
   8587     }
   8588   else if (default_imported_symver)
   8589     {
   8590       if (freeidx < 3)
   8591 	freeidx = 3;
   8592       else
   8593 	freeidx++;
   8594 
   8595       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
   8596 	  bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
   8597       if (elf_tdata (abfd)->verdef == NULL)
   8598 	goto error_return;
   8599 
   8600       elf_tdata (abfd)->cverdefs = freeidx;
   8601     }
   8602 
   8603   /* Create a default version based on the soname.  */
   8604   if (default_imported_symver)
   8605     {
   8606       Elf_Internal_Verdef *iverdef;
   8607       Elf_Internal_Verdaux *iverdaux;
   8608 
   8609       iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
   8610 
   8611       iverdef->vd_version = VER_DEF_CURRENT;
   8612       iverdef->vd_flags = 0;
   8613       iverdef->vd_ndx = freeidx;
   8614       iverdef->vd_cnt = 1;
   8615 
   8616       iverdef->vd_bfd = abfd;
   8617 
   8618       iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
   8619       if (iverdef->vd_nodename == NULL)
   8620 	goto error_return_verdef;
   8621       iverdef->vd_nextdef = NULL;
   8622       iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
   8623 			    bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
   8624       if (iverdef->vd_auxptr == NULL)
   8625 	goto error_return_verdef;
   8626 
   8627       iverdaux = iverdef->vd_auxptr;
   8628       iverdaux->vda_nodename = iverdef->vd_nodename;
   8629     }
   8630 
   8631   return TRUE;
   8632 
   8633  error_return:
   8634   if (contents != NULL)
   8635     free (contents);
   8636   return FALSE;
   8637 }
   8638 
   8639 asymbol *
   8641 _bfd_elf_make_empty_symbol (bfd *abfd)
   8642 {
   8643   elf_symbol_type *newsym;
   8644 
   8645   newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym);
   8646   if (!newsym)
   8647     return NULL;
   8648   newsym->symbol.the_bfd = abfd;
   8649   return &newsym->symbol;
   8650 }
   8651 
   8652 void
   8653 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
   8654 			  asymbol *symbol,
   8655 			  symbol_info *ret)
   8656 {
   8657   bfd_symbol_info (symbol, ret);
   8658 }
   8659 
   8660 /* Return whether a symbol name implies a local symbol.  Most targets
   8661    use this function for the is_local_label_name entry point, but some
   8662    override it.  */
   8663 
   8664 bfd_boolean
   8665 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
   8666 			      const char *name)
   8667 {
   8668   /* Normal local symbols start with ``.L''.  */
   8669   if (name[0] == '.' && name[1] == 'L')
   8670     return TRUE;
   8671 
   8672   /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
   8673      DWARF debugging symbols starting with ``..''.  */
   8674   if (name[0] == '.' && name[1] == '.')
   8675     return TRUE;
   8676 
   8677   /* gcc will sometimes generate symbols beginning with ``_.L_'' when
   8678      emitting DWARF debugging output.  I suspect this is actually a
   8679      small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
   8680      ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
   8681      underscore to be emitted on some ELF targets).  For ease of use,
   8682      we treat such symbols as local.  */
   8683   if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
   8684     return TRUE;
   8685 
   8686   /* Treat assembler generated fake symbols, dollar local labels and
   8687      forward-backward labels (aka local labels) as locals.
   8688      These labels have the form:
   8689 
   8690        L0^A.*				       (fake symbols)
   8691 
   8692        [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
   8693 
   8694      Versions which start with .L will have already been matched above,
   8695      so we only need to match the rest.  */
   8696   if (name[0] == 'L' && ISDIGIT (name[1]))
   8697     {
   8698       bfd_boolean ret = FALSE;
   8699       const char * p;
   8700       char c;
   8701 
   8702       for (p = name + 2; (c = *p); p++)
   8703 	{
   8704 	  if (c == 1 || c == 2)
   8705 	    {
   8706 	      if (c == 1 && p == name + 2)
   8707 		/* A fake symbol.  */
   8708 		return TRUE;
   8709 
   8710 	      /* FIXME: We are being paranoid here and treating symbols like
   8711 		 L0^Bfoo as if there were non-local, on the grounds that the
   8712 		 assembler will never generate them.  But can any symbol
   8713 		 containing an ASCII value in the range 1-31 ever be anything
   8714 		 other than some kind of local ?  */
   8715 	      ret = TRUE;
   8716 	    }
   8717 
   8718 	  if (! ISDIGIT (c))
   8719 	    {
   8720 	      ret = FALSE;
   8721 	      break;
   8722 	    }
   8723 	}
   8724       return ret;
   8725     }
   8726 
   8727   return FALSE;
   8728 }
   8729 
   8730 alent *
   8731 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
   8732 		     asymbol *symbol ATTRIBUTE_UNUSED)
   8733 {
   8734   abort ();
   8735   return NULL;
   8736 }
   8737 
   8738 bfd_boolean
   8739 _bfd_elf_set_arch_mach (bfd *abfd,
   8740 			enum bfd_architecture arch,
   8741 			unsigned long machine)
   8742 {
   8743   /* If this isn't the right architecture for this backend, and this
   8744      isn't the generic backend, fail.  */
   8745   if (arch != get_elf_backend_data (abfd)->arch
   8746       && arch != bfd_arch_unknown
   8747       && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
   8748     return FALSE;
   8749 
   8750   return bfd_default_set_arch_mach (abfd, arch, machine);
   8751 }
   8752 
   8753 /* Find the nearest line to a particular section and offset,
   8754    for error reporting.  */
   8755 
   8756 bfd_boolean
   8757 _bfd_elf_find_nearest_line (bfd *abfd,
   8758 			    asymbol **symbols,
   8759 			    asection *section,
   8760 			    bfd_vma offset,
   8761 			    const char **filename_ptr,
   8762 			    const char **functionname_ptr,
   8763 			    unsigned int *line_ptr,
   8764 			    unsigned int *discriminator_ptr)
   8765 {
   8766   bfd_boolean found;
   8767 
   8768   if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
   8769 				     filename_ptr, functionname_ptr,
   8770 				     line_ptr, discriminator_ptr,
   8771 				     dwarf_debug_sections, 0,
   8772 				     &elf_tdata (abfd)->dwarf2_find_line_info)
   8773       || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
   8774 					filename_ptr, functionname_ptr,
   8775 					line_ptr))
   8776     {
   8777       if (!*functionname_ptr)
   8778 	_bfd_elf_find_function (abfd, symbols, section, offset,
   8779 				*filename_ptr ? NULL : filename_ptr,
   8780 				functionname_ptr);
   8781       return TRUE;
   8782     }
   8783 
   8784   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
   8785 					     &found, filename_ptr,
   8786 					     functionname_ptr, line_ptr,
   8787 					     &elf_tdata (abfd)->line_info))
   8788     return FALSE;
   8789   if (found && (*functionname_ptr || *line_ptr))
   8790     return TRUE;
   8791 
   8792   if (symbols == NULL)
   8793     return FALSE;
   8794 
   8795   if (! _bfd_elf_find_function (abfd, symbols, section, offset,
   8796 				filename_ptr, functionname_ptr))
   8797     return FALSE;
   8798 
   8799   *line_ptr = 0;
   8800   return TRUE;
   8801 }
   8802 
   8803 /* Find the line for a symbol.  */
   8804 
   8805 bfd_boolean
   8806 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
   8807 		    const char **filename_ptr, unsigned int *line_ptr)
   8808 {
   8809   return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
   8810 					filename_ptr, NULL, line_ptr, NULL,
   8811 					dwarf_debug_sections, 0,
   8812 					&elf_tdata (abfd)->dwarf2_find_line_info);
   8813 }
   8814 
   8815 /* After a call to bfd_find_nearest_line, successive calls to
   8816    bfd_find_inliner_info can be used to get source information about
   8817    each level of function inlining that terminated at the address
   8818    passed to bfd_find_nearest_line.  Currently this is only supported
   8819    for DWARF2 with appropriate DWARF3 extensions. */
   8820 
   8821 bfd_boolean
   8822 _bfd_elf_find_inliner_info (bfd *abfd,
   8823 			    const char **filename_ptr,
   8824 			    const char **functionname_ptr,
   8825 			    unsigned int *line_ptr)
   8826 {
   8827   bfd_boolean found;
   8828   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
   8829 					 functionname_ptr, line_ptr,
   8830 					 & elf_tdata (abfd)->dwarf2_find_line_info);
   8831   return found;
   8832 }
   8833 
   8834 int
   8835 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
   8836 {
   8837   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8838   int ret = bed->s->sizeof_ehdr;
   8839 
   8840   if (!bfd_link_relocatable (info))
   8841     {
   8842       bfd_size_type phdr_size = elf_program_header_size (abfd);
   8843 
   8844       if (phdr_size == (bfd_size_type) -1)
   8845 	{
   8846 	  struct elf_segment_map *m;
   8847 
   8848 	  phdr_size = 0;
   8849 	  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   8850 	    phdr_size += bed->s->sizeof_phdr;
   8851 
   8852 	  if (phdr_size == 0)
   8853 	    phdr_size = get_program_header_size (abfd, info);
   8854 	}
   8855 
   8856       elf_program_header_size (abfd) = phdr_size;
   8857       ret += phdr_size;
   8858     }
   8859 
   8860   return ret;
   8861 }
   8862 
   8863 bfd_boolean
   8864 _bfd_elf_set_section_contents (bfd *abfd,
   8865 			       sec_ptr section,
   8866 			       const void *location,
   8867 			       file_ptr offset,
   8868 			       bfd_size_type count)
   8869 {
   8870   Elf_Internal_Shdr *hdr;
   8871   file_ptr pos;
   8872 
   8873   if (! abfd->output_has_begun
   8874       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   8875     return FALSE;
   8876 
   8877   if (!count)
   8878     return TRUE;
   8879 
   8880   hdr = &elf_section_data (section)->this_hdr;
   8881   if (hdr->sh_offset == (file_ptr) -1)
   8882     {
   8883       /* We must compress this section.  Write output to the buffer.  */
   8884       unsigned char *contents = hdr->contents;
   8885       if ((offset + count) > hdr->sh_size
   8886 	  || (section->flags & SEC_ELF_COMPRESS) == 0
   8887 	  || contents == NULL)
   8888 	abort ();
   8889       memcpy (contents + offset, location, count);
   8890       return TRUE;
   8891     }
   8892   pos = hdr->sh_offset + offset;
   8893   if (bfd_seek (abfd, pos, SEEK_SET) != 0
   8894       || bfd_bwrite (location, count, abfd) != count)
   8895     return FALSE;
   8896 
   8897   return TRUE;
   8898 }
   8899 
   8900 bfd_boolean
   8901 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
   8902 			   arelent *cache_ptr ATTRIBUTE_UNUSED,
   8903 			   Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
   8904 {
   8905   abort ();
   8906   return FALSE;
   8907 }
   8908 
   8909 /* Try to convert a non-ELF reloc into an ELF one.  */
   8910 
   8911 bfd_boolean
   8912 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
   8913 {
   8914   /* Check whether we really have an ELF howto.  */
   8915 
   8916   if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
   8917     {
   8918       bfd_reloc_code_real_type code;
   8919       reloc_howto_type *howto;
   8920 
   8921       /* Alien reloc: Try to determine its type to replace it with an
   8922 	 equivalent ELF reloc.  */
   8923 
   8924       if (areloc->howto->pc_relative)
   8925 	{
   8926 	  switch (areloc->howto->bitsize)
   8927 	    {
   8928 	    case 8:
   8929 	      code = BFD_RELOC_8_PCREL;
   8930 	      break;
   8931 	    case 12:
   8932 	      code = BFD_RELOC_12_PCREL;
   8933 	      break;
   8934 	    case 16:
   8935 	      code = BFD_RELOC_16_PCREL;
   8936 	      break;
   8937 	    case 24:
   8938 	      code = BFD_RELOC_24_PCREL;
   8939 	      break;
   8940 	    case 32:
   8941 	      code = BFD_RELOC_32_PCREL;
   8942 	      break;
   8943 	    case 64:
   8944 	      code = BFD_RELOC_64_PCREL;
   8945 	      break;
   8946 	    default:
   8947 	      goto fail;
   8948 	    }
   8949 
   8950 	  howto = bfd_reloc_type_lookup (abfd, code);
   8951 
   8952 	  if (areloc->howto->pcrel_offset != howto->pcrel_offset)
   8953 	    {
   8954 	      if (howto->pcrel_offset)
   8955 		areloc->addend += areloc->address;
   8956 	      else
   8957 		areloc->addend -= areloc->address; /* addend is unsigned!! */
   8958 	    }
   8959 	}
   8960       else
   8961 	{
   8962 	  switch (areloc->howto->bitsize)
   8963 	    {
   8964 	    case 8:
   8965 	      code = BFD_RELOC_8;
   8966 	      break;
   8967 	    case 14:
   8968 	      code = BFD_RELOC_14;
   8969 	      break;
   8970 	    case 16:
   8971 	      code = BFD_RELOC_16;
   8972 	      break;
   8973 	    case 26:
   8974 	      code = BFD_RELOC_26;
   8975 	      break;
   8976 	    case 32:
   8977 	      code = BFD_RELOC_32;
   8978 	      break;
   8979 	    case 64:
   8980 	      code = BFD_RELOC_64;
   8981 	      break;
   8982 	    default:
   8983 	      goto fail;
   8984 	    }
   8985 
   8986 	  howto = bfd_reloc_type_lookup (abfd, code);
   8987 	}
   8988 
   8989       if (howto)
   8990 	areloc->howto = howto;
   8991       else
   8992 	goto fail;
   8993     }
   8994 
   8995   return TRUE;
   8996 
   8997  fail:
   8998   /* xgettext:c-format */
   8999   _bfd_error_handler (_("%pB: %s unsupported"),
   9000 		      abfd, areloc->howto->name);
   9001   bfd_set_error (bfd_error_bad_value);
   9002   return FALSE;
   9003 }
   9004 
   9005 bfd_boolean
   9006 _bfd_elf_close_and_cleanup (bfd *abfd)
   9007 {
   9008   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   9009   if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
   9010     {
   9011       if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
   9012 	_bfd_elf_strtab_free (elf_shstrtab (abfd));
   9013       _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
   9014     }
   9015 
   9016   return _bfd_generic_close_and_cleanup (abfd);
   9017 }
   9018 
   9019 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
   9020    in the relocation's offset.  Thus we cannot allow any sort of sanity
   9021    range-checking to interfere.  There is nothing else to do in processing
   9022    this reloc.  */
   9023 
   9024 bfd_reloc_status_type
   9025 _bfd_elf_rel_vtable_reloc_fn
   9026   (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
   9027    struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
   9028    void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
   9029    bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
   9030 {
   9031   return bfd_reloc_ok;
   9032 }
   9033 
   9034 /* Elf core file support.  Much of this only works on native
   9036    toolchains, since we rely on knowing the
   9037    machine-dependent procfs structure in order to pick
   9038    out details about the corefile.  */
   9039 
   9040 #ifdef HAVE_SYS_PROCFS_H
   9041 /* Needed for new procfs interface on sparc-solaris.  */
   9042 # define _STRUCTURED_PROC 1
   9043 # include <sys/procfs.h>
   9044 #endif
   9045 
   9046 /* Return a PID that identifies a "thread" for threaded cores, or the
   9047    PID of the main process for non-threaded cores.  */
   9048 
   9049 static int
   9050 elfcore_make_pid (bfd *abfd)
   9051 {
   9052   int pid;
   9053 
   9054   pid = elf_tdata (abfd)->core->lwpid;
   9055   if (pid == 0)
   9056     pid = elf_tdata (abfd)->core->pid;
   9057 
   9058   return pid;
   9059 }
   9060 
   9061 /* If there isn't a section called NAME, make one, using
   9062    data from SECT.  Note, this function will generate a
   9063    reference to NAME, so you shouldn't deallocate or
   9064    overwrite it.  */
   9065 
   9066 static bfd_boolean
   9067 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
   9068 {
   9069   asection *sect2;
   9070 
   9071   if (bfd_get_section_by_name (abfd, name) != NULL)
   9072     return TRUE;
   9073 
   9074   sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
   9075   if (sect2 == NULL)
   9076     return FALSE;
   9077 
   9078   sect2->size = sect->size;
   9079   sect2->filepos = sect->filepos;
   9080   sect2->alignment_power = sect->alignment_power;
   9081   return TRUE;
   9082 }
   9083 
   9084 /* Create a pseudosection containing SIZE bytes at FILEPOS.  This
   9085    actually creates up to two pseudosections:
   9086    - For the single-threaded case, a section named NAME, unless
   9087      such a section already exists.
   9088    - For the multi-threaded case, a section named "NAME/PID", where
   9089      PID is elfcore_make_pid (abfd).
   9090    Both pseudosections have identical contents.  */
   9091 bfd_boolean
   9092 _bfd_elfcore_make_pseudosection (bfd *abfd,
   9093 				 char *name,
   9094 				 size_t size,
   9095 				 ufile_ptr filepos)
   9096 {
   9097   char buf[100];
   9098   char *threaded_name;
   9099   size_t len;
   9100   asection *sect;
   9101 
   9102   /* Build the section name.  */
   9103 
   9104   sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
   9105   len = strlen (buf) + 1;
   9106   threaded_name = (char *) bfd_alloc (abfd, len);
   9107   if (threaded_name == NULL)
   9108     return FALSE;
   9109   memcpy (threaded_name, buf, len);
   9110 
   9111   sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
   9112 					     SEC_HAS_CONTENTS);
   9113   if (sect == NULL)
   9114     return FALSE;
   9115   sect->size = size;
   9116   sect->filepos = filepos;
   9117   sect->alignment_power = 2;
   9118 
   9119   return elfcore_maybe_make_sect (abfd, name, sect);
   9120 }
   9121 
   9122 static bfd_boolean
   9123 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
   9124     size_t offs)
   9125 {
   9126   asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
   9127     SEC_HAS_CONTENTS);
   9128 
   9129   if (sect == NULL)
   9130     return FALSE;
   9131   sect->size = note->descsz - offs;
   9132   sect->filepos = note->descpos + offs;
   9133   sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   9134 
   9135   return TRUE;
   9136 }
   9137 
   9138 /* prstatus_t exists on:
   9139      solaris 2.5+
   9140      linux 2.[01] + glibc
   9141      unixware 4.2
   9142 */
   9143 
   9144 #if defined (HAVE_PRSTATUS_T)
   9145 
   9146 static bfd_boolean
   9147 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   9148 {
   9149   size_t size;
   9150   int offset;
   9151 
   9152   if (note->descsz == sizeof (prstatus_t))
   9153     {
   9154       prstatus_t prstat;
   9155 
   9156       size = sizeof (prstat.pr_reg);
   9157       offset   = offsetof (prstatus_t, pr_reg);
   9158       memcpy (&prstat, note->descdata, sizeof (prstat));
   9159 
   9160       /* Do not overwrite the core signal if it
   9161 	 has already been set by another thread.  */
   9162       if (elf_tdata (abfd)->core->signal == 0)
   9163 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   9164       if (elf_tdata (abfd)->core->pid == 0)
   9165 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   9166 
   9167       /* pr_who exists on:
   9168 	 solaris 2.5+
   9169 	 unixware 4.2
   9170 	 pr_who doesn't exist on:
   9171 	 linux 2.[01]
   9172 	 */
   9173 #if defined (HAVE_PRSTATUS_T_PR_WHO)
   9174       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   9175 #else
   9176       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   9177 #endif
   9178     }
   9179 #if defined (HAVE_PRSTATUS32_T)
   9180   else if (note->descsz == sizeof (prstatus32_t))
   9181     {
   9182       /* 64-bit host, 32-bit corefile */
   9183       prstatus32_t prstat;
   9184 
   9185       size = sizeof (prstat.pr_reg);
   9186       offset   = offsetof (prstatus32_t, pr_reg);
   9187       memcpy (&prstat, note->descdata, sizeof (prstat));
   9188 
   9189       /* Do not overwrite the core signal if it
   9190 	 has already been set by another thread.  */
   9191       if (elf_tdata (abfd)->core->signal == 0)
   9192 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   9193       if (elf_tdata (abfd)->core->pid == 0)
   9194 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   9195 
   9196       /* pr_who exists on:
   9197 	 solaris 2.5+
   9198 	 unixware 4.2
   9199 	 pr_who doesn't exist on:
   9200 	 linux 2.[01]
   9201 	 */
   9202 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
   9203       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   9204 #else
   9205       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   9206 #endif
   9207     }
   9208 #endif /* HAVE_PRSTATUS32_T */
   9209   else
   9210     {
   9211       /* Fail - we don't know how to handle any other
   9212 	 note size (ie. data object type).  */
   9213       return TRUE;
   9214     }
   9215 
   9216   /* Make a ".reg/999" section and a ".reg" section.  */
   9217   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   9218 					  size, note->descpos + offset);
   9219 }
   9220 #endif /* defined (HAVE_PRSTATUS_T) */
   9221 
   9222 /* Create a pseudosection containing the exact contents of NOTE.  */
   9223 static bfd_boolean
   9224 elfcore_make_note_pseudosection (bfd *abfd,
   9225 				 char *name,
   9226 				 Elf_Internal_Note *note)
   9227 {
   9228   return _bfd_elfcore_make_pseudosection (abfd, name,
   9229 					  note->descsz, note->descpos);
   9230 }
   9231 
   9232 /* There isn't a consistent prfpregset_t across platforms,
   9233    but it doesn't matter, because we don't have to pick this
   9234    data structure apart.  */
   9235 
   9236 static bfd_boolean
   9237 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
   9238 {
   9239   return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   9240 }
   9241 
   9242 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
   9243    type of NT_PRXFPREG.  Just include the whole note's contents
   9244    literally.  */
   9245 
   9246 static bfd_boolean
   9247 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
   9248 {
   9249   return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
   9250 }
   9251 
   9252 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
   9253    with a note type of NT_X86_XSTATE.  Just include the whole note's
   9254    contents literally.  */
   9255 
   9256 static bfd_boolean
   9257 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
   9258 {
   9259   return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
   9260 }
   9261 
   9262 static bfd_boolean
   9263 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
   9264 {
   9265   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
   9266 }
   9267 
   9268 static bfd_boolean
   9269 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
   9270 {
   9271   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
   9272 }
   9273 
   9274 static bfd_boolean
   9275 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
   9276 {
   9277   return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
   9278 }
   9279 
   9280 static bfd_boolean
   9281 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
   9282 {
   9283   return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
   9284 }
   9285 
   9286 static bfd_boolean
   9287 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
   9288 {
   9289   return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
   9290 }
   9291 
   9292 static bfd_boolean
   9293 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
   9294 {
   9295   return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
   9296 }
   9297 
   9298 static bfd_boolean
   9299 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
   9300 {
   9301   return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
   9302 }
   9303 
   9304 static bfd_boolean
   9305 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
   9306 {
   9307   return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
   9308 }
   9309 
   9310 static bfd_boolean
   9311 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
   9312 {
   9313   return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
   9314 }
   9315 
   9316 static bfd_boolean
   9317 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
   9318 {
   9319   return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
   9320 }
   9321 
   9322 static bfd_boolean
   9323 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
   9324 {
   9325   return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
   9326 }
   9327 
   9328 static bfd_boolean
   9329 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
   9330 {
   9331   return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
   9332 }
   9333 
   9334 static bfd_boolean
   9335 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
   9336 {
   9337   return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
   9338 }
   9339 
   9340 static bfd_boolean
   9341 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
   9342 {
   9343   return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
   9344 }
   9345 
   9346 static bfd_boolean
   9347 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
   9348 {
   9349   return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
   9350 }
   9351 
   9352 static bfd_boolean
   9353 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
   9354 {
   9355   return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
   9356 }
   9357 
   9358 static bfd_boolean
   9359 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
   9360 {
   9361   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
   9362 }
   9363 
   9364 static bfd_boolean
   9365 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
   9366 {
   9367   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
   9368 }
   9369 
   9370 static bfd_boolean
   9371 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
   9372 {
   9373   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
   9374 }
   9375 
   9376 #if defined (HAVE_PRPSINFO_T)
   9377 typedef prpsinfo_t   elfcore_psinfo_t;
   9378 #if defined (HAVE_PRPSINFO32_T)		/* Sparc64 cross Sparc32 */
   9379 typedef prpsinfo32_t elfcore_psinfo32_t;
   9380 #endif
   9381 #endif
   9382 
   9383 #if defined (HAVE_PSINFO_T)
   9384 typedef psinfo_t   elfcore_psinfo_t;
   9385 #if defined (HAVE_PSINFO32_T)		/* Sparc64 cross Sparc32 */
   9386 typedef psinfo32_t elfcore_psinfo32_t;
   9387 #endif
   9388 #endif
   9389 
   9390 /* return a malloc'ed copy of a string at START which is at
   9391    most MAX bytes long, possibly without a terminating '\0'.
   9392    the copy will always have a terminating '\0'.  */
   9393 
   9394 char *
   9395 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
   9396 {
   9397   char *dups;
   9398   char *end = (char *) memchr (start, '\0', max);
   9399   size_t len;
   9400 
   9401   if (end == NULL)
   9402     len = max;
   9403   else
   9404     len = end - start;
   9405 
   9406   dups = (char *) bfd_alloc (abfd, len + 1);
   9407   if (dups == NULL)
   9408     return NULL;
   9409 
   9410   memcpy (dups, start, len);
   9411   dups[len] = '\0';
   9412 
   9413   return dups;
   9414 }
   9415 
   9416 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   9417 static bfd_boolean
   9418 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   9419 {
   9420   if (note->descsz == sizeof (elfcore_psinfo_t))
   9421     {
   9422       elfcore_psinfo_t psinfo;
   9423 
   9424       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   9425 
   9426 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
   9427       elf_tdata (abfd)->core->pid = psinfo.pr_pid;
   9428 #endif
   9429       elf_tdata (abfd)->core->program
   9430 	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
   9431 				sizeof (psinfo.pr_fname));
   9432 
   9433       elf_tdata (abfd)->core->command
   9434 	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
   9435 				sizeof (psinfo.pr_psargs));
   9436     }
   9437 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   9438   else if (note->descsz == sizeof (elfcore_psinfo32_t))
   9439     {
   9440       /* 64-bit host, 32-bit corefile */
   9441       elfcore_psinfo32_t psinfo;
   9442 
   9443       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   9444 
   9445 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
   9446       elf_tdata (abfd)->core->pid = psinfo.pr_pid;
   9447 #endif
   9448       elf_tdata (abfd)->core->program
   9449 	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
   9450 				sizeof (psinfo.pr_fname));
   9451 
   9452       elf_tdata (abfd)->core->command
   9453 	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
   9454 				sizeof (psinfo.pr_psargs));
   9455     }
   9456 #endif
   9457 
   9458   else
   9459     {
   9460       /* Fail - we don't know how to handle any other
   9461 	 note size (ie. data object type).  */
   9462       return TRUE;
   9463     }
   9464 
   9465   /* Note that for some reason, a spurious space is tacked
   9466      onto the end of the args in some (at least one anyway)
   9467      implementations, so strip it off if it exists.  */
   9468 
   9469   {
   9470     char *command = elf_tdata (abfd)->core->command;
   9471     int n = strlen (command);
   9472 
   9473     if (0 < n && command[n - 1] == ' ')
   9474       command[n - 1] = '\0';
   9475   }
   9476 
   9477   return TRUE;
   9478 }
   9479 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
   9480 
   9481 #if defined (HAVE_PSTATUS_T)
   9482 static bfd_boolean
   9483 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
   9484 {
   9485   if (note->descsz == sizeof (pstatus_t)
   9486 #if defined (HAVE_PXSTATUS_T)
   9487       || note->descsz == sizeof (pxstatus_t)
   9488 #endif
   9489       )
   9490     {
   9491       pstatus_t pstat;
   9492 
   9493       memcpy (&pstat, note->descdata, sizeof (pstat));
   9494 
   9495       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   9496     }
   9497 #if defined (HAVE_PSTATUS32_T)
   9498   else if (note->descsz == sizeof (pstatus32_t))
   9499     {
   9500       /* 64-bit host, 32-bit corefile */
   9501       pstatus32_t pstat;
   9502 
   9503       memcpy (&pstat, note->descdata, sizeof (pstat));
   9504 
   9505       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   9506     }
   9507 #endif
   9508   /* Could grab some more details from the "representative"
   9509      lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
   9510      NT_LWPSTATUS note, presumably.  */
   9511 
   9512   return TRUE;
   9513 }
   9514 #endif /* defined (HAVE_PSTATUS_T) */
   9515 
   9516 #if defined (HAVE_LWPSTATUS_T)
   9517 static bfd_boolean
   9518 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
   9519 {
   9520   lwpstatus_t lwpstat;
   9521   char buf[100];
   9522   char *name;
   9523   size_t len;
   9524   asection *sect;
   9525 
   9526   if (note->descsz != sizeof (lwpstat)
   9527 #if defined (HAVE_LWPXSTATUS_T)
   9528       && note->descsz != sizeof (lwpxstatus_t)
   9529 #endif
   9530       )
   9531     return TRUE;
   9532 
   9533   memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
   9534 
   9535   elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
   9536   /* Do not overwrite the core signal if it has already been set by
   9537      another thread.  */
   9538   if (elf_tdata (abfd)->core->signal == 0)
   9539     elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
   9540 
   9541   /* Make a ".reg/999" section.  */
   9542 
   9543   sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
   9544   len = strlen (buf) + 1;
   9545   name = bfd_alloc (abfd, len);
   9546   if (name == NULL)
   9547     return FALSE;
   9548   memcpy (name, buf, len);
   9549 
   9550   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   9551   if (sect == NULL)
   9552     return FALSE;
   9553 
   9554 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   9555   sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
   9556   sect->filepos = note->descpos
   9557     + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
   9558 #endif
   9559 
   9560 #if defined (HAVE_LWPSTATUS_T_PR_REG)
   9561   sect->size = sizeof (lwpstat.pr_reg);
   9562   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
   9563 #endif
   9564 
   9565   sect->alignment_power = 2;
   9566 
   9567   if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
   9568     return FALSE;
   9569 
   9570   /* Make a ".reg2/999" section */
   9571 
   9572   sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
   9573   len = strlen (buf) + 1;
   9574   name = bfd_alloc (abfd, len);
   9575   if (name == NULL)
   9576     return FALSE;
   9577   memcpy (name, buf, len);
   9578 
   9579   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   9580   if (sect == NULL)
   9581     return FALSE;
   9582 
   9583 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   9584   sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
   9585   sect->filepos = note->descpos
   9586     + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
   9587 #endif
   9588 
   9589 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
   9590   sect->size = sizeof (lwpstat.pr_fpreg);
   9591   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
   9592 #endif
   9593 
   9594   sect->alignment_power = 2;
   9595 
   9596   return elfcore_maybe_make_sect (abfd, ".reg2", sect);
   9597 }
   9598 #endif /* defined (HAVE_LWPSTATUS_T) */
   9599 
   9600 static bfd_boolean
   9601 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
   9602 {
   9603   char buf[30];
   9604   char *name;
   9605   size_t len;
   9606   asection *sect;
   9607   int type;
   9608   int is_active_thread;
   9609   bfd_vma base_addr;
   9610 
   9611   if (note->descsz < 728)
   9612     return TRUE;
   9613 
   9614   if (! CONST_STRNEQ (note->namedata, "win32"))
   9615     return TRUE;
   9616 
   9617   type = bfd_get_32 (abfd, note->descdata);
   9618 
   9619   switch (type)
   9620     {
   9621     case 1 /* NOTE_INFO_PROCESS */:
   9622       /* FIXME: need to add ->core->command.  */
   9623       /* process_info.pid */
   9624       elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
   9625       /* process_info.signal */
   9626       elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
   9627       break;
   9628 
   9629     case 2 /* NOTE_INFO_THREAD */:
   9630       /* Make a ".reg/999" section.  */
   9631       /* thread_info.tid */
   9632       sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
   9633 
   9634       len = strlen (buf) + 1;
   9635       name = (char *) bfd_alloc (abfd, len);
   9636       if (name == NULL)
   9637 	return FALSE;
   9638 
   9639       memcpy (name, buf, len);
   9640 
   9641       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   9642       if (sect == NULL)
   9643 	return FALSE;
   9644 
   9645       /* sizeof (thread_info.thread_context) */
   9646       sect->size = 716;
   9647       /* offsetof (thread_info.thread_context) */
   9648       sect->filepos = note->descpos + 12;
   9649       sect->alignment_power = 2;
   9650 
   9651       /* thread_info.is_active_thread */
   9652       is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
   9653 
   9654       if (is_active_thread)
   9655 	if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
   9656 	  return FALSE;
   9657       break;
   9658 
   9659     case 3 /* NOTE_INFO_MODULE */:
   9660       /* Make a ".module/xxxxxxxx" section.  */
   9661       /* module_info.base_address */
   9662       base_addr = bfd_get_32 (abfd, note->descdata + 4);
   9663       sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
   9664 
   9665       len = strlen (buf) + 1;
   9666       name = (char *) bfd_alloc (abfd, len);
   9667       if (name == NULL)
   9668 	return FALSE;
   9669 
   9670       memcpy (name, buf, len);
   9671 
   9672       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   9673 
   9674       if (sect == NULL)
   9675 	return FALSE;
   9676 
   9677       sect->size = note->descsz;
   9678       sect->filepos = note->descpos;
   9679       sect->alignment_power = 2;
   9680       break;
   9681 
   9682     default:
   9683       return TRUE;
   9684     }
   9685 
   9686   return TRUE;
   9687 }
   9688 
   9689 static bfd_boolean
   9690 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
   9691 {
   9692   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   9693 
   9694   switch (note->type)
   9695     {
   9696     default:
   9697       return TRUE;
   9698 
   9699     case NT_PRSTATUS:
   9700       if (bed->elf_backend_grok_prstatus)
   9701 	if ((*bed->elf_backend_grok_prstatus) (abfd, note))
   9702 	  return TRUE;
   9703 #if defined (HAVE_PRSTATUS_T)
   9704       return elfcore_grok_prstatus (abfd, note);
   9705 #else
   9706       return TRUE;
   9707 #endif
   9708 
   9709 #if defined (HAVE_PSTATUS_T)
   9710     case NT_PSTATUS:
   9711       return elfcore_grok_pstatus (abfd, note);
   9712 #endif
   9713 
   9714 #if defined (HAVE_LWPSTATUS_T)
   9715     case NT_LWPSTATUS:
   9716       return elfcore_grok_lwpstatus (abfd, note);
   9717 #endif
   9718 
   9719     case NT_FPREGSET:		/* FIXME: rename to NT_PRFPREG */
   9720       return elfcore_grok_prfpreg (abfd, note);
   9721 
   9722     case NT_WIN32PSTATUS:
   9723       return elfcore_grok_win32pstatus (abfd, note);
   9724 
   9725     case NT_PRXFPREG:		/* Linux SSE extension */
   9726       if (note->namesz == 6
   9727 	  && strcmp (note->namedata, "LINUX") == 0)
   9728 	return elfcore_grok_prxfpreg (abfd, note);
   9729       else
   9730 	return TRUE;
   9731 
   9732     case NT_X86_XSTATE:		/* Linux XSAVE extension */
   9733       if (note->namesz == 6
   9734 	  && strcmp (note->namedata, "LINUX") == 0)
   9735 	return elfcore_grok_xstatereg (abfd, note);
   9736       else
   9737 	return TRUE;
   9738 
   9739     case NT_PPC_VMX:
   9740       if (note->namesz == 6
   9741 	  && strcmp (note->namedata, "LINUX") == 0)
   9742 	return elfcore_grok_ppc_vmx (abfd, note);
   9743       else
   9744 	return TRUE;
   9745 
   9746     case NT_PPC_VSX:
   9747       if (note->namesz == 6
   9748 	  && strcmp (note->namedata, "LINUX") == 0)
   9749 	return elfcore_grok_ppc_vsx (abfd, note);
   9750       else
   9751 	return TRUE;
   9752 
   9753     case NT_S390_HIGH_GPRS:
   9754       if (note->namesz == 6
   9755 	  && strcmp (note->namedata, "LINUX") == 0)
   9756 	return elfcore_grok_s390_high_gprs (abfd, note);
   9757       else
   9758 	return TRUE;
   9759 
   9760     case NT_S390_TIMER:
   9761       if (note->namesz == 6
   9762 	  && strcmp (note->namedata, "LINUX") == 0)
   9763 	return elfcore_grok_s390_timer (abfd, note);
   9764       else
   9765 	return TRUE;
   9766 
   9767     case NT_S390_TODCMP:
   9768       if (note->namesz == 6
   9769 	  && strcmp (note->namedata, "LINUX") == 0)
   9770 	return elfcore_grok_s390_todcmp (abfd, note);
   9771       else
   9772 	return TRUE;
   9773 
   9774     case NT_S390_TODPREG:
   9775       if (note->namesz == 6
   9776 	  && strcmp (note->namedata, "LINUX") == 0)
   9777 	return elfcore_grok_s390_todpreg (abfd, note);
   9778       else
   9779 	return TRUE;
   9780 
   9781     case NT_S390_CTRS:
   9782       if (note->namesz == 6
   9783 	  && strcmp (note->namedata, "LINUX") == 0)
   9784 	return elfcore_grok_s390_ctrs (abfd, note);
   9785       else
   9786 	return TRUE;
   9787 
   9788     case NT_S390_PREFIX:
   9789       if (note->namesz == 6
   9790 	  && strcmp (note->namedata, "LINUX") == 0)
   9791 	return elfcore_grok_s390_prefix (abfd, note);
   9792       else
   9793 	return TRUE;
   9794 
   9795     case NT_S390_LAST_BREAK:
   9796       if (note->namesz == 6
   9797 	  && strcmp (note->namedata, "LINUX") == 0)
   9798 	return elfcore_grok_s390_last_break (abfd, note);
   9799       else
   9800 	return TRUE;
   9801 
   9802     case NT_S390_SYSTEM_CALL:
   9803       if (note->namesz == 6
   9804 	  && strcmp (note->namedata, "LINUX") == 0)
   9805 	return elfcore_grok_s390_system_call (abfd, note);
   9806       else
   9807 	return TRUE;
   9808 
   9809     case NT_S390_TDB:
   9810       if (note->namesz == 6
   9811 	  && strcmp (note->namedata, "LINUX") == 0)
   9812 	return elfcore_grok_s390_tdb (abfd, note);
   9813       else
   9814 	return TRUE;
   9815 
   9816     case NT_S390_VXRS_LOW:
   9817       if (note->namesz == 6
   9818 	  && strcmp (note->namedata, "LINUX") == 0)
   9819 	return elfcore_grok_s390_vxrs_low (abfd, note);
   9820       else
   9821 	return TRUE;
   9822 
   9823     case NT_S390_VXRS_HIGH:
   9824       if (note->namesz == 6
   9825 	  && strcmp (note->namedata, "LINUX") == 0)
   9826 	return elfcore_grok_s390_vxrs_high (abfd, note);
   9827       else
   9828 	return TRUE;
   9829 
   9830     case NT_S390_GS_CB:
   9831       if (note->namesz == 6
   9832 	  && strcmp (note->namedata, "LINUX") == 0)
   9833 	return elfcore_grok_s390_gs_cb (abfd, note);
   9834       else
   9835 	return TRUE;
   9836 
   9837     case NT_S390_GS_BC:
   9838       if (note->namesz == 6
   9839 	  && strcmp (note->namedata, "LINUX") == 0)
   9840 	return elfcore_grok_s390_gs_bc (abfd, note);
   9841       else
   9842 	return TRUE;
   9843 
   9844     case NT_ARM_VFP:
   9845       if (note->namesz == 6
   9846 	  && strcmp (note->namedata, "LINUX") == 0)
   9847 	return elfcore_grok_arm_vfp (abfd, note);
   9848       else
   9849 	return TRUE;
   9850 
   9851     case NT_ARM_TLS:
   9852       if (note->namesz == 6
   9853 	  && strcmp (note->namedata, "LINUX") == 0)
   9854 	return elfcore_grok_aarch_tls (abfd, note);
   9855       else
   9856 	return TRUE;
   9857 
   9858     case NT_ARM_HW_BREAK:
   9859       if (note->namesz == 6
   9860 	  && strcmp (note->namedata, "LINUX") == 0)
   9861 	return elfcore_grok_aarch_hw_break (abfd, note);
   9862       else
   9863 	return TRUE;
   9864 
   9865     case NT_ARM_HW_WATCH:
   9866       if (note->namesz == 6
   9867 	  && strcmp (note->namedata, "LINUX") == 0)
   9868 	return elfcore_grok_aarch_hw_watch (abfd, note);
   9869       else
   9870 	return TRUE;
   9871 
   9872     case NT_PRPSINFO:
   9873     case NT_PSINFO:
   9874       if (bed->elf_backend_grok_psinfo)
   9875 	if ((*bed->elf_backend_grok_psinfo) (abfd, note))
   9876 	  return TRUE;
   9877 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   9878       return elfcore_grok_psinfo (abfd, note);
   9879 #else
   9880       return TRUE;
   9881 #endif
   9882 
   9883     case NT_AUXV:
   9884       return elfcore_make_auxv_note_section (abfd, note, 0);
   9885 
   9886     case NT_FILE:
   9887       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
   9888 					      note);
   9889 
   9890     case NT_SIGINFO:
   9891       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
   9892 					      note);
   9893 
   9894     }
   9895 }
   9896 
   9897 static bfd_boolean
   9898 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
   9899 {
   9900   struct bfd_build_id* build_id;
   9901 
   9902   if (note->descsz == 0)
   9903     return FALSE;
   9904 
   9905   build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
   9906   if (build_id == NULL)
   9907     return FALSE;
   9908 
   9909   build_id->size = note->descsz;
   9910   memcpy (build_id->data, note->descdata, note->descsz);
   9911   abfd->build_id = build_id;
   9912 
   9913   return TRUE;
   9914 }
   9915 
   9916 static bfd_boolean
   9917 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
   9918 {
   9919   switch (note->type)
   9920     {
   9921     default:
   9922       return TRUE;
   9923 
   9924     case NT_GNU_PROPERTY_TYPE_0:
   9925       return _bfd_elf_parse_gnu_properties (abfd, note);
   9926 
   9927     case NT_GNU_BUILD_ID:
   9928       return elfobj_grok_gnu_build_id (abfd, note);
   9929     }
   9930 }
   9931 
   9932 static bfd_boolean
   9933 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
   9934 {
   9935   struct sdt_note *cur =
   9936     (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note)
   9937 				   + note->descsz);
   9938 
   9939   cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
   9940   cur->size = (bfd_size_type) note->descsz;
   9941   memcpy (cur->data, note->descdata, note->descsz);
   9942 
   9943   elf_tdata (abfd)->sdt_note_head = cur;
   9944 
   9945   return TRUE;
   9946 }
   9947 
   9948 static bfd_boolean
   9949 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
   9950 {
   9951   switch (note->type)
   9952     {
   9953     case NT_STAPSDT:
   9954       return elfobj_grok_stapsdt_note_1 (abfd, note);
   9955 
   9956     default:
   9957       return TRUE;
   9958     }
   9959 }
   9960 
   9961 static bfd_boolean
   9962 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
   9963 {
   9964   size_t offset;
   9965 
   9966   switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
   9967     {
   9968     case ELFCLASS32:
   9969       if (note->descsz < 108)
   9970 	return FALSE;
   9971       break;
   9972 
   9973     case ELFCLASS64:
   9974       if (note->descsz < 120)
   9975 	return FALSE;
   9976       break;
   9977 
   9978     default:
   9979       return FALSE;
   9980     }
   9981 
   9982   /* Check for version 1 in pr_version.  */
   9983   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   9984     return FALSE;
   9985 
   9986   offset = 4;
   9987 
   9988   /* Skip over pr_psinfosz. */
   9989   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
   9990     offset += 4;
   9991   else
   9992     {
   9993       offset += 4;	/* Padding before pr_psinfosz. */
   9994       offset += 8;
   9995     }
   9996 
   9997   /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
   9998   elf_tdata (abfd)->core->program
   9999     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
   10000   offset += 17;
   10001 
   10002   /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
   10003   elf_tdata (abfd)->core->command
   10004     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
   10005   offset += 81;
   10006 
   10007   /* Padding before pr_pid.  */
   10008   offset += 2;
   10009 
   10010   /* The pr_pid field was added in version "1a".  */
   10011   if (note->descsz < offset + 4)
   10012     return TRUE;
   10013 
   10014   elf_tdata (abfd)->core->pid
   10015     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   10016 
   10017   return TRUE;
   10018 }
   10019 
   10020 static bfd_boolean
   10021 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
   10022 {
   10023   size_t offset;
   10024   size_t size;
   10025   size_t min_size;
   10026 
   10027   /* Compute offset of pr_getregsz, skipping over pr_statussz.
   10028      Also compute minimum size of this note.  */
   10029   switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
   10030     {
   10031     case ELFCLASS32:
   10032       offset = 4 + 4;
   10033       min_size = offset + (4 * 2) + 4 + 4 + 4;
   10034       break;
   10035 
   10036     case ELFCLASS64:
   10037       offset = 4 + 4 + 8;	/* Includes padding before pr_statussz.  */
   10038       min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
   10039       break;
   10040 
   10041     default:
   10042       return FALSE;
   10043     }
   10044 
   10045   if (note->descsz < min_size)
   10046     return FALSE;
   10047 
   10048   /* Check for version 1 in pr_version.  */
   10049   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   10050     return FALSE;
   10051 
   10052   /* Extract size of pr_reg from pr_gregsetsz.  */
   10053   /* Skip over pr_gregsetsz and pr_fpregsetsz.  */
   10054   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
   10055     {
   10056       size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   10057       offset += 4 * 2;
   10058     }
   10059   else
   10060     {
   10061       size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
   10062       offset += 8 * 2;
   10063     }
   10064 
   10065   /* Skip over pr_osreldate.  */
   10066   offset += 4;
   10067 
   10068   /* Read signal from pr_cursig.  */
   10069   if (elf_tdata (abfd)->core->signal == 0)
   10070     elf_tdata (abfd)->core->signal
   10071       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   10072   offset += 4;
   10073 
   10074   /* Read TID from pr_pid.  */
   10075   elf_tdata (abfd)->core->lwpid
   10076       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   10077   offset += 4;
   10078 
   10079   /* Padding before pr_reg.  */
   10080   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
   10081     offset += 4;
   10082 
   10083   /* Make sure that there is enough data remaining in the note.  */
   10084   if ((note->descsz - offset) < size)
   10085     return FALSE;
   10086 
   10087   /* Make a ".reg/999" section and a ".reg" section.  */
   10088   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   10089 					  size, note->descpos + offset);
   10090 }
   10091 
   10092 static bfd_boolean
   10093 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
   10094 {
   10095   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   10096 
   10097   switch (note->type)
   10098     {
   10099     case NT_PRSTATUS:
   10100       if (bed->elf_backend_grok_freebsd_prstatus)
   10101 	if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
   10102 	  return TRUE;
   10103       return elfcore_grok_freebsd_prstatus (abfd, note);
   10104 
   10105     case NT_FPREGSET:
   10106       return elfcore_grok_prfpreg (abfd, note);
   10107 
   10108     case NT_PRPSINFO:
   10109       return elfcore_grok_freebsd_psinfo (abfd, note);
   10110 
   10111     case NT_FREEBSD_THRMISC:
   10112       if (note->namesz == 8)
   10113 	return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
   10114       else
   10115 	return TRUE;
   10116 
   10117     case NT_FREEBSD_PROCSTAT_PROC:
   10118       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
   10119 					      note);
   10120 
   10121     case NT_FREEBSD_PROCSTAT_FILES:
   10122       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
   10123 					      note);
   10124 
   10125     case NT_FREEBSD_PROCSTAT_VMMAP:
   10126       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
   10127 					      note);
   10128 
   10129     case NT_FREEBSD_PROCSTAT_AUXV:
   10130       return elfcore_make_auxv_note_section (abfd, note, 4);
   10131 
   10132     case NT_X86_XSTATE:
   10133       if (note->namesz == 8)
   10134 	return elfcore_grok_xstatereg (abfd, note);
   10135       else
   10136 	return TRUE;
   10137 
   10138     case NT_FREEBSD_PTLWPINFO:
   10139       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
   10140 					      note);
   10141 
   10142     case NT_ARM_VFP:
   10143       return elfcore_grok_arm_vfp (abfd, note);
   10144 
   10145     default:
   10146       return TRUE;
   10147     }
   10148 }
   10149 
   10150 static bfd_boolean
   10151 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
   10152 {
   10153   char *cp;
   10154 
   10155   cp = strchr (note->namedata, '@');
   10156   if (cp != NULL)
   10157     {
   10158       *lwpidp = atoi(cp + 1);
   10159       return TRUE;
   10160     }
   10161   return FALSE;
   10162 }
   10163 
   10164 static bfd_boolean
   10165 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   10166 {
   10167   if (note->descsz <= 0x7c + 31)
   10168     return FALSE;
   10169 
   10170   /* Signal number at offset 0x08. */
   10171   elf_tdata (abfd)->core->signal
   10172     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   10173 
   10174   /* Process ID at offset 0x50. */
   10175   elf_tdata (abfd)->core->pid
   10176     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
   10177 
   10178   /* Command name at 0x7c (max 32 bytes, including nul). */
   10179   elf_tdata (abfd)->core->command
   10180     = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
   10181 
   10182   return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
   10183 					  note);
   10184 }
   10185 
   10186 
   10187 static bfd_boolean
   10188 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
   10189 {
   10190   int lwp;
   10191 
   10192   if (elfcore_netbsd_get_lwpid (note, &lwp))
   10193     elf_tdata (abfd)->core->lwpid = lwp;
   10194 
   10195   switch (note->type)
   10196     {
   10197     case NT_NETBSDCORE_PROCINFO:
   10198       /* NetBSD-specific core "procinfo".  Note that we expect to
   10199 	 find this note before any of the others, which is fine,
   10200 	 since the kernel writes this note out first when it
   10201 	 creates a core file.  */
   10202       return elfcore_grok_netbsd_procinfo (abfd, note);
   10203 
   10204     case NT_NETBSDCORE_AUXV:
   10205       /* NetBSD-specific Elf Auxiliary Vector data. */
   10206       return elfcore_make_auxv_note_section (abfd, note, 4);
   10207 
   10208     default:
   10209       break;
   10210     }
   10211 
   10212   /* As of March 2017 there are no other machine-independent notes
   10213      defined for NetBSD core files.  If the note type is less
   10214      than the start of the machine-dependent note types, we don't
   10215      understand it.  */
   10216 
   10217   if (note->type < NT_NETBSDCORE_FIRSTMACH)
   10218     return TRUE;
   10219 
   10220 
   10221   switch (bfd_get_arch (abfd))
   10222     {
   10223       /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
   10224 	 PT_GETFPREGS == mach+2.  */
   10225 
   10226     case bfd_arch_alpha:
   10227     case bfd_arch_sparc:
   10228       switch (note->type)
   10229 	{
   10230 	case NT_NETBSDCORE_FIRSTMACH+0:
   10231 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   10232 
   10233 	case NT_NETBSDCORE_FIRSTMACH+2:
   10234 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   10235 
   10236 	default:
   10237 	  return TRUE;
   10238 	}
   10239 
   10240       /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
   10241 	 There's also old PT___GETREGS40 == mach + 1 for old reg
   10242 	 structure which lacks GBR.  */
   10243 
   10244     case bfd_arch_sh:
   10245       switch (note->type)
   10246 	{
   10247 	case NT_NETBSDCORE_FIRSTMACH+3:
   10248 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   10249 
   10250 	case NT_NETBSDCORE_FIRSTMACH+5:
   10251 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   10252 
   10253 	default:
   10254 	  return TRUE;
   10255 	}
   10256 
   10257       /* On all other arch's, PT_GETREGS == mach+1 and
   10258 	 PT_GETFPREGS == mach+3.  */
   10259 
   10260     default:
   10261       switch (note->type)
   10262 	{
   10263 	case NT_NETBSDCORE_FIRSTMACH+1:
   10264 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   10265 
   10266 	case NT_NETBSDCORE_FIRSTMACH+3:
   10267 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   10268 
   10269 	default:
   10270 	  return TRUE;
   10271 	}
   10272     }
   10273     /* NOTREACHED */
   10274 }
   10275 
   10276 static bfd_boolean
   10277 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   10278 {
   10279   if (note->descsz <= 0x48 + 31)
   10280     return FALSE;
   10281 
   10282   /* Signal number at offset 0x08. */
   10283   elf_tdata (abfd)->core->signal
   10284     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   10285 
   10286   /* Process ID at offset 0x20. */
   10287   elf_tdata (abfd)->core->pid
   10288     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
   10289 
   10290   /* Command name at 0x48 (max 32 bytes, including nul). */
   10291   elf_tdata (abfd)->core->command
   10292     = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
   10293 
   10294   return TRUE;
   10295 }
   10296 
   10297 static bfd_boolean
   10298 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
   10299 {
   10300   if (note->type == NT_OPENBSD_PROCINFO)
   10301     return elfcore_grok_openbsd_procinfo (abfd, note);
   10302 
   10303   if (note->type == NT_OPENBSD_REGS)
   10304     return elfcore_make_note_pseudosection (abfd, ".reg", note);
   10305 
   10306   if (note->type == NT_OPENBSD_FPREGS)
   10307     return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   10308 
   10309   if (note->type == NT_OPENBSD_XFPREGS)
   10310     return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
   10311 
   10312   if (note->type == NT_OPENBSD_AUXV)
   10313     return elfcore_make_auxv_note_section (abfd, note, 0);
   10314 
   10315   if (note->type == NT_OPENBSD_WCOOKIE)
   10316     {
   10317       asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
   10318 							   SEC_HAS_CONTENTS);
   10319 
   10320       if (sect == NULL)
   10321 	return FALSE;
   10322       sect->size = note->descsz;
   10323       sect->filepos = note->descpos;
   10324       sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   10325 
   10326       return TRUE;
   10327     }
   10328 
   10329   return TRUE;
   10330 }
   10331 
   10332 static bfd_boolean
   10333 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
   10334 {
   10335   void *ddata = note->descdata;
   10336   char buf[100];
   10337   char *name;
   10338   asection *sect;
   10339   short sig;
   10340   unsigned flags;
   10341 
   10342   if (note->descsz < 16)
   10343     return FALSE;
   10344 
   10345   /* nto_procfs_status 'pid' field is at offset 0.  */
   10346   elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
   10347 
   10348   /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
   10349   *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
   10350 
   10351   /* nto_procfs_status 'flags' field is at offset 8.  */
   10352   flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
   10353 
   10354   /* nto_procfs_status 'what' field is at offset 14.  */
   10355   if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
   10356     {
   10357       elf_tdata (abfd)->core->signal = sig;
   10358       elf_tdata (abfd)->core->lwpid = *tid;
   10359     }
   10360 
   10361   /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
   10362      do not come from signals so we make sure we set the current
   10363      thread just in case.  */
   10364   if (flags & 0x00000080)
   10365     elf_tdata (abfd)->core->lwpid = *tid;
   10366 
   10367   /* Make a ".qnx_core_status/%d" section.  */
   10368   sprintf (buf, ".qnx_core_status/%ld", *tid);
   10369 
   10370   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   10371   if (name == NULL)
   10372     return FALSE;
   10373   strcpy (name, buf);
   10374 
   10375   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10376   if (sect == NULL)
   10377     return FALSE;
   10378 
   10379   sect->size		= note->descsz;
   10380   sect->filepos		= note->descpos;
   10381   sect->alignment_power = 2;
   10382 
   10383   return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
   10384 }
   10385 
   10386 static bfd_boolean
   10387 elfcore_grok_nto_regs (bfd *abfd,
   10388 		       Elf_Internal_Note *note,
   10389 		       long tid,
   10390 		       char *base)
   10391 {
   10392   char buf[100];
   10393   char *name;
   10394   asection *sect;
   10395 
   10396   /* Make a "(base)/%d" section.  */
   10397   sprintf (buf, "%s/%ld", base, tid);
   10398 
   10399   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   10400   if (name == NULL)
   10401     return FALSE;
   10402   strcpy (name, buf);
   10403 
   10404   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10405   if (sect == NULL)
   10406     return FALSE;
   10407 
   10408   sect->size		= note->descsz;
   10409   sect->filepos		= note->descpos;
   10410   sect->alignment_power = 2;
   10411 
   10412   /* This is the current thread.  */
   10413   if (elf_tdata (abfd)->core->lwpid == tid)
   10414     return elfcore_maybe_make_sect (abfd, base, sect);
   10415 
   10416   return TRUE;
   10417 }
   10418 
   10419 #define BFD_QNT_CORE_INFO	7
   10420 #define BFD_QNT_CORE_STATUS	8
   10421 #define BFD_QNT_CORE_GREG	9
   10422 #define BFD_QNT_CORE_FPREG	10
   10423 
   10424 static bfd_boolean
   10425 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
   10426 {
   10427   /* Every GREG section has a STATUS section before it.  Store the
   10428      tid from the previous call to pass down to the next gregs
   10429      function.  */
   10430   static long tid = 1;
   10431 
   10432   switch (note->type)
   10433     {
   10434     case BFD_QNT_CORE_INFO:
   10435       return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
   10436     case BFD_QNT_CORE_STATUS:
   10437       return elfcore_grok_nto_status (abfd, note, &tid);
   10438     case BFD_QNT_CORE_GREG:
   10439       return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
   10440     case BFD_QNT_CORE_FPREG:
   10441       return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
   10442     default:
   10443       return TRUE;
   10444     }
   10445 }
   10446 
   10447 static bfd_boolean
   10448 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
   10449 {
   10450   char *name;
   10451   asection *sect;
   10452   size_t len;
   10453 
   10454   /* Use note name as section name.  */
   10455   len = note->namesz;
   10456   name = (char *) bfd_alloc (abfd, len);
   10457   if (name == NULL)
   10458     return FALSE;
   10459   memcpy (name, note->namedata, len);
   10460   name[len - 1] = '\0';
   10461 
   10462   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10463   if (sect == NULL)
   10464     return FALSE;
   10465 
   10466   sect->size		= note->descsz;
   10467   sect->filepos		= note->descpos;
   10468   sect->alignment_power = 1;
   10469 
   10470   return TRUE;
   10471 }
   10472 
   10473 /* Function: elfcore_write_note
   10474 
   10475    Inputs:
   10476      buffer to hold note, and current size of buffer
   10477      name of note
   10478      type of note
   10479      data for note
   10480      size of data for note
   10481 
   10482    Writes note to end of buffer.  ELF64 notes are written exactly as
   10483    for ELF32, despite the current (as of 2006) ELF gabi specifying
   10484    that they ought to have 8-byte namesz and descsz field, and have
   10485    8-byte alignment.  Other writers, eg. Linux kernel, do the same.
   10486 
   10487    Return:
   10488    Pointer to realloc'd buffer, *BUFSIZ updated.  */
   10489 
   10490 char *
   10491 elfcore_write_note (bfd *abfd,
   10492 		    char *buf,
   10493 		    int *bufsiz,
   10494 		    const char *name,
   10495 		    int type,
   10496 		    const void *input,
   10497 		    int size)
   10498 {
   10499   Elf_External_Note *xnp;
   10500   size_t namesz;
   10501   size_t newspace;
   10502   char *dest;
   10503 
   10504   namesz = 0;
   10505   if (name != NULL)
   10506     namesz = strlen (name) + 1;
   10507 
   10508   newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
   10509 
   10510   buf = (char *) realloc (buf, *bufsiz + newspace);
   10511   if (buf == NULL)
   10512     return buf;
   10513   dest = buf + *bufsiz;
   10514   *bufsiz += newspace;
   10515   xnp = (Elf_External_Note *) dest;
   10516   H_PUT_32 (abfd, namesz, xnp->namesz);
   10517   H_PUT_32 (abfd, size, xnp->descsz);
   10518   H_PUT_32 (abfd, type, xnp->type);
   10519   dest = xnp->name;
   10520   if (name != NULL)
   10521     {
   10522       memcpy (dest, name, namesz);
   10523       dest += namesz;
   10524       while (namesz & 3)
   10525 	{
   10526 	  *dest++ = '\0';
   10527 	  ++namesz;
   10528 	}
   10529     }
   10530   memcpy (dest, input, size);
   10531   dest += size;
   10532   while (size & 3)
   10533     {
   10534       *dest++ = '\0';
   10535       ++size;
   10536     }
   10537   return buf;
   10538 }
   10539 
   10540 /* gcc-8 warns (*) on all the strncpy calls in this function about
   10541    possible string truncation.  The "truncation" is not a bug.  We
   10542    have an external representation of structs with fields that are not
   10543    necessarily NULL terminated and corresponding internal
   10544    representation fields that are one larger so that they can always
   10545    be NULL terminated.
   10546    gcc versions between 4.2 and 4.6 do not allow pragma control of
   10547    diagnostics inside functions, giving a hard error if you try to use
   10548    the finer control available with later versions.
   10549    gcc prior to 4.2 warns about diagnostic push and pop.
   10550    gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
   10551    unless you also add #pragma GCC diagnostic ignored "-Wpragma".
   10552    (*) Depending on your system header files!  */
   10553 #if GCC_VERSION >= 8000
   10554 # pragma GCC diagnostic push
   10555 # pragma GCC diagnostic ignored "-Wstringop-truncation"
   10556 #endif
   10557 char *
   10558 elfcore_write_prpsinfo (bfd  *abfd,
   10559 			char *buf,
   10560 			int  *bufsiz,
   10561 			const char *fname,
   10562 			const char *psargs)
   10563 {
   10564   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   10565 
   10566   if (bed->elf_backend_write_core_note != NULL)
   10567     {
   10568       char *ret;
   10569       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   10570 						 NT_PRPSINFO, fname, psargs);
   10571       if (ret != NULL)
   10572 	return ret;
   10573     }
   10574 
   10575 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   10576 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   10577   if (bed->s->elfclass == ELFCLASS32)
   10578     {
   10579 #  if defined (HAVE_PSINFO32_T)
   10580       psinfo32_t data;
   10581       int note_type = NT_PSINFO;
   10582 #  else
   10583       prpsinfo32_t data;
   10584       int note_type = NT_PRPSINFO;
   10585 #  endif
   10586 
   10587       memset (&data, 0, sizeof (data));
   10588       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   10589       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   10590       return elfcore_write_note (abfd, buf, bufsiz,
   10591 				 "CORE", note_type, &data, sizeof (data));
   10592     }
   10593   else
   10594 # endif
   10595     {
   10596 # if defined (HAVE_PSINFO_T)
   10597       psinfo_t data;
   10598       int note_type = NT_PSINFO;
   10599 # else
   10600       prpsinfo_t data;
   10601       int note_type = NT_PRPSINFO;
   10602 # endif
   10603 
   10604       memset (&data, 0, sizeof (data));
   10605       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   10606       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   10607       return elfcore_write_note (abfd, buf, bufsiz,
   10608 				 "CORE", note_type, &data, sizeof (data));
   10609     }
   10610 #endif	/* PSINFO_T or PRPSINFO_T */
   10611 
   10612   free (buf);
   10613   return NULL;
   10614 }
   10615 #if GCC_VERSION >= 8000
   10616 # pragma GCC diagnostic pop
   10617 #endif
   10618 
   10619 char *
   10620 elfcore_write_linux_prpsinfo32
   10621   (bfd *abfd, char *buf, int *bufsiz,
   10622    const struct elf_internal_linux_prpsinfo *prpsinfo)
   10623 {
   10624   if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
   10625     {
   10626       struct elf_external_linux_prpsinfo32_ugid16 data;
   10627 
   10628       swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
   10629       return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
   10630 				 &data, sizeof (data));
   10631     }
   10632   else
   10633     {
   10634       struct elf_external_linux_prpsinfo32_ugid32 data;
   10635 
   10636       swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
   10637       return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
   10638 				 &data, sizeof (data));
   10639     }
   10640 }
   10641 
   10642 char *
   10643 elfcore_write_linux_prpsinfo64
   10644   (bfd *abfd, char *buf, int *bufsiz,
   10645    const struct elf_internal_linux_prpsinfo *prpsinfo)
   10646 {
   10647   if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
   10648     {
   10649       struct elf_external_linux_prpsinfo64_ugid16 data;
   10650 
   10651       swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
   10652       return elfcore_write_note (abfd, buf, bufsiz,
   10653 				 "CORE", NT_PRPSINFO, &data, sizeof (data));
   10654     }
   10655   else
   10656     {
   10657       struct elf_external_linux_prpsinfo64_ugid32 data;
   10658 
   10659       swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
   10660       return elfcore_write_note (abfd, buf, bufsiz,
   10661 				 "CORE", NT_PRPSINFO, &data, sizeof (data));
   10662     }
   10663 }
   10664 
   10665 char *
   10666 elfcore_write_prstatus (bfd *abfd,
   10667 			char *buf,
   10668 			int *bufsiz,
   10669 			long pid,
   10670 			int cursig,
   10671 			const void *gregs)
   10672 {
   10673   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   10674 
   10675   if (bed->elf_backend_write_core_note != NULL)
   10676     {
   10677       char *ret;
   10678       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   10679 						 NT_PRSTATUS,
   10680 						 pid, cursig, gregs);
   10681       if (ret != NULL)
   10682 	return ret;
   10683     }
   10684 
   10685 #if defined (HAVE_PRSTATUS_T)
   10686 #if defined (HAVE_PRSTATUS32_T)
   10687   if (bed->s->elfclass == ELFCLASS32)
   10688     {
   10689       prstatus32_t prstat;
   10690 
   10691       memset (&prstat, 0, sizeof (prstat));
   10692       prstat.pr_pid = pid;
   10693       prstat.pr_cursig = cursig;
   10694       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   10695       return elfcore_write_note (abfd, buf, bufsiz, "CORE",
   10696 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   10697     }
   10698   else
   10699 #endif
   10700     {
   10701       prstatus_t prstat;
   10702 
   10703       memset (&prstat, 0, sizeof (prstat));
   10704       prstat.pr_pid = pid;
   10705       prstat.pr_cursig = cursig;
   10706       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   10707       return elfcore_write_note (abfd, buf, bufsiz, "CORE",
   10708 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   10709     }
   10710 #endif /* HAVE_PRSTATUS_T */
   10711 
   10712   free (buf);
   10713   return NULL;
   10714 }
   10715 
   10716 #if defined (HAVE_LWPSTATUS_T)
   10717 char *
   10718 elfcore_write_lwpstatus (bfd *abfd,
   10719 			 char *buf,
   10720 			 int *bufsiz,
   10721 			 long pid,
   10722 			 int cursig,
   10723 			 const void *gregs)
   10724 {
   10725   lwpstatus_t lwpstat;
   10726   const char *note_name = "CORE";
   10727 
   10728   memset (&lwpstat, 0, sizeof (lwpstat));
   10729   lwpstat.pr_lwpid  = pid >> 16;
   10730   lwpstat.pr_cursig = cursig;
   10731 #if defined (HAVE_LWPSTATUS_T_PR_REG)
   10732   memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
   10733 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   10734 #if !defined(gregs)
   10735   memcpy (lwpstat.pr_context.uc_mcontext.gregs,
   10736 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
   10737 #else
   10738   memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
   10739 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
   10740 #endif
   10741 #endif
   10742   return elfcore_write_note (abfd, buf, bufsiz, note_name,
   10743 			     NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
   10744 }
   10745 #endif /* HAVE_LWPSTATUS_T */
   10746 
   10747 #if defined (HAVE_PSTATUS_T)
   10748 char *
   10749 elfcore_write_pstatus (bfd *abfd,
   10750 		       char *buf,
   10751 		       int *bufsiz,
   10752 		       long pid,
   10753 		       int cursig ATTRIBUTE_UNUSED,
   10754 		       const void *gregs ATTRIBUTE_UNUSED)
   10755 {
   10756   const char *note_name = "CORE";
   10757 #if defined (HAVE_PSTATUS32_T)
   10758   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   10759 
   10760   if (bed->s->elfclass == ELFCLASS32)
   10761     {
   10762       pstatus32_t pstat;
   10763 
   10764       memset (&pstat, 0, sizeof (pstat));
   10765       pstat.pr_pid = pid & 0xffff;
   10766       buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
   10767 				NT_PSTATUS, &pstat, sizeof (pstat));
   10768       return buf;
   10769     }
   10770   else
   10771 #endif
   10772     {
   10773       pstatus_t pstat;
   10774 
   10775       memset (&pstat, 0, sizeof (pstat));
   10776       pstat.pr_pid = pid & 0xffff;
   10777       buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
   10778 				NT_PSTATUS, &pstat, sizeof (pstat));
   10779       return buf;
   10780     }
   10781 }
   10782 #endif /* HAVE_PSTATUS_T */
   10783 
   10784 char *
   10785 elfcore_write_prfpreg (bfd *abfd,
   10786 		       char *buf,
   10787 		       int *bufsiz,
   10788 		       const void *fpregs,
   10789 		       int size)
   10790 {
   10791   const char *note_name = "CORE";
   10792   return elfcore_write_note (abfd, buf, bufsiz,
   10793 			     note_name, NT_FPREGSET, fpregs, size);
   10794 }
   10795 
   10796 char *
   10797 elfcore_write_prxfpreg (bfd *abfd,
   10798 			char *buf,
   10799 			int *bufsiz,
   10800 			const void *xfpregs,
   10801 			int size)
   10802 {
   10803   char *note_name = "LINUX";
   10804   return elfcore_write_note (abfd, buf, bufsiz,
   10805 			     note_name, NT_PRXFPREG, xfpregs, size);
   10806 }
   10807 
   10808 char *
   10809 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
   10810 			 const void *xfpregs, int size)
   10811 {
   10812   char *note_name;
   10813   if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
   10814     note_name = "FreeBSD";
   10815   else
   10816     note_name = "LINUX";
   10817   return elfcore_write_note (abfd, buf, bufsiz,
   10818 			     note_name, NT_X86_XSTATE, xfpregs, size);
   10819 }
   10820 
   10821 char *
   10822 elfcore_write_ppc_vmx (bfd *abfd,
   10823 		       char *buf,
   10824 		       int *bufsiz,
   10825 		       const void *ppc_vmx,
   10826 		       int size)
   10827 {
   10828   char *note_name = "LINUX";
   10829   return elfcore_write_note (abfd, buf, bufsiz,
   10830 			     note_name, NT_PPC_VMX, ppc_vmx, size);
   10831 }
   10832 
   10833 char *
   10834 elfcore_write_ppc_vsx (bfd *abfd,
   10835 		       char *buf,
   10836 		       int *bufsiz,
   10837 		       const void *ppc_vsx,
   10838 		       int size)
   10839 {
   10840   char *note_name = "LINUX";
   10841   return elfcore_write_note (abfd, buf, bufsiz,
   10842 			     note_name, NT_PPC_VSX, ppc_vsx, size);
   10843 }
   10844 
   10845 static char *
   10846 elfcore_write_s390_high_gprs (bfd *abfd,
   10847 			      char *buf,
   10848 			      int *bufsiz,
   10849 			      const void *s390_high_gprs,
   10850 			      int size)
   10851 {
   10852   char *note_name = "LINUX";
   10853   return elfcore_write_note (abfd, buf, bufsiz,
   10854 			     note_name, NT_S390_HIGH_GPRS,
   10855 			     s390_high_gprs, size);
   10856 }
   10857 
   10858 char *
   10859 elfcore_write_s390_timer (bfd *abfd,
   10860 			  char *buf,
   10861 			  int *bufsiz,
   10862 			  const void *s390_timer,
   10863 			  int size)
   10864 {
   10865   char *note_name = "LINUX";
   10866   return elfcore_write_note (abfd, buf, bufsiz,
   10867 			     note_name, NT_S390_TIMER, s390_timer, size);
   10868 }
   10869 
   10870 char *
   10871 elfcore_write_s390_todcmp (bfd *abfd,
   10872 			   char *buf,
   10873 			   int *bufsiz,
   10874 			   const void *s390_todcmp,
   10875 			   int size)
   10876 {
   10877   char *note_name = "LINUX";
   10878   return elfcore_write_note (abfd, buf, bufsiz,
   10879 			     note_name, NT_S390_TODCMP, s390_todcmp, size);
   10880 }
   10881 
   10882 char *
   10883 elfcore_write_s390_todpreg (bfd *abfd,
   10884 			    char *buf,
   10885 			    int *bufsiz,
   10886 			    const void *s390_todpreg,
   10887 			    int size)
   10888 {
   10889   char *note_name = "LINUX";
   10890   return elfcore_write_note (abfd, buf, bufsiz,
   10891 			     note_name, NT_S390_TODPREG, s390_todpreg, size);
   10892 }
   10893 
   10894 char *
   10895 elfcore_write_s390_ctrs (bfd *abfd,
   10896 			 char *buf,
   10897 			 int *bufsiz,
   10898 			 const void *s390_ctrs,
   10899 			 int size)
   10900 {
   10901   char *note_name = "LINUX";
   10902   return elfcore_write_note (abfd, buf, bufsiz,
   10903 			     note_name, NT_S390_CTRS, s390_ctrs, size);
   10904 }
   10905 
   10906 char *
   10907 elfcore_write_s390_prefix (bfd *abfd,
   10908 			   char *buf,
   10909 			   int *bufsiz,
   10910 			   const void *s390_prefix,
   10911 			   int size)
   10912 {
   10913   char *note_name = "LINUX";
   10914   return elfcore_write_note (abfd, buf, bufsiz,
   10915 			     note_name, NT_S390_PREFIX, s390_prefix, size);
   10916 }
   10917 
   10918 char *
   10919 elfcore_write_s390_last_break (bfd *abfd,
   10920 			       char *buf,
   10921 			       int *bufsiz,
   10922 			       const void *s390_last_break,
   10923 			       int size)
   10924 {
   10925   char *note_name = "LINUX";
   10926   return elfcore_write_note (abfd, buf, bufsiz,
   10927 			     note_name, NT_S390_LAST_BREAK,
   10928 			     s390_last_break, size);
   10929 }
   10930 
   10931 char *
   10932 elfcore_write_s390_system_call (bfd *abfd,
   10933 				char *buf,
   10934 				int *bufsiz,
   10935 				const void *s390_system_call,
   10936 				int size)
   10937 {
   10938   char *note_name = "LINUX";
   10939   return elfcore_write_note (abfd, buf, bufsiz,
   10940 			     note_name, NT_S390_SYSTEM_CALL,
   10941 			     s390_system_call, size);
   10942 }
   10943 
   10944 char *
   10945 elfcore_write_s390_tdb (bfd *abfd,
   10946 			char *buf,
   10947 			int *bufsiz,
   10948 			const void *s390_tdb,
   10949 			int size)
   10950 {
   10951   char *note_name = "LINUX";
   10952   return elfcore_write_note (abfd, buf, bufsiz,
   10953 			     note_name, NT_S390_TDB, s390_tdb, size);
   10954 }
   10955 
   10956 char *
   10957 elfcore_write_s390_vxrs_low (bfd *abfd,
   10958 			     char *buf,
   10959 			     int *bufsiz,
   10960 			     const void *s390_vxrs_low,
   10961 			     int size)
   10962 {
   10963   char *note_name = "LINUX";
   10964   return elfcore_write_note (abfd, buf, bufsiz,
   10965 			     note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
   10966 }
   10967 
   10968 char *
   10969 elfcore_write_s390_vxrs_high (bfd *abfd,
   10970 			     char *buf,
   10971 			     int *bufsiz,
   10972 			     const void *s390_vxrs_high,
   10973 			     int size)
   10974 {
   10975   char *note_name = "LINUX";
   10976   return elfcore_write_note (abfd, buf, bufsiz,
   10977 			     note_name, NT_S390_VXRS_HIGH,
   10978 			     s390_vxrs_high, size);
   10979 }
   10980 
   10981 char *
   10982 elfcore_write_s390_gs_cb (bfd *abfd,
   10983 			  char *buf,
   10984 			  int *bufsiz,
   10985 			  const void *s390_gs_cb,
   10986 			  int size)
   10987 {
   10988   char *note_name = "LINUX";
   10989   return elfcore_write_note (abfd, buf, bufsiz,
   10990 			     note_name, NT_S390_GS_CB,
   10991 			     s390_gs_cb, size);
   10992 }
   10993 
   10994 char *
   10995 elfcore_write_s390_gs_bc (bfd *abfd,
   10996 			  char *buf,
   10997 			  int *bufsiz,
   10998 			  const void *s390_gs_bc,
   10999 			  int size)
   11000 {
   11001   char *note_name = "LINUX";
   11002   return elfcore_write_note (abfd, buf, bufsiz,
   11003 			     note_name, NT_S390_GS_BC,
   11004 			     s390_gs_bc, size);
   11005 }
   11006 
   11007 char *
   11008 elfcore_write_arm_vfp (bfd *abfd,
   11009 		       char *buf,
   11010 		       int *bufsiz,
   11011 		       const void *arm_vfp,
   11012 		       int size)
   11013 {
   11014   char *note_name = "LINUX";
   11015   return elfcore_write_note (abfd, buf, bufsiz,
   11016 			     note_name, NT_ARM_VFP, arm_vfp, size);
   11017 }
   11018 
   11019 char *
   11020 elfcore_write_aarch_tls (bfd *abfd,
   11021 		       char *buf,
   11022 		       int *bufsiz,
   11023 		       const void *aarch_tls,
   11024 		       int size)
   11025 {
   11026   char *note_name = "LINUX";
   11027   return elfcore_write_note (abfd, buf, bufsiz,
   11028 			     note_name, NT_ARM_TLS, aarch_tls, size);
   11029 }
   11030 
   11031 char *
   11032 elfcore_write_aarch_hw_break (bfd *abfd,
   11033 			    char *buf,
   11034 			    int *bufsiz,
   11035 			    const void *aarch_hw_break,
   11036 			    int size)
   11037 {
   11038   char *note_name = "LINUX";
   11039   return elfcore_write_note (abfd, buf, bufsiz,
   11040 			     note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
   11041 }
   11042 
   11043 char *
   11044 elfcore_write_aarch_hw_watch (bfd *abfd,
   11045 			    char *buf,
   11046 			    int *bufsiz,
   11047 			    const void *aarch_hw_watch,
   11048 			    int size)
   11049 {
   11050   char *note_name = "LINUX";
   11051   return elfcore_write_note (abfd, buf, bufsiz,
   11052 			     note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
   11053 }
   11054 
   11055 char *
   11056 elfcore_write_register_note (bfd *abfd,
   11057 			     char *buf,
   11058 			     int *bufsiz,
   11059 			     const char *section,
   11060 			     const void *data,
   11061 			     int size)
   11062 {
   11063   if (strcmp (section, ".reg2") == 0)
   11064     return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
   11065   if (strcmp (section, ".reg-xfp") == 0)
   11066     return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
   11067   if (strcmp (section, ".reg-xstate") == 0)
   11068     return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
   11069   if (strcmp (section, ".reg-ppc-vmx") == 0)
   11070     return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
   11071   if (strcmp (section, ".reg-ppc-vsx") == 0)
   11072     return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
   11073   if (strcmp (section, ".reg-s390-high-gprs") == 0)
   11074     return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
   11075   if (strcmp (section, ".reg-s390-timer") == 0)
   11076     return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
   11077   if (strcmp (section, ".reg-s390-todcmp") == 0)
   11078     return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
   11079   if (strcmp (section, ".reg-s390-todpreg") == 0)
   11080     return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
   11081   if (strcmp (section, ".reg-s390-ctrs") == 0)
   11082     return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
   11083   if (strcmp (section, ".reg-s390-prefix") == 0)
   11084     return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
   11085   if (strcmp (section, ".reg-s390-last-break") == 0)
   11086     return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
   11087   if (strcmp (section, ".reg-s390-system-call") == 0)
   11088     return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
   11089   if (strcmp (section, ".reg-s390-tdb") == 0)
   11090     return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
   11091   if (strcmp (section, ".reg-s390-vxrs-low") == 0)
   11092     return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
   11093   if (strcmp (section, ".reg-s390-vxrs-high") == 0)
   11094     return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
   11095   if (strcmp (section, ".reg-s390-gs-cb") == 0)
   11096     return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
   11097   if (strcmp (section, ".reg-s390-gs-bc") == 0)
   11098     return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
   11099   if (strcmp (section, ".reg-arm-vfp") == 0)
   11100     return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
   11101   if (strcmp (section, ".reg-aarch-tls") == 0)
   11102     return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
   11103   if (strcmp (section, ".reg-aarch-hw-break") == 0)
   11104     return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
   11105   if (strcmp (section, ".reg-aarch-hw-watch") == 0)
   11106     return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
   11107   return NULL;
   11108 }
   11109 
   11110 static bfd_boolean
   11111 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
   11112 		 size_t align)
   11113 {
   11114   char *p;
   11115 
   11116   /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
   11117      gABI specifies that PT_NOTE alignment should be aligned to 4
   11118      bytes for 32-bit objects and to 8 bytes for 64-bit objects.  If
   11119      align is less than 4, we use 4 byte alignment.   */
   11120   if (align < 4)
   11121     align = 4;
   11122   if (align != 4 && align != 8)
   11123     return FALSE;
   11124 
   11125   p = buf;
   11126   while (p < buf + size)
   11127     {
   11128       Elf_External_Note *xnp = (Elf_External_Note *) p;
   11129       Elf_Internal_Note in;
   11130 
   11131       if (offsetof (Elf_External_Note, name) > buf - p + size)
   11132 	return FALSE;
   11133 
   11134       in.type = H_GET_32 (abfd, xnp->type);
   11135 
   11136       in.namesz = H_GET_32 (abfd, xnp->namesz);
   11137       in.namedata = xnp->name;
   11138       if (in.namesz > buf - in.namedata + size)
   11139 	return FALSE;
   11140 
   11141       in.descsz = H_GET_32 (abfd, xnp->descsz);
   11142       in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
   11143       in.descpos = offset + (in.descdata - buf);
   11144       if (in.descsz != 0
   11145 	  && (in.descdata >= buf + size
   11146 	      || in.descsz > buf - in.descdata + size))
   11147 	return FALSE;
   11148 
   11149       switch (bfd_get_format (abfd))
   11150 	{
   11151 	default:
   11152 	  return TRUE;
   11153 
   11154 	case bfd_core:
   11155 	  {
   11156 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
   11157 	    struct
   11158 	    {
   11159 	      const char * string;
   11160 	      size_t len;
   11161 	      bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
   11162 	    }
   11163 	    grokers[] =
   11164 	    {
   11165 	      GROKER_ELEMENT ("", elfcore_grok_note),
   11166 	      GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
   11167 	      GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
   11168 	      GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
   11169 	      GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
   11170 	      GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note)
   11171 	    };
   11172 #undef GROKER_ELEMENT
   11173 	    int i;
   11174 
   11175 	    for (i = ARRAY_SIZE (grokers); i--;)
   11176 	      {
   11177 		if (in.namesz >= grokers[i].len
   11178 		    && strncmp (in.namedata, grokers[i].string,
   11179 				grokers[i].len) == 0)
   11180 		  {
   11181 		    if (! grokers[i].func (abfd, & in))
   11182 		      return FALSE;
   11183 		    break;
   11184 		  }
   11185 	      }
   11186 	    break;
   11187 	  }
   11188 
   11189 	case bfd_object:
   11190 	  if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
   11191 	    {
   11192 	      if (! elfobj_grok_gnu_note (abfd, &in))
   11193 		return FALSE;
   11194 	    }
   11195 	  else if (in.namesz == sizeof "stapsdt"
   11196 		   && strcmp (in.namedata, "stapsdt") == 0)
   11197 	    {
   11198 	      if (! elfobj_grok_stapsdt_note (abfd, &in))
   11199 		return FALSE;
   11200 	    }
   11201 	  break;
   11202 	}
   11203 
   11204       p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
   11205     }
   11206 
   11207   return TRUE;
   11208 }
   11209 
   11210 static bfd_boolean
   11211 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
   11212 		size_t align)
   11213 {
   11214   char *buf;
   11215 
   11216   if (size == 0 || (size + 1) == 0)
   11217     return TRUE;
   11218 
   11219   if (bfd_seek (abfd, offset, SEEK_SET) != 0)
   11220     return FALSE;
   11221 
   11222   buf = (char *) bfd_malloc (size + 1);
   11223   if (buf == NULL)
   11224     return FALSE;
   11225 
   11226   /* PR 17512: file: ec08f814
   11227      0-termintate the buffer so that string searches will not overflow.  */
   11228   buf[size] = 0;
   11229 
   11230   if (bfd_bread (buf, size, abfd) != size
   11231       || !elf_parse_notes (abfd, buf, size, offset, align))
   11232     {
   11233       free (buf);
   11234       return FALSE;
   11235     }
   11236 
   11237   free (buf);
   11238   return TRUE;
   11239 }
   11240 
   11241 /* Providing external access to the ELF program header table.  */
   11243 
   11244 /* Return an upper bound on the number of bytes required to store a
   11245    copy of ABFD's program header table entries.  Return -1 if an error
   11246    occurs; bfd_get_error will return an appropriate code.  */
   11247 
   11248 long
   11249 bfd_get_elf_phdr_upper_bound (bfd *abfd)
   11250 {
   11251   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   11252     {
   11253       bfd_set_error (bfd_error_wrong_format);
   11254       return -1;
   11255     }
   11256 
   11257   return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
   11258 }
   11259 
   11260 /* Copy ABFD's program header table entries to *PHDRS.  The entries
   11261    will be stored as an array of Elf_Internal_Phdr structures, as
   11262    defined in include/elf/internal.h.  To find out how large the
   11263    buffer needs to be, call bfd_get_elf_phdr_upper_bound.
   11264 
   11265    Return the number of program header table entries read, or -1 if an
   11266    error occurs; bfd_get_error will return an appropriate code.  */
   11267 
   11268 int
   11269 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
   11270 {
   11271   int num_phdrs;
   11272 
   11273   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   11274     {
   11275       bfd_set_error (bfd_error_wrong_format);
   11276       return -1;
   11277     }
   11278 
   11279   num_phdrs = elf_elfheader (abfd)->e_phnum;
   11280   memcpy (phdrs, elf_tdata (abfd)->phdr,
   11281 	  num_phdrs * sizeof (Elf_Internal_Phdr));
   11282 
   11283   return num_phdrs;
   11284 }
   11285 
   11286 enum elf_reloc_type_class
   11287 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   11288 			   const asection *rel_sec ATTRIBUTE_UNUSED,
   11289 			   const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
   11290 {
   11291   return reloc_class_normal;
   11292 }
   11293 
   11294 /* For RELA architectures, return the relocation value for a
   11295    relocation against a local symbol.  */
   11296 
   11297 bfd_vma
   11298 _bfd_elf_rela_local_sym (bfd *abfd,
   11299 			 Elf_Internal_Sym *sym,
   11300 			 asection **psec,
   11301 			 Elf_Internal_Rela *rel)
   11302 {
   11303   asection *sec = *psec;
   11304   bfd_vma relocation;
   11305 
   11306   relocation = (sec->output_section->vma
   11307 		+ sec->output_offset
   11308 		+ sym->st_value);
   11309   if ((sec->flags & SEC_MERGE)
   11310       && ELF_ST_TYPE (sym->st_info) == STT_SECTION
   11311       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
   11312     {
   11313       rel->r_addend =
   11314 	_bfd_merged_section_offset (abfd, psec,
   11315 				    elf_section_data (sec)->sec_info,
   11316 				    sym->st_value + rel->r_addend);
   11317       if (sec != *psec)
   11318 	{
   11319 	  /* If we have changed the section, and our original section is
   11320 	     marked with SEC_EXCLUDE, it means that the original
   11321 	     SEC_MERGE section has been completely subsumed in some
   11322 	     other SEC_MERGE section.  In this case, we need to leave
   11323 	     some info around for --emit-relocs.  */
   11324 	  if ((sec->flags & SEC_EXCLUDE) != 0)
   11325 	    sec->kept_section = *psec;
   11326 	  sec = *psec;
   11327 	}
   11328       rel->r_addend -= relocation;
   11329       rel->r_addend += sec->output_section->vma + sec->output_offset;
   11330     }
   11331   return relocation;
   11332 }
   11333 
   11334 bfd_vma
   11335 _bfd_elf_rel_local_sym (bfd *abfd,
   11336 			Elf_Internal_Sym *sym,
   11337 			asection **psec,
   11338 			bfd_vma addend)
   11339 {
   11340   asection *sec = *psec;
   11341 
   11342   if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
   11343     return sym->st_value + addend;
   11344 
   11345   return _bfd_merged_section_offset (abfd, psec,
   11346 				     elf_section_data (sec)->sec_info,
   11347 				     sym->st_value + addend);
   11348 }
   11349 
   11350 /* Adjust an address within a section.  Given OFFSET within SEC, return
   11351    the new offset within the section, based upon changes made to the
   11352    section.  Returns -1 if the offset is now invalid.
   11353    The offset (in abnd out) is in target sized bytes, however big a
   11354    byte may be.  */
   11355 
   11356 bfd_vma
   11357 _bfd_elf_section_offset (bfd *abfd,
   11358 			 struct bfd_link_info *info,
   11359 			 asection *sec,
   11360 			 bfd_vma offset)
   11361 {
   11362   switch (sec->sec_info_type)
   11363     {
   11364     case SEC_INFO_TYPE_STABS:
   11365       return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
   11366 				       offset);
   11367     case SEC_INFO_TYPE_EH_FRAME:
   11368       return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
   11369 
   11370     default:
   11371       if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
   11372 	{
   11373 	  /* Reverse the offset.  */
   11374 	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11375 	  bfd_size_type address_size = bed->s->arch_size / 8;
   11376 
   11377 	  /* address_size and sec->size are in octets.  Convert
   11378 	     to bytes before subtracting the original offset.  */
   11379 	  offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset;
   11380 	}
   11381       return offset;
   11382     }
   11383 }
   11384 
   11385 /* Create a new BFD as if by bfd_openr.  Rather than opening a file,
   11387    reconstruct an ELF file by reading the segments out of remote memory
   11388    based on the ELF file header at EHDR_VMA and the ELF program headers it
   11389    points to.  If not null, *LOADBASEP is filled in with the difference
   11390    between the VMAs from which the segments were read, and the VMAs the
   11391    file headers (and hence BFD's idea of each section's VMA) put them at.
   11392 
   11393    The function TARGET_READ_MEMORY is called to copy LEN bytes from the
   11394    remote memory at target address VMA into the local buffer at MYADDR; it
   11395    should return zero on success or an `errno' code on failure.  TEMPL must
   11396    be a BFD for an ELF target with the word size and byte order found in
   11397    the remote memory.  */
   11398 
   11399 bfd *
   11400 bfd_elf_bfd_from_remote_memory
   11401   (bfd *templ,
   11402    bfd_vma ehdr_vma,
   11403    bfd_size_type size,
   11404    bfd_vma *loadbasep,
   11405    int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
   11406 {
   11407   return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
   11408     (templ, ehdr_vma, size, loadbasep, target_read_memory);
   11409 }
   11410 
   11411 long
   11413 _bfd_elf_get_synthetic_symtab (bfd *abfd,
   11414 			       long symcount ATTRIBUTE_UNUSED,
   11415 			       asymbol **syms ATTRIBUTE_UNUSED,
   11416 			       long dynsymcount,
   11417 			       asymbol **dynsyms,
   11418 			       asymbol **ret)
   11419 {
   11420   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11421   asection *relplt;
   11422   asymbol *s;
   11423   const char *relplt_name;
   11424   bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
   11425   arelent *p;
   11426   long count, i, n;
   11427   size_t size;
   11428   Elf_Internal_Shdr *hdr;
   11429   char *names;
   11430   asection *plt;
   11431 
   11432   *ret = NULL;
   11433 
   11434   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
   11435     return 0;
   11436 
   11437   if (dynsymcount <= 0)
   11438     return 0;
   11439 
   11440   if (!bed->plt_sym_val)
   11441     return 0;
   11442 
   11443   relplt_name = bed->relplt_name;
   11444   if (relplt_name == NULL)
   11445     relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
   11446   relplt = bfd_get_section_by_name (abfd, relplt_name);
   11447   if (relplt == NULL)
   11448     return 0;
   11449 
   11450   hdr = &elf_section_data (relplt)->this_hdr;
   11451   if (hdr->sh_link != elf_dynsymtab (abfd)
   11452       || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
   11453     return 0;
   11454 
   11455   plt = bfd_get_section_by_name (abfd, ".plt");
   11456   if (plt == NULL)
   11457     return 0;
   11458 
   11459   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   11460   if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
   11461     return -1;
   11462 
   11463   count = relplt->size / hdr->sh_entsize;
   11464   size = count * sizeof (asymbol);
   11465   p = relplt->relocation;
   11466   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   11467     {
   11468       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
   11469       if (p->addend != 0)
   11470 	{
   11471 #ifdef BFD64
   11472 	  size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
   11473 #else
   11474 	  size += sizeof ("+0x") - 1 + 8;
   11475 #endif
   11476 	}
   11477     }
   11478 
   11479   s = *ret = (asymbol *) bfd_malloc (size);
   11480   if (s == NULL)
   11481     return -1;
   11482 
   11483   names = (char *) (s + count);
   11484   p = relplt->relocation;
   11485   n = 0;
   11486   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   11487     {
   11488       size_t len;
   11489       bfd_vma addr;
   11490 
   11491       addr = bed->plt_sym_val (i, plt, p);
   11492       if (addr == (bfd_vma) -1)
   11493 	continue;
   11494 
   11495       *s = **p->sym_ptr_ptr;
   11496       /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   11497 	 we are defining a symbol, ensure one of them is set.  */
   11498       if ((s->flags & BSF_LOCAL) == 0)
   11499 	s->flags |= BSF_GLOBAL;
   11500       s->flags |= BSF_SYNTHETIC;
   11501       s->section = plt;
   11502       s->value = addr - plt->vma;
   11503       s->name = names;
   11504       s->udata.p = NULL;
   11505       len = strlen ((*p->sym_ptr_ptr)->name);
   11506       memcpy (names, (*p->sym_ptr_ptr)->name, len);
   11507       names += len;
   11508       if (p->addend != 0)
   11509 	{
   11510 	  char buf[30], *a;
   11511 
   11512 	  memcpy (names, "+0x", sizeof ("+0x") - 1);
   11513 	  names += sizeof ("+0x") - 1;
   11514 	  bfd_sprintf_vma (abfd, buf, p->addend);
   11515 	  for (a = buf; *a == '0'; ++a)
   11516 	    ;
   11517 	  len = strlen (a);
   11518 	  memcpy (names, a, len);
   11519 	  names += len;
   11520 	}
   11521       memcpy (names, "@plt", sizeof ("@plt"));
   11522       names += sizeof ("@plt");
   11523       ++s, ++n;
   11524     }
   11525 
   11526   return n;
   11527 }
   11528 
   11529 /* It is only used by x86-64 so far.
   11530    ??? This repeats *COM* id of zero.  sec->id is supposed to be unique,
   11531    but current usage would allow all of _bfd_std_section to be zero.  */
   11532 static const asymbol lcomm_sym
   11533   = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
   11534 asection _bfd_elf_large_com_section
   11535   = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
   11536 		      "LARGE_COMMON", 0, SEC_IS_COMMON);
   11537 
   11538 void
   11539 _bfd_elf_post_process_headers (bfd * abfd,
   11540 			       struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
   11541 {
   11542   Elf_Internal_Ehdr * i_ehdrp;	/* ELF file header, internal form.  */
   11543 
   11544   i_ehdrp = elf_elfheader (abfd);
   11545 
   11546   i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
   11547 
   11548   /* To make things simpler for the loader on Linux systems we set the
   11549      osabi field to ELFOSABI_GNU if the binary contains symbols of
   11550      the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding.  */
   11551   if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
   11552       && elf_tdata (abfd)->has_gnu_symbols)
   11553     i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
   11554 }
   11555 
   11556 
   11557 /* Return TRUE for ELF symbol types that represent functions.
   11558    This is the default version of this function, which is sufficient for
   11559    most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
   11560 
   11561 bfd_boolean
   11562 _bfd_elf_is_function_type (unsigned int type)
   11563 {
   11564   return (type == STT_FUNC
   11565 	  || type == STT_GNU_IFUNC);
   11566 }
   11567 
   11568 /* If the ELF symbol SYM might be a function in SEC, return the
   11569    function size and set *CODE_OFF to the function's entry point,
   11570    otherwise return zero.  */
   11571 
   11572 bfd_size_type
   11573 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
   11574 			     bfd_vma *code_off)
   11575 {
   11576   bfd_size_type size;
   11577 
   11578   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
   11579 		     | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
   11580       || sym->section != sec)
   11581     return 0;
   11582 
   11583   *code_off = sym->value;
   11584   size = 0;
   11585   if (!(sym->flags & BSF_SYNTHETIC))
   11586     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
   11587   if (size == 0)
   11588     size = 1;
   11589   return size;
   11590 }
   11591