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