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