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