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