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