Home | History | Annotate | Line # | Download | only in bfd
elf.c revision 1.9
      1  1.1  christos /* ELF executable support for BFD.
      2  1.1  christos 
      3  1.9  christos    Copyright (C) 1993-2017 Free Software Foundation, Inc.
      4  1.1  christos 
      5  1.1  christos    This file is part of BFD, the Binary File Descriptor library.
      6  1.1  christos 
      7  1.1  christos    This program is free software; you can redistribute it and/or modify
      8  1.1  christos    it under the terms of the GNU General Public License as published by
      9  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10  1.1  christos    (at your option) any later version.
     11  1.1  christos 
     12  1.1  christos    This program is distributed in the hope that it will be useful,
     13  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  1.1  christos    GNU General Public License for more details.
     16  1.1  christos 
     17  1.1  christos    You should have received a copy of the GNU General Public License
     18  1.1  christos    along with this program; if not, write to the Free Software
     19  1.1  christos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     20  1.1  christos    MA 02110-1301, USA.  */
     21  1.1  christos 
     22  1.1  christos 
     23  1.1  christos /*
     24  1.1  christos SECTION
     25  1.1  christos 	ELF backends
     26  1.1  christos 
     27  1.1  christos 	BFD support for ELF formats is being worked on.
     28  1.1  christos 	Currently, the best supported back ends are for sparc and i386
     29  1.1  christos 	(running svr4 or Solaris 2).
     30  1.1  christos 
     31  1.1  christos 	Documentation of the internals of the support code still needs
     32  1.1  christos 	to be written.  The code is changing quickly enough that we
     33  1.1  christos 	haven't bothered yet.  */
     34  1.1  christos 
     35  1.1  christos /* For sparc64-cross-sparc32.  */
     36  1.1  christos #define _SYSCALL32
     37  1.1  christos #include "sysdep.h"
     38  1.1  christos #include "bfd.h"
     39  1.1  christos #include "bfdlink.h"
     40  1.1  christos #include "libbfd.h"
     41  1.1  christos #define ARCH_SIZE 0
     42  1.1  christos #include "elf-bfd.h"
     43  1.1  christos #include "libiberty.h"
     44  1.1  christos #include "safe-ctype.h"
     45  1.8  christos #include "elf-linux-core.h"
     46  1.1  christos 
     47  1.1  christos #ifdef CORE_HEADER
     48  1.1  christos #include CORE_HEADER
     49  1.1  christos #endif
     50  1.1  christos 
     51  1.1  christos static int elf_sort_sections (const void *, const void *);
     52  1.1  christos static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
     53  1.1  christos static bfd_boolean prep_headers (bfd *);
     54  1.6  christos static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ;
     55  1.1  christos static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ;
     56  1.1  christos static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
     57  1.1  christos 				    file_ptr offset);
     58  1.1  christos 
     59  1.1  christos /* Swap version information in and out.  The version information is
     60  1.1  christos    currently size independent.  If that ever changes, this code will
     61  1.1  christos    need to move into elfcode.h.  */
     62  1.1  christos 
     63  1.1  christos /* Swap in a Verdef structure.  */
     64  1.1  christos 
     65  1.1  christos void
     66  1.1  christos _bfd_elf_swap_verdef_in (bfd *abfd,
     67  1.1  christos 			 const Elf_External_Verdef *src,
     68  1.1  christos 			 Elf_Internal_Verdef *dst)
     69  1.1  christos {
     70  1.1  christos   dst->vd_version = H_GET_16 (abfd, src->vd_version);
     71  1.1  christos   dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
     72  1.1  christos   dst->vd_ndx     = H_GET_16 (abfd, src->vd_ndx);
     73  1.1  christos   dst->vd_cnt     = H_GET_16 (abfd, src->vd_cnt);
     74  1.1  christos   dst->vd_hash    = H_GET_32 (abfd, src->vd_hash);
     75  1.1  christos   dst->vd_aux     = H_GET_32 (abfd, src->vd_aux);
     76  1.1  christos   dst->vd_next    = H_GET_32 (abfd, src->vd_next);
     77  1.1  christos }
     78  1.1  christos 
     79  1.1  christos /* Swap out a Verdef structure.  */
     80  1.1  christos 
     81  1.1  christos void
     82  1.1  christos _bfd_elf_swap_verdef_out (bfd *abfd,
     83  1.1  christos 			  const Elf_Internal_Verdef *src,
     84  1.1  christos 			  Elf_External_Verdef *dst)
     85  1.1  christos {
     86  1.1  christos   H_PUT_16 (abfd, src->vd_version, dst->vd_version);
     87  1.1  christos   H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
     88  1.1  christos   H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
     89  1.1  christos   H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
     90  1.1  christos   H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
     91  1.1  christos   H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
     92  1.1  christos   H_PUT_32 (abfd, src->vd_next, dst->vd_next);
     93  1.1  christos }
     94  1.1  christos 
     95  1.1  christos /* Swap in a Verdaux structure.  */
     96  1.1  christos 
     97  1.1  christos void
     98  1.1  christos _bfd_elf_swap_verdaux_in (bfd *abfd,
     99  1.1  christos 			  const Elf_External_Verdaux *src,
    100  1.1  christos 			  Elf_Internal_Verdaux *dst)
    101  1.1  christos {
    102  1.1  christos   dst->vda_name = H_GET_32 (abfd, src->vda_name);
    103  1.1  christos   dst->vda_next = H_GET_32 (abfd, src->vda_next);
    104  1.1  christos }
    105  1.1  christos 
    106  1.1  christos /* Swap out a Verdaux structure.  */
    107  1.1  christos 
    108  1.1  christos void
    109  1.1  christos _bfd_elf_swap_verdaux_out (bfd *abfd,
    110  1.1  christos 			   const Elf_Internal_Verdaux *src,
    111  1.1  christos 			   Elf_External_Verdaux *dst)
    112  1.1  christos {
    113  1.1  christos   H_PUT_32 (abfd, src->vda_name, dst->vda_name);
    114  1.1  christos   H_PUT_32 (abfd, src->vda_next, dst->vda_next);
    115  1.1  christos }
    116  1.1  christos 
    117  1.1  christos /* Swap in a Verneed structure.  */
    118  1.1  christos 
    119  1.1  christos void
    120  1.1  christos _bfd_elf_swap_verneed_in (bfd *abfd,
    121  1.1  christos 			  const Elf_External_Verneed *src,
    122  1.1  christos 			  Elf_Internal_Verneed *dst)
    123  1.1  christos {
    124  1.1  christos   dst->vn_version = H_GET_16 (abfd, src->vn_version);
    125  1.1  christos   dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
    126  1.1  christos   dst->vn_file    = H_GET_32 (abfd, src->vn_file);
    127  1.1  christos   dst->vn_aux     = H_GET_32 (abfd, src->vn_aux);
    128  1.1  christos   dst->vn_next    = H_GET_32 (abfd, src->vn_next);
    129  1.1  christos }
    130  1.1  christos 
    131  1.1  christos /* Swap out a Verneed structure.  */
    132  1.1  christos 
    133  1.1  christos void
    134  1.1  christos _bfd_elf_swap_verneed_out (bfd *abfd,
    135  1.1  christos 			   const Elf_Internal_Verneed *src,
    136  1.1  christos 			   Elf_External_Verneed *dst)
    137  1.1  christos {
    138  1.1  christos   H_PUT_16 (abfd, src->vn_version, dst->vn_version);
    139  1.1  christos   H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
    140  1.1  christos   H_PUT_32 (abfd, src->vn_file, dst->vn_file);
    141  1.1  christos   H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
    142  1.1  christos   H_PUT_32 (abfd, src->vn_next, dst->vn_next);
    143  1.1  christos }
    144  1.1  christos 
    145  1.1  christos /* Swap in a Vernaux structure.  */
    146  1.1  christos 
    147  1.1  christos void
    148  1.1  christos _bfd_elf_swap_vernaux_in (bfd *abfd,
    149  1.1  christos 			  const Elf_External_Vernaux *src,
    150  1.1  christos 			  Elf_Internal_Vernaux *dst)
    151  1.1  christos {
    152  1.1  christos   dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
    153  1.1  christos   dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
    154  1.1  christos   dst->vna_other = H_GET_16 (abfd, src->vna_other);
    155  1.1  christos   dst->vna_name  = H_GET_32 (abfd, src->vna_name);
    156  1.1  christos   dst->vna_next  = H_GET_32 (abfd, src->vna_next);
    157  1.1  christos }
    158  1.1  christos 
    159  1.1  christos /* Swap out a Vernaux structure.  */
    160  1.1  christos 
    161  1.1  christos void
    162  1.1  christos _bfd_elf_swap_vernaux_out (bfd *abfd,
    163  1.1  christos 			   const Elf_Internal_Vernaux *src,
    164  1.1  christos 			   Elf_External_Vernaux *dst)
    165  1.1  christos {
    166  1.1  christos   H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
    167  1.1  christos   H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
    168  1.1  christos   H_PUT_16 (abfd, src->vna_other, dst->vna_other);
    169  1.1  christos   H_PUT_32 (abfd, src->vna_name, dst->vna_name);
    170  1.1  christos   H_PUT_32 (abfd, src->vna_next, dst->vna_next);
    171  1.1  christos }
    172  1.1  christos 
    173  1.1  christos /* Swap in a Versym structure.  */
    174  1.1  christos 
    175  1.1  christos void
    176  1.1  christos _bfd_elf_swap_versym_in (bfd *abfd,
    177  1.1  christos 			 const Elf_External_Versym *src,
    178  1.1  christos 			 Elf_Internal_Versym *dst)
    179  1.1  christos {
    180  1.1  christos   dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
    181  1.1  christos }
    182  1.1  christos 
    183  1.1  christos /* Swap out a Versym structure.  */
    184  1.1  christos 
    185  1.1  christos void
    186  1.1  christos _bfd_elf_swap_versym_out (bfd *abfd,
    187  1.1  christos 			  const Elf_Internal_Versym *src,
    188  1.1  christos 			  Elf_External_Versym *dst)
    189  1.1  christos {
    190  1.1  christos   H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
    191  1.1  christos }
    192  1.1  christos 
    193  1.1  christos /* Standard ELF hash function.  Do not change this function; you will
    194  1.1  christos    cause invalid hash tables to be generated.  */
    195  1.1  christos 
    196  1.1  christos unsigned long
    197  1.1  christos bfd_elf_hash (const char *namearg)
    198  1.1  christos {
    199  1.1  christos   const unsigned char *name = (const unsigned char *) namearg;
    200  1.1  christos   unsigned long h = 0;
    201  1.1  christos   unsigned long g;
    202  1.1  christos   int ch;
    203  1.1  christos 
    204  1.1  christos   while ((ch = *name++) != '\0')
    205  1.1  christos     {
    206  1.1  christos       h = (h << 4) + ch;
    207  1.1  christos       if ((g = (h & 0xf0000000)) != 0)
    208  1.1  christos 	{
    209  1.1  christos 	  h ^= g >> 24;
    210  1.1  christos 	  /* The ELF ABI says `h &= ~g', but this is equivalent in
    211  1.1  christos 	     this case and on some machines one insn instead of two.  */
    212  1.1  christos 	  h ^= g;
    213  1.1  christos 	}
    214  1.1  christos     }
    215  1.1  christos   return h & 0xffffffff;
    216  1.1  christos }
    217  1.1  christos 
    218  1.1  christos /* DT_GNU_HASH hash function.  Do not change this function; you will
    219  1.1  christos    cause invalid hash tables to be generated.  */
    220  1.1  christos 
    221  1.1  christos unsigned long
    222  1.1  christos bfd_elf_gnu_hash (const char *namearg)
    223  1.1  christos {
    224  1.1  christos   const unsigned char *name = (const unsigned char *) namearg;
    225  1.1  christos   unsigned long h = 5381;
    226  1.1  christos   unsigned char ch;
    227  1.1  christos 
    228  1.1  christos   while ((ch = *name++) != '\0')
    229  1.1  christos     h = (h << 5) + h + ch;
    230  1.1  christos   return h & 0xffffffff;
    231  1.1  christos }
    232  1.1  christos 
    233  1.1  christos /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
    234  1.1  christos    the object_id field of an elf_obj_tdata field set to OBJECT_ID.  */
    235  1.1  christos bfd_boolean
    236  1.1  christos bfd_elf_allocate_object (bfd *abfd,
    237  1.1  christos 			 size_t object_size,
    238  1.1  christos 			 enum elf_target_id object_id)
    239  1.1  christos {
    240  1.1  christos   BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
    241  1.1  christos   abfd->tdata.any = bfd_zalloc (abfd, object_size);
    242  1.1  christos   if (abfd->tdata.any == NULL)
    243  1.1  christos     return FALSE;
    244  1.1  christos 
    245  1.1  christos   elf_object_id (abfd) = object_id;
    246  1.3  christos   if (abfd->direction != read_direction)
    247  1.3  christos     {
    248  1.3  christos       struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
    249  1.3  christos       if (o == NULL)
    250  1.3  christos 	return FALSE;
    251  1.3  christos       elf_tdata (abfd)->o = o;
    252  1.3  christos       elf_program_header_size (abfd) = (bfd_size_type) -1;
    253  1.3  christos     }
    254  1.1  christos   return TRUE;
    255  1.1  christos }
    256  1.1  christos 
    257  1.1  christos 
    258  1.1  christos bfd_boolean
    259  1.1  christos bfd_elf_make_object (bfd *abfd)
    260  1.1  christos {
    261  1.1  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    262  1.1  christos   return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
    263  1.1  christos 				  bed->target_id);
    264  1.1  christos }
    265  1.1  christos 
    266  1.1  christos bfd_boolean
    267  1.1  christos bfd_elf_mkcorefile (bfd *abfd)
    268  1.1  christos {
    269  1.1  christos   /* I think this can be done just like an object file.  */
    270  1.3  christos   if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
    271  1.3  christos     return FALSE;
    272  1.3  christos   elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
    273  1.3  christos   return elf_tdata (abfd)->core != NULL;
    274  1.1  christos }
    275  1.1  christos 
    276  1.1  christos static char *
    277  1.1  christos bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
    278  1.1  christos {
    279  1.1  christos   Elf_Internal_Shdr **i_shdrp;
    280  1.1  christos   bfd_byte *shstrtab = NULL;
    281  1.1  christos   file_ptr offset;
    282  1.1  christos   bfd_size_type shstrtabsize;
    283  1.1  christos 
    284  1.1  christos   i_shdrp = elf_elfsections (abfd);
    285  1.1  christos   if (i_shdrp == 0
    286  1.1  christos       || shindex >= elf_numsections (abfd)
    287  1.1  christos       || i_shdrp[shindex] == 0)
    288  1.1  christos     return NULL;
    289  1.1  christos 
    290  1.1  christos   shstrtab = i_shdrp[shindex]->contents;
    291  1.1  christos   if (shstrtab == NULL)
    292  1.1  christos     {
    293  1.1  christos       /* No cached one, attempt to read, and cache what we read.  */
    294  1.1  christos       offset = i_shdrp[shindex]->sh_offset;
    295  1.1  christos       shstrtabsize = i_shdrp[shindex]->sh_size;
    296  1.1  christos 
    297  1.1  christos       /* Allocate and clear an extra byte at the end, to prevent crashes
    298  1.1  christos 	 in case the string table is not terminated.  */
    299  1.1  christos       if (shstrtabsize + 1 <= 1
    300  1.5  christos 	  || bfd_seek (abfd, offset, SEEK_SET) != 0
    301  1.5  christos 	  || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL)
    302  1.1  christos 	shstrtab = NULL;
    303  1.1  christos       else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
    304  1.1  christos 	{
    305  1.1  christos 	  if (bfd_get_error () != bfd_error_system_call)
    306  1.1  christos 	    bfd_set_error (bfd_error_file_truncated);
    307  1.5  christos 	  bfd_release (abfd, shstrtab);
    308  1.1  christos 	  shstrtab = NULL;
    309  1.1  christos 	  /* Once we've failed to read it, make sure we don't keep
    310  1.1  christos 	     trying.  Otherwise, we'll keep allocating space for
    311  1.1  christos 	     the string table over and over.  */
    312  1.1  christos 	  i_shdrp[shindex]->sh_size = 0;
    313  1.1  christos 	}
    314  1.1  christos       else
    315  1.1  christos 	shstrtab[shstrtabsize] = '\0';
    316  1.1  christos       i_shdrp[shindex]->contents = shstrtab;
    317  1.1  christos     }
    318  1.1  christos   return (char *) shstrtab;
    319  1.1  christos }
    320  1.1  christos 
    321  1.1  christos char *
    322  1.1  christos bfd_elf_string_from_elf_section (bfd *abfd,
    323  1.1  christos 				 unsigned int shindex,
    324  1.1  christos 				 unsigned int strindex)
    325  1.1  christos {
    326  1.1  christos   Elf_Internal_Shdr *hdr;
    327  1.1  christos 
    328  1.1  christos   if (strindex == 0)
    329  1.1  christos     return "";
    330  1.1  christos 
    331  1.1  christos   if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
    332  1.1  christos     return NULL;
    333  1.1  christos 
    334  1.1  christos   hdr = elf_elfsections (abfd)[shindex];
    335  1.1  christos 
    336  1.5  christos   if (hdr->contents == NULL)
    337  1.5  christos     {
    338  1.5  christos       if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
    339  1.5  christos 	{
    340  1.5  christos 	  /* PR 17512: file: f057ec89.  */
    341  1.9  christos 	  /* xgettext:c-format */
    342  1.9  christos 	  _bfd_error_handler (_("%B: attempt to load strings from"
    343  1.9  christos 				" a non-string section (number %d)"),
    344  1.5  christos 			      abfd, shindex);
    345  1.5  christos 	  return NULL;
    346  1.5  christos 	}
    347  1.6  christos 
    348  1.5  christos       if (bfd_elf_get_str_section (abfd, shindex) == NULL)
    349  1.5  christos 	return NULL;
    350  1.5  christos     }
    351  1.1  christos 
    352  1.1  christos   if (strindex >= hdr->sh_size)
    353  1.1  christos     {
    354  1.1  christos       unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
    355  1.9  christos       _bfd_error_handler
    356  1.9  christos 	/* xgettext:c-format */
    357  1.1  christos 	(_("%B: invalid string offset %u >= %lu for section `%s'"),
    358  1.1  christos 	 abfd, strindex, (unsigned long) hdr->sh_size,
    359  1.1  christos 	 (shindex == shstrndx && strindex == hdr->sh_name
    360  1.1  christos 	  ? ".shstrtab"
    361  1.1  christos 	  : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
    362  1.1  christos       return NULL;
    363  1.1  christos     }
    364  1.1  christos 
    365  1.1  christos   return ((char *) hdr->contents) + strindex;
    366  1.1  christos }
    367  1.1  christos 
    368  1.1  christos /* Read and convert symbols to internal format.
    369  1.1  christos    SYMCOUNT specifies the number of symbols to read, starting from
    370  1.1  christos    symbol SYMOFFSET.  If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
    371  1.1  christos    are non-NULL, they are used to store the internal symbols, external
    372  1.1  christos    symbols, and symbol section index extensions, respectively.
    373  1.1  christos    Returns a pointer to the internal symbol buffer (malloced if necessary)
    374  1.1  christos    or NULL if there were no symbols or some kind of problem.  */
    375  1.1  christos 
    376  1.1  christos Elf_Internal_Sym *
    377  1.1  christos bfd_elf_get_elf_syms (bfd *ibfd,
    378  1.1  christos 		      Elf_Internal_Shdr *symtab_hdr,
    379  1.1  christos 		      size_t symcount,
    380  1.1  christos 		      size_t symoffset,
    381  1.1  christos 		      Elf_Internal_Sym *intsym_buf,
    382  1.1  christos 		      void *extsym_buf,
    383  1.1  christos 		      Elf_External_Sym_Shndx *extshndx_buf)
    384  1.1  christos {
    385  1.1  christos   Elf_Internal_Shdr *shndx_hdr;
    386  1.1  christos   void *alloc_ext;
    387  1.1  christos   const bfd_byte *esym;
    388  1.1  christos   Elf_External_Sym_Shndx *alloc_extshndx;
    389  1.1  christos   Elf_External_Sym_Shndx *shndx;
    390  1.1  christos   Elf_Internal_Sym *alloc_intsym;
    391  1.1  christos   Elf_Internal_Sym *isym;
    392  1.1  christos   Elf_Internal_Sym *isymend;
    393  1.1  christos   const struct elf_backend_data *bed;
    394  1.1  christos   size_t extsym_size;
    395  1.1  christos   bfd_size_type amt;
    396  1.1  christos   file_ptr pos;
    397  1.1  christos 
    398  1.1  christos   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
    399  1.1  christos     abort ();
    400  1.1  christos 
    401  1.1  christos   if (symcount == 0)
    402  1.1  christos     return intsym_buf;
    403  1.1  christos 
    404  1.1  christos   /* Normal syms might have section extension entries.  */
    405  1.1  christos   shndx_hdr = NULL;
    406  1.8  christos   if (elf_symtab_shndx_list (ibfd) != NULL)
    407  1.8  christos     {
    408  1.8  christos       elf_section_list * entry;
    409  1.8  christos       Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
    410  1.8  christos 
    411  1.8  christos       /* Find an index section that is linked to this symtab section.  */
    412  1.8  christos       for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
    413  1.8  christos 	{
    414  1.8  christos 	  /* PR 20063.  */
    415  1.8  christos 	  if (entry->hdr.sh_link >= elf_numsections (ibfd))
    416  1.8  christos 	    continue;
    417  1.8  christos 
    418  1.8  christos 	  if (sections[entry->hdr.sh_link] == symtab_hdr)
    419  1.8  christos 	    {
    420  1.8  christos 	      shndx_hdr = & entry->hdr;
    421  1.8  christos 	      break;
    422  1.8  christos 	    };
    423  1.8  christos 	}
    424  1.8  christos 
    425  1.8  christos       if (shndx_hdr == NULL)
    426  1.8  christos 	{
    427  1.8  christos 	  if (symtab_hdr == & elf_symtab_hdr (ibfd))
    428  1.8  christos 	    /* Not really accurate, but this was how the old code used to work.  */
    429  1.8  christos 	    shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
    430  1.8  christos 	  /* Otherwise we do nothing.  The assumption is that
    431  1.8  christos 	     the index table will not be needed.  */
    432  1.8  christos 	}
    433  1.8  christos     }
    434  1.1  christos 
    435  1.1  christos   /* Read the symbols.  */
    436  1.1  christos   alloc_ext = NULL;
    437  1.1  christos   alloc_extshndx = NULL;
    438  1.1  christos   alloc_intsym = NULL;
    439  1.1  christos   bed = get_elf_backend_data (ibfd);
    440  1.1  christos   extsym_size = bed->s->sizeof_sym;
    441  1.8  christos   amt = (bfd_size_type) symcount * extsym_size;
    442  1.1  christos   pos = symtab_hdr->sh_offset + symoffset * extsym_size;
    443  1.1  christos   if (extsym_buf == NULL)
    444  1.1  christos     {
    445  1.1  christos       alloc_ext = bfd_malloc2 (symcount, extsym_size);
    446  1.1  christos       extsym_buf = alloc_ext;
    447  1.1  christos     }
    448  1.1  christos   if (extsym_buf == NULL
    449  1.1  christos       || bfd_seek (ibfd, pos, SEEK_SET) != 0
    450  1.1  christos       || bfd_bread (extsym_buf, amt, ibfd) != amt)
    451  1.1  christos     {
    452  1.1  christos       intsym_buf = NULL;
    453  1.1  christos       goto out;
    454  1.1  christos     }
    455  1.1  christos 
    456  1.1  christos   if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
    457  1.1  christos     extshndx_buf = NULL;
    458  1.1  christos   else
    459  1.1  christos     {
    460  1.8  christos       amt = (bfd_size_type) symcount * sizeof (Elf_External_Sym_Shndx);
    461  1.1  christos       pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
    462  1.1  christos       if (extshndx_buf == NULL)
    463  1.1  christos 	{
    464  1.1  christos 	  alloc_extshndx = (Elf_External_Sym_Shndx *)
    465  1.1  christos               bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx));
    466  1.1  christos 	  extshndx_buf = alloc_extshndx;
    467  1.1  christos 	}
    468  1.1  christos       if (extshndx_buf == NULL
    469  1.1  christos 	  || bfd_seek (ibfd, pos, SEEK_SET) != 0
    470  1.1  christos 	  || bfd_bread (extshndx_buf, amt, ibfd) != amt)
    471  1.1  christos 	{
    472  1.1  christos 	  intsym_buf = NULL;
    473  1.1  christos 	  goto out;
    474  1.1  christos 	}
    475  1.1  christos     }
    476  1.1  christos 
    477  1.1  christos   if (intsym_buf == NULL)
    478  1.1  christos     {
    479  1.1  christos       alloc_intsym = (Elf_Internal_Sym *)
    480  1.1  christos           bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
    481  1.1  christos       intsym_buf = alloc_intsym;
    482  1.1  christos       if (intsym_buf == NULL)
    483  1.1  christos 	goto out;
    484  1.1  christos     }
    485  1.1  christos 
    486  1.1  christos   /* Convert the symbols to internal form.  */
    487  1.1  christos   isymend = intsym_buf + symcount;
    488  1.1  christos   for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
    489  1.1  christos            shndx = extshndx_buf;
    490  1.1  christos        isym < isymend;
    491  1.1  christos        esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
    492  1.1  christos     if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
    493  1.1  christos       {
    494  1.1  christos 	symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
    495  1.9  christos 	/* xgettext:c-format */
    496  1.9  christos 	_bfd_error_handler (_("%B symbol number %lu references"
    497  1.9  christos 			      " nonexistent SHT_SYMTAB_SHNDX section"),
    498  1.9  christos 			    ibfd, (unsigned long) symoffset);
    499  1.1  christos 	if (alloc_intsym != NULL)
    500  1.1  christos 	  free (alloc_intsym);
    501  1.1  christos 	intsym_buf = NULL;
    502  1.1  christos 	goto out;
    503  1.1  christos       }
    504  1.1  christos 
    505  1.1  christos  out:
    506  1.1  christos   if (alloc_ext != NULL)
    507  1.1  christos     free (alloc_ext);
    508  1.1  christos   if (alloc_extshndx != NULL)
    509  1.1  christos     free (alloc_extshndx);
    510  1.1  christos 
    511  1.1  christos   return intsym_buf;
    512  1.1  christos }
    513  1.1  christos 
    514  1.1  christos /* Look up a symbol name.  */
    515  1.1  christos const char *
    516  1.1  christos bfd_elf_sym_name (bfd *abfd,
    517  1.1  christos 		  Elf_Internal_Shdr *symtab_hdr,
    518  1.1  christos 		  Elf_Internal_Sym *isym,
    519  1.1  christos 		  asection *sym_sec)
    520  1.1  christos {
    521  1.1  christos   const char *name;
    522  1.1  christos   unsigned int iname = isym->st_name;
    523  1.1  christos   unsigned int shindex = symtab_hdr->sh_link;
    524  1.1  christos 
    525  1.1  christos   if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
    526  1.1  christos       /* Check for a bogus st_shndx to avoid crashing.  */
    527  1.1  christos       && isym->st_shndx < elf_numsections (abfd))
    528  1.1  christos     {
    529  1.1  christos       iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
    530  1.1  christos       shindex = elf_elfheader (abfd)->e_shstrndx;
    531  1.1  christos     }
    532  1.1  christos 
    533  1.1  christos   name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
    534  1.1  christos   if (name == NULL)
    535  1.1  christos     name = "(null)";
    536  1.1  christos   else if (sym_sec && *name == '\0')
    537  1.1  christos     name = bfd_section_name (abfd, sym_sec);
    538  1.1  christos 
    539  1.1  christos   return name;
    540  1.1  christos }
    541  1.1  christos 
    542  1.1  christos /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
    543  1.1  christos    sections.  The first element is the flags, the rest are section
    544  1.1  christos    pointers.  */
    545  1.1  christos 
    546  1.1  christos typedef union elf_internal_group {
    547  1.1  christos   Elf_Internal_Shdr *shdr;
    548  1.1  christos   unsigned int flags;
    549  1.1  christos } Elf_Internal_Group;
    550  1.1  christos 
    551  1.1  christos /* Return the name of the group signature symbol.  Why isn't the
    552  1.1  christos    signature just a string?  */
    553  1.1  christos 
    554  1.1  christos static const char *
    555  1.1  christos group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
    556  1.1  christos {
    557  1.1  christos   Elf_Internal_Shdr *hdr;
    558  1.1  christos   unsigned char esym[sizeof (Elf64_External_Sym)];
    559  1.1  christos   Elf_External_Sym_Shndx eshndx;
    560  1.1  christos   Elf_Internal_Sym isym;
    561  1.1  christos 
    562  1.1  christos   /* First we need to ensure the symbol table is available.  Make sure
    563  1.1  christos      that it is a symbol table section.  */
    564  1.1  christos   if (ghdr->sh_link >= elf_numsections (abfd))
    565  1.1  christos     return NULL;
    566  1.1  christos   hdr = elf_elfsections (abfd) [ghdr->sh_link];
    567  1.1  christos   if (hdr->sh_type != SHT_SYMTAB
    568  1.1  christos       || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
    569  1.1  christos     return NULL;
    570  1.1  christos 
    571  1.1  christos   /* Go read the symbol.  */
    572  1.1  christos   hdr = &elf_tdata (abfd)->symtab_hdr;
    573  1.1  christos   if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
    574  1.1  christos 			    &isym, esym, &eshndx) == NULL)
    575  1.1  christos     return NULL;
    576  1.1  christos 
    577  1.1  christos   return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
    578  1.1  christos }
    579  1.1  christos 
    580  1.1  christos /* Set next_in_group list pointer, and group name for NEWSECT.  */
    581  1.1  christos 
    582  1.1  christos static bfd_boolean
    583  1.1  christos setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
    584  1.1  christos {
    585  1.1  christos   unsigned int num_group = elf_tdata (abfd)->num_group;
    586  1.1  christos 
    587  1.1  christos   /* If num_group is zero, read in all SHT_GROUP sections.  The count
    588  1.1  christos      is set to -1 if there are no SHT_GROUP sections.  */
    589  1.1  christos   if (num_group == 0)
    590  1.1  christos     {
    591  1.1  christos       unsigned int i, shnum;
    592  1.1  christos 
    593  1.1  christos       /* First count the number of groups.  If we have a SHT_GROUP
    594  1.1  christos 	 section with just a flag word (ie. sh_size is 4), ignore it.  */
    595  1.1  christos       shnum = elf_numsections (abfd);
    596  1.1  christos       num_group = 0;
    597  1.1  christos 
    598  1.3  christos #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize)	\
    599  1.1  christos 	(   (shdr)->sh_type == SHT_GROUP		\
    600  1.3  christos 	 && (shdr)->sh_size >= minsize			\
    601  1.1  christos 	 && (shdr)->sh_entsize == GRP_ENTRY_SIZE	\
    602  1.1  christos 	 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
    603  1.1  christos 
    604  1.1  christos       for (i = 0; i < shnum; i++)
    605  1.1  christos 	{
    606  1.1  christos 	  Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
    607  1.1  christos 
    608  1.3  christos 	  if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
    609  1.1  christos 	    num_group += 1;
    610  1.1  christos 	}
    611  1.1  christos 
    612  1.1  christos       if (num_group == 0)
    613  1.1  christos 	{
    614  1.1  christos 	  num_group = (unsigned) -1;
    615  1.1  christos 	  elf_tdata (abfd)->num_group = num_group;
    616  1.1  christos 	}
    617  1.1  christos       else
    618  1.1  christos 	{
    619  1.1  christos 	  /* We keep a list of elf section headers for group sections,
    620  1.1  christos 	     so we can find them quickly.  */
    621  1.1  christos 	  bfd_size_type amt;
    622  1.1  christos 
    623  1.1  christos 	  elf_tdata (abfd)->num_group = num_group;
    624  1.1  christos 	  elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
    625  1.1  christos               bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
    626  1.1  christos 	  if (elf_tdata (abfd)->group_sect_ptr == NULL)
    627  1.1  christos 	    return FALSE;
    628  1.1  christos 
    629  1.1  christos 	  num_group = 0;
    630  1.1  christos 	  for (i = 0; i < shnum; i++)
    631  1.1  christos 	    {
    632  1.1  christos 	      Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
    633  1.1  christos 
    634  1.3  christos 	      if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
    635  1.1  christos 		{
    636  1.1  christos 		  unsigned char *src;
    637  1.1  christos 		  Elf_Internal_Group *dest;
    638  1.1  christos 
    639  1.1  christos 		  /* Add to list of sections.  */
    640  1.1  christos 		  elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
    641  1.1  christos 		  num_group += 1;
    642  1.1  christos 
    643  1.1  christos 		  /* Read the raw contents.  */
    644  1.1  christos 		  BFD_ASSERT (sizeof (*dest) >= 4);
    645  1.1  christos 		  amt = shdr->sh_size * sizeof (*dest) / 4;
    646  1.1  christos 		  shdr->contents = (unsigned char *)
    647  1.1  christos                       bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
    648  1.1  christos 		  /* PR binutils/4110: Handle corrupt group headers.  */
    649  1.1  christos 		  if (shdr->contents == NULL)
    650  1.1  christos 		    {
    651  1.1  christos 		      _bfd_error_handler
    652  1.9  christos 			/* xgettext:c-format */
    653  1.9  christos 			(_("%B: corrupt size field in group section"
    654  1.9  christos 			   " header: 0x%lx"), abfd, shdr->sh_size);
    655  1.1  christos 		      bfd_set_error (bfd_error_bad_value);
    656  1.5  christos 		      -- num_group;
    657  1.5  christos 		      continue;
    658  1.1  christos 		    }
    659  1.1  christos 
    660  1.1  christos 		  memset (shdr->contents, 0, amt);
    661  1.1  christos 
    662  1.1  christos 		  if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
    663  1.1  christos 		      || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
    664  1.1  christos 			  != shdr->sh_size))
    665  1.5  christos 		    {
    666  1.5  christos 		      _bfd_error_handler
    667  1.9  christos 			/* xgettext:c-format */
    668  1.9  christos 			(_("%B: invalid size field in group section"
    669  1.9  christos 			   " header: 0x%lx"), abfd, shdr->sh_size);
    670  1.5  christos 		      bfd_set_error (bfd_error_bad_value);
    671  1.5  christos 		      -- num_group;
    672  1.9  christos 		      /* PR 17510: If the group contents are even
    673  1.9  christos 			 partially corrupt, do not allow any of the
    674  1.9  christos 			 contents to be used.  */
    675  1.5  christos 		      memset (shdr->contents, 0, amt);
    676  1.5  christos 		      continue;
    677  1.5  christos 		    }
    678  1.1  christos 
    679  1.1  christos 		  /* Translate raw contents, a flag word followed by an
    680  1.1  christos 		     array of elf section indices all in target byte order,
    681  1.1  christos 		     to the flag word followed by an array of elf section
    682  1.1  christos 		     pointers.  */
    683  1.1  christos 		  src = shdr->contents + shdr->sh_size;
    684  1.1  christos 		  dest = (Elf_Internal_Group *) (shdr->contents + amt);
    685  1.5  christos 
    686  1.1  christos 		  while (1)
    687  1.1  christos 		    {
    688  1.1  christos 		      unsigned int idx;
    689  1.1  christos 
    690  1.1  christos 		      src -= 4;
    691  1.1  christos 		      --dest;
    692  1.1  christos 		      idx = H_GET_32 (abfd, src);
    693  1.1  christos 		      if (src == shdr->contents)
    694  1.1  christos 			{
    695  1.1  christos 			  dest->flags = idx;
    696  1.1  christos 			  if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
    697  1.1  christos 			    shdr->bfd_section->flags
    698  1.1  christos 			      |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
    699  1.1  christos 			  break;
    700  1.1  christos 			}
    701  1.1  christos 		      if (idx >= shnum)
    702  1.1  christos 			{
    703  1.9  christos 			  _bfd_error_handler
    704  1.9  christos 			    (_("%B: invalid SHT_GROUP entry"), abfd);
    705  1.1  christos 			  idx = 0;
    706  1.1  christos 			}
    707  1.1  christos 		      dest->shdr = elf_elfsections (abfd)[idx];
    708  1.1  christos 		    }
    709  1.1  christos 		}
    710  1.1  christos 	    }
    711  1.5  christos 
    712  1.5  christos 	  /* PR 17510: Corrupt binaries might contain invalid groups.  */
    713  1.5  christos 	  if (num_group != (unsigned) elf_tdata (abfd)->num_group)
    714  1.5  christos 	    {
    715  1.5  christos 	      elf_tdata (abfd)->num_group = num_group;
    716  1.5  christos 
    717  1.5  christos 	      /* If all groups are invalid then fail.  */
    718  1.5  christos 	      if (num_group == 0)
    719  1.5  christos 		{
    720  1.5  christos 		  elf_tdata (abfd)->group_sect_ptr = NULL;
    721  1.5  christos 		  elf_tdata (abfd)->num_group = num_group = -1;
    722  1.9  christos 		  _bfd_error_handler
    723  1.9  christos 		    (_("%B: no valid group sections found"), abfd);
    724  1.5  christos 		  bfd_set_error (bfd_error_bad_value);
    725  1.5  christos 		}
    726  1.5  christos 	    }
    727  1.1  christos 	}
    728  1.1  christos     }
    729  1.1  christos 
    730  1.1  christos   if (num_group != (unsigned) -1)
    731  1.1  christos     {
    732  1.1  christos       unsigned int i;
    733  1.1  christos 
    734  1.1  christos       for (i = 0; i < num_group; i++)
    735  1.1  christos 	{
    736  1.1  christos 	  Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
    737  1.1  christos 	  Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
    738  1.1  christos 	  unsigned int n_elt = shdr->sh_size / 4;
    739  1.1  christos 
    740  1.1  christos 	  /* Look through this group's sections to see if current
    741  1.1  christos 	     section is a member.  */
    742  1.1  christos 	  while (--n_elt != 0)
    743  1.1  christos 	    if ((++idx)->shdr == hdr)
    744  1.1  christos 	      {
    745  1.1  christos 		asection *s = NULL;
    746  1.1  christos 
    747  1.1  christos 		/* We are a member of this group.  Go looking through
    748  1.1  christos 		   other members to see if any others are linked via
    749  1.1  christos 		   next_in_group.  */
    750  1.1  christos 		idx = (Elf_Internal_Group *) shdr->contents;
    751  1.1  christos 		n_elt = shdr->sh_size / 4;
    752  1.1  christos 		while (--n_elt != 0)
    753  1.1  christos 		  if ((s = (++idx)->shdr->bfd_section) != NULL
    754  1.1  christos 		      && elf_next_in_group (s) != NULL)
    755  1.1  christos 		    break;
    756  1.1  christos 		if (n_elt != 0)
    757  1.1  christos 		  {
    758  1.1  christos 		    /* Snarf the group name from other member, and
    759  1.1  christos 		       insert current section in circular list.  */
    760  1.1  christos 		    elf_group_name (newsect) = elf_group_name (s);
    761  1.1  christos 		    elf_next_in_group (newsect) = elf_next_in_group (s);
    762  1.1  christos 		    elf_next_in_group (s) = newsect;
    763  1.1  christos 		  }
    764  1.1  christos 		else
    765  1.1  christos 		  {
    766  1.1  christos 		    const char *gname;
    767  1.1  christos 
    768  1.1  christos 		    gname = group_signature (abfd, shdr);
    769  1.1  christos 		    if (gname == NULL)
    770  1.1  christos 		      return FALSE;
    771  1.1  christos 		    elf_group_name (newsect) = gname;
    772  1.1  christos 
    773  1.1  christos 		    /* Start a circular list with one element.  */
    774  1.1  christos 		    elf_next_in_group (newsect) = newsect;
    775  1.1  christos 		  }
    776  1.1  christos 
    777  1.1  christos 		/* If the group section has been created, point to the
    778  1.1  christos 		   new member.  */
    779  1.1  christos 		if (shdr->bfd_section != NULL)
    780  1.1  christos 		  elf_next_in_group (shdr->bfd_section) = newsect;
    781  1.1  christos 
    782  1.1  christos 		i = num_group - 1;
    783  1.1  christos 		break;
    784  1.1  christos 	      }
    785  1.1  christos 	}
    786  1.1  christos     }
    787  1.1  christos 
    788  1.1  christos   if (elf_group_name (newsect) == NULL)
    789  1.1  christos     {
    790  1.9  christos       /* xgettext:c-format */
    791  1.9  christos       _bfd_error_handler (_("%B: no group info for section %A"),
    792  1.9  christos 			  abfd, newsect);
    793  1.5  christos       return FALSE;
    794  1.1  christos     }
    795  1.1  christos   return TRUE;
    796  1.1  christos }
    797  1.1  christos 
    798  1.1  christos bfd_boolean
    799  1.1  christos _bfd_elf_setup_sections (bfd *abfd)
    800  1.1  christos {
    801  1.1  christos   unsigned int i;
    802  1.1  christos   unsigned int num_group = elf_tdata (abfd)->num_group;
    803  1.1  christos   bfd_boolean result = TRUE;
    804  1.1  christos   asection *s;
    805  1.1  christos 
    806  1.1  christos   /* Process SHF_LINK_ORDER.  */
    807  1.1  christos   for (s = abfd->sections; s != NULL; s = s->next)
    808  1.1  christos     {
    809  1.1  christos       Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
    810  1.1  christos       if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
    811  1.1  christos 	{
    812  1.1  christos 	  unsigned int elfsec = this_hdr->sh_link;
    813  1.1  christos 	  /* FIXME: The old Intel compiler and old strip/objcopy may
    814  1.1  christos 	     not set the sh_link or sh_info fields.  Hence we could
    815  1.1  christos 	     get the situation where elfsec is 0.  */
    816  1.1  christos 	  if (elfsec == 0)
    817  1.1  christos 	    {
    818  1.1  christos 	      const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    819  1.1  christos 	      if (bed->link_order_error_handler)
    820  1.1  christos 		bed->link_order_error_handler
    821  1.9  christos 		  /* xgettext:c-format */
    822  1.1  christos 		  (_("%B: warning: sh_link not set for section `%A'"),
    823  1.1  christos 		   abfd, s);
    824  1.1  christos 	    }
    825  1.1  christos 	  else
    826  1.1  christos 	    {
    827  1.1  christos 	      asection *linksec = NULL;
    828  1.1  christos 
    829  1.1  christos 	      if (elfsec < elf_numsections (abfd))
    830  1.1  christos 		{
    831  1.1  christos 		  this_hdr = elf_elfsections (abfd)[elfsec];
    832  1.1  christos 		  linksec = this_hdr->bfd_section;
    833  1.1  christos 		}
    834  1.1  christos 
    835  1.1  christos 	      /* PR 1991, 2008:
    836  1.1  christos 		 Some strip/objcopy may leave an incorrect value in
    837  1.1  christos 		 sh_link.  We don't want to proceed.  */
    838  1.1  christos 	      if (linksec == NULL)
    839  1.1  christos 		{
    840  1.9  christos 		  _bfd_error_handler
    841  1.9  christos 		    /* xgettext:c-format */
    842  1.1  christos 		    (_("%B: sh_link [%d] in section `%A' is incorrect"),
    843  1.9  christos 		     s->owner, elfsec, s);
    844  1.1  christos 		  result = FALSE;
    845  1.1  christos 		}
    846  1.1  christos 
    847  1.1  christos 	      elf_linked_to_section (s) = linksec;
    848  1.1  christos 	    }
    849  1.1  christos 	}
    850  1.9  christos       else if (this_hdr->sh_type == SHT_GROUP
    851  1.9  christos 	       && elf_next_in_group (s) == NULL)
    852  1.9  christos 	{
    853  1.9  christos 	  _bfd_error_handler
    854  1.9  christos 	    /* xgettext:c-format */
    855  1.9  christos 	    (_("%B: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
    856  1.9  christos 	     abfd, elf_section_data (s)->this_idx);
    857  1.9  christos 	  result = FALSE;
    858  1.9  christos 	}
    859  1.1  christos     }
    860  1.1  christos 
    861  1.1  christos   /* Process section groups.  */
    862  1.1  christos   if (num_group == (unsigned) -1)
    863  1.1  christos     return result;
    864  1.1  christos 
    865  1.1  christos   for (i = 0; i < num_group; i++)
    866  1.1  christos     {
    867  1.1  christos       Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
    868  1.8  christos       Elf_Internal_Group *idx;
    869  1.8  christos       unsigned int n_elt;
    870  1.8  christos 
    871  1.8  christos       /* PR binutils/18758: Beware of corrupt binaries with invalid group data.  */
    872  1.8  christos       if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
    873  1.8  christos 	{
    874  1.9  christos 	  _bfd_error_handler
    875  1.9  christos 	    /* xgettext:c-format */
    876  1.8  christos 	    (_("%B: section group entry number %u is corrupt"),
    877  1.8  christos 	     abfd, i);
    878  1.8  christos 	  result = FALSE;
    879  1.8  christos 	  continue;
    880  1.8  christos 	}
    881  1.8  christos 
    882  1.8  christos       idx = (Elf_Internal_Group *) shdr->contents;
    883  1.8  christos       n_elt = shdr->sh_size / 4;
    884  1.1  christos 
    885  1.1  christos       while (--n_elt != 0)
    886  1.1  christos 	if ((++idx)->shdr->bfd_section)
    887  1.1  christos 	  elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
    888  1.1  christos 	else if (idx->shdr->sh_type == SHT_RELA
    889  1.1  christos 		 || idx->shdr->sh_type == SHT_REL)
    890  1.1  christos 	  /* We won't include relocation sections in section groups in
    891  1.1  christos 	     output object files. We adjust the group section size here
    892  1.1  christos 	     so that relocatable link will work correctly when
    893  1.1  christos 	     relocation sections are in section group in input object
    894  1.1  christos 	     files.  */
    895  1.1  christos 	  shdr->bfd_section->size -= 4;
    896  1.1  christos 	else
    897  1.1  christos 	  {
    898  1.1  christos 	    /* There are some unknown sections in the group.  */
    899  1.9  christos 	    _bfd_error_handler
    900  1.9  christos 	      /* xgettext:c-format */
    901  1.9  christos 	      (_("%B: unknown [%d] section `%s' in group [%A]"),
    902  1.1  christos 	       abfd,
    903  1.1  christos 	       (unsigned int) idx->shdr->sh_type,
    904  1.1  christos 	       bfd_elf_string_from_elf_section (abfd,
    905  1.1  christos 						(elf_elfheader (abfd)
    906  1.1  christos 						 ->e_shstrndx),
    907  1.1  christos 						idx->shdr->sh_name),
    908  1.9  christos 	       shdr->bfd_section);
    909  1.1  christos 	    result = FALSE;
    910  1.1  christos 	  }
    911  1.1  christos     }
    912  1.1  christos   return result;
    913  1.1  christos }
    914  1.1  christos 
    915  1.1  christos bfd_boolean
    916  1.1  christos bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
    917  1.1  christos {
    918  1.1  christos   return elf_next_in_group (sec) != NULL;
    919  1.1  christos }
    920  1.1  christos 
    921  1.6  christos static char *
    922  1.6  christos convert_debug_to_zdebug (bfd *abfd, const char *name)
    923  1.6  christos {
    924  1.6  christos   unsigned int len = strlen (name);
    925  1.6  christos   char *new_name = bfd_alloc (abfd, len + 2);
    926  1.6  christos   if (new_name == NULL)
    927  1.6  christos     return NULL;
    928  1.6  christos   new_name[0] = '.';
    929  1.6  christos   new_name[1] = 'z';
    930  1.6  christos   memcpy (new_name + 2, name + 1, len);
    931  1.6  christos   return new_name;
    932  1.6  christos }
    933  1.6  christos 
    934  1.6  christos static char *
    935  1.6  christos convert_zdebug_to_debug (bfd *abfd, const char *name)
    936  1.6  christos {
    937  1.6  christos   unsigned int len = strlen (name);
    938  1.6  christos   char *new_name = bfd_alloc (abfd, len);
    939  1.6  christos   if (new_name == NULL)
    940  1.6  christos     return NULL;
    941  1.6  christos   new_name[0] = '.';
    942  1.6  christos   memcpy (new_name + 1, name + 2, len - 1);
    943  1.6  christos   return new_name;
    944  1.6  christos }
    945  1.6  christos 
    946  1.1  christos /* Make a BFD section from an ELF section.  We store a pointer to the
    947  1.1  christos    BFD section in the bfd_section field of the header.  */
    948  1.1  christos 
    949  1.1  christos bfd_boolean
    950  1.1  christos _bfd_elf_make_section_from_shdr (bfd *abfd,
    951  1.1  christos 				 Elf_Internal_Shdr *hdr,
    952  1.1  christos 				 const char *name,
    953  1.1  christos 				 int shindex)
    954  1.1  christos {
    955  1.1  christos   asection *newsect;
    956  1.1  christos   flagword flags;
    957  1.1  christos   const struct elf_backend_data *bed;
    958  1.1  christos 
    959  1.1  christos   if (hdr->bfd_section != NULL)
    960  1.1  christos     return TRUE;
    961  1.1  christos 
    962  1.1  christos   newsect = bfd_make_section_anyway (abfd, name);
    963  1.1  christos   if (newsect == NULL)
    964  1.1  christos     return FALSE;
    965  1.1  christos 
    966  1.1  christos   hdr->bfd_section = newsect;
    967  1.1  christos   elf_section_data (newsect)->this_hdr = *hdr;
    968  1.1  christos   elf_section_data (newsect)->this_idx = shindex;
    969  1.1  christos 
    970  1.1  christos   /* Always use the real type/flags.  */
    971  1.1  christos   elf_section_type (newsect) = hdr->sh_type;
    972  1.1  christos   elf_section_flags (newsect) = hdr->sh_flags;
    973  1.1  christos 
    974  1.1  christos   newsect->filepos = hdr->sh_offset;
    975  1.1  christos 
    976  1.1  christos   if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
    977  1.1  christos       || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
    978  1.1  christos       || ! bfd_set_section_alignment (abfd, newsect,
    979  1.1  christos 				      bfd_log2 (hdr->sh_addralign)))
    980  1.1  christos     return FALSE;
    981  1.1  christos 
    982  1.1  christos   flags = SEC_NO_FLAGS;
    983  1.1  christos   if (hdr->sh_type != SHT_NOBITS)
    984  1.1  christos     flags |= SEC_HAS_CONTENTS;
    985  1.1  christos   if (hdr->sh_type == SHT_GROUP)
    986  1.1  christos     flags |= SEC_GROUP | SEC_EXCLUDE;
    987  1.1  christos   if ((hdr->sh_flags & SHF_ALLOC) != 0)
    988  1.1  christos     {
    989  1.1  christos       flags |= SEC_ALLOC;
    990  1.1  christos       if (hdr->sh_type != SHT_NOBITS)
    991  1.1  christos 	flags |= SEC_LOAD;
    992  1.1  christos     }
    993  1.1  christos   if ((hdr->sh_flags & SHF_WRITE) == 0)
    994  1.1  christos     flags |= SEC_READONLY;
    995  1.1  christos   if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
    996  1.1  christos     flags |= SEC_CODE;
    997  1.1  christos   else if ((flags & SEC_LOAD) != 0)
    998  1.1  christos     flags |= SEC_DATA;
    999  1.1  christos   if ((hdr->sh_flags & SHF_MERGE) != 0)
   1000  1.1  christos     {
   1001  1.1  christos       flags |= SEC_MERGE;
   1002  1.1  christos       newsect->entsize = hdr->sh_entsize;
   1003  1.1  christos     }
   1004  1.8  christos   if ((hdr->sh_flags & SHF_STRINGS) != 0)
   1005  1.8  christos     flags |= SEC_STRINGS;
   1006  1.1  christos   if (hdr->sh_flags & SHF_GROUP)
   1007  1.1  christos     if (!setup_group (abfd, hdr, newsect))
   1008  1.1  christos       return FALSE;
   1009  1.1  christos   if ((hdr->sh_flags & SHF_TLS) != 0)
   1010  1.1  christos     flags |= SEC_THREAD_LOCAL;
   1011  1.1  christos   if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
   1012  1.1  christos     flags |= SEC_EXCLUDE;
   1013  1.1  christos 
   1014  1.1  christos   if ((flags & SEC_ALLOC) == 0)
   1015  1.1  christos     {
   1016  1.1  christos       /* The debugging sections appear to be recognized only by name,
   1017  1.1  christos 	 not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
   1018  1.1  christos       if (name [0] == '.')
   1019  1.1  christos 	{
   1020  1.3  christos 	  const char *p;
   1021  1.3  christos 	  int n;
   1022  1.3  christos 	  if (name[1] == 'd')
   1023  1.3  christos 	    p = ".debug", n = 6;
   1024  1.3  christos 	  else if (name[1] == 'g' && name[2] == 'n')
   1025  1.3  christos 	    p = ".gnu.linkonce.wi.", n = 17;
   1026  1.3  christos 	  else if (name[1] == 'g' && name[2] == 'd')
   1027  1.3  christos 	    p = ".gdb_index", n = 11; /* yes we really do mean 11.  */
   1028  1.3  christos 	  else if (name[1] == 'l')
   1029  1.3  christos 	    p = ".line", n = 5;
   1030  1.3  christos 	  else if (name[1] == 's')
   1031  1.3  christos 	    p = ".stab", n = 5;
   1032  1.3  christos 	  else if (name[1] == 'z')
   1033  1.3  christos 	    p = ".zdebug", n = 7;
   1034  1.3  christos 	  else
   1035  1.3  christos 	    p = NULL, n = 0;
   1036  1.3  christos 	  if (p != NULL && strncmp (name, p, n) == 0)
   1037  1.1  christos 	    flags |= SEC_DEBUGGING;
   1038  1.1  christos 	}
   1039  1.1  christos     }
   1040  1.1  christos 
   1041  1.1  christos   /* As a GNU extension, if the name begins with .gnu.linkonce, we
   1042  1.1  christos      only link a single copy of the section.  This is used to support
   1043  1.1  christos      g++.  g++ will emit each template expansion in its own section.
   1044  1.1  christos      The symbols will be defined as weak, so that multiple definitions
   1045  1.1  christos      are permitted.  The GNU linker extension is to actually discard
   1046  1.1  christos      all but one of the sections.  */
   1047  1.1  christos   if (CONST_STRNEQ (name, ".gnu.linkonce")
   1048  1.1  christos       && elf_next_in_group (newsect) == NULL)
   1049  1.1  christos     flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
   1050  1.1  christos 
   1051  1.1  christos   bed = get_elf_backend_data (abfd);
   1052  1.1  christos   if (bed->elf_backend_section_flags)
   1053  1.1  christos     if (! bed->elf_backend_section_flags (&flags, hdr))
   1054  1.1  christos       return FALSE;
   1055  1.1  christos 
   1056  1.1  christos   if (! bfd_set_section_flags (abfd, newsect, flags))
   1057  1.1  christos     return FALSE;
   1058  1.1  christos 
   1059  1.1  christos   /* We do not parse the PT_NOTE segments as we are interested even in the
   1060  1.1  christos      separate debug info files which may have the segments offsets corrupted.
   1061  1.1  christos      PT_NOTEs from the core files are currently not parsed using BFD.  */
   1062  1.1  christos   if (hdr->sh_type == SHT_NOTE)
   1063  1.1  christos     {
   1064  1.1  christos       bfd_byte *contents;
   1065  1.1  christos 
   1066  1.1  christos       if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
   1067  1.1  christos 	return FALSE;
   1068  1.1  christos 
   1069  1.9  christos       elf_parse_notes (abfd, (char *) contents, hdr->sh_size, hdr->sh_offset);
   1070  1.1  christos       free (contents);
   1071  1.1  christos     }
   1072  1.1  christos 
   1073  1.1  christos   if ((flags & SEC_ALLOC) != 0)
   1074  1.1  christos     {
   1075  1.1  christos       Elf_Internal_Phdr *phdr;
   1076  1.1  christos       unsigned int i, nload;
   1077  1.1  christos 
   1078  1.1  christos       /* Some ELF linkers produce binaries with all the program header
   1079  1.1  christos 	 p_paddr fields zero.  If we have such a binary with more than
   1080  1.1  christos 	 one PT_LOAD header, then leave the section lma equal to vma
   1081  1.1  christos 	 so that we don't create sections with overlapping lma.  */
   1082  1.1  christos       phdr = elf_tdata (abfd)->phdr;
   1083  1.1  christos       for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
   1084  1.1  christos 	if (phdr->p_paddr != 0)
   1085  1.1  christos 	  break;
   1086  1.1  christos 	else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
   1087  1.1  christos 	  ++nload;
   1088  1.1  christos       if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
   1089  1.1  christos 	return TRUE;
   1090  1.1  christos 
   1091  1.1  christos       phdr = elf_tdata (abfd)->phdr;
   1092  1.1  christos       for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
   1093  1.1  christos 	{
   1094  1.3  christos 	  if (((phdr->p_type == PT_LOAD
   1095  1.3  christos 		&& (hdr->sh_flags & SHF_TLS) == 0)
   1096  1.3  christos 	       || phdr->p_type == PT_TLS)
   1097  1.1  christos 	      && ELF_SECTION_IN_SEGMENT (hdr, phdr))
   1098  1.1  christos 	    {
   1099  1.1  christos 	      if ((flags & SEC_LOAD) == 0)
   1100  1.1  christos 		newsect->lma = (phdr->p_paddr
   1101  1.1  christos 				+ hdr->sh_addr - phdr->p_vaddr);
   1102  1.1  christos 	      else
   1103  1.1  christos 		/* We used to use the same adjustment for SEC_LOAD
   1104  1.1  christos 		   sections, but that doesn't work if the segment
   1105  1.1  christos 		   is packed with code from multiple VMAs.
   1106  1.1  christos 		   Instead we calculate the section LMA based on
   1107  1.1  christos 		   the segment LMA.  It is assumed that the
   1108  1.1  christos 		   segment will contain sections with contiguous
   1109  1.1  christos 		   LMAs, even if the VMAs are not.  */
   1110  1.1  christos 		newsect->lma = (phdr->p_paddr
   1111  1.1  christos 				+ hdr->sh_offset - phdr->p_offset);
   1112  1.1  christos 
   1113  1.1  christos 	      /* With contiguous segments, we can't tell from file
   1114  1.1  christos 		 offsets whether a section with zero size should
   1115  1.1  christos 		 be placed at the end of one segment or the
   1116  1.1  christos 		 beginning of the next.  Decide based on vaddr.  */
   1117  1.1  christos 	      if (hdr->sh_addr >= phdr->p_vaddr
   1118  1.1  christos 		  && (hdr->sh_addr + hdr->sh_size
   1119  1.1  christos 		      <= phdr->p_vaddr + phdr->p_memsz))
   1120  1.1  christos 		break;
   1121  1.1  christos 	    }
   1122  1.1  christos 	}
   1123  1.1  christos     }
   1124  1.1  christos 
   1125  1.1  christos   /* Compress/decompress DWARF debug sections with names: .debug_* and
   1126  1.1  christos      .zdebug_*, after the section flags is set.  */
   1127  1.1  christos   if ((flags & SEC_DEBUGGING)
   1128  1.1  christos       && ((name[1] == 'd' && name[6] == '_')
   1129  1.1  christos 	  || (name[1] == 'z' && name[7] == '_')))
   1130  1.1  christos     {
   1131  1.1  christos       enum { nothing, compress, decompress } action = nothing;
   1132  1.6  christos       int compression_header_size;
   1133  1.6  christos       bfd_size_type uncompressed_size;
   1134  1.6  christos       bfd_boolean compressed
   1135  1.6  christos 	= bfd_is_section_compressed_with_header (abfd, newsect,
   1136  1.6  christos 						 &compression_header_size,
   1137  1.6  christos 						 &uncompressed_size);
   1138  1.1  christos 
   1139  1.6  christos       if (compressed)
   1140  1.1  christos 	{
   1141  1.1  christos 	  /* Compressed section.  Check if we should decompress.  */
   1142  1.1  christos 	  if ((abfd->flags & BFD_DECOMPRESS))
   1143  1.1  christos 	    action = decompress;
   1144  1.1  christos 	}
   1145  1.6  christos 
   1146  1.6  christos       /* Compress the uncompressed section or convert from/to .zdebug*
   1147  1.6  christos 	 section.  Check if we should compress.  */
   1148  1.6  christos       if (action == nothing)
   1149  1.6  christos 	{
   1150  1.6  christos 	  if (newsect->size != 0
   1151  1.6  christos 	      && (abfd->flags & BFD_COMPRESS)
   1152  1.6  christos 	      && compression_header_size >= 0
   1153  1.6  christos 	      && uncompressed_size > 0
   1154  1.6  christos 	      && (!compressed
   1155  1.6  christos 		  || ((compression_header_size > 0)
   1156  1.6  christos 		      != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
   1157  1.1  christos 	    action = compress;
   1158  1.6  christos 	  else
   1159  1.6  christos 	    return TRUE;
   1160  1.1  christos 	}
   1161  1.1  christos 
   1162  1.6  christos       if (action == compress)
   1163  1.1  christos 	{
   1164  1.1  christos 	  if (!bfd_init_section_compress_status (abfd, newsect))
   1165  1.1  christos 	    {
   1166  1.9  christos 	      _bfd_error_handler
   1167  1.9  christos 		/* xgettext:c-format */
   1168  1.3  christos 		(_("%B: unable to initialize compress status for section %s"),
   1169  1.1  christos 		 abfd, name);
   1170  1.1  christos 	      return FALSE;
   1171  1.1  christos 	    }
   1172  1.6  christos 	}
   1173  1.6  christos       else
   1174  1.6  christos 	{
   1175  1.1  christos 	  if (!bfd_init_section_decompress_status (abfd, newsect))
   1176  1.1  christos 	    {
   1177  1.9  christos 	      _bfd_error_handler
   1178  1.9  christos 		/* xgettext:c-format */
   1179  1.3  christos 		(_("%B: unable to initialize decompress status for section %s"),
   1180  1.1  christos 		 abfd, name);
   1181  1.1  christos 	      return FALSE;
   1182  1.1  christos 	    }
   1183  1.6  christos 	}
   1184  1.1  christos 
   1185  1.6  christos       if (abfd->is_linker_input)
   1186  1.6  christos 	{
   1187  1.6  christos 	  if (name[1] == 'z'
   1188  1.6  christos 	      && (action == decompress
   1189  1.6  christos 		  || (action == compress
   1190  1.6  christos 		      && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
   1191  1.6  christos 	    {
   1192  1.6  christos 	      /* Convert section name from .zdebug_* to .debug_* so
   1193  1.6  christos 		 that linker will consider this section as a debug
   1194  1.6  christos 		 section.  */
   1195  1.6  christos 	      char *new_name = convert_zdebug_to_debug (abfd, name);
   1196  1.1  christos 	      if (new_name == NULL)
   1197  1.1  christos 		return FALSE;
   1198  1.6  christos 	      bfd_rename_section (abfd, newsect, new_name);
   1199  1.1  christos 	    }
   1200  1.1  christos 	}
   1201  1.6  christos       else
   1202  1.6  christos 	/* For objdump, don't rename the section.  For objcopy, delay
   1203  1.6  christos 	   section rename to elf_fake_sections.  */
   1204  1.6  christos 	newsect->flags |= SEC_ELF_RENAME;
   1205  1.1  christos     }
   1206  1.1  christos 
   1207  1.1  christos   return TRUE;
   1208  1.1  christos }
   1209  1.1  christos 
   1210  1.8  christos const char *const bfd_elf_section_type_names[] =
   1211  1.8  christos {
   1212  1.1  christos   "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
   1213  1.1  christos   "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
   1214  1.1  christos   "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
   1215  1.1  christos };
   1216  1.1  christos 
   1217  1.1  christos /* ELF relocs are against symbols.  If we are producing relocatable
   1218  1.1  christos    output, and the reloc is against an external symbol, and nothing
   1219  1.1  christos    has given us any additional addend, the resulting reloc will also
   1220  1.1  christos    be against the same symbol.  In such a case, we don't want to
   1221  1.1  christos    change anything about the way the reloc is handled, since it will
   1222  1.1  christos    all be done at final link time.  Rather than put special case code
   1223  1.1  christos    into bfd_perform_relocation, all the reloc types use this howto
   1224  1.1  christos    function.  It just short circuits the reloc if producing
   1225  1.1  christos    relocatable output against an external symbol.  */
   1226  1.1  christos 
   1227  1.1  christos bfd_reloc_status_type
   1228  1.1  christos bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
   1229  1.1  christos 		       arelent *reloc_entry,
   1230  1.1  christos 		       asymbol *symbol,
   1231  1.1  christos 		       void *data ATTRIBUTE_UNUSED,
   1232  1.1  christos 		       asection *input_section,
   1233  1.1  christos 		       bfd *output_bfd,
   1234  1.1  christos 		       char **error_message ATTRIBUTE_UNUSED)
   1235  1.1  christos {
   1236  1.1  christos   if (output_bfd != NULL
   1237  1.1  christos       && (symbol->flags & BSF_SECTION_SYM) == 0
   1238  1.1  christos       && (! reloc_entry->howto->partial_inplace
   1239  1.1  christos 	  || reloc_entry->addend == 0))
   1240  1.1  christos     {
   1241  1.1  christos       reloc_entry->address += input_section->output_offset;
   1242  1.1  christos       return bfd_reloc_ok;
   1243  1.1  christos     }
   1244  1.1  christos 
   1245  1.1  christos   return bfd_reloc_continue;
   1246  1.1  christos }
   1247  1.1  christos 
   1248  1.8  christos /* Returns TRUE if section A matches section B.
   1250  1.8  christos    Names, addresses and links may be different, but everything else
   1251  1.8  christos    should be the same.  */
   1252  1.8  christos 
   1253  1.8  christos static bfd_boolean
   1254  1.8  christos section_match (const Elf_Internal_Shdr * a,
   1255  1.8  christos 	       const Elf_Internal_Shdr * b)
   1256  1.8  christos {
   1257  1.8  christos   return
   1258  1.8  christos     a->sh_type         == b->sh_type
   1259  1.8  christos     && (a->sh_flags & ~ SHF_INFO_LINK)
   1260  1.8  christos     == (b->sh_flags & ~ SHF_INFO_LINK)
   1261  1.8  christos     && a->sh_addralign == b->sh_addralign
   1262  1.8  christos     && a->sh_size      == b->sh_size
   1263  1.8  christos     && a->sh_entsize   == b->sh_entsize
   1264  1.8  christos     /* FIXME: Check sh_addr ?  */
   1265  1.8  christos     ;
   1266  1.8  christos }
   1267  1.8  christos 
   1268  1.8  christos /* Find a section in OBFD that has the same characteristics
   1269  1.8  christos    as IHEADER.  Return the index of this section or SHN_UNDEF if
   1270  1.8  christos    none can be found.  Check's section HINT first, as this is likely
   1271  1.8  christos    to be the correct section.  */
   1272  1.8  christos 
   1273  1.8  christos static unsigned int
   1274  1.8  christos find_link (const bfd * obfd, const Elf_Internal_Shdr * iheader, const unsigned int hint)
   1275  1.8  christos {
   1276  1.8  christos   Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
   1277  1.8  christos   unsigned int i;
   1278  1.9  christos 
   1279  1.9  christos   BFD_ASSERT (iheader != NULL);
   1280  1.9  christos 
   1281  1.9  christos   /* See PR 20922 for a reproducer of the NULL test.  */
   1282  1.9  christos   if (oheaders[hint] != NULL
   1283  1.8  christos       && section_match (oheaders[hint], iheader))
   1284  1.8  christos     return hint;
   1285  1.8  christos 
   1286  1.8  christos   for (i = 1; i < elf_numsections (obfd); i++)
   1287  1.8  christos     {
   1288  1.8  christos       Elf_Internal_Shdr * oheader = oheaders[i];
   1289  1.9  christos 
   1290  1.9  christos       if (oheader == NULL)
   1291  1.8  christos 	continue;
   1292  1.8  christos       if (section_match (oheader, iheader))
   1293  1.8  christos 	/* FIXME: Do we care if there is a potential for
   1294  1.8  christos 	   multiple matches ?  */
   1295  1.8  christos 	return i;
   1296  1.8  christos     }
   1297  1.8  christos 
   1298  1.8  christos   return SHN_UNDEF;
   1299  1.8  christos }
   1300  1.8  christos 
   1301  1.8  christos /* PR 19938: Attempt to set the ELF section header fields of an OS or
   1302  1.8  christos    Processor specific section, based upon a matching input section.
   1303  1.8  christos    Returns TRUE upon success, FALSE otherwise.  */
   1304  1.8  christos 
   1305  1.8  christos static bfd_boolean
   1306  1.8  christos copy_special_section_fields (const bfd *ibfd,
   1307  1.8  christos 			     bfd *obfd,
   1308  1.8  christos 			     const Elf_Internal_Shdr *iheader,
   1309  1.8  christos 			     Elf_Internal_Shdr *oheader,
   1310  1.8  christos 			     const unsigned int secnum)
   1311  1.8  christos {
   1312  1.8  christos   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
   1313  1.8  christos   const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
   1314  1.8  christos   bfd_boolean changed = FALSE;
   1315  1.8  christos   unsigned int sh_link;
   1316  1.8  christos 
   1317  1.8  christos   if (oheader->sh_type == SHT_NOBITS)
   1318  1.8  christos     {
   1319  1.8  christos       /* This is a feature for objcopy --only-keep-debug:
   1320  1.8  christos 	 When a section's type is changed to NOBITS, we preserve
   1321  1.8  christos 	 the sh_link and sh_info fields so that they can be
   1322  1.8  christos 	 matched up with the original.
   1323  1.8  christos 
   1324  1.8  christos 	 Note: Strictly speaking these assignments are wrong.
   1325  1.8  christos 	 The sh_link and sh_info fields should point to the
   1326  1.8  christos 	 relevent sections in the output BFD, which may not be in
   1327  1.8  christos 	 the same location as they were in the input BFD.  But
   1328  1.8  christos 	 the whole point of this action is to preserve the
   1329  1.8  christos 	 original values of the sh_link and sh_info fields, so
   1330  1.8  christos 	 that they can be matched up with the section headers in
   1331  1.8  christos 	 the original file.  So strictly speaking we may be
   1332  1.8  christos 	 creating an invalid ELF file, but it is only for a file
   1333  1.8  christos 	 that just contains debug info and only for sections
   1334  1.8  christos 	 without any contents.  */
   1335  1.8  christos       if (oheader->sh_link == 0)
   1336  1.8  christos 	oheader->sh_link = iheader->sh_link;
   1337  1.8  christos       if (oheader->sh_info == 0)
   1338  1.8  christos 	oheader->sh_info = iheader->sh_info;
   1339  1.8  christos       return TRUE;
   1340  1.8  christos     }
   1341  1.8  christos 
   1342  1.8  christos   /* Allow the target a chance to decide how these fields should be set.  */
   1343  1.8  christos   if (bed->elf_backend_copy_special_section_fields != NULL
   1344  1.8  christos       && bed->elf_backend_copy_special_section_fields
   1345  1.8  christos       (ibfd, obfd, iheader, oheader))
   1346  1.8  christos     return TRUE;
   1347  1.8  christos 
   1348  1.8  christos   /* We have an iheader which might match oheader, and which has non-zero
   1349  1.8  christos      sh_info and/or sh_link fields.  Attempt to follow those links and find
   1350  1.8  christos      the section in the output bfd which corresponds to the linked section
   1351  1.8  christos      in the input bfd.  */
   1352  1.8  christos   if (iheader->sh_link != SHN_UNDEF)
   1353  1.9  christos     {
   1354  1.9  christos       /* See PR 20931 for a reproducer.  */
   1355  1.9  christos       if (iheader->sh_link >= elf_numsections (ibfd))
   1356  1.9  christos 	{
   1357  1.9  christos 	  (* _bfd_error_handler)
   1358  1.9  christos 	    /* xgettext:c-format */
   1359  1.9  christos 	    (_("%B: Invalid sh_link field (%d) in section number %d"),
   1360  1.9  christos 	     ibfd, iheader->sh_link, secnum);
   1361  1.9  christos 	  return FALSE;
   1362  1.9  christos 	}
   1363  1.8  christos 
   1364  1.8  christos       sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
   1365  1.8  christos       if (sh_link != SHN_UNDEF)
   1366  1.8  christos 	{
   1367  1.8  christos 	  oheader->sh_link = sh_link;
   1368  1.8  christos 	  changed = TRUE;
   1369  1.8  christos 	}
   1370  1.8  christos       else
   1371  1.8  christos 	/* FIXME: Should we install iheader->sh_link
   1372  1.8  christos 	   if we could not find a match ?  */
   1373  1.9  christos 	(* _bfd_error_handler)
   1374  1.8  christos 	  /* xgettext:c-format */
   1375  1.8  christos 	  (_("%B: Failed to find link section for section %d"), obfd, secnum);
   1376  1.8  christos     }
   1377  1.8  christos 
   1378  1.8  christos   if (iheader->sh_info)
   1379  1.8  christos     {
   1380  1.8  christos       /* The sh_info field can hold arbitrary information, but if the
   1381  1.8  christos 	 SHF_LINK_INFO flag is set then it should be interpreted as a
   1382  1.8  christos 	 section index.  */
   1383  1.8  christos       if (iheader->sh_flags & SHF_INFO_LINK)
   1384  1.8  christos 	{
   1385  1.8  christos 	  sh_link = find_link (obfd, iheaders[iheader->sh_info],
   1386  1.8  christos 			       iheader->sh_info);
   1387  1.8  christos 	  if (sh_link != SHN_UNDEF)
   1388  1.8  christos 	    oheader->sh_flags |= SHF_INFO_LINK;
   1389  1.8  christos 	}
   1390  1.8  christos       else
   1391  1.8  christos 	/* No idea what it means - just copy it.  */
   1392  1.8  christos 	sh_link = iheader->sh_info;
   1393  1.8  christos 
   1394  1.8  christos       if (sh_link != SHN_UNDEF)
   1395  1.8  christos 	{
   1396  1.8  christos 	  oheader->sh_info = sh_link;
   1397  1.8  christos 	  changed = TRUE;
   1398  1.8  christos 	}
   1399  1.8  christos       else
   1400  1.9  christos 	(* _bfd_error_handler)
   1401  1.8  christos 	  /* xgettext:c-format */
   1402  1.8  christos 	  (_("%B: Failed to find info section for section %d"), obfd, secnum);
   1403  1.8  christos     }
   1404  1.8  christos 
   1405  1.8  christos   return changed;
   1406  1.8  christos }
   1407  1.1  christos 
   1408  1.1  christos /* Copy the program header and other data from one object module to
   1409  1.1  christos    another.  */
   1410  1.1  christos 
   1411  1.1  christos bfd_boolean
   1412  1.1  christos _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   1413  1.8  christos {
   1414  1.8  christos   const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
   1415  1.8  christos   Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
   1416  1.8  christos   const struct elf_backend_data *bed;
   1417  1.8  christos   unsigned int i;
   1418  1.1  christos 
   1419  1.8  christos   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   1420  1.1  christos     || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   1421  1.1  christos     return TRUE;
   1422  1.4  christos 
   1423  1.4  christos   if (!elf_flags_init (obfd))
   1424  1.4  christos     {
   1425  1.4  christos       elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
   1426  1.4  christos       elf_flags_init (obfd) = TRUE;
   1427  1.1  christos     }
   1428  1.1  christos 
   1429  1.4  christos   elf_gp (obfd) = elf_gp (ibfd);
   1430  1.4  christos 
   1431  1.4  christos   /* Also copy the EI_OSABI field.  */
   1432  1.4  christos   elf_elfheader (obfd)->e_ident[EI_OSABI] =
   1433  1.1  christos     elf_elfheader (ibfd)->e_ident[EI_OSABI];
   1434  1.8  christos 
   1435  1.8  christos   /* If set, copy the EI_ABIVERSION field.  */
   1436  1.8  christos   if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
   1437  1.8  christos     elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
   1438  1.8  christos       = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
   1439  1.1  christos 
   1440  1.1  christos   /* Copy object attributes.  */
   1441  1.8  christos   _bfd_elf_copy_obj_attributes (ibfd, obfd);
   1442  1.8  christos 
   1443  1.8  christos   if (iheaders == NULL || oheaders == NULL)
   1444  1.8  christos     return TRUE;
   1445  1.8  christos 
   1446  1.8  christos   bed = get_elf_backend_data (obfd);
   1447  1.8  christos 
   1448  1.8  christos   /* Possibly copy other fields in the section header.  */
   1449  1.8  christos   for (i = 1; i < elf_numsections (obfd); i++)
   1450  1.8  christos     {
   1451  1.8  christos       unsigned int j;
   1452  1.8  christos       Elf_Internal_Shdr * oheader = oheaders[i];
   1453  1.8  christos 
   1454  1.8  christos       /* Ignore ordinary sections.  SHT_NOBITS sections are considered however
   1455  1.8  christos 	 because of a special case need for generating separate debug info
   1456  1.8  christos 	 files.  See below for more details.  */
   1457  1.8  christos       if (oheader == NULL
   1458  1.8  christos 	  || (oheader->sh_type != SHT_NOBITS
   1459  1.8  christos 	      && oheader->sh_type < SHT_LOOS))
   1460  1.8  christos 	continue;
   1461  1.8  christos 
   1462  1.8  christos       /* Ignore empty sections, and sections whose
   1463  1.8  christos 	 fields have already been initialised.  */
   1464  1.8  christos       if (oheader->sh_size == 0
   1465  1.8  christos 	  || (oheader->sh_info != 0 && oheader->sh_link != 0))
   1466  1.8  christos 	continue;
   1467  1.8  christos 
   1468  1.8  christos       /* Scan for the matching section in the input bfd.
   1469  1.8  christos 	 First we try for a direct mapping between the input and output sections.  */
   1470  1.8  christos       for (j = 1; j < elf_numsections (ibfd); j++)
   1471  1.8  christos 	{
   1472  1.8  christos 	  const Elf_Internal_Shdr * iheader = iheaders[j];
   1473  1.8  christos 
   1474  1.8  christos 	  if (iheader == NULL)
   1475  1.8  christos 	    continue;
   1476  1.8  christos 
   1477  1.8  christos 	  if (oheader->bfd_section != NULL
   1478  1.8  christos 	      && iheader->bfd_section != NULL
   1479  1.8  christos 	      && iheader->bfd_section->output_section != NULL
   1480  1.8  christos 	      && iheader->bfd_section->output_section == oheader->bfd_section)
   1481  1.8  christos 	    {
   1482  1.8  christos 	      /* We have found a connection from the input section to the
   1483  1.8  christos 		 output section.  Attempt to copy the header fields.  If
   1484  1.8  christos 		 this fails then do not try any further sections - there
   1485  1.8  christos 		 should only be a one-to-one mapping between input and output. */
   1486  1.8  christos 	      if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
   1487  1.8  christos 		j = elf_numsections (ibfd);
   1488  1.8  christos 	      break;
   1489  1.8  christos 	    }
   1490  1.8  christos 	}
   1491  1.8  christos 
   1492  1.8  christos       if (j < elf_numsections (ibfd))
   1493  1.8  christos 	continue;
   1494  1.8  christos 
   1495  1.8  christos       /* That failed.  So try to deduce the corresponding input section.
   1496  1.8  christos 	 Unfortunately we cannot compare names as the output string table
   1497  1.8  christos 	 is empty, so instead we check size, address and type.  */
   1498  1.8  christos       for (j = 1; j < elf_numsections (ibfd); j++)
   1499  1.8  christos 	{
   1500  1.8  christos 	  const Elf_Internal_Shdr * iheader = iheaders[j];
   1501  1.8  christos 
   1502  1.8  christos 	  if (iheader == NULL)
   1503  1.8  christos 	    continue;
   1504  1.8  christos 
   1505  1.8  christos 	  /* Try matching fields in the input section's header.
   1506  1.8  christos 	     Since --only-keep-debug turns all non-debug sections into
   1507  1.8  christos 	     SHT_NOBITS sections, the output SHT_NOBITS type matches any
   1508  1.8  christos 	     input type.  */
   1509  1.8  christos 	  if ((oheader->sh_type == SHT_NOBITS
   1510  1.8  christos 	       || iheader->sh_type == oheader->sh_type)
   1511  1.8  christos 	      && (iheader->sh_flags & ~ SHF_INFO_LINK)
   1512  1.8  christos 	      == (oheader->sh_flags & ~ SHF_INFO_LINK)
   1513  1.8  christos 	      && iheader->sh_addralign == oheader->sh_addralign
   1514  1.8  christos 	      && iheader->sh_entsize == oheader->sh_entsize
   1515  1.8  christos 	      && iheader->sh_size == oheader->sh_size
   1516  1.8  christos 	      && iheader->sh_addr == oheader->sh_addr
   1517  1.8  christos 	      && (iheader->sh_info != oheader->sh_info
   1518  1.8  christos 		  || iheader->sh_link != oheader->sh_link))
   1519  1.8  christos 	    {
   1520  1.8  christos 	      if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
   1521  1.8  christos 		break;
   1522  1.8  christos 	    }
   1523  1.8  christos 	}
   1524  1.8  christos 
   1525  1.8  christos       if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
   1526  1.8  christos 	{
   1527  1.8  christos 	  /* Final attempt.  Call the backend copy function
   1528  1.8  christos 	     with a NULL input section.  */
   1529  1.8  christos 	  if (bed->elf_backend_copy_special_section_fields != NULL)
   1530  1.8  christos 	    bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader);
   1531  1.8  christos 	}
   1532  1.8  christos     }
   1533  1.1  christos 
   1534  1.1  christos   return TRUE;
   1535  1.1  christos }
   1536  1.1  christos 
   1537  1.1  christos static const char *
   1538  1.1  christos get_segment_type (unsigned int p_type)
   1539  1.1  christos {
   1540  1.1  christos   const char *pt;
   1541  1.1  christos   switch (p_type)
   1542  1.1  christos     {
   1543  1.1  christos     case PT_NULL: pt = "NULL"; break;
   1544  1.1  christos     case PT_LOAD: pt = "LOAD"; break;
   1545  1.1  christos     case PT_DYNAMIC: pt = "DYNAMIC"; break;
   1546  1.1  christos     case PT_INTERP: pt = "INTERP"; break;
   1547  1.1  christos     case PT_NOTE: pt = "NOTE"; break;
   1548  1.1  christos     case PT_SHLIB: pt = "SHLIB"; break;
   1549  1.1  christos     case PT_PHDR: pt = "PHDR"; break;
   1550  1.1  christos     case PT_TLS: pt = "TLS"; break;
   1551  1.1  christos     case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
   1552  1.1  christos     case PT_GNU_STACK: pt = "STACK"; break;
   1553  1.1  christos     case PT_GNU_RELRO: pt = "RELRO"; break;
   1554  1.1  christos     default: pt = NULL; break;
   1555  1.1  christos     }
   1556  1.1  christos   return pt;
   1557  1.1  christos }
   1558  1.1  christos 
   1559  1.1  christos /* Print out the program headers.  */
   1560  1.1  christos 
   1561  1.1  christos bfd_boolean
   1562  1.1  christos _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
   1563  1.1  christos {
   1564  1.1  christos   FILE *f = (FILE *) farg;
   1565  1.1  christos   Elf_Internal_Phdr *p;
   1566  1.1  christos   asection *s;
   1567  1.1  christos   bfd_byte *dynbuf = NULL;
   1568  1.1  christos 
   1569  1.1  christos   p = elf_tdata (abfd)->phdr;
   1570  1.1  christos   if (p != NULL)
   1571  1.1  christos     {
   1572  1.1  christos       unsigned int i, c;
   1573  1.1  christos 
   1574  1.1  christos       fprintf (f, _("\nProgram Header:\n"));
   1575  1.1  christos       c = elf_elfheader (abfd)->e_phnum;
   1576  1.1  christos       for (i = 0; i < c; i++, p++)
   1577  1.1  christos 	{
   1578  1.1  christos 	  const char *pt = get_segment_type (p->p_type);
   1579  1.1  christos 	  char buf[20];
   1580  1.1  christos 
   1581  1.1  christos 	  if (pt == NULL)
   1582  1.1  christos 	    {
   1583  1.1  christos 	      sprintf (buf, "0x%lx", p->p_type);
   1584  1.1  christos 	      pt = buf;
   1585  1.1  christos 	    }
   1586  1.1  christos 	  fprintf (f, "%8s off    0x", pt);
   1587  1.1  christos 	  bfd_fprintf_vma (abfd, f, p->p_offset);
   1588  1.1  christos 	  fprintf (f, " vaddr 0x");
   1589  1.1  christos 	  bfd_fprintf_vma (abfd, f, p->p_vaddr);
   1590  1.1  christos 	  fprintf (f, " paddr 0x");
   1591  1.1  christos 	  bfd_fprintf_vma (abfd, f, p->p_paddr);
   1592  1.1  christos 	  fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
   1593  1.1  christos 	  fprintf (f, "         filesz 0x");
   1594  1.1  christos 	  bfd_fprintf_vma (abfd, f, p->p_filesz);
   1595  1.1  christos 	  fprintf (f, " memsz 0x");
   1596  1.1  christos 	  bfd_fprintf_vma (abfd, f, p->p_memsz);
   1597  1.1  christos 	  fprintf (f, " flags %c%c%c",
   1598  1.1  christos 		   (p->p_flags & PF_R) != 0 ? 'r' : '-',
   1599  1.1  christos 		   (p->p_flags & PF_W) != 0 ? 'w' : '-',
   1600  1.1  christos 		   (p->p_flags & PF_X) != 0 ? 'x' : '-');
   1601  1.1  christos 	  if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
   1602  1.1  christos 	    fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
   1603  1.1  christos 	  fprintf (f, "\n");
   1604  1.1  christos 	}
   1605  1.1  christos     }
   1606  1.1  christos 
   1607  1.1  christos   s = bfd_get_section_by_name (abfd, ".dynamic");
   1608  1.1  christos   if (s != NULL)
   1609  1.1  christos     {
   1610  1.1  christos       unsigned int elfsec;
   1611  1.1  christos       unsigned long shlink;
   1612  1.1  christos       bfd_byte *extdyn, *extdynend;
   1613  1.1  christos       size_t extdynsize;
   1614  1.1  christos       void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   1615  1.1  christos 
   1616  1.1  christos       fprintf (f, _("\nDynamic Section:\n"));
   1617  1.1  christos 
   1618  1.1  christos       if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
   1619  1.1  christos 	goto error_return;
   1620  1.1  christos 
   1621  1.1  christos       elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   1622  1.1  christos       if (elfsec == SHN_BAD)
   1623  1.1  christos 	goto error_return;
   1624  1.1  christos       shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   1625  1.1  christos 
   1626  1.1  christos       extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   1627  1.1  christos       swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   1628  1.1  christos 
   1629  1.5  christos       extdyn = dynbuf;
   1630  1.5  christos       /* PR 17512: file: 6f427532.  */
   1631  1.5  christos       if (s->size < extdynsize)
   1632  1.1  christos 	goto error_return;
   1633  1.5  christos       extdynend = extdyn + s->size;
   1634  1.5  christos       /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
   1635  1.5  christos          Fix range check.  */
   1636  1.1  christos       for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
   1637  1.1  christos 	{
   1638  1.1  christos 	  Elf_Internal_Dyn dyn;
   1639  1.1  christos 	  const char *name = "";
   1640  1.1  christos 	  char ab[20];
   1641  1.1  christos 	  bfd_boolean stringp;
   1642  1.1  christos 	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   1643  1.1  christos 
   1644  1.1  christos 	  (*swap_dyn_in) (abfd, extdyn, &dyn);
   1645  1.1  christos 
   1646  1.1  christos 	  if (dyn.d_tag == DT_NULL)
   1647  1.1  christos 	    break;
   1648  1.1  christos 
   1649  1.1  christos 	  stringp = FALSE;
   1650  1.1  christos 	  switch (dyn.d_tag)
   1651  1.1  christos 	    {
   1652  1.1  christos 	    default:
   1653  1.1  christos 	      if (bed->elf_backend_get_target_dtag)
   1654  1.1  christos 		name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
   1655  1.1  christos 
   1656  1.1  christos 	      if (!strcmp (name, ""))
   1657  1.1  christos 		{
   1658  1.1  christos 		  sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
   1659  1.1  christos 		  name = ab;
   1660  1.1  christos 		}
   1661  1.1  christos 	      break;
   1662  1.1  christos 
   1663  1.1  christos 	    case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
   1664  1.1  christos 	    case DT_PLTRELSZ: name = "PLTRELSZ"; break;
   1665  1.1  christos 	    case DT_PLTGOT: name = "PLTGOT"; break;
   1666  1.1  christos 	    case DT_HASH: name = "HASH"; break;
   1667  1.1  christos 	    case DT_STRTAB: name = "STRTAB"; break;
   1668  1.1  christos 	    case DT_SYMTAB: name = "SYMTAB"; break;
   1669  1.1  christos 	    case DT_RELA: name = "RELA"; break;
   1670  1.1  christos 	    case DT_RELASZ: name = "RELASZ"; break;
   1671  1.1  christos 	    case DT_RELAENT: name = "RELAENT"; break;
   1672  1.1  christos 	    case DT_STRSZ: name = "STRSZ"; break;
   1673  1.1  christos 	    case DT_SYMENT: name = "SYMENT"; break;
   1674  1.1  christos 	    case DT_INIT: name = "INIT"; break;
   1675  1.1  christos 	    case DT_FINI: name = "FINI"; break;
   1676  1.1  christos 	    case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
   1677  1.1  christos 	    case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
   1678  1.1  christos 	    case DT_SYMBOLIC: name = "SYMBOLIC"; break;
   1679  1.1  christos 	    case DT_REL: name = "REL"; break;
   1680  1.1  christos 	    case DT_RELSZ: name = "RELSZ"; break;
   1681  1.1  christos 	    case DT_RELENT: name = "RELENT"; break;
   1682  1.1  christos 	    case DT_PLTREL: name = "PLTREL"; break;
   1683  1.1  christos 	    case DT_DEBUG: name = "DEBUG"; break;
   1684  1.1  christos 	    case DT_TEXTREL: name = "TEXTREL"; break;
   1685  1.1  christos 	    case DT_JMPREL: name = "JMPREL"; break;
   1686  1.1  christos 	    case DT_BIND_NOW: name = "BIND_NOW"; break;
   1687  1.1  christos 	    case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
   1688  1.1  christos 	    case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
   1689  1.1  christos 	    case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
   1690  1.1  christos 	    case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
   1691  1.1  christos 	    case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
   1692  1.1  christos 	    case DT_FLAGS: name = "FLAGS"; break;
   1693  1.1  christos 	    case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
   1694  1.1  christos 	    case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
   1695  1.1  christos 	    case DT_CHECKSUM: name = "CHECKSUM"; break;
   1696  1.1  christos 	    case DT_PLTPADSZ: name = "PLTPADSZ"; break;
   1697  1.1  christos 	    case DT_MOVEENT: name = "MOVEENT"; break;
   1698  1.1  christos 	    case DT_MOVESZ: name = "MOVESZ"; break;
   1699  1.1  christos 	    case DT_FEATURE: name = "FEATURE"; break;
   1700  1.1  christos 	    case DT_POSFLAG_1: name = "POSFLAG_1"; break;
   1701  1.1  christos 	    case DT_SYMINSZ: name = "SYMINSZ"; break;
   1702  1.1  christos 	    case DT_SYMINENT: name = "SYMINENT"; break;
   1703  1.1  christos 	    case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
   1704  1.1  christos 	    case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
   1705  1.1  christos 	    case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
   1706  1.1  christos 	    case DT_PLTPAD: name = "PLTPAD"; break;
   1707  1.1  christos 	    case DT_MOVETAB: name = "MOVETAB"; break;
   1708  1.1  christos 	    case DT_SYMINFO: name = "SYMINFO"; break;
   1709  1.1  christos 	    case DT_RELACOUNT: name = "RELACOUNT"; break;
   1710  1.1  christos 	    case DT_RELCOUNT: name = "RELCOUNT"; break;
   1711  1.1  christos 	    case DT_FLAGS_1: name = "FLAGS_1"; break;
   1712  1.1  christos 	    case DT_VERSYM: name = "VERSYM"; break;
   1713  1.1  christos 	    case DT_VERDEF: name = "VERDEF"; break;
   1714  1.1  christos 	    case DT_VERDEFNUM: name = "VERDEFNUM"; break;
   1715  1.1  christos 	    case DT_VERNEED: name = "VERNEED"; break;
   1716  1.1  christos 	    case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
   1717  1.1  christos 	    case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
   1718  1.1  christos 	    case DT_USED: name = "USED"; break;
   1719  1.1  christos 	    case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
   1720  1.1  christos 	    case DT_GNU_HASH: name = "GNU_HASH"; break;
   1721  1.1  christos 	    }
   1722  1.1  christos 
   1723  1.1  christos 	  fprintf (f, "  %-20s ", name);
   1724  1.1  christos 	  if (! stringp)
   1725  1.1  christos 	    {
   1726  1.1  christos 	      fprintf (f, "0x");
   1727  1.1  christos 	      bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
   1728  1.1  christos 	    }
   1729  1.1  christos 	  else
   1730  1.1  christos 	    {
   1731  1.1  christos 	      const char *string;
   1732  1.1  christos 	      unsigned int tagv = dyn.d_un.d_val;
   1733  1.1  christos 
   1734  1.1  christos 	      string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   1735  1.1  christos 	      if (string == NULL)
   1736  1.1  christos 		goto error_return;
   1737  1.1  christos 	      fprintf (f, "%s", string);
   1738  1.1  christos 	    }
   1739  1.1  christos 	  fprintf (f, "\n");
   1740  1.1  christos 	}
   1741  1.1  christos 
   1742  1.1  christos       free (dynbuf);
   1743  1.1  christos       dynbuf = NULL;
   1744  1.1  christos     }
   1745  1.1  christos 
   1746  1.1  christos   if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
   1747  1.1  christos       || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
   1748  1.1  christos     {
   1749  1.1  christos       if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
   1750  1.1  christos 	return FALSE;
   1751  1.1  christos     }
   1752  1.1  christos 
   1753  1.1  christos   if (elf_dynverdef (abfd) != 0)
   1754  1.1  christos     {
   1755  1.1  christos       Elf_Internal_Verdef *t;
   1756  1.1  christos 
   1757  1.1  christos       fprintf (f, _("\nVersion definitions:\n"));
   1758  1.1  christos       for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
   1759  1.1  christos 	{
   1760  1.1  christos 	  fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
   1761  1.1  christos 		   t->vd_flags, t->vd_hash,
   1762  1.1  christos 		   t->vd_nodename ? t->vd_nodename : "<corrupt>");
   1763  1.1  christos 	  if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
   1764  1.1  christos 	    {
   1765  1.1  christos 	      Elf_Internal_Verdaux *a;
   1766  1.1  christos 
   1767  1.1  christos 	      fprintf (f, "\t");
   1768  1.1  christos 	      for (a = t->vd_auxptr->vda_nextptr;
   1769  1.1  christos 		   a != NULL;
   1770  1.1  christos 		   a = a->vda_nextptr)
   1771  1.1  christos 		fprintf (f, "%s ",
   1772  1.1  christos 			 a->vda_nodename ? a->vda_nodename : "<corrupt>");
   1773  1.1  christos 	      fprintf (f, "\n");
   1774  1.1  christos 	    }
   1775  1.1  christos 	}
   1776  1.1  christos     }
   1777  1.1  christos 
   1778  1.1  christos   if (elf_dynverref (abfd) != 0)
   1779  1.1  christos     {
   1780  1.1  christos       Elf_Internal_Verneed *t;
   1781  1.1  christos 
   1782  1.1  christos       fprintf (f, _("\nVersion References:\n"));
   1783  1.1  christos       for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
   1784  1.1  christos 	{
   1785  1.1  christos 	  Elf_Internal_Vernaux *a;
   1786  1.1  christos 
   1787  1.1  christos 	  fprintf (f, _("  required from %s:\n"),
   1788  1.1  christos 		   t->vn_filename ? t->vn_filename : "<corrupt>");
   1789  1.1  christos 	  for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   1790  1.1  christos 	    fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
   1791  1.1  christos 		     a->vna_flags, a->vna_other,
   1792  1.1  christos 		     a->vna_nodename ? a->vna_nodename : "<corrupt>");
   1793  1.1  christos 	}
   1794  1.1  christos     }
   1795  1.1  christos 
   1796  1.1  christos   return TRUE;
   1797  1.1  christos 
   1798  1.1  christos  error_return:
   1799  1.1  christos   if (dynbuf != NULL)
   1800  1.1  christos     free (dynbuf);
   1801  1.1  christos   return FALSE;
   1802  1.1  christos }
   1803  1.5  christos 
   1804  1.5  christos /* Get version string.  */
   1805  1.5  christos 
   1806  1.5  christos const char *
   1807  1.5  christos _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
   1808  1.5  christos 				    bfd_boolean *hidden)
   1809  1.5  christos {
   1810  1.5  christos   const char *version_string = NULL;
   1811  1.5  christos   if (elf_dynversym (abfd) != 0
   1812  1.5  christos       && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
   1813  1.5  christos     {
   1814  1.5  christos       unsigned int vernum = ((elf_symbol_type *) symbol)->version;
   1815  1.5  christos 
   1816  1.5  christos       *hidden = (vernum & VERSYM_HIDDEN) != 0;
   1817  1.5  christos       vernum &= VERSYM_VERSION;
   1818  1.5  christos 
   1819  1.5  christos       if (vernum == 0)
   1820  1.5  christos 	version_string = "";
   1821  1.5  christos       else if (vernum == 1)
   1822  1.5  christos 	version_string = "Base";
   1823  1.5  christos       else if (vernum <= elf_tdata (abfd)->cverdefs)
   1824  1.5  christos 	version_string =
   1825  1.5  christos 	  elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
   1826  1.5  christos       else
   1827  1.5  christos 	{
   1828  1.5  christos 	  Elf_Internal_Verneed *t;
   1829  1.5  christos 
   1830  1.5  christos 	  version_string = "";
   1831  1.5  christos 	  for (t = elf_tdata (abfd)->verref;
   1832  1.5  christos 	       t != NULL;
   1833  1.5  christos 	       t = t->vn_nextref)
   1834  1.5  christos 	    {
   1835  1.5  christos 	      Elf_Internal_Vernaux *a;
   1836  1.5  christos 
   1837  1.5  christos 	      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   1838  1.5  christos 		{
   1839  1.5  christos 		  if (a->vna_other == vernum)
   1840  1.5  christos 		    {
   1841  1.5  christos 		      version_string = a->vna_nodename;
   1842  1.5  christos 		      break;
   1843  1.5  christos 		    }
   1844  1.5  christos 		}
   1845  1.5  christos 	    }
   1846  1.5  christos 	}
   1847  1.5  christos     }
   1848  1.5  christos   return version_string;
   1849  1.5  christos }
   1850  1.1  christos 
   1851  1.1  christos /* Display ELF-specific fields of a symbol.  */
   1852  1.1  christos 
   1853  1.1  christos void
   1854  1.1  christos bfd_elf_print_symbol (bfd *abfd,
   1855  1.1  christos 		      void *filep,
   1856  1.1  christos 		      asymbol *symbol,
   1857  1.1  christos 		      bfd_print_symbol_type how)
   1858  1.1  christos {
   1859  1.1  christos   FILE *file = (FILE *) filep;
   1860  1.1  christos   switch (how)
   1861  1.1  christos     {
   1862  1.1  christos     case bfd_print_symbol_name:
   1863  1.1  christos       fprintf (file, "%s", symbol->name);
   1864  1.1  christos       break;
   1865  1.1  christos     case bfd_print_symbol_more:
   1866  1.1  christos       fprintf (file, "elf ");
   1867  1.1  christos       bfd_fprintf_vma (abfd, file, symbol->value);
   1868  1.1  christos       fprintf (file, " %lx", (unsigned long) symbol->flags);
   1869  1.1  christos       break;
   1870  1.1  christos     case bfd_print_symbol_all:
   1871  1.1  christos       {
   1872  1.1  christos 	const char *section_name;
   1873  1.1  christos 	const char *name = NULL;
   1874  1.1  christos 	const struct elf_backend_data *bed;
   1875  1.1  christos 	unsigned char st_other;
   1876  1.5  christos 	bfd_vma val;
   1877  1.5  christos 	const char *version_string;
   1878  1.1  christos 	bfd_boolean hidden;
   1879  1.1  christos 
   1880  1.1  christos 	section_name = symbol->section ? symbol->section->name : "(*none*)";
   1881  1.1  christos 
   1882  1.1  christos 	bed = get_elf_backend_data (abfd);
   1883  1.1  christos 	if (bed->elf_backend_print_symbol_all)
   1884  1.1  christos 	  name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
   1885  1.1  christos 
   1886  1.1  christos 	if (name == NULL)
   1887  1.1  christos 	  {
   1888  1.1  christos 	    name = symbol->name;
   1889  1.1  christos 	    bfd_print_symbol_vandf (abfd, file, symbol);
   1890  1.1  christos 	  }
   1891  1.1  christos 
   1892  1.1  christos 	fprintf (file, " %s\t", section_name);
   1893  1.1  christos 	/* Print the "other" value for a symbol.  For common symbols,
   1894  1.1  christos 	   we've already printed the size; now print the alignment.
   1895  1.1  christos 	   For other symbols, we have no specified alignment, and
   1896  1.1  christos 	   we've printed the address; now print the size.  */
   1897  1.1  christos 	if (symbol->section && bfd_is_com_section (symbol->section))
   1898  1.1  christos 	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
   1899  1.1  christos 	else
   1900  1.1  christos 	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
   1901  1.1  christos 	bfd_fprintf_vma (abfd, file, val);
   1902  1.1  christos 
   1903  1.5  christos 	/* If we have version information, print it.  */
   1904  1.5  christos 	version_string = _bfd_elf_get_symbol_version_string (abfd,
   1905  1.5  christos 							     symbol,
   1906  1.5  christos 							     &hidden);
   1907  1.1  christos 	if (version_string)
   1908  1.5  christos 	  {
   1909  1.1  christos 	    if (!hidden)
   1910  1.1  christos 	      fprintf (file, "  %-11s", version_string);
   1911  1.1  christos 	    else
   1912  1.1  christos 	      {
   1913  1.1  christos 		int i;
   1914  1.1  christos 
   1915  1.1  christos 		fprintf (file, " (%s)", version_string);
   1916  1.1  christos 		for (i = 10 - strlen (version_string); i > 0; --i)
   1917  1.1  christos 		  putc (' ', file);
   1918  1.1  christos 	      }
   1919  1.1  christos 	  }
   1920  1.1  christos 
   1921  1.1  christos 	/* If the st_other field is not zero, print it.  */
   1922  1.1  christos 	st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
   1923  1.1  christos 
   1924  1.1  christos 	switch (st_other)
   1925  1.1  christos 	  {
   1926  1.1  christos 	  case 0: break;
   1927  1.1  christos 	  case STV_INTERNAL:  fprintf (file, " .internal");  break;
   1928  1.1  christos 	  case STV_HIDDEN:    fprintf (file, " .hidden");    break;
   1929  1.1  christos 	  case STV_PROTECTED: fprintf (file, " .protected"); break;
   1930  1.1  christos 	  default:
   1931  1.1  christos 	    /* Some other non-defined flags are also present, so print
   1932  1.1  christos 	       everything hex.  */
   1933  1.1  christos 	    fprintf (file, " 0x%02x", (unsigned int) st_other);
   1934  1.1  christos 	  }
   1935  1.1  christos 
   1936  1.1  christos 	fprintf (file, " %s", name);
   1937  1.1  christos       }
   1938  1.1  christos       break;
   1939  1.1  christos     }
   1940  1.1  christos }
   1941  1.1  christos 
   1942  1.1  christos /* ELF .o/exec file reading */
   1944  1.1  christos 
   1945  1.1  christos /* Create a new bfd section from an ELF section header.  */
   1946  1.1  christos 
   1947  1.1  christos bfd_boolean
   1948  1.1  christos bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
   1949  1.1  christos {
   1950  1.1  christos   Elf_Internal_Shdr *hdr;
   1951  1.1  christos   Elf_Internal_Ehdr *ehdr;
   1952  1.5  christos   const struct elf_backend_data *bed;
   1953  1.5  christos   const char *name;
   1954  1.5  christos   bfd_boolean ret = TRUE;
   1955  1.5  christos   static bfd_boolean * sections_being_created = NULL;
   1956  1.1  christos   static bfd * sections_being_created_abfd = NULL;
   1957  1.1  christos   static unsigned int nesting = 0;
   1958  1.1  christos 
   1959  1.1  christos   if (shindex >= elf_numsections (abfd))
   1960  1.5  christos     return FALSE;
   1961  1.5  christos 
   1962  1.5  christos   if (++ nesting > 3)
   1963  1.5  christos     {
   1964  1.5  christos       /* PR17512: A corrupt ELF binary might contain a recursive group of
   1965  1.5  christos 	 sections, with each the string indicies pointing to the next in the
   1966  1.5  christos 	 loop.  Detect this here, by refusing to load a section that we are
   1967  1.5  christos 	 already in the process of loading.  We only trigger this test if
   1968  1.5  christos 	 we have nested at least three sections deep as normal ELF binaries
   1969  1.5  christos 	 can expect to recurse at least once.
   1970  1.5  christos 
   1971  1.5  christos 	 FIXME: It would be better if this array was attached to the bfd,
   1972  1.5  christos 	 rather than being held in a static pointer.  */
   1973  1.5  christos 
   1974  1.5  christos       if (sections_being_created_abfd != abfd)
   1975  1.5  christos 	sections_being_created = NULL;
   1976  1.5  christos       if (sections_being_created == NULL)
   1977  1.5  christos 	{
   1978  1.5  christos 	  /* FIXME: It would be more efficient to attach this array to the bfd somehow.  */
   1979  1.5  christos 	  sections_being_created = (bfd_boolean *)
   1980  1.5  christos 	    bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean));
   1981  1.5  christos 	  sections_being_created_abfd = abfd;
   1982  1.5  christos 	}
   1983  1.9  christos       if (sections_being_created [shindex])
   1984  1.5  christos 	{
   1985  1.5  christos 	  _bfd_error_handler
   1986  1.5  christos 	    (_("%B: warning: loop in section dependencies detected"), abfd);
   1987  1.5  christos 	  return FALSE;
   1988  1.5  christos 	}
   1989  1.5  christos       sections_being_created [shindex] = TRUE;
   1990  1.1  christos     }
   1991  1.1  christos 
   1992  1.1  christos   hdr = elf_elfsections (abfd)[shindex];
   1993  1.1  christos   ehdr = elf_elfheader (abfd);
   1994  1.1  christos   name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
   1995  1.5  christos 					  hdr->sh_name);
   1996  1.1  christos   if (name == NULL)
   1997  1.1  christos     goto fail;
   1998  1.1  christos 
   1999  1.1  christos   bed = get_elf_backend_data (abfd);
   2000  1.1  christos   switch (hdr->sh_type)
   2001  1.1  christos     {
   2002  1.5  christos     case SHT_NULL:
   2003  1.1  christos       /* Inactive section. Throw it away.  */
   2004  1.5  christos       goto success;
   2005  1.5  christos 
   2006  1.5  christos     case SHT_PROGBITS:		/* Normal section with contents.  */
   2007  1.5  christos     case SHT_NOBITS:		/* .bss section.  */
   2008  1.1  christos     case SHT_HASH:		/* .hash section.  */
   2009  1.1  christos     case SHT_NOTE:		/* .note section.  */
   2010  1.1  christos     case SHT_INIT_ARRAY:	/* .init_array section.  */
   2011  1.1  christos     case SHT_FINI_ARRAY:	/* .fini_array section.  */
   2012  1.1  christos     case SHT_PREINIT_ARRAY:	/* .preinit_array section.  */
   2013  1.5  christos     case SHT_GNU_LIBLIST:	/* .gnu.liblist section.  */
   2014  1.5  christos     case SHT_GNU_HASH:		/* .gnu.hash section.  */
   2015  1.1  christos       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2016  1.1  christos       goto success;
   2017  1.1  christos 
   2018  1.5  christos     case SHT_DYNAMIC:	/* Dynamic linking information.  */
   2019  1.5  christos       if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2020  1.1  christos 	goto fail;
   2021  1.1  christos 
   2022  1.1  christos       if (hdr->sh_link > elf_numsections (abfd))
   2023  1.1  christos 	{
   2024  1.1  christos 	  /* PR 10478: Accept Solaris binaries with a sh_link
   2025  1.1  christos 	     field set to SHN_BEFORE or SHN_AFTER.  */
   2026  1.1  christos 	  switch (bfd_get_arch (abfd))
   2027  1.1  christos 	    {
   2028  1.1  christos 	    case bfd_arch_i386:
   2029  1.1  christos 	    case bfd_arch_sparc:
   2030  1.1  christos 	      if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
   2031  1.1  christos 		  || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
   2032  1.1  christos 		break;
   2033  1.5  christos 	      /* Otherwise fall through.  */
   2034  1.1  christos 	    default:
   2035  1.1  christos 	      goto fail;
   2036  1.1  christos 	    }
   2037  1.5  christos 	}
   2038  1.1  christos       else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
   2039  1.1  christos 	goto fail;
   2040  1.1  christos       else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
   2041  1.1  christos 	{
   2042  1.1  christos 	  Elf_Internal_Shdr *dynsymhdr;
   2043  1.1  christos 
   2044  1.1  christos 	  /* The shared libraries distributed with hpux11 have a bogus
   2045  1.1  christos 	     sh_link field for the ".dynamic" section.  Find the
   2046  1.1  christos 	     string table for the ".dynsym" section instead.  */
   2047  1.1  christos 	  if (elf_dynsymtab (abfd) != 0)
   2048  1.1  christos 	    {
   2049  1.1  christos 	      dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
   2050  1.1  christos 	      hdr->sh_link = dynsymhdr->sh_link;
   2051  1.1  christos 	    }
   2052  1.1  christos 	  else
   2053  1.1  christos 	    {
   2054  1.1  christos 	      unsigned int i, num_sec;
   2055  1.1  christos 
   2056  1.1  christos 	      num_sec = elf_numsections (abfd);
   2057  1.1  christos 	      for (i = 1; i < num_sec; i++)
   2058  1.1  christos 		{
   2059  1.1  christos 		  dynsymhdr = elf_elfsections (abfd)[i];
   2060  1.1  christos 		  if (dynsymhdr->sh_type == SHT_DYNSYM)
   2061  1.1  christos 		    {
   2062  1.1  christos 		      hdr->sh_link = dynsymhdr->sh_link;
   2063  1.1  christos 		      break;
   2064  1.1  christos 		    }
   2065  1.1  christos 		}
   2066  1.5  christos 	    }
   2067  1.1  christos 	}
   2068  1.5  christos       goto success;
   2069  1.1  christos 
   2070  1.5  christos     case SHT_SYMTAB:		/* A symbol table.  */
   2071  1.1  christos       if (elf_onesymtab (abfd) == shindex)
   2072  1.1  christos 	goto success;
   2073  1.5  christos 
   2074  1.5  christos       if (hdr->sh_entsize != bed->s->sizeof_sym)
   2075  1.1  christos 	goto fail;
   2076  1.3  christos 
   2077  1.3  christos       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
   2078  1.5  christos 	{
   2079  1.3  christos 	  if (hdr->sh_size != 0)
   2080  1.3  christos 	    goto fail;
   2081  1.3  christos 	  /* Some assemblers erroneously set sh_info to one with a
   2082  1.3  christos 	     zero sh_size.  ld sees this as a global symbol count
   2083  1.5  christos 	     of (unsigned) -1.  Fix it here.  */
   2084  1.3  christos 	  hdr->sh_info = 0;
   2085  1.5  christos 	  goto success;
   2086  1.8  christos 	}
   2087  1.8  christos 
   2088  1.8  christos       /* PR 18854: A binary might contain more than one symbol table.
   2089  1.8  christos 	 Unusual, but possible.  Warn, but continue.  */
   2090  1.9  christos       if (elf_onesymtab (abfd) != 0)
   2091  1.9  christos 	{
   2092  1.9  christos 	  _bfd_error_handler
   2093  1.9  christos 	    /* xgettext:c-format */
   2094  1.8  christos 	    (_("%B: warning: multiple symbol tables detected"
   2095  1.8  christos 	       " - ignoring the table in section %u"),
   2096  1.8  christos 	     abfd, shindex);
   2097  1.1  christos 	  goto success;
   2098  1.8  christos 	}
   2099  1.8  christos       elf_onesymtab (abfd) = shindex;
   2100  1.1  christos       elf_symtab_hdr (abfd) = *hdr;
   2101  1.1  christos       elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
   2102  1.1  christos       abfd->flags |= HAS_SYMS;
   2103  1.1  christos 
   2104  1.1  christos       /* Sometimes a shared object will map in the symbol table.  If
   2105  1.1  christos 	 SHF_ALLOC is set, and this is a shared object, then we also
   2106  1.1  christos 	 treat this section as a BFD section.  We can not base the
   2107  1.1  christos 	 decision purely on SHF_ALLOC, because that flag is sometimes
   2108  1.1  christos 	 set in a relocatable object file, which would confuse the
   2109  1.1  christos 	 linker.  */
   2110  1.1  christos       if ((hdr->sh_flags & SHF_ALLOC) != 0
   2111  1.1  christos 	  && (abfd->flags & DYNAMIC) != 0
   2112  1.5  christos 	  && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2113  1.1  christos 						shindex))
   2114  1.1  christos 	goto fail;
   2115  1.1  christos 
   2116  1.1  christos       /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
   2117  1.8  christos 	 can't read symbols without that section loaded as well.  It
   2118  1.8  christos 	 is most likely specified by the next section header.  */
   2119  1.8  christos       {
   2120  1.8  christos 	elf_section_list * entry;
   2121  1.8  christos 	unsigned int i, num_sec;
   2122  1.8  christos 
   2123  1.8  christos 	for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
   2124  1.8  christos 	  if (entry->hdr.sh_link == shindex)
   2125  1.8  christos 	    goto success;
   2126  1.8  christos 
   2127  1.8  christos 	num_sec = elf_numsections (abfd);
   2128  1.8  christos 	for (i = shindex + 1; i < num_sec; i++)
   2129  1.8  christos 	  {
   2130  1.8  christos 	    Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2131  1.8  christos 
   2132  1.8  christos 	    if (hdr2->sh_type == SHT_SYMTAB_SHNDX
   2133  1.8  christos 		&& hdr2->sh_link == shindex)
   2134  1.1  christos 	      break;
   2135  1.8  christos 	  }
   2136  1.8  christos 
   2137  1.1  christos 	if (i == num_sec)
   2138  1.1  christos 	  for (i = 1; i < shindex; i++)
   2139  1.8  christos 	    {
   2140  1.1  christos 	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2141  1.1  christos 
   2142  1.1  christos 	      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
   2143  1.1  christos 		  && hdr2->sh_link == shindex)
   2144  1.8  christos 		break;
   2145  1.8  christos 	    }
   2146  1.8  christos 
   2147  1.8  christos 	if (i != shindex)
   2148  1.8  christos 	  ret = bfd_section_from_shdr (abfd, i);
   2149  1.8  christos 	/* else FIXME: we have failed to find the symbol table - should we issue an error ? */
   2150  1.1  christos 	goto success;
   2151  1.5  christos       }
   2152  1.1  christos 
   2153  1.5  christos     case SHT_DYNSYM:		/* A dynamic symbol table.  */
   2154  1.1  christos       if (elf_dynsymtab (abfd) == shindex)
   2155  1.1  christos 	goto success;
   2156  1.5  christos 
   2157  1.5  christos       if (hdr->sh_entsize != bed->s->sizeof_sym)
   2158  1.3  christos 	goto fail;
   2159  1.3  christos 
   2160  1.3  christos       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
   2161  1.5  christos 	{
   2162  1.5  christos 	  if (hdr->sh_size != 0)
   2163  1.3  christos 	    goto fail;
   2164  1.3  christos 
   2165  1.3  christos 	  /* Some linkers erroneously set sh_info to one with a
   2166  1.3  christos 	     zero sh_size.  ld sees this as a global symbol count
   2167  1.5  christos 	     of (unsigned) -1.  Fix it here.  */
   2168  1.3  christos 	  hdr->sh_info = 0;
   2169  1.5  christos 	  goto success;
   2170  1.8  christos 	}
   2171  1.8  christos 
   2172  1.8  christos       /* PR 18854: A binary might contain more than one dynamic symbol table.
   2173  1.8  christos 	 Unusual, but possible.  Warn, but continue.  */
   2174  1.9  christos       if (elf_dynsymtab (abfd) != 0)
   2175  1.9  christos 	{
   2176  1.9  christos 	  _bfd_error_handler
   2177  1.9  christos 	    /* xgettext:c-format */
   2178  1.8  christos 	    (_("%B: warning: multiple dynamic symbol tables detected"
   2179  1.8  christos 	       " - ignoring the table in section %u"),
   2180  1.8  christos 	     abfd, shindex);
   2181  1.1  christos 	  goto success;
   2182  1.1  christos 	}
   2183  1.1  christos       elf_dynsymtab (abfd) = shindex;
   2184  1.1  christos       elf_tdata (abfd)->dynsymtab_hdr = *hdr;
   2185  1.1  christos       elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   2186  1.1  christos       abfd->flags |= HAS_SYMS;
   2187  1.1  christos 
   2188  1.5  christos       /* Besides being a symbol table, we also treat this as a regular
   2189  1.5  christos 	 section, so that objcopy can handle it.  */
   2190  1.1  christos       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2191  1.5  christos       goto success;
   2192  1.8  christos 
   2193  1.8  christos     case SHT_SYMTAB_SHNDX:	/* Symbol section indices when >64k sections.  */
   2194  1.8  christos       {
   2195  1.8  christos 	elf_section_list * entry;
   2196  1.8  christos 
   2197  1.8  christos 	for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
   2198  1.8  christos 	  if (entry->ndx == shindex)
   2199  1.8  christos 	    goto success;
   2200  1.8  christos 
   2201  1.8  christos 	entry = bfd_alloc (abfd, sizeof * entry);
   2202  1.8  christos 	if (entry == NULL)
   2203  1.8  christos 	  goto fail;
   2204  1.8  christos 	entry->ndx = shindex;
   2205  1.8  christos 	entry->hdr = * hdr;
   2206  1.8  christos 	entry->next = elf_symtab_shndx_list (abfd);
   2207  1.5  christos 	elf_symtab_shndx_list (abfd) = entry;
   2208  1.8  christos 	elf_elfsections (abfd)[shindex] = & entry->hdr;
   2209  1.1  christos 	goto success;
   2210  1.5  christos       }
   2211  1.1  christos 
   2212  1.5  christos     case SHT_STRTAB:		/* A string table.  */
   2213  1.5  christos       if (hdr->bfd_section != NULL)
   2214  1.1  christos 	goto success;
   2215  1.1  christos 
   2216  1.1  christos       if (ehdr->e_shstrndx == shindex)
   2217  1.1  christos 	{
   2218  1.5  christos 	  elf_tdata (abfd)->shstrtab_hdr = *hdr;
   2219  1.1  christos 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
   2220  1.5  christos 	  goto success;
   2221  1.1  christos 	}
   2222  1.1  christos 
   2223  1.1  christos       if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
   2224  1.1  christos 	{
   2225  1.1  christos 	symtab_strtab:
   2226  1.5  christos 	  elf_tdata (abfd)->strtab_hdr = *hdr;
   2227  1.1  christos 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
   2228  1.5  christos 	  goto success;
   2229  1.1  christos 	}
   2230  1.1  christos 
   2231  1.1  christos       if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
   2232  1.1  christos 	{
   2233  1.1  christos 	dynsymtab_strtab:
   2234  1.1  christos 	  elf_tdata (abfd)->dynstrtab_hdr = *hdr;
   2235  1.1  christos 	  hdr = &elf_tdata (abfd)->dynstrtab_hdr;
   2236  1.1  christos 	  elf_elfsections (abfd)[shindex] = hdr;
   2237  1.5  christos 	  /* We also treat this as a regular section, so that objcopy
   2238  1.5  christos 	     can handle it.  */
   2239  1.5  christos 	  ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2240  1.1  christos 						 shindex);
   2241  1.1  christos 	  goto success;
   2242  1.1  christos 	}
   2243  1.1  christos 
   2244  1.1  christos       /* If the string table isn't one of the above, then treat it as a
   2245  1.1  christos 	 regular section.  We need to scan all the headers to be sure,
   2246  1.1  christos 	 just in case this strtab section appeared before the above.  */
   2247  1.1  christos       if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
   2248  1.1  christos 	{
   2249  1.1  christos 	  unsigned int i, num_sec;
   2250  1.1  christos 
   2251  1.1  christos 	  num_sec = elf_numsections (abfd);
   2252  1.1  christos 	  for (i = 1; i < num_sec; i++)
   2253  1.1  christos 	    {
   2254  1.1  christos 	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2255  1.1  christos 	      if (hdr2->sh_link == shindex)
   2256  1.1  christos 		{
   2257  1.5  christos 		  /* Prevent endless recursion on broken objects.  */
   2258  1.1  christos 		  if (i == shindex)
   2259  1.5  christos 		    goto fail;
   2260  1.1  christos 		  if (! bfd_section_from_shdr (abfd, i))
   2261  1.1  christos 		    goto fail;
   2262  1.1  christos 		  if (elf_onesymtab (abfd) == i)
   2263  1.1  christos 		    goto symtab_strtab;
   2264  1.1  christos 		  if (elf_dynsymtab (abfd) == i)
   2265  1.1  christos 		    goto dynsymtab_strtab;
   2266  1.1  christos 		}
   2267  1.5  christos 	    }
   2268  1.5  christos 	}
   2269  1.1  christos       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2270  1.1  christos       goto success;
   2271  1.1  christos 
   2272  1.1  christos     case SHT_REL:
   2273  1.1  christos     case SHT_RELA:
   2274  1.1  christos       /* *These* do a lot of work -- but build no sections!  */
   2275  1.1  christos       {
   2276  1.1  christos 	asection *target_sect;
   2277  1.1  christos 	Elf_Internal_Shdr *hdr2, **p_hdr;
   2278  1.1  christos 	unsigned int num_sec = elf_numsections (abfd);
   2279  1.1  christos 	struct bfd_elf_section_data *esdt;
   2280  1.1  christos 
   2281  1.1  christos 	if (hdr->sh_entsize
   2282  1.5  christos 	    != (bfd_size_type) (hdr->sh_type == SHT_REL
   2283  1.1  christos 				? bed->s->sizeof_rel : bed->s->sizeof_rela))
   2284  1.1  christos 	  goto fail;
   2285  1.1  christos 
   2286  1.1  christos 	/* Check for a bogus link to avoid crashing.  */
   2287  1.9  christos 	if (hdr->sh_link >= num_sec)
   2288  1.9  christos 	  {
   2289  1.9  christos 	    _bfd_error_handler
   2290  1.9  christos 	      /* xgettext:c-format */
   2291  1.5  christos 	      (_("%B: invalid link %lu for reloc section %s (index %u)"),
   2292  1.5  christos 	       abfd, hdr->sh_link, name, shindex);
   2293  1.5  christos 	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2294  1.1  christos 						   shindex);
   2295  1.1  christos 	    goto success;
   2296  1.1  christos 	  }
   2297  1.1  christos 
   2298  1.1  christos 	/* For some incomprehensible reason Oracle distributes
   2299  1.1  christos 	   libraries for Solaris in which some of the objects have
   2300  1.1  christos 	   bogus sh_link fields.  It would be nice if we could just
   2301  1.1  christos 	   reject them, but, unfortunately, some people need to use
   2302  1.1  christos 	   them.  We scan through the section headers; if we find only
   2303  1.1  christos 	   one suitable symbol table, we clobber the sh_link to point
   2304  1.1  christos 	   to it.  I hope this doesn't break anything.
   2305  1.1  christos 
   2306  1.1  christos 	   Don't do it on executable nor shared library.  */
   2307  1.1  christos 	if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
   2308  1.1  christos 	    && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
   2309  1.1  christos 	    && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
   2310  1.1  christos 	  {
   2311  1.1  christos 	    unsigned int scan;
   2312  1.1  christos 	    int found;
   2313  1.1  christos 
   2314  1.1  christos 	    found = 0;
   2315  1.1  christos 	    for (scan = 1; scan < num_sec; scan++)
   2316  1.1  christos 	      {
   2317  1.1  christos 		if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
   2318  1.1  christos 		    || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
   2319  1.1  christos 		  {
   2320  1.1  christos 		    if (found != 0)
   2321  1.1  christos 		      {
   2322  1.1  christos 			found = 0;
   2323  1.1  christos 			break;
   2324  1.1  christos 		      }
   2325  1.1  christos 		    found = scan;
   2326  1.1  christos 		  }
   2327  1.1  christos 	      }
   2328  1.1  christos 	    if (found != 0)
   2329  1.1  christos 	      hdr->sh_link = found;
   2330  1.1  christos 	  }
   2331  1.1  christos 
   2332  1.1  christos 	/* Get the symbol table.  */
   2333  1.1  christos 	if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
   2334  1.5  christos 	     || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
   2335  1.1  christos 	    && ! bfd_section_from_shdr (abfd, hdr->sh_link))
   2336  1.1  christos 	  goto fail;
   2337  1.1  christos 
   2338  1.1  christos 	/* If this reloc section does not use the main symbol table we
   2339  1.1  christos 	   don't treat it as a reloc section.  BFD can't adequately
   2340  1.1  christos 	   represent such a section, so at least for now, we don't
   2341  1.1  christos 	   try.  We just present it as a normal section.  We also
   2342  1.1  christos 	   can't use it as a reloc section if it points to the null
   2343  1.1  christos 	   section, an invalid section, another reloc section, or its
   2344  1.1  christos 	   sh_link points to the null section.  */
   2345  1.1  christos 	if (hdr->sh_link != elf_onesymtab (abfd)
   2346  1.1  christos 	    || hdr->sh_link == SHN_UNDEF
   2347  1.1  christos 	    || hdr->sh_info == SHN_UNDEF
   2348  1.1  christos 	    || hdr->sh_info >= num_sec
   2349  1.5  christos 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
   2350  1.5  christos 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
   2351  1.5  christos 	  {
   2352  1.5  christos 	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2353  1.5  christos 						   shindex);
   2354  1.1  christos 	    goto success;
   2355  1.1  christos 	  }
   2356  1.5  christos 
   2357  1.5  christos 	if (! bfd_section_from_shdr (abfd, hdr->sh_info))
   2358  1.1  christos 	  goto fail;
   2359  1.1  christos 
   2360  1.5  christos 	target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
   2361  1.1  christos 	if (target_sect == NULL)
   2362  1.1  christos 	  goto fail;
   2363  1.1  christos 
   2364  1.1  christos 	esdt = elf_section_data (target_sect);
   2365  1.1  christos 	if (hdr->sh_type == SHT_RELA)
   2366  1.1  christos 	  p_hdr = &esdt->rela.hdr;
   2367  1.1  christos 	else
   2368  1.5  christos 	  p_hdr = &esdt->rel.hdr;
   2369  1.5  christos 
   2370  1.5  christos 	/* PR 17512: file: 0b4f81b7.  */
   2371  1.8  christos 	if (*p_hdr != NULL)
   2372  1.1  christos 	  goto fail;
   2373  1.5  christos 	hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
   2374  1.1  christos 	if (hdr2 == NULL)
   2375  1.1  christos 	  goto fail;
   2376  1.1  christos 	*hdr2 = *hdr;
   2377  1.1  christos 	*p_hdr = hdr2;
   2378  1.1  christos 	elf_elfsections (abfd)[shindex] = hdr2;
   2379  1.1  christos 	target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
   2380  1.1  christos 	target_sect->flags |= SEC_RELOC;
   2381  1.1  christos 	target_sect->relocation = NULL;
   2382  1.1  christos 	target_sect->rel_filepos = hdr->sh_offset;
   2383  1.1  christos 	/* In the section to which the relocations apply, mark whether
   2384  1.1  christos 	   its relocations are of the REL or RELA variety.  */
   2385  1.1  christos 	if (hdr->sh_size != 0)
   2386  1.1  christos 	  {
   2387  1.1  christos 	    if (hdr->sh_type == SHT_RELA)
   2388  1.1  christos 	      target_sect->use_rela_p = 1;
   2389  1.5  christos 	  }
   2390  1.1  christos 	abfd->flags |= HAS_RELOC;
   2391  1.1  christos 	goto success;
   2392  1.1  christos       }
   2393  1.1  christos 
   2394  1.1  christos     case SHT_GNU_verdef:
   2395  1.5  christos       elf_dynverdef (abfd) = shindex;
   2396  1.5  christos       elf_tdata (abfd)->dynverdef_hdr = *hdr;
   2397  1.1  christos       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2398  1.1  christos       goto success;
   2399  1.1  christos 
   2400  1.5  christos     case SHT_GNU_versym:
   2401  1.5  christos       if (hdr->sh_entsize != sizeof (Elf_External_Versym))
   2402  1.1  christos 	goto fail;
   2403  1.1  christos 
   2404  1.5  christos       elf_dynversym (abfd) = shindex;
   2405  1.5  christos       elf_tdata (abfd)->dynversym_hdr = *hdr;
   2406  1.1  christos       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2407  1.1  christos       goto success;
   2408  1.1  christos 
   2409  1.1  christos     case SHT_GNU_verneed:
   2410  1.5  christos       elf_dynverref (abfd) = shindex;
   2411  1.5  christos       elf_tdata (abfd)->dynverref_hdr = *hdr;
   2412  1.1  christos       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2413  1.1  christos       goto success;
   2414  1.5  christos 
   2415  1.1  christos     case SHT_SHLIB:
   2416  1.1  christos       goto success;
   2417  1.3  christos 
   2418  1.5  christos     case SHT_GROUP:
   2419  1.5  christos       if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
   2420  1.1  christos 	goto fail;
   2421  1.5  christos 
   2422  1.5  christos       if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2423  1.1  christos 	goto fail;
   2424  1.1  christos 
   2425  1.1  christos       if (hdr->contents != NULL)
   2426  1.5  christos 	{
   2427  1.1  christos 	  Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
   2428  1.1  christos 	  unsigned int n_elt = hdr->sh_size / sizeof (* idx);
   2429  1.5  christos 	  asection *s;
   2430  1.5  christos 
   2431  1.1  christos 	  if (n_elt == 0)
   2432  1.1  christos 	    goto fail;
   2433  1.1  christos 	  if (idx->flags & GRP_COMDAT)
   2434  1.1  christos 	    hdr->bfd_section->flags
   2435  1.1  christos 	      |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
   2436  1.1  christos 
   2437  1.5  christos 	  /* We try to keep the same section order as it comes in.  */
   2438  1.1  christos 	  idx += n_elt;
   2439  1.1  christos 
   2440  1.1  christos 	  while (--n_elt != 0)
   2441  1.1  christos 	    {
   2442  1.1  christos 	      --idx;
   2443  1.1  christos 
   2444  1.1  christos 	      if (idx->shdr != NULL
   2445  1.1  christos 		  && (s = idx->shdr->bfd_section) != NULL
   2446  1.1  christos 		  && elf_next_in_group (s) != NULL)
   2447  1.1  christos 		{
   2448  1.1  christos 		  elf_next_in_group (hdr->bfd_section) = s;
   2449  1.1  christos 		  break;
   2450  1.1  christos 		}
   2451  1.5  christos 	    }
   2452  1.1  christos 	}
   2453  1.1  christos       goto success;
   2454  1.1  christos 
   2455  1.1  christos     default:
   2456  1.1  christos       /* Possibly an attributes section.  */
   2457  1.1  christos       if (hdr->sh_type == SHT_GNU_ATTRIBUTES
   2458  1.1  christos 	  || hdr->sh_type == bed->obj_attrs_section_type)
   2459  1.5  christos 	{
   2460  1.1  christos 	  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2461  1.5  christos 	    goto fail;
   2462  1.1  christos 	  _bfd_elf_parse_attributes (abfd, hdr);
   2463  1.1  christos 	  goto success;
   2464  1.1  christos 	}
   2465  1.1  christos 
   2466  1.5  christos       /* Check for any processor-specific section types.  */
   2467  1.1  christos       if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
   2468  1.1  christos 	goto success;
   2469  1.1  christos 
   2470  1.1  christos       if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
   2471  1.1  christos 	{
   2472  1.1  christos 	  if ((hdr->sh_flags & SHF_ALLOC) != 0)
   2473  1.9  christos 	    /* FIXME: How to properly handle allocated section reserved
   2474  1.9  christos 	       for applications?  */
   2475  1.1  christos 	    _bfd_error_handler
   2476  1.1  christos 	      /* xgettext:c-format */
   2477  1.1  christos 	      (_("%B: don't know how to handle allocated, application "
   2478  1.1  christos 		 "specific section `%s' [0x%8x]"),
   2479  1.5  christos 	       abfd, name, hdr->sh_type);
   2480  1.5  christos 	  else
   2481  1.5  christos 	    {
   2482  1.5  christos 	      /* Allow sections reserved for applications.  */
   2483  1.5  christos 	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2484  1.5  christos 						     shindex);
   2485  1.1  christos 	      goto success;
   2486  1.1  christos 	    }
   2487  1.1  christos 	}
   2488  1.1  christos       else if (hdr->sh_type >= SHT_LOPROC
   2489  1.9  christos 	       && hdr->sh_type <= SHT_HIPROC)
   2490  1.9  christos 	/* FIXME: We should handle this section.  */
   2491  1.1  christos 	_bfd_error_handler
   2492  1.1  christos 	  /* xgettext:c-format */
   2493  1.1  christos 	  (_("%B: don't know how to handle processor specific section "
   2494  1.1  christos 	     "`%s' [0x%8x]"),
   2495  1.1  christos 	   abfd, name, hdr->sh_type);
   2496  1.1  christos       else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
   2497  1.1  christos 	{
   2498  1.1  christos 	  /* Unrecognised OS-specific sections.  */
   2499  1.1  christos 	  if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
   2500  1.1  christos 	    /* SHF_OS_NONCONFORMING indicates that special knowledge is
   2501  1.9  christos 	       required to correctly process the section and the file should
   2502  1.9  christos 	       be rejected with an error message.  */
   2503  1.1  christos 	    _bfd_error_handler
   2504  1.1  christos 	      /* xgettext:c-format */
   2505  1.1  christos 	      (_("%B: don't know how to handle OS specific section "
   2506  1.1  christos 		 "`%s' [0x%8x]"),
   2507  1.5  christos 	       abfd, name, hdr->sh_type);
   2508  1.5  christos 	  else
   2509  1.5  christos 	    {
   2510  1.5  christos 	      /* Otherwise it should be processed.  */
   2511  1.5  christos 	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2512  1.1  christos 	      goto success;
   2513  1.1  christos 	    }
   2514  1.1  christos 	}
   2515  1.9  christos       else
   2516  1.9  christos 	/* FIXME: We should handle this section.  */
   2517  1.1  christos 	_bfd_error_handler
   2518  1.1  christos 	  /* xgettext:c-format */
   2519  1.1  christos 	  (_("%B: don't know how to handle section `%s' [0x%8x]"),
   2520  1.5  christos 	   abfd, name, hdr->sh_type);
   2521  1.1  christos 
   2522  1.1  christos       goto fail;
   2523  1.5  christos     }
   2524  1.5  christos 
   2525  1.5  christos  fail:
   2526  1.5  christos   ret = FALSE;
   2527  1.5  christos  success:
   2528  1.5  christos   if (sections_being_created && sections_being_created_abfd == abfd)
   2529  1.5  christos     sections_being_created [shindex] = FALSE;
   2530  1.5  christos   if (-- nesting == 0)
   2531  1.5  christos     {
   2532  1.5  christos       sections_being_created = NULL;
   2533  1.5  christos       sections_being_created_abfd = abfd;
   2534  1.1  christos     }
   2535  1.1  christos   return ret;
   2536  1.1  christos }
   2537  1.1  christos 
   2538  1.1  christos /* Return the local symbol specified by ABFD, R_SYMNDX.  */
   2539  1.1  christos 
   2540  1.1  christos Elf_Internal_Sym *
   2541  1.1  christos bfd_sym_from_r_symndx (struct sym_cache *cache,
   2542  1.1  christos 		       bfd *abfd,
   2543  1.1  christos 		       unsigned long r_symndx)
   2544  1.1  christos {
   2545  1.1  christos   unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
   2546  1.1  christos 
   2547  1.1  christos   if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
   2548  1.1  christos     {
   2549  1.1  christos       Elf_Internal_Shdr *symtab_hdr;
   2550  1.1  christos       unsigned char esym[sizeof (Elf64_External_Sym)];
   2551  1.1  christos       Elf_External_Sym_Shndx eshndx;
   2552  1.1  christos 
   2553  1.1  christos       symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   2554  1.1  christos       if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
   2555  1.1  christos 				&cache->sym[ent], esym, &eshndx) == NULL)
   2556  1.1  christos 	return NULL;
   2557  1.1  christos 
   2558  1.1  christos       if (cache->abfd != abfd)
   2559  1.1  christos 	{
   2560  1.1  christos 	  memset (cache->indx, -1, sizeof (cache->indx));
   2561  1.1  christos 	  cache->abfd = abfd;
   2562  1.1  christos 	}
   2563  1.1  christos       cache->indx[ent] = r_symndx;
   2564  1.1  christos     }
   2565  1.1  christos 
   2566  1.1  christos   return &cache->sym[ent];
   2567  1.1  christos }
   2568  1.1  christos 
   2569  1.1  christos /* Given an ELF section number, retrieve the corresponding BFD
   2570  1.1  christos    section.  */
   2571  1.1  christos 
   2572  1.1  christos asection *
   2573  1.1  christos bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
   2574  1.1  christos {
   2575  1.1  christos   if (sec_index >= elf_numsections (abfd))
   2576  1.1  christos     return NULL;
   2577  1.1  christos   return elf_elfsections (abfd)[sec_index]->bfd_section;
   2578  1.1  christos }
   2579  1.1  christos 
   2580  1.1  christos static const struct bfd_elf_special_section special_sections_b[] =
   2581  1.1  christos {
   2582  1.1  christos   { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   2583  1.1  christos   { NULL,                   0,  0, 0,            0 }
   2584  1.1  christos };
   2585  1.1  christos 
   2586  1.1  christos static const struct bfd_elf_special_section special_sections_c[] =
   2587  1.1  christos {
   2588  1.1  christos   { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
   2589  1.1  christos   { NULL,                       0, 0, 0,            0 }
   2590  1.1  christos };
   2591  1.1  christos 
   2592  1.1  christos static const struct bfd_elf_special_section special_sections_d[] =
   2593  1.1  christos {
   2594  1.3  christos   { STRING_COMMA_LEN (".data"),         -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2595  1.3  christos   { STRING_COMMA_LEN (".data1"),         0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2596  1.3  christos   /* There are more DWARF sections than these, but they needn't be added here
   2597  1.1  christos      unless you have to cope with broken compilers that don't emit section
   2598  1.1  christos      attributes or you want to help the user writing assembler.  */
   2599  1.1  christos   { STRING_COMMA_LEN (".debug"),         0, SHT_PROGBITS, 0 },
   2600  1.1  christos   { STRING_COMMA_LEN (".debug_line"),    0, SHT_PROGBITS, 0 },
   2601  1.1  christos   { STRING_COMMA_LEN (".debug_info"),    0, SHT_PROGBITS, 0 },
   2602  1.1  christos   { STRING_COMMA_LEN (".debug_abbrev"),  0, SHT_PROGBITS, 0 },
   2603  1.1  christos   { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
   2604  1.1  christos   { STRING_COMMA_LEN (".dynamic"),       0, SHT_DYNAMIC,  SHF_ALLOC },
   2605  1.1  christos   { STRING_COMMA_LEN (".dynstr"),        0, SHT_STRTAB,   SHF_ALLOC },
   2606  1.1  christos   { STRING_COMMA_LEN (".dynsym"),        0, SHT_DYNSYM,   SHF_ALLOC },
   2607  1.1  christos   { NULL,                      0,        0, 0,            0 }
   2608  1.1  christos };
   2609  1.1  christos 
   2610  1.9  christos static const struct bfd_elf_special_section special_sections_f[] =
   2611  1.9  christos {
   2612  1.9  christos   { STRING_COMMA_LEN (".fini"),        0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
   2613  1.1  christos   { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
   2614  1.1  christos   { NULL,                          0 , 0, 0,              0 }
   2615  1.1  christos };
   2616  1.1  christos 
   2617  1.1  christos static const struct bfd_elf_special_section special_sections_g[] =
   2618  1.1  christos {
   2619  1.1  christos   { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
   2620  1.1  christos   { STRING_COMMA_LEN (".gnu.lto_"),       -1, SHT_PROGBITS,    SHF_EXCLUDE },
   2621  1.1  christos   { STRING_COMMA_LEN (".got"),             0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
   2622  1.1  christos   { STRING_COMMA_LEN (".gnu.version"),     0, SHT_GNU_versym,  0 },
   2623  1.1  christos   { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
   2624  1.1  christos   { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
   2625  1.1  christos   { STRING_COMMA_LEN (".gnu.liblist"),     0, SHT_GNU_LIBLIST, SHF_ALLOC },
   2626  1.1  christos   { STRING_COMMA_LEN (".gnu.conflict"),    0, SHT_RELA,        SHF_ALLOC },
   2627  1.1  christos   { STRING_COMMA_LEN (".gnu.hash"),        0, SHT_GNU_HASH,    SHF_ALLOC },
   2628  1.1  christos   { NULL,                        0,        0, 0,               0 }
   2629  1.1  christos };
   2630  1.1  christos 
   2631  1.1  christos static const struct bfd_elf_special_section special_sections_h[] =
   2632  1.1  christos {
   2633  1.1  christos   { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,     SHF_ALLOC },
   2634  1.1  christos   { NULL,                    0, 0, 0,            0 }
   2635  1.1  christos };
   2636  1.1  christos 
   2637  1.9  christos static const struct bfd_elf_special_section special_sections_i[] =
   2638  1.9  christos {
   2639  1.9  christos   { STRING_COMMA_LEN (".init"),        0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
   2640  1.9  christos   { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
   2641  1.1  christos   { STRING_COMMA_LEN (".interp"),      0, SHT_PROGBITS,   0 },
   2642  1.1  christos   { NULL,                      0,      0, 0,              0 }
   2643  1.1  christos };
   2644  1.1  christos 
   2645  1.1  christos static const struct bfd_elf_special_section special_sections_l[] =
   2646  1.1  christos {
   2647  1.1  christos   { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
   2648  1.1  christos   { NULL,                    0, 0, 0,            0 }
   2649  1.1  christos };
   2650  1.1  christos 
   2651  1.1  christos static const struct bfd_elf_special_section special_sections_n[] =
   2652  1.1  christos {
   2653  1.1  christos   { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
   2654  1.1  christos   { STRING_COMMA_LEN (".note"),          -1, SHT_NOTE,     0 },
   2655  1.1  christos   { NULL,                    0,           0, 0,            0 }
   2656  1.1  christos };
   2657  1.1  christos 
   2658  1.9  christos static const struct bfd_elf_special_section special_sections_p[] =
   2659  1.9  christos {
   2660  1.9  christos   { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
   2661  1.1  christos   { STRING_COMMA_LEN (".plt"),            0, SHT_PROGBITS,      SHF_ALLOC + SHF_EXECINSTR },
   2662  1.1  christos   { NULL,                   0,            0, 0,                 0 }
   2663  1.1  christos };
   2664  1.1  christos 
   2665  1.1  christos static const struct bfd_elf_special_section special_sections_r[] =
   2666  1.1  christos {
   2667  1.1  christos   { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
   2668  1.1  christos   { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
   2669  1.1  christos   { STRING_COMMA_LEN (".rela"),   -1, SHT_RELA,     0 },
   2670  1.1  christos   { STRING_COMMA_LEN (".rel"),    -1, SHT_REL,      0 },
   2671  1.1  christos   { NULL,                   0,     0, 0,            0 }
   2672  1.1  christos };
   2673  1.1  christos 
   2674  1.1  christos static const struct bfd_elf_special_section special_sections_s[] =
   2675  1.1  christos {
   2676  1.1  christos   { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
   2677  1.1  christos   { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
   2678  1.1  christos   { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
   2679  1.1  christos   /* See struct bfd_elf_special_section declaration for the semantics of
   2680  1.1  christos      this special case where .prefix_length != strlen (.prefix).  */
   2681  1.1  christos   { ".stabstr",			5,  3, SHT_STRTAB, 0 },
   2682  1.1  christos   { NULL,                       0,  0, 0,          0 }
   2683  1.1  christos };
   2684  1.1  christos 
   2685  1.1  christos static const struct bfd_elf_special_section special_sections_t[] =
   2686  1.1  christos {
   2687  1.1  christos   { STRING_COMMA_LEN (".text"),  -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
   2688  1.1  christos   { STRING_COMMA_LEN (".tbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
   2689  1.1  christos   { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
   2690  1.1  christos   { NULL,                     0,  0, 0,            0 }
   2691  1.1  christos };
   2692  1.1  christos 
   2693  1.1  christos static const struct bfd_elf_special_section special_sections_z[] =
   2694  1.1  christos {
   2695  1.1  christos   { STRING_COMMA_LEN (".zdebug_line"),    0, SHT_PROGBITS, 0 },
   2696  1.1  christos   { STRING_COMMA_LEN (".zdebug_info"),    0, SHT_PROGBITS, 0 },
   2697  1.1  christos   { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
   2698  1.1  christos   { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
   2699  1.1  christos   { NULL,                     0,  0, 0,            0 }
   2700  1.3  christos };
   2701  1.1  christos 
   2702  1.1  christos static const struct bfd_elf_special_section * const special_sections[] =
   2703  1.1  christos {
   2704  1.1  christos   special_sections_b,		/* 'b' */
   2705  1.1  christos   special_sections_c,		/* 'c' */
   2706  1.1  christos   special_sections_d,		/* 'd' */
   2707  1.1  christos   NULL,				/* 'e' */
   2708  1.1  christos   special_sections_f,		/* 'f' */
   2709  1.1  christos   special_sections_g,		/* 'g' */
   2710  1.1  christos   special_sections_h,		/* 'h' */
   2711  1.1  christos   special_sections_i,		/* 'i' */
   2712  1.1  christos   NULL,				/* 'j' */
   2713  1.1  christos   NULL,				/* 'k' */
   2714  1.1  christos   special_sections_l,		/* 'l' */
   2715  1.1  christos   NULL,				/* 'm' */
   2716  1.1  christos   special_sections_n,		/* 'n' */
   2717  1.1  christos   NULL,				/* 'o' */
   2718  1.1  christos   special_sections_p,		/* 'p' */
   2719  1.1  christos   NULL,				/* 'q' */
   2720  1.1  christos   special_sections_r,		/* 'r' */
   2721  1.1  christos   special_sections_s,		/* 's' */
   2722  1.1  christos   special_sections_t,		/* 't' */
   2723  1.1  christos   NULL,				/* 'u' */
   2724  1.1  christos   NULL,				/* 'v' */
   2725  1.1  christos   NULL,				/* 'w' */
   2726  1.1  christos   NULL,				/* 'x' */
   2727  1.1  christos   NULL,				/* 'y' */
   2728  1.1  christos   special_sections_z		/* 'z' */
   2729  1.1  christos };
   2730  1.1  christos 
   2731  1.1  christos const struct bfd_elf_special_section *
   2732  1.1  christos _bfd_elf_get_special_section (const char *name,
   2733  1.1  christos 			      const struct bfd_elf_special_section *spec,
   2734  1.1  christos 			      unsigned int rela)
   2735  1.1  christos {
   2736  1.1  christos   int i;
   2737  1.1  christos   int len;
   2738  1.1  christos 
   2739  1.1  christos   len = strlen (name);
   2740  1.1  christos 
   2741  1.1  christos   for (i = 0; spec[i].prefix != NULL; i++)
   2742  1.1  christos     {
   2743  1.1  christos       int suffix_len;
   2744  1.1  christos       int prefix_len = spec[i].prefix_length;
   2745  1.1  christos 
   2746  1.1  christos       if (len < prefix_len)
   2747  1.1  christos 	continue;
   2748  1.1  christos       if (memcmp (name, spec[i].prefix, prefix_len) != 0)
   2749  1.1  christos 	continue;
   2750  1.1  christos 
   2751  1.1  christos       suffix_len = spec[i].suffix_length;
   2752  1.1  christos       if (suffix_len <= 0)
   2753  1.1  christos 	{
   2754  1.1  christos 	  if (name[prefix_len] != 0)
   2755  1.1  christos 	    {
   2756  1.1  christos 	      if (suffix_len == 0)
   2757  1.1  christos 		continue;
   2758  1.1  christos 	      if (name[prefix_len] != '.'
   2759  1.1  christos 		  && (suffix_len == -2
   2760  1.1  christos 		      || (rela && spec[i].type == SHT_REL)))
   2761  1.1  christos 		continue;
   2762  1.1  christos 	    }
   2763  1.1  christos 	}
   2764  1.1  christos       else
   2765  1.1  christos 	{
   2766  1.1  christos 	  if (len < prefix_len + suffix_len)
   2767  1.1  christos 	    continue;
   2768  1.1  christos 	  if (memcmp (name + len - suffix_len,
   2769  1.1  christos 		      spec[i].prefix + prefix_len,
   2770  1.1  christos 		      suffix_len) != 0)
   2771  1.1  christos 	    continue;
   2772  1.1  christos 	}
   2773  1.1  christos       return &spec[i];
   2774  1.1  christos     }
   2775  1.1  christos 
   2776  1.1  christos   return NULL;
   2777  1.1  christos }
   2778  1.1  christos 
   2779  1.1  christos const struct bfd_elf_special_section *
   2780  1.1  christos _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
   2781  1.1  christos {
   2782  1.1  christos   int i;
   2783  1.1  christos   const struct bfd_elf_special_section *spec;
   2784  1.1  christos   const struct elf_backend_data *bed;
   2785  1.1  christos 
   2786  1.1  christos   /* See if this is one of the special sections.  */
   2787  1.1  christos   if (sec->name == NULL)
   2788  1.1  christos     return NULL;
   2789  1.1  christos 
   2790  1.1  christos   bed = get_elf_backend_data (abfd);
   2791  1.1  christos   spec = bed->special_sections;
   2792  1.1  christos   if (spec)
   2793  1.1  christos     {
   2794  1.1  christos       spec = _bfd_elf_get_special_section (sec->name,
   2795  1.1  christos 					   bed->special_sections,
   2796  1.1  christos 					   sec->use_rela_p);
   2797  1.1  christos       if (spec != NULL)
   2798  1.1  christos 	return spec;
   2799  1.1  christos     }
   2800  1.1  christos 
   2801  1.1  christos   if (sec->name[0] != '.')
   2802  1.1  christos     return NULL;
   2803  1.1  christos 
   2804  1.1  christos   i = sec->name[1] - 'b';
   2805  1.1  christos   if (i < 0 || i > 'z' - 'b')
   2806  1.1  christos     return NULL;
   2807  1.1  christos 
   2808  1.1  christos   spec = special_sections[i];
   2809  1.1  christos 
   2810  1.1  christos   if (spec == NULL)
   2811  1.1  christos     return NULL;
   2812  1.1  christos 
   2813  1.1  christos   return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
   2814  1.1  christos }
   2815  1.1  christos 
   2816  1.1  christos bfd_boolean
   2817  1.1  christos _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
   2818  1.1  christos {
   2819  1.1  christos   struct bfd_elf_section_data *sdata;
   2820  1.1  christos   const struct elf_backend_data *bed;
   2821  1.1  christos   const struct bfd_elf_special_section *ssect;
   2822  1.1  christos 
   2823  1.1  christos   sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
   2824  1.1  christos   if (sdata == NULL)
   2825  1.1  christos     {
   2826  1.1  christos       sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
   2827  1.1  christos                                                           sizeof (*sdata));
   2828  1.1  christos       if (sdata == NULL)
   2829  1.1  christos 	return FALSE;
   2830  1.1  christos       sec->used_by_bfd = sdata;
   2831  1.1  christos     }
   2832  1.1  christos 
   2833  1.1  christos   /* Indicate whether or not this section should use RELA relocations.  */
   2834  1.1  christos   bed = get_elf_backend_data (abfd);
   2835  1.1  christos   sec->use_rela_p = bed->default_use_rela_p;
   2836  1.1  christos 
   2837  1.1  christos   /* When we read a file, we don't need to set ELF section type and
   2838  1.1  christos      flags.  They will be overridden in _bfd_elf_make_section_from_shdr
   2839  1.1  christos      anyway.  We will set ELF section type and flags for all linker
   2840  1.1  christos      created sections.  If user specifies BFD section flags, we will
   2841  1.1  christos      set ELF section type and flags based on BFD section flags in
   2842  1.1  christos      elf_fake_sections.  Special handling for .init_array/.fini_array
   2843  1.1  christos      output sections since they may contain .ctors/.dtors input
   2844  1.1  christos      sections.  We don't want _bfd_elf_init_private_section_data to
   2845  1.1  christos      copy ELF section type from .ctors/.dtors input sections.  */
   2846  1.1  christos   if (abfd->direction != read_direction
   2847  1.1  christos       || (sec->flags & SEC_LINKER_CREATED) != 0)
   2848  1.1  christos     {
   2849  1.1  christos       ssect = (*bed->get_sec_type_attr) (abfd, sec);
   2850  1.1  christos       if (ssect != NULL
   2851  1.1  christos 	  && (!sec->flags
   2852  1.1  christos 	      || (sec->flags & SEC_LINKER_CREATED) != 0
   2853  1.1  christos 	      || ssect->type == SHT_INIT_ARRAY
   2854  1.1  christos 	      || ssect->type == SHT_FINI_ARRAY))
   2855  1.1  christos 	{
   2856  1.1  christos 	  elf_section_type (sec) = ssect->type;
   2857  1.1  christos 	  elf_section_flags (sec) = ssect->attr;
   2858  1.1  christos 	}
   2859  1.1  christos     }
   2860  1.1  christos 
   2861  1.1  christos   return _bfd_generic_new_section_hook (abfd, sec);
   2862  1.1  christos }
   2863  1.1  christos 
   2864  1.1  christos /* Create a new bfd section from an ELF program header.
   2865  1.1  christos 
   2866  1.1  christos    Since program segments have no names, we generate a synthetic name
   2867  1.1  christos    of the form segment<NUM>, where NUM is generally the index in the
   2868  1.1  christos    program header table.  For segments that are split (see below) we
   2869  1.1  christos    generate the names segment<NUM>a and segment<NUM>b.
   2870  1.1  christos 
   2871  1.1  christos    Note that some program segments may have a file size that is different than
   2872  1.1  christos    (less than) the memory size.  All this means is that at execution the
   2873  1.1  christos    system must allocate the amount of memory specified by the memory size,
   2874  1.1  christos    but only initialize it with the first "file size" bytes read from the
   2875  1.1  christos    file.  This would occur for example, with program segments consisting
   2876  1.1  christos    of combined data+bss.
   2877  1.1  christos 
   2878  1.1  christos    To handle the above situation, this routine generates TWO bfd sections
   2879  1.1  christos    for the single program segment.  The first has the length specified by
   2880  1.1  christos    the file size of the segment, and the second has the length specified
   2881  1.1  christos    by the difference between the two sizes.  In effect, the segment is split
   2882  1.1  christos    into its initialized and uninitialized parts.
   2883  1.1  christos 
   2884  1.1  christos  */
   2885  1.1  christos 
   2886  1.1  christos bfd_boolean
   2887  1.1  christos _bfd_elf_make_section_from_phdr (bfd *abfd,
   2888  1.1  christos 				 Elf_Internal_Phdr *hdr,
   2889  1.1  christos 				 int hdr_index,
   2890  1.1  christos 				 const char *type_name)
   2891  1.1  christos {
   2892  1.1  christos   asection *newsect;
   2893  1.1  christos   char *name;
   2894  1.1  christos   char namebuf[64];
   2895  1.1  christos   size_t len;
   2896  1.1  christos   int split;
   2897  1.1  christos 
   2898  1.1  christos   split = ((hdr->p_memsz > 0)
   2899  1.1  christos 	    && (hdr->p_filesz > 0)
   2900  1.1  christos 	    && (hdr->p_memsz > hdr->p_filesz));
   2901  1.1  christos 
   2902  1.1  christos   if (hdr->p_filesz > 0)
   2903  1.1  christos     {
   2904  1.1  christos       sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
   2905  1.1  christos       len = strlen (namebuf) + 1;
   2906  1.1  christos       name = (char *) bfd_alloc (abfd, len);
   2907  1.1  christos       if (!name)
   2908  1.1  christos 	return FALSE;
   2909  1.1  christos       memcpy (name, namebuf, len);
   2910  1.1  christos       newsect = bfd_make_section (abfd, name);
   2911  1.1  christos       if (newsect == NULL)
   2912  1.1  christos 	return FALSE;
   2913  1.1  christos       newsect->vma = hdr->p_vaddr;
   2914  1.1  christos       newsect->lma = hdr->p_paddr;
   2915  1.1  christos       newsect->size = hdr->p_filesz;
   2916  1.1  christos       newsect->filepos = hdr->p_offset;
   2917  1.1  christos       newsect->flags |= SEC_HAS_CONTENTS;
   2918  1.1  christos       newsect->alignment_power = bfd_log2 (hdr->p_align);
   2919  1.1  christos       if (hdr->p_type == PT_LOAD)
   2920  1.1  christos 	{
   2921  1.1  christos 	  newsect->flags |= SEC_ALLOC;
   2922  1.1  christos 	  newsect->flags |= SEC_LOAD;
   2923  1.1  christos 	  if (hdr->p_flags & PF_X)
   2924  1.1  christos 	    {
   2925  1.1  christos 	      /* FIXME: all we known is that it has execute PERMISSION,
   2926  1.1  christos 		 may be data.  */
   2927  1.1  christos 	      newsect->flags |= SEC_CODE;
   2928  1.1  christos 	    }
   2929  1.1  christos 	}
   2930  1.1  christos       if (!(hdr->p_flags & PF_W))
   2931  1.1  christos 	{
   2932  1.1  christos 	  newsect->flags |= SEC_READONLY;
   2933  1.1  christos 	}
   2934  1.1  christos     }
   2935  1.1  christos 
   2936  1.1  christos   if (hdr->p_memsz > hdr->p_filesz)
   2937  1.1  christos     {
   2938  1.1  christos       bfd_vma align;
   2939  1.1  christos 
   2940  1.1  christos       sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
   2941  1.1  christos       len = strlen (namebuf) + 1;
   2942  1.1  christos       name = (char *) bfd_alloc (abfd, len);
   2943  1.1  christos       if (!name)
   2944  1.1  christos 	return FALSE;
   2945  1.1  christos       memcpy (name, namebuf, len);
   2946  1.1  christos       newsect = bfd_make_section (abfd, name);
   2947  1.1  christos       if (newsect == NULL)
   2948  1.1  christos 	return FALSE;
   2949  1.1  christos       newsect->vma = hdr->p_vaddr + hdr->p_filesz;
   2950  1.1  christos       newsect->lma = hdr->p_paddr + hdr->p_filesz;
   2951  1.1  christos       newsect->size = hdr->p_memsz - hdr->p_filesz;
   2952  1.1  christos       newsect->filepos = hdr->p_offset + hdr->p_filesz;
   2953  1.1  christos       align = newsect->vma & -newsect->vma;
   2954  1.1  christos       if (align == 0 || align > hdr->p_align)
   2955  1.1  christos 	align = hdr->p_align;
   2956  1.1  christos       newsect->alignment_power = bfd_log2 (align);
   2957  1.1  christos       if (hdr->p_type == PT_LOAD)
   2958  1.1  christos 	{
   2959  1.1  christos 	  /* Hack for gdb.  Segments that have not been modified do
   2960  1.1  christos 	     not have their contents written to a core file, on the
   2961  1.1  christos 	     assumption that a debugger can find the contents in the
   2962  1.1  christos 	     executable.  We flag this case by setting the fake
   2963  1.1  christos 	     section size to zero.  Note that "real" bss sections will
   2964  1.1  christos 	     always have their contents dumped to the core file.  */
   2965  1.1  christos 	  if (bfd_get_format (abfd) == bfd_core)
   2966  1.1  christos 	    newsect->size = 0;
   2967  1.1  christos 	  newsect->flags |= SEC_ALLOC;
   2968  1.1  christos 	  if (hdr->p_flags & PF_X)
   2969  1.1  christos 	    newsect->flags |= SEC_CODE;
   2970  1.1  christos 	}
   2971  1.1  christos       if (!(hdr->p_flags & PF_W))
   2972  1.1  christos 	newsect->flags |= SEC_READONLY;
   2973  1.1  christos     }
   2974  1.1  christos 
   2975  1.1  christos   return TRUE;
   2976  1.1  christos }
   2977  1.1  christos 
   2978  1.1  christos bfd_boolean
   2979  1.1  christos bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
   2980  1.1  christos {
   2981  1.1  christos   const struct elf_backend_data *bed;
   2982  1.1  christos 
   2983  1.1  christos   switch (hdr->p_type)
   2984  1.1  christos     {
   2985  1.1  christos     case PT_NULL:
   2986  1.1  christos       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
   2987  1.1  christos 
   2988  1.1  christos     case PT_LOAD:
   2989  1.1  christos       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
   2990  1.1  christos 
   2991  1.1  christos     case PT_DYNAMIC:
   2992  1.1  christos       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
   2993  1.1  christos 
   2994  1.1  christos     case PT_INTERP:
   2995  1.1  christos       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
   2996  1.1  christos 
   2997  1.1  christos     case PT_NOTE:
   2998  1.1  christos       if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
   2999  1.1  christos 	return FALSE;
   3000  1.1  christos       if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
   3001  1.1  christos 	return FALSE;
   3002  1.1  christos       return TRUE;
   3003  1.1  christos 
   3004  1.1  christos     case PT_SHLIB:
   3005  1.1  christos       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
   3006  1.1  christos 
   3007  1.1  christos     case PT_PHDR:
   3008  1.1  christos       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
   3009  1.1  christos 
   3010  1.1  christos     case PT_GNU_EH_FRAME:
   3011  1.1  christos       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
   3012  1.1  christos 					      "eh_frame_hdr");
   3013  1.1  christos 
   3014  1.1  christos     case PT_GNU_STACK:
   3015  1.1  christos       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
   3016  1.1  christos 
   3017  1.1  christos     case PT_GNU_RELRO:
   3018  1.1  christos       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
   3019  1.1  christos 
   3020  1.1  christos     default:
   3021  1.1  christos       /* Check for any processor-specific program segment types.  */
   3022  1.1  christos       bed = get_elf_backend_data (abfd);
   3023  1.1  christos       return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
   3024  1.1  christos     }
   3025  1.1  christos }
   3026  1.1  christos 
   3027  1.1  christos /* Return the REL_HDR for SEC, assuming there is only a single one, either
   3028  1.1  christos    REL or RELA.  */
   3029  1.1  christos 
   3030  1.1  christos Elf_Internal_Shdr *
   3031  1.1  christos _bfd_elf_single_rel_hdr (asection *sec)
   3032  1.1  christos {
   3033  1.1  christos   if (elf_section_data (sec)->rel.hdr)
   3034  1.1  christos     {
   3035  1.1  christos       BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
   3036  1.1  christos       return elf_section_data (sec)->rel.hdr;
   3037  1.1  christos     }
   3038  1.1  christos   else
   3039  1.1  christos     return elf_section_data (sec)->rela.hdr;
   3040  1.6  christos }
   3041  1.6  christos 
   3042  1.6  christos static bfd_boolean
   3043  1.6  christos _bfd_elf_set_reloc_sh_name (bfd *abfd,
   3044  1.6  christos 			    Elf_Internal_Shdr *rel_hdr,
   3045  1.6  christos 			    const char *sec_name,
   3046  1.6  christos 			    bfd_boolean use_rela_p)
   3047  1.6  christos {
   3048  1.6  christos   char *name = (char *) bfd_alloc (abfd,
   3049  1.6  christos 				   sizeof ".rela" + strlen (sec_name));
   3050  1.6  christos   if (name == NULL)
   3051  1.6  christos     return FALSE;
   3052  1.6  christos 
   3053  1.6  christos   sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
   3054  1.6  christos   rel_hdr->sh_name =
   3055  1.6  christos     (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
   3056  1.6  christos 					FALSE);
   3057  1.6  christos   if (rel_hdr->sh_name == (unsigned int) -1)
   3058  1.6  christos     return FALSE;
   3059  1.6  christos 
   3060  1.6  christos   return TRUE;
   3061  1.1  christos }
   3062  1.1  christos 
   3063  1.1  christos /* Allocate and initialize a section-header for a new reloc section,
   3064  1.1  christos    containing relocations against ASECT.  It is stored in RELDATA.  If
   3065  1.1  christos    USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
   3066  1.4  christos    relocations.  */
   3067  1.1  christos 
   3068  1.1  christos static bfd_boolean
   3069  1.6  christos _bfd_elf_init_reloc_shdr (bfd *abfd,
   3070  1.6  christos 			  struct bfd_elf_section_reloc_data *reldata,
   3071  1.6  christos 			  const char *sec_name,
   3072  1.1  christos 			  bfd_boolean use_rela_p,
   3073  1.1  christos 			  bfd_boolean delay_st_name_p)
   3074  1.1  christos {
   3075  1.1  christos   Elf_Internal_Shdr *rel_hdr;
   3076  1.1  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3077  1.8  christos 
   3078  1.1  christos   BFD_ASSERT (reldata->hdr == NULL);
   3079  1.1  christos   rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
   3080  1.6  christos   reldata->hdr = rel_hdr;
   3081  1.6  christos 
   3082  1.6  christos   if (delay_st_name_p)
   3083  1.6  christos     rel_hdr->sh_name = (unsigned int) -1;
   3084  1.1  christos   else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
   3085  1.1  christos 					use_rela_p))
   3086  1.1  christos     return FALSE;
   3087  1.1  christos   rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
   3088  1.1  christos   rel_hdr->sh_entsize = (use_rela_p
   3089  1.1  christos 			 ? bed->s->sizeof_rela
   3090  1.1  christos 			 : bed->s->sizeof_rel);
   3091  1.1  christos   rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   3092  1.1  christos   rel_hdr->sh_flags = 0;
   3093  1.1  christos   rel_hdr->sh_addr = 0;
   3094  1.1  christos   rel_hdr->sh_size = 0;
   3095  1.1  christos   rel_hdr->sh_offset = 0;
   3096  1.1  christos 
   3097  1.1  christos   return TRUE;
   3098  1.1  christos }
   3099  1.1  christos 
   3100  1.1  christos /* Return the default section type based on the passed in section flags.  */
   3101  1.1  christos 
   3102  1.1  christos int
   3103  1.1  christos bfd_elf_get_default_section_type (flagword flags)
   3104  1.1  christos {
   3105  1.1  christos   if ((flags & SEC_ALLOC) != 0
   3106  1.1  christos       && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   3107  1.1  christos     return SHT_NOBITS;
   3108  1.1  christos   return SHT_PROGBITS;
   3109  1.1  christos }
   3110  1.1  christos 
   3111  1.1  christos struct fake_section_arg
   3112  1.1  christos {
   3113  1.1  christos   struct bfd_link_info *link_info;
   3114  1.1  christos   bfd_boolean failed;
   3115  1.1  christos };
   3116  1.1  christos 
   3117  1.1  christos /* Set up an ELF internal section header for a section.  */
   3118  1.1  christos 
   3119  1.1  christos static void
   3120  1.1  christos elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
   3121  1.1  christos {
   3122  1.1  christos   struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
   3123  1.1  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3124  1.1  christos   struct bfd_elf_section_data *esd = elf_section_data (asect);
   3125  1.6  christos   Elf_Internal_Shdr *this_hdr;
   3126  1.6  christos   unsigned int sh_type;
   3127  1.1  christos   const char *name = asect->name;
   3128  1.1  christos   bfd_boolean delay_st_name_p = FALSE;
   3129  1.1  christos 
   3130  1.1  christos   if (arg->failed)
   3131  1.1  christos     {
   3132  1.1  christos       /* We already failed; just get out of the bfd_map_over_sections
   3133  1.1  christos 	 loop.  */
   3134  1.1  christos       return;
   3135  1.1  christos     }
   3136  1.1  christos 
   3137  1.6  christos   this_hdr = &esd->this_hdr;
   3138  1.6  christos 
   3139  1.6  christos   if (arg->link_info)
   3140  1.6  christos     {
   3141  1.6  christos       /* ld: compress DWARF debug sections with names: .debug_*.  */
   3142  1.6  christos       if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
   3143  1.6  christos 	  && (asect->flags & SEC_DEBUGGING)
   3144  1.6  christos 	  && name[1] == 'd'
   3145  1.6  christos 	  && name[6] == '_')
   3146  1.6  christos 	{
   3147  1.6  christos 	  /* Set SEC_ELF_COMPRESS to indicate this section should be
   3148  1.6  christos 	     compressed.  */
   3149  1.9  christos 	  asect->flags |= SEC_ELF_COMPRESS;
   3150  1.6  christos 
   3151  1.6  christos 	  /* If this section will be compressed, delay adding section
   3152  1.6  christos 	     name to section name section after it is compressed in
   3153  1.6  christos 	     _bfd_elf_assign_file_positions_for_non_load.  */
   3154  1.6  christos 	  delay_st_name_p = TRUE;
   3155  1.6  christos 	}
   3156  1.6  christos     }
   3157  1.6  christos   else if ((asect->flags & SEC_ELF_RENAME))
   3158  1.6  christos     {
   3159  1.6  christos       /* objcopy: rename output DWARF debug section.  */
   3160  1.6  christos       if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
   3161  1.6  christos 	{
   3162  1.6  christos 	  /* When we decompress or compress with SHF_COMPRESSED,
   3163  1.6  christos 	     convert section name from .zdebug_* to .debug_* if
   3164  1.6  christos 	     needed.  */
   3165  1.6  christos 	  if (name[1] == 'z')
   3166  1.6  christos 	    {
   3167  1.6  christos 	      char *new_name = convert_zdebug_to_debug (abfd, name);
   3168  1.6  christos 	      if (new_name == NULL)
   3169  1.6  christos 		{
   3170  1.6  christos 		  arg->failed = TRUE;
   3171  1.6  christos 		  return;
   3172  1.6  christos 		}
   3173  1.6  christos 	      name = new_name;
   3174  1.6  christos 	    }
   3175  1.6  christos 	}
   3176  1.6  christos       else if (asect->compress_status == COMPRESS_SECTION_DONE)
   3177  1.6  christos 	{
   3178  1.6  christos 	  /* PR binutils/18087: Compression does not always make a
   3179  1.6  christos 	     section smaller.  So only rename the section when
   3180  1.6  christos 	     compression has actually taken place.  If input section
   3181  1.6  christos 	     name is .zdebug_*, we should never compress it again.  */
   3182  1.6  christos 	  char *new_name = convert_debug_to_zdebug (abfd, name);
   3183  1.6  christos 	  if (new_name == NULL)
   3184  1.6  christos 	    {
   3185  1.6  christos 	      arg->failed = TRUE;
   3186  1.6  christos 	      return;
   3187  1.6  christos 	    }
   3188  1.6  christos 	  BFD_ASSERT (name[1] != 'z');
   3189  1.6  christos 	  name = new_name;
   3190  1.6  christos 	}
   3191  1.6  christos     }
   3192  1.6  christos 
   3193  1.6  christos   if (delay_st_name_p)
   3194  1.1  christos     this_hdr->sh_name = (unsigned int) -1;
   3195  1.6  christos   else
   3196  1.6  christos     {
   3197  1.6  christos       this_hdr->sh_name
   3198  1.6  christos 	= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   3199  1.6  christos 					      name, FALSE);
   3200  1.6  christos       if (this_hdr->sh_name == (unsigned int) -1)
   3201  1.6  christos 	{
   3202  1.6  christos 	  arg->failed = TRUE;
   3203  1.1  christos 	  return;
   3204  1.1  christos 	}
   3205  1.1  christos     }
   3206  1.1  christos 
   3207  1.1  christos   /* Don't clear sh_flags. Assembler may set additional bits.  */
   3208  1.1  christos 
   3209  1.1  christos   if ((asect->flags & SEC_ALLOC) != 0
   3210  1.1  christos       || asect->user_set_vma)
   3211  1.1  christos     this_hdr->sh_addr = asect->vma;
   3212  1.1  christos   else
   3213  1.1  christos     this_hdr->sh_addr = 0;
   3214  1.1  christos 
   3215  1.1  christos   this_hdr->sh_offset = 0;
   3216  1.6  christos   this_hdr->sh_size = asect->size;
   3217  1.6  christos   this_hdr->sh_link = 0;
   3218  1.6  christos   /* PR 17512: file: 0eb809fe, 8b0535ee.  */
   3219  1.9  christos   if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
   3220  1.9  christos     {
   3221  1.6  christos       _bfd_error_handler
   3222  1.9  christos 	/* xgettext:c-format */
   3223  1.6  christos 	(_("%B: error: Alignment power %d of section `%A' is too big"),
   3224  1.6  christos 	 abfd, asect->alignment_power, asect);
   3225  1.6  christos       arg->failed = TRUE;
   3226  1.1  christos       return;
   3227  1.1  christos     }
   3228  1.1  christos   this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
   3229  1.1  christos   /* The sh_entsize and sh_info fields may have been set already by
   3230  1.1  christos      copy_private_section_data.  */
   3231  1.1  christos 
   3232  1.1  christos   this_hdr->bfd_section = asect;
   3233  1.1  christos   this_hdr->contents = NULL;
   3234  1.1  christos 
   3235  1.1  christos   /* If the section type is unspecified, we set it based on
   3236  1.1  christos      asect->flags.  */
   3237  1.1  christos   if ((asect->flags & SEC_GROUP) != 0)
   3238  1.1  christos     sh_type = SHT_GROUP;
   3239  1.1  christos   else
   3240  1.1  christos     sh_type = bfd_elf_get_default_section_type (asect->flags);
   3241  1.1  christos 
   3242  1.1  christos   if (this_hdr->sh_type == SHT_NULL)
   3243  1.1  christos     this_hdr->sh_type = sh_type;
   3244  1.1  christos   else if (this_hdr->sh_type == SHT_NOBITS
   3245  1.1  christos 	   && sh_type == SHT_PROGBITS
   3246  1.1  christos 	   && (asect->flags & SEC_ALLOC) != 0)
   3247  1.1  christos     {
   3248  1.1  christos       /* Warn if we are changing a NOBITS section to PROGBITS, but
   3249  1.1  christos 	 allow the link to proceed.  This can happen when users link
   3250  1.9  christos 	 non-bss input sections to bss output sections, or emit data
   3251  1.1  christos 	 to a bss output section via a linker script.  */
   3252  1.1  christos       _bfd_error_handler
   3253  1.1  christos 	(_("warning: section `%A' type changed to PROGBITS"), asect);
   3254  1.1  christos       this_hdr->sh_type = sh_type;
   3255  1.1  christos     }
   3256  1.1  christos 
   3257  1.1  christos   switch (this_hdr->sh_type)
   3258  1.1  christos     {
   3259  1.1  christos     default:
   3260  1.1  christos       break;
   3261  1.8  christos 
   3262  1.8  christos     case SHT_STRTAB:
   3263  1.8  christos     case SHT_NOTE:
   3264  1.8  christos     case SHT_NOBITS:
   3265  1.8  christos     case SHT_PROGBITS:
   3266  1.1  christos       break;
   3267  1.1  christos 
   3268  1.1  christos     case SHT_INIT_ARRAY:
   3269  1.8  christos     case SHT_FINI_ARRAY:
   3270  1.1  christos     case SHT_PREINIT_ARRAY:
   3271  1.1  christos       this_hdr->sh_entsize = bed->s->arch_size / 8;
   3272  1.1  christos       break;
   3273  1.1  christos 
   3274  1.1  christos     case SHT_HASH:
   3275  1.1  christos       this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
   3276  1.1  christos       break;
   3277  1.1  christos 
   3278  1.1  christos     case SHT_DYNSYM:
   3279  1.1  christos       this_hdr->sh_entsize = bed->s->sizeof_sym;
   3280  1.1  christos       break;
   3281  1.1  christos 
   3282  1.1  christos     case SHT_DYNAMIC:
   3283  1.1  christos       this_hdr->sh_entsize = bed->s->sizeof_dyn;
   3284  1.1  christos       break;
   3285  1.1  christos 
   3286  1.1  christos     case SHT_RELA:
   3287  1.1  christos       if (get_elf_backend_data (abfd)->may_use_rela_p)
   3288  1.1  christos 	this_hdr->sh_entsize = bed->s->sizeof_rela;
   3289  1.1  christos       break;
   3290  1.1  christos 
   3291  1.1  christos      case SHT_REL:
   3292  1.1  christos       if (get_elf_backend_data (abfd)->may_use_rel_p)
   3293  1.1  christos 	this_hdr->sh_entsize = bed->s->sizeof_rel;
   3294  1.1  christos       break;
   3295  1.1  christos 
   3296  1.1  christos      case SHT_GNU_versym:
   3297  1.1  christos       this_hdr->sh_entsize = sizeof (Elf_External_Versym);
   3298  1.1  christos       break;
   3299  1.1  christos 
   3300  1.1  christos      case SHT_GNU_verdef:
   3301  1.1  christos       this_hdr->sh_entsize = 0;
   3302  1.1  christos       /* objcopy or strip will copy over sh_info, but may not set
   3303  1.1  christos 	 cverdefs.  The linker will set cverdefs, but sh_info will be
   3304  1.1  christos 	 zero.  */
   3305  1.1  christos       if (this_hdr->sh_info == 0)
   3306  1.1  christos 	this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
   3307  1.1  christos       else
   3308  1.1  christos 	BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
   3309  1.1  christos 		    || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
   3310  1.1  christos       break;
   3311  1.1  christos 
   3312  1.1  christos     case SHT_GNU_verneed:
   3313  1.1  christos       this_hdr->sh_entsize = 0;
   3314  1.1  christos       /* objcopy or strip will copy over sh_info, but may not set
   3315  1.1  christos 	 cverrefs.  The linker will set cverrefs, but sh_info will be
   3316  1.1  christos 	 zero.  */
   3317  1.1  christos       if (this_hdr->sh_info == 0)
   3318  1.1  christos 	this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
   3319  1.1  christos       else
   3320  1.1  christos 	BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
   3321  1.1  christos 		    || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
   3322  1.1  christos       break;
   3323  1.1  christos 
   3324  1.1  christos     case SHT_GROUP:
   3325  1.1  christos       this_hdr->sh_entsize = GRP_ENTRY_SIZE;
   3326  1.1  christos       break;
   3327  1.1  christos 
   3328  1.1  christos     case SHT_GNU_HASH:
   3329  1.1  christos       this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
   3330  1.1  christos       break;
   3331  1.1  christos     }
   3332  1.1  christos 
   3333  1.1  christos   if ((asect->flags & SEC_ALLOC) != 0)
   3334  1.1  christos     this_hdr->sh_flags |= SHF_ALLOC;
   3335  1.1  christos   if ((asect->flags & SEC_READONLY) == 0)
   3336  1.1  christos     this_hdr->sh_flags |= SHF_WRITE;
   3337  1.1  christos   if ((asect->flags & SEC_CODE) != 0)
   3338  1.1  christos     this_hdr->sh_flags |= SHF_EXECINSTR;
   3339  1.1  christos   if ((asect->flags & SEC_MERGE) != 0)
   3340  1.1  christos     {
   3341  1.1  christos       this_hdr->sh_flags |= SHF_MERGE;
   3342  1.8  christos       this_hdr->sh_entsize = asect->entsize;
   3343  1.8  christos     }
   3344  1.1  christos   if ((asect->flags & SEC_STRINGS) != 0)
   3345  1.1  christos     this_hdr->sh_flags |= SHF_STRINGS;
   3346  1.1  christos   if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
   3347  1.1  christos     this_hdr->sh_flags |= SHF_GROUP;
   3348  1.1  christos   if ((asect->flags & SEC_THREAD_LOCAL) != 0)
   3349  1.1  christos     {
   3350  1.1  christos       this_hdr->sh_flags |= SHF_TLS;
   3351  1.1  christos       if (asect->size == 0
   3352  1.1  christos 	  && (asect->flags & SEC_HAS_CONTENTS) == 0)
   3353  1.1  christos 	{
   3354  1.1  christos 	  struct bfd_link_order *o = asect->map_tail.link_order;
   3355  1.1  christos 
   3356  1.1  christos 	  this_hdr->sh_size = 0;
   3357  1.1  christos 	  if (o != NULL)
   3358  1.1  christos 	    {
   3359  1.1  christos 	      this_hdr->sh_size = o->offset + o->size;
   3360  1.1  christos 	      if (this_hdr->sh_size != 0)
   3361  1.1  christos 		this_hdr->sh_type = SHT_NOBITS;
   3362  1.1  christos 	    }
   3363  1.1  christos 	}
   3364  1.1  christos     }
   3365  1.1  christos   if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
   3366  1.1  christos     this_hdr->sh_flags |= SHF_EXCLUDE;
   3367  1.1  christos 
   3368  1.1  christos   /* If the section has relocs, set up a section header for the
   3369  1.1  christos      SHT_REL[A] section.  If two relocation sections are required for
   3370  1.1  christos      this section, it is up to the processor-specific back-end to
   3371  1.1  christos      create the other.  */
   3372  1.1  christos   if ((asect->flags & SEC_RELOC) != 0)
   3373  1.1  christos     {
   3374  1.1  christos       /* When doing a relocatable link, create both REL and RELA sections if
   3375  1.1  christos 	 needed.  */
   3376  1.1  christos       if (arg->link_info
   3377  1.8  christos 	  /* Do the normal setup if we wouldn't create any sections here.  */
   3378  1.8  christos 	  && esd->rel.count + esd->rela.count > 0
   3379  1.1  christos 	  && (bfd_link_relocatable (arg->link_info)
   3380  1.1  christos 	      || arg->link_info->emitrelocations))
   3381  1.6  christos 	{
   3382  1.6  christos 	  if (esd->rel.count && esd->rel.hdr == NULL
   3383  1.1  christos 	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, FALSE,
   3384  1.1  christos 					    delay_st_name_p))
   3385  1.1  christos 	    {
   3386  1.1  christos 	      arg->failed = TRUE;
   3387  1.1  christos 	      return;
   3388  1.6  christos 	    }
   3389  1.6  christos 	  if (esd->rela.count && esd->rela.hdr == NULL
   3390  1.1  christos 	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, TRUE,
   3391  1.1  christos 					    delay_st_name_p))
   3392  1.1  christos 	    {
   3393  1.1  christos 	      arg->failed = TRUE;
   3394  1.1  christos 	      return;
   3395  1.1  christos 	    }
   3396  1.1  christos 	}
   3397  1.1  christos       else if (!_bfd_elf_init_reloc_shdr (abfd,
   3398  1.6  christos 					  (asect->use_rela_p
   3399  1.6  christos 					   ? &esd->rela : &esd->rel),
   3400  1.6  christos 					  name,
   3401  1.1  christos 					  asect->use_rela_p,
   3402  1.1  christos 					  delay_st_name_p))
   3403  1.1  christos 	  arg->failed = TRUE;
   3404  1.1  christos     }
   3405  1.1  christos 
   3406  1.1  christos   /* Check for processor-specific section types.  */
   3407  1.1  christos   sh_type = this_hdr->sh_type;
   3408  1.1  christos   if (bed->elf_backend_fake_sections
   3409  1.1  christos       && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
   3410  1.1  christos     arg->failed = TRUE;
   3411  1.1  christos 
   3412  1.1  christos   if (sh_type == SHT_NOBITS && asect->size != 0)
   3413  1.1  christos     {
   3414  1.1  christos       /* Don't change the header type from NOBITS if we are being
   3415  1.1  christos 	 called for objcopy --only-keep-debug.  */
   3416  1.1  christos       this_hdr->sh_type = sh_type;
   3417  1.1  christos     }
   3418  1.1  christos }
   3419  1.1  christos 
   3420  1.1  christos /* Fill in the contents of a SHT_GROUP section.  Called from
   3421  1.1  christos    _bfd_elf_compute_section_file_positions for gas, objcopy, and
   3422  1.1  christos    when ELF targets use the generic linker, ld.  Called for ld -r
   3423  1.1  christos    from bfd_elf_final_link.  */
   3424  1.1  christos 
   3425  1.1  christos void
   3426  1.1  christos bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
   3427  1.1  christos {
   3428  1.1  christos   bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
   3429  1.1  christos   asection *elt, *first;
   3430  1.1  christos   unsigned char *loc;
   3431  1.1  christos   bfd_boolean gas;
   3432  1.1  christos 
   3433  1.1  christos   /* Ignore linker created group section.  See elfNN_ia64_object_p in
   3434  1.1  christos      elfxx-ia64.c.  */
   3435  1.1  christos   if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
   3436  1.1  christos       || *failedptr)
   3437  1.1  christos     return;
   3438  1.1  christos 
   3439  1.1  christos   if (elf_section_data (sec)->this_hdr.sh_info == 0)
   3440  1.1  christos     {
   3441  1.1  christos       unsigned long symindx = 0;
   3442  1.1  christos 
   3443  1.1  christos       /* elf_group_id will have been set up by objcopy and the
   3444  1.1  christos 	 generic linker.  */
   3445  1.1  christos       if (elf_group_id (sec) != NULL)
   3446  1.1  christos 	symindx = elf_group_id (sec)->udata.i;
   3447  1.1  christos 
   3448  1.1  christos       if (symindx == 0)
   3449  1.1  christos 	{
   3450  1.1  christos 	  /* If called from the assembler, swap_out_syms will have set up
   3451  1.1  christos 	     elf_section_syms.  */
   3452  1.1  christos 	  BFD_ASSERT (elf_section_syms (abfd) != NULL);
   3453  1.1  christos 	  symindx = elf_section_syms (abfd)[sec->index]->udata.i;
   3454  1.1  christos 	}
   3455  1.1  christos       elf_section_data (sec)->this_hdr.sh_info = symindx;
   3456  1.1  christos     }
   3457  1.1  christos   else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
   3458  1.1  christos     {
   3459  1.1  christos       /* The ELF backend linker sets sh_info to -2 when the group
   3460  1.9  christos 	 signature symbol is global, and thus the index can't be
   3461  1.9  christos 	 set until all local symbols are output.  */
   3462  1.9  christos       asection *igroup;
   3463  1.9  christos       struct bfd_elf_section_data *sec_data;
   3464  1.1  christos       unsigned long symndx;
   3465  1.1  christos       unsigned long extsymoff;
   3466  1.9  christos       struct elf_link_hash_entry *h;
   3467  1.9  christos 
   3468  1.9  christos       /* The point of this little dance to the first SHF_GROUP section
   3469  1.9  christos 	 then back to the SHT_GROUP section is that this gets us to
   3470  1.9  christos 	 the SHT_GROUP in the input object.  */
   3471  1.9  christos       igroup = elf_sec_group (elf_next_in_group (sec));
   3472  1.9  christos       sec_data = elf_section_data (igroup);
   3473  1.1  christos       symndx = sec_data->this_hdr.sh_info;
   3474  1.1  christos       extsymoff = 0;
   3475  1.1  christos       if (!elf_bad_symtab (igroup->owner))
   3476  1.1  christos 	{
   3477  1.1  christos 	  Elf_Internal_Shdr *symtab_hdr;
   3478  1.1  christos 
   3479  1.1  christos 	  symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
   3480  1.1  christos 	  extsymoff = symtab_hdr->sh_info;
   3481  1.1  christos 	}
   3482  1.1  christos       h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
   3483  1.1  christos       while (h->root.type == bfd_link_hash_indirect
   3484  1.1  christos 	     || h->root.type == bfd_link_hash_warning)
   3485  1.1  christos 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3486  1.1  christos 
   3487  1.1  christos       elf_section_data (sec)->this_hdr.sh_info = h->indx;
   3488  1.1  christos     }
   3489  1.1  christos 
   3490  1.1  christos   /* The contents won't be allocated for "ld -r" or objcopy.  */
   3491  1.1  christos   gas = TRUE;
   3492  1.1  christos   if (sec->contents == NULL)
   3493  1.1  christos     {
   3494  1.1  christos       gas = FALSE;
   3495  1.1  christos       sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
   3496  1.1  christos 
   3497  1.1  christos       /* Arrange for the section to be written out.  */
   3498  1.1  christos       elf_section_data (sec)->this_hdr.contents = sec->contents;
   3499  1.1  christos       if (sec->contents == NULL)
   3500  1.1  christos 	{
   3501  1.1  christos 	  *failedptr = TRUE;
   3502  1.1  christos 	  return;
   3503  1.1  christos 	}
   3504  1.1  christos     }
   3505  1.1  christos 
   3506  1.1  christos   loc = sec->contents + sec->size;
   3507  1.1  christos 
   3508  1.1  christos   /* Get the pointer to the first section in the group that gas
   3509  1.1  christos      squirreled away here.  objcopy arranges for this to be set to the
   3510  1.1  christos      start of the input section group.  */
   3511  1.1  christos   first = elt = elf_next_in_group (sec);
   3512  1.1  christos 
   3513  1.1  christos   /* First element is a flag word.  Rest of section is elf section
   3514  1.1  christos      indices for all the sections of the group.  Write them backwards
   3515  1.1  christos      just to keep the group in the same order as given in .section
   3516  1.1  christos      directives, not that it matters.  */
   3517  1.1  christos   while (elt != NULL)
   3518  1.1  christos     {
   3519  1.1  christos       asection *s;
   3520  1.1  christos 
   3521  1.1  christos       s = elt;
   3522  1.1  christos       if (!gas)
   3523  1.1  christos 	s = s->output_section;
   3524  1.1  christos       if (s != NULL
   3525  1.1  christos 	  && !bfd_is_abs_section (s))
   3526  1.1  christos 	{
   3527  1.1  christos 	  unsigned int idx = elf_section_data (s)->this_idx;
   3528  1.1  christos 
   3529  1.1  christos 	  loc -= 4;
   3530  1.1  christos 	  H_PUT_32 (abfd, idx, loc);
   3531  1.1  christos 	}
   3532  1.1  christos       elt = elf_next_in_group (elt);
   3533  1.1  christos       if (elt == first)
   3534  1.1  christos 	break;
   3535  1.1  christos     }
   3536  1.1  christos 
   3537  1.1  christos   if ((loc -= 4) != sec->contents)
   3538  1.1  christos     abort ();
   3539  1.1  christos 
   3540  1.1  christos   H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
   3541  1.6  christos }
   3542  1.6  christos 
   3543  1.6  christos /* Return the section which RELOC_SEC applies to.  */
   3544  1.6  christos 
   3545  1.6  christos asection *
   3546  1.6  christos _bfd_elf_get_reloc_section (asection *reloc_sec)
   3547  1.6  christos {
   3548  1.6  christos   const char *name;
   3549  1.6  christos   unsigned int type;
   3550  1.6  christos   bfd *abfd;
   3551  1.6  christos 
   3552  1.6  christos   if (reloc_sec == NULL)
   3553  1.6  christos     return NULL;
   3554  1.6  christos 
   3555  1.6  christos   type = elf_section_data (reloc_sec)->this_hdr.sh_type;
   3556  1.6  christos   if (type != SHT_REL && type != SHT_RELA)
   3557  1.6  christos     return NULL;
   3558  1.6  christos 
   3559  1.6  christos   /* We look up the section the relocs apply to by name.  */
   3560  1.6  christos   name = reloc_sec->name;
   3561  1.6  christos   if (type == SHT_REL)
   3562  1.6  christos     name += 4;
   3563  1.6  christos   else
   3564  1.6  christos     name += 5;
   3565  1.6  christos 
   3566  1.6  christos   /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
   3567  1.6  christos      section apply to .got.plt section.  */
   3568  1.6  christos   abfd = reloc_sec->owner;
   3569  1.6  christos   if (get_elf_backend_data (abfd)->want_got_plt
   3570  1.6  christos       && strcmp (name, ".plt") == 0)
   3571  1.6  christos     {
   3572  1.6  christos       /* .got.plt is a linker created input section.  It may be mapped
   3573  1.6  christos 	 to some other output section.  Try two likely sections.  */
   3574  1.6  christos       name = ".got.plt";
   3575  1.6  christos       reloc_sec = bfd_get_section_by_name (abfd, name);
   3576  1.6  christos       if (reloc_sec != NULL)
   3577  1.6  christos 	return reloc_sec;
   3578  1.6  christos       name = ".got";
   3579  1.6  christos     }
   3580  1.6  christos 
   3581  1.6  christos   reloc_sec = bfd_get_section_by_name (abfd, name);
   3582  1.6  christos   return reloc_sec;
   3583  1.1  christos }
   3584  1.1  christos 
   3585  1.1  christos /* Assign all ELF section numbers.  The dummy first section is handled here
   3586  1.1  christos    too.  The link/info pointers for the standard section types are filled
   3587  1.1  christos    in here too, while we're at it.  */
   3588  1.1  christos 
   3589  1.1  christos static bfd_boolean
   3590  1.1  christos assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
   3591  1.1  christos {
   3592  1.6  christos   struct elf_obj_tdata *t = elf_tdata (abfd);
   3593  1.1  christos   asection *sec;
   3594  1.1  christos   unsigned int section_number;
   3595  1.1  christos   Elf_Internal_Shdr **i_shdrp;
   3596  1.1  christos   struct bfd_elf_section_data *d;
   3597  1.1  christos   bfd_boolean need_symtab;
   3598  1.1  christos 
   3599  1.1  christos   section_number = 1;
   3600  1.1  christos 
   3601  1.1  christos   _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
   3602  1.8  christos 
   3603  1.1  christos   /* SHT_GROUP sections are in relocatable files only.  */
   3604  1.8  christos   if (link_info == NULL || bfd_link_relocatable (link_info))
   3605  1.8  christos     {
   3606  1.1  christos       size_t reloc_count = 0;
   3607  1.1  christos 
   3608  1.1  christos       /* Put SHT_GROUP sections first.  */
   3609  1.1  christos       for (sec = abfd->sections; sec != NULL; sec = sec->next)
   3610  1.1  christos 	{
   3611  1.1  christos 	  d = elf_section_data (sec);
   3612  1.1  christos 
   3613  1.1  christos 	  if (d->this_hdr.sh_type == SHT_GROUP)
   3614  1.1  christos 	    {
   3615  1.1  christos 	      if (sec->flags & SEC_LINKER_CREATED)
   3616  1.1  christos 		{
   3617  1.1  christos 		  /* Remove the linker created SHT_GROUP sections.  */
   3618  1.1  christos 		  bfd_section_list_remove (abfd, sec);
   3619  1.1  christos 		  abfd->section_count--;
   3620  1.1  christos 		}
   3621  1.1  christos 	      else
   3622  1.8  christos 		d->this_idx = section_number++;
   3623  1.8  christos 	    }
   3624  1.8  christos 
   3625  1.1  christos 	  /* Count relocations.  */
   3626  1.8  christos 	  reloc_count += sec->reloc_count;
   3627  1.8  christos 	}
   3628  1.8  christos 
   3629  1.8  christos       /* Clear HAS_RELOC if there are no relocations.  */
   3630  1.1  christos       if (reloc_count == 0)
   3631  1.1  christos 	abfd->flags &= ~HAS_RELOC;
   3632  1.1  christos     }
   3633  1.1  christos 
   3634  1.1  christos   for (sec = abfd->sections; sec; sec = sec->next)
   3635  1.1  christos     {
   3636  1.1  christos       d = elf_section_data (sec);
   3637  1.1  christos 
   3638  1.6  christos       if (d->this_hdr.sh_type != SHT_GROUP)
   3639  1.6  christos 	d->this_idx = section_number++;
   3640  1.1  christos       if (d->this_hdr.sh_name != (unsigned int) -1)
   3641  1.1  christos 	_bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
   3642  1.1  christos       if (d->rel.hdr)
   3643  1.6  christos 	{
   3644  1.6  christos 	  d->rel.idx = section_number++;
   3645  1.1  christos 	  if (d->rel.hdr->sh_name != (unsigned int) -1)
   3646  1.1  christos 	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
   3647  1.1  christos 	}
   3648  1.1  christos       else
   3649  1.1  christos 	d->rel.idx = 0;
   3650  1.1  christos 
   3651  1.1  christos       if (d->rela.hdr)
   3652  1.6  christos 	{
   3653  1.6  christos 	  d->rela.idx = section_number++;
   3654  1.1  christos 	  if (d->rela.hdr->sh_name != (unsigned int) -1)
   3655  1.1  christos 	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
   3656  1.1  christos 	}
   3657  1.1  christos       else
   3658  1.1  christos 	d->rela.idx = 0;
   3659  1.1  christos     }
   3660  1.1  christos 
   3661  1.1  christos   need_symtab = (bfd_get_symcount (abfd) > 0
   3662  1.1  christos 		|| (link_info == NULL
   3663  1.1  christos 		    && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
   3664  1.1  christos 			== HAS_RELOC)));
   3665  1.3  christos   if (need_symtab)
   3666  1.1  christos     {
   3667  1.1  christos       elf_onesymtab (abfd) = section_number++;
   3668  1.1  christos       _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
   3669  1.8  christos       if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
   3670  1.8  christos 	{
   3671  1.8  christos 	  elf_section_list * entry;
   3672  1.8  christos 
   3673  1.8  christos 	  BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
   3674  1.8  christos 
   3675  1.8  christos 	  entry = bfd_zalloc (abfd, sizeof * entry);
   3676  1.8  christos 	  entry->ndx = section_number++;
   3677  1.1  christos 	  elf_symtab_shndx_list (abfd) = entry;
   3678  1.1  christos 	  entry->hdr.sh_name
   3679  1.8  christos 	    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   3680  1.1  christos 						  ".symtab_shndx", FALSE);
   3681  1.1  christos 	  if (entry->hdr.sh_name == (unsigned int) -1)
   3682  1.3  christos 	    return FALSE;
   3683  1.1  christos 	}
   3684  1.1  christos       elf_strtab_sec (abfd) = section_number++;
   3685  1.1  christos       _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
   3686  1.9  christos     }
   3687  1.9  christos 
   3688  1.9  christos   elf_shstrtab_sec (abfd) = section_number++;
   3689  1.9  christos   _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
   3690  1.3  christos   elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
   3691  1.3  christos 
   3692  1.9  christos   if (section_number >= SHN_LORESERVE)
   3693  1.3  christos     {
   3694  1.3  christos       /* xgettext:c-format */
   3695  1.3  christos       _bfd_error_handler (_("%B: too many sections: %u"),
   3696  1.3  christos 			  abfd, section_number);
   3697  1.3  christos       return FALSE;
   3698  1.1  christos     }
   3699  1.1  christos 
   3700  1.1  christos   elf_numsections (abfd) = section_number;
   3701  1.1  christos   elf_elfheader (abfd)->e_shnum = section_number;
   3702  1.1  christos 
   3703  1.1  christos   /* Set up the list of section header pointers, in agreement with the
   3704  1.1  christos      indices.  */
   3705  1.1  christos   i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
   3706  1.1  christos                                                 sizeof (Elf_Internal_Shdr *));
   3707  1.1  christos   if (i_shdrp == NULL)
   3708  1.1  christos     return FALSE;
   3709  1.1  christos 
   3710  1.1  christos   i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
   3711  1.1  christos                                                  sizeof (Elf_Internal_Shdr));
   3712  1.1  christos   if (i_shdrp[0] == NULL)
   3713  1.1  christos     {
   3714  1.1  christos       bfd_release (abfd, i_shdrp);
   3715  1.1  christos       return FALSE;
   3716  1.1  christos     }
   3717  1.1  christos 
   3718  1.3  christos   elf_elfsections (abfd) = i_shdrp;
   3719  1.1  christos 
   3720  1.1  christos   i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
   3721  1.3  christos   if (need_symtab)
   3722  1.1  christos     {
   3723  1.1  christos       i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
   3724  1.8  christos       if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
   3725  1.8  christos 	{
   3726  1.8  christos 	  elf_section_list * entry = elf_symtab_shndx_list (abfd);
   3727  1.8  christos 	  BFD_ASSERT (entry != NULL);
   3728  1.1  christos 	  i_shdrp[entry->ndx] = & entry->hdr;
   3729  1.3  christos 	  entry->hdr.sh_link = elf_onesymtab (abfd);
   3730  1.3  christos 	}
   3731  1.1  christos       i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
   3732  1.1  christos       t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
   3733  1.1  christos     }
   3734  1.1  christos 
   3735  1.1  christos   for (sec = abfd->sections; sec; sec = sec->next)
   3736  1.1  christos     {
   3737  1.1  christos       asection *s;
   3738  1.1  christos 
   3739  1.1  christos       d = elf_section_data (sec);
   3740  1.1  christos 
   3741  1.1  christos       i_shdrp[d->this_idx] = &d->this_hdr;
   3742  1.1  christos       if (d->rel.idx != 0)
   3743  1.1  christos 	i_shdrp[d->rel.idx] = d->rel.hdr;
   3744  1.1  christos       if (d->rela.idx != 0)
   3745  1.1  christos 	i_shdrp[d->rela.idx] = d->rela.hdr;
   3746  1.1  christos 
   3747  1.1  christos       /* Fill in the sh_link and sh_info fields while we're at it.  */
   3748  1.1  christos 
   3749  1.1  christos       /* sh_link of a reloc section is the section index of the symbol
   3750  1.1  christos 	 table.  sh_info is the section index of the section to which
   3751  1.1  christos 	 the relocation entries apply.  */
   3752  1.3  christos       if (d->rel.idx != 0)
   3753  1.1  christos 	{
   3754  1.4  christos 	  d->rel.hdr->sh_link = elf_onesymtab (abfd);
   3755  1.1  christos 	  d->rel.hdr->sh_info = d->this_idx;
   3756  1.1  christos 	  d->rel.hdr->sh_flags |= SHF_INFO_LINK;
   3757  1.1  christos 	}
   3758  1.3  christos       if (d->rela.idx != 0)
   3759  1.1  christos 	{
   3760  1.4  christos 	  d->rela.hdr->sh_link = elf_onesymtab (abfd);
   3761  1.1  christos 	  d->rela.hdr->sh_info = d->this_idx;
   3762  1.1  christos 	  d->rela.hdr->sh_flags |= SHF_INFO_LINK;
   3763  1.1  christos 	}
   3764  1.1  christos 
   3765  1.1  christos       /* We need to set up sh_link for SHF_LINK_ORDER.  */
   3766  1.1  christos       if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
   3767  1.1  christos 	{
   3768  1.1  christos 	  s = elf_linked_to_section (sec);
   3769  1.1  christos 	  if (s)
   3770  1.1  christos 	    {
   3771  1.1  christos 	      /* elf_linked_to_section points to the input section.  */
   3772  1.1  christos 	      if (link_info != NULL)
   3773  1.3  christos 		{
   3774  1.1  christos 		  /* Check discarded linkonce section.  */
   3775  1.1  christos 		  if (discarded_section (s))
   3776  1.9  christos 		    {
   3777  1.9  christos 		      asection *kept;
   3778  1.9  christos 		      _bfd_error_handler
   3779  1.9  christos 			/* xgettext:c-format */
   3780  1.1  christos 			(_("%B: sh_link of section `%A' points to"
   3781  1.1  christos 			   " discarded section `%A' of `%B'"),
   3782  1.1  christos 			 abfd, d->this_hdr.bfd_section,
   3783  1.1  christos 			 s, s->owner);
   3784  1.1  christos 		      /* Point to the kept section if it has the same
   3785  1.1  christos 			 size as the discarded one.  */
   3786  1.1  christos 		      kept = _bfd_elf_check_kept_section (s, link_info);
   3787  1.1  christos 		      if (kept == NULL)
   3788  1.1  christos 			{
   3789  1.1  christos 			  bfd_set_error (bfd_error_bad_value);
   3790  1.1  christos 			  return FALSE;
   3791  1.1  christos 			}
   3792  1.1  christos 		      s = kept;
   3793  1.1  christos 		    }
   3794  1.1  christos 
   3795  1.1  christos 		  s = s->output_section;
   3796  1.1  christos 		  BFD_ASSERT (s != NULL);
   3797  1.1  christos 		}
   3798  1.1  christos 	      else
   3799  1.1  christos 		{
   3800  1.1  christos 		  /* Handle objcopy. */
   3801  1.9  christos 		  if (s->output_section == NULL)
   3802  1.9  christos 		    {
   3803  1.9  christos 		      _bfd_error_handler
   3804  1.9  christos 			/* xgettext:c-format */
   3805  1.1  christos 			(_("%B: sh_link of section `%A' points to"
   3806  1.1  christos 			   " removed section `%A' of `%B'"),
   3807  1.1  christos 			 abfd, d->this_hdr.bfd_section, s, s->owner);
   3808  1.1  christos 		      bfd_set_error (bfd_error_bad_value);
   3809  1.1  christos 		      return FALSE;
   3810  1.1  christos 		    }
   3811  1.1  christos 		  s = s->output_section;
   3812  1.1  christos 		}
   3813  1.1  christos 	      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3814  1.1  christos 	    }
   3815  1.1  christos 	  else
   3816  1.1  christos 	    {
   3817  1.1  christos 	      /* PR 290:
   3818  1.1  christos 		 The Intel C compiler generates SHT_IA_64_UNWIND with
   3819  1.1  christos 		 SHF_LINK_ORDER.  But it doesn't set the sh_link or
   3820  1.1  christos 		 sh_info fields.  Hence we could get the situation
   3821  1.1  christos 		 where s is NULL.  */
   3822  1.1  christos 	      const struct elf_backend_data *bed
   3823  1.1  christos 		= get_elf_backend_data (abfd);
   3824  1.9  christos 	      if (bed->link_order_error_handler)
   3825  1.1  christos 		bed->link_order_error_handler
   3826  1.1  christos 		  /* xgettext:c-format */
   3827  1.1  christos 		  (_("%B: warning: sh_link not set for section `%A'"),
   3828  1.1  christos 		   abfd, sec);
   3829  1.1  christos 	    }
   3830  1.1  christos 	}
   3831  1.1  christos 
   3832  1.1  christos       switch (d->this_hdr.sh_type)
   3833  1.1  christos 	{
   3834  1.1  christos 	case SHT_REL:
   3835  1.1  christos 	case SHT_RELA:
   3836  1.1  christos 	  /* A reloc section which we are treating as a normal BFD
   3837  1.1  christos 	     section.  sh_link is the section index of the symbol
   3838  1.1  christos 	     table.  sh_info is the section index of the section to
   3839  1.1  christos 	     which the relocation entries apply.  We assume that an
   3840  1.1  christos 	     allocated reloc section uses the dynamic symbol table.
   3841  1.1  christos 	     FIXME: How can we be sure?  */
   3842  1.1  christos 	  s = bfd_get_section_by_name (abfd, ".dynsym");
   3843  1.1  christos 	  if (s != NULL)
   3844  1.6  christos 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3845  1.1  christos 
   3846  1.4  christos 	  s = get_elf_backend_data (abfd)->get_reloc_section (sec);
   3847  1.4  christos 	  if (s != NULL)
   3848  1.4  christos 	    {
   3849  1.4  christos 	      d->this_hdr.sh_info = elf_section_data (s)->this_idx;
   3850  1.1  christos 	      d->this_hdr.sh_flags |= SHF_INFO_LINK;
   3851  1.1  christos 	    }
   3852  1.1  christos 	  break;
   3853  1.1  christos 
   3854  1.1  christos 	case SHT_STRTAB:
   3855  1.1  christos 	  /* We assume that a section named .stab*str is a stabs
   3856  1.1  christos 	     string section.  We look for a section with the same name
   3857  1.1  christos 	     but without the trailing ``str'', and set its sh_link
   3858  1.1  christos 	     field to point to this section.  */
   3859  1.1  christos 	  if (CONST_STRNEQ (sec->name, ".stab")
   3860  1.1  christos 	      && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
   3861  1.1  christos 	    {
   3862  1.1  christos 	      size_t len;
   3863  1.1  christos 	      char *alc;
   3864  1.1  christos 
   3865  1.1  christos 	      len = strlen (sec->name);
   3866  1.1  christos 	      alc = (char *) bfd_malloc (len - 2);
   3867  1.1  christos 	      if (alc == NULL)
   3868  1.1  christos 		return FALSE;
   3869  1.1  christos 	      memcpy (alc, sec->name, len - 3);
   3870  1.1  christos 	      alc[len - 3] = '\0';
   3871  1.1  christos 	      s = bfd_get_section_by_name (abfd, alc);
   3872  1.1  christos 	      free (alc);
   3873  1.1  christos 	      if (s != NULL)
   3874  1.1  christos 		{
   3875  1.1  christos 		  elf_section_data (s)->this_hdr.sh_link = d->this_idx;
   3876  1.1  christos 
   3877  1.1  christos 		  /* This is a .stab section.  */
   3878  1.1  christos 		  if (elf_section_data (s)->this_hdr.sh_entsize == 0)
   3879  1.1  christos 		    elf_section_data (s)->this_hdr.sh_entsize
   3880  1.1  christos 		      = 4 + 2 * bfd_get_arch_size (abfd) / 8;
   3881  1.1  christos 		}
   3882  1.1  christos 	    }
   3883  1.1  christos 	  break;
   3884  1.1  christos 
   3885  1.1  christos 	case SHT_DYNAMIC:
   3886  1.1  christos 	case SHT_DYNSYM:
   3887  1.1  christos 	case SHT_GNU_verneed:
   3888  1.1  christos 	case SHT_GNU_verdef:
   3889  1.1  christos 	  /* sh_link is the section header index of the string table
   3890  1.1  christos 	     used for the dynamic entries, or the symbol table, or the
   3891  1.1  christos 	     version strings.  */
   3892  1.1  christos 	  s = bfd_get_section_by_name (abfd, ".dynstr");
   3893  1.1  christos 	  if (s != NULL)
   3894  1.1  christos 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3895  1.1  christos 	  break;
   3896  1.1  christos 
   3897  1.1  christos 	case SHT_GNU_LIBLIST:
   3898  1.1  christos 	  /* sh_link is the section header index of the prelink library
   3899  1.1  christos 	     list used for the dynamic entries, or the symbol table, or
   3900  1.1  christos 	     the version strings.  */
   3901  1.1  christos 	  s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
   3902  1.1  christos 					     ? ".dynstr" : ".gnu.libstr");
   3903  1.1  christos 	  if (s != NULL)
   3904  1.1  christos 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3905  1.1  christos 	  break;
   3906  1.1  christos 
   3907  1.1  christos 	case SHT_HASH:
   3908  1.1  christos 	case SHT_GNU_HASH:
   3909  1.1  christos 	case SHT_GNU_versym:
   3910  1.1  christos 	  /* sh_link is the section header index of the symbol table
   3911  1.1  christos 	     this hash table or version table is for.  */
   3912  1.1  christos 	  s = bfd_get_section_by_name (abfd, ".dynsym");
   3913  1.1  christos 	  if (s != NULL)
   3914  1.1  christos 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3915  1.1  christos 	  break;
   3916  1.3  christos 
   3917  1.1  christos 	case SHT_GROUP:
   3918  1.1  christos 	  d->this_hdr.sh_link = elf_onesymtab (abfd);
   3919  1.1  christos 	}
   3920  1.6  christos     }
   3921  1.6  christos 
   3922  1.6  christos   /* Delay setting sh_name to _bfd_elf_write_object_contents so that
   3923  1.6  christos      _bfd_elf_assign_file_positions_for_non_load can convert DWARF
   3924  1.1  christos      debug section name from .debug_* to .zdebug_* if needed.  */
   3925  1.1  christos 
   3926  1.1  christos   return TRUE;
   3927  1.1  christos }
   3928  1.1  christos 
   3929  1.1  christos static bfd_boolean
   3930  1.1  christos sym_is_global (bfd *abfd, asymbol *sym)
   3931  1.1  christos {
   3932  1.1  christos   /* If the backend has a special mapping, use it.  */
   3933  1.1  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3934  1.1  christos   if (bed->elf_backend_sym_is_global)
   3935  1.1  christos     return (*bed->elf_backend_sym_is_global) (abfd, sym);
   3936  1.1  christos 
   3937  1.1  christos   return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
   3938  1.1  christos 	  || bfd_is_und_section (bfd_get_section (sym))
   3939  1.1  christos 	  || bfd_is_com_section (bfd_get_section (sym)));
   3940  1.8  christos }
   3941  1.8  christos 
   3942  1.8  christos /* Filter global symbols of ABFD to include in the import library.  All
   3943  1.8  christos    SYMCOUNT symbols of ABFD can be examined from their pointers in
   3944  1.8  christos    SYMS.  Pointers of symbols to keep should be stored contiguously at
   3945  1.8  christos    the beginning of that array.
   3946  1.8  christos 
   3947  1.8  christos    Returns the number of symbols to keep.  */
   3948  1.8  christos 
   3949  1.8  christos unsigned int
   3950  1.8  christos _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
   3951  1.8  christos 				asymbol **syms, long symcount)
   3952  1.8  christos {
   3953  1.8  christos   long src_count, dst_count = 0;
   3954  1.8  christos 
   3955  1.8  christos   for (src_count = 0; src_count < symcount; src_count++)
   3956  1.8  christos     {
   3957  1.8  christos       asymbol *sym = syms[src_count];
   3958  1.8  christos       char *name = (char *) bfd_asymbol_name (sym);
   3959  1.8  christos       struct bfd_link_hash_entry *h;
   3960  1.8  christos 
   3961  1.8  christos       if (!sym_is_global (abfd, sym))
   3962  1.8  christos 	continue;
   3963  1.8  christos 
   3964  1.8  christos       h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
   3965  1.8  christos       if (h == NULL)
   3966  1.8  christos 	continue;
   3967  1.8  christos       if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
   3968  1.8  christos 	continue;
   3969  1.8  christos       if (h->linker_def || h->ldscript_def)
   3970  1.8  christos 	continue;
   3971  1.8  christos 
   3972  1.8  christos       syms[dst_count++] = sym;
   3973  1.8  christos     }
   3974  1.8  christos 
   3975  1.8  christos   syms[dst_count] = NULL;
   3976  1.8  christos 
   3977  1.8  christos   return dst_count;
   3978  1.1  christos }
   3979  1.3  christos 
   3980  1.1  christos /* Don't output section symbols for sections that are not going to be
   3981  1.1  christos    output, that are duplicates or there is no BFD section.  */
   3982  1.1  christos 
   3983  1.1  christos static bfd_boolean
   3984  1.3  christos ignore_section_sym (bfd *abfd, asymbol *sym)
   3985  1.3  christos {
   3986  1.3  christos   elf_symbol_type *type_ptr;
   3987  1.3  christos 
   3988  1.3  christos   if ((sym->flags & BSF_SECTION_SYM) == 0)
   3989  1.3  christos     return FALSE;
   3990  1.3  christos 
   3991  1.3  christos   type_ptr = elf_symbol_from (abfd, sym);
   3992  1.3  christos   return ((type_ptr != NULL
   3993  1.3  christos 	   && type_ptr->internal_elf_sym.st_shndx != 0
   3994  1.1  christos 	   && bfd_is_abs_section (sym->section))
   3995  1.3  christos 	  || !(sym->section->owner == abfd
   3996  1.3  christos 	       || (sym->section->output_section->owner == abfd
   3997  1.1  christos 		   && sym->section->output_offset == 0)
   3998  1.1  christos 	       || bfd_is_abs_section (sym->section)));
   3999  1.3  christos }
   4000  1.3  christos 
   4001  1.3  christos /* Map symbol from it's internal number to the external number, moving
   4002  1.1  christos    all local symbols to be at the head of the list.  */
   4003  1.3  christos 
   4004  1.1  christos static bfd_boolean
   4005  1.1  christos elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
   4006  1.1  christos {
   4007  1.1  christos   unsigned int symcount = bfd_get_symcount (abfd);
   4008  1.1  christos   asymbol **syms = bfd_get_outsymbols (abfd);
   4009  1.1  christos   asymbol **sect_syms;
   4010  1.1  christos   unsigned int num_locals = 0;
   4011  1.1  christos   unsigned int num_globals = 0;
   4012  1.8  christos   unsigned int num_locals2 = 0;
   4013  1.1  christos   unsigned int num_globals2 = 0;
   4014  1.1  christos   unsigned int max_index = 0;
   4015  1.1  christos   unsigned int idx;
   4016  1.1  christos   asection *asect;
   4017  1.1  christos   asymbol **new_syms;
   4018  1.1  christos 
   4019  1.1  christos #ifdef DEBUG
   4020  1.1  christos   fprintf (stderr, "elf_map_symbols\n");
   4021  1.1  christos   fflush (stderr);
   4022  1.1  christos #endif
   4023  1.1  christos 
   4024  1.1  christos   for (asect = abfd->sections; asect; asect = asect->next)
   4025  1.1  christos     {
   4026  1.1  christos       if (max_index < asect->index)
   4027  1.1  christos 	max_index = asect->index;
   4028  1.1  christos     }
   4029  1.1  christos 
   4030  1.1  christos   max_index++;
   4031  1.1  christos   sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
   4032  1.1  christos   if (sect_syms == NULL)
   4033  1.1  christos     return FALSE;
   4034  1.1  christos   elf_section_syms (abfd) = sect_syms;
   4035  1.1  christos   elf_num_section_syms (abfd) = max_index;
   4036  1.1  christos 
   4037  1.1  christos   /* Init sect_syms entries for any section symbols we have already
   4038  1.1  christos      decided to output.  */
   4039  1.1  christos   for (idx = 0; idx < symcount; idx++)
   4040  1.1  christos     {
   4041  1.1  christos       asymbol *sym = syms[idx];
   4042  1.1  christos 
   4043  1.3  christos       if ((sym->flags & BSF_SECTION_SYM) != 0
   4044  1.3  christos 	  && sym->value == 0
   4045  1.1  christos 	  && !ignore_section_sym (abfd, sym)
   4046  1.1  christos 	  && !bfd_is_abs_section (sym->section))
   4047  1.1  christos 	{
   4048  1.1  christos 	  asection *sec = sym->section;
   4049  1.1  christos 
   4050  1.1  christos 	  if (sec->owner != abfd)
   4051  1.1  christos 	    sec = sec->output_section;
   4052  1.1  christos 
   4053  1.1  christos 	  sect_syms[sec->index] = syms[idx];
   4054  1.1  christos 	}
   4055  1.1  christos     }
   4056  1.1  christos 
   4057  1.1  christos   /* Classify all of the symbols.  */
   4058  1.3  christos   for (idx = 0; idx < symcount; idx++)
   4059  1.3  christos     {
   4060  1.3  christos       if (sym_is_global (abfd, syms[idx]))
   4061  1.1  christos 	num_globals++;
   4062  1.1  christos       else if (!ignore_section_sym (abfd, syms[idx]))
   4063  1.1  christos 	num_locals++;
   4064  1.1  christos     }
   4065  1.1  christos 
   4066  1.1  christos   /* We will be adding a section symbol for each normal BFD section.  Most
   4067  1.1  christos      sections will already have a section symbol in outsymbols, but
   4068  1.1  christos      eg. SHT_GROUP sections will not, and we need the section symbol mapped
   4069  1.1  christos      at least in that case.  */
   4070  1.1  christos   for (asect = abfd->sections; asect; asect = asect->next)
   4071  1.1  christos     {
   4072  1.1  christos       if (sect_syms[asect->index] == NULL)
   4073  1.1  christos 	{
   4074  1.1  christos 	  if (!sym_is_global (abfd, asect->symbol))
   4075  1.1  christos 	    num_locals++;
   4076  1.1  christos 	  else
   4077  1.1  christos 	    num_globals++;
   4078  1.1  christos 	}
   4079  1.1  christos     }
   4080  1.1  christos 
   4081  1.1  christos   /* Now sort the symbols so the local symbols are first.  */
   4082  1.1  christos   new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
   4083  1.1  christos                                       sizeof (asymbol *));
   4084  1.1  christos 
   4085  1.1  christos   if (new_syms == NULL)
   4086  1.1  christos     return FALSE;
   4087  1.1  christos 
   4088  1.1  christos   for (idx = 0; idx < symcount; idx++)
   4089  1.1  christos     {
   4090  1.1  christos       asymbol *sym = syms[idx];
   4091  1.3  christos       unsigned int i;
   4092  1.3  christos 
   4093  1.3  christos       if (sym_is_global (abfd, sym))
   4094  1.1  christos 	i = num_locals + num_globals2++;
   4095  1.1  christos       else if (!ignore_section_sym (abfd, sym))
   4096  1.3  christos 	i = num_locals2++;
   4097  1.1  christos       else
   4098  1.1  christos 	continue;
   4099  1.1  christos       new_syms[i] = sym;
   4100  1.1  christos       sym->udata.i = i + 1;
   4101  1.1  christos     }
   4102  1.1  christos   for (asect = abfd->sections; asect; asect = asect->next)
   4103  1.1  christos     {
   4104  1.1  christos       if (sect_syms[asect->index] == NULL)
   4105  1.1  christos 	{
   4106  1.1  christos 	  asymbol *sym = asect->symbol;
   4107  1.1  christos 	  unsigned int i;
   4108  1.1  christos 
   4109  1.1  christos 	  sect_syms[asect->index] = sym;
   4110  1.1  christos 	  if (!sym_is_global (abfd, sym))
   4111  1.1  christos 	    i = num_locals2++;
   4112  1.1  christos 	  else
   4113  1.1  christos 	    i = num_locals + num_globals2++;
   4114  1.1  christos 	  new_syms[i] = sym;
   4115  1.1  christos 	  sym->udata.i = i + 1;
   4116  1.1  christos 	}
   4117  1.1  christos     }
   4118  1.1  christos 
   4119  1.3  christos   bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
   4120  1.1  christos 
   4121  1.1  christos   *pnum_locals = num_locals;
   4122  1.1  christos   return TRUE;
   4123  1.1  christos }
   4124  1.1  christos 
   4125  1.1  christos /* Align to the maximum file alignment that could be required for any
   4126  1.1  christos    ELF data structure.  */
   4127  1.1  christos 
   4128  1.1  christos static inline file_ptr
   4129  1.1  christos align_file_position (file_ptr off, int align)
   4130  1.1  christos {
   4131  1.1  christos   return (off + align - 1) & ~(align - 1);
   4132  1.1  christos }
   4133  1.1  christos 
   4134  1.1  christos /* Assign a file position to a section, optionally aligning to the
   4135  1.1  christos    required section alignment.  */
   4136  1.1  christos 
   4137  1.1  christos file_ptr
   4138  1.1  christos _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
   4139  1.1  christos 					   file_ptr offset,
   4140  1.1  christos 					   bfd_boolean align)
   4141  1.1  christos {
   4142  1.1  christos   if (align && i_shdrp->sh_addralign > 1)
   4143  1.1  christos     offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
   4144  1.1  christos   i_shdrp->sh_offset = offset;
   4145  1.1  christos   if (i_shdrp->bfd_section != NULL)
   4146  1.1  christos     i_shdrp->bfd_section->filepos = offset;
   4147  1.1  christos   if (i_shdrp->sh_type != SHT_NOBITS)
   4148  1.1  christos     offset += i_shdrp->sh_size;
   4149  1.1  christos   return offset;
   4150  1.1  christos }
   4151  1.1  christos 
   4152  1.1  christos /* Compute the file positions we are going to put the sections at, and
   4153  1.1  christos    otherwise prepare to begin writing out the ELF file.  If LINK_INFO
   4154  1.1  christos    is not NULL, this is being called by the ELF backend linker.  */
   4155  1.1  christos 
   4156  1.1  christos bfd_boolean
   4157  1.1  christos _bfd_elf_compute_section_file_positions (bfd *abfd,
   4158  1.1  christos 					 struct bfd_link_info *link_info)
   4159  1.1  christos {
   4160  1.1  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4161  1.6  christos   struct fake_section_arg fsargs;
   4162  1.1  christos   bfd_boolean failed;
   4163  1.1  christos   struct elf_strtab_hash *strtab = NULL;
   4164  1.1  christos   Elf_Internal_Shdr *shstrtab_hdr;
   4165  1.1  christos   bfd_boolean need_symtab;
   4166  1.1  christos 
   4167  1.1  christos   if (abfd->output_has_begun)
   4168  1.1  christos     return TRUE;
   4169  1.1  christos 
   4170  1.1  christos   /* Do any elf backend specific processing first.  */
   4171  1.1  christos   if (bed->elf_backend_begin_write_processing)
   4172  1.1  christos     (*bed->elf_backend_begin_write_processing) (abfd, link_info);
   4173  1.1  christos 
   4174  1.1  christos   if (! prep_headers (abfd))
   4175  1.1  christos     return FALSE;
   4176  1.4  christos 
   4177  1.1  christos   /* Post process the headers if necessary.  */
   4178  1.1  christos   (*bed->elf_backend_post_process_headers) (abfd, link_info);
   4179  1.1  christos 
   4180  1.1  christos   fsargs.failed = FALSE;
   4181  1.1  christos   fsargs.link_info = link_info;
   4182  1.1  christos   bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
   4183  1.1  christos   if (fsargs.failed)
   4184  1.1  christos     return FALSE;
   4185  1.1  christos 
   4186  1.1  christos   if (!assign_section_numbers (abfd, link_info))
   4187  1.1  christos     return FALSE;
   4188  1.1  christos 
   4189  1.1  christos   /* The backend linker builds symbol table information itself.  */
   4190  1.1  christos   need_symtab = (link_info == NULL
   4191  1.1  christos 		 && (bfd_get_symcount (abfd) > 0
   4192  1.1  christos 		     || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
   4193  1.1  christos 			 == HAS_RELOC)));
   4194  1.1  christos   if (need_symtab)
   4195  1.1  christos     {
   4196  1.1  christos       /* Non-zero if doing a relocatable link.  */
   4197  1.1  christos       int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
   4198  1.1  christos 
   4199  1.1  christos       if (! swap_out_syms (abfd, &strtab, relocatable_p))
   4200  1.1  christos 	return FALSE;
   4201  1.1  christos     }
   4202  1.1  christos 
   4203  1.1  christos   failed = FALSE;
   4204  1.1  christos   if (link_info == NULL)
   4205  1.1  christos     {
   4206  1.1  christos       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
   4207  1.1  christos       if (failed)
   4208  1.1  christos 	return FALSE;
   4209  1.1  christos     }
   4210  1.1  christos 
   4211  1.1  christos   shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
   4212  1.8  christos   /* sh_name was set in prep_headers.  */
   4213  1.1  christos   shstrtab_hdr->sh_type = SHT_STRTAB;
   4214  1.6  christos   shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   4215  1.1  christos   shstrtab_hdr->sh_addr = 0;
   4216  1.1  christos   /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
   4217  1.1  christos   shstrtab_hdr->sh_entsize = 0;
   4218  1.6  christos   shstrtab_hdr->sh_link = 0;
   4219  1.1  christos   shstrtab_hdr->sh_info = 0;
   4220  1.1  christos   /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
   4221  1.1  christos   shstrtab_hdr->sh_addralign = 1;
   4222  1.1  christos 
   4223  1.1  christos   if (!assign_file_positions_except_relocs (abfd, link_info))
   4224  1.1  christos     return FALSE;
   4225  1.1  christos 
   4226  1.1  christos   if (need_symtab)
   4227  1.1  christos     {
   4228  1.1  christos       file_ptr off;
   4229  1.3  christos       Elf_Internal_Shdr *hdr;
   4230  1.1  christos 
   4231  1.8  christos       off = elf_next_file_pos (abfd);
   4232  1.1  christos 
   4233  1.1  christos       hdr = & elf_symtab_hdr (abfd);
   4234  1.8  christos       off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
   4235  1.8  christos 
   4236  1.8  christos       if (elf_symtab_shndx_list (abfd) != NULL)
   4237  1.8  christos 	{
   4238  1.8  christos 	  hdr = & elf_symtab_shndx_list (abfd)->hdr;
   4239  1.8  christos 	  if (hdr->sh_size != 0)
   4240  1.8  christos 	    off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
   4241  1.1  christos 	  /* FIXME: What about other symtab_shndx sections in the list ?  */
   4242  1.1  christos 	}
   4243  1.1  christos 
   4244  1.1  christos       hdr = &elf_tdata (abfd)->strtab_hdr;
   4245  1.3  christos       off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
   4246  1.1  christos 
   4247  1.1  christos       elf_next_file_pos (abfd) = off;
   4248  1.1  christos 
   4249  1.1  christos       /* Now that we know where the .strtab section goes, write it
   4250  1.6  christos 	 out.  */
   4251  1.1  christos       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   4252  1.6  christos 	  || ! _bfd_elf_strtab_emit (abfd, strtab))
   4253  1.1  christos 	return FALSE;
   4254  1.1  christos       _bfd_elf_strtab_free (strtab);
   4255  1.1  christos     }
   4256  1.1  christos 
   4257  1.1  christos   abfd->output_has_begun = TRUE;
   4258  1.1  christos 
   4259  1.1  christos   return TRUE;
   4260  1.1  christos }
   4261  1.1  christos 
   4262  1.1  christos /* Make an initial estimate of the size of the program header.  If we
   4263  1.1  christos    get the number wrong here, we'll redo section placement.  */
   4264  1.1  christos 
   4265  1.1  christos static bfd_size_type
   4266  1.1  christos get_program_header_size (bfd *abfd, struct bfd_link_info *info)
   4267  1.1  christos {
   4268  1.1  christos   size_t segs;
   4269  1.1  christos   asection *s;
   4270  1.1  christos   const struct elf_backend_data *bed;
   4271  1.1  christos 
   4272  1.1  christos   /* Assume we will need exactly two PT_LOAD segments: one for text
   4273  1.1  christos      and one for data.  */
   4274  1.1  christos   segs = 2;
   4275  1.1  christos 
   4276  1.1  christos   s = bfd_get_section_by_name (abfd, ".interp");
   4277  1.1  christos   if (s != NULL && (s->flags & SEC_LOAD) != 0)
   4278  1.1  christos     {
   4279  1.1  christos       /* If we have a loadable interpreter section, we need a
   4280  1.1  christos 	 PT_INTERP segment.  In this case, assume we also need a
   4281  1.1  christos 	 PT_PHDR segment, although that may not be true for all
   4282  1.1  christos 	 targets.  */
   4283  1.1  christos       segs += 2;
   4284  1.1  christos     }
   4285  1.1  christos 
   4286  1.1  christos   if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
   4287  1.1  christos     {
   4288  1.1  christos       /* We need a PT_DYNAMIC segment.  */
   4289  1.1  christos       ++segs;
   4290  1.1  christos     }
   4291  1.1  christos 
   4292  1.1  christos   if (info != NULL && info->relro)
   4293  1.1  christos     {
   4294  1.1  christos       /* We need a PT_GNU_RELRO segment.  */
   4295  1.1  christos       ++segs;
   4296  1.3  christos     }
   4297  1.1  christos 
   4298  1.1  christos   if (elf_eh_frame_hdr (abfd))
   4299  1.1  christos     {
   4300  1.1  christos       /* We need a PT_GNU_EH_FRAME segment.  */
   4301  1.1  christos       ++segs;
   4302  1.3  christos     }
   4303  1.1  christos 
   4304  1.1  christos   if (elf_stack_flags (abfd))
   4305  1.1  christos     {
   4306  1.1  christos       /* We need a PT_GNU_STACK segment.  */
   4307  1.1  christos       ++segs;
   4308  1.1  christos     }
   4309  1.1  christos 
   4310  1.1  christos   for (s = abfd->sections; s != NULL; s = s->next)
   4311  1.1  christos     {
   4312  1.1  christos       if ((s->flags & SEC_LOAD) != 0
   4313  1.1  christos 	  && CONST_STRNEQ (s->name, ".note"))
   4314  1.1  christos 	{
   4315  1.1  christos 	  /* We need a PT_NOTE segment.  */
   4316  1.1  christos 	  ++segs;
   4317  1.1  christos 	  /* Try to create just one PT_NOTE segment
   4318  1.1  christos 	     for all adjacent loadable .note* sections.
   4319  1.1  christos 	     gABI requires that within a PT_NOTE segment
   4320  1.1  christos 	     (and also inside of each SHT_NOTE section)
   4321  1.1  christos 	     each note is padded to a multiple of 4 size,
   4322  1.1  christos 	     so we check whether the sections are correctly
   4323  1.1  christos 	     aligned.  */
   4324  1.1  christos 	  if (s->alignment_power == 2)
   4325  1.1  christos 	    while (s->next != NULL
   4326  1.1  christos 		   && s->next->alignment_power == 2
   4327  1.1  christos 		   && (s->next->flags & SEC_LOAD) != 0
   4328  1.1  christos 		   && CONST_STRNEQ (s->next->name, ".note"))
   4329  1.1  christos 	      s = s->next;
   4330  1.1  christos 	}
   4331  1.1  christos     }
   4332  1.1  christos 
   4333  1.1  christos   for (s = abfd->sections; s != NULL; s = s->next)
   4334  1.1  christos     {
   4335  1.1  christos       if (s->flags & SEC_THREAD_LOCAL)
   4336  1.1  christos 	{
   4337  1.1  christos 	  /* We need a PT_TLS segment.  */
   4338  1.1  christos 	  ++segs;
   4339  1.1  christos 	  break;
   4340  1.1  christos 	}
   4341  1.1  christos     }
   4342  1.9  christos 
   4343  1.9  christos   bed = get_elf_backend_data (abfd);
   4344  1.9  christos 
   4345  1.9  christos  if ((abfd->flags & D_PAGED) != 0)
   4346  1.9  christos    {
   4347  1.9  christos      /* Add a PT_GNU_MBIND segment for each mbind section.  */
   4348  1.9  christos      unsigned int page_align_power = bfd_log2 (bed->commonpagesize);
   4349  1.9  christos      for (s = abfd->sections; s != NULL; s = s->next)
   4350  1.9  christos        if (elf_section_flags (s) & SHF_GNU_MBIND)
   4351  1.9  christos 	 {
   4352  1.9  christos 	   if (elf_section_data (s)->this_hdr.sh_info
   4353  1.9  christos 	       > PT_GNU_MBIND_NUM)
   4354  1.9  christos 	     {
   4355  1.9  christos 	       _bfd_error_handler
   4356  1.9  christos 		 /* xgettext:c-format */
   4357  1.9  christos 		 (_("%B: GNU_MBIN section `%A' has invalid sh_info field: %d"),
   4358  1.9  christos 		     abfd, s, elf_section_data (s)->this_hdr.sh_info);
   4359  1.9  christos 	       continue;
   4360  1.9  christos 	     }
   4361  1.9  christos 	   /* Align mbind section to page size.  */
   4362  1.9  christos 	   if (s->alignment_power < page_align_power)
   4363  1.9  christos 	     s->alignment_power = page_align_power;
   4364  1.9  christos 	   segs ++;
   4365  1.9  christos 	 }
   4366  1.9  christos    }
   4367  1.9  christos 
   4368  1.1  christos  /* Let the backend count up any program headers it might need.  */
   4369  1.1  christos  if (bed->elf_backend_additional_program_headers)
   4370  1.1  christos     {
   4371  1.1  christos       int a;
   4372  1.1  christos 
   4373  1.1  christos       a = (*bed->elf_backend_additional_program_headers) (abfd, info);
   4374  1.1  christos       if (a == -1)
   4375  1.1  christos 	abort ();
   4376  1.1  christos       segs += a;
   4377  1.1  christos     }
   4378  1.1  christos 
   4379  1.1  christos   return segs * bed->s->sizeof_phdr;
   4380  1.1  christos }
   4381  1.1  christos 
   4382  1.1  christos /* Find the segment that contains the output_section of section.  */
   4383  1.1  christos 
   4384  1.1  christos Elf_Internal_Phdr *
   4385  1.1  christos _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
   4386  1.1  christos {
   4387  1.1  christos   struct elf_segment_map *m;
   4388  1.3  christos   Elf_Internal_Phdr *p;
   4389  1.1  christos 
   4390  1.1  christos   for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
   4391  1.1  christos        m != NULL;
   4392  1.1  christos        m = m->next, p++)
   4393  1.1  christos     {
   4394  1.1  christos       int i;
   4395  1.1  christos 
   4396  1.1  christos       for (i = m->count - 1; i >= 0; i--)
   4397  1.1  christos 	if (m->sections[i] == section)
   4398  1.1  christos 	  return p;
   4399  1.1  christos     }
   4400  1.1  christos 
   4401  1.1  christos   return NULL;
   4402  1.1  christos }
   4403  1.1  christos 
   4404  1.1  christos /* Create a mapping from a set of sections to a program segment.  */
   4405  1.1  christos 
   4406  1.1  christos static struct elf_segment_map *
   4407  1.1  christos make_mapping (bfd *abfd,
   4408  1.1  christos 	      asection **sections,
   4409  1.1  christos 	      unsigned int from,
   4410  1.1  christos 	      unsigned int to,
   4411  1.1  christos 	      bfd_boolean phdr)
   4412  1.1  christos {
   4413  1.1  christos   struct elf_segment_map *m;
   4414  1.1  christos   unsigned int i;
   4415  1.1  christos   asection **hdrpp;
   4416  1.1  christos   bfd_size_type amt;
   4417  1.1  christos 
   4418  1.1  christos   amt = sizeof (struct elf_segment_map);
   4419  1.1  christos   amt += (to - from - 1) * sizeof (asection *);
   4420  1.1  christos   m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4421  1.1  christos   if (m == NULL)
   4422  1.1  christos     return NULL;
   4423  1.1  christos   m->next = NULL;
   4424  1.1  christos   m->p_type = PT_LOAD;
   4425  1.1  christos   for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
   4426  1.1  christos     m->sections[i - from] = *hdrpp;
   4427  1.1  christos   m->count = to - from;
   4428  1.1  christos 
   4429  1.1  christos   if (from == 0 && phdr)
   4430  1.1  christos     {
   4431  1.1  christos       /* Include the headers in the first PT_LOAD segment.  */
   4432  1.1  christos       m->includes_filehdr = 1;
   4433  1.1  christos       m->includes_phdrs = 1;
   4434  1.1  christos     }
   4435  1.1  christos 
   4436  1.1  christos   return m;
   4437  1.1  christos }
   4438  1.1  christos 
   4439  1.1  christos /* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
   4440  1.1  christos    on failure.  */
   4441  1.1  christos 
   4442  1.1  christos struct elf_segment_map *
   4443  1.1  christos _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
   4444  1.1  christos {
   4445  1.1  christos   struct elf_segment_map *m;
   4446  1.1  christos 
   4447  1.1  christos   m = (struct elf_segment_map *) bfd_zalloc (abfd,
   4448  1.1  christos                                              sizeof (struct elf_segment_map));
   4449  1.1  christos   if (m == NULL)
   4450  1.1  christos     return NULL;
   4451  1.1  christos   m->next = NULL;
   4452  1.1  christos   m->p_type = PT_DYNAMIC;
   4453  1.1  christos   m->count = 1;
   4454  1.1  christos   m->sections[0] = dynsec;
   4455  1.1  christos 
   4456  1.1  christos   return m;
   4457  1.1  christos }
   4458  1.1  christos 
   4459  1.1  christos /* Possibly add or remove segments from the segment map.  */
   4460  1.1  christos 
   4461  1.1  christos static bfd_boolean
   4462  1.1  christos elf_modify_segment_map (bfd *abfd,
   4463  1.1  christos 			struct bfd_link_info *info,
   4464  1.1  christos 			bfd_boolean remove_empty_load)
   4465  1.1  christos {
   4466  1.1  christos   struct elf_segment_map **m;
   4467  1.1  christos   const struct elf_backend_data *bed;
   4468  1.1  christos 
   4469  1.1  christos   /* The placement algorithm assumes that non allocated sections are
   4470  1.1  christos      not in PT_LOAD segments.  We ensure this here by removing such
   4471  1.1  christos      sections from the segment map.  We also remove excluded
   4472  1.3  christos      sections.  Finally, any PT_LOAD segment without sections is
   4473  1.1  christos      removed.  */
   4474  1.1  christos   m = &elf_seg_map (abfd);
   4475  1.1  christos   while (*m)
   4476  1.1  christos     {
   4477  1.1  christos       unsigned int i, new_count;
   4478  1.1  christos 
   4479  1.1  christos       for (new_count = 0, i = 0; i < (*m)->count; i++)
   4480  1.1  christos 	{
   4481  1.1  christos 	  if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
   4482  1.1  christos 	      && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
   4483  1.1  christos 		  || (*m)->p_type != PT_LOAD))
   4484  1.1  christos 	    {
   4485  1.1  christos 	      (*m)->sections[new_count] = (*m)->sections[i];
   4486  1.1  christos 	      new_count++;
   4487  1.1  christos 	    }
   4488  1.1  christos 	}
   4489  1.9  christos       (*m)->count = new_count;
   4490  1.9  christos 
   4491  1.9  christos       if (remove_empty_load
   4492  1.9  christos 	  && (*m)->p_type == PT_LOAD
   4493  1.1  christos 	  && (*m)->count == 0
   4494  1.1  christos 	  && !(*m)->includes_phdrs)
   4495  1.1  christos 	*m = (*m)->next;
   4496  1.1  christos       else
   4497  1.1  christos 	m = &(*m)->next;
   4498  1.1  christos     }
   4499  1.1  christos 
   4500  1.1  christos   bed = get_elf_backend_data (abfd);
   4501  1.1  christos   if (bed->elf_backend_modify_segment_map != NULL)
   4502  1.1  christos     {
   4503  1.1  christos       if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
   4504  1.1  christos 	return FALSE;
   4505  1.1  christos     }
   4506  1.1  christos 
   4507  1.1  christos   return TRUE;
   4508  1.1  christos }
   4509  1.1  christos 
   4510  1.1  christos /* Set up a mapping from BFD sections to program segments.  */
   4511  1.1  christos 
   4512  1.1  christos bfd_boolean
   4513  1.1  christos _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
   4514  1.1  christos {
   4515  1.1  christos   unsigned int count;
   4516  1.1  christos   struct elf_segment_map *m;
   4517  1.1  christos   asection **sections = NULL;
   4518  1.1  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4519  1.3  christos   bfd_boolean no_user_phdrs;
   4520  1.3  christos 
   4521  1.3  christos   no_user_phdrs = elf_seg_map (abfd) == NULL;
   4522  1.3  christos 
   4523  1.3  christos   if (info != NULL)
   4524  1.1  christos     info->user_phdrs = !no_user_phdrs;
   4525  1.1  christos 
   4526  1.1  christos   if (no_user_phdrs && bfd_count_sections (abfd) != 0)
   4527  1.1  christos     {
   4528  1.1  christos       asection *s;
   4529  1.1  christos       unsigned int i;
   4530  1.1  christos       struct elf_segment_map *mfirst;
   4531  1.1  christos       struct elf_segment_map **pm;
   4532  1.1  christos       asection *last_hdr;
   4533  1.1  christos       bfd_vma last_size;
   4534  1.1  christos       unsigned int phdr_index;
   4535  1.1  christos       bfd_vma maxpagesize;
   4536  1.1  christos       asection **hdrpp;
   4537  1.1  christos       bfd_boolean phdr_in_segment = TRUE;
   4538  1.1  christos       bfd_boolean writable;
   4539  1.9  christos       int tls_count = 0;
   4540  1.1  christos       asection *first_tls = NULL;
   4541  1.1  christos       asection *first_mbind = NULL;
   4542  1.1  christos       asection *dynsec, *eh_frame_hdr;
   4543  1.9  christos       bfd_size_type amt;
   4544  1.1  christos       bfd_vma addr_mask, wrap_to = 0;
   4545  1.1  christos       bfd_boolean linker_created_pt_phdr_segment = FALSE;
   4546  1.1  christos 
   4547  1.1  christos       /* Select the allocated sections, and sort them.  */
   4548  1.1  christos 
   4549  1.1  christos       sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
   4550  1.1  christos                                             sizeof (asection *));
   4551  1.1  christos       if (sections == NULL)
   4552  1.1  christos 	goto error_return;
   4553  1.1  christos 
   4554  1.1  christos       /* Calculate top address, avoiding undefined behaviour of shift
   4555  1.1  christos 	 left operator when shift count is equal to size of type
   4556  1.1  christos 	 being shifted.  */
   4557  1.1  christos       addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
   4558  1.1  christos       addr_mask = (addr_mask << 1) + 1;
   4559  1.1  christos 
   4560  1.1  christos       i = 0;
   4561  1.1  christos       for (s = abfd->sections; s != NULL; s = s->next)
   4562  1.1  christos 	{
   4563  1.1  christos 	  if ((s->flags & SEC_ALLOC) != 0)
   4564  1.1  christos 	    {
   4565  1.1  christos 	      sections[i] = s;
   4566  1.1  christos 	      ++i;
   4567  1.1  christos 	      /* A wrapping section potentially clashes with header.  */
   4568  1.1  christos 	      if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
   4569  1.1  christos 		wrap_to = (s->lma + s->size) & addr_mask;
   4570  1.1  christos 	    }
   4571  1.1  christos 	}
   4572  1.1  christos       BFD_ASSERT (i <= bfd_count_sections (abfd));
   4573  1.1  christos       count = i;
   4574  1.1  christos 
   4575  1.1  christos       qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
   4576  1.1  christos 
   4577  1.1  christos       /* Build the mapping.  */
   4578  1.1  christos 
   4579  1.1  christos       mfirst = NULL;
   4580  1.1  christos       pm = &mfirst;
   4581  1.1  christos 
   4582  1.1  christos       /* If we have a .interp section, then create a PT_PHDR segment for
   4583  1.1  christos 	 the program headers and a PT_INTERP segment for the .interp
   4584  1.1  christos 	 section.  */
   4585  1.1  christos       s = bfd_get_section_by_name (abfd, ".interp");
   4586  1.1  christos       if (s != NULL && (s->flags & SEC_LOAD) != 0)
   4587  1.1  christos 	{
   4588  1.1  christos 	  amt = sizeof (struct elf_segment_map);
   4589  1.1  christos 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4590  1.1  christos 	  if (m == NULL)
   4591  1.1  christos 	    goto error_return;
   4592  1.1  christos 	  m->next = NULL;
   4593  1.1  christos 	  m->p_type = PT_PHDR;
   4594  1.1  christos 	  /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not.  */
   4595  1.1  christos 	  m->p_flags = PF_R | PF_X;
   4596  1.9  christos 	  m->p_flags_valid = 1;
   4597  1.1  christos 	  m->includes_phdrs = 1;
   4598  1.1  christos 	  linker_created_pt_phdr_segment = TRUE;
   4599  1.1  christos 	  *pm = m;
   4600  1.1  christos 	  pm = &m->next;
   4601  1.1  christos 
   4602  1.1  christos 	  amt = sizeof (struct elf_segment_map);
   4603  1.1  christos 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4604  1.1  christos 	  if (m == NULL)
   4605  1.1  christos 	    goto error_return;
   4606  1.1  christos 	  m->next = NULL;
   4607  1.1  christos 	  m->p_type = PT_INTERP;
   4608  1.1  christos 	  m->count = 1;
   4609  1.1  christos 	  m->sections[0] = s;
   4610  1.1  christos 
   4611  1.1  christos 	  *pm = m;
   4612  1.1  christos 	  pm = &m->next;
   4613  1.1  christos 	}
   4614  1.1  christos 
   4615  1.1  christos       /* Look through the sections.  We put sections in the same program
   4616  1.1  christos 	 segment when the start of the second section can be placed within
   4617  1.1  christos 	 a few bytes of the end of the first section.  */
   4618  1.1  christos       last_hdr = NULL;
   4619  1.1  christos       last_size = 0;
   4620  1.5  christos       phdr_index = 0;
   4621  1.5  christos       maxpagesize = bed->maxpagesize;
   4622  1.5  christos       /* PR 17512: file: c8455299.
   4623  1.5  christos 	 Avoid divide-by-zero errors later on.
   4624  1.5  christos 	 FIXME: Should we abort if the maxpagesize is zero ?  */
   4625  1.1  christos       if (maxpagesize == 0)
   4626  1.1  christos 	maxpagesize = 1;
   4627  1.1  christos       writable = FALSE;
   4628  1.1  christos       dynsec = bfd_get_section_by_name (abfd, ".dynamic");
   4629  1.1  christos       if (dynsec != NULL
   4630  1.1  christos 	  && (dynsec->flags & SEC_LOAD) == 0)
   4631  1.1  christos 	dynsec = NULL;
   4632  1.1  christos 
   4633  1.1  christos       /* Deal with -Ttext or something similar such that the first section
   4634  1.1  christos 	 is not adjacent to the program headers.  This is an
   4635  1.1  christos 	 approximation, since at this point we don't know exactly how many
   4636  1.1  christos 	 program headers we will need.  */
   4637  1.3  christos       if (count > 0)
   4638  1.1  christos 	{
   4639  1.1  christos 	  bfd_size_type phdr_size = elf_program_header_size (abfd);
   4640  1.1  christos 
   4641  1.3  christos 	  if (phdr_size == (bfd_size_type) -1)
   4642  1.1  christos 	    phdr_size = get_program_header_size (abfd, info);
   4643  1.1  christos 	  phdr_size += bed->s->sizeof_ehdr;
   4644  1.1  christos 	  if ((abfd->flags & D_PAGED) == 0
   4645  1.1  christos 	      || (sections[0]->lma & addr_mask) < phdr_size
   4646  1.1  christos 	      || ((sections[0]->lma & addr_mask) % maxpagesize
   4647  1.9  christos 		  < phdr_size % maxpagesize)
   4648  1.9  christos 	      || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to)
   4649  1.9  christos 	    {
   4650  1.9  christos 	      /* PR 20815: The ELF standard says that a PT_PHDR segment, if
   4651  1.9  christos 		 present, must be included as part of the memory image of the
   4652  1.9  christos 		 program.  Ie it must be part of a PT_LOAD segment as well.
   4653  1.9  christos 		 If we have had to create our own PT_PHDR segment, but it is
   4654  1.9  christos 		 not going to be covered by the first PT_LOAD segment, then
   4655  1.9  christos 		 force the inclusion if we can...  */
   4656  1.9  christos 	      if ((abfd->flags & D_PAGED) != 0
   4657  1.9  christos 		  && linker_created_pt_phdr_segment)
   4658  1.9  christos 		phdr_in_segment = TRUE;
   4659  1.9  christos 	      else
   4660  1.1  christos 		phdr_in_segment = FALSE;
   4661  1.1  christos 	    }
   4662  1.1  christos 	}
   4663  1.1  christos 
   4664  1.1  christos       for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
   4665  1.1  christos 	{
   4666  1.1  christos 	  asection *hdr;
   4667  1.1  christos 	  bfd_boolean new_segment;
   4668  1.1  christos 
   4669  1.1  christos 	  hdr = *hdrpp;
   4670  1.1  christos 
   4671  1.1  christos 	  /* See if this section and the last one will fit in the same
   4672  1.1  christos 	     segment.  */
   4673  1.1  christos 
   4674  1.1  christos 	  if (last_hdr == NULL)
   4675  1.1  christos 	    {
   4676  1.1  christos 	      /* If we don't have a segment yet, then we don't need a new
   4677  1.1  christos 		 one (we build the last one after this loop).  */
   4678  1.1  christos 	      new_segment = FALSE;
   4679  1.1  christos 	    }
   4680  1.1  christos 	  else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
   4681  1.1  christos 	    {
   4682  1.1  christos 	      /* If this section has a different relation between the
   4683  1.1  christos 		 virtual address and the load address, then we need a new
   4684  1.1  christos 		 segment.  */
   4685  1.1  christos 	      new_segment = TRUE;
   4686  1.1  christos 	    }
   4687  1.1  christos 	  else if (hdr->lma < last_hdr->lma + last_size
   4688  1.1  christos 		   || last_hdr->lma + last_size < last_hdr->lma)
   4689  1.1  christos 	    {
   4690  1.1  christos 	      /* If this section has a load address that makes it overlap
   4691  1.1  christos 		 the previous section, then we need a new segment.  */
   4692  1.1  christos 	      new_segment = TRUE;
   4693  1.1  christos 	    }
   4694  1.1  christos 	  /* In the next test we have to be careful when last_hdr->lma is close
   4695  1.1  christos 	     to the end of the address space.  If the aligned address wraps
   4696  1.1  christos 	     around to the start of the address space, then there are no more
   4697  1.1  christos 	     pages left in memory and it is OK to assume that the current
   4698  1.1  christos 	     section can be included in the current segment.  */
   4699  1.1  christos 	  else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
   4700  1.1  christos 		    > last_hdr->lma)
   4701  1.1  christos 		   && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
   4702  1.1  christos 		       <= hdr->lma))
   4703  1.1  christos 	    {
   4704  1.1  christos 	      /* If putting this section in this segment would force us to
   4705  1.1  christos 		 skip a page in the segment, then we need a new segment.  */
   4706  1.1  christos 	      new_segment = TRUE;
   4707  1.8  christos 	    }
   4708  1.8  christos 	  else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
   4709  1.8  christos 		   && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0
   4710  1.8  christos 		   && ((abfd->flags & D_PAGED) == 0
   4711  1.8  christos 		       || (((last_hdr->lma + last_size - 1) & -maxpagesize)
   4712  1.8  christos 			   != (hdr->lma & -maxpagesize))))
   4713  1.8  christos 	    {
   4714  1.8  christos 	      /* We don't want to put a loaded section after a
   4715  1.8  christos 		 nonloaded (ie. bss style) section in the same segment
   4716  1.8  christos 		 as that will force the non-loaded section to be loaded.
   4717  1.8  christos 		 Consider .tbss sections as loaded for this purpose.
   4718  1.8  christos 		 However, like the writable/non-writable case below,
   4719  1.1  christos 		 if they are on the same page then they must be put
   4720  1.1  christos 		 in the same segment.  */
   4721  1.1  christos 	      new_segment = TRUE;
   4722  1.1  christos 	    }
   4723  1.1  christos 	  else if ((abfd->flags & D_PAGED) == 0)
   4724  1.1  christos 	    {
   4725  1.1  christos 	      /* If the file is not demand paged, which means that we
   4726  1.1  christos 		 don't require the sections to be correctly aligned in the
   4727  1.1  christos 		 file, then there is no other reason for a new segment.  */
   4728  1.1  christos 	      new_segment = FALSE;
   4729  1.1  christos 	    }
   4730  1.1  christos 	  else if (! writable
   4731  1.1  christos 		   && (hdr->flags & SEC_READONLY) == 0
   4732  1.1  christos 		   && (((last_hdr->lma + last_size - 1) & -maxpagesize)
   4733  1.1  christos 		       != (hdr->lma & -maxpagesize)))
   4734  1.1  christos 	    {
   4735  1.1  christos 	      /* We don't want to put a writable section in a read only
   4736  1.1  christos 		 segment, unless they are on the same page in memory
   4737  1.1  christos 		 anyhow.  We already know that the last section does not
   4738  1.1  christos 		 bring us past the current section on the page, so the
   4739  1.1  christos 		 only case in which the new section is not on the same
   4740  1.1  christos 		 page as the previous section is when the previous section
   4741  1.1  christos 		 ends precisely on a page boundary.  */
   4742  1.1  christos 	      new_segment = TRUE;
   4743  1.1  christos 	    }
   4744  1.1  christos 	  else
   4745  1.1  christos 	    {
   4746  1.1  christos 	      /* Otherwise, we can use the same segment.  */
   4747  1.1  christos 	      new_segment = FALSE;
   4748  1.1  christos 	    }
   4749  1.1  christos 
   4750  1.1  christos 	  /* Allow interested parties a chance to override our decision.  */
   4751  1.1  christos 	  if (last_hdr != NULL
   4752  1.1  christos 	      && info != NULL
   4753  1.1  christos 	      && info->callbacks->override_segment_assignment != NULL)
   4754  1.1  christos 	    new_segment
   4755  1.1  christos 	      = info->callbacks->override_segment_assignment (info, abfd, hdr,
   4756  1.1  christos 							      last_hdr,
   4757  1.1  christos 							      new_segment);
   4758  1.1  christos 
   4759  1.1  christos 	  if (! new_segment)
   4760  1.1  christos 	    {
   4761  1.1  christos 	      if ((hdr->flags & SEC_READONLY) == 0)
   4762  1.1  christos 		writable = TRUE;
   4763  1.1  christos 	      last_hdr = hdr;
   4764  1.1  christos 	      /* .tbss sections effectively have zero size.  */
   4765  1.1  christos 	      if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
   4766  1.1  christos 		  != SEC_THREAD_LOCAL)
   4767  1.1  christos 		last_size = hdr->size;
   4768  1.1  christos 	      else
   4769  1.1  christos 		last_size = 0;
   4770  1.1  christos 	      continue;
   4771  1.1  christos 	    }
   4772  1.1  christos 
   4773  1.1  christos 	  /* We need a new program segment.  We must create a new program
   4774  1.1  christos 	     header holding all the sections from phdr_index until hdr.  */
   4775  1.1  christos 
   4776  1.1  christos 	  m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
   4777  1.1  christos 	  if (m == NULL)
   4778  1.1  christos 	    goto error_return;
   4779  1.1  christos 
   4780  1.1  christos 	  *pm = m;
   4781  1.1  christos 	  pm = &m->next;
   4782  1.1  christos 
   4783  1.1  christos 	  if ((hdr->flags & SEC_READONLY) == 0)
   4784  1.1  christos 	    writable = TRUE;
   4785  1.1  christos 	  else
   4786  1.1  christos 	    writable = FALSE;
   4787  1.1  christos 
   4788  1.1  christos 	  last_hdr = hdr;
   4789  1.1  christos 	  /* .tbss sections effectively have zero size.  */
   4790  1.1  christos 	  if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
   4791  1.1  christos 	    last_size = hdr->size;
   4792  1.1  christos 	  else
   4793  1.1  christos 	    last_size = 0;
   4794  1.1  christos 	  phdr_index = i;
   4795  1.1  christos 	  phdr_in_segment = FALSE;
   4796  1.3  christos 	}
   4797  1.3  christos 
   4798  1.3  christos       /* Create a final PT_LOAD program segment, but not if it's just
   4799  1.3  christos 	 for .tbss.  */
   4800  1.3  christos       if (last_hdr != NULL
   4801  1.3  christos 	  && (i - phdr_index != 1
   4802  1.1  christos 	      || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
   4803  1.1  christos 		  != SEC_THREAD_LOCAL)))
   4804  1.1  christos 	{
   4805  1.1  christos 	  m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
   4806  1.1  christos 	  if (m == NULL)
   4807  1.1  christos 	    goto error_return;
   4808  1.1  christos 
   4809  1.1  christos 	  *pm = m;
   4810  1.1  christos 	  pm = &m->next;
   4811  1.1  christos 	}
   4812  1.1  christos 
   4813  1.1  christos       /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
   4814  1.1  christos       if (dynsec != NULL)
   4815  1.1  christos 	{
   4816  1.1  christos 	  m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
   4817  1.1  christos 	  if (m == NULL)
   4818  1.1  christos 	    goto error_return;
   4819  1.1  christos 	  *pm = m;
   4820  1.1  christos 	  pm = &m->next;
   4821  1.1  christos 	}
   4822  1.1  christos 
   4823  1.1  christos       /* For each batch of consecutive loadable .note sections,
   4824  1.1  christos 	 add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
   4825  1.1  christos 	 because if we link together nonloadable .note sections and
   4826  1.1  christos 	 loadable .note sections, we will generate two .note sections
   4827  1.1  christos 	 in the output file.  FIXME: Using names for section types is
   4828  1.1  christos 	 bogus anyhow.  */
   4829  1.1  christos       for (s = abfd->sections; s != NULL; s = s->next)
   4830  1.1  christos 	{
   4831  1.1  christos 	  if ((s->flags & SEC_LOAD) != 0
   4832  1.1  christos 	      && CONST_STRNEQ (s->name, ".note"))
   4833  1.1  christos 	    {
   4834  1.1  christos 	      asection *s2;
   4835  1.1  christos 
   4836  1.1  christos 	      count = 1;
   4837  1.1  christos 	      amt = sizeof (struct elf_segment_map);
   4838  1.1  christos 	      if (s->alignment_power == 2)
   4839  1.1  christos 		for (s2 = s; s2->next != NULL; s2 = s2->next)
   4840  1.1  christos 		  {
   4841  1.1  christos 		    if (s2->next->alignment_power == 2
   4842  1.1  christos 			&& (s2->next->flags & SEC_LOAD) != 0
   4843  1.1  christos 			&& CONST_STRNEQ (s2->next->name, ".note")
   4844  1.1  christos 			&& align_power (s2->lma + s2->size, 2)
   4845  1.1  christos 			   == s2->next->lma)
   4846  1.1  christos 		      count++;
   4847  1.1  christos 		    else
   4848  1.1  christos 		      break;
   4849  1.1  christos 		  }
   4850  1.1  christos 	      amt += (count - 1) * sizeof (asection *);
   4851  1.1  christos 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4852  1.1  christos 	      if (m == NULL)
   4853  1.1  christos 		goto error_return;
   4854  1.1  christos 	      m->next = NULL;
   4855  1.1  christos 	      m->p_type = PT_NOTE;
   4856  1.1  christos 	      m->count = count;
   4857  1.1  christos 	      while (count > 1)
   4858  1.1  christos 		{
   4859  1.1  christos 		  m->sections[m->count - count--] = s;
   4860  1.1  christos 		  BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   4861  1.1  christos 		  s = s->next;
   4862  1.1  christos 		}
   4863  1.1  christos 	      m->sections[m->count - 1] = s;
   4864  1.1  christos 	      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   4865  1.1  christos 	      *pm = m;
   4866  1.1  christos 	      pm = &m->next;
   4867  1.1  christos 	    }
   4868  1.1  christos 	  if (s->flags & SEC_THREAD_LOCAL)
   4869  1.1  christos 	    {
   4870  1.1  christos 	      if (! tls_count)
   4871  1.1  christos 		first_tls = s;
   4872  1.9  christos 	      tls_count++;
   4873  1.9  christos 	    }
   4874  1.9  christos 	  if (first_mbind == NULL
   4875  1.1  christos 	      && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
   4876  1.1  christos 	    first_mbind = s;
   4877  1.1  christos 	}
   4878  1.1  christos 
   4879  1.1  christos       /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
   4880  1.1  christos       if (tls_count > 0)
   4881  1.1  christos 	{
   4882  1.1  christos 	  amt = sizeof (struct elf_segment_map);
   4883  1.1  christos 	  amt += (tls_count - 1) * sizeof (asection *);
   4884  1.1  christos 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4885  1.1  christos 	  if (m == NULL)
   4886  1.1  christos 	    goto error_return;
   4887  1.1  christos 	  m->next = NULL;
   4888  1.1  christos 	  m->p_type = PT_TLS;
   4889  1.1  christos 	  m->count = tls_count;
   4890  1.1  christos 	  /* Mandated PF_R.  */
   4891  1.5  christos 	  m->p_flags = PF_R;
   4892  1.1  christos 	  m->p_flags_valid = 1;
   4893  1.1  christos 	  s = first_tls;
   4894  1.5  christos 	  for (i = 0; i < (unsigned int) tls_count; ++i)
   4895  1.5  christos 	    {
   4896  1.5  christos 	      if ((s->flags & SEC_THREAD_LOCAL) == 0)
   4897  1.5  christos 		{
   4898  1.5  christos 		  _bfd_error_handler
   4899  1.5  christos 		    (_("%B: TLS sections are not adjacent:"), abfd);
   4900  1.5  christos 		  s = first_tls;
   4901  1.5  christos 		  i = 0;
   4902  1.5  christos 		  while (i < (unsigned int) tls_count)
   4903  1.5  christos 		    {
   4904  1.5  christos 		      if ((s->flags & SEC_THREAD_LOCAL) != 0)
   4905  1.5  christos 			{
   4906  1.5  christos 			  _bfd_error_handler (_("	    TLS: %A"), s);
   4907  1.5  christos 			  i++;
   4908  1.5  christos 			}
   4909  1.5  christos 		      else
   4910  1.5  christos 			_bfd_error_handler (_("	non-TLS: %A"), s);
   4911  1.5  christos 		      s = s->next;
   4912  1.5  christos 		    }
   4913  1.5  christos 		  bfd_set_error (bfd_error_bad_value);
   4914  1.5  christos 		  goto error_return;
   4915  1.5  christos 		}
   4916  1.1  christos 	      m->sections[i] = s;
   4917  1.1  christos 	      s = s->next;
   4918  1.1  christos 	    }
   4919  1.1  christos 
   4920  1.1  christos 	  *pm = m;
   4921  1.1  christos 	  pm = &m->next;
   4922  1.9  christos 	}
   4923  1.9  christos 
   4924  1.9  christos       if (first_mbind && (abfd->flags & D_PAGED) != 0)
   4925  1.9  christos 	for (s = first_mbind; s != NULL; s = s->next)
   4926  1.9  christos 	  if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
   4927  1.9  christos 	      && (elf_section_data (s)->this_hdr.sh_info
   4928  1.9  christos 		  <= PT_GNU_MBIND_NUM))
   4929  1.9  christos 	    {
   4930  1.9  christos 	      /* Mandated PF_R.  */
   4931  1.9  christos 	      unsigned long p_flags = PF_R;
   4932  1.9  christos 	      if ((s->flags & SEC_READONLY) == 0)
   4933  1.9  christos 		p_flags |= PF_W;
   4934  1.9  christos 	      if ((s->flags & SEC_CODE) != 0)
   4935  1.9  christos 		p_flags |= PF_X;
   4936  1.9  christos 
   4937  1.9  christos 	      amt = sizeof (struct elf_segment_map) + sizeof (asection *);
   4938  1.9  christos 	      m = bfd_zalloc (abfd, amt);
   4939  1.9  christos 	      if (m == NULL)
   4940  1.9  christos 		goto error_return;
   4941  1.9  christos 	      m->next = NULL;
   4942  1.9  christos 	      m->p_type = (PT_GNU_MBIND_LO
   4943  1.9  christos 			   + elf_section_data (s)->this_hdr.sh_info);
   4944  1.9  christos 	      m->count = 1;
   4945  1.9  christos 	      m->p_flags_valid = 1;
   4946  1.9  christos 	      m->sections[0] = s;
   4947  1.9  christos 	      m->p_flags = p_flags;
   4948  1.9  christos 
   4949  1.9  christos 	      *pm = m;
   4950  1.9  christos 	      pm = &m->next;
   4951  1.1  christos 	    }
   4952  1.1  christos 
   4953  1.3  christos       /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
   4954  1.1  christos 	 segment.  */
   4955  1.1  christos       eh_frame_hdr = elf_eh_frame_hdr (abfd);
   4956  1.1  christos       if (eh_frame_hdr != NULL
   4957  1.1  christos 	  && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
   4958  1.1  christos 	{
   4959  1.1  christos 	  amt = sizeof (struct elf_segment_map);
   4960  1.1  christos 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4961  1.1  christos 	  if (m == NULL)
   4962  1.1  christos 	    goto error_return;
   4963  1.1  christos 	  m->next = NULL;
   4964  1.1  christos 	  m->p_type = PT_GNU_EH_FRAME;
   4965  1.1  christos 	  m->count = 1;
   4966  1.1  christos 	  m->sections[0] = eh_frame_hdr->output_section;
   4967  1.1  christos 
   4968  1.1  christos 	  *pm = m;
   4969  1.1  christos 	  pm = &m->next;
   4970  1.3  christos 	}
   4971  1.1  christos 
   4972  1.1  christos       if (elf_stack_flags (abfd))
   4973  1.1  christos 	{
   4974  1.1  christos 	  amt = sizeof (struct elf_segment_map);
   4975  1.1  christos 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4976  1.1  christos 	  if (m == NULL)
   4977  1.1  christos 	    goto error_return;
   4978  1.3  christos 	  m->next = NULL;
   4979  1.3  christos 	  m->p_type = PT_GNU_STACK;
   4980  1.1  christos 	  m->p_flags = elf_stack_flags (abfd);
   4981  1.3  christos 	  m->p_align = bed->stack_align;
   4982  1.3  christos 	  m->p_flags_valid = 1;
   4983  1.3  christos 	  m->p_align_valid = m->p_align != 0;
   4984  1.3  christos 	  if (info->stacksize > 0)
   4985  1.3  christos 	    {
   4986  1.3  christos 	      m->p_size = info->stacksize;
   4987  1.1  christos 	      m->p_size_valid = 1;
   4988  1.1  christos 	    }
   4989  1.1  christos 
   4990  1.1  christos 	  *pm = m;
   4991  1.1  christos 	  pm = &m->next;
   4992  1.1  christos 	}
   4993  1.1  christos 
   4994  1.1  christos       if (info != NULL && info->relro)
   4995  1.1  christos 	{
   4996  1.3  christos 	  for (m = mfirst; m != NULL; m = m->next)
   4997  1.3  christos 	    {
   4998  1.3  christos 	      if (m->p_type == PT_LOAD
   4999  1.3  christos 		  && m->count != 0
   5000  1.1  christos 		  && m->sections[0]->vma >= info->relro_start
   5001  1.3  christos 		  && m->sections[0]->vma < info->relro_end)
   5002  1.3  christos 		{
   5003  1.3  christos 		  i = m->count;
   5004  1.3  christos 		  while (--i != (unsigned) -1)
   5005  1.3  christos 		    if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
   5006  1.3  christos 			== (SEC_LOAD | SEC_HAS_CONTENTS))
   5007  1.5  christos 		      break;
   5008  1.1  christos 
   5009  1.1  christos 		  if (i != (unsigned) -1)
   5010  1.3  christos 		    break;
   5011  1.1  christos 		}
   5012  1.1  christos 	    }
   5013  1.1  christos 
   5014  1.1  christos 	  /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
   5015  1.1  christos 	  if (m != NULL)
   5016  1.1  christos 	    {
   5017  1.1  christos 	      amt = sizeof (struct elf_segment_map);
   5018  1.1  christos 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5019  1.1  christos 	      if (m == NULL)
   5020  1.1  christos 		goto error_return;
   5021  1.1  christos 	      m->next = NULL;
   5022  1.1  christos 	      m->p_type = PT_GNU_RELRO;
   5023  1.1  christos 	      *pm = m;
   5024  1.1  christos 	      pm = &m->next;
   5025  1.1  christos 	    }
   5026  1.1  christos 	}
   5027  1.3  christos 
   5028  1.1  christos       free (sections);
   5029  1.1  christos       elf_seg_map (abfd) = mfirst;
   5030  1.1  christos     }
   5031  1.1  christos 
   5032  1.1  christos   if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
   5033  1.3  christos     return FALSE;
   5034  1.1  christos 
   5035  1.3  christos   for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
   5036  1.1  christos     ++count;
   5037  1.1  christos   elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
   5038  1.1  christos 
   5039  1.1  christos   return TRUE;
   5040  1.1  christos 
   5041  1.1  christos  error_return:
   5042  1.1  christos   if (sections != NULL)
   5043  1.1  christos     free (sections);
   5044  1.1  christos   return FALSE;
   5045  1.1  christos }
   5046  1.1  christos 
   5047  1.1  christos /* Sort sections by address.  */
   5048  1.1  christos 
   5049  1.1  christos static int
   5050  1.1  christos elf_sort_sections (const void *arg1, const void *arg2)
   5051  1.1  christos {
   5052  1.1  christos   const asection *sec1 = *(const asection **) arg1;
   5053  1.1  christos   const asection *sec2 = *(const asection **) arg2;
   5054  1.1  christos   bfd_size_type size1, size2;
   5055  1.1  christos 
   5056  1.1  christos   /* Sort by LMA first, since this is the address used to
   5057  1.1  christos      place the section into a segment.  */
   5058  1.1  christos   if (sec1->lma < sec2->lma)
   5059  1.1  christos     return -1;
   5060  1.1  christos   else if (sec1->lma > sec2->lma)
   5061  1.1  christos     return 1;
   5062  1.1  christos 
   5063  1.1  christos   /* Then sort by VMA.  Normally the LMA and the VMA will be
   5064  1.1  christos      the same, and this will do nothing.  */
   5065  1.1  christos   if (sec1->vma < sec2->vma)
   5066  1.1  christos     return -1;
   5067  1.1  christos   else if (sec1->vma > sec2->vma)
   5068  1.1  christos     return 1;
   5069  1.1  christos 
   5070  1.1  christos   /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
   5071  1.1  christos 
   5072  1.1  christos #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
   5073  1.1  christos 
   5074  1.1  christos   if (TOEND (sec1))
   5075  1.1  christos     {
   5076  1.1  christos       if (TOEND (sec2))
   5077  1.1  christos 	{
   5078  1.1  christos 	  /* If the indicies are the same, do not return 0
   5079  1.1  christos 	     here, but continue to try the next comparison.  */
   5080  1.1  christos 	  if (sec1->target_index - sec2->target_index != 0)
   5081  1.1  christos 	    return sec1->target_index - sec2->target_index;
   5082  1.1  christos 	}
   5083  1.1  christos       else
   5084  1.1  christos 	return 1;
   5085  1.1  christos     }
   5086  1.1  christos   else if (TOEND (sec2))
   5087  1.1  christos     return -1;
   5088  1.1  christos 
   5089  1.1  christos #undef TOEND
   5090  1.1  christos 
   5091  1.1  christos   /* Sort by size, to put zero sized sections
   5092  1.1  christos      before others at the same address.  */
   5093  1.1  christos 
   5094  1.1  christos   size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
   5095  1.1  christos   size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
   5096  1.1  christos 
   5097  1.1  christos   if (size1 < size2)
   5098  1.1  christos     return -1;
   5099  1.1  christos   if (size1 > size2)
   5100  1.1  christos     return 1;
   5101  1.1  christos 
   5102  1.1  christos   return sec1->target_index - sec2->target_index;
   5103  1.1  christos }
   5104  1.1  christos 
   5105  1.1  christos /* Ian Lance Taylor writes:
   5106  1.1  christos 
   5107  1.1  christos    We shouldn't be using % with a negative signed number.  That's just
   5108  1.1  christos    not good.  We have to make sure either that the number is not
   5109  1.1  christos    negative, or that the number has an unsigned type.  When the types
   5110  1.1  christos    are all the same size they wind up as unsigned.  When file_ptr is a
   5111  1.1  christos    larger signed type, the arithmetic winds up as signed long long,
   5112  1.1  christos    which is wrong.
   5113  1.1  christos 
   5114  1.1  christos    What we're trying to say here is something like ``increase OFF by
   5115  1.1  christos    the least amount that will cause it to be equal to the VMA modulo
   5116  1.1  christos    the page size.''  */
   5117  1.1  christos /* In other words, something like:
   5118  1.1  christos 
   5119  1.1  christos    vma_offset = m->sections[0]->vma % bed->maxpagesize;
   5120  1.1  christos    off_offset = off % bed->maxpagesize;
   5121  1.1  christos    if (vma_offset < off_offset)
   5122  1.1  christos      adjustment = vma_offset + bed->maxpagesize - off_offset;
   5123  1.1  christos    else
   5124  1.1  christos      adjustment = vma_offset - off_offset;
   5125  1.1  christos 
   5126  1.1  christos    which can can be collapsed into the expression below.  */
   5127  1.1  christos 
   5128  1.1  christos static file_ptr
   5129  1.4  christos vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
   5130  1.4  christos {
   5131  1.4  christos   /* PR binutils/16199: Handle an alignment of zero.  */
   5132  1.1  christos   if (maxpagesize == 0)
   5133  1.1  christos     maxpagesize = 1;
   5134  1.1  christos   return ((vma - off) % maxpagesize);
   5135  1.1  christos }
   5136  1.1  christos 
   5137  1.1  christos static void
   5138  1.1  christos print_segment_map (const struct elf_segment_map *m)
   5139  1.1  christos {
   5140  1.1  christos   unsigned int j;
   5141  1.1  christos   const char *pt = get_segment_type (m->p_type);
   5142  1.1  christos   char buf[32];
   5143  1.1  christos 
   5144  1.1  christos   if (pt == NULL)
   5145  1.1  christos     {
   5146  1.1  christos       if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
   5147  1.1  christos 	sprintf (buf, "LOPROC+%7.7x",
   5148  1.1  christos 		 (unsigned int) (m->p_type - PT_LOPROC));
   5149  1.1  christos       else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
   5150  1.1  christos 	sprintf (buf, "LOOS+%7.7x",
   5151  1.1  christos 		 (unsigned int) (m->p_type - PT_LOOS));
   5152  1.1  christos       else
   5153  1.1  christos 	snprintf (buf, sizeof (buf), "%8.8x",
   5154  1.1  christos 		  (unsigned int) m->p_type);
   5155  1.1  christos       pt = buf;
   5156  1.1  christos     }
   5157  1.1  christos   fflush (stdout);
   5158  1.1  christos   fprintf (stderr, "%s:", pt);
   5159  1.1  christos   for (j = 0; j < m->count; j++)
   5160  1.1  christos     fprintf (stderr, " %s", m->sections [j]->name);
   5161  1.1  christos   putc ('\n',stderr);
   5162  1.1  christos   fflush (stderr);
   5163  1.1  christos }
   5164  1.1  christos 
   5165  1.1  christos static bfd_boolean
   5166  1.1  christos write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
   5167  1.1  christos {
   5168  1.1  christos   void *buf;
   5169  1.1  christos   bfd_boolean ret;
   5170  1.1  christos 
   5171  1.1  christos   if (bfd_seek (abfd, pos, SEEK_SET) != 0)
   5172  1.1  christos     return FALSE;
   5173  1.1  christos   buf = bfd_zmalloc (len);
   5174  1.1  christos   if (buf == NULL)
   5175  1.1  christos     return FALSE;
   5176  1.1  christos   ret = bfd_bwrite (buf, len, abfd) == len;
   5177  1.1  christos   free (buf);
   5178  1.1  christos   return ret;
   5179  1.1  christos }
   5180  1.1  christos 
   5181  1.1  christos /* Assign file positions to the sections based on the mapping from
   5182  1.1  christos    sections to segments.  This function also sets up some fields in
   5183  1.1  christos    the file header.  */
   5184  1.1  christos 
   5185  1.1  christos static bfd_boolean
   5186  1.1  christos assign_file_positions_for_load_sections (bfd *abfd,
   5187  1.1  christos 					 struct bfd_link_info *link_info)
   5188  1.1  christos {
   5189  1.1  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   5190  1.1  christos   struct elf_segment_map *m;
   5191  1.1  christos   Elf_Internal_Phdr *phdrs;
   5192  1.1  christos   Elf_Internal_Phdr *p;
   5193  1.9  christos   file_ptr off;
   5194  1.1  christos   bfd_size_type maxpagesize;
   5195  1.1  christos   unsigned int pt_load_count = 0;
   5196  1.1  christos   unsigned int alloc;
   5197  1.1  christos   unsigned int i, j;
   5198  1.1  christos   bfd_vma header_pad = 0;
   5199  1.1  christos 
   5200  1.1  christos   if (link_info == NULL
   5201  1.1  christos       && !_bfd_elf_map_sections_to_segments (abfd, link_info))
   5202  1.1  christos     return FALSE;
   5203  1.3  christos 
   5204  1.1  christos   alloc = 0;
   5205  1.1  christos   for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   5206  1.1  christos     {
   5207  1.1  christos       ++alloc;
   5208  1.1  christos       if (m->header_size)
   5209  1.1  christos 	header_pad = m->header_size;
   5210  1.1  christos     }
   5211  1.1  christos 
   5212  1.1  christos   if (alloc)
   5213  1.1  christos     {
   5214  1.1  christos       elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
   5215  1.1  christos       elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
   5216  1.1  christos     }
   5217  1.1  christos   else
   5218  1.1  christos     {
   5219  1.1  christos       /* PR binutils/12467.  */
   5220  1.1  christos       elf_elfheader (abfd)->e_phoff = 0;
   5221  1.3  christos       elf_elfheader (abfd)->e_phentsize = 0;
   5222  1.1  christos     }
   5223  1.1  christos 
   5224  1.3  christos   elf_elfheader (abfd)->e_phnum = alloc;
   5225  1.3  christos 
   5226  1.1  christos   if (elf_program_header_size (abfd) == (bfd_size_type) -1)
   5227  1.3  christos     elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
   5228  1.1  christos   else
   5229  1.1  christos     BFD_ASSERT (elf_program_header_size (abfd)
   5230  1.1  christos 		>= alloc * bed->s->sizeof_phdr);
   5231  1.1  christos 
   5232  1.3  christos   if (alloc == 0)
   5233  1.1  christos     {
   5234  1.1  christos       elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
   5235  1.1  christos       return TRUE;
   5236  1.3  christos     }
   5237  1.1  christos 
   5238  1.1  christos   /* We're writing the size in elf_program_header_size (abfd),
   5239  1.3  christos      see assign_file_positions_except_relocs, so make sure we have
   5240  1.3  christos      that amount allocated, with trailing space cleared.
   5241  1.1  christos      The variable alloc contains the computed need, while
   5242  1.1  christos      elf_program_header_size (abfd) contains the size used for the
   5243  1.1  christos      layout.
   5244  1.1  christos      See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
   5245  1.3  christos      where the layout is forced to according to a larger size in the
   5246  1.1  christos      last iterations for the testcase ld-elf/header.  */
   5247  1.1  christos   BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
   5248  1.1  christos 	      == 0);
   5249  1.3  christos   phdrs = (Elf_Internal_Phdr *)
   5250  1.1  christos      bfd_zalloc2 (abfd,
   5251  1.1  christos                   (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
   5252  1.1  christos                   sizeof (Elf_Internal_Phdr));
   5253  1.1  christos   elf_tdata (abfd)->phdr = phdrs;
   5254  1.1  christos   if (phdrs == NULL)
   5255  1.1  christos     return FALSE;
   5256  1.1  christos 
   5257  1.1  christos   maxpagesize = 1;
   5258  1.1  christos   if ((abfd->flags & D_PAGED) != 0)
   5259  1.1  christos     maxpagesize = bed->maxpagesize;
   5260  1.1  christos 
   5261  1.1  christos   off = bed->s->sizeof_ehdr;
   5262  1.1  christos   off += alloc * bed->s->sizeof_phdr;
   5263  1.1  christos   if (header_pad < (bfd_vma) off)
   5264  1.1  christos     header_pad = 0;
   5265  1.1  christos   else
   5266  1.1  christos     header_pad -= off;
   5267  1.3  christos   off += header_pad;
   5268  1.1  christos 
   5269  1.1  christos   for (m = elf_seg_map (abfd), p = phdrs, j = 0;
   5270  1.1  christos        m != NULL;
   5271  1.1  christos        m = m->next, p++, j++)
   5272  1.1  christos     {
   5273  1.1  christos       asection **secpp;
   5274  1.1  christos       bfd_vma off_adjust;
   5275  1.1  christos       bfd_boolean no_contents;
   5276  1.1  christos 
   5277  1.1  christos       /* If elf_segment_map is not from map_sections_to_segments, the
   5278  1.1  christos 	 sections may not be correctly ordered.  NOTE: sorting should
   5279  1.1  christos 	 not be done to the PT_NOTE section of a corefile, which may
   5280  1.1  christos 	 contain several pseudo-sections artificially created by bfd.
   5281  1.1  christos 	 Sorting these pseudo-sections breaks things badly.  */
   5282  1.1  christos       if (m->count > 1
   5283  1.1  christos 	  && !(elf_elfheader (abfd)->e_type == ET_CORE
   5284  1.1  christos 	       && m->p_type == PT_NOTE))
   5285  1.1  christos 	qsort (m->sections, (size_t) m->count, sizeof (asection *),
   5286  1.1  christos 	       elf_sort_sections);
   5287  1.1  christos 
   5288  1.1  christos       /* An ELF segment (described by Elf_Internal_Phdr) may contain a
   5289  1.1  christos 	 number of sections with contents contributing to both p_filesz
   5290  1.1  christos 	 and p_memsz, followed by a number of sections with no contents
   5291  1.1  christos 	 that just contribute to p_memsz.  In this loop, OFF tracks next
   5292  1.1  christos 	 available file offset for PT_LOAD and PT_NOTE segments.  */
   5293  1.1  christos       p->p_type = m->p_type;
   5294  1.1  christos       p->p_flags = m->p_flags;
   5295  1.1  christos 
   5296  1.1  christos       if (m->count == 0)
   5297  1.1  christos 	p->p_vaddr = 0;
   5298  1.1  christos       else
   5299  1.1  christos 	p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
   5300  1.1  christos 
   5301  1.1  christos       if (m->p_paddr_valid)
   5302  1.1  christos 	p->p_paddr = m->p_paddr;
   5303  1.1  christos       else if (m->count == 0)
   5304  1.1  christos 	p->p_paddr = 0;
   5305  1.1  christos       else
   5306  1.1  christos 	p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
   5307  1.1  christos 
   5308  1.1  christos       if (p->p_type == PT_LOAD
   5309  1.1  christos 	  && (abfd->flags & D_PAGED) != 0)
   5310  1.1  christos 	{
   5311  1.1  christos 	  /* p_align in demand paged PT_LOAD segments effectively stores
   5312  1.1  christos 	     the maximum page size.  When copying an executable with
   5313  1.1  christos 	     objcopy, we set m->p_align from the input file.  Use this
   5314  1.1  christos 	     value for maxpagesize rather than bed->maxpagesize, which
   5315  1.1  christos 	     may be different.  Note that we use maxpagesize for PT_TLS
   5316  1.1  christos 	     segment alignment later in this function, so we are relying
   5317  1.1  christos 	     on at least one PT_LOAD segment appearing before a PT_TLS
   5318  1.1  christos 	     segment.  */
   5319  1.1  christos 	  if (m->p_align_valid)
   5320  1.1  christos 	    maxpagesize = m->p_align;
   5321  1.9  christos 
   5322  1.1  christos 	  p->p_align = maxpagesize;
   5323  1.1  christos 	  pt_load_count += 1;
   5324  1.1  christos 	}
   5325  1.1  christos       else if (m->p_align_valid)
   5326  1.1  christos 	p->p_align = m->p_align;
   5327  1.1  christos       else if (m->count == 0)
   5328  1.1  christos 	p->p_align = 1 << bed->s->log_file_align;
   5329  1.1  christos       else
   5330  1.1  christos 	p->p_align = 0;
   5331  1.1  christos 
   5332  1.1  christos       no_contents = FALSE;
   5333  1.1  christos       off_adjust = 0;
   5334  1.1  christos       if (p->p_type == PT_LOAD
   5335  1.1  christos 	  && m->count > 0)
   5336  1.1  christos 	{
   5337  1.1  christos 	  bfd_size_type align;
   5338  1.1  christos 	  unsigned int align_power = 0;
   5339  1.1  christos 
   5340  1.1  christos 	  if (m->p_align_valid)
   5341  1.1  christos 	    align = p->p_align;
   5342  1.1  christos 	  else
   5343  1.1  christos 	    {
   5344  1.1  christos 	      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   5345  1.1  christos 		{
   5346  1.1  christos 		  unsigned int secalign;
   5347  1.1  christos 
   5348  1.1  christos 		  secalign = bfd_get_section_alignment (abfd, *secpp);
   5349  1.1  christos 		  if (secalign > align_power)
   5350  1.1  christos 		    align_power = secalign;
   5351  1.1  christos 		}
   5352  1.1  christos 	      align = (bfd_size_type) 1 << align_power;
   5353  1.1  christos 	      if (align < maxpagesize)
   5354  1.1  christos 		align = maxpagesize;
   5355  1.1  christos 	    }
   5356  1.1  christos 
   5357  1.1  christos 	  for (i = 0; i < m->count; i++)
   5358  1.1  christos 	    if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   5359  1.1  christos 	      /* If we aren't making room for this section, then
   5360  1.1  christos 		 it must be SHT_NOBITS regardless of what we've
   5361  1.1  christos 		 set via struct bfd_elf_special_section.  */
   5362  1.1  christos 	      elf_section_type (m->sections[i]) = SHT_NOBITS;
   5363  1.1  christos 
   5364  1.1  christos 	  /* Find out whether this segment contains any loadable
   5365  1.1  christos 	     sections.  */
   5366  1.1  christos 	  no_contents = TRUE;
   5367  1.1  christos 	  for (i = 0; i < m->count; i++)
   5368  1.1  christos 	    if (elf_section_type (m->sections[i]) != SHT_NOBITS)
   5369  1.1  christos 	      {
   5370  1.1  christos 		no_contents = FALSE;
   5371  1.1  christos 		break;
   5372  1.1  christos 	      }
   5373  1.9  christos 
   5374  1.9  christos 	  off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
   5375  1.9  christos 
   5376  1.9  christos 	  /* Broken hardware and/or kernel require that files do not
   5377  1.9  christos 	     map the same page with different permissions on some hppa
   5378  1.9  christos 	     processors.  */
   5379  1.9  christos 	  if (pt_load_count > 1
   5380  1.9  christos 	      && bed->no_page_alias
   5381  1.9  christos 	      && (off & (maxpagesize - 1)) != 0
   5382  1.1  christos 	      && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize))
   5383  1.1  christos 	    off_adjust += maxpagesize;
   5384  1.1  christos 	  off += off_adjust;
   5385  1.1  christos 	  if (no_contents)
   5386  1.1  christos 	    {
   5387  1.1  christos 	      /* We shouldn't need to align the segment on disk since
   5388  1.1  christos 		 the segment doesn't need file space, but the gABI
   5389  1.1  christos 		 arguably requires the alignment and glibc ld.so
   5390  1.1  christos 		 checks it.  So to comply with the alignment
   5391  1.1  christos 		 requirement but not waste file space, we adjust
   5392  1.1  christos 		 p_offset for just this segment.  (OFF_ADJUST is
   5393  1.1  christos 		 subtracted from OFF later.)  This may put p_offset
   5394  1.1  christos 		 past the end of file, but that shouldn't matter.  */
   5395  1.1  christos 	    }
   5396  1.1  christos 	  else
   5397  1.1  christos 	    off_adjust = 0;
   5398  1.1  christos 	}
   5399  1.1  christos       /* Make sure the .dynamic section is the first section in the
   5400  1.1  christos 	 PT_DYNAMIC segment.  */
   5401  1.1  christos       else if (p->p_type == PT_DYNAMIC
   5402  1.1  christos 	       && m->count > 1
   5403  1.1  christos 	       && strcmp (m->sections[0]->name, ".dynamic") != 0)
   5404  1.9  christos 	{
   5405  1.9  christos 	  _bfd_error_handler
   5406  1.1  christos 	    (_("%B: The first section in the PT_DYNAMIC segment"
   5407  1.1  christos 	       " is not the .dynamic section"),
   5408  1.1  christos 	     abfd);
   5409  1.1  christos 	  bfd_set_error (bfd_error_bad_value);
   5410  1.1  christos 	  return FALSE;
   5411  1.1  christos 	}
   5412  1.1  christos       /* Set the note section type to SHT_NOTE.  */
   5413  1.1  christos       else if (p->p_type == PT_NOTE)
   5414  1.1  christos 	for (i = 0; i < m->count; i++)
   5415  1.1  christos 	  elf_section_type (m->sections[i]) = SHT_NOTE;
   5416  1.1  christos 
   5417  1.1  christos       p->p_offset = 0;
   5418  1.1  christos       p->p_filesz = 0;
   5419  1.1  christos       p->p_memsz = 0;
   5420  1.1  christos 
   5421  1.1  christos       if (m->includes_filehdr)
   5422  1.1  christos 	{
   5423  1.1  christos 	  if (!m->p_flags_valid)
   5424  1.1  christos 	    p->p_flags |= PF_R;
   5425  1.1  christos 	  p->p_filesz = bed->s->sizeof_ehdr;
   5426  1.1  christos 	  p->p_memsz = bed->s->sizeof_ehdr;
   5427  1.8  christos 	  if (m->count > 0)
   5428  1.8  christos 	    {
   5429  1.8  christos 	      if (p->p_vaddr < (bfd_vma) off
   5430  1.1  christos 		  || (!m->p_paddr_valid
   5431  1.9  christos 		      && p->p_paddr < (bfd_vma) off))
   5432  1.9  christos 		{
   5433  1.9  christos 		  _bfd_error_handler
   5434  1.1  christos 		    (_("%B: Not enough room for program headers,"
   5435  1.1  christos 		       " try linking with -N"),
   5436  1.1  christos 		     abfd);
   5437  1.1  christos 		  bfd_set_error (bfd_error_bad_value);
   5438  1.1  christos 		  return FALSE;
   5439  1.1  christos 		}
   5440  1.1  christos 
   5441  1.1  christos 	      p->p_vaddr -= off;
   5442  1.1  christos 	      if (!m->p_paddr_valid)
   5443  1.1  christos 		p->p_paddr -= off;
   5444  1.1  christos 	    }
   5445  1.1  christos 	}
   5446  1.1  christos 
   5447  1.1  christos       if (m->includes_phdrs)
   5448  1.1  christos 	{
   5449  1.1  christos 	  if (!m->p_flags_valid)
   5450  1.1  christos 	    p->p_flags |= PF_R;
   5451  1.1  christos 
   5452  1.1  christos 	  if (!m->includes_filehdr)
   5453  1.1  christos 	    {
   5454  1.1  christos 	      p->p_offset = bed->s->sizeof_ehdr;
   5455  1.1  christos 
   5456  1.1  christos 	      if (m->count > 0)
   5457  1.1  christos 		{
   5458  1.1  christos 		  p->p_vaddr -= off - p->p_offset;
   5459  1.1  christos 		  if (!m->p_paddr_valid)
   5460  1.1  christos 		    p->p_paddr -= off - p->p_offset;
   5461  1.1  christos 		}
   5462  1.1  christos 	    }
   5463  1.1  christos 
   5464  1.1  christos 	  p->p_filesz += alloc * bed->s->sizeof_phdr;
   5465  1.1  christos 	  p->p_memsz += alloc * bed->s->sizeof_phdr;
   5466  1.1  christos 	  if (m->count)
   5467  1.1  christos 	    {
   5468  1.1  christos 	      p->p_filesz += header_pad;
   5469  1.1  christos 	      p->p_memsz += header_pad;
   5470  1.1  christos 	    }
   5471  1.1  christos 	}
   5472  1.1  christos 
   5473  1.1  christos       if (p->p_type == PT_LOAD
   5474  1.1  christos 	  || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
   5475  1.1  christos 	{
   5476  1.1  christos 	  if (!m->includes_filehdr && !m->includes_phdrs)
   5477  1.1  christos 	    p->p_offset = off;
   5478  1.1  christos 	  else
   5479  1.1  christos 	    {
   5480  1.1  christos 	      file_ptr adjust;
   5481  1.1  christos 
   5482  1.1  christos 	      adjust = off - (p->p_offset + p->p_filesz);
   5483  1.1  christos 	      if (!no_contents)
   5484  1.1  christos 		p->p_filesz += adjust;
   5485  1.1  christos 	      p->p_memsz += adjust;
   5486  1.1  christos 	    }
   5487  1.1  christos 	}
   5488  1.1  christos 
   5489  1.1  christos       /* Set up p_filesz, p_memsz, p_align and p_flags from the section
   5490  1.1  christos 	 maps.  Set filepos for sections in PT_LOAD segments, and in
   5491  1.1  christos 	 core files, for sections in PT_NOTE segments.
   5492  1.1  christos 	 assign_file_positions_for_non_load_sections will set filepos
   5493  1.1  christos 	 for other sections and update p_filesz for other segments.  */
   5494  1.1  christos       for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   5495  1.1  christos 	{
   5496  1.1  christos 	  asection *sec;
   5497  1.1  christos 	  bfd_size_type align;
   5498  1.1  christos 	  Elf_Internal_Shdr *this_hdr;
   5499  1.1  christos 
   5500  1.1  christos 	  sec = *secpp;
   5501  1.1  christos 	  this_hdr = &elf_section_data (sec)->this_hdr;
   5502  1.1  christos 	  align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
   5503  1.1  christos 
   5504  1.1  christos 	  if ((p->p_type == PT_LOAD
   5505  1.1  christos 	       || p->p_type == PT_TLS)
   5506  1.1  christos 	      && (this_hdr->sh_type != SHT_NOBITS
   5507  1.1  christos 		  || ((this_hdr->sh_flags & SHF_ALLOC) != 0
   5508  1.1  christos 		      && ((this_hdr->sh_flags & SHF_TLS) == 0
   5509  1.1  christos 			  || p->p_type == PT_TLS))))
   5510  1.1  christos 	    {
   5511  1.1  christos 	      bfd_vma p_start = p->p_paddr;
   5512  1.1  christos 	      bfd_vma p_end = p_start + p->p_memsz;
   5513  1.1  christos 	      bfd_vma s_start = sec->lma;
   5514  1.1  christos 	      bfd_vma adjust = s_start - p_end;
   5515  1.1  christos 
   5516  1.1  christos 	      if (adjust != 0
   5517  1.1  christos 		  && (s_start < p_end
   5518  1.9  christos 		      || p_end < p_start))
   5519  1.9  christos 		{
   5520  1.1  christos 		  _bfd_error_handler
   5521  1.1  christos 		    /* xgettext:c-format */
   5522  1.1  christos 		    (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec,
   5523  1.1  christos 		     (unsigned long) s_start, (unsigned long) p_end);
   5524  1.1  christos 		  adjust = 0;
   5525  1.1  christos 		  sec->lma = p_end;
   5526  1.1  christos 		}
   5527  1.1  christos 	      p->p_memsz += adjust;
   5528  1.1  christos 
   5529  1.1  christos 	      if (this_hdr->sh_type != SHT_NOBITS)
   5530  1.1  christos 		{
   5531  1.1  christos 		  if (p->p_filesz + adjust < p->p_memsz)
   5532  1.1  christos 		    {
   5533  1.1  christos 		      /* We have a PROGBITS section following NOBITS ones.
   5534  1.1  christos 		         Allocate file space for the NOBITS section(s) and
   5535  1.1  christos 			 zero it.  */
   5536  1.1  christos 		      adjust = p->p_memsz - p->p_filesz;
   5537  1.1  christos 		      if (!write_zeros (abfd, off, adjust))
   5538  1.1  christos 			return FALSE;
   5539  1.1  christos 		    }
   5540  1.1  christos 		  off += adjust;
   5541  1.1  christos 		  p->p_filesz += adjust;
   5542  1.1  christos 		}
   5543  1.1  christos 	    }
   5544  1.1  christos 
   5545  1.1  christos 	  if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
   5546  1.1  christos 	    {
   5547  1.1  christos 	      /* The section at i == 0 is the one that actually contains
   5548  1.1  christos 		 everything.  */
   5549  1.1  christos 	      if (i == 0)
   5550  1.1  christos 		{
   5551  1.1  christos 		  this_hdr->sh_offset = sec->filepos = off;
   5552  1.1  christos 		  off += this_hdr->sh_size;
   5553  1.1  christos 		  p->p_filesz = this_hdr->sh_size;
   5554  1.1  christos 		  p->p_memsz = 0;
   5555  1.1  christos 		  p->p_align = 1;
   5556  1.1  christos 		}
   5557  1.1  christos 	      else
   5558  1.1  christos 		{
   5559  1.1  christos 		  /* The rest are fake sections that shouldn't be written.  */
   5560  1.1  christos 		  sec->filepos = 0;
   5561  1.1  christos 		  sec->size = 0;
   5562  1.1  christos 		  sec->flags = 0;
   5563  1.1  christos 		  continue;
   5564  1.1  christos 		}
   5565  1.1  christos 	    }
   5566  1.1  christos 	  else
   5567  1.1  christos 	    {
   5568  1.1  christos 	      if (p->p_type == PT_LOAD)
   5569  1.1  christos 		{
   5570  1.1  christos 		  this_hdr->sh_offset = sec->filepos = off;
   5571  1.1  christos 		  if (this_hdr->sh_type != SHT_NOBITS)
   5572  1.3  christos 		    off += this_hdr->sh_size;
   5573  1.3  christos 		}
   5574  1.3  christos 	      else if (this_hdr->sh_type == SHT_NOBITS
   5575  1.3  christos 		       && (this_hdr->sh_flags & SHF_TLS) != 0
   5576  1.3  christos 		       && this_hdr->sh_offset == 0)
   5577  1.3  christos 		{
   5578  1.3  christos 		  /* This is a .tbss section that didn't get a PT_LOAD.
   5579  1.3  christos 		     (See _bfd_elf_map_sections_to_segments "Create a
   5580  1.3  christos 		     final PT_LOAD".)  Set sh_offset to the value it
   5581  1.3  christos 		     would have if we had created a zero p_filesz and
   5582  1.3  christos 		     p_memsz PT_LOAD header for the section.  This
   5583  1.3  christos 		     also makes the PT_TLS header have the same
   5584  1.3  christos 		     p_offset value.  */
   5585  1.3  christos 		  bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
   5586  1.3  christos 							  off, align);
   5587  1.1  christos 		  this_hdr->sh_offset = sec->filepos = off + adjust;
   5588  1.1  christos 		}
   5589  1.1  christos 
   5590  1.1  christos 	      if (this_hdr->sh_type != SHT_NOBITS)
   5591  1.1  christos 		{
   5592  1.1  christos 		  p->p_filesz += this_hdr->sh_size;
   5593  1.1  christos 		  /* A load section without SHF_ALLOC is something like
   5594  1.1  christos 		     a note section in a PT_NOTE segment.  These take
   5595  1.1  christos 		     file space but are not loaded into memory.  */
   5596  1.1  christos 		  if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   5597  1.1  christos 		    p->p_memsz += this_hdr->sh_size;
   5598  1.1  christos 		}
   5599  1.1  christos 	      else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   5600  1.1  christos 		{
   5601  1.1  christos 		  if (p->p_type == PT_TLS)
   5602  1.1  christos 		    p->p_memsz += this_hdr->sh_size;
   5603  1.1  christos 
   5604  1.1  christos 		  /* .tbss is special.  It doesn't contribute to p_memsz of
   5605  1.1  christos 		     normal segments.  */
   5606  1.1  christos 		  else if ((this_hdr->sh_flags & SHF_TLS) == 0)
   5607  1.1  christos 		    p->p_memsz += this_hdr->sh_size;
   5608  1.1  christos 		}
   5609  1.1  christos 
   5610  1.1  christos 	      if (align > p->p_align
   5611  1.1  christos 		  && !m->p_align_valid
   5612  1.1  christos 		  && (p->p_type != PT_LOAD
   5613  1.1  christos 		      || (abfd->flags & D_PAGED) == 0))
   5614  1.1  christos 		p->p_align = align;
   5615  1.1  christos 	    }
   5616  1.1  christos 
   5617  1.1  christos 	  if (!m->p_flags_valid)
   5618  1.1  christos 	    {
   5619  1.1  christos 	      p->p_flags |= PF_R;
   5620  1.1  christos 	      if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
   5621  1.1  christos 		p->p_flags |= PF_X;
   5622  1.1  christos 	      if ((this_hdr->sh_flags & SHF_WRITE) != 0)
   5623  1.1  christos 		p->p_flags |= PF_W;
   5624  1.5  christos 	    }
   5625  1.1  christos 	}
   5626  1.1  christos 
   5627  1.1  christos       off -= off_adjust;
   5628  1.1  christos 
   5629  1.1  christos       /* Check that all sections are in a PT_LOAD segment.
   5630  1.1  christos 	 Don't check funky gdb generated core files.  */
   5631  1.1  christos       if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
   5632  1.1  christos 	{
   5633  1.1  christos 	  bfd_boolean check_vma = TRUE;
   5634  1.1  christos 
   5635  1.1  christos 	  for (i = 1; i < m->count; i++)
   5636  1.1  christos 	    if (m->sections[i]->vma == m->sections[i - 1]->vma
   5637  1.1  christos 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
   5638  1.1  christos 				       ->this_hdr), p) != 0
   5639  1.1  christos 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
   5640  1.1  christos 				       ->this_hdr), p) != 0)
   5641  1.1  christos 	      {
   5642  1.1  christos 		/* Looks like we have overlays packed into the segment.  */
   5643  1.1  christos 		check_vma = FALSE;
   5644  1.1  christos 		break;
   5645  1.1  christos 	      }
   5646  1.1  christos 
   5647  1.1  christos 	  for (i = 0; i < m->count; i++)
   5648  1.1  christos 	    {
   5649  1.1  christos 	      Elf_Internal_Shdr *this_hdr;
   5650  1.1  christos 	      asection *sec;
   5651  1.1  christos 
   5652  1.3  christos 	      sec = m->sections[i];
   5653  1.3  christos 	      this_hdr = &(elf_section_data(sec)->this_hdr);
   5654  1.1  christos 	      if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
   5655  1.9  christos 		  && !ELF_TBSS_SPECIAL (this_hdr, p))
   5656  1.9  christos 		{
   5657  1.1  christos 		  _bfd_error_handler
   5658  1.1  christos 		    /* xgettext:c-format */
   5659  1.1  christos 		    (_("%B: section `%A' can't be allocated in segment %d"),
   5660  1.1  christos 		     abfd, sec, j);
   5661  1.1  christos 		  print_segment_map (m);
   5662  1.1  christos 		}
   5663  1.1  christos 	    }
   5664  1.1  christos 	}
   5665  1.3  christos     }
   5666  1.1  christos 
   5667  1.1  christos   elf_next_file_pos (abfd) = off;
   5668  1.1  christos   return TRUE;
   5669  1.1  christos }
   5670  1.1  christos 
   5671  1.1  christos /* Assign file positions for the other sections.  */
   5672  1.1  christos 
   5673  1.1  christos static bfd_boolean
   5674  1.1  christos assign_file_positions_for_non_load_sections (bfd *abfd,
   5675  1.1  christos 					     struct bfd_link_info *link_info)
   5676  1.1  christos {
   5677  1.6  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   5678  1.1  christos   Elf_Internal_Shdr **i_shdrpp;
   5679  1.1  christos   Elf_Internal_Shdr **hdrpp, **end_hdrpp;
   5680  1.1  christos   Elf_Internal_Phdr *phdrs;
   5681  1.3  christos   Elf_Internal_Phdr *p;
   5682  1.1  christos   struct elf_segment_map *m;
   5683  1.1  christos   struct elf_segment_map *hdrs_segment;
   5684  1.1  christos   bfd_vma filehdr_vaddr, filehdr_paddr;
   5685  1.1  christos   bfd_vma phdrs_vaddr, phdrs_paddr;
   5686  1.1  christos   file_ptr off;
   5687  1.1  christos   unsigned int count;
   5688  1.6  christos 
   5689  1.3  christos   i_shdrpp = elf_elfsections (abfd);
   5690  1.6  christos   end_hdrpp = i_shdrpp + elf_numsections (abfd);
   5691  1.1  christos   off = elf_next_file_pos (abfd);
   5692  1.1  christos   for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
   5693  1.1  christos     {
   5694  1.1  christos       Elf_Internal_Shdr *hdr;
   5695  1.1  christos 
   5696  1.1  christos       hdr = *hdrpp;
   5697  1.1  christos       if (hdr->bfd_section != NULL
   5698  1.1  christos 	  && (hdr->bfd_section->filepos != 0
   5699  1.1  christos 	      || (hdr->sh_type == SHT_NOBITS
   5700  1.1  christos 		  && hdr->contents == NULL)))
   5701  1.1  christos 	BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
   5702  1.3  christos       else if ((hdr->sh_flags & SHF_ALLOC) != 0)
   5703  1.9  christos 	{
   5704  1.9  christos 	  if (hdr->sh_size != 0)
   5705  1.3  christos 	    _bfd_error_handler
   5706  1.3  christos 	      /* xgettext:c-format */
   5707  1.3  christos 	      (_("%B: warning: allocated section `%s' not in segment"),
   5708  1.3  christos 	       abfd,
   5709  1.3  christos 	       (hdr->bfd_section == NULL
   5710  1.1  christos 		? "*unknown*"
   5711  1.1  christos 		: hdr->bfd_section->name));
   5712  1.1  christos 	  /* We don't need to page align empty sections.  */
   5713  1.1  christos 	  if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
   5714  1.1  christos 	    off += vma_page_aligned_bias (hdr->sh_addr, off,
   5715  1.1  christos 					  bed->maxpagesize);
   5716  1.1  christos 	  else
   5717  1.1  christos 	    off += vma_page_aligned_bias (hdr->sh_addr, off,
   5718  1.1  christos 					  hdr->sh_addralign);
   5719  1.1  christos 	  off = _bfd_elf_assign_file_position_for_section (hdr, off,
   5720  1.1  christos 							   FALSE);
   5721  1.1  christos 	}
   5722  1.6  christos       else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   5723  1.6  christos 		&& hdr->bfd_section == NULL)
   5724  1.6  christos 	       || (hdr->bfd_section != NULL
   5725  1.3  christos 		   && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
   5726  1.8  christos 		   /* Compress DWARF debug sections.  */
   5727  1.8  christos 	       || hdr == i_shdrpp[elf_onesymtab (abfd)]
   5728  1.6  christos 	       || (elf_symtab_shndx_list (abfd) != NULL
   5729  1.6  christos 		   && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   5730  1.1  christos 	       || hdr == i_shdrpp[elf_strtab_sec (abfd)]
   5731  1.1  christos 	       || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
   5732  1.1  christos 	hdr->sh_offset = -1;
   5733  1.1  christos       else
   5734  1.1  christos 	off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
   5735  1.1  christos     }
   5736  1.1  christos 
   5737  1.1  christos   /* Now that we have set the section file positions, we can set up
   5738  1.1  christos      the file positions for the non PT_LOAD segments.  */
   5739  1.1  christos   count = 0;
   5740  1.1  christos   filehdr_vaddr = 0;
   5741  1.1  christos   filehdr_paddr = 0;
   5742  1.3  christos   phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
   5743  1.1  christos   phdrs_paddr = 0;
   5744  1.3  christos   hdrs_segment = NULL;
   5745  1.1  christos   phdrs = elf_tdata (abfd)->phdr;
   5746  1.1  christos   for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
   5747  1.1  christos     {
   5748  1.1  christos       ++count;
   5749  1.1  christos       if (p->p_type != PT_LOAD)
   5750  1.1  christos 	continue;
   5751  1.1  christos 
   5752  1.1  christos       if (m->includes_filehdr)
   5753  1.1  christos 	{
   5754  1.1  christos 	  filehdr_vaddr = p->p_vaddr;
   5755  1.1  christos 	  filehdr_paddr = p->p_paddr;
   5756  1.1  christos 	}
   5757  1.1  christos       if (m->includes_phdrs)
   5758  1.1  christos 	{
   5759  1.1  christos 	  phdrs_vaddr = p->p_vaddr;
   5760  1.1  christos 	  phdrs_paddr = p->p_paddr;
   5761  1.3  christos 	  if (m->includes_filehdr)
   5762  1.1  christos 	    {
   5763  1.1  christos 	      hdrs_segment = m;
   5764  1.1  christos 	      phdrs_vaddr += bed->s->sizeof_ehdr;
   5765  1.1  christos 	      phdrs_paddr += bed->s->sizeof_ehdr;
   5766  1.1  christos 	    }
   5767  1.1  christos 	}
   5768  1.3  christos     }
   5769  1.3  christos 
   5770  1.3  christos   if (hdrs_segment != NULL && link_info != NULL)
   5771  1.3  christos     {
   5772  1.3  christos       /* There is a segment that contains both the file headers and the
   5773  1.3  christos 	 program headers, so provide a symbol __ehdr_start pointing there.
   5774  1.3  christos 	 A program can use this to examine itself robustly.  */
   5775  1.3  christos 
   5776  1.3  christos       struct elf_link_hash_entry *hash
   5777  1.3  christos 	= elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
   5778  1.3  christos 				FALSE, FALSE, TRUE);
   5779  1.3  christos       /* If the symbol was referenced and not defined, define it.  */
   5780  1.3  christos       if (hash != NULL
   5781  1.3  christos 	  && (hash->root.type == bfd_link_hash_new
   5782  1.3  christos 	      || hash->root.type == bfd_link_hash_undefined
   5783  1.3  christos 	      || hash->root.type == bfd_link_hash_undefweak
   5784  1.3  christos 	      || hash->root.type == bfd_link_hash_common))
   5785  1.3  christos 	{
   5786  1.3  christos 	  asection *s = NULL;
   5787  1.3  christos 	  if (hdrs_segment->count != 0)
   5788  1.3  christos 	    /* The segment contains sections, so use the first one.  */
   5789  1.3  christos 	    s = hdrs_segment->sections[0];
   5790  1.3  christos 	  else
   5791  1.3  christos 	    /* Use the first (i.e. lowest-addressed) section in any segment.  */
   5792  1.3  christos 	    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   5793  1.3  christos 	      if (m->count != 0)
   5794  1.3  christos 		{
   5795  1.3  christos 		  s = m->sections[0];
   5796  1.3  christos 		  break;
   5797  1.3  christos 		}
   5798  1.3  christos 
   5799  1.3  christos 	  if (s != NULL)
   5800  1.3  christos 	    {
   5801  1.3  christos 	      hash->root.u.def.value = filehdr_vaddr - s->vma;
   5802  1.3  christos 	      hash->root.u.def.section = s;
   5803  1.3  christos 	    }
   5804  1.3  christos 	  else
   5805  1.3  christos 	    {
   5806  1.3  christos 	      hash->root.u.def.value = filehdr_vaddr;
   5807  1.3  christos 	      hash->root.u.def.section = bfd_abs_section_ptr;
   5808  1.3  christos 	    }
   5809  1.3  christos 
   5810  1.3  christos 	  hash->root.type = bfd_link_hash_defined;
   5811  1.3  christos 	  hash->def_regular = 1;
   5812  1.3  christos 	  hash->non_elf = 0;
   5813  1.3  christos 	}
   5814  1.3  christos     }
   5815  1.1  christos 
   5816  1.1  christos   for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
   5817  1.1  christos     {
   5818  1.1  christos       if (p->p_type == PT_GNU_RELRO)
   5819  1.3  christos 	{
   5820  1.1  christos 	  const Elf_Internal_Phdr *lp;
   5821  1.1  christos 	  struct elf_segment_map *lm;
   5822  1.1  christos 
   5823  1.1  christos 	  if (link_info != NULL)
   5824  1.1  christos 	    {
   5825  1.3  christos 	      /* During linking the range of the RELRO segment is passed
   5826  1.3  christos 		 in link_info.  */
   5827  1.3  christos 	      for (lm = elf_seg_map (abfd), lp = phdrs;
   5828  1.1  christos 		   lm != NULL;
   5829  1.1  christos 		   lm = lm->next, lp++)
   5830  1.1  christos 		{
   5831  1.3  christos 		  if (lp->p_type == PT_LOAD
   5832  1.3  christos 		      && lp->p_vaddr < link_info->relro_end
   5833  1.1  christos 		      && lm->count != 0
   5834  1.1  christos 		      && lm->sections[0]->vma >= link_info->relro_start)
   5835  1.3  christos 		    break;
   5836  1.3  christos 		}
   5837  1.1  christos 
   5838  1.1  christos 	      BFD_ASSERT (lm != NULL);
   5839  1.1  christos 	    }
   5840  1.1  christos 	  else
   5841  1.1  christos 	    {
   5842  1.1  christos 	      /* Otherwise we are copying an executable or shared
   5843  1.1  christos 		 library, but we need to use the same linker logic.  */
   5844  1.1  christos 	      for (lp = phdrs; lp < phdrs + count; ++lp)
   5845  1.1  christos 		{
   5846  1.1  christos 		  if (lp->p_type == PT_LOAD
   5847  1.1  christos 		      && lp->p_paddr == p->p_paddr)
   5848  1.1  christos 		    break;
   5849  1.1  christos 		}
   5850  1.1  christos 	    }
   5851  1.1  christos 
   5852  1.1  christos 	  if (lp < phdrs + count)
   5853  1.1  christos 	    {
   5854  1.1  christos 	      p->p_vaddr = lp->p_vaddr;
   5855  1.1  christos 	      p->p_paddr = lp->p_paddr;
   5856  1.1  christos 	      p->p_offset = lp->p_offset;
   5857  1.1  christos 	      if (link_info != NULL)
   5858  1.1  christos 		p->p_filesz = link_info->relro_end - lp->p_vaddr;
   5859  1.1  christos 	      else if (m->p_size_valid)
   5860  1.1  christos 		p->p_filesz = m->p_size;
   5861  1.1  christos 	      else
   5862  1.3  christos 		abort ();
   5863  1.3  christos 	      p->p_memsz = p->p_filesz;
   5864  1.3  christos 	      /* Preserve the alignment and flags if they are valid. The
   5865  1.3  christos 	         gold linker generates RW/4 for the PT_GNU_RELRO section.
   5866  1.3  christos 		 It is better for objcopy/strip to honor these attributes
   5867  1.3  christos 		 otherwise gdb will choke when using separate debug files.
   5868  1.3  christos 	       */
   5869  1.3  christos 	      if (!m->p_align_valid)
   5870  1.8  christos 		p->p_align = 1;
   5871  1.1  christos 	      if (!m->p_flags_valid)
   5872  1.1  christos 		p->p_flags = PF_R;
   5873  1.1  christos 	    }
   5874  1.1  christos 	  else
   5875  1.1  christos 	    {
   5876  1.1  christos 	      memset (p, 0, sizeof *p);
   5877  1.1  christos 	      p->p_type = PT_NULL;
   5878  1.3  christos 	    }
   5879  1.3  christos 	}
   5880  1.3  christos       else if (p->p_type == PT_GNU_STACK)
   5881  1.3  christos 	{
   5882  1.3  christos 	  if (m->p_size_valid)
   5883  1.1  christos 	    p->p_memsz = m->p_size;
   5884  1.1  christos 	}
   5885  1.6  christos       else if (m->count != 0)
   5886  1.9  christos 	{
   5887  1.1  christos 	  unsigned int i;
   5888  1.1  christos 
   5889  1.1  christos 	  if (p->p_type != PT_LOAD
   5890  1.1  christos 	      && (p->p_type != PT_NOTE
   5891  1.9  christos 		  || bfd_get_format (abfd) != bfd_core))
   5892  1.9  christos 	    {
   5893  1.9  christos 	      /* A user specified segment layout may include a PHDR
   5894  1.9  christos 		 segment that overlaps with a LOAD segment...  */
   5895  1.9  christos 	      if (p->p_type == PT_PHDR)
   5896  1.9  christos 		{
   5897  1.9  christos 		  m->count = 0;
   5898  1.9  christos 		  continue;
   5899  1.6  christos 		}
   5900  1.6  christos 
   5901  1.6  christos 	      if (m->includes_filehdr || m->includes_phdrs)
   5902  1.9  christos 		{
   5903  1.9  christos 		  /* PR 17512: file: 2195325e.  */
   5904  1.9  christos 		  _bfd_error_handler
   5905  1.6  christos 		    (_("%B: error: non-load segment %d includes file header and/or program header"),
   5906  1.6  christos 		     abfd, (int)(p - phdrs));
   5907  1.1  christos 		  return FALSE;
   5908  1.3  christos 		}
   5909  1.1  christos 
   5910  1.3  christos 	      p->p_filesz = 0;
   5911  1.3  christos 	      p->p_offset = m->sections[0]->filepos;
   5912  1.3  christos 	      for (i = m->count; i-- != 0;)
   5913  1.3  christos 		{
   5914  1.3  christos 		  asection *sect = m->sections[i];
   5915  1.3  christos 		  Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
   5916  1.3  christos 		  if (hdr->sh_type != SHT_NOBITS)
   5917  1.3  christos 		    {
   5918  1.3  christos 		      p->p_filesz = (sect->filepos - m->sections[0]->filepos
   5919  1.3  christos 				     + hdr->sh_size);
   5920  1.3  christos 		      break;
   5921  1.1  christos 		    }
   5922  1.1  christos 		}
   5923  1.1  christos 	    }
   5924  1.1  christos 	}
   5925  1.1  christos       else if (m->includes_filehdr)
   5926  1.1  christos 	{
   5927  1.1  christos 	  p->p_vaddr = filehdr_vaddr;
   5928  1.1  christos 	  if (! m->p_paddr_valid)
   5929  1.1  christos 	    p->p_paddr = filehdr_paddr;
   5930  1.1  christos 	}
   5931  1.1  christos       else if (m->includes_phdrs)
   5932  1.1  christos 	{
   5933  1.1  christos 	  p->p_vaddr = phdrs_vaddr;
   5934  1.1  christos 	  if (! m->p_paddr_valid)
   5935  1.1  christos 	    p->p_paddr = phdrs_paddr;
   5936  1.1  christos 	}
   5937  1.3  christos     }
   5938  1.1  christos 
   5939  1.1  christos   elf_next_file_pos (abfd) = off;
   5940  1.1  christos 
   5941  1.1  christos   return TRUE;
   5942  1.8  christos }
   5943  1.8  christos 
   5944  1.8  christos static elf_section_list *
   5945  1.8  christos find_section_in_list (unsigned int i, elf_section_list * list)
   5946  1.8  christos {
   5947  1.8  christos   for (;list != NULL; list = list->next)
   5948  1.8  christos     if (list->ndx == i)
   5949  1.8  christos       break;
   5950  1.8  christos   return list;
   5951  1.1  christos }
   5952  1.1  christos 
   5953  1.1  christos /* Work out the file positions of all the sections.  This is called by
   5954  1.1  christos    _bfd_elf_compute_section_file_positions.  All the section sizes and
   5955  1.1  christos    VMAs must be known before this is called.
   5956  1.1  christos 
   5957  1.1  christos    Reloc sections come in two flavours: Those processed specially as
   5958  1.1  christos    "side-channel" data attached to a section to which they apply, and
   5959  1.1  christos    those that bfd doesn't process as relocations.  The latter sort are
   5960  1.1  christos    stored in a normal bfd section by bfd_section_from_shdr.   We don't
   5961  1.1  christos    consider the former sort here, unless they form part of the loadable
   5962  1.1  christos    image.  Reloc sections not assigned here will be handled later by
   5963  1.1  christos    assign_file_positions_for_relocs.
   5964  1.1  christos 
   5965  1.1  christos    We also don't set the positions of the .symtab and .strtab here.  */
   5966  1.1  christos 
   5967  1.1  christos static bfd_boolean
   5968  1.1  christos assign_file_positions_except_relocs (bfd *abfd,
   5969  1.1  christos 				     struct bfd_link_info *link_info)
   5970  1.1  christos {
   5971  1.1  christos   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   5972  1.1  christos   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
   5973  1.1  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   5974  1.1  christos 
   5975  1.1  christos   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
   5976  1.1  christos       && bfd_get_format (abfd) != bfd_core)
   5977  1.1  christos     {
   5978  1.1  christos       Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
   5979  1.1  christos       unsigned int num_sec = elf_numsections (abfd);
   5980  1.5  christos       Elf_Internal_Shdr **hdrpp;
   5981  1.1  christos       unsigned int i;
   5982  1.1  christos       file_ptr off;
   5983  1.1  christos 
   5984  1.1  christos       /* Start after the ELF header.  */
   5985  1.1  christos       off = i_ehdrp->e_ehsize;
   5986  1.1  christos 
   5987  1.1  christos       /* We are not creating an executable, which means that we are
   5988  1.1  christos 	 not creating a program header, and that the actual order of
   5989  1.1  christos 	 the sections in the file is unimportant.  */
   5990  1.1  christos       for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
   5991  1.1  christos 	{
   5992  1.1  christos 	  Elf_Internal_Shdr *hdr;
   5993  1.1  christos 
   5994  1.1  christos 	  hdr = *hdrpp;
   5995  1.6  christos 	  if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   5996  1.6  christos 	       && hdr->bfd_section == NULL)
   5997  1.6  christos 	      || (hdr->bfd_section != NULL
   5998  1.3  christos 		  && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
   5999  1.8  christos 		  /* Compress DWARF debug sections.  */
   6000  1.8  christos 	      || i == elf_onesymtab (abfd)
   6001  1.6  christos 	      || (elf_symtab_shndx_list (abfd) != NULL
   6002  1.6  christos 		  && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   6003  1.1  christos 	      || i == elf_strtab_sec (abfd)
   6004  1.1  christos 	      || i == elf_shstrtab_sec (abfd))
   6005  1.1  christos 	    {
   6006  1.1  christos 	      hdr->sh_offset = -1;
   6007  1.1  christos 	    }
   6008  1.1  christos 	  else
   6009  1.5  christos 	    off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
   6010  1.5  christos 	}
   6011  1.1  christos 
   6012  1.1  christos       elf_next_file_pos (abfd) = off;
   6013  1.1  christos     }
   6014  1.1  christos   else
   6015  1.1  christos     {
   6016  1.1  christos       unsigned int alloc;
   6017  1.1  christos 
   6018  1.1  christos       /* Assign file positions for the loaded sections based on the
   6019  1.1  christos 	 assignment of sections to segments.  */
   6020  1.1  christos       if (!assign_file_positions_for_load_sections (abfd, link_info))
   6021  1.1  christos 	return FALSE;
   6022  1.1  christos 
   6023  1.1  christos       /* And for non-load sections.  */
   6024  1.1  christos       if (!assign_file_positions_for_non_load_sections (abfd, link_info))
   6025  1.1  christos 	return FALSE;
   6026  1.1  christos 
   6027  1.1  christos       if (bed->elf_backend_modify_program_headers != NULL)
   6028  1.1  christos 	{
   6029  1.1  christos 	  if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
   6030  1.1  christos 	    return FALSE;
   6031  1.4  christos 	}
   6032  1.8  christos 
   6033  1.4  christos       /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.  */
   6034  1.4  christos       if (link_info != NULL && bfd_link_pie (link_info))
   6035  1.4  christos 	{
   6036  1.4  christos 	  unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
   6037  1.4  christos 	  Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
   6038  1.4  christos 	  Elf_Internal_Phdr *end_segment = &segment[num_segments];
   6039  1.4  christos 
   6040  1.4  christos 	  /* Find the lowest p_vaddr in PT_LOAD segments.  */
   6041  1.4  christos 	  bfd_vma p_vaddr = (bfd_vma) -1;
   6042  1.4  christos 	  for (; segment < end_segment; segment++)
   6043  1.4  christos 	    if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
   6044  1.4  christos 	      p_vaddr = segment->p_vaddr;
   6045  1.4  christos 
   6046  1.4  christos 	  /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
   6047  1.4  christos 	     segments is non-zero.  */
   6048  1.4  christos 	  if (p_vaddr)
   6049  1.4  christos 	    i_ehdrp->e_type = ET_EXEC;
   6050  1.1  christos 	}
   6051  1.3  christos 
   6052  1.9  christos       /* Write out the program headers.  */
   6053  1.9  christos       alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
   6054  1.9  christos 
   6055  1.9  christos       /* Sort the program headers into the ordering required by the ELF standard.  */
   6056  1.9  christos       if (alloc == 0)
   6057  1.9  christos 	return TRUE;
   6058  1.9  christos 
   6059  1.9  christos       /* PR ld/20815 - Check that the program header segment, if present, will
   6060  1.9  christos 	 be loaded into memory.  FIXME: The check below is not sufficient as
   6061  1.9  christos 	 really all PT_LOAD segments should be checked before issuing an error
   6062  1.9  christos 	 message.  Plus the PHDR segment does not have to be the first segment
   6063  1.9  christos 	 in the program header table.  But this version of the check should
   6064  1.9  christos 	 catch all real world use cases.
   6065  1.9  christos 
   6066  1.9  christos 	 FIXME: We used to have code here to sort the PT_LOAD segments into
   6067  1.9  christos 	 ascending order, as per the ELF spec.  But this breaks some programs,
   6068  1.9  christos 	 including the Linux kernel.  But really either the spec should be
   6069  1.9  christos          changed or the programs updated.  */
   6070  1.9  christos       if (alloc > 1
   6071  1.9  christos 	  && tdata->phdr[0].p_type == PT_PHDR
   6072  1.9  christos 	  && ! bed->elf_backend_allow_non_load_phdr (abfd, tdata->phdr, alloc)
   6073  1.9  christos 	  && tdata->phdr[1].p_type == PT_LOAD
   6074  1.9  christos 	  && (tdata->phdr[1].p_vaddr > tdata->phdr[0].p_vaddr
   6075  1.9  christos 	      || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz)
   6076  1.9  christos 	      <  (tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz)))
   6077  1.9  christos 	{
   6078  1.9  christos 	  /* The fix for this error is usually to edit the linker script being
   6079  1.9  christos 	     used and set up the program headers manually.  Either that or
   6080  1.9  christos 	     leave room for the headers at the start of the SECTIONS.  */
   6081  1.9  christos 	  _bfd_error_handler (_("\
   6082  1.9  christos %B: error: PHDR segment not covered by LOAD segment"),
   6083  1.9  christos 			      abfd);
   6084  1.9  christos 	  return FALSE;
   6085  1.1  christos 	}
   6086  1.1  christos 
   6087  1.1  christos       if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
   6088  1.1  christos 	  || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
   6089  1.1  christos 	return FALSE;
   6090  1.1  christos     }
   6091  1.1  christos 
   6092  1.1  christos   return TRUE;
   6093  1.1  christos }
   6094  1.1  christos 
   6095  1.1  christos static bfd_boolean
   6096  1.1  christos prep_headers (bfd *abfd)
   6097  1.1  christos {
   6098  1.1  christos   Elf_Internal_Ehdr *i_ehdrp;	/* Elf file header, internal form.  */
   6099  1.1  christos   struct elf_strtab_hash *shstrtab;
   6100  1.1  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6101  1.1  christos 
   6102  1.1  christos   i_ehdrp = elf_elfheader (abfd);
   6103  1.1  christos 
   6104  1.1  christos   shstrtab = _bfd_elf_strtab_init ();
   6105  1.1  christos   if (shstrtab == NULL)
   6106  1.1  christos     return FALSE;
   6107  1.1  christos 
   6108  1.1  christos   elf_shstrtab (abfd) = shstrtab;
   6109  1.1  christos 
   6110  1.1  christos   i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
   6111  1.1  christos   i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
   6112  1.1  christos   i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
   6113  1.1  christos   i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
   6114  1.1  christos 
   6115  1.1  christos   i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
   6116  1.1  christos   i_ehdrp->e_ident[EI_DATA] =
   6117  1.1  christos     bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
   6118  1.1  christos   i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
   6119  1.1  christos 
   6120  1.1  christos   if ((abfd->flags & DYNAMIC) != 0)
   6121  1.1  christos     i_ehdrp->e_type = ET_DYN;
   6122  1.1  christos   else if ((abfd->flags & EXEC_P) != 0)
   6123  1.1  christos     i_ehdrp->e_type = ET_EXEC;
   6124  1.1  christos   else if (bfd_get_format (abfd) == bfd_core)
   6125  1.1  christos     i_ehdrp->e_type = ET_CORE;
   6126  1.1  christos   else
   6127  1.1  christos     i_ehdrp->e_type = ET_REL;
   6128  1.1  christos 
   6129  1.1  christos   switch (bfd_get_arch (abfd))
   6130  1.1  christos     {
   6131  1.1  christos     case bfd_arch_unknown:
   6132  1.1  christos       i_ehdrp->e_machine = EM_NONE;
   6133  1.1  christos       break;
   6134  1.1  christos 
   6135  1.1  christos       /* There used to be a long list of cases here, each one setting
   6136  1.1  christos 	 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
   6137  1.1  christos 	 in the corresponding bfd definition.  To avoid duplication,
   6138  1.1  christos 	 the switch was removed.  Machines that need special handling
   6139  1.1  christos 	 can generally do it in elf_backend_final_write_processing(),
   6140  1.1  christos 	 unless they need the information earlier than the final write.
   6141  1.1  christos 	 Such need can generally be supplied by replacing the tests for
   6142  1.1  christos 	 e_machine with the conditions used to determine it.  */
   6143  1.1  christos     default:
   6144  1.1  christos       i_ehdrp->e_machine = bed->elf_machine_code;
   6145  1.1  christos     }
   6146  1.1  christos 
   6147  1.1  christos   i_ehdrp->e_version = bed->s->ev_current;
   6148  1.1  christos   i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
   6149  1.1  christos 
   6150  1.1  christos   /* No program header, for now.  */
   6151  1.1  christos   i_ehdrp->e_phoff = 0;
   6152  1.1  christos   i_ehdrp->e_phentsize = 0;
   6153  1.1  christos   i_ehdrp->e_phnum = 0;
   6154  1.1  christos 
   6155  1.1  christos   /* Each bfd section is section header entry.  */
   6156  1.1  christos   i_ehdrp->e_entry = bfd_get_start_address (abfd);
   6157  1.1  christos   i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
   6158  1.1  christos 
   6159  1.1  christos   /* If we're building an executable, we'll need a program header table.  */
   6160  1.1  christos   if (abfd->flags & EXEC_P)
   6161  1.1  christos     /* It all happens later.  */
   6162  1.1  christos     ;
   6163  1.1  christos   else
   6164  1.1  christos     {
   6165  1.1  christos       i_ehdrp->e_phentsize = 0;
   6166  1.1  christos       i_ehdrp->e_phoff = 0;
   6167  1.1  christos     }
   6168  1.1  christos 
   6169  1.1  christos   elf_tdata (abfd)->symtab_hdr.sh_name =
   6170  1.1  christos     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
   6171  1.1  christos   elf_tdata (abfd)->strtab_hdr.sh_name =
   6172  1.1  christos     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
   6173  1.1  christos   elf_tdata (abfd)->shstrtab_hdr.sh_name =
   6174  1.5  christos     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
   6175  1.1  christos   if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
   6176  1.1  christos       || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
   6177  1.1  christos       || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
   6178  1.1  christos     return FALSE;
   6179  1.1  christos 
   6180  1.1  christos   return TRUE;
   6181  1.1  christos }
   6182  1.5  christos 
   6183  1.1  christos /* Assign file positions for all the reloc sections which are not part
   6184  1.6  christos    of the loadable file image, and the file position of section headers.  */
   6185  1.6  christos 
   6186  1.1  christos static bfd_boolean
   6187  1.1  christos _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
   6188  1.6  christos {
   6189  1.6  christos   file_ptr off;
   6190  1.5  christos   Elf_Internal_Shdr **shdrpp, **end_shdrpp;
   6191  1.5  christos   Elf_Internal_Shdr *shdrp;
   6192  1.1  christos   Elf_Internal_Ehdr *i_ehdrp;
   6193  1.3  christos   const struct elf_backend_data *bed;
   6194  1.1  christos 
   6195  1.6  christos   off = elf_next_file_pos (abfd);
   6196  1.6  christos 
   6197  1.6  christos   shdrpp = elf_elfsections (abfd);
   6198  1.1  christos   end_shdrpp = shdrpp + elf_numsections (abfd);
   6199  1.6  christos   for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
   6200  1.6  christos     {
   6201  1.6  christos       shdrp = *shdrpp;
   6202  1.6  christos       if (shdrp->sh_offset == -1)
   6203  1.6  christos 	{
   6204  1.6  christos 	  asection *sec = shdrp->bfd_section;
   6205  1.6  christos 	  bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
   6206  1.6  christos 				|| shdrp->sh_type == SHT_RELA);
   6207  1.6  christos 	  if (is_rel
   6208  1.6  christos 	      || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
   6209  1.6  christos 	    {
   6210  1.6  christos 	      if (!is_rel)
   6211  1.6  christos 		{
   6212  1.6  christos 		  const char *name = sec->name;
   6213  1.6  christos 		  struct bfd_elf_section_data *d;
   6214  1.6  christos 
   6215  1.6  christos 		  /* Compress DWARF debug sections.  */
   6216  1.6  christos 		  if (!bfd_compress_section (abfd, sec,
   6217  1.1  christos 					     shdrp->contents))
   6218  1.6  christos 		    return FALSE;
   6219  1.6  christos 
   6220  1.6  christos 		  if (sec->compress_status == COMPRESS_SECTION_DONE
   6221  1.6  christos 		      && (abfd->flags & BFD_COMPRESS_GABI) == 0)
   6222  1.6  christos 		    {
   6223  1.6  christos 		      /* If section is compressed with zlib-gnu, convert
   6224  1.6  christos 			 section name from .debug_* to .zdebug_*.  */
   6225  1.6  christos 		      char *new_name
   6226  1.6  christos 			= convert_debug_to_zdebug (abfd, name);
   6227  1.6  christos 		      if (new_name == NULL)
   6228  1.6  christos 			return FALSE;
   6229  1.9  christos 		      name = new_name;
   6230  1.6  christos 		    }
   6231  1.6  christos 		  /* Add section name to section name section.  */
   6232  1.6  christos 		  if (shdrp->sh_name != (unsigned int) -1)
   6233  1.6  christos 		    abort ();
   6234  1.6  christos 		  shdrp->sh_name
   6235  1.6  christos 		    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   6236  1.6  christos 							  name, FALSE);
   6237  1.9  christos 		  d = elf_section_data (sec);
   6238  1.6  christos 
   6239  1.6  christos 		  /* Add reloc section name to section name section.  */
   6240  1.6  christos 		  if (d->rel.hdr
   6241  1.6  christos 		      && !_bfd_elf_set_reloc_sh_name (abfd,
   6242  1.6  christos 						      d->rel.hdr,
   6243  1.6  christos 						      name, FALSE))
   6244  1.6  christos 		    return FALSE;
   6245  1.6  christos 		  if (d->rela.hdr
   6246  1.8  christos 		      && !_bfd_elf_set_reloc_sh_name (abfd,
   6247  1.6  christos 						      d->rela.hdr,
   6248  1.6  christos 						      name, TRUE))
   6249  1.6  christos 		    return FALSE;
   6250  1.6  christos 
   6251  1.6  christos 		  /* Update section size and contents.  */
   6252  1.6  christos 		  shdrp->sh_size = sec->size;
   6253  1.6  christos 		  shdrp->contents = sec->contents;
   6254  1.6  christos 		  shdrp->bfd_section->contents = NULL;
   6255  1.6  christos 		}
   6256  1.6  christos 	      off = _bfd_elf_assign_file_position_for_section (shdrp,
   6257  1.6  christos 							       off,
   6258  1.6  christos 							       TRUE);
   6259  1.1  christos 	    }
   6260  1.1  christos 	}
   6261  1.6  christos     }
   6262  1.6  christos 
   6263  1.6  christos   /* Place section name section after DWARF debug sections have been
   6264  1.6  christos      compressed.  */
   6265  1.6  christos   _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
   6266  1.6  christos   shdrp = &elf_tdata (abfd)->shstrtab_hdr;
   6267  1.6  christos   shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
   6268  1.6  christos   off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
   6269  1.5  christos 
   6270  1.5  christos   /* Place the section headers.  */
   6271  1.5  christos   i_ehdrp = elf_elfheader (abfd);
   6272  1.5  christos   bed = get_elf_backend_data (abfd);
   6273  1.5  christos   off = align_file_position (off, 1 << bed->s->log_file_align);
   6274  1.3  christos   i_ehdrp->e_shoff = off;
   6275  1.6  christos   off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
   6276  1.6  christos   elf_next_file_pos (abfd) = off;
   6277  1.1  christos 
   6278  1.1  christos   return TRUE;
   6279  1.1  christos }
   6280  1.1  christos 
   6281  1.1  christos bfd_boolean
   6282  1.1  christos _bfd_elf_write_object_contents (bfd *abfd)
   6283  1.1  christos {
   6284  1.1  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6285  1.1  christos   Elf_Internal_Shdr **i_shdrp;
   6286  1.3  christos   bfd_boolean failed;
   6287  1.1  christos   unsigned int count, num_sec;
   6288  1.1  christos   struct elf_obj_tdata *t;
   6289  1.1  christos 
   6290  1.1  christos   if (! abfd->output_has_begun
   6291  1.1  christos       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   6292  1.1  christos     return FALSE;
   6293  1.1  christos 
   6294  1.1  christos   i_shdrp = elf_elfsections (abfd);
   6295  1.1  christos 
   6296  1.1  christos   failed = FALSE;
   6297  1.1  christos   bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
   6298  1.1  christos   if (failed)
   6299  1.6  christos     return FALSE;
   6300  1.6  christos 
   6301  1.1  christos   if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
   6302  1.1  christos     return FALSE;
   6303  1.1  christos 
   6304  1.1  christos   /* After writing the headers, we need to write the sections too...  */
   6305  1.1  christos   num_sec = elf_numsections (abfd);
   6306  1.6  christos   for (count = 1; count < num_sec; count++)
   6307  1.6  christos     {
   6308  1.6  christos       i_shdrp[count]->sh_name
   6309  1.1  christos 	= _bfd_elf_strtab_offset (elf_shstrtab (abfd),
   6310  1.1  christos 				  i_shdrp[count]->sh_name);
   6311  1.1  christos       if (bed->elf_backend_section_processing)
   6312  1.1  christos 	(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
   6313  1.1  christos       if (i_shdrp[count]->contents)
   6314  1.1  christos 	{
   6315  1.1  christos 	  bfd_size_type amt = i_shdrp[count]->sh_size;
   6316  1.1  christos 
   6317  1.1  christos 	  if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
   6318  1.1  christos 	      || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
   6319  1.1  christos 	    return FALSE;
   6320  1.1  christos 	}
   6321  1.1  christos     }
   6322  1.3  christos 
   6323  1.1  christos   /* Write out the section header names.  */
   6324  1.3  christos   t = elf_tdata (abfd);
   6325  1.1  christos   if (elf_shstrtab (abfd) != NULL
   6326  1.1  christos       && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
   6327  1.1  christos 	  || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
   6328  1.1  christos     return FALSE;
   6329  1.3  christos 
   6330  1.1  christos   if (bed->elf_backend_final_write_processing)
   6331  1.1  christos     (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd));
   6332  1.1  christos 
   6333  1.1  christos   if (!bed->s->write_shdrs_and_ehdr (abfd))
   6334  1.1  christos     return FALSE;
   6335  1.3  christos 
   6336  1.3  christos   /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
   6337  1.1  christos   if (t->o->build_id.after_write_object_contents != NULL)
   6338  1.1  christos     return (*t->o->build_id.after_write_object_contents) (abfd);
   6339  1.1  christos 
   6340  1.1  christos   return TRUE;
   6341  1.1  christos }
   6342  1.1  christos 
   6343  1.1  christos bfd_boolean
   6344  1.1  christos _bfd_elf_write_corefile_contents (bfd *abfd)
   6345  1.1  christos {
   6346  1.1  christos   /* Hopefully this can be done just like an object file.  */
   6347  1.1  christos   return _bfd_elf_write_object_contents (abfd);
   6348  1.1  christos }
   6349  1.1  christos 
   6350  1.1  christos /* Given a section, search the header to find them.  */
   6351  1.1  christos 
   6352  1.1  christos unsigned int
   6353  1.1  christos _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
   6354  1.1  christos {
   6355  1.1  christos   const struct elf_backend_data *bed;
   6356  1.1  christos   unsigned int sec_index;
   6357  1.1  christos 
   6358  1.1  christos   if (elf_section_data (asect) != NULL
   6359  1.1  christos       && elf_section_data (asect)->this_idx != 0)
   6360  1.1  christos     return elf_section_data (asect)->this_idx;
   6361  1.1  christos 
   6362  1.1  christos   if (bfd_is_abs_section (asect))
   6363  1.1  christos     sec_index = SHN_ABS;
   6364  1.1  christos   else if (bfd_is_com_section (asect))
   6365  1.1  christos     sec_index = SHN_COMMON;
   6366  1.1  christos   else if (bfd_is_und_section (asect))
   6367  1.1  christos     sec_index = SHN_UNDEF;
   6368  1.1  christos   else
   6369  1.1  christos     sec_index = SHN_BAD;
   6370  1.1  christos 
   6371  1.1  christos   bed = get_elf_backend_data (abfd);
   6372  1.1  christos   if (bed->elf_backend_section_from_bfd_section)
   6373  1.1  christos     {
   6374  1.1  christos       int retval = sec_index;
   6375  1.1  christos 
   6376  1.1  christos       if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
   6377  1.1  christos 	return retval;
   6378  1.1  christos     }
   6379  1.1  christos 
   6380  1.1  christos   if (sec_index == SHN_BAD)
   6381  1.1  christos     bfd_set_error (bfd_error_nonrepresentable_section);
   6382  1.1  christos 
   6383  1.1  christos   return sec_index;
   6384  1.1  christos }
   6385  1.1  christos 
   6386  1.1  christos /* Given a BFD symbol, return the index in the ELF symbol table, or -1
   6387  1.1  christos    on error.  */
   6388  1.1  christos 
   6389  1.1  christos int
   6390  1.1  christos _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
   6391  1.1  christos {
   6392  1.1  christos   asymbol *asym_ptr = *asym_ptr_ptr;
   6393  1.1  christos   int idx;
   6394  1.1  christos   flagword flags = asym_ptr->flags;
   6395  1.1  christos 
   6396  1.1  christos   /* When gas creates relocations against local labels, it creates its
   6397  1.1  christos      own symbol for the section, but does put the symbol into the
   6398  1.1  christos      symbol chain, so udata is 0.  When the linker is generating
   6399  1.1  christos      relocatable output, this section symbol may be for one of the
   6400  1.1  christos      input sections rather than the output section.  */
   6401  1.1  christos   if (asym_ptr->udata.i == 0
   6402  1.1  christos       && (flags & BSF_SECTION_SYM)
   6403  1.1  christos       && asym_ptr->section)
   6404  1.1  christos     {
   6405  1.1  christos       asection *sec;
   6406  1.1  christos       int indx;
   6407  1.1  christos 
   6408  1.1  christos       sec = asym_ptr->section;
   6409  1.1  christos       if (sec->owner != abfd && sec->output_section != NULL)
   6410  1.1  christos 	sec = sec->output_section;
   6411  1.1  christos       if (sec->owner == abfd
   6412  1.1  christos 	  && (indx = sec->index) < elf_num_section_syms (abfd)
   6413  1.1  christos 	  && elf_section_syms (abfd)[indx] != NULL)
   6414  1.1  christos 	asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
   6415  1.1  christos     }
   6416  1.1  christos 
   6417  1.1  christos   idx = asym_ptr->udata.i;
   6418  1.1  christos 
   6419  1.1  christos   if (idx == 0)
   6420  1.1  christos     {
   6421  1.9  christos       /* This case can occur when using --strip-symbol on a symbol
   6422  1.9  christos 	 which is used in a relocation entry.  */
   6423  1.1  christos       _bfd_error_handler
   6424  1.1  christos 	/* xgettext:c-format */
   6425  1.1  christos 	(_("%B: symbol `%s' required but not present"),
   6426  1.1  christos 	 abfd, bfd_asymbol_name (asym_ptr));
   6427  1.1  christos       bfd_set_error (bfd_error_no_symbols);
   6428  1.1  christos       return -1;
   6429  1.1  christos     }
   6430  1.1  christos 
   6431  1.1  christos #if DEBUG & 4
   6432  1.1  christos   {
   6433  1.1  christos     fprintf (stderr,
   6434  1.1  christos 	     "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
   6435  1.1  christos 	     (long) asym_ptr, asym_ptr->name, idx, (long) flags);
   6436  1.1  christos     fflush (stderr);
   6437  1.1  christos   }
   6438  1.1  christos #endif
   6439  1.1  christos 
   6440  1.1  christos   return idx;
   6441  1.1  christos }
   6442  1.1  christos 
   6443  1.1  christos /* Rewrite program header information.  */
   6444  1.1  christos 
   6445  1.1  christos static bfd_boolean
   6446  1.1  christos rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
   6447  1.1  christos {
   6448  1.1  christos   Elf_Internal_Ehdr *iehdr;
   6449  1.1  christos   struct elf_segment_map *map;
   6450  1.1  christos   struct elf_segment_map *map_first;
   6451  1.1  christos   struct elf_segment_map **pointer_to_map;
   6452  1.1  christos   Elf_Internal_Phdr *segment;
   6453  1.1  christos   asection *section;
   6454  1.1  christos   unsigned int i;
   6455  1.1  christos   unsigned int num_segments;
   6456  1.1  christos   bfd_boolean phdr_included = FALSE;
   6457  1.1  christos   bfd_boolean p_paddr_valid;
   6458  1.1  christos   bfd_vma maxpagesize;
   6459  1.1  christos   struct elf_segment_map *phdr_adjust_seg = NULL;
   6460  1.1  christos   unsigned int phdr_adjust_num = 0;
   6461  1.1  christos   const struct elf_backend_data *bed;
   6462  1.1  christos 
   6463  1.1  christos   bed = get_elf_backend_data (ibfd);
   6464  1.1  christos   iehdr = elf_elfheader (ibfd);
   6465  1.1  christos 
   6466  1.1  christos   map_first = NULL;
   6467  1.1  christos   pointer_to_map = &map_first;
   6468  1.1  christos 
   6469  1.1  christos   num_segments = elf_elfheader (ibfd)->e_phnum;
   6470  1.1  christos   maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
   6471  1.1  christos 
   6472  1.1  christos   /* Returns the end address of the segment + 1.  */
   6473  1.1  christos #define SEGMENT_END(segment, start)					\
   6474  1.1  christos   (start + (segment->p_memsz > segment->p_filesz			\
   6475  1.1  christos 	    ? segment->p_memsz : segment->p_filesz))
   6476  1.1  christos 
   6477  1.1  christos #define SECTION_SIZE(section, segment)					\
   6478  1.1  christos   (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL))		\
   6479  1.1  christos     != SEC_THREAD_LOCAL || segment->p_type == PT_TLS)			\
   6480  1.1  christos    ? section->size : 0)
   6481  1.1  christos 
   6482  1.1  christos   /* Returns TRUE if the given section is contained within
   6483  1.1  christos      the given segment.  VMA addresses are compared.  */
   6484  1.1  christos #define IS_CONTAINED_BY_VMA(section, segment)				\
   6485  1.1  christos   (section->vma >= segment->p_vaddr					\
   6486  1.1  christos    && (section->vma + SECTION_SIZE (section, segment)			\
   6487  1.1  christos        <= (SEGMENT_END (segment, segment->p_vaddr))))
   6488  1.1  christos 
   6489  1.1  christos   /* Returns TRUE if the given section is contained within
   6490  1.1  christos      the given segment.  LMA addresses are compared.  */
   6491  1.1  christos #define IS_CONTAINED_BY_LMA(section, segment, base)			\
   6492  1.1  christos   (section->lma >= base							\
   6493  1.1  christos    && (section->lma + SECTION_SIZE (section, segment)			\
   6494  1.1  christos        <= SEGMENT_END (segment, base)))
   6495  1.1  christos 
   6496  1.1  christos   /* Handle PT_NOTE segment.  */
   6497  1.1  christos #define IS_NOTE(p, s)							\
   6498  1.1  christos   (p->p_type == PT_NOTE							\
   6499  1.1  christos    && elf_section_type (s) == SHT_NOTE					\
   6500  1.1  christos    && (bfd_vma) s->filepos >= p->p_offset				\
   6501  1.1  christos    && ((bfd_vma) s->filepos + s->size					\
   6502  1.1  christos        <= p->p_offset + p->p_filesz))
   6503  1.1  christos 
   6504  1.1  christos   /* Special case: corefile "NOTE" section containing regs, prpsinfo
   6505  1.1  christos      etc.  */
   6506  1.1  christos #define IS_COREFILE_NOTE(p, s)						\
   6507  1.1  christos   (IS_NOTE (p, s)							\
   6508  1.1  christos    && bfd_get_format (ibfd) == bfd_core					\
   6509  1.1  christos    && s->vma == 0							\
   6510  1.1  christos    && s->lma == 0)
   6511  1.1  christos 
   6512  1.1  christos   /* The complicated case when p_vaddr is 0 is to handle the Solaris
   6513  1.1  christos      linker, which generates a PT_INTERP section with p_vaddr and
   6514  1.1  christos      p_memsz set to 0.  */
   6515  1.1  christos #define IS_SOLARIS_PT_INTERP(p, s)					\
   6516  1.1  christos   (p->p_vaddr == 0							\
   6517  1.1  christos    && p->p_paddr == 0							\
   6518  1.1  christos    && p->p_memsz == 0							\
   6519  1.1  christos    && p->p_filesz > 0							\
   6520  1.1  christos    && (s->flags & SEC_HAS_CONTENTS) != 0				\
   6521  1.1  christos    && s->size > 0							\
   6522  1.1  christos    && (bfd_vma) s->filepos >= p->p_offset				\
   6523  1.1  christos    && ((bfd_vma) s->filepos + s->size					\
   6524  1.1  christos        <= p->p_offset + p->p_filesz))
   6525  1.1  christos 
   6526  1.1  christos   /* Decide if the given section should be included in the given segment.
   6527  1.1  christos      A section will be included if:
   6528  1.1  christos        1. It is within the address space of the segment -- we use the LMA
   6529  1.3  christos 	  if that is set for the segment and the VMA otherwise,
   6530  1.1  christos        2. It is an allocated section or a NOTE section in a PT_NOTE
   6531  1.1  christos 	  segment.
   6532  1.1  christos        3. There is an output section associated with it,
   6533  1.1  christos        4. The section has not already been allocated to a previous segment.
   6534  1.1  christos        5. PT_GNU_STACK segments do not include any sections.
   6535  1.1  christos        6. PT_TLS segment includes only SHF_TLS sections.
   6536  1.1  christos        7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
   6537  1.1  christos        8. PT_DYNAMIC should not contain empty sections at the beginning
   6538  1.1  christos 	  (with the possible exception of .dynamic).  */
   6539  1.1  christos #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed)		\
   6540  1.1  christos   ((((segment->p_paddr							\
   6541  1.1  christos       ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr)	\
   6542  1.1  christos       : IS_CONTAINED_BY_VMA (section, segment))				\
   6543  1.1  christos      && (section->flags & SEC_ALLOC) != 0)				\
   6544  1.1  christos     || IS_NOTE (segment, section))					\
   6545  1.1  christos    && segment->p_type != PT_GNU_STACK					\
   6546  1.1  christos    && (segment->p_type != PT_TLS					\
   6547  1.1  christos        || (section->flags & SEC_THREAD_LOCAL))				\
   6548  1.1  christos    && (segment->p_type == PT_LOAD					\
   6549  1.1  christos        || segment->p_type == PT_TLS					\
   6550  1.1  christos        || (section->flags & SEC_THREAD_LOCAL) == 0)			\
   6551  1.1  christos    && (segment->p_type != PT_DYNAMIC					\
   6552  1.1  christos        || SECTION_SIZE (section, segment) > 0				\
   6553  1.1  christos        || (segment->p_paddr						\
   6554  1.1  christos 	   ? segment->p_paddr != section->lma				\
   6555  1.1  christos 	   : segment->p_vaddr != section->vma)				\
   6556  1.1  christos        || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic")	\
   6557  1.1  christos 	   == 0))							\
   6558  1.1  christos    && !section->segment_mark)
   6559  1.1  christos 
   6560  1.1  christos /* If the output section of a section in the input segment is NULL,
   6561  1.1  christos    it is removed from the corresponding output segment.   */
   6562  1.1  christos #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed)		\
   6563  1.1  christos   (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)		\
   6564  1.1  christos    && section->output_section != NULL)
   6565  1.1  christos 
   6566  1.1  christos   /* Returns TRUE iff seg1 starts after the end of seg2.  */
   6567  1.1  christos #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)			\
   6568  1.1  christos   (seg1->field >= SEGMENT_END (seg2, seg2->field))
   6569  1.1  christos 
   6570  1.1  christos   /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
   6571  1.1  christos      their VMA address ranges and their LMA address ranges overlap.
   6572  1.1  christos      It is possible to have overlapping VMA ranges without overlapping LMA
   6573  1.1  christos      ranges.  RedBoot images for example can have both .data and .bss mapped
   6574  1.1  christos      to the same VMA range, but with the .data section mapped to a different
   6575  1.1  christos      LMA.  */
   6576  1.1  christos #define SEGMENT_OVERLAPS(seg1, seg2)					\
   6577  1.1  christos   (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)			\
   6578  1.1  christos 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))			\
   6579  1.1  christos    && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)			\
   6580  1.1  christos 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
   6581  1.1  christos 
   6582  1.1  christos   /* Initialise the segment mark field.  */
   6583  1.1  christos   for (section = ibfd->sections; section != NULL; section = section->next)
   6584  1.1  christos     section->segment_mark = FALSE;
   6585  1.1  christos 
   6586  1.1  christos   /* The Solaris linker creates program headers in which all the
   6587  1.1  christos      p_paddr fields are zero.  When we try to objcopy or strip such a
   6588  1.1  christos      file, we get confused.  Check for this case, and if we find it
   6589  1.1  christos      don't set the p_paddr_valid fields.  */
   6590  1.1  christos   p_paddr_valid = FALSE;
   6591  1.1  christos   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   6592  1.1  christos        i < num_segments;
   6593  1.1  christos        i++, segment++)
   6594  1.1  christos     if (segment->p_paddr != 0)
   6595  1.1  christos       {
   6596  1.1  christos 	p_paddr_valid = TRUE;
   6597  1.1  christos 	break;
   6598  1.1  christos       }
   6599  1.1  christos 
   6600  1.1  christos   /* Scan through the segments specified in the program header
   6601  1.1  christos      of the input BFD.  For this first scan we look for overlaps
   6602  1.1  christos      in the loadable segments.  These can be created by weird
   6603  1.1  christos      parameters to objcopy.  Also, fix some solaris weirdness.  */
   6604  1.1  christos   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   6605  1.1  christos        i < num_segments;
   6606  1.1  christos        i++, segment++)
   6607  1.1  christos     {
   6608  1.1  christos       unsigned int j;
   6609  1.1  christos       Elf_Internal_Phdr *segment2;
   6610  1.1  christos 
   6611  1.1  christos       if (segment->p_type == PT_INTERP)
   6612  1.1  christos 	for (section = ibfd->sections; section; section = section->next)
   6613  1.1  christos 	  if (IS_SOLARIS_PT_INTERP (segment, section))
   6614  1.1  christos 	    {
   6615  1.1  christos 	      /* Mininal change so that the normal section to segment
   6616  1.1  christos 		 assignment code will work.  */
   6617  1.1  christos 	      segment->p_vaddr = section->vma;
   6618  1.1  christos 	      break;
   6619  1.1  christos 	    }
   6620  1.1  christos 
   6621  1.1  christos       if (segment->p_type != PT_LOAD)
   6622  1.1  christos 	{
   6623  1.1  christos 	  /* Remove PT_GNU_RELRO segment.  */
   6624  1.1  christos 	  if (segment->p_type == PT_GNU_RELRO)
   6625  1.1  christos 	    segment->p_type = PT_NULL;
   6626  1.1  christos 	  continue;
   6627  1.1  christos 	}
   6628  1.1  christos 
   6629  1.1  christos       /* Determine if this segment overlaps any previous segments.  */
   6630  1.1  christos       for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
   6631  1.1  christos 	{
   6632  1.1  christos 	  bfd_signed_vma extra_length;
   6633  1.1  christos 
   6634  1.1  christos 	  if (segment2->p_type != PT_LOAD
   6635  1.1  christos 	      || !SEGMENT_OVERLAPS (segment, segment2))
   6636  1.1  christos 	    continue;
   6637  1.1  christos 
   6638  1.1  christos 	  /* Merge the two segments together.  */
   6639  1.1  christos 	  if (segment2->p_vaddr < segment->p_vaddr)
   6640  1.1  christos 	    {
   6641  1.1  christos 	      /* Extend SEGMENT2 to include SEGMENT and then delete
   6642  1.1  christos 		 SEGMENT.  */
   6643  1.1  christos 	      extra_length = (SEGMENT_END (segment, segment->p_vaddr)
   6644  1.1  christos 			      - SEGMENT_END (segment2, segment2->p_vaddr));
   6645  1.1  christos 
   6646  1.1  christos 	      if (extra_length > 0)
   6647  1.1  christos 		{
   6648  1.1  christos 		  segment2->p_memsz += extra_length;
   6649  1.1  christos 		  segment2->p_filesz += extra_length;
   6650  1.1  christos 		}
   6651  1.1  christos 
   6652  1.1  christos 	      segment->p_type = PT_NULL;
   6653  1.1  christos 
   6654  1.1  christos 	      /* Since we have deleted P we must restart the outer loop.  */
   6655  1.1  christos 	      i = 0;
   6656  1.1  christos 	      segment = elf_tdata (ibfd)->phdr;
   6657  1.1  christos 	      break;
   6658  1.1  christos 	    }
   6659  1.1  christos 	  else
   6660  1.1  christos 	    {
   6661  1.1  christos 	      /* Extend SEGMENT to include SEGMENT2 and then delete
   6662  1.1  christos 		 SEGMENT2.  */
   6663  1.1  christos 	      extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
   6664  1.1  christos 			      - SEGMENT_END (segment, segment->p_vaddr));
   6665  1.1  christos 
   6666  1.1  christos 	      if (extra_length > 0)
   6667  1.1  christos 		{
   6668  1.1  christos 		  segment->p_memsz += extra_length;
   6669  1.1  christos 		  segment->p_filesz += extra_length;
   6670  1.1  christos 		}
   6671  1.1  christos 
   6672  1.1  christos 	      segment2->p_type = PT_NULL;
   6673  1.1  christos 	    }
   6674  1.1  christos 	}
   6675  1.1  christos     }
   6676  1.1  christos 
   6677  1.1  christos   /* The second scan attempts to assign sections to segments.  */
   6678  1.1  christos   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   6679  1.1  christos        i < num_segments;
   6680  1.1  christos        i++, segment++)
   6681  1.1  christos     {
   6682  1.1  christos       unsigned int section_count;
   6683  1.1  christos       asection **sections;
   6684  1.1  christos       asection *output_section;
   6685  1.1  christos       unsigned int isec;
   6686  1.1  christos       bfd_vma matching_lma;
   6687  1.1  christos       bfd_vma suggested_lma;
   6688  1.1  christos       unsigned int j;
   6689  1.1  christos       bfd_size_type amt;
   6690  1.1  christos       asection *first_section;
   6691  1.1  christos       bfd_boolean first_matching_lma;
   6692  1.1  christos       bfd_boolean first_suggested_lma;
   6693  1.1  christos 
   6694  1.1  christos       if (segment->p_type == PT_NULL)
   6695  1.1  christos 	continue;
   6696  1.1  christos 
   6697  1.1  christos       first_section = NULL;
   6698  1.1  christos       /* Compute how many sections might be placed into this segment.  */
   6699  1.1  christos       for (section = ibfd->sections, section_count = 0;
   6700  1.1  christos 	   section != NULL;
   6701  1.1  christos 	   section = section->next)
   6702  1.1  christos 	{
   6703  1.1  christos 	  /* Find the first section in the input segment, which may be
   6704  1.1  christos 	     removed from the corresponding output segment.   */
   6705  1.1  christos 	  if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
   6706  1.1  christos 	    {
   6707  1.1  christos 	      if (first_section == NULL)
   6708  1.1  christos 		first_section = section;
   6709  1.1  christos 	      if (section->output_section != NULL)
   6710  1.1  christos 		++section_count;
   6711  1.1  christos 	    }
   6712  1.1  christos 	}
   6713  1.1  christos 
   6714  1.1  christos       /* Allocate a segment map big enough to contain
   6715  1.1  christos 	 all of the sections we have selected.  */
   6716  1.1  christos       amt = sizeof (struct elf_segment_map);
   6717  1.1  christos       amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
   6718  1.1  christos       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   6719  1.1  christos       if (map == NULL)
   6720  1.1  christos 	return FALSE;
   6721  1.1  christos 
   6722  1.1  christos       /* Initialise the fields of the segment map.  Default to
   6723  1.1  christos 	 using the physical address of the segment in the input BFD.  */
   6724  1.1  christos       map->next = NULL;
   6725  1.1  christos       map->p_type = segment->p_type;
   6726  1.1  christos       map->p_flags = segment->p_flags;
   6727  1.1  christos       map->p_flags_valid = 1;
   6728  1.1  christos 
   6729  1.1  christos       /* If the first section in the input segment is removed, there is
   6730  1.1  christos 	 no need to preserve segment physical address in the corresponding
   6731  1.1  christos 	 output segment.  */
   6732  1.1  christos       if (!first_section || first_section->output_section != NULL)
   6733  1.1  christos 	{
   6734  1.1  christos 	  map->p_paddr = segment->p_paddr;
   6735  1.1  christos 	  map->p_paddr_valid = p_paddr_valid;
   6736  1.1  christos 	}
   6737  1.1  christos 
   6738  1.1  christos       /* Determine if this segment contains the ELF file header
   6739  1.1  christos 	 and if it contains the program headers themselves.  */
   6740  1.1  christos       map->includes_filehdr = (segment->p_offset == 0
   6741  1.1  christos 			       && segment->p_filesz >= iehdr->e_ehsize);
   6742  1.1  christos       map->includes_phdrs = 0;
   6743  1.1  christos 
   6744  1.1  christos       if (!phdr_included || segment->p_type != PT_LOAD)
   6745  1.1  christos 	{
   6746  1.1  christos 	  map->includes_phdrs =
   6747  1.1  christos 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   6748  1.1  christos 	     && (segment->p_offset + segment->p_filesz
   6749  1.1  christos 		 >= ((bfd_vma) iehdr->e_phoff
   6750  1.1  christos 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   6751  1.1  christos 
   6752  1.1  christos 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   6753  1.1  christos 	    phdr_included = TRUE;
   6754  1.1  christos 	}
   6755  1.1  christos 
   6756  1.1  christos       if (section_count == 0)
   6757  1.1  christos 	{
   6758  1.1  christos 	  /* Special segments, such as the PT_PHDR segment, may contain
   6759  1.9  christos 	     no sections, but ordinary, loadable segments should contain
   6760  1.9  christos 	     something.  They are allowed by the ELF spec however, so only
   6761  1.9  christos 	     a warning is produced.
   6762  1.9  christos 	     There is however the valid use case of embedded systems which
   6763  1.9  christos 	     have segments with p_filesz of 0 and a p_memsz > 0 to initialize
   6764  1.9  christos 	     flash memory with zeros.  No warning is shown for that case.  */
   6765  1.9  christos 	  if (segment->p_type == PT_LOAD
   6766  1.9  christos 	      && (segment->p_filesz > 0 || segment->p_memsz == 0))
   6767  1.9  christos 	    /* xgettext:c-format */
   6768  1.9  christos 	    _bfd_error_handler (_("\
   6769  1.1  christos %B: warning: Empty loadable segment detected at vaddr=0x%.8x, is this intentional ?"),
   6770  1.1  christos 				ibfd, segment->p_vaddr);
   6771  1.1  christos 
   6772  1.1  christos 	  map->count = 0;
   6773  1.1  christos 	  *pointer_to_map = map;
   6774  1.1  christos 	  pointer_to_map = &map->next;
   6775  1.1  christos 
   6776  1.1  christos 	  continue;
   6777  1.1  christos 	}
   6778  1.1  christos 
   6779  1.1  christos       /* Now scan the sections in the input BFD again and attempt
   6780  1.1  christos 	 to add their corresponding output sections to the segment map.
   6781  1.1  christos 	 The problem here is how to handle an output section which has
   6782  1.1  christos 	 been moved (ie had its LMA changed).  There are four possibilities:
   6783  1.1  christos 
   6784  1.1  christos 	 1. None of the sections have been moved.
   6785  1.1  christos 	    In this case we can continue to use the segment LMA from the
   6786  1.1  christos 	    input BFD.
   6787  1.1  christos 
   6788  1.1  christos 	 2. All of the sections have been moved by the same amount.
   6789  1.1  christos 	    In this case we can change the segment's LMA to match the LMA
   6790  1.1  christos 	    of the first section.
   6791  1.1  christos 
   6792  1.1  christos 	 3. Some of the sections have been moved, others have not.
   6793  1.1  christos 	    In this case those sections which have not been moved can be
   6794  1.1  christos 	    placed in the current segment which will have to have its size,
   6795  1.1  christos 	    and possibly its LMA changed, and a new segment or segments will
   6796  1.1  christos 	    have to be created to contain the other sections.
   6797  1.1  christos 
   6798  1.1  christos 	 4. The sections have been moved, but not by the same amount.
   6799  1.1  christos 	    In this case we can change the segment's LMA to match the LMA
   6800  1.1  christos 	    of the first section and we will have to create a new segment
   6801  1.1  christos 	    or segments to contain the other sections.
   6802  1.1  christos 
   6803  1.1  christos 	 In order to save time, we allocate an array to hold the section
   6804  1.1  christos 	 pointers that we are interested in.  As these sections get assigned
   6805  1.1  christos 	 to a segment, they are removed from this array.  */
   6806  1.1  christos 
   6807  1.1  christos       sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
   6808  1.1  christos       if (sections == NULL)
   6809  1.1  christos 	return FALSE;
   6810  1.1  christos 
   6811  1.1  christos       /* Step One: Scan for segment vs section LMA conflicts.
   6812  1.1  christos 	 Also add the sections to the section array allocated above.
   6813  1.1  christos 	 Also add the sections to the current segment.  In the common
   6814  1.1  christos 	 case, where the sections have not been moved, this means that
   6815  1.1  christos 	 we have completely filled the segment, and there is nothing
   6816  1.1  christos 	 more to do.  */
   6817  1.1  christos       isec = 0;
   6818  1.1  christos       matching_lma = 0;
   6819  1.1  christos       suggested_lma = 0;
   6820  1.1  christos       first_matching_lma = TRUE;
   6821  1.8  christos       first_suggested_lma = TRUE;
   6822  1.1  christos 
   6823  1.1  christos       for (section = first_section, j = 0;
   6824  1.1  christos 	   section != NULL;
   6825  1.1  christos 	   section = section->next)
   6826  1.1  christos 	{
   6827  1.1  christos 	  if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
   6828  1.1  christos 	    {
   6829  1.1  christos 	      output_section = section->output_section;
   6830  1.1  christos 
   6831  1.1  christos 	      sections[j++] = section;
   6832  1.1  christos 
   6833  1.1  christos 	      /* The Solaris native linker always sets p_paddr to 0.
   6834  1.1  christos 		 We try to catch that case here, and set it to the
   6835  1.1  christos 		 correct value.  Note - some backends require that
   6836  1.1  christos 		 p_paddr be left as zero.  */
   6837  1.1  christos 	      if (!p_paddr_valid
   6838  1.1  christos 		  && segment->p_vaddr != 0
   6839  1.1  christos 		  && !bed->want_p_paddr_set_to_zero
   6840  1.1  christos 		  && isec == 0
   6841  1.1  christos 		  && output_section->lma != 0
   6842  1.1  christos 		  && output_section->vma == (segment->p_vaddr
   6843  1.1  christos 					     + (map->includes_filehdr
   6844  1.1  christos 						? iehdr->e_ehsize
   6845  1.1  christos 						: 0)
   6846  1.1  christos 					     + (map->includes_phdrs
   6847  1.1  christos 						? (iehdr->e_phnum
   6848  1.1  christos 						   * iehdr->e_phentsize)
   6849  1.1  christos 						: 0)))
   6850  1.1  christos 		map->p_paddr = segment->p_vaddr;
   6851  1.1  christos 
   6852  1.1  christos 	      /* Match up the physical address of the segment with the
   6853  1.1  christos 		 LMA address of the output section.  */
   6854  1.1  christos 	      if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
   6855  1.1  christos 		  || IS_COREFILE_NOTE (segment, section)
   6856  1.1  christos 		  || (bed->want_p_paddr_set_to_zero
   6857  1.1  christos 		      && IS_CONTAINED_BY_VMA (output_section, segment)))
   6858  1.1  christos 		{
   6859  1.1  christos 		  if (first_matching_lma || output_section->lma < matching_lma)
   6860  1.1  christos 		    {
   6861  1.1  christos 		      matching_lma = output_section->lma;
   6862  1.1  christos 		      first_matching_lma = FALSE;
   6863  1.1  christos 		    }
   6864  1.1  christos 
   6865  1.1  christos 		  /* We assume that if the section fits within the segment
   6866  1.1  christos 		     then it does not overlap any other section within that
   6867  1.1  christos 		     segment.  */
   6868  1.1  christos 		  map->sections[isec++] = output_section;
   6869  1.1  christos 		}
   6870  1.1  christos 	      else if (first_suggested_lma)
   6871  1.1  christos 		{
   6872  1.1  christos 		  suggested_lma = output_section->lma;
   6873  1.1  christos 		  first_suggested_lma = FALSE;
   6874  1.1  christos 		}
   6875  1.1  christos 
   6876  1.1  christos 	      if (j == section_count)
   6877  1.1  christos 		break;
   6878  1.1  christos 	    }
   6879  1.1  christos 	}
   6880  1.1  christos 
   6881  1.1  christos       BFD_ASSERT (j == section_count);
   6882  1.1  christos 
   6883  1.1  christos       /* Step Two: Adjust the physical address of the current segment,
   6884  1.1  christos 	 if necessary.  */
   6885  1.1  christos       if (isec == section_count)
   6886  1.1  christos 	{
   6887  1.1  christos 	  /* All of the sections fitted within the segment as currently
   6888  1.1  christos 	     specified.  This is the default case.  Add the segment to
   6889  1.1  christos 	     the list of built segments and carry on to process the next
   6890  1.1  christos 	     program header in the input BFD.  */
   6891  1.1  christos 	  map->count = section_count;
   6892  1.1  christos 	  *pointer_to_map = map;
   6893  1.1  christos 	  pointer_to_map = &map->next;
   6894  1.1  christos 
   6895  1.1  christos 	  if (p_paddr_valid
   6896  1.1  christos 	      && !bed->want_p_paddr_set_to_zero
   6897  1.1  christos 	      && matching_lma != map->p_paddr
   6898  1.1  christos 	      && !map->includes_filehdr
   6899  1.1  christos 	      && !map->includes_phdrs)
   6900  1.1  christos 	    /* There is some padding before the first section in the
   6901  1.1  christos 	       segment.  So, we must account for that in the output
   6902  1.1  christos 	       segment's vma.  */
   6903  1.1  christos 	    map->p_vaddr_offset = matching_lma - map->p_paddr;
   6904  1.1  christos 
   6905  1.1  christos 	  free (sections);
   6906  1.1  christos 	  continue;
   6907  1.1  christos 	}
   6908  1.1  christos       else
   6909  1.1  christos 	{
   6910  1.1  christos 	  if (!first_matching_lma)
   6911  1.1  christos 	    {
   6912  1.1  christos 	      /* At least one section fits inside the current segment.
   6913  1.1  christos 		 Keep it, but modify its physical address to match the
   6914  1.1  christos 		 LMA of the first section that fitted.  */
   6915  1.1  christos 	      map->p_paddr = matching_lma;
   6916  1.1  christos 	    }
   6917  1.1  christos 	  else
   6918  1.1  christos 	    {
   6919  1.1  christos 	      /* None of the sections fitted inside the current segment.
   6920  1.1  christos 		 Change the current segment's physical address to match
   6921  1.1  christos 		 the LMA of the first section.  */
   6922  1.1  christos 	      map->p_paddr = suggested_lma;
   6923  1.1  christos 	    }
   6924  1.1  christos 
   6925  1.1  christos 	  /* Offset the segment physical address from the lma
   6926  1.1  christos 	     to allow for space taken up by elf headers.  */
   6927  1.1  christos 	  if (map->includes_filehdr)
   6928  1.1  christos 	    {
   6929  1.1  christos 	      if (map->p_paddr >= iehdr->e_ehsize)
   6930  1.1  christos 		map->p_paddr -= iehdr->e_ehsize;
   6931  1.1  christos 	      else
   6932  1.1  christos 		{
   6933  1.1  christos 		  map->includes_filehdr = FALSE;
   6934  1.1  christos 		  map->includes_phdrs = FALSE;
   6935  1.1  christos 		}
   6936  1.1  christos 	    }
   6937  1.1  christos 
   6938  1.1  christos 	  if (map->includes_phdrs)
   6939  1.1  christos 	    {
   6940  1.1  christos 	      if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
   6941  1.1  christos 		{
   6942  1.1  christos 		  map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
   6943  1.1  christos 
   6944  1.1  christos 		  /* iehdr->e_phnum is just an estimate of the number
   6945  1.1  christos 		     of program headers that we will need.  Make a note
   6946  1.1  christos 		     here of the number we used and the segment we chose
   6947  1.1  christos 		     to hold these headers, so that we can adjust the
   6948  1.1  christos 		     offset when we know the correct value.  */
   6949  1.1  christos 		  phdr_adjust_num = iehdr->e_phnum;
   6950  1.1  christos 		  phdr_adjust_seg = map;
   6951  1.1  christos 		}
   6952  1.1  christos 	      else
   6953  1.1  christos 		map->includes_phdrs = FALSE;
   6954  1.1  christos 	    }
   6955  1.1  christos 	}
   6956  1.1  christos 
   6957  1.1  christos       /* Step Three: Loop over the sections again, this time assigning
   6958  1.1  christos 	 those that fit to the current segment and removing them from the
   6959  1.1  christos 	 sections array; but making sure not to leave large gaps.  Once all
   6960  1.1  christos 	 possible sections have been assigned to the current segment it is
   6961  1.1  christos 	 added to the list of built segments and if sections still remain
   6962  1.1  christos 	 to be assigned, a new segment is constructed before repeating
   6963  1.1  christos 	 the loop.  */
   6964  1.1  christos       isec = 0;
   6965  1.1  christos       do
   6966  1.1  christos 	{
   6967  1.1  christos 	  map->count = 0;
   6968  1.1  christos 	  suggested_lma = 0;
   6969  1.1  christos 	  first_suggested_lma = TRUE;
   6970  1.1  christos 
   6971  1.1  christos 	  /* Fill the current segment with sections that fit.  */
   6972  1.1  christos 	  for (j = 0; j < section_count; j++)
   6973  1.1  christos 	    {
   6974  1.1  christos 	      section = sections[j];
   6975  1.1  christos 
   6976  1.1  christos 	      if (section == NULL)
   6977  1.1  christos 		continue;
   6978  1.1  christos 
   6979  1.1  christos 	      output_section = section->output_section;
   6980  1.1  christos 
   6981  1.1  christos 	      BFD_ASSERT (output_section != NULL);
   6982  1.1  christos 
   6983  1.1  christos 	      if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
   6984  1.1  christos 		  || IS_COREFILE_NOTE (segment, section))
   6985  1.1  christos 		{
   6986  1.1  christos 		  if (map->count == 0)
   6987  1.1  christos 		    {
   6988  1.1  christos 		      /* If the first section in a segment does not start at
   6989  1.1  christos 			 the beginning of the segment, then something is
   6990  1.1  christos 			 wrong.  */
   6991  1.1  christos 		      if (output_section->lma
   6992  1.1  christos 			  != (map->p_paddr
   6993  1.1  christos 			      + (map->includes_filehdr ? iehdr->e_ehsize : 0)
   6994  1.1  christos 			      + (map->includes_phdrs
   6995  1.1  christos 				 ? iehdr->e_phnum * iehdr->e_phentsize
   6996  1.1  christos 				 : 0)))
   6997  1.1  christos 			abort ();
   6998  1.1  christos 		    }
   6999  1.1  christos 		  else
   7000  1.1  christos 		    {
   7001  1.1  christos 		      asection *prev_sec;
   7002  1.1  christos 
   7003  1.1  christos 		      prev_sec = map->sections[map->count - 1];
   7004  1.1  christos 
   7005  1.1  christos 		      /* If the gap between the end of the previous section
   7006  1.1  christos 			 and the start of this section is more than
   7007  1.1  christos 			 maxpagesize then we need to start a new segment.  */
   7008  1.1  christos 		      if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
   7009  1.1  christos 				      maxpagesize)
   7010  1.1  christos 			   < BFD_ALIGN (output_section->lma, maxpagesize))
   7011  1.1  christos 			  || (prev_sec->lma + prev_sec->size
   7012  1.1  christos 			      > output_section->lma))
   7013  1.1  christos 			{
   7014  1.1  christos 			  if (first_suggested_lma)
   7015  1.1  christos 			    {
   7016  1.1  christos 			      suggested_lma = output_section->lma;
   7017  1.1  christos 			      first_suggested_lma = FALSE;
   7018  1.1  christos 			    }
   7019  1.1  christos 
   7020  1.1  christos 			  continue;
   7021  1.1  christos 			}
   7022  1.1  christos 		    }
   7023  1.1  christos 
   7024  1.1  christos 		  map->sections[map->count++] = output_section;
   7025  1.1  christos 		  ++isec;
   7026  1.1  christos 		  sections[j] = NULL;
   7027  1.1  christos 		  section->segment_mark = TRUE;
   7028  1.1  christos 		}
   7029  1.1  christos 	      else if (first_suggested_lma)
   7030  1.1  christos 		{
   7031  1.1  christos 		  suggested_lma = output_section->lma;
   7032  1.1  christos 		  first_suggested_lma = FALSE;
   7033  1.1  christos 		}
   7034  1.1  christos 	    }
   7035  1.1  christos 
   7036  1.1  christos 	  BFD_ASSERT (map->count > 0);
   7037  1.1  christos 
   7038  1.1  christos 	  /* Add the current segment to the list of built segments.  */
   7039  1.1  christos 	  *pointer_to_map = map;
   7040  1.1  christos 	  pointer_to_map = &map->next;
   7041  1.1  christos 
   7042  1.1  christos 	  if (isec < section_count)
   7043  1.1  christos 	    {
   7044  1.1  christos 	      /* We still have not allocated all of the sections to
   7045  1.1  christos 		 segments.  Create a new segment here, initialise it
   7046  1.1  christos 		 and carry on looping.  */
   7047  1.3  christos 	      amt = sizeof (struct elf_segment_map);
   7048  1.1  christos 	      amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
   7049  1.1  christos 	      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7050  1.1  christos 	      if (map == NULL)
   7051  1.1  christos 		{
   7052  1.1  christos 		  free (sections);
   7053  1.1  christos 		  return FALSE;
   7054  1.1  christos 		}
   7055  1.1  christos 
   7056  1.1  christos 	      /* Initialise the fields of the segment map.  Set the physical
   7057  1.1  christos 		 physical address to the LMA of the first section that has
   7058  1.1  christos 		 not yet been assigned.  */
   7059  1.1  christos 	      map->next = NULL;
   7060  1.1  christos 	      map->p_type = segment->p_type;
   7061  1.1  christos 	      map->p_flags = segment->p_flags;
   7062  1.1  christos 	      map->p_flags_valid = 1;
   7063  1.1  christos 	      map->p_paddr = suggested_lma;
   7064  1.1  christos 	      map->p_paddr_valid = p_paddr_valid;
   7065  1.1  christos 	      map->includes_filehdr = 0;
   7066  1.1  christos 	      map->includes_phdrs = 0;
   7067  1.1  christos 	    }
   7068  1.1  christos 	}
   7069  1.1  christos       while (isec < section_count);
   7070  1.1  christos 
   7071  1.1  christos       free (sections);
   7072  1.3  christos     }
   7073  1.1  christos 
   7074  1.1  christos   elf_seg_map (obfd) = map_first;
   7075  1.1  christos 
   7076  1.1  christos   /* If we had to estimate the number of program headers that were
   7077  1.1  christos      going to be needed, then check our estimate now and adjust
   7078  1.1  christos      the offset if necessary.  */
   7079  1.1  christos   if (phdr_adjust_seg != NULL)
   7080  1.1  christos     {
   7081  1.1  christos       unsigned int count;
   7082  1.1  christos 
   7083  1.1  christos       for (count = 0, map = map_first; map != NULL; map = map->next)
   7084  1.1  christos 	count++;
   7085  1.1  christos 
   7086  1.1  christos       if (count > phdr_adjust_num)
   7087  1.1  christos 	phdr_adjust_seg->p_paddr
   7088  1.1  christos 	  -= (count - phdr_adjust_num) * iehdr->e_phentsize;
   7089  1.1  christos     }
   7090  1.1  christos 
   7091  1.1  christos #undef SEGMENT_END
   7092  1.1  christos #undef SECTION_SIZE
   7093  1.1  christos #undef IS_CONTAINED_BY_VMA
   7094  1.1  christos #undef IS_CONTAINED_BY_LMA
   7095  1.1  christos #undef IS_NOTE
   7096  1.1  christos #undef IS_COREFILE_NOTE
   7097  1.1  christos #undef IS_SOLARIS_PT_INTERP
   7098  1.1  christos #undef IS_SECTION_IN_INPUT_SEGMENT
   7099  1.1  christos #undef INCLUDE_SECTION_IN_SEGMENT
   7100  1.1  christos #undef SEGMENT_AFTER_SEGMENT
   7101  1.1  christos #undef SEGMENT_OVERLAPS
   7102  1.1  christos   return TRUE;
   7103  1.1  christos }
   7104  1.1  christos 
   7105  1.1  christos /* Copy ELF program header information.  */
   7106  1.1  christos 
   7107  1.1  christos static bfd_boolean
   7108  1.1  christos copy_elf_program_header (bfd *ibfd, bfd *obfd)
   7109  1.1  christos {
   7110  1.1  christos   Elf_Internal_Ehdr *iehdr;
   7111  1.1  christos   struct elf_segment_map *map;
   7112  1.1  christos   struct elf_segment_map *map_first;
   7113  1.1  christos   struct elf_segment_map **pointer_to_map;
   7114  1.1  christos   Elf_Internal_Phdr *segment;
   7115  1.1  christos   unsigned int i;
   7116  1.1  christos   unsigned int num_segments;
   7117  1.1  christos   bfd_boolean phdr_included = FALSE;
   7118  1.1  christos   bfd_boolean p_paddr_valid;
   7119  1.1  christos 
   7120  1.1  christos   iehdr = elf_elfheader (ibfd);
   7121  1.1  christos 
   7122  1.1  christos   map_first = NULL;
   7123  1.1  christos   pointer_to_map = &map_first;
   7124  1.1  christos 
   7125  1.1  christos   /* If all the segment p_paddr fields are zero, don't set
   7126  1.1  christos      map->p_paddr_valid.  */
   7127  1.1  christos   p_paddr_valid = FALSE;
   7128  1.1  christos   num_segments = elf_elfheader (ibfd)->e_phnum;
   7129  1.1  christos   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7130  1.1  christos        i < num_segments;
   7131  1.1  christos        i++, segment++)
   7132  1.1  christos     if (segment->p_paddr != 0)
   7133  1.1  christos       {
   7134  1.1  christos 	p_paddr_valid = TRUE;
   7135  1.1  christos 	break;
   7136  1.1  christos       }
   7137  1.1  christos 
   7138  1.1  christos   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7139  1.1  christos        i < num_segments;
   7140  1.1  christos        i++, segment++)
   7141  1.1  christos     {
   7142  1.1  christos       asection *section;
   7143  1.1  christos       unsigned int section_count;
   7144  1.1  christos       bfd_size_type amt;
   7145  1.1  christos       Elf_Internal_Shdr *this_hdr;
   7146  1.1  christos       asection *first_section = NULL;
   7147  1.1  christos       asection *lowest_section;
   7148  1.1  christos 
   7149  1.1  christos       /* Compute how many sections are in this segment.  */
   7150  1.1  christos       for (section = ibfd->sections, section_count = 0;
   7151  1.1  christos 	   section != NULL;
   7152  1.1  christos 	   section = section->next)
   7153  1.1  christos 	{
   7154  1.1  christos 	  this_hdr = &(elf_section_data(section)->this_hdr);
   7155  1.1  christos 	  if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   7156  1.1  christos 	    {
   7157  1.1  christos 	      if (first_section == NULL)
   7158  1.1  christos 		first_section = section;
   7159  1.1  christos 	      section_count++;
   7160  1.1  christos 	    }
   7161  1.1  christos 	}
   7162  1.1  christos 
   7163  1.1  christos       /* Allocate a segment map big enough to contain
   7164  1.1  christos 	 all of the sections we have selected.  */
   7165  1.1  christos       amt = sizeof (struct elf_segment_map);
   7166  1.1  christos       if (section_count != 0)
   7167  1.1  christos 	amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
   7168  1.1  christos       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7169  1.1  christos       if (map == NULL)
   7170  1.1  christos 	return FALSE;
   7171  1.1  christos 
   7172  1.1  christos       /* Initialize the fields of the output segment map with the
   7173  1.1  christos 	 input segment.  */
   7174  1.1  christos       map->next = NULL;
   7175  1.1  christos       map->p_type = segment->p_type;
   7176  1.1  christos       map->p_flags = segment->p_flags;
   7177  1.1  christos       map->p_flags_valid = 1;
   7178  1.1  christos       map->p_paddr = segment->p_paddr;
   7179  1.1  christos       map->p_paddr_valid = p_paddr_valid;
   7180  1.1  christos       map->p_align = segment->p_align;
   7181  1.1  christos       map->p_align_valid = 1;
   7182  1.3  christos       map->p_vaddr_offset = 0;
   7183  1.3  christos 
   7184  1.1  christos       if (map->p_type == PT_GNU_RELRO
   7185  1.1  christos 	  || map->p_type == PT_GNU_STACK)
   7186  1.1  christos 	{
   7187  1.1  christos 	  /* The PT_GNU_RELRO segment may contain the first a few
   7188  1.3  christos 	     bytes in the .got.plt section even if the whole .got.plt
   7189  1.3  christos 	     section isn't in the PT_GNU_RELRO segment.  We won't
   7190  1.3  christos 	     change the size of the PT_GNU_RELRO segment.
   7191  1.1  christos 	     Similarly, PT_GNU_STACK size is significant on uclinux
   7192  1.1  christos 	     systems.    */
   7193  1.1  christos 	  map->p_size = segment->p_memsz;
   7194  1.1  christos 	  map->p_size_valid = 1;
   7195  1.1  christos 	}
   7196  1.1  christos 
   7197  1.1  christos       /* Determine if this segment contains the ELF file header
   7198  1.1  christos 	 and if it contains the program headers themselves.  */
   7199  1.1  christos       map->includes_filehdr = (segment->p_offset == 0
   7200  1.1  christos 			       && segment->p_filesz >= iehdr->e_ehsize);
   7201  1.1  christos 
   7202  1.1  christos       map->includes_phdrs = 0;
   7203  1.1  christos       if (! phdr_included || segment->p_type != PT_LOAD)
   7204  1.1  christos 	{
   7205  1.1  christos 	  map->includes_phdrs =
   7206  1.1  christos 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   7207  1.1  christos 	     && (segment->p_offset + segment->p_filesz
   7208  1.1  christos 		 >= ((bfd_vma) iehdr->e_phoff
   7209  1.1  christos 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   7210  1.1  christos 
   7211  1.1  christos 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   7212  1.1  christos 	    phdr_included = TRUE;
   7213  1.5  christos 	}
   7214  1.1  christos 
   7215  1.1  christos       lowest_section = NULL;
   7216  1.1  christos       if (section_count != 0)
   7217  1.1  christos 	{
   7218  1.1  christos 	  unsigned int isec = 0;
   7219  1.1  christos 
   7220  1.1  christos 	  for (section = first_section;
   7221  1.1  christos 	       section != NULL;
   7222  1.1  christos 	       section = section->next)
   7223  1.1  christos 	    {
   7224  1.1  christos 	      this_hdr = &(elf_section_data(section)->this_hdr);
   7225  1.1  christos 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   7226  1.1  christos 		{
   7227  1.1  christos 		  map->sections[isec++] = section->output_section;
   7228  1.1  christos 		  if ((section->flags & SEC_ALLOC) != 0)
   7229  1.1  christos 		    {
   7230  1.5  christos 		      bfd_vma seg_off;
   7231  1.5  christos 
   7232  1.4  christos 		      if (lowest_section == NULL
   7233  1.4  christos 			  || section->lma < lowest_section->lma)
   7234  1.1  christos 			lowest_section = section;
   7235  1.1  christos 
   7236  1.1  christos 		      /* Section lmas are set up from PT_LOAD header
   7237  1.1  christos 			 p_paddr in _bfd_elf_make_section_from_shdr.
   7238  1.1  christos 			 If this header has a p_paddr that disagrees
   7239  1.1  christos 			 with the section lma, flag the p_paddr as
   7240  1.1  christos 			 invalid.  */
   7241  1.1  christos 		      if ((section->flags & SEC_LOAD) != 0)
   7242  1.1  christos 			seg_off = this_hdr->sh_offset - segment->p_offset;
   7243  1.1  christos 		      else
   7244  1.1  christos 			seg_off = this_hdr->sh_addr - segment->p_vaddr;
   7245  1.1  christos 		      if (section->lma - segment->p_paddr != seg_off)
   7246  1.1  christos 			map->p_paddr_valid = FALSE;
   7247  1.1  christos 		    }
   7248  1.1  christos 		  if (isec == section_count)
   7249  1.1  christos 		    break;
   7250  1.1  christos 		}
   7251  1.1  christos 	    }
   7252  1.1  christos 	}
   7253  1.1  christos 
   7254  1.1  christos       if (map->includes_filehdr && lowest_section != NULL)
   7255  1.3  christos 	/* We need to keep the space used by the headers fixed.  */
   7256  1.1  christos 	map->header_size = lowest_section->vma - segment->p_vaddr;
   7257  1.1  christos 
   7258  1.1  christos       if (!map->includes_phdrs
   7259  1.1  christos 	  && !map->includes_filehdr
   7260  1.1  christos 	  && map->p_paddr_valid)
   7261  1.1  christos 	/* There is some other padding before the first section.  */
   7262  1.1  christos 	map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
   7263  1.1  christos 			       - segment->p_paddr);
   7264  1.1  christos 
   7265  1.1  christos       map->count = section_count;
   7266  1.1  christos       *pointer_to_map = map;
   7267  1.1  christos       pointer_to_map = &map->next;
   7268  1.3  christos     }
   7269  1.1  christos 
   7270  1.1  christos   elf_seg_map (obfd) = map_first;
   7271  1.1  christos   return TRUE;
   7272  1.1  christos }
   7273  1.1  christos 
   7274  1.1  christos /* Copy private BFD data.  This copies or rewrites ELF program header
   7275  1.1  christos    information.  */
   7276  1.1  christos 
   7277  1.1  christos static bfd_boolean
   7278  1.1  christos copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   7279  1.1  christos {
   7280  1.1  christos   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   7281  1.1  christos       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   7282  1.1  christos     return TRUE;
   7283  1.1  christos 
   7284  1.1  christos   if (elf_tdata (ibfd)->phdr == NULL)
   7285  1.1  christos     return TRUE;
   7286  1.1  christos 
   7287  1.1  christos   if (ibfd->xvec == obfd->xvec)
   7288  1.1  christos     {
   7289  1.1  christos       /* Check to see if any sections in the input BFD
   7290  1.1  christos 	 covered by ELF program header have changed.  */
   7291  1.1  christos       Elf_Internal_Phdr *segment;
   7292  1.1  christos       asection *section, *osec;
   7293  1.1  christos       unsigned int i, num_segments;
   7294  1.1  christos       Elf_Internal_Shdr *this_hdr;
   7295  1.1  christos       const struct elf_backend_data *bed;
   7296  1.1  christos 
   7297  1.1  christos       bed = get_elf_backend_data (ibfd);
   7298  1.1  christos 
   7299  1.1  christos       /* Regenerate the segment map if p_paddr is set to 0.  */
   7300  1.1  christos       if (bed->want_p_paddr_set_to_zero)
   7301  1.1  christos 	goto rewrite;
   7302  1.1  christos 
   7303  1.1  christos       /* Initialize the segment mark field.  */
   7304  1.1  christos       for (section = obfd->sections; section != NULL;
   7305  1.1  christos 	   section = section->next)
   7306  1.1  christos 	section->segment_mark = FALSE;
   7307  1.1  christos 
   7308  1.1  christos       num_segments = elf_elfheader (ibfd)->e_phnum;
   7309  1.1  christos       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7310  1.1  christos 	   i < num_segments;
   7311  1.1  christos 	   i++, segment++)
   7312  1.1  christos 	{
   7313  1.1  christos 	  /* PR binutils/3535.  The Solaris linker always sets the p_paddr
   7314  1.1  christos 	     and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
   7315  1.1  christos 	     which severly confuses things, so always regenerate the segment
   7316  1.1  christos 	     map in this case.  */
   7317  1.1  christos 	  if (segment->p_paddr == 0
   7318  1.1  christos 	      && segment->p_memsz == 0
   7319  1.1  christos 	      && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
   7320  1.1  christos 	    goto rewrite;
   7321  1.1  christos 
   7322  1.1  christos 	  for (section = ibfd->sections;
   7323  1.1  christos 	       section != NULL; section = section->next)
   7324  1.1  christos 	    {
   7325  1.1  christos 	      /* We mark the output section so that we know it comes
   7326  1.1  christos 		 from the input BFD.  */
   7327  1.1  christos 	      osec = section->output_section;
   7328  1.1  christos 	      if (osec)
   7329  1.1  christos 		osec->segment_mark = TRUE;
   7330  1.1  christos 
   7331  1.1  christos 	      /* Check if this section is covered by the segment.  */
   7332  1.1  christos 	      this_hdr = &(elf_section_data(section)->this_hdr);
   7333  1.1  christos 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   7334  1.1  christos 		{
   7335  1.1  christos 		  /* FIXME: Check if its output section is changed or
   7336  1.1  christos 		     removed.  What else do we need to check?  */
   7337  1.1  christos 		  if (osec == NULL
   7338  1.1  christos 		      || section->flags != osec->flags
   7339  1.1  christos 		      || section->lma != osec->lma
   7340  1.1  christos 		      || section->vma != osec->vma
   7341  1.1  christos 		      || section->size != osec->size
   7342  1.1  christos 		      || section->rawsize != osec->rawsize
   7343  1.1  christos 		      || section->alignment_power != osec->alignment_power)
   7344  1.1  christos 		    goto rewrite;
   7345  1.1  christos 		}
   7346  1.1  christos 	    }
   7347  1.1  christos 	}
   7348  1.1  christos 
   7349  1.1  christos       /* Check to see if any output section do not come from the
   7350  1.1  christos 	 input BFD.  */
   7351  1.1  christos       for (section = obfd->sections; section != NULL;
   7352  1.1  christos 	   section = section->next)
   7353  1.1  christos 	{
   7354  1.1  christos 	  if (section->segment_mark == FALSE)
   7355  1.1  christos 	    goto rewrite;
   7356  1.1  christos 	  else
   7357  1.1  christos 	    section->segment_mark = FALSE;
   7358  1.1  christos 	}
   7359  1.1  christos 
   7360  1.1  christos       return copy_elf_program_header (ibfd, obfd);
   7361  1.1  christos     }
   7362  1.3  christos 
   7363  1.3  christos rewrite:
   7364  1.3  christos   if (ibfd->xvec == obfd->xvec)
   7365  1.3  christos     {
   7366  1.3  christos       /* When rewriting program header, set the output maxpagesize to
   7367  1.3  christos 	 the maximum alignment of input PT_LOAD segments.  */
   7368  1.3  christos       Elf_Internal_Phdr *segment;
   7369  1.3  christos       unsigned int i;
   7370  1.3  christos       unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
   7371  1.3  christos       bfd_vma maxpagesize = 0;
   7372  1.3  christos 
   7373  1.3  christos       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7374  1.3  christos 	   i < num_segments;
   7375  1.3  christos 	   i++, segment++)
   7376  1.6  christos 	if (segment->p_type == PT_LOAD
   7377  1.6  christos 	    && maxpagesize < segment->p_align)
   7378  1.6  christos 	  {
   7379  1.9  christos 	    /* PR 17512: file: f17299af.  */
   7380  1.9  christos 	    if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
   7381  1.6  christos 	      /* xgettext:c-format */
   7382  1.9  christos 	      _bfd_error_handler (_("\
   7383  1.6  christos %B: warning: segment alignment of 0x%llx is too large"),
   7384  1.6  christos 				  ibfd, (long long) segment->p_align);
   7385  1.6  christos 	    else
   7386  1.3  christos 	      maxpagesize = segment->p_align;
   7387  1.3  christos 	  }
   7388  1.3  christos 
   7389  1.3  christos       if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
   7390  1.3  christos 	bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
   7391  1.1  christos     }
   7392  1.1  christos 
   7393  1.1  christos   return rewrite_elf_program_header (ibfd, obfd);
   7394  1.1  christos }
   7395  1.1  christos 
   7396  1.1  christos /* Initialize private output section information from input section.  */
   7397  1.1  christos 
   7398  1.1  christos bfd_boolean
   7399  1.1  christos _bfd_elf_init_private_section_data (bfd *ibfd,
   7400  1.1  christos 				    asection *isec,
   7401  1.1  christos 				    bfd *obfd,
   7402  1.1  christos 				    asection *osec,
   7403  1.1  christos 				    struct bfd_link_info *link_info)
   7404  1.1  christos 
   7405  1.8  christos {
   7406  1.8  christos   Elf_Internal_Shdr *ihdr, *ohdr;
   7407  1.1  christos   bfd_boolean final_link = (link_info != NULL
   7408  1.1  christos 			    && !bfd_link_relocatable (link_info));
   7409  1.1  christos 
   7410  1.1  christos   if (ibfd->xvec->flavour != bfd_target_elf_flavour
   7411  1.1  christos       || obfd->xvec->flavour != bfd_target_elf_flavour)
   7412  1.3  christos     return TRUE;
   7413  1.3  christos 
   7414  1.1  christos   BFD_ASSERT (elf_section_data (osec) != NULL);
   7415  1.1  christos 
   7416  1.1  christos   /* For objcopy and relocatable link, don't copy the output ELF
   7417  1.1  christos      section type from input if the output BFD section flags have been
   7418  1.1  christos      set to something different.  For a final link allow some flags
   7419  1.1  christos      that the linker clears to differ.  */
   7420  1.1  christos   if (elf_section_type (osec) == SHT_NULL
   7421  1.1  christos       && (osec->flags == isec->flags
   7422  1.1  christos 	  || (final_link
   7423  1.1  christos 	      && ((osec->flags ^ isec->flags)
   7424  1.1  christos 		  & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
   7425  1.1  christos     elf_section_type (osec) = elf_section_type (isec);
   7426  1.1  christos 
   7427  1.1  christos   /* FIXME: Is this correct for all OS/PROC specific flags?  */
   7428  1.1  christos   elf_section_flags (osec) |= (elf_section_flags (isec)
   7429  1.9  christos 			       & (SHF_MASKOS | SHF_MASKPROC));
   7430  1.9  christos 
   7431  1.9  christos   /* Copy sh_info from input for mbind section.  */
   7432  1.9  christos   if (elf_section_flags (isec) & SHF_GNU_MBIND)
   7433  1.9  christos     elf_section_data (osec)->this_hdr.sh_info
   7434  1.1  christos       = elf_section_data (isec)->this_hdr.sh_info;
   7435  1.1  christos 
   7436  1.1  christos   /* Set things up for objcopy and relocatable link.  The output
   7437  1.1  christos      SHT_GROUP section will have its elf_next_in_group pointing back
   7438  1.1  christos      to the input group members.  Ignore linker created group section.
   7439  1.1  christos      See elfNN_ia64_object_p in elfxx-ia64.c.  */
   7440  1.1  christos   if (!final_link)
   7441  1.1  christos     {
   7442  1.1  christos       if (elf_sec_group (isec) == NULL
   7443  1.1  christos 	  || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
   7444  1.1  christos 	{
   7445  1.1  christos 	  if (elf_section_flags (isec) & SHF_GROUP)
   7446  1.1  christos 	    elf_section_flags (osec) |= SHF_GROUP;
   7447  1.1  christos 	  elf_next_in_group (osec) = elf_next_in_group (isec);
   7448  1.6  christos 	  elf_section_data (osec)->group = elf_section_data (isec)->group;
   7449  1.6  christos 	}
   7450  1.6  christos 
   7451  1.6  christos       /* If not decompress, preserve SHF_COMPRESSED.  */
   7452  1.6  christos       if ((ibfd->flags & BFD_DECOMPRESS) == 0)
   7453  1.1  christos 	elf_section_flags (osec) |= (elf_section_flags (isec)
   7454  1.1  christos 				     & SHF_COMPRESSED);
   7455  1.1  christos     }
   7456  1.1  christos 
   7457  1.1  christos   ihdr = &elf_section_data (isec)->this_hdr;
   7458  1.1  christos 
   7459  1.1  christos   /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
   7460  1.1  christos      don't use the output section of the linked-to section since it
   7461  1.1  christos      may be NULL at this point.  */
   7462  1.1  christos   if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
   7463  1.1  christos     {
   7464  1.1  christos       ohdr = &elf_section_data (osec)->this_hdr;
   7465  1.1  christos       ohdr->sh_flags |= SHF_LINK_ORDER;
   7466  1.1  christos       elf_linked_to_section (osec) = elf_linked_to_section (isec);
   7467  1.1  christos     }
   7468  1.1  christos 
   7469  1.1  christos   osec->use_rela_p = isec->use_rela_p;
   7470  1.1  christos 
   7471  1.1  christos   return TRUE;
   7472  1.1  christos }
   7473  1.1  christos 
   7474  1.1  christos /* Copy private section information.  This copies over the entsize
   7475  1.1  christos    field, and sometimes the info field.  */
   7476  1.1  christos 
   7477  1.1  christos bfd_boolean
   7478  1.1  christos _bfd_elf_copy_private_section_data (bfd *ibfd,
   7479  1.1  christos 				    asection *isec,
   7480  1.1  christos 				    bfd *obfd,
   7481  1.1  christos 				    asection *osec)
   7482  1.1  christos {
   7483  1.1  christos   Elf_Internal_Shdr *ihdr, *ohdr;
   7484  1.1  christos 
   7485  1.1  christos   if (ibfd->xvec->flavour != bfd_target_elf_flavour
   7486  1.1  christos       || obfd->xvec->flavour != bfd_target_elf_flavour)
   7487  1.1  christos     return TRUE;
   7488  1.1  christos 
   7489  1.1  christos   ihdr = &elf_section_data (isec)->this_hdr;
   7490  1.1  christos   ohdr = &elf_section_data (osec)->this_hdr;
   7491  1.1  christos 
   7492  1.1  christos   ohdr->sh_entsize = ihdr->sh_entsize;
   7493  1.1  christos 
   7494  1.1  christos   if (ihdr->sh_type == SHT_SYMTAB
   7495  1.1  christos       || ihdr->sh_type == SHT_DYNSYM
   7496  1.1  christos       || ihdr->sh_type == SHT_GNU_verneed
   7497  1.1  christos       || ihdr->sh_type == SHT_GNU_verdef)
   7498  1.1  christos     ohdr->sh_info = ihdr->sh_info;
   7499  1.1  christos 
   7500  1.1  christos   return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
   7501  1.1  christos 					     NULL);
   7502  1.1  christos }
   7503  1.1  christos 
   7504  1.1  christos /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
   7505  1.1  christos    necessary if we are removing either the SHT_GROUP section or any of
   7506  1.1  christos    the group member sections.  DISCARDED is the value that a section's
   7507  1.1  christos    output_section has if the section will be discarded, NULL when this
   7508  1.1  christos    function is called from objcopy, bfd_abs_section_ptr when called
   7509  1.1  christos    from the linker.  */
   7510  1.1  christos 
   7511  1.1  christos bfd_boolean
   7512  1.1  christos _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
   7513  1.1  christos {
   7514  1.1  christos   asection *isec;
   7515  1.1  christos 
   7516  1.1  christos   for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   7517  1.1  christos     if (elf_section_type (isec) == SHT_GROUP)
   7518  1.1  christos       {
   7519  1.1  christos 	asection *first = elf_next_in_group (isec);
   7520  1.1  christos 	asection *s = first;
   7521  1.1  christos 	bfd_size_type removed = 0;
   7522  1.1  christos 
   7523  1.1  christos 	while (s != NULL)
   7524  1.1  christos 	  {
   7525  1.1  christos 	    /* If this member section is being output but the
   7526  1.1  christos 	       SHT_GROUP section is not, then clear the group info
   7527  1.1  christos 	       set up by _bfd_elf_copy_private_section_data.  */
   7528  1.1  christos 	    if (s->output_section != discarded
   7529  1.1  christos 		&& isec->output_section == discarded)
   7530  1.1  christos 	      {
   7531  1.1  christos 		elf_section_flags (s->output_section) &= ~SHF_GROUP;
   7532  1.1  christos 		elf_group_name (s->output_section) = NULL;
   7533  1.1  christos 	      }
   7534  1.1  christos 	    /* Conversely, if the member section is not being output
   7535  1.1  christos 	       but the SHT_GROUP section is, then adjust its size.  */
   7536  1.1  christos 	    else if (s->output_section == discarded
   7537  1.1  christos 		     && isec->output_section != discarded)
   7538  1.1  christos 	      removed += 4;
   7539  1.1  christos 	    s = elf_next_in_group (s);
   7540  1.1  christos 	    if (s == first)
   7541  1.1  christos 	      break;
   7542  1.1  christos 	  }
   7543  1.1  christos 	if (removed != 0)
   7544  1.1  christos 	  {
   7545  1.1  christos 	    if (discarded != NULL)
   7546  1.1  christos 	      {
   7547  1.1  christos 		/* If we've been called for ld -r, then we need to
   7548  1.1  christos 		   adjust the input section size.  This function may
   7549  1.1  christos 		   be called multiple times, so save the original
   7550  1.1  christos 		   size.  */
   7551  1.1  christos 		if (isec->rawsize == 0)
   7552  1.1  christos 		  isec->rawsize = isec->size;
   7553  1.1  christos 		isec->size = isec->rawsize - removed;
   7554  1.1  christos 	      }
   7555  1.1  christos 	    else
   7556  1.1  christos 	      {
   7557  1.1  christos 		/* Adjust the output section size when called from
   7558  1.1  christos 		   objcopy. */
   7559  1.1  christos 		isec->output_section->size -= removed;
   7560  1.1  christos 	      }
   7561  1.1  christos 	  }
   7562  1.1  christos       }
   7563  1.1  christos 
   7564  1.1  christos   return TRUE;
   7565  1.1  christos }
   7566  1.1  christos 
   7567  1.1  christos /* Copy private header information.  */
   7568  1.1  christos 
   7569  1.1  christos bfd_boolean
   7570  1.1  christos _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
   7571  1.1  christos {
   7572  1.1  christos   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   7573  1.1  christos       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   7574  1.1  christos     return TRUE;
   7575  1.1  christos 
   7576  1.1  christos   /* Copy over private BFD data if it has not already been copied.
   7577  1.1  christos      This must be done here, rather than in the copy_private_bfd_data
   7578  1.1  christos      entry point, because the latter is called after the section
   7579  1.3  christos      contents have been set, which means that the program headers have
   7580  1.1  christos      already been worked out.  */
   7581  1.1  christos   if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
   7582  1.1  christos     {
   7583  1.1  christos       if (! copy_private_bfd_data (ibfd, obfd))
   7584  1.1  christos 	return FALSE;
   7585  1.1  christos     }
   7586  1.1  christos 
   7587  1.1  christos   return _bfd_elf_fixup_group_sections (ibfd, NULL);
   7588  1.1  christos }
   7589  1.1  christos 
   7590  1.1  christos /* Copy private symbol information.  If this symbol is in a section
   7591  1.1  christos    which we did not map into a BFD section, try to map the section
   7592  1.1  christos    index correctly.  We use special macro definitions for the mapped
   7593  1.1  christos    section indices; these definitions are interpreted by the
   7594  1.1  christos    swap_out_syms function.  */
   7595  1.1  christos 
   7596  1.1  christos #define MAP_ONESYMTAB (SHN_HIOS + 1)
   7597  1.1  christos #define MAP_DYNSYMTAB (SHN_HIOS + 2)
   7598  1.1  christos #define MAP_STRTAB    (SHN_HIOS + 3)
   7599  1.1  christos #define MAP_SHSTRTAB  (SHN_HIOS + 4)
   7600  1.1  christos #define MAP_SYM_SHNDX (SHN_HIOS + 5)
   7601  1.1  christos 
   7602  1.1  christos bfd_boolean
   7603  1.1  christos _bfd_elf_copy_private_symbol_data (bfd *ibfd,
   7604  1.1  christos 				   asymbol *isymarg,
   7605  1.1  christos 				   bfd *obfd,
   7606  1.1  christos 				   asymbol *osymarg)
   7607  1.1  christos {
   7608  1.1  christos   elf_symbol_type *isym, *osym;
   7609  1.1  christos 
   7610  1.1  christos   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   7611  1.1  christos       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   7612  1.1  christos     return TRUE;
   7613  1.1  christos 
   7614  1.1  christos   isym = elf_symbol_from (ibfd, isymarg);
   7615  1.1  christos   osym = elf_symbol_from (obfd, osymarg);
   7616  1.1  christos 
   7617  1.1  christos   if (isym != NULL
   7618  1.1  christos       && isym->internal_elf_sym.st_shndx != 0
   7619  1.1  christos       && osym != NULL
   7620  1.1  christos       && bfd_is_abs_section (isym->symbol.section))
   7621  1.1  christos     {
   7622  1.1  christos       unsigned int shndx;
   7623  1.1  christos 
   7624  1.1  christos       shndx = isym->internal_elf_sym.st_shndx;
   7625  1.1  christos       if (shndx == elf_onesymtab (ibfd))
   7626  1.1  christos 	shndx = MAP_ONESYMTAB;
   7627  1.3  christos       else if (shndx == elf_dynsymtab (ibfd))
   7628  1.1  christos 	shndx = MAP_DYNSYMTAB;
   7629  1.3  christos       else if (shndx == elf_strtab_sec (ibfd))
   7630  1.1  christos 	shndx = MAP_STRTAB;
   7631  1.8  christos       else if (shndx == elf_shstrtab_sec (ibfd))
   7632  1.1  christos 	shndx = MAP_SHSTRTAB;
   7633  1.1  christos       else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
   7634  1.1  christos 	shndx = MAP_SYM_SHNDX;
   7635  1.1  christos       osym->internal_elf_sym.st_shndx = shndx;
   7636  1.1  christos     }
   7637  1.1  christos 
   7638  1.1  christos   return TRUE;
   7639  1.1  christos }
   7640  1.1  christos 
   7641  1.1  christos /* Swap out the symbols.  */
   7642  1.1  christos 
   7643  1.6  christos static bfd_boolean
   7644  1.1  christos swap_out_syms (bfd *abfd,
   7645  1.1  christos 	       struct elf_strtab_hash **sttp,
   7646  1.1  christos 	       int relocatable_p)
   7647  1.1  christos {
   7648  1.1  christos   const struct elf_backend_data *bed;
   7649  1.6  christos   int symcount;
   7650  1.1  christos   asymbol **syms;
   7651  1.1  christos   struct elf_strtab_hash *stt;
   7652  1.1  christos   Elf_Internal_Shdr *symtab_hdr;
   7653  1.6  christos   Elf_Internal_Shdr *symtab_shndx_hdr;
   7654  1.1  christos   Elf_Internal_Shdr *symstrtab_hdr;
   7655  1.1  christos   struct elf_sym_strtab *symstrtab;
   7656  1.6  christos   bfd_byte *outbound_syms;
   7657  1.6  christos   bfd_byte *outbound_shndx;
   7658  1.1  christos   unsigned long outbound_syms_index;
   7659  1.3  christos   unsigned long outbound_shndx_index;
   7660  1.1  christos   int idx;
   7661  1.1  christos   unsigned int num_locals;
   7662  1.1  christos   bfd_size_type amt;
   7663  1.3  christos   bfd_boolean name_local_sections;
   7664  1.1  christos 
   7665  1.1  christos   if (!elf_map_symbols (abfd, &num_locals))
   7666  1.1  christos     return FALSE;
   7667  1.6  christos 
   7668  1.1  christos   /* Dump out the symtabs.  */
   7669  1.1  christos   stt = _bfd_elf_strtab_init ();
   7670  1.1  christos   if (stt == NULL)
   7671  1.1  christos     return FALSE;
   7672  1.1  christos 
   7673  1.1  christos   bed = get_elf_backend_data (abfd);
   7674  1.1  christos   symcount = bfd_get_symcount (abfd);
   7675  1.1  christos   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   7676  1.1  christos   symtab_hdr->sh_type = SHT_SYMTAB;
   7677  1.3  christos   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
   7678  1.1  christos   symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
   7679  1.1  christos   symtab_hdr->sh_info = num_locals + 1;
   7680  1.1  christos   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   7681  1.1  christos 
   7682  1.1  christos   symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
   7683  1.6  christos   symstrtab_hdr->sh_type = SHT_STRTAB;
   7684  1.6  christos 
   7685  1.6  christos   /* Allocate buffer to swap out the .strtab section.  */
   7686  1.6  christos   symstrtab = (struct elf_sym_strtab *) bfd_malloc ((symcount + 1)
   7687  1.6  christos 						    * sizeof (*symstrtab));
   7688  1.6  christos   if (symstrtab == NULL)
   7689  1.6  christos     {
   7690  1.6  christos       _bfd_elf_strtab_free (stt);
   7691  1.6  christos       return FALSE;
   7692  1.1  christos     }
   7693  1.1  christos 
   7694  1.1  christos   outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
   7695  1.1  christos                                            bed->s->sizeof_sym);
   7696  1.6  christos   if (outbound_syms == NULL)
   7697  1.6  christos     {
   7698  1.6  christos error_return:
   7699  1.1  christos       _bfd_elf_strtab_free (stt);
   7700  1.1  christos       free (symstrtab);
   7701  1.1  christos       return FALSE;
   7702  1.6  christos     }
   7703  1.1  christos   symtab_hdr->contents = outbound_syms;
   7704  1.1  christos   outbound_syms_index = 0;
   7705  1.6  christos 
   7706  1.8  christos   outbound_shndx = NULL;
   7707  1.8  christos   outbound_shndx_index = 0;
   7708  1.1  christos 
   7709  1.8  christos   if (elf_symtab_shndx_list (abfd))
   7710  1.8  christos     {
   7711  1.8  christos       symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
   7712  1.8  christos       if (symtab_shndx_hdr->sh_name != 0)
   7713  1.8  christos 	{
   7714  1.8  christos 	  amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
   7715  1.8  christos 	  outbound_shndx =  (bfd_byte *)
   7716  1.8  christos 	    bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
   7717  1.1  christos 	  if (outbound_shndx == NULL)
   7718  1.8  christos 	    goto error_return;
   7719  1.8  christos 
   7720  1.8  christos 	  symtab_shndx_hdr->contents = outbound_shndx;
   7721  1.8  christos 	  symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
   7722  1.8  christos 	  symtab_shndx_hdr->sh_size = amt;
   7723  1.8  christos 	  symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
   7724  1.8  christos 	  symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
   7725  1.1  christos 	}
   7726  1.1  christos       /* FIXME: What about any other headers in the list ?  */
   7727  1.1  christos     }
   7728  1.1  christos 
   7729  1.1  christos   /* Now generate the data (for "contents").  */
   7730  1.1  christos   {
   7731  1.1  christos     /* Fill in zeroth symbol and swap it out.  */
   7732  1.1  christos     Elf_Internal_Sym sym;
   7733  1.1  christos     sym.st_name = 0;
   7734  1.1  christos     sym.st_value = 0;
   7735  1.1  christos     sym.st_size = 0;
   7736  1.1  christos     sym.st_info = 0;
   7737  1.1  christos     sym.st_other = 0;
   7738  1.6  christos     sym.st_shndx = SHN_UNDEF;
   7739  1.6  christos     sym.st_target_internal = 0;
   7740  1.6  christos     symstrtab[0].sym = sym;
   7741  1.6  christos     symstrtab[0].dest_index = outbound_syms_index;
   7742  1.1  christos     symstrtab[0].destshndx_index = outbound_shndx_index;
   7743  1.6  christos     outbound_syms_index++;
   7744  1.1  christos     if (outbound_shndx != NULL)
   7745  1.1  christos       outbound_shndx_index++;
   7746  1.1  christos   }
   7747  1.1  christos 
   7748  1.1  christos   name_local_sections
   7749  1.1  christos     = (bed->elf_backend_name_local_section_symbols
   7750  1.1  christos        && bed->elf_backend_name_local_section_symbols (abfd));
   7751  1.6  christos 
   7752  1.1  christos   syms = bfd_get_outsymbols (abfd);
   7753  1.1  christos   for (idx = 0; idx < symcount;)
   7754  1.1  christos     {
   7755  1.1  christos       Elf_Internal_Sym sym;
   7756  1.1  christos       bfd_vma value = syms[idx]->value;
   7757  1.1  christos       elf_symbol_type *type_ptr;
   7758  1.1  christos       flagword flags = syms[idx]->flags;
   7759  1.1  christos       int type;
   7760  1.1  christos 
   7761  1.1  christos       if (!name_local_sections
   7762  1.1  christos 	  && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
   7763  1.6  christos 	{
   7764  1.1  christos 	  /* Local section symbols have no name.  */
   7765  1.1  christos 	  sym.st_name = (unsigned long) -1;
   7766  1.1  christos 	}
   7767  1.6  christos       else
   7768  1.6  christos 	{
   7769  1.6  christos 	  /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
   7770  1.6  christos 	     to get the final offset for st_name.  */
   7771  1.6  christos 	  sym.st_name
   7772  1.1  christos 	    = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
   7773  1.6  christos 						   FALSE);
   7774  1.1  christos 	  if (sym.st_name == (unsigned long) -1)
   7775  1.1  christos 	    goto error_return;
   7776  1.1  christos 	}
   7777  1.1  christos 
   7778  1.1  christos       type_ptr = elf_symbol_from (abfd, syms[idx]);
   7779  1.1  christos 
   7780  1.1  christos       if ((flags & BSF_SECTION_SYM) == 0
   7781  1.1  christos 	  && bfd_is_com_section (syms[idx]->section))
   7782  1.1  christos 	{
   7783  1.1  christos 	  /* ELF common symbols put the alignment into the `value' field,
   7784  1.1  christos 	     and the size into the `size' field.  This is backwards from
   7785  1.1  christos 	     how BFD handles it, so reverse it here.  */
   7786  1.1  christos 	  sym.st_size = value;
   7787  1.1  christos 	  if (type_ptr == NULL
   7788  1.1  christos 	      || type_ptr->internal_elf_sym.st_value == 0)
   7789  1.1  christos 	    sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
   7790  1.1  christos 	  else
   7791  1.1  christos 	    sym.st_value = type_ptr->internal_elf_sym.st_value;
   7792  1.1  christos 	  sym.st_shndx = _bfd_elf_section_from_bfd_section
   7793  1.1  christos 	    (abfd, syms[idx]->section);
   7794  1.1  christos 	}
   7795  1.1  christos       else
   7796  1.1  christos 	{
   7797  1.1  christos 	  asection *sec = syms[idx]->section;
   7798  1.1  christos 	  unsigned int shndx;
   7799  1.1  christos 
   7800  1.1  christos 	  if (sec->output_section)
   7801  1.1  christos 	    {
   7802  1.1  christos 	      value += sec->output_offset;
   7803  1.1  christos 	      sec = sec->output_section;
   7804  1.1  christos 	    }
   7805  1.1  christos 
   7806  1.1  christos 	  /* Don't add in the section vma for relocatable output.  */
   7807  1.1  christos 	  if (! relocatable_p)
   7808  1.1  christos 	    value += sec->vma;
   7809  1.1  christos 	  sym.st_value = value;
   7810  1.1  christos 	  sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
   7811  1.1  christos 
   7812  1.1  christos 	  if (bfd_is_abs_section (sec)
   7813  1.1  christos 	      && type_ptr != NULL
   7814  1.1  christos 	      && type_ptr->internal_elf_sym.st_shndx != 0)
   7815  1.1  christos 	    {
   7816  1.1  christos 	      /* This symbol is in a real ELF section which we did
   7817  1.1  christos 		 not create as a BFD section.  Undo the mapping done
   7818  1.1  christos 		 by copy_private_symbol_data.  */
   7819  1.1  christos 	      shndx = type_ptr->internal_elf_sym.st_shndx;
   7820  1.1  christos 	      switch (shndx)
   7821  1.1  christos 		{
   7822  1.1  christos 		case MAP_ONESYMTAB:
   7823  1.1  christos 		  shndx = elf_onesymtab (abfd);
   7824  1.1  christos 		  break;
   7825  1.1  christos 		case MAP_DYNSYMTAB:
   7826  1.1  christos 		  shndx = elf_dynsymtab (abfd);
   7827  1.3  christos 		  break;
   7828  1.1  christos 		case MAP_STRTAB:
   7829  1.1  christos 		  shndx = elf_strtab_sec (abfd);
   7830  1.3  christos 		  break;
   7831  1.1  christos 		case MAP_SHSTRTAB:
   7832  1.1  christos 		  shndx = elf_shstrtab_sec (abfd);
   7833  1.8  christos 		  break;
   7834  1.8  christos 		case MAP_SYM_SHNDX:
   7835  1.1  christos 		  if (elf_symtab_shndx_list (abfd))
   7836  1.1  christos 		    shndx = elf_symtab_shndx_list (abfd)->ndx;
   7837  1.3  christos 		  break;
   7838  1.1  christos 		default:
   7839  1.1  christos 		  shndx = SHN_ABS;
   7840  1.1  christos 		  break;
   7841  1.1  christos 		}
   7842  1.1  christos 	    }
   7843  1.1  christos 	  else
   7844  1.1  christos 	    {
   7845  1.1  christos 	      shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   7846  1.1  christos 
   7847  1.1  christos 	      if (shndx == SHN_BAD)
   7848  1.1  christos 		{
   7849  1.1  christos 		  asection *sec2;
   7850  1.1  christos 
   7851  1.1  christos 		  /* Writing this would be a hell of a lot easier if
   7852  1.1  christos 		     we had some decent documentation on bfd, and
   7853  1.1  christos 		     knew what to expect of the library, and what to
   7854  1.1  christos 		     demand of applications.  For example, it
   7855  1.1  christos 		     appears that `objcopy' might not set the
   7856  1.1  christos 		     section of a symbol to be a section that is
   7857  1.8  christos 		     actually in the output file.  */
   7858  1.8  christos 		  sec2 = bfd_get_section_by_name (abfd, sec->name);
   7859  1.8  christos 		  if (sec2 != NULL)
   7860  1.1  christos 		    shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
   7861  1.9  christos 		  if (shndx == SHN_BAD)
   7862  1.1  christos 		    {
   7863  1.1  christos 		      /* xgettext:c-format */
   7864  1.1  christos 		      _bfd_error_handler (_("\
   7865  1.1  christos Unable to find equivalent output section for symbol '%s' from section '%s'"),
   7866  1.1  christos 					  syms[idx]->name ? syms[idx]->name : "<Local sym>",
   7867  1.6  christos 					  sec->name);
   7868  1.1  christos 		      bfd_set_error (bfd_error_invalid_operation);
   7869  1.1  christos 		      goto error_return;
   7870  1.1  christos 		    }
   7871  1.1  christos 		}
   7872  1.1  christos 	    }
   7873  1.1  christos 
   7874  1.1  christos 	  sym.st_shndx = shndx;
   7875  1.1  christos 	}
   7876  1.1  christos 
   7877  1.1  christos       if ((flags & BSF_THREAD_LOCAL) != 0)
   7878  1.1  christos 	type = STT_TLS;
   7879  1.1  christos       else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
   7880  1.1  christos 	type = STT_GNU_IFUNC;
   7881  1.1  christos       else if ((flags & BSF_FUNCTION) != 0)
   7882  1.1  christos 	type = STT_FUNC;
   7883  1.1  christos       else if ((flags & BSF_OBJECT) != 0)
   7884  1.1  christos 	type = STT_OBJECT;
   7885  1.1  christos       else if ((flags & BSF_RELC) != 0)
   7886  1.1  christos 	type = STT_RELC;
   7887  1.1  christos       else if ((flags & BSF_SRELC) != 0)
   7888  1.1  christos 	type = STT_SRELC;
   7889  1.1  christos       else
   7890  1.1  christos 	type = STT_NOTYPE;
   7891  1.1  christos 
   7892  1.1  christos       if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
   7893  1.1  christos 	type = STT_TLS;
   7894  1.1  christos 
   7895  1.1  christos       /* Processor-specific types.  */
   7896  1.1  christos       if (type_ptr != NULL
   7897  1.1  christos 	  && bed->elf_backend_get_symbol_type)
   7898  1.1  christos 	type = ((*bed->elf_backend_get_symbol_type)
   7899  1.1  christos 		(&type_ptr->internal_elf_sym, type));
   7900  1.1  christos 
   7901  1.1  christos       if (flags & BSF_SECTION_SYM)
   7902  1.1  christos 	{
   7903  1.1  christos 	  if (flags & BSF_GLOBAL)
   7904  1.1  christos 	    sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   7905  1.1  christos 	  else
   7906  1.1  christos 	    sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   7907  1.1  christos 	}
   7908  1.8  christos       else if (bfd_is_com_section (syms[idx]->section))
   7909  1.8  christos 	{
   7910  1.8  christos 	  if (type != STT_TLS)
   7911  1.8  christos 	    {
   7912  1.8  christos 	      if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
   7913  1.8  christos 		type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
   7914  1.8  christos 			? STT_COMMON : STT_OBJECT);
   7915  1.8  christos 	      else
   7916  1.8  christos 		type = ((flags & BSF_ELF_COMMON) != 0
   7917  1.8  christos 			? STT_COMMON : STT_OBJECT);
   7918  1.1  christos 	    }
   7919  1.1  christos 	  sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
   7920  1.1  christos 	}
   7921  1.1  christos       else if (bfd_is_und_section (syms[idx]->section))
   7922  1.1  christos 	sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
   7923  1.1  christos 				    ? STB_WEAK
   7924  1.1  christos 				    : STB_GLOBAL),
   7925  1.1  christos 				   type);
   7926  1.1  christos       else if (flags & BSF_FILE)
   7927  1.1  christos 	sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   7928  1.1  christos       else
   7929  1.1  christos 	{
   7930  1.1  christos 	  int bind = STB_LOCAL;
   7931  1.1  christos 
   7932  1.1  christos 	  if (flags & BSF_LOCAL)
   7933  1.1  christos 	    bind = STB_LOCAL;
   7934  1.1  christos 	  else if (flags & BSF_GNU_UNIQUE)
   7935  1.1  christos 	    bind = STB_GNU_UNIQUE;
   7936  1.1  christos 	  else if (flags & BSF_WEAK)
   7937  1.1  christos 	    bind = STB_WEAK;
   7938  1.1  christos 	  else if (flags & BSF_GLOBAL)
   7939  1.1  christos 	    bind = STB_GLOBAL;
   7940  1.1  christos 
   7941  1.1  christos 	  sym.st_info = ELF_ST_INFO (bind, type);
   7942  1.1  christos 	}
   7943  1.1  christos 
   7944  1.1  christos       if (type_ptr != NULL)
   7945  1.1  christos 	{
   7946  1.1  christos 	  sym.st_other = type_ptr->internal_elf_sym.st_other;
   7947  1.1  christos 	  sym.st_target_internal
   7948  1.1  christos 	    = type_ptr->internal_elf_sym.st_target_internal;
   7949  1.1  christos 	}
   7950  1.1  christos       else
   7951  1.1  christos 	{
   7952  1.1  christos 	  sym.st_other = 0;
   7953  1.1  christos 	  sym.st_target_internal = 0;
   7954  1.6  christos 	}
   7955  1.6  christos 
   7956  1.6  christos       idx++;
   7957  1.6  christos       symstrtab[idx].sym = sym;
   7958  1.6  christos       symstrtab[idx].dest_index = outbound_syms_index;
   7959  1.6  christos       symstrtab[idx].destshndx_index = outbound_shndx_index;
   7960  1.1  christos 
   7961  1.6  christos       outbound_syms_index++;
   7962  1.6  christos       if (outbound_shndx != NULL)
   7963  1.6  christos 	outbound_shndx_index++;
   7964  1.6  christos     }
   7965  1.6  christos 
   7966  1.6  christos   /* Finalize the .strtab section.  */
   7967  1.6  christos   _bfd_elf_strtab_finalize (stt);
   7968  1.6  christos 
   7969  1.6  christos   /* Swap out the .strtab section.  */
   7970  1.6  christos   for (idx = 0; idx <= symcount; idx++)
   7971  1.6  christos     {
   7972  1.6  christos       struct elf_sym_strtab *elfsym = &symstrtab[idx];
   7973  1.6  christos       if (elfsym->sym.st_name == (unsigned long) -1)
   7974  1.6  christos 	elfsym->sym.st_name = 0;
   7975  1.6  christos       else
   7976  1.6  christos 	elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
   7977  1.6  christos 						      elfsym->sym.st_name);
   7978  1.6  christos       bed->s->swap_symbol_out (abfd, &elfsym->sym,
   7979  1.6  christos 			       (outbound_syms
   7980  1.6  christos 				+ (elfsym->dest_index
   7981  1.6  christos 				   * bed->s->sizeof_sym)),
   7982  1.6  christos 			       (outbound_shndx
   7983  1.1  christos 				+ (elfsym->destshndx_index
   7984  1.6  christos 				   * sizeof (Elf_External_Sym_Shndx))));
   7985  1.1  christos     }
   7986  1.1  christos   free (symstrtab);
   7987  1.6  christos 
   7988  1.1  christos   *sttp = stt;
   7989  1.8  christos   symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
   7990  1.1  christos   symstrtab_hdr->sh_type = SHT_STRTAB;
   7991  1.1  christos   symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   7992  1.1  christos   symstrtab_hdr->sh_addr = 0;
   7993  1.1  christos   symstrtab_hdr->sh_entsize = 0;
   7994  1.1  christos   symstrtab_hdr->sh_link = 0;
   7995  1.1  christos   symstrtab_hdr->sh_info = 0;
   7996  1.1  christos   symstrtab_hdr->sh_addralign = 1;
   7997  1.1  christos 
   7998  1.1  christos   return TRUE;
   7999  1.1  christos }
   8000  1.1  christos 
   8001  1.1  christos /* Return the number of bytes required to hold the symtab vector.
   8002  1.1  christos 
   8003  1.1  christos    Note that we base it on the count plus 1, since we will null terminate
   8004  1.1  christos    the vector allocated based on this size.  However, the ELF symbol table
   8005  1.1  christos    always has a dummy entry as symbol #0, so it ends up even.  */
   8006  1.1  christos 
   8007  1.1  christos long
   8008  1.1  christos _bfd_elf_get_symtab_upper_bound (bfd *abfd)
   8009  1.1  christos {
   8010  1.1  christos   long symcount;
   8011  1.1  christos   long symtab_size;
   8012  1.1  christos   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
   8013  1.1  christos 
   8014  1.1  christos   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   8015  1.1  christos   symtab_size = (symcount + 1) * (sizeof (asymbol *));
   8016  1.1  christos   if (symcount > 0)
   8017  1.1  christos     symtab_size -= sizeof (asymbol *);
   8018  1.1  christos 
   8019  1.1  christos   return symtab_size;
   8020  1.1  christos }
   8021  1.1  christos 
   8022  1.1  christos long
   8023  1.1  christos _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
   8024  1.1  christos {
   8025  1.1  christos   long symcount;
   8026  1.1  christos   long symtab_size;
   8027  1.1  christos   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   8028  1.1  christos 
   8029  1.1  christos   if (elf_dynsymtab (abfd) == 0)
   8030  1.1  christos     {
   8031  1.1  christos       bfd_set_error (bfd_error_invalid_operation);
   8032  1.1  christos       return -1;
   8033  1.1  christos     }
   8034  1.1  christos 
   8035  1.1  christos   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   8036  1.1  christos   symtab_size = (symcount + 1) * (sizeof (asymbol *));
   8037  1.1  christos   if (symcount > 0)
   8038  1.1  christos     symtab_size -= sizeof (asymbol *);
   8039  1.1  christos 
   8040  1.1  christos   return symtab_size;
   8041  1.1  christos }
   8042  1.1  christos 
   8043  1.1  christos long
   8044  1.1  christos _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
   8045  1.1  christos 				sec_ptr asect)
   8046  1.1  christos {
   8047  1.1  christos   return (asect->reloc_count + 1) * sizeof (arelent *);
   8048  1.1  christos }
   8049  1.1  christos 
   8050  1.1  christos /* Canonicalize the relocs.  */
   8051  1.1  christos 
   8052  1.1  christos long
   8053  1.1  christos _bfd_elf_canonicalize_reloc (bfd *abfd,
   8054  1.1  christos 			     sec_ptr section,
   8055  1.1  christos 			     arelent **relptr,
   8056  1.1  christos 			     asymbol **symbols)
   8057  1.1  christos {
   8058  1.1  christos   arelent *tblptr;
   8059  1.1  christos   unsigned int i;
   8060  1.1  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8061  1.1  christos 
   8062  1.1  christos   if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
   8063  1.1  christos     return -1;
   8064  1.1  christos 
   8065  1.1  christos   tblptr = section->relocation;
   8066  1.1  christos   for (i = 0; i < section->reloc_count; i++)
   8067  1.1  christos     *relptr++ = tblptr++;
   8068  1.1  christos 
   8069  1.1  christos   *relptr = NULL;
   8070  1.1  christos 
   8071  1.1  christos   return section->reloc_count;
   8072  1.1  christos }
   8073  1.1  christos 
   8074  1.1  christos long
   8075  1.1  christos _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
   8076  1.1  christos {
   8077  1.1  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8078  1.1  christos   long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
   8079  1.1  christos 
   8080  1.1  christos   if (symcount >= 0)
   8081  1.1  christos     bfd_get_symcount (abfd) = symcount;
   8082  1.1  christos   return symcount;
   8083  1.1  christos }
   8084  1.1  christos 
   8085  1.1  christos long
   8086  1.1  christos _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
   8087  1.1  christos 				      asymbol **allocation)
   8088  1.1  christos {
   8089  1.1  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8090  1.1  christos   long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
   8091  1.1  christos 
   8092  1.1  christos   if (symcount >= 0)
   8093  1.1  christos     bfd_get_dynamic_symcount (abfd) = symcount;
   8094  1.1  christos   return symcount;
   8095  1.1  christos }
   8096  1.1  christos 
   8097  1.1  christos /* Return the size required for the dynamic reloc entries.  Any loadable
   8098  1.1  christos    section that was actually installed in the BFD, and has type SHT_REL
   8099  1.1  christos    or SHT_RELA, and uses the dynamic symbol table, is considered to be a
   8100  1.1  christos    dynamic reloc section.  */
   8101  1.1  christos 
   8102  1.1  christos long
   8103  1.1  christos _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
   8104  1.1  christos {
   8105  1.1  christos   long ret;
   8106  1.1  christos   asection *s;
   8107  1.1  christos 
   8108  1.1  christos   if (elf_dynsymtab (abfd) == 0)
   8109  1.1  christos     {
   8110  1.1  christos       bfd_set_error (bfd_error_invalid_operation);
   8111  1.1  christos       return -1;
   8112  1.1  christos     }
   8113  1.1  christos 
   8114  1.1  christos   ret = sizeof (arelent *);
   8115  1.1  christos   for (s = abfd->sections; s != NULL; s = s->next)
   8116  1.1  christos     if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   8117  1.1  christos 	&& (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   8118  1.1  christos 	    || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
   8119  1.1  christos       ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
   8120  1.1  christos 	      * sizeof (arelent *));
   8121  1.1  christos 
   8122  1.1  christos   return ret;
   8123  1.1  christos }
   8124  1.1  christos 
   8125  1.1  christos /* Canonicalize the dynamic relocation entries.  Note that we return the
   8126  1.1  christos    dynamic relocations as a single block, although they are actually
   8127  1.1  christos    associated with particular sections; the interface, which was
   8128  1.1  christos    designed for SunOS style shared libraries, expects that there is only
   8129  1.1  christos    one set of dynamic relocs.  Any loadable section that was actually
   8130  1.1  christos    installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
   8131  1.1  christos    dynamic symbol table, is considered to be a dynamic reloc section.  */
   8132  1.1  christos 
   8133  1.1  christos long
   8134  1.1  christos _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
   8135  1.1  christos 				     arelent **storage,
   8136  1.1  christos 				     asymbol **syms)
   8137  1.1  christos {
   8138  1.1  christos   bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
   8139  1.1  christos   asection *s;
   8140  1.1  christos   long ret;
   8141  1.1  christos 
   8142  1.1  christos   if (elf_dynsymtab (abfd) == 0)
   8143  1.1  christos     {
   8144  1.1  christos       bfd_set_error (bfd_error_invalid_operation);
   8145  1.1  christos       return -1;
   8146  1.1  christos     }
   8147  1.1  christos 
   8148  1.1  christos   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   8149  1.1  christos   ret = 0;
   8150  1.1  christos   for (s = abfd->sections; s != NULL; s = s->next)
   8151  1.1  christos     {
   8152  1.1  christos       if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   8153  1.1  christos 	  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   8154  1.1  christos 	      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
   8155  1.1  christos 	{
   8156  1.1  christos 	  arelent *p;
   8157  1.1  christos 	  long count, i;
   8158  1.1  christos 
   8159  1.1  christos 	  if (! (*slurp_relocs) (abfd, s, syms, TRUE))
   8160  1.1  christos 	    return -1;
   8161  1.1  christos 	  count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
   8162  1.1  christos 	  p = s->relocation;
   8163  1.1  christos 	  for (i = 0; i < count; i++)
   8164  1.1  christos 	    *storage++ = p++;
   8165  1.1  christos 	  ret += count;
   8166  1.1  christos 	}
   8167  1.1  christos     }
   8168  1.1  christos 
   8169  1.1  christos   *storage = NULL;
   8170  1.1  christos 
   8171  1.1  christos   return ret;
   8172  1.1  christos }
   8173  1.1  christos 
   8174  1.1  christos /* Read in the version information.  */
   8176  1.1  christos 
   8177  1.1  christos bfd_boolean
   8178  1.1  christos _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
   8179  1.1  christos {
   8180  1.1  christos   bfd_byte *contents = NULL;
   8181  1.1  christos   unsigned int freeidx = 0;
   8182  1.1  christos 
   8183  1.1  christos   if (elf_dynverref (abfd) != 0)
   8184  1.1  christos     {
   8185  1.1  christos       Elf_Internal_Shdr *hdr;
   8186  1.1  christos       Elf_External_Verneed *everneed;
   8187  1.1  christos       Elf_Internal_Verneed *iverneed;
   8188  1.1  christos       unsigned int i;
   8189  1.1  christos       bfd_byte *contents_end;
   8190  1.5  christos 
   8191  1.1  christos       hdr = &elf_tdata (abfd)->dynverref_hdr;
   8192  1.5  christos 
   8193  1.9  christos       if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verneed))
   8194  1.5  christos 	{
   8195  1.5  christos error_return_bad_verref:
   8196  1.1  christos 	  _bfd_error_handler
   8197  1.1  christos 	    (_("%B: .gnu.version_r invalid entry"), abfd);
   8198  1.1  christos 	  bfd_set_error (bfd_error_bad_value);
   8199  1.1  christos error_return_verref:
   8200  1.1  christos 	  elf_tdata (abfd)->verref = NULL;
   8201  1.5  christos 	  elf_tdata (abfd)->cverrefs = 0;
   8202  1.5  christos 	  goto error_return;
   8203  1.5  christos 	}
   8204  1.5  christos 
   8205  1.5  christos       contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
   8206  1.1  christos       if (contents == NULL)
   8207  1.1  christos 	goto error_return_verref;
   8208  1.1  christos 
   8209  1.1  christos       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   8210  1.5  christos 	  || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
   8211  1.5  christos 	goto error_return_verref;
   8212  1.5  christos 
   8213  1.5  christos       elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
   8214  1.1  christos 	bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
   8215  1.1  christos 
   8216  1.1  christos       if (elf_tdata (abfd)->verref == NULL)
   8217  1.1  christos 	goto error_return_verref;
   8218  1.1  christos 
   8219  1.1  christos       BFD_ASSERT (sizeof (Elf_External_Verneed)
   8220  1.1  christos 		  == sizeof (Elf_External_Vernaux));
   8221  1.1  christos       contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
   8222  1.1  christos       everneed = (Elf_External_Verneed *) contents;
   8223  1.1  christos       iverneed = elf_tdata (abfd)->verref;
   8224  1.1  christos       for (i = 0; i < hdr->sh_info; i++, iverneed++)
   8225  1.1  christos 	{
   8226  1.1  christos 	  Elf_External_Vernaux *evernaux;
   8227  1.1  christos 	  Elf_Internal_Vernaux *ivernaux;
   8228  1.1  christos 	  unsigned int j;
   8229  1.1  christos 
   8230  1.1  christos 	  _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
   8231  1.1  christos 
   8232  1.1  christos 	  iverneed->vn_bfd = abfd;
   8233  1.1  christos 
   8234  1.1  christos 	  iverneed->vn_filename =
   8235  1.5  christos 	    bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   8236  1.1  christos 					     iverneed->vn_file);
   8237  1.1  christos 	  if (iverneed->vn_filename == NULL)
   8238  1.1  christos 	    goto error_return_bad_verref;
   8239  1.1  christos 
   8240  1.1  christos 	  if (iverneed->vn_cnt == 0)
   8241  1.1  christos 	    iverneed->vn_auxptr = NULL;
   8242  1.1  christos 	  else
   8243  1.1  christos 	    {
   8244  1.1  christos 	      iverneed->vn_auxptr = (struct elf_internal_vernaux *)
   8245  1.1  christos                   bfd_alloc2 (abfd, iverneed->vn_cnt,
   8246  1.1  christos                               sizeof (Elf_Internal_Vernaux));
   8247  1.1  christos 	      if (iverneed->vn_auxptr == NULL)
   8248  1.1  christos 		goto error_return_verref;
   8249  1.1  christos 	    }
   8250  1.5  christos 
   8251  1.1  christos 	  if (iverneed->vn_aux
   8252  1.1  christos 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   8253  1.1  christos 	    goto error_return_bad_verref;
   8254  1.1  christos 
   8255  1.1  christos 	  evernaux = ((Elf_External_Vernaux *)
   8256  1.1  christos 		      ((bfd_byte *) everneed + iverneed->vn_aux));
   8257  1.1  christos 	  ivernaux = iverneed->vn_auxptr;
   8258  1.1  christos 	  for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
   8259  1.1  christos 	    {
   8260  1.1  christos 	      _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
   8261  1.1  christos 
   8262  1.1  christos 	      ivernaux->vna_nodename =
   8263  1.5  christos 		bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   8264  1.5  christos 						 ivernaux->vna_name);
   8265  1.5  christos 	      if (ivernaux->vna_nodename == NULL)
   8266  1.5  christos 		goto error_return_bad_verref;
   8267  1.1  christos 
   8268  1.5  christos 	      if (ivernaux->vna_other > freeidx)
   8269  1.5  christos 		freeidx = ivernaux->vna_other;
   8270  1.5  christos 
   8271  1.5  christos 	      ivernaux->vna_nextptr = NULL;
   8272  1.5  christos 	      if (ivernaux->vna_next == 0)
   8273  1.5  christos 		{
   8274  1.1  christos 		  iverneed->vn_cnt = j + 1;
   8275  1.1  christos 		  break;
   8276  1.1  christos 		}
   8277  1.1  christos 	      if (j + 1 < iverneed->vn_cnt)
   8278  1.1  christos 		ivernaux->vna_nextptr = ivernaux + 1;
   8279  1.5  christos 
   8280  1.1  christos 	      if (ivernaux->vna_next
   8281  1.1  christos 		  > (size_t) (contents_end - (bfd_byte *) evernaux))
   8282  1.1  christos 		goto error_return_bad_verref;
   8283  1.1  christos 
   8284  1.1  christos 	      evernaux = ((Elf_External_Vernaux *)
   8285  1.5  christos 			  ((bfd_byte *) evernaux + ivernaux->vna_next));
   8286  1.5  christos 	    }
   8287  1.5  christos 
   8288  1.1  christos 	  iverneed->vn_nextref = NULL;
   8289  1.1  christos 	  if (iverneed->vn_next == 0)
   8290  1.1  christos 	    break;
   8291  1.1  christos 	  if (i + 1 < hdr->sh_info)
   8292  1.1  christos 	    iverneed->vn_nextref = iverneed + 1;
   8293  1.5  christos 
   8294  1.1  christos 	  if (iverneed->vn_next
   8295  1.1  christos 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   8296  1.1  christos 	    goto error_return_bad_verref;
   8297  1.1  christos 
   8298  1.5  christos 	  everneed = ((Elf_External_Verneed *)
   8299  1.1  christos 		      ((bfd_byte *) everneed + iverneed->vn_next));
   8300  1.1  christos 	}
   8301  1.1  christos       elf_tdata (abfd)->cverrefs = i;
   8302  1.1  christos 
   8303  1.1  christos       free (contents);
   8304  1.1  christos       contents = NULL;
   8305  1.1  christos     }
   8306  1.1  christos 
   8307  1.1  christos   if (elf_dynverdef (abfd) != 0)
   8308  1.1  christos     {
   8309  1.1  christos       Elf_Internal_Shdr *hdr;
   8310  1.1  christos       Elf_External_Verdef *everdef;
   8311  1.1  christos       Elf_Internal_Verdef *iverdef;
   8312  1.1  christos       Elf_Internal_Verdef *iverdefarr;
   8313  1.1  christos       Elf_Internal_Verdef iverdefmem;
   8314  1.1  christos       unsigned int i;
   8315  1.1  christos       unsigned int maxidx;
   8316  1.1  christos       bfd_byte *contents_end_def, *contents_end_aux;
   8317  1.5  christos 
   8318  1.5  christos       hdr = &elf_tdata (abfd)->dynverdef_hdr;
   8319  1.5  christos 
   8320  1.9  christos       if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
   8321  1.5  christos 	{
   8322  1.5  christos 	error_return_bad_verdef:
   8323  1.5  christos 	  _bfd_error_handler
   8324  1.5  christos 	    (_("%B: .gnu.version_d invalid entry"), abfd);
   8325  1.5  christos 	  bfd_set_error (bfd_error_bad_value);
   8326  1.5  christos 	error_return_verdef:
   8327  1.5  christos 	  elf_tdata (abfd)->verdef = NULL;
   8328  1.5  christos 	  elf_tdata (abfd)->cverdefs = 0;
   8329  1.1  christos 	  goto error_return;
   8330  1.1  christos 	}
   8331  1.5  christos 
   8332  1.1  christos       contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
   8333  1.1  christos       if (contents == NULL)
   8334  1.5  christos 	goto error_return_verdef;
   8335  1.1  christos       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   8336  1.1  christos 	  || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
   8337  1.1  christos 	goto error_return_verdef;
   8338  1.1  christos 
   8339  1.1  christos       BFD_ASSERT (sizeof (Elf_External_Verdef)
   8340  1.1  christos 		  >= sizeof (Elf_External_Verdaux));
   8341  1.1  christos       contents_end_def = contents + hdr->sh_size
   8342  1.1  christos 			 - sizeof (Elf_External_Verdef);
   8343  1.1  christos       contents_end_aux = contents + hdr->sh_size
   8344  1.1  christos 			 - sizeof (Elf_External_Verdaux);
   8345  1.1  christos 
   8346  1.1  christos       /* We know the number of entries in the section but not the maximum
   8347  1.1  christos 	 index.  Therefore we have to run through all entries and find
   8348  1.1  christos 	 the maximum.  */
   8349  1.1  christos       everdef = (Elf_External_Verdef *) contents;
   8350  1.1  christos       maxidx = 0;
   8351  1.1  christos       for (i = 0; i < hdr->sh_info; ++i)
   8352  1.5  christos 	{
   8353  1.5  christos 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   8354  1.1  christos 
   8355  1.1  christos 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
   8356  1.1  christos 	    goto error_return_bad_verdef;
   8357  1.5  christos 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
   8358  1.5  christos 	    maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
   8359  1.5  christos 
   8360  1.1  christos 	  if (iverdefmem.vd_next == 0)
   8361  1.1  christos 	    break;
   8362  1.5  christos 
   8363  1.1  christos 	  if (iverdefmem.vd_next
   8364  1.1  christos 	      > (size_t) (contents_end_def - (bfd_byte *) everdef))
   8365  1.1  christos 	    goto error_return_bad_verdef;
   8366  1.1  christos 
   8367  1.1  christos 	  everdef = ((Elf_External_Verdef *)
   8368  1.1  christos 		     ((bfd_byte *) everdef + iverdefmem.vd_next));
   8369  1.1  christos 	}
   8370  1.1  christos 
   8371  1.1  christos       if (default_imported_symver)
   8372  1.1  christos 	{
   8373  1.1  christos 	  if (freeidx > maxidx)
   8374  1.1  christos 	    maxidx = ++freeidx;
   8375  1.5  christos 	  else
   8376  1.1  christos 	    freeidx = ++maxidx;
   8377  1.5  christos 	}
   8378  1.1  christos 
   8379  1.5  christos       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
   8380  1.1  christos 	bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
   8381  1.1  christos       if (elf_tdata (abfd)->verdef == NULL)
   8382  1.1  christos 	goto error_return_verdef;
   8383  1.1  christos 
   8384  1.1  christos       elf_tdata (abfd)->cverdefs = maxidx;
   8385  1.1  christos 
   8386  1.1  christos       everdef = (Elf_External_Verdef *) contents;
   8387  1.1  christos       iverdefarr = elf_tdata (abfd)->verdef;
   8388  1.1  christos       for (i = 0; i < hdr->sh_info; i++)
   8389  1.1  christos 	{
   8390  1.1  christos 	  Elf_External_Verdaux *everdaux;
   8391  1.1  christos 	  Elf_Internal_Verdaux *iverdaux;
   8392  1.1  christos 	  unsigned int j;
   8393  1.1  christos 
   8394  1.5  christos 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   8395  1.1  christos 
   8396  1.1  christos 	  if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
   8397  1.8  christos 	    goto error_return_bad_verdef;
   8398  1.1  christos 
   8399  1.1  christos 	  iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
   8400  1.1  christos 	  memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
   8401  1.1  christos 
   8402  1.1  christos 	  iverdef->vd_bfd = abfd;
   8403  1.1  christos 
   8404  1.1  christos 	  if (iverdef->vd_cnt == 0)
   8405  1.1  christos 	    iverdef->vd_auxptr = NULL;
   8406  1.1  christos 	  else
   8407  1.1  christos 	    {
   8408  1.1  christos 	      iverdef->vd_auxptr = (struct elf_internal_verdaux *)
   8409  1.1  christos                   bfd_alloc2 (abfd, iverdef->vd_cnt,
   8410  1.1  christos                               sizeof (Elf_Internal_Verdaux));
   8411  1.1  christos 	      if (iverdef->vd_auxptr == NULL)
   8412  1.1  christos 		goto error_return_verdef;
   8413  1.1  christos 	    }
   8414  1.5  christos 
   8415  1.1  christos 	  if (iverdef->vd_aux
   8416  1.1  christos 	      > (size_t) (contents_end_aux - (bfd_byte *) everdef))
   8417  1.1  christos 	    goto error_return_bad_verdef;
   8418  1.1  christos 
   8419  1.1  christos 	  everdaux = ((Elf_External_Verdaux *)
   8420  1.1  christos 		      ((bfd_byte *) everdef + iverdef->vd_aux));
   8421  1.1  christos 	  iverdaux = iverdef->vd_auxptr;
   8422  1.1  christos 	  for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
   8423  1.1  christos 	    {
   8424  1.1  christos 	      _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
   8425  1.1  christos 
   8426  1.1  christos 	      iverdaux->vda_nodename =
   8427  1.5  christos 		bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   8428  1.1  christos 						 iverdaux->vda_name);
   8429  1.5  christos 	      if (iverdaux->vda_nodename == NULL)
   8430  1.5  christos 		goto error_return_bad_verdef;
   8431  1.5  christos 
   8432  1.5  christos 	      iverdaux->vda_nextptr = NULL;
   8433  1.5  christos 	      if (iverdaux->vda_next == 0)
   8434  1.5  christos 		{
   8435  1.1  christos 		  iverdef->vd_cnt = j + 1;
   8436  1.1  christos 		  break;
   8437  1.1  christos 		}
   8438  1.1  christos 	      if (j + 1 < iverdef->vd_cnt)
   8439  1.1  christos 		iverdaux->vda_nextptr = iverdaux + 1;
   8440  1.5  christos 
   8441  1.1  christos 	      if (iverdaux->vda_next
   8442  1.1  christos 		  > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
   8443  1.1  christos 		goto error_return_bad_verdef;
   8444  1.1  christos 
   8445  1.1  christos 	      everdaux = ((Elf_External_Verdaux *)
   8446  1.8  christos 			  ((bfd_byte *) everdaux + iverdaux->vda_next));
   8447  1.1  christos 	    }
   8448  1.1  christos 
   8449  1.1  christos 	  iverdef->vd_nodename = NULL;
   8450  1.5  christos 	  if (iverdef->vd_cnt)
   8451  1.5  christos 	    iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
   8452  1.5  christos 
   8453  1.1  christos 	  iverdef->vd_nextdef = NULL;
   8454  1.1  christos 	  if (iverdef->vd_next == 0)
   8455  1.1  christos 	    break;
   8456  1.1  christos 	  if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
   8457  1.1  christos 	    iverdef->vd_nextdef = iverdef + 1;
   8458  1.1  christos 
   8459  1.1  christos 	  everdef = ((Elf_External_Verdef *)
   8460  1.1  christos 		     ((bfd_byte *) everdef + iverdef->vd_next));
   8461  1.1  christos 	}
   8462  1.1  christos 
   8463  1.1  christos       free (contents);
   8464  1.1  christos       contents = NULL;
   8465  1.1  christos     }
   8466  1.1  christos   else if (default_imported_symver)
   8467  1.1  christos     {
   8468  1.1  christos       if (freeidx < 3)
   8469  1.1  christos 	freeidx = 3;
   8470  1.1  christos       else
   8471  1.1  christos 	freeidx++;
   8472  1.1  christos 
   8473  1.1  christos       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
   8474  1.1  christos           bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
   8475  1.1  christos       if (elf_tdata (abfd)->verdef == NULL)
   8476  1.1  christos 	goto error_return;
   8477  1.1  christos 
   8478  1.1  christos       elf_tdata (abfd)->cverdefs = freeidx;
   8479  1.1  christos     }
   8480  1.1  christos 
   8481  1.1  christos   /* Create a default version based on the soname.  */
   8482  1.1  christos   if (default_imported_symver)
   8483  1.1  christos     {
   8484  1.3  christos       Elf_Internal_Verdef *iverdef;
   8485  1.1  christos       Elf_Internal_Verdaux *iverdaux;
   8486  1.1  christos 
   8487  1.1  christos       iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
   8488  1.1  christos 
   8489  1.1  christos       iverdef->vd_version = VER_DEF_CURRENT;
   8490  1.1  christos       iverdef->vd_flags = 0;
   8491  1.1  christos       iverdef->vd_ndx = freeidx;
   8492  1.1  christos       iverdef->vd_cnt = 1;
   8493  1.1  christos 
   8494  1.1  christos       iverdef->vd_bfd = abfd;
   8495  1.1  christos 
   8496  1.1  christos       iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
   8497  1.5  christos       if (iverdef->vd_nodename == NULL)
   8498  1.5  christos 	goto error_return_verdef;
   8499  1.1  christos       iverdef->vd_nextdef = NULL;
   8500  1.1  christos       iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
   8501  1.1  christos 			    bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
   8502  1.1  christos       if (iverdef->vd_auxptr == NULL)
   8503  1.1  christos 	goto error_return_verdef;
   8504  1.1  christos 
   8505  1.1  christos       iverdaux = iverdef->vd_auxptr;
   8506  1.1  christos       iverdaux->vda_nodename = iverdef->vd_nodename;
   8507  1.1  christos     }
   8508  1.1  christos 
   8509  1.1  christos   return TRUE;
   8510  1.1  christos 
   8511  1.1  christos  error_return:
   8512  1.1  christos   if (contents != NULL)
   8513  1.1  christos     free (contents);
   8514  1.1  christos   return FALSE;
   8515  1.1  christos }
   8516  1.1  christos 
   8517  1.1  christos asymbol *
   8519  1.5  christos _bfd_elf_make_empty_symbol (bfd *abfd)
   8520  1.1  christos {
   8521  1.1  christos   elf_symbol_type *newsym;
   8522  1.5  christos 
   8523  1.5  christos   newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym);
   8524  1.1  christos   if (!newsym)
   8525  1.1  christos     return NULL;
   8526  1.1  christos   newsym->symbol.the_bfd = abfd;
   8527  1.1  christos   return &newsym->symbol;
   8528  1.1  christos }
   8529  1.1  christos 
   8530  1.1  christos void
   8531  1.1  christos _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
   8532  1.1  christos 			  asymbol *symbol,
   8533  1.1  christos 			  symbol_info *ret)
   8534  1.1  christos {
   8535  1.1  christos   bfd_symbol_info (symbol, ret);
   8536  1.1  christos }
   8537  1.1  christos 
   8538  1.1  christos /* Return whether a symbol name implies a local symbol.  Most targets
   8539  1.1  christos    use this function for the is_local_label_name entry point, but some
   8540  1.1  christos    override it.  */
   8541  1.1  christos 
   8542  1.1  christos bfd_boolean
   8543  1.1  christos _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
   8544  1.1  christos 			      const char *name)
   8545  1.1  christos {
   8546  1.1  christos   /* Normal local symbols start with ``.L''.  */
   8547  1.1  christos   if (name[0] == '.' && name[1] == 'L')
   8548  1.1  christos     return TRUE;
   8549  1.1  christos 
   8550  1.1  christos   /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
   8551  1.1  christos      DWARF debugging symbols starting with ``..''.  */
   8552  1.1  christos   if (name[0] == '.' && name[1] == '.')
   8553  1.1  christos     return TRUE;
   8554  1.1  christos 
   8555  1.1  christos   /* gcc will sometimes generate symbols beginning with ``_.L_'' when
   8556  1.1  christos      emitting DWARF debugging output.  I suspect this is actually a
   8557  1.1  christos      small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
   8558  1.1  christos      ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
   8559  1.1  christos      underscore to be emitted on some ELF targets).  For ease of use,
   8560  1.6  christos      we treat such symbols as local.  */
   8561  1.6  christos   if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
   8562  1.6  christos     return TRUE;
   8563  1.6  christos 
   8564  1.6  christos   /* Treat assembler generated fake symbols, dollar local labels and
   8565  1.6  christos      forward-backward labels (aka local labels) as locals.
   8566  1.6  christos      These labels have the form:
   8567  1.6  christos 
   8568  1.6  christos        L0^A.*                                  (fake symbols)
   8569  1.6  christos 
   8570  1.6  christos        [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
   8571  1.6  christos 
   8572  1.6  christos      Versions which start with .L will have already been matched above,
   8573  1.6  christos      so we only need to match the rest.  */
   8574  1.6  christos   if (name[0] == 'L' && ISDIGIT (name[1]))
   8575  1.6  christos     {
   8576  1.6  christos       bfd_boolean ret = FALSE;
   8577  1.6  christos       const char * p;
   8578  1.6  christos       char c;
   8579  1.6  christos 
   8580  1.6  christos       for (p = name + 2; (c = *p); p++)
   8581  1.6  christos 	{
   8582  1.6  christos 	  if (c == 1 || c == 2)
   8583  1.6  christos 	    {
   8584  1.6  christos 	      if (c == 1 && p == name + 2)
   8585  1.6  christos 		/* A fake symbol.  */
   8586  1.6  christos 		return TRUE;
   8587  1.6  christos 
   8588  1.6  christos 	      /* FIXME: We are being paranoid here and treating symbols like
   8589  1.6  christos 		 L0^Bfoo as if there were non-local, on the grounds that the
   8590  1.6  christos 		 assembler will never generate them.  But can any symbol
   8591  1.6  christos 		 containing an ASCII value in the range 1-31 ever be anything
   8592  1.6  christos 		 other than some kind of local ?  */
   8593  1.6  christos 	      ret = TRUE;
   8594  1.6  christos 	    }
   8595  1.6  christos 
   8596  1.6  christos 	  if (! ISDIGIT (c))
   8597  1.6  christos 	    {
   8598  1.6  christos 	      ret = FALSE;
   8599  1.6  christos 	      break;
   8600  1.6  christos 	    }
   8601  1.1  christos 	}
   8602  1.1  christos       return ret;
   8603  1.1  christos     }
   8604  1.1  christos 
   8605  1.1  christos   return FALSE;
   8606  1.1  christos }
   8607  1.1  christos 
   8608  1.1  christos alent *
   8609  1.1  christos _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
   8610  1.1  christos 		     asymbol *symbol ATTRIBUTE_UNUSED)
   8611  1.1  christos {
   8612  1.1  christos   abort ();
   8613  1.1  christos   return NULL;
   8614  1.1  christos }
   8615  1.1  christos 
   8616  1.1  christos bfd_boolean
   8617  1.1  christos _bfd_elf_set_arch_mach (bfd *abfd,
   8618  1.1  christos 			enum bfd_architecture arch,
   8619  1.1  christos 			unsigned long machine)
   8620  1.1  christos {
   8621  1.1  christos   /* If this isn't the right architecture for this backend, and this
   8622  1.1  christos      isn't the generic backend, fail.  */
   8623  1.1  christos   if (arch != get_elf_backend_data (abfd)->arch
   8624  1.1  christos       && arch != bfd_arch_unknown
   8625  1.1  christos       && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
   8626  1.1  christos     return FALSE;
   8627  1.1  christos 
   8628  1.1  christos   return bfd_default_set_arch_mach (abfd, arch, machine);
   8629  1.1  christos }
   8630  1.1  christos 
   8631  1.1  christos /* Find the nearest line to a particular section and offset,
   8632  1.5  christos    for error reporting.  */
   8633  1.1  christos 
   8634  1.1  christos bfd_boolean
   8635  1.1  christos _bfd_elf_find_nearest_line (bfd *abfd,
   8636  1.1  christos 			    asymbol **symbols,
   8637  1.5  christos 			    asection *section,
   8638  1.5  christos 			    bfd_vma offset,
   8639  1.3  christos 			    const char **filename_ptr,
   8640  1.1  christos 			    const char **functionname_ptr,
   8641  1.1  christos 			    unsigned int *line_ptr,
   8642  1.5  christos 			    unsigned int *discriminator_ptr)
   8643  1.1  christos {
   8644  1.5  christos   bfd_boolean found;
   8645  1.5  christos 
   8646  1.5  christos   if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
   8647  1.5  christos 				     filename_ptr, functionname_ptr,
   8648  1.5  christos 				     line_ptr, discriminator_ptr,
   8649  1.5  christos 				     dwarf_debug_sections, 0,
   8650  1.1  christos 				     &elf_tdata (abfd)->dwarf2_find_line_info)
   8651  1.1  christos       || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
   8652  1.5  christos 					filename_ptr, functionname_ptr,
   8653  1.5  christos 					line_ptr))
   8654  1.5  christos     {
   8655  1.1  christos       if (!*functionname_ptr)
   8656  1.1  christos 	_bfd_elf_find_function (abfd, symbols, section, offset,
   8657  1.1  christos 				*filename_ptr ? NULL : filename_ptr,
   8658  1.1  christos 				functionname_ptr);
   8659  1.1  christos       return TRUE;
   8660  1.1  christos     }
   8661  1.1  christos 
   8662  1.1  christos   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
   8663  1.1  christos 					     &found, filename_ptr,
   8664  1.1  christos 					     functionname_ptr, line_ptr,
   8665  1.1  christos 					     &elf_tdata (abfd)->line_info))
   8666  1.1  christos     return FALSE;
   8667  1.1  christos   if (found && (*functionname_ptr || *line_ptr))
   8668  1.1  christos     return TRUE;
   8669  1.5  christos 
   8670  1.5  christos   if (symbols == NULL)
   8671  1.1  christos     return FALSE;
   8672  1.1  christos 
   8673  1.1  christos   if (! _bfd_elf_find_function (abfd, symbols, section, offset,
   8674  1.1  christos 				filename_ptr, functionname_ptr))
   8675  1.1  christos     return FALSE;
   8676  1.1  christos 
   8677  1.1  christos   *line_ptr = 0;
   8678  1.1  christos   return TRUE;
   8679  1.1  christos }
   8680  1.1  christos 
   8681  1.1  christos /* Find the line for a symbol.  */
   8682  1.1  christos 
   8683  1.5  christos bfd_boolean
   8684  1.5  christos _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
   8685  1.5  christos 		    const char **filename_ptr, unsigned int *line_ptr)
   8686  1.5  christos {
   8687  1.1  christos   return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
   8688  1.1  christos 					filename_ptr, NULL, line_ptr, NULL,
   8689  1.1  christos 					dwarf_debug_sections, 0,
   8690  1.1  christos 					&elf_tdata (abfd)->dwarf2_find_line_info);
   8691  1.1  christos }
   8692  1.1  christos 
   8693  1.1  christos /* After a call to bfd_find_nearest_line, successive calls to
   8694  1.1  christos    bfd_find_inliner_info can be used to get source information about
   8695  1.1  christos    each level of function inlining that terminated at the address
   8696  1.1  christos    passed to bfd_find_nearest_line.  Currently this is only supported
   8697  1.1  christos    for DWARF2 with appropriate DWARF3 extensions. */
   8698  1.1  christos 
   8699  1.1  christos bfd_boolean
   8700  1.1  christos _bfd_elf_find_inliner_info (bfd *abfd,
   8701  1.1  christos 			    const char **filename_ptr,
   8702  1.1  christos 			    const char **functionname_ptr,
   8703  1.1  christos 			    unsigned int *line_ptr)
   8704  1.1  christos {
   8705  1.1  christos   bfd_boolean found;
   8706  1.1  christos   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
   8707  1.1  christos 					 functionname_ptr, line_ptr,
   8708  1.1  christos 					 & elf_tdata (abfd)->dwarf2_find_line_info);
   8709  1.1  christos   return found;
   8710  1.1  christos }
   8711  1.1  christos 
   8712  1.1  christos int
   8713  1.1  christos _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
   8714  1.8  christos {
   8715  1.1  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8716  1.3  christos   int ret = bed->s->sizeof_ehdr;
   8717  1.1  christos 
   8718  1.1  christos   if (!bfd_link_relocatable (info))
   8719  1.1  christos     {
   8720  1.1  christos       bfd_size_type phdr_size = elf_program_header_size (abfd);
   8721  1.1  christos 
   8722  1.1  christos       if (phdr_size == (bfd_size_type) -1)
   8723  1.3  christos 	{
   8724  1.1  christos 	  struct elf_segment_map *m;
   8725  1.1  christos 
   8726  1.1  christos 	  phdr_size = 0;
   8727  1.1  christos 	  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   8728  1.1  christos 	    phdr_size += bed->s->sizeof_phdr;
   8729  1.1  christos 
   8730  1.3  christos 	  if (phdr_size == 0)
   8731  1.1  christos 	    phdr_size = get_program_header_size (abfd, info);
   8732  1.1  christos 	}
   8733  1.1  christos 
   8734  1.1  christos       elf_program_header_size (abfd) = phdr_size;
   8735  1.1  christos       ret += phdr_size;
   8736  1.1  christos     }
   8737  1.1  christos 
   8738  1.1  christos   return ret;
   8739  1.1  christos }
   8740  1.1  christos 
   8741  1.1  christos bfd_boolean
   8742  1.1  christos _bfd_elf_set_section_contents (bfd *abfd,
   8743  1.1  christos 			       sec_ptr section,
   8744  1.1  christos 			       const void *location,
   8745  1.5  christos 			       file_ptr offset,
   8746  1.1  christos 			       bfd_size_type count)
   8747  1.1  christos {
   8748  1.1  christos   Elf_Internal_Shdr *hdr;
   8749  1.1  christos   file_ptr pos;
   8750  1.1  christos 
   8751  1.6  christos   if (! abfd->output_has_begun
   8752  1.6  christos       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   8753  1.6  christos     return FALSE;
   8754  1.1  christos 
   8755  1.6  christos   if (!count)
   8756  1.6  christos     return TRUE;
   8757  1.6  christos 
   8758  1.6  christos   hdr = &elf_section_data (section)->this_hdr;
   8759  1.6  christos   if (hdr->sh_offset == (file_ptr) -1)
   8760  1.6  christos     {
   8761  1.6  christos       /* We must compress this section.  Write output to the buffer.  */
   8762  1.6  christos       unsigned char *contents = hdr->contents;
   8763  1.6  christos       if ((offset + count) > hdr->sh_size
   8764  1.6  christos 	  || (section->flags & SEC_ELF_COMPRESS) == 0
   8765  1.6  christos 	  || contents == NULL)
   8766  1.1  christos 	abort ();
   8767  1.1  christos       memcpy (contents + offset, location, count);
   8768  1.1  christos       return TRUE;
   8769  1.1  christos     }
   8770  1.1  christos   pos = hdr->sh_offset + offset;
   8771  1.1  christos   if (bfd_seek (abfd, pos, SEEK_SET) != 0
   8772  1.1  christos       || bfd_bwrite (location, count, abfd) != count)
   8773  1.1  christos     return FALSE;
   8774  1.1  christos 
   8775  1.1  christos   return TRUE;
   8776  1.1  christos }
   8777  1.1  christos 
   8778  1.1  christos void
   8779  1.1  christos _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
   8780  1.1  christos 			   arelent *cache_ptr ATTRIBUTE_UNUSED,
   8781  1.1  christos 			   Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
   8782  1.1  christos {
   8783  1.1  christos   abort ();
   8784  1.1  christos }
   8785  1.1  christos 
   8786  1.1  christos /* Try to convert a non-ELF reloc into an ELF one.  */
   8787  1.1  christos 
   8788  1.1  christos bfd_boolean
   8789  1.1  christos _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
   8790  1.1  christos {
   8791  1.1  christos   /* Check whether we really have an ELF howto.  */
   8792  1.1  christos 
   8793  1.1  christos   if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
   8794  1.1  christos     {
   8795  1.1  christos       bfd_reloc_code_real_type code;
   8796  1.1  christos       reloc_howto_type *howto;
   8797  1.1  christos 
   8798  1.1  christos       /* Alien reloc: Try to determine its type to replace it with an
   8799  1.1  christos 	 equivalent ELF reloc.  */
   8800  1.1  christos 
   8801  1.1  christos       if (areloc->howto->pc_relative)
   8802  1.1  christos 	{
   8803  1.1  christos 	  switch (areloc->howto->bitsize)
   8804  1.1  christos 	    {
   8805  1.1  christos 	    case 8:
   8806  1.1  christos 	      code = BFD_RELOC_8_PCREL;
   8807  1.1  christos 	      break;
   8808  1.1  christos 	    case 12:
   8809  1.1  christos 	      code = BFD_RELOC_12_PCREL;
   8810  1.1  christos 	      break;
   8811  1.1  christos 	    case 16:
   8812  1.1  christos 	      code = BFD_RELOC_16_PCREL;
   8813  1.1  christos 	      break;
   8814  1.1  christos 	    case 24:
   8815  1.1  christos 	      code = BFD_RELOC_24_PCREL;
   8816  1.1  christos 	      break;
   8817  1.1  christos 	    case 32:
   8818  1.1  christos 	      code = BFD_RELOC_32_PCREL;
   8819  1.1  christos 	      break;
   8820  1.1  christos 	    case 64:
   8821  1.1  christos 	      code = BFD_RELOC_64_PCREL;
   8822  1.1  christos 	      break;
   8823  1.1  christos 	    default:
   8824  1.1  christos 	      goto fail;
   8825  1.1  christos 	    }
   8826  1.1  christos 
   8827  1.1  christos 	  howto = bfd_reloc_type_lookup (abfd, code);
   8828  1.1  christos 
   8829  1.1  christos 	  if (areloc->howto->pcrel_offset != howto->pcrel_offset)
   8830  1.1  christos 	    {
   8831  1.1  christos 	      if (howto->pcrel_offset)
   8832  1.1  christos 		areloc->addend += areloc->address;
   8833  1.1  christos 	      else
   8834  1.1  christos 		areloc->addend -= areloc->address; /* addend is unsigned!! */
   8835  1.1  christos 	    }
   8836  1.1  christos 	}
   8837  1.1  christos       else
   8838  1.1  christos 	{
   8839  1.1  christos 	  switch (areloc->howto->bitsize)
   8840  1.1  christos 	    {
   8841  1.1  christos 	    case 8:
   8842  1.1  christos 	      code = BFD_RELOC_8;
   8843  1.1  christos 	      break;
   8844  1.1  christos 	    case 14:
   8845  1.1  christos 	      code = BFD_RELOC_14;
   8846  1.1  christos 	      break;
   8847  1.1  christos 	    case 16:
   8848  1.1  christos 	      code = BFD_RELOC_16;
   8849  1.1  christos 	      break;
   8850  1.1  christos 	    case 26:
   8851  1.1  christos 	      code = BFD_RELOC_26;
   8852  1.1  christos 	      break;
   8853  1.1  christos 	    case 32:
   8854  1.1  christos 	      code = BFD_RELOC_32;
   8855  1.1  christos 	      break;
   8856  1.1  christos 	    case 64:
   8857  1.1  christos 	      code = BFD_RELOC_64;
   8858  1.1  christos 	      break;
   8859  1.1  christos 	    default:
   8860  1.1  christos 	      goto fail;
   8861  1.1  christos 	    }
   8862  1.1  christos 
   8863  1.1  christos 	  howto = bfd_reloc_type_lookup (abfd, code);
   8864  1.1  christos 	}
   8865  1.1  christos 
   8866  1.1  christos       if (howto)
   8867  1.1  christos 	areloc->howto = howto;
   8868  1.1  christos       else
   8869  1.1  christos 	goto fail;
   8870  1.1  christos     }
   8871  1.9  christos 
   8872  1.9  christos   return TRUE;
   8873  1.1  christos 
   8874  1.1  christos  fail:
   8875  1.1  christos   _bfd_error_handler
   8876  1.1  christos     /* xgettext:c-format */
   8877  1.1  christos     (_("%B: unsupported relocation type %s"),
   8878  1.1  christos      abfd, areloc->howto->name);
   8879  1.1  christos   bfd_set_error (bfd_error_bad_value);
   8880  1.1  christos   return FALSE;
   8881  1.1  christos }
   8882  1.3  christos 
   8883  1.3  christos bfd_boolean
   8884  1.1  christos _bfd_elf_close_and_cleanup (bfd *abfd)
   8885  1.3  christos {
   8886  1.1  christos   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   8887  1.3  christos   if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
   8888  1.1  christos     {
   8889  1.1  christos       if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
   8890  1.1  christos 	_bfd_elf_strtab_free (elf_shstrtab (abfd));
   8891  1.1  christos       _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
   8892  1.1  christos     }
   8893  1.1  christos 
   8894  1.1  christos   return _bfd_generic_close_and_cleanup (abfd);
   8895  1.1  christos }
   8896  1.1  christos 
   8897  1.1  christos /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
   8898  1.1  christos    in the relocation's offset.  Thus we cannot allow any sort of sanity
   8899  1.1  christos    range-checking to interfere.  There is nothing else to do in processing
   8900  1.1  christos    this reloc.  */
   8901  1.1  christos 
   8902  1.1  christos bfd_reloc_status_type
   8903  1.1  christos _bfd_elf_rel_vtable_reloc_fn
   8904  1.1  christos   (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
   8905  1.1  christos    struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
   8906  1.1  christos    void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
   8907  1.1  christos    bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
   8908  1.1  christos {
   8909  1.1  christos   return bfd_reloc_ok;
   8910  1.1  christos }
   8911  1.1  christos 
   8912  1.1  christos /* Elf core file support.  Much of this only works on native
   8914  1.1  christos    toolchains, since we rely on knowing the
   8915  1.1  christos    machine-dependent procfs structure in order to pick
   8916  1.1  christos    out details about the corefile.  */
   8917  1.1  christos 
   8918  1.1  christos #ifdef HAVE_SYS_PROCFS_H
   8919  1.1  christos /* Needed for new procfs interface on sparc-solaris.  */
   8920  1.1  christos # define _STRUCTURED_PROC 1
   8921  1.1  christos # include <sys/procfs.h>
   8922  1.1  christos #endif
   8923  1.1  christos 
   8924  1.1  christos /* Return a PID that identifies a "thread" for threaded cores, or the
   8925  1.1  christos    PID of the main process for non-threaded cores.  */
   8926  1.1  christos 
   8927  1.3  christos static int
   8928  1.1  christos elfcore_make_pid (bfd *abfd)
   8929  1.3  christos {
   8930  1.1  christos   int pid;
   8931  1.1  christos 
   8932  1.1  christos   pid = elf_tdata (abfd)->core->lwpid;
   8933  1.1  christos   if (pid == 0)
   8934  1.1  christos     pid = elf_tdata (abfd)->core->pid;
   8935  1.1  christos 
   8936  1.1  christos   return pid;
   8937  1.1  christos }
   8938  1.1  christos 
   8939  1.1  christos /* If there isn't a section called NAME, make one, using
   8940  1.1  christos    data from SECT.  Note, this function will generate a
   8941  1.1  christos    reference to NAME, so you shouldn't deallocate or
   8942  1.1  christos    overwrite it.  */
   8943  1.1  christos 
   8944  1.1  christos static bfd_boolean
   8945  1.1  christos elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
   8946  1.1  christos {
   8947  1.1  christos   asection *sect2;
   8948  1.1  christos 
   8949  1.1  christos   if (bfd_get_section_by_name (abfd, name) != NULL)
   8950  1.1  christos     return TRUE;
   8951  1.1  christos 
   8952  1.1  christos   sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
   8953  1.1  christos   if (sect2 == NULL)
   8954  1.1  christos     return FALSE;
   8955  1.1  christos 
   8956  1.1  christos   sect2->size = sect->size;
   8957  1.1  christos   sect2->filepos = sect->filepos;
   8958  1.1  christos   sect2->alignment_power = sect->alignment_power;
   8959  1.1  christos   return TRUE;
   8960  1.1  christos }
   8961  1.1  christos 
   8962  1.1  christos /* Create a pseudosection containing SIZE bytes at FILEPOS.  This
   8963  1.1  christos    actually creates up to two pseudosections:
   8964  1.1  christos    - For the single-threaded case, a section named NAME, unless
   8965  1.1  christos      such a section already exists.
   8966  1.1  christos    - For the multi-threaded case, a section named "NAME/PID", where
   8967  1.1  christos      PID is elfcore_make_pid (abfd).
   8968  1.1  christos    Both pseudosections have identical contents. */
   8969  1.1  christos bfd_boolean
   8970  1.1  christos _bfd_elfcore_make_pseudosection (bfd *abfd,
   8971  1.1  christos 				 char *name,
   8972  1.1  christos 				 size_t size,
   8973  1.1  christos 				 ufile_ptr filepos)
   8974  1.1  christos {
   8975  1.1  christos   char buf[100];
   8976  1.1  christos   char *threaded_name;
   8977  1.1  christos   size_t len;
   8978  1.1  christos   asection *sect;
   8979  1.1  christos 
   8980  1.1  christos   /* Build the section name.  */
   8981  1.1  christos 
   8982  1.1  christos   sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
   8983  1.1  christos   len = strlen (buf) + 1;
   8984  1.1  christos   threaded_name = (char *) bfd_alloc (abfd, len);
   8985  1.1  christos   if (threaded_name == NULL)
   8986  1.1  christos     return FALSE;
   8987  1.1  christos   memcpy (threaded_name, buf, len);
   8988  1.1  christos 
   8989  1.1  christos   sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
   8990  1.1  christos 					     SEC_HAS_CONTENTS);
   8991  1.1  christos   if (sect == NULL)
   8992  1.1  christos     return FALSE;
   8993  1.1  christos   sect->size = size;
   8994  1.1  christos   sect->filepos = filepos;
   8995  1.1  christos   sect->alignment_power = 2;
   8996  1.1  christos 
   8997  1.1  christos   return elfcore_maybe_make_sect (abfd, name, sect);
   8998  1.1  christos }
   8999  1.1  christos 
   9000  1.1  christos /* prstatus_t exists on:
   9001  1.1  christos      solaris 2.5+
   9002  1.1  christos      linux 2.[01] + glibc
   9003  1.1  christos      unixware 4.2
   9004  1.1  christos */
   9005  1.1  christos 
   9006  1.1  christos #if defined (HAVE_PRSTATUS_T)
   9007  1.1  christos 
   9008  1.1  christos static bfd_boolean
   9009  1.1  christos elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   9010  1.1  christos {
   9011  1.1  christos   size_t size;
   9012  1.1  christos   int offset;
   9013  1.1  christos 
   9014  1.1  christos   if (note->descsz == sizeof (prstatus_t))
   9015  1.1  christos     {
   9016  1.1  christos       prstatus_t prstat;
   9017  1.1  christos 
   9018  1.1  christos       size = sizeof (prstat.pr_reg);
   9019  1.3  christos       offset   = offsetof (prstatus_t, pr_reg);
   9020  1.3  christos       memcpy (&prstat, note->descdata, sizeof (prstat));
   9021  1.3  christos 
   9022  1.3  christos       /* Do not overwrite the core signal if it
   9023  1.1  christos 	 has already been set by another thread.  */
   9024  1.1  christos       if (elf_tdata (abfd)->core->signal == 0)
   9025  1.1  christos 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   9026  1.1  christos       if (elf_tdata (abfd)->core->pid == 0)
   9027  1.1  christos 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   9028  1.1  christos 
   9029  1.1  christos       /* pr_who exists on:
   9030  1.1  christos 	 solaris 2.5+
   9031  1.3  christos 	 unixware 4.2
   9032  1.1  christos 	 pr_who doesn't exist on:
   9033  1.3  christos 	 linux 2.[01]
   9034  1.1  christos 	 */
   9035  1.1  christos #if defined (HAVE_PRSTATUS_T_PR_WHO)
   9036  1.1  christos       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   9037  1.1  christos #else
   9038  1.1  christos       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   9039  1.1  christos #endif
   9040  1.1  christos     }
   9041  1.1  christos #if defined (HAVE_PRSTATUS32_T)
   9042  1.1  christos   else if (note->descsz == sizeof (prstatus32_t))
   9043  1.1  christos     {
   9044  1.1  christos       /* 64-bit host, 32-bit corefile */
   9045  1.1  christos       prstatus32_t prstat;
   9046  1.1  christos 
   9047  1.1  christos       size = sizeof (prstat.pr_reg);
   9048  1.3  christos       offset   = offsetof (prstatus32_t, pr_reg);
   9049  1.3  christos       memcpy (&prstat, note->descdata, sizeof (prstat));
   9050  1.3  christos 
   9051  1.3  christos       /* Do not overwrite the core signal if it
   9052  1.1  christos 	 has already been set by another thread.  */
   9053  1.1  christos       if (elf_tdata (abfd)->core->signal == 0)
   9054  1.1  christos 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   9055  1.1  christos       if (elf_tdata (abfd)->core->pid == 0)
   9056  1.1  christos 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   9057  1.1  christos 
   9058  1.1  christos       /* pr_who exists on:
   9059  1.1  christos 	 solaris 2.5+
   9060  1.3  christos 	 unixware 4.2
   9061  1.1  christos 	 pr_who doesn't exist on:
   9062  1.3  christos 	 linux 2.[01]
   9063  1.1  christos 	 */
   9064  1.1  christos #if defined (HAVE_PRSTATUS32_T_PR_WHO)
   9065  1.1  christos       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   9066  1.1  christos #else
   9067  1.1  christos       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   9068  1.1  christos #endif
   9069  1.1  christos     }
   9070  1.1  christos #endif /* HAVE_PRSTATUS32_T */
   9071  1.1  christos   else
   9072  1.1  christos     {
   9073  1.1  christos       /* Fail - we don't know how to handle any other
   9074  1.1  christos 	 note size (ie. data object type).  */
   9075  1.1  christos       return TRUE;
   9076  1.1  christos     }
   9077  1.1  christos 
   9078  1.1  christos   /* Make a ".reg/999" section and a ".reg" section.  */
   9079  1.1  christos   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   9080  1.1  christos 					  size, note->descpos + offset);
   9081  1.1  christos }
   9082  1.1  christos #endif /* defined (HAVE_PRSTATUS_T) */
   9083  1.1  christos 
   9084  1.1  christos /* Create a pseudosection containing the exact contents of NOTE.  */
   9085  1.1  christos static bfd_boolean
   9086  1.1  christos elfcore_make_note_pseudosection (bfd *abfd,
   9087  1.1  christos 				 char *name,
   9088  1.1  christos 				 Elf_Internal_Note *note)
   9089  1.1  christos {
   9090  1.1  christos   return _bfd_elfcore_make_pseudosection (abfd, name,
   9091  1.1  christos 					  note->descsz, note->descpos);
   9092  1.1  christos }
   9093  1.1  christos 
   9094  1.1  christos /* There isn't a consistent prfpregset_t across platforms,
   9095  1.1  christos    but it doesn't matter, because we don't have to pick this
   9096  1.1  christos    data structure apart.  */
   9097  1.1  christos 
   9098  1.1  christos static bfd_boolean
   9099  1.1  christos elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
   9100  1.1  christos {
   9101  1.1  christos   return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   9102  1.1  christos }
   9103  1.1  christos 
   9104  1.1  christos /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
   9105  1.1  christos    type of NT_PRXFPREG.  Just include the whole note's contents
   9106  1.1  christos    literally.  */
   9107  1.1  christos 
   9108  1.1  christos static bfd_boolean
   9109  1.1  christos elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
   9110  1.1  christos {
   9111  1.1  christos   return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
   9112  1.1  christos }
   9113  1.1  christos 
   9114  1.1  christos /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
   9115  1.1  christos    with a note type of NT_X86_XSTATE.  Just include the whole note's
   9116  1.1  christos    contents literally.  */
   9117  1.1  christos 
   9118  1.1  christos static bfd_boolean
   9119  1.1  christos elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
   9120  1.1  christos {
   9121  1.1  christos   return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
   9122  1.1  christos }
   9123  1.1  christos 
   9124  1.1  christos static bfd_boolean
   9125  1.1  christos elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
   9126  1.1  christos {
   9127  1.1  christos   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
   9128  1.1  christos }
   9129  1.1  christos 
   9130  1.1  christos static bfd_boolean
   9131  1.1  christos elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
   9132  1.1  christos {
   9133  1.1  christos   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
   9134  1.1  christos }
   9135  1.1  christos 
   9136  1.1  christos static bfd_boolean
   9137  1.1  christos elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
   9138  1.1  christos {
   9139  1.1  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
   9140  1.1  christos }
   9141  1.1  christos 
   9142  1.1  christos static bfd_boolean
   9143  1.1  christos elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
   9144  1.1  christos {
   9145  1.1  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
   9146  1.1  christos }
   9147  1.1  christos 
   9148  1.1  christos static bfd_boolean
   9149  1.1  christos elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
   9150  1.1  christos {
   9151  1.1  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
   9152  1.1  christos }
   9153  1.1  christos 
   9154  1.1  christos static bfd_boolean
   9155  1.1  christos elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
   9156  1.1  christos {
   9157  1.1  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
   9158  1.1  christos }
   9159  1.1  christos 
   9160  1.1  christos static bfd_boolean
   9161  1.1  christos elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
   9162  1.1  christos {
   9163  1.1  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
   9164  1.1  christos }
   9165  1.1  christos 
   9166  1.1  christos static bfd_boolean
   9167  1.3  christos elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
   9168  1.3  christos {
   9169  1.3  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
   9170  1.3  christos }
   9171  1.3  christos 
   9172  1.3  christos static bfd_boolean
   9173  1.3  christos elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
   9174  1.3  christos {
   9175  1.3  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
   9176  1.3  christos }
   9177  1.3  christos 
   9178  1.3  christos static bfd_boolean
   9179  1.3  christos elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
   9180  1.3  christos {
   9181  1.3  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
   9182  1.3  christos }
   9183  1.3  christos 
   9184  1.3  christos static bfd_boolean
   9185  1.3  christos elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
   9186  1.6  christos {
   9187  1.6  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
   9188  1.6  christos }
   9189  1.6  christos 
   9190  1.6  christos static bfd_boolean
   9191  1.6  christos elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
   9192  1.6  christos {
   9193  1.6  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
   9194  1.6  christos }
   9195  1.6  christos 
   9196  1.6  christos static bfd_boolean
   9197  1.6  christos elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
   9198  1.3  christos {
   9199  1.3  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
   9200  1.3  christos }
   9201  1.3  christos 
   9202  1.3  christos static bfd_boolean
   9203  1.3  christos elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
   9204  1.3  christos {
   9205  1.3  christos   return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
   9206  1.3  christos }
   9207  1.3  christos 
   9208  1.3  christos static bfd_boolean
   9209  1.3  christos elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
   9210  1.3  christos {
   9211  1.3  christos   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
   9212  1.3  christos }
   9213  1.3  christos 
   9214  1.3  christos static bfd_boolean
   9215  1.3  christos elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
   9216  1.3  christos {
   9217  1.3  christos   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
   9218  1.3  christos }
   9219  1.3  christos 
   9220  1.3  christos static bfd_boolean
   9221  1.1  christos elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
   9222  1.1  christos {
   9223  1.1  christos   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
   9224  1.1  christos }
   9225  1.1  christos 
   9226  1.1  christos #if defined (HAVE_PRPSINFO_T)
   9227  1.1  christos typedef prpsinfo_t   elfcore_psinfo_t;
   9228  1.1  christos #if defined (HAVE_PRPSINFO32_T)		/* Sparc64 cross Sparc32 */
   9229  1.1  christos typedef prpsinfo32_t elfcore_psinfo32_t;
   9230  1.1  christos #endif
   9231  1.1  christos #endif
   9232  1.1  christos 
   9233  1.1  christos #if defined (HAVE_PSINFO_T)
   9234  1.1  christos typedef psinfo_t   elfcore_psinfo_t;
   9235  1.1  christos #if defined (HAVE_PSINFO32_T)		/* Sparc64 cross Sparc32 */
   9236  1.1  christos typedef psinfo32_t elfcore_psinfo32_t;
   9237  1.1  christos #endif
   9238  1.1  christos #endif
   9239  1.1  christos 
   9240  1.1  christos /* return a malloc'ed copy of a string at START which is at
   9241  1.1  christos    most MAX bytes long, possibly without a terminating '\0'.
   9242  1.1  christos    the copy will always have a terminating '\0'.  */
   9243  1.1  christos 
   9244  1.1  christos char *
   9245  1.1  christos _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
   9246  1.1  christos {
   9247  1.1  christos   char *dups;
   9248  1.1  christos   char *end = (char *) memchr (start, '\0', max);
   9249  1.1  christos   size_t len;
   9250  1.1  christos 
   9251  1.1  christos   if (end == NULL)
   9252  1.1  christos     len = max;
   9253  1.1  christos   else
   9254  1.1  christos     len = end - start;
   9255  1.1  christos 
   9256  1.1  christos   dups = (char *) bfd_alloc (abfd, len + 1);
   9257  1.1  christos   if (dups == NULL)
   9258  1.1  christos     return NULL;
   9259  1.1  christos 
   9260  1.1  christos   memcpy (dups, start, len);
   9261  1.1  christos   dups[len] = '\0';
   9262  1.1  christos 
   9263  1.1  christos   return dups;
   9264  1.1  christos }
   9265  1.1  christos 
   9266  1.1  christos #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   9267  1.1  christos static bfd_boolean
   9268  1.1  christos elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   9269  1.1  christos {
   9270  1.1  christos   if (note->descsz == sizeof (elfcore_psinfo_t))
   9271  1.3  christos     {
   9272  1.3  christos       elfcore_psinfo_t psinfo;
   9273  1.3  christos 
   9274  1.3  christos       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   9275  1.1  christos 
   9276  1.1  christos #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
   9277  1.1  christos       elf_tdata (abfd)->core->pid = psinfo.pr_pid;
   9278  1.3  christos #endif
   9279  1.1  christos       elf_tdata (abfd)->core->program
   9280  1.1  christos 	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
   9281  1.1  christos 				sizeof (psinfo.pr_fname));
   9282  1.1  christos 
   9283  1.1  christos       elf_tdata (abfd)->core->command
   9284  1.1  christos 	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
   9285  1.1  christos 				sizeof (psinfo.pr_psargs));
   9286  1.1  christos     }
   9287  1.1  christos #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   9288  1.1  christos   else if (note->descsz == sizeof (elfcore_psinfo32_t))
   9289  1.1  christos     {
   9290  1.3  christos       /* 64-bit host, 32-bit corefile */
   9291  1.3  christos       elfcore_psinfo32_t psinfo;
   9292  1.3  christos 
   9293  1.3  christos       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   9294  1.1  christos 
   9295  1.1  christos #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
   9296  1.1  christos       elf_tdata (abfd)->core->pid = psinfo.pr_pid;
   9297  1.3  christos #endif
   9298  1.1  christos       elf_tdata (abfd)->core->program
   9299  1.1  christos 	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
   9300  1.1  christos 				sizeof (psinfo.pr_fname));
   9301  1.1  christos 
   9302  1.1  christos       elf_tdata (abfd)->core->command
   9303  1.1  christos 	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
   9304  1.1  christos 				sizeof (psinfo.pr_psargs));
   9305  1.1  christos     }
   9306  1.1  christos #endif
   9307  1.1  christos 
   9308  1.1  christos   else
   9309  1.1  christos     {
   9310  1.1  christos       /* Fail - we don't know how to handle any other
   9311  1.1  christos 	 note size (ie. data object type).  */
   9312  1.1  christos       return TRUE;
   9313  1.1  christos     }
   9314  1.1  christos 
   9315  1.3  christos   /* Note that for some reason, a spurious space is tacked
   9316  1.1  christos      onto the end of the args in some (at least one anyway)
   9317  1.1  christos      implementations, so strip it off if it exists.  */
   9318  1.1  christos 
   9319  1.1  christos   {
   9320  1.1  christos     char *command = elf_tdata (abfd)->core->command;
   9321  1.1  christos     int n = strlen (command);
   9322  1.1  christos 
   9323  1.1  christos     if (0 < n && command[n - 1] == ' ')
   9324  1.1  christos       command[n - 1] = '\0';
   9325  1.1  christos   }
   9326  1.1  christos 
   9327  1.1  christos   return TRUE;
   9328  1.1  christos }
   9329  1.1  christos #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
   9330  1.1  christos 
   9331  1.1  christos #if defined (HAVE_PSTATUS_T)
   9332  1.1  christos static bfd_boolean
   9333  1.1  christos elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
   9334  1.1  christos {
   9335  1.1  christos   if (note->descsz == sizeof (pstatus_t)
   9336  1.1  christos #if defined (HAVE_PXSTATUS_T)
   9337  1.1  christos       || note->descsz == sizeof (pxstatus_t)
   9338  1.1  christos #endif
   9339  1.1  christos       )
   9340  1.3  christos     {
   9341  1.1  christos       pstatus_t pstat;
   9342  1.1  christos 
   9343  1.1  christos       memcpy (&pstat, note->descdata, sizeof (pstat));
   9344  1.1  christos 
   9345  1.1  christos       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   9346  1.1  christos     }
   9347  1.1  christos #if defined (HAVE_PSTATUS32_T)
   9348  1.1  christos   else if (note->descsz == sizeof (pstatus32_t))
   9349  1.1  christos     {
   9350  1.3  christos       /* 64-bit host, 32-bit corefile */
   9351  1.1  christos       pstatus32_t pstat;
   9352  1.1  christos 
   9353  1.1  christos       memcpy (&pstat, note->descdata, sizeof (pstat));
   9354  1.1  christos 
   9355  1.1  christos       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   9356  1.1  christos     }
   9357  1.1  christos #endif
   9358  1.1  christos   /* Could grab some more details from the "representative"
   9359  1.1  christos      lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
   9360  1.1  christos      NT_LWPSTATUS note, presumably.  */
   9361  1.1  christos 
   9362  1.1  christos   return TRUE;
   9363  1.1  christos }
   9364  1.1  christos #endif /* defined (HAVE_PSTATUS_T) */
   9365  1.1  christos 
   9366  1.1  christos #if defined (HAVE_LWPSTATUS_T)
   9367  1.1  christos static bfd_boolean
   9368  1.1  christos elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
   9369  1.1  christos {
   9370  1.1  christos   lwpstatus_t lwpstat;
   9371  1.1  christos   char buf[100];
   9372  1.1  christos   char *name;
   9373  1.1  christos   size_t len;
   9374  1.1  christos   asection *sect;
   9375  1.1  christos 
   9376  1.1  christos   if (note->descsz != sizeof (lwpstat)
   9377  1.1  christos #if defined (HAVE_LWPXSTATUS_T)
   9378  1.1  christos       && note->descsz != sizeof (lwpxstatus_t)
   9379  1.1  christos #endif
   9380  1.3  christos       )
   9381  1.1  christos     return TRUE;
   9382  1.1  christos 
   9383  1.3  christos   memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
   9384  1.3  christos 
   9385  1.1  christos   elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
   9386  1.1  christos   /* Do not overwrite the core signal if it has already been set by
   9387  1.1  christos      another thread.  */
   9388  1.1  christos   if (elf_tdata (abfd)->core->signal == 0)
   9389  1.1  christos     elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
   9390  1.1  christos 
   9391  1.1  christos   /* Make a ".reg/999" section.  */
   9392  1.1  christos 
   9393  1.1  christos   sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
   9394  1.1  christos   len = strlen (buf) + 1;
   9395  1.1  christos   name = bfd_alloc (abfd, len);
   9396  1.1  christos   if (name == NULL)
   9397  1.1  christos     return FALSE;
   9398  1.1  christos   memcpy (name, buf, len);
   9399  1.1  christos 
   9400  1.1  christos   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   9401  1.1  christos   if (sect == NULL)
   9402  1.1  christos     return FALSE;
   9403  1.1  christos 
   9404  1.1  christos #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   9405  1.1  christos   sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
   9406  1.1  christos   sect->filepos = note->descpos
   9407  1.1  christos     + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
   9408  1.1  christos #endif
   9409  1.1  christos 
   9410  1.1  christos #if defined (HAVE_LWPSTATUS_T_PR_REG)
   9411  1.1  christos   sect->size = sizeof (lwpstat.pr_reg);
   9412  1.1  christos   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
   9413  1.1  christos #endif
   9414  1.1  christos 
   9415  1.1  christos   sect->alignment_power = 2;
   9416  1.1  christos 
   9417  1.1  christos   if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
   9418  1.1  christos     return FALSE;
   9419  1.1  christos 
   9420  1.1  christos   /* Make a ".reg2/999" section */
   9421  1.1  christos 
   9422  1.1  christos   sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
   9423  1.1  christos   len = strlen (buf) + 1;
   9424  1.1  christos   name = bfd_alloc (abfd, len);
   9425  1.1  christos   if (name == NULL)
   9426  1.1  christos     return FALSE;
   9427  1.1  christos   memcpy (name, buf, len);
   9428  1.1  christos 
   9429  1.1  christos   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   9430  1.1  christos   if (sect == NULL)
   9431  1.1  christos     return FALSE;
   9432  1.1  christos 
   9433  1.1  christos #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   9434  1.1  christos   sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
   9435  1.1  christos   sect->filepos = note->descpos
   9436  1.1  christos     + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
   9437  1.1  christos #endif
   9438  1.1  christos 
   9439  1.1  christos #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
   9440  1.1  christos   sect->size = sizeof (lwpstat.pr_fpreg);
   9441  1.1  christos   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
   9442  1.1  christos #endif
   9443  1.1  christos 
   9444  1.1  christos   sect->alignment_power = 2;
   9445  1.1  christos 
   9446  1.1  christos   return elfcore_maybe_make_sect (abfd, ".reg2", sect);
   9447  1.1  christos }
   9448  1.1  christos #endif /* defined (HAVE_LWPSTATUS_T) */
   9449  1.1  christos 
   9450  1.1  christos static bfd_boolean
   9451  1.1  christos elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
   9452  1.1  christos {
   9453  1.1  christos   char buf[30];
   9454  1.1  christos   char *name;
   9455  1.1  christos   size_t len;
   9456  1.1  christos   asection *sect;
   9457  1.1  christos   int type;
   9458  1.1  christos   int is_active_thread;
   9459  1.1  christos   bfd_vma base_addr;
   9460  1.1  christos 
   9461  1.1  christos   if (note->descsz < 728)
   9462  1.1  christos     return TRUE;
   9463  1.1  christos 
   9464  1.1  christos   if (! CONST_STRNEQ (note->namedata, "win32"))
   9465  1.1  christos     return TRUE;
   9466  1.1  christos 
   9467  1.3  christos   type = bfd_get_32 (abfd, note->descdata);
   9468  1.1  christos 
   9469  1.3  christos   switch (type)
   9470  1.1  christos     {
   9471  1.3  christos     case 1 /* NOTE_INFO_PROCESS */:
   9472  1.1  christos       /* FIXME: need to add ->core->command.  */
   9473  1.1  christos       /* process_info.pid */
   9474  1.1  christos       elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
   9475  1.1  christos       /* process_info.signal */
   9476  1.1  christos       elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
   9477  1.1  christos       break;
   9478  1.1  christos 
   9479  1.1  christos     case 2 /* NOTE_INFO_THREAD */:
   9480  1.1  christos       /* Make a ".reg/999" section.  */
   9481  1.1  christos       /* thread_info.tid */
   9482  1.1  christos       sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
   9483  1.1  christos 
   9484  1.1  christos       len = strlen (buf) + 1;
   9485  1.1  christos       name = (char *) bfd_alloc (abfd, len);
   9486  1.1  christos       if (name == NULL)
   9487  1.1  christos 	return FALSE;
   9488  1.1  christos 
   9489  1.1  christos       memcpy (name, buf, len);
   9490  1.1  christos 
   9491  1.1  christos       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   9492  1.1  christos       if (sect == NULL)
   9493  1.1  christos 	return FALSE;
   9494  1.1  christos 
   9495  1.1  christos       /* sizeof (thread_info.thread_context) */
   9496  1.1  christos       sect->size = 716;
   9497  1.1  christos       /* offsetof (thread_info.thread_context) */
   9498  1.1  christos       sect->filepos = note->descpos + 12;
   9499  1.1  christos       sect->alignment_power = 2;
   9500  1.1  christos 
   9501  1.1  christos       /* thread_info.is_active_thread */
   9502  1.1  christos       is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
   9503  1.1  christos 
   9504  1.1  christos       if (is_active_thread)
   9505  1.1  christos 	if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
   9506  1.1  christos 	  return FALSE;
   9507  1.1  christos       break;
   9508  1.1  christos 
   9509  1.1  christos     case 3 /* NOTE_INFO_MODULE */:
   9510  1.1  christos       /* Make a ".module/xxxxxxxx" section.  */
   9511  1.1  christos       /* module_info.base_address */
   9512  1.1  christos       base_addr = bfd_get_32 (abfd, note->descdata + 4);
   9513  1.1  christos       sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
   9514  1.1  christos 
   9515  1.1  christos       len = strlen (buf) + 1;
   9516  1.1  christos       name = (char *) bfd_alloc (abfd, len);
   9517  1.1  christos       if (name == NULL)
   9518  1.1  christos 	return FALSE;
   9519  1.1  christos 
   9520  1.1  christos       memcpy (name, buf, len);
   9521  1.1  christos 
   9522  1.1  christos       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   9523  1.1  christos 
   9524  1.1  christos       if (sect == NULL)
   9525  1.1  christos 	return FALSE;
   9526  1.1  christos 
   9527  1.1  christos       sect->size = note->descsz;
   9528  1.1  christos       sect->filepos = note->descpos;
   9529  1.1  christos       sect->alignment_power = 2;
   9530  1.1  christos       break;
   9531  1.1  christos 
   9532  1.1  christos     default:
   9533  1.1  christos       return TRUE;
   9534  1.1  christos     }
   9535  1.1  christos 
   9536  1.1  christos   return TRUE;
   9537  1.1  christos }
   9538  1.1  christos 
   9539  1.1  christos static bfd_boolean
   9540  1.1  christos elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
   9541  1.1  christos {
   9542  1.1  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   9543  1.1  christos 
   9544  1.1  christos   switch (note->type)
   9545  1.1  christos     {
   9546  1.1  christos     default:
   9547  1.1  christos       return TRUE;
   9548  1.1  christos 
   9549  1.1  christos     case NT_PRSTATUS:
   9550  1.1  christos       if (bed->elf_backend_grok_prstatus)
   9551  1.1  christos 	if ((*bed->elf_backend_grok_prstatus) (abfd, note))
   9552  1.1  christos 	  return TRUE;
   9553  1.1  christos #if defined (HAVE_PRSTATUS_T)
   9554  1.1  christos       return elfcore_grok_prstatus (abfd, note);
   9555  1.1  christos #else
   9556  1.1  christos       return TRUE;
   9557  1.1  christos #endif
   9558  1.1  christos 
   9559  1.1  christos #if defined (HAVE_PSTATUS_T)
   9560  1.1  christos     case NT_PSTATUS:
   9561  1.1  christos       return elfcore_grok_pstatus (abfd, note);
   9562  1.1  christos #endif
   9563  1.1  christos 
   9564  1.1  christos #if defined (HAVE_LWPSTATUS_T)
   9565  1.1  christos     case NT_LWPSTATUS:
   9566  1.1  christos       return elfcore_grok_lwpstatus (abfd, note);
   9567  1.1  christos #endif
   9568  1.1  christos 
   9569  1.1  christos     case NT_FPREGSET:		/* FIXME: rename to NT_PRFPREG */
   9570  1.1  christos       return elfcore_grok_prfpreg (abfd, note);
   9571  1.1  christos 
   9572  1.1  christos     case NT_WIN32PSTATUS:
   9573  1.1  christos       return elfcore_grok_win32pstatus (abfd, note);
   9574  1.1  christos 
   9575  1.1  christos     case NT_PRXFPREG:		/* Linux SSE extension */
   9576  1.1  christos       if (note->namesz == 6
   9577  1.1  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   9578  1.1  christos 	return elfcore_grok_prxfpreg (abfd, note);
   9579  1.1  christos       else
   9580  1.1  christos 	return TRUE;
   9581  1.1  christos 
   9582  1.1  christos     case NT_X86_XSTATE:		/* Linux XSAVE extension */
   9583  1.1  christos       if (note->namesz == 6
   9584  1.1  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   9585  1.1  christos 	return elfcore_grok_xstatereg (abfd, note);
   9586  1.1  christos       else
   9587  1.1  christos 	return TRUE;
   9588  1.1  christos 
   9589  1.1  christos     case NT_PPC_VMX:
   9590  1.1  christos       if (note->namesz == 6
   9591  1.1  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   9592  1.1  christos 	return elfcore_grok_ppc_vmx (abfd, note);
   9593  1.1  christos       else
   9594  1.1  christos 	return TRUE;
   9595  1.1  christos 
   9596  1.1  christos     case NT_PPC_VSX:
   9597  1.1  christos       if (note->namesz == 6
   9598  1.1  christos           && strcmp (note->namedata, "LINUX") == 0)
   9599  1.1  christos         return elfcore_grok_ppc_vsx (abfd, note);
   9600  1.1  christos       else
   9601  1.1  christos         return TRUE;
   9602  1.1  christos 
   9603  1.1  christos     case NT_S390_HIGH_GPRS:
   9604  1.1  christos       if (note->namesz == 6
   9605  1.1  christos           && strcmp (note->namedata, "LINUX") == 0)
   9606  1.1  christos         return elfcore_grok_s390_high_gprs (abfd, note);
   9607  1.1  christos       else
   9608  1.1  christos         return TRUE;
   9609  1.1  christos 
   9610  1.1  christos     case NT_S390_TIMER:
   9611  1.1  christos       if (note->namesz == 6
   9612  1.1  christos           && strcmp (note->namedata, "LINUX") == 0)
   9613  1.1  christos         return elfcore_grok_s390_timer (abfd, note);
   9614  1.1  christos       else
   9615  1.1  christos         return TRUE;
   9616  1.1  christos 
   9617  1.1  christos     case NT_S390_TODCMP:
   9618  1.1  christos       if (note->namesz == 6
   9619  1.1  christos           && strcmp (note->namedata, "LINUX") == 0)
   9620  1.1  christos         return elfcore_grok_s390_todcmp (abfd, note);
   9621  1.1  christos       else
   9622  1.1  christos         return TRUE;
   9623  1.1  christos 
   9624  1.1  christos     case NT_S390_TODPREG:
   9625  1.1  christos       if (note->namesz == 6
   9626  1.1  christos           && strcmp (note->namedata, "LINUX") == 0)
   9627  1.1  christos         return elfcore_grok_s390_todpreg (abfd, note);
   9628  1.1  christos       else
   9629  1.1  christos         return TRUE;
   9630  1.1  christos 
   9631  1.1  christos     case NT_S390_CTRS:
   9632  1.1  christos       if (note->namesz == 6
   9633  1.1  christos           && strcmp (note->namedata, "LINUX") == 0)
   9634  1.1  christos         return elfcore_grok_s390_ctrs (abfd, note);
   9635  1.1  christos       else
   9636  1.1  christos         return TRUE;
   9637  1.1  christos 
   9638  1.1  christos     case NT_S390_PREFIX:
   9639  1.1  christos       if (note->namesz == 6
   9640  1.3  christos           && strcmp (note->namedata, "LINUX") == 0)
   9641  1.3  christos         return elfcore_grok_s390_prefix (abfd, note);
   9642  1.3  christos       else
   9643  1.3  christos         return TRUE;
   9644  1.3  christos 
   9645  1.3  christos     case NT_S390_LAST_BREAK:
   9646  1.3  christos       if (note->namesz == 6
   9647  1.3  christos           && strcmp (note->namedata, "LINUX") == 0)
   9648  1.3  christos         return elfcore_grok_s390_last_break (abfd, note);
   9649  1.3  christos       else
   9650  1.3  christos         return TRUE;
   9651  1.3  christos 
   9652  1.3  christos     case NT_S390_SYSTEM_CALL:
   9653  1.3  christos       if (note->namesz == 6
   9654  1.3  christos           && strcmp (note->namedata, "LINUX") == 0)
   9655  1.3  christos         return elfcore_grok_s390_system_call (abfd, note);
   9656  1.3  christos       else
   9657  1.3  christos         return TRUE;
   9658  1.3  christos 
   9659  1.3  christos     case NT_S390_TDB:
   9660  1.3  christos       if (note->namesz == 6
   9661  1.6  christos           && strcmp (note->namedata, "LINUX") == 0)
   9662  1.6  christos         return elfcore_grok_s390_tdb (abfd, note);
   9663  1.6  christos       else
   9664  1.6  christos         return TRUE;
   9665  1.6  christos 
   9666  1.6  christos     case NT_S390_VXRS_LOW:
   9667  1.6  christos       if (note->namesz == 6
   9668  1.6  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   9669  1.6  christos 	return elfcore_grok_s390_vxrs_low (abfd, note);
   9670  1.6  christos       else
   9671  1.6  christos 	return TRUE;
   9672  1.6  christos 
   9673  1.6  christos     case NT_S390_VXRS_HIGH:
   9674  1.6  christos       if (note->namesz == 6
   9675  1.3  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   9676  1.3  christos 	return elfcore_grok_s390_vxrs_high (abfd, note);
   9677  1.3  christos       else
   9678  1.3  christos 	return TRUE;
   9679  1.3  christos 
   9680  1.3  christos     case NT_ARM_VFP:
   9681  1.3  christos       if (note->namesz == 6
   9682  1.3  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   9683  1.3  christos 	return elfcore_grok_arm_vfp (abfd, note);
   9684  1.3  christos       else
   9685  1.3  christos 	return TRUE;
   9686  1.3  christos 
   9687  1.3  christos     case NT_ARM_TLS:
   9688  1.3  christos       if (note->namesz == 6
   9689  1.3  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   9690  1.3  christos 	return elfcore_grok_aarch_tls (abfd, note);
   9691  1.3  christos       else
   9692  1.3  christos 	return TRUE;
   9693  1.3  christos 
   9694  1.3  christos     case NT_ARM_HW_BREAK:
   9695  1.3  christos       if (note->namesz == 6
   9696  1.3  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   9697  1.3  christos 	return elfcore_grok_aarch_hw_break (abfd, note);
   9698  1.3  christos       else
   9699  1.3  christos 	return TRUE;
   9700  1.3  christos 
   9701  1.3  christos     case NT_ARM_HW_WATCH:
   9702  1.3  christos       if (note->namesz == 6
   9703  1.1  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   9704  1.1  christos 	return elfcore_grok_aarch_hw_watch (abfd, note);
   9705  1.1  christos       else
   9706  1.1  christos 	return TRUE;
   9707  1.1  christos 
   9708  1.1  christos     case NT_PRPSINFO:
   9709  1.1  christos     case NT_PSINFO:
   9710  1.1  christos       if (bed->elf_backend_grok_psinfo)
   9711  1.1  christos 	if ((*bed->elf_backend_grok_psinfo) (abfd, note))
   9712  1.1  christos 	  return TRUE;
   9713  1.1  christos #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   9714  1.1  christos       return elfcore_grok_psinfo (abfd, note);
   9715  1.1  christos #else
   9716  1.1  christos       return TRUE;
   9717  1.1  christos #endif
   9718  1.1  christos 
   9719  1.1  christos     case NT_AUXV:
   9720  1.1  christos       {
   9721  1.1  christos 	asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
   9722  1.1  christos 							     SEC_HAS_CONTENTS);
   9723  1.1  christos 
   9724  1.1  christos 	if (sect == NULL)
   9725  1.1  christos 	  return FALSE;
   9726  1.1  christos 	sect->size = note->descsz;
   9727  1.3  christos 	sect->filepos = note->descpos;
   9728  1.3  christos 	sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   9729  1.3  christos 
   9730  1.3  christos 	return TRUE;
   9731  1.3  christos       }
   9732  1.3  christos 
   9733  1.3  christos     case NT_FILE:
   9734  1.3  christos       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
   9735  1.8  christos 					      note);
   9736  1.1  christos 
   9737  1.1  christos     case NT_SIGINFO:
   9738  1.1  christos       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
   9739  1.1  christos 					      note);
   9740  1.1  christos 
   9741  1.1  christos     }
   9742  1.6  christos }
   9743  1.3  christos 
   9744  1.3  christos static bfd_boolean
   9745  1.1  christos elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
   9746  1.1  christos {
   9747  1.6  christos   struct bfd_build_id* build_id;
   9748  1.6  christos 
   9749  1.3  christos   if (note->descsz == 0)
   9750  1.3  christos     return FALSE;
   9751  1.6  christos 
   9752  1.6  christos   build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
   9753  1.6  christos   if (build_id == NULL)
   9754  1.1  christos     return FALSE;
   9755  1.1  christos 
   9756  1.1  christos   build_id->size = note->descsz;
   9757  1.1  christos   memcpy (build_id->data, note->descdata, note->descsz);
   9758  1.1  christos   abfd->build_id = build_id;
   9759  1.1  christos 
   9760  1.1  christos   return TRUE;
   9761  1.1  christos }
   9762  1.1  christos 
   9763  1.1  christos static bfd_boolean
   9764  1.1  christos elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
   9765  1.1  christos {
   9766  1.9  christos   switch (note->type)
   9767  1.9  christos     {
   9768  1.9  christos     default:
   9769  1.1  christos       return TRUE;
   9770  1.1  christos 
   9771  1.1  christos     case NT_GNU_PROPERTY_TYPE_0:
   9772  1.1  christos       return _bfd_elf_parse_gnu_properties (abfd, note);
   9773  1.1  christos 
   9774  1.1  christos     case NT_GNU_BUILD_ID:
   9775  1.3  christos       return elfobj_grok_gnu_build_id (abfd, note);
   9776  1.3  christos     }
   9777  1.3  christos }
   9778  1.3  christos 
   9779  1.3  christos static bfd_boolean
   9780  1.3  christos elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
   9781  1.3  christos {
   9782  1.3  christos   struct sdt_note *cur =
   9783  1.3  christos     (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note)
   9784  1.3  christos 				   + note->descsz);
   9785  1.3  christos 
   9786  1.3  christos   cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
   9787  1.3  christos   cur->size = (bfd_size_type) note->descsz;
   9788  1.3  christos   memcpy (cur->data, note->descdata, note->descsz);
   9789  1.3  christos 
   9790  1.3  christos   elf_tdata (abfd)->sdt_note_head = cur;
   9791  1.3  christos 
   9792  1.3  christos   return TRUE;
   9793  1.3  christos }
   9794  1.3  christos 
   9795  1.3  christos static bfd_boolean
   9796  1.3  christos elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
   9797  1.3  christos {
   9798  1.3  christos   switch (note->type)
   9799  1.3  christos     {
   9800  1.3  christos     case NT_STAPSDT:
   9801  1.3  christos       return elfobj_grok_stapsdt_note_1 (abfd, note);
   9802  1.3  christos 
   9803  1.3  christos     default:
   9804  1.8  christos       return TRUE;
   9805  1.8  christos     }
   9806  1.8  christos }
   9807  1.8  christos 
   9808  1.8  christos static bfd_boolean
   9809  1.8  christos elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
   9810  1.8  christos {
   9811  1.8  christos   size_t offset;
   9812  1.8  christos 
   9813  1.8  christos   switch (abfd->arch_info->bits_per_word)
   9814  1.8  christos     {
   9815  1.8  christos     case 32:
   9816  1.8  christos       if (note->descsz < 108)
   9817  1.8  christos 	return FALSE;
   9818  1.8  christos       break;
   9819  1.8  christos 
   9820  1.8  christos     case 64:
   9821  1.8  christos       if (note->descsz < 120)
   9822  1.8  christos 	return FALSE;
   9823  1.8  christos       break;
   9824  1.8  christos 
   9825  1.8  christos     default:
   9826  1.8  christos       return FALSE;
   9827  1.8  christos     }
   9828  1.8  christos 
   9829  1.8  christos   /* Check for version 1 in pr_version.  */
   9830  1.8  christos   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   9831  1.8  christos     return FALSE;
   9832  1.8  christos   offset = 4;
   9833  1.8  christos 
   9834  1.8  christos   /* Skip over pr_psinfosz. */
   9835  1.8  christos   if (abfd->arch_info->bits_per_word == 32)
   9836  1.8  christos     offset += 4;
   9837  1.8  christos   else
   9838  1.8  christos     {
   9839  1.8  christos       offset += 4;	/* Padding before pr_psinfosz. */
   9840  1.8  christos       offset += 8;
   9841  1.8  christos     }
   9842  1.8  christos 
   9843  1.8  christos   /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
   9844  1.8  christos   elf_tdata (abfd)->core->program
   9845  1.8  christos     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
   9846  1.8  christos   offset += 17;
   9847  1.8  christos 
   9848  1.8  christos   /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
   9849  1.8  christos   elf_tdata (abfd)->core->command
   9850  1.8  christos     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
   9851  1.8  christos   offset += 81;
   9852  1.8  christos 
   9853  1.8  christos   /* Padding before pr_pid.  */
   9854  1.8  christos   offset += 2;
   9855  1.8  christos 
   9856  1.8  christos   /* The pr_pid field was added in version "1a".  */
   9857  1.8  christos   if (note->descsz < offset + 4)
   9858  1.8  christos     return TRUE;
   9859  1.8  christos 
   9860  1.8  christos   elf_tdata (abfd)->core->pid
   9861  1.8  christos     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   9862  1.8  christos 
   9863  1.8  christos   return TRUE;
   9864  1.8  christos }
   9865  1.8  christos 
   9866  1.8  christos static bfd_boolean
   9867  1.8  christos elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
   9868  1.8  christos {
   9869  1.8  christos   size_t offset;
   9870  1.8  christos   size_t size;
   9871  1.8  christos 
   9872  1.8  christos   /* Check for version 1 in pr_version. */
   9873  1.8  christos   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   9874  1.8  christos     return FALSE;
   9875  1.8  christos   offset = 4;
   9876  1.8  christos 
   9877  1.8  christos   /* Skip over pr_statussz.  */
   9878  1.8  christos   switch (abfd->arch_info->bits_per_word)
   9879  1.8  christos     {
   9880  1.8  christos     case 32:
   9881  1.8  christos       offset += 4;
   9882  1.8  christos       break;
   9883  1.8  christos 
   9884  1.8  christos     case 64:
   9885  1.8  christos       offset += 4;	/* Padding before pr_statussz. */
   9886  1.8  christos       offset += 8;
   9887  1.8  christos       break;
   9888  1.8  christos 
   9889  1.8  christos     default:
   9890  1.8  christos       return FALSE;
   9891  1.8  christos     }
   9892  1.8  christos 
   9893  1.8  christos   /* Extract size of pr_reg from pr_gregsetsz.  */
   9894  1.8  christos   if (abfd->arch_info->bits_per_word == 32)
   9895  1.8  christos     size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   9896  1.8  christos   else
   9897  1.8  christos     size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
   9898  1.8  christos 
   9899  1.8  christos   /* Skip over pr_gregsetsz and pr_fpregsetsz. */
   9900  1.8  christos   offset += (abfd->arch_info->bits_per_word / 8) * 2;
   9901  1.8  christos 
   9902  1.8  christos   /* Skip over pr_osreldate. */
   9903  1.8  christos   offset += 4;
   9904  1.8  christos 
   9905  1.8  christos   /* Read signal from pr_cursig. */
   9906  1.8  christos   if (elf_tdata (abfd)->core->signal == 0)
   9907  1.8  christos     elf_tdata (abfd)->core->signal
   9908  1.8  christos       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   9909  1.8  christos   offset += 4;
   9910  1.8  christos 
   9911  1.8  christos   /* Read TID from pr_pid. */
   9912  1.8  christos   elf_tdata (abfd)->core->lwpid
   9913  1.8  christos       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   9914  1.8  christos   offset += 4;
   9915  1.8  christos 
   9916  1.8  christos   /* Padding before pr_reg. */
   9917  1.8  christos   if (abfd->arch_info->bits_per_word == 64)
   9918  1.8  christos     offset += 4;
   9919  1.8  christos 
   9920  1.8  christos   /* Make a ".reg/999" section and a ".reg" section.  */
   9921  1.8  christos   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   9922  1.8  christos 					  size, note->descpos + offset);
   9923  1.8  christos }
   9924  1.8  christos 
   9925  1.8  christos static bfd_boolean
   9926  1.8  christos elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
   9927  1.8  christos {
   9928  1.8  christos   switch (note->type)
   9929  1.8  christos     {
   9930  1.8  christos     case NT_PRSTATUS:
   9931  1.8  christos       return elfcore_grok_freebsd_prstatus (abfd, note);
   9932  1.8  christos 
   9933  1.8  christos     case NT_FPREGSET:
   9934  1.8  christos       return elfcore_grok_prfpreg (abfd, note);
   9935  1.8  christos 
   9936  1.8  christos     case NT_PRPSINFO:
   9937  1.8  christos       return elfcore_grok_freebsd_psinfo (abfd, note);
   9938  1.8  christos 
   9939  1.8  christos     case NT_FREEBSD_THRMISC:
   9940  1.8  christos       if (note->namesz == 8)
   9941  1.8  christos 	return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
   9942  1.8  christos       else
   9943  1.8  christos 	return TRUE;
   9944  1.8  christos 
   9945  1.8  christos     case NT_FREEBSD_PROCSTAT_AUXV:
   9946  1.8  christos       {
   9947  1.8  christos 	asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
   9948  1.8  christos 							     SEC_HAS_CONTENTS);
   9949  1.8  christos 
   9950  1.8  christos 	if (sect == NULL)
   9951  1.8  christos 	  return FALSE;
   9952  1.8  christos 	sect->size = note->descsz - 4;
   9953  1.8  christos 	sect->filepos = note->descpos + 4;
   9954  1.8  christos 	sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   9955  1.8  christos 
   9956  1.8  christos 	return TRUE;
   9957  1.8  christos       }
   9958  1.8  christos 
   9959  1.8  christos     case NT_X86_XSTATE:
   9960  1.8  christos       if (note->namesz == 8)
   9961  1.8  christos 	return elfcore_grok_xstatereg (abfd, note);
   9962  1.8  christos       else
   9963  1.8  christos 	return TRUE;
   9964  1.8  christos 
   9965  1.8  christos     default:
   9966  1.1  christos       return TRUE;
   9967  1.1  christos     }
   9968  1.1  christos }
   9969  1.1  christos 
   9970  1.1  christos static bfd_boolean
   9971  1.1  christos elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
   9972  1.1  christos {
   9973  1.1  christos   char *cp;
   9974  1.1  christos 
   9975  1.1  christos   cp = strchr (note->namedata, '@');
   9976  1.1  christos   if (cp != NULL)
   9977  1.1  christos     {
   9978  1.1  christos       *lwpidp = atoi(cp + 1);
   9979  1.1  christos       return TRUE;
   9980  1.1  christos     }
   9981  1.1  christos   return FALSE;
   9982  1.1  christos }
   9983  1.3  christos 
   9984  1.1  christos static bfd_boolean
   9985  1.1  christos elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   9986  1.1  christos {
   9987  1.3  christos   /* Signal number at offset 0x08. */
   9988  1.1  christos   elf_tdata (abfd)->core->signal
   9989  1.1  christos     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   9990  1.1  christos 
   9991  1.3  christos   /* Process ID at offset 0x50. */
   9992  1.1  christos   elf_tdata (abfd)->core->pid
   9993  1.1  christos     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
   9994  1.1  christos 
   9995  1.1  christos   /* Command name at 0x7c (max 32 bytes, including nul). */
   9996  1.1  christos   elf_tdata (abfd)->core->command
   9997  1.1  christos     = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
   9998  1.1  christos 
   9999  1.1  christos   return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
   10000  1.1  christos 					  note);
   10001  1.1  christos }
   10002  1.1  christos 
   10003  1.1  christos static bfd_boolean
   10004  1.3  christos elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
   10005  1.1  christos {
   10006  1.1  christos   int lwp;
   10007  1.1  christos 
   10008  1.1  christos   if (elfcore_netbsd_get_lwpid (note, &lwp))
   10009  1.1  christos     elf_tdata (abfd)->core->lwpid = lwp;
   10010  1.1  christos 
   10011  1.1  christos   if (note->type == NT_NETBSDCORE_PROCINFO)
   10012  1.1  christos     {
   10013  1.1  christos       /* NetBSD-specific core "procinfo".  Note that we expect to
   10014  1.1  christos 	 find this note before any of the others, which is fine,
   10015  1.1  christos 	 since the kernel writes this note out first when it
   10016  1.7  christos 	 creates a core file.  */
   10017  1.7  christos 
   10018  1.7  christos       return elfcore_grok_netbsd_procinfo (abfd, note);
   10019  1.7  christos     }
   10020  1.7  christos 
   10021  1.7  christos   if (note->type == NT_NETBSDCORE_AUXV)
   10022  1.7  christos     {
   10023  1.7  christos       asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
   10024  1.7  christos 							   SEC_HAS_CONTENTS);
   10025  1.7  christos 
   10026  1.7  christos       if (sect == NULL)
   10027  1.7  christos 	return FALSE;
   10028  1.7  christos       sect->size = note->descsz;
   10029  1.7  christos       sect->filepos = note->descpos;
   10030  1.1  christos       sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   10031  1.1  christos 
   10032  1.1  christos       return TRUE;
   10033  1.1  christos     }
   10034  1.1  christos 
   10035  1.1  christos   /* As of Jan 2002 there are no other machine-independent notes
   10036  1.1  christos      defined for NetBSD core files.  If the note type is less
   10037  1.1  christos      than the start of the machine-dependent note types, we don't
   10038  1.1  christos      understand it.  */
   10039  1.1  christos 
   10040  1.1  christos   if (note->type < NT_NETBSDCORE_FIRSTMACH)
   10041  1.1  christos     return TRUE;
   10042  1.1  christos 
   10043  1.1  christos 
   10044  1.1  christos   switch (bfd_get_arch (abfd))
   10045  1.1  christos     {
   10046  1.1  christos       /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
   10047  1.1  christos 	 PT_GETFPREGS == mach+2.  */
   10048  1.1  christos 
   10049  1.1  christos     case bfd_arch_alpha:
   10050  1.1  christos     case bfd_arch_sparc:
   10051  1.1  christos       switch (note->type)
   10052  1.1  christos 	{
   10053  1.1  christos 	case NT_NETBSDCORE_FIRSTMACH+0:
   10054  1.1  christos 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   10055  1.1  christos 
   10056  1.1  christos 	case NT_NETBSDCORE_FIRSTMACH+2:
   10057  1.1  christos 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   10058  1.2       uwe 
   10059  1.2       uwe 	default:
   10060  1.2       uwe 	  return TRUE;
   10061  1.2       uwe 	}
   10062  1.2       uwe 
   10063  1.2       uwe       /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
   10064  1.2       uwe 	 There's also old PT___GETREGS40 == mach + 1 for old reg
   10065  1.2       uwe 	 structure which lacks GBR.  */
   10066  1.2       uwe 
   10067  1.2       uwe     case bfd_arch_sh:
   10068  1.2       uwe       switch (note->type)
   10069  1.2       uwe 	{
   10070  1.2       uwe 	case NT_NETBSDCORE_FIRSTMACH+3:
   10071  1.2       uwe 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   10072  1.2       uwe 
   10073  1.2       uwe 	case NT_NETBSDCORE_FIRSTMACH+5:
   10074  1.2       uwe 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   10075  1.1  christos 
   10076  1.1  christos 	default:
   10077  1.1  christos 	  return TRUE;
   10078  1.1  christos 	}
   10079  1.1  christos 
   10080  1.1  christos       /* On all other arch's, PT_GETREGS == mach+1 and
   10081  1.1  christos 	 PT_GETFPREGS == mach+3.  */
   10082  1.1  christos 
   10083  1.1  christos     default:
   10084  1.1  christos       switch (note->type)
   10085  1.1  christos 	{
   10086  1.1  christos 	case NT_NETBSDCORE_FIRSTMACH+1:
   10087  1.1  christos 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   10088  1.1  christos 
   10089  1.1  christos 	case NT_NETBSDCORE_FIRSTMACH+3:
   10090  1.1  christos 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   10091  1.1  christos 
   10092  1.1  christos 	default:
   10093  1.1  christos 	  return TRUE;
   10094  1.1  christos 	}
   10095  1.1  christos     }
   10096  1.1  christos     /* NOTREACHED */
   10097  1.1  christos }
   10098  1.3  christos 
   10099  1.1  christos static bfd_boolean
   10100  1.1  christos elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   10101  1.1  christos {
   10102  1.3  christos   /* Signal number at offset 0x08. */
   10103  1.1  christos   elf_tdata (abfd)->core->signal
   10104  1.1  christos     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   10105  1.1  christos 
   10106  1.3  christos   /* Process ID at offset 0x20. */
   10107  1.1  christos   elf_tdata (abfd)->core->pid
   10108  1.1  christos     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
   10109  1.1  christos 
   10110  1.1  christos   /* Command name at 0x48 (max 32 bytes, including nul). */
   10111  1.1  christos   elf_tdata (abfd)->core->command
   10112  1.1  christos     = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
   10113  1.1  christos 
   10114  1.1  christos   return TRUE;
   10115  1.1  christos }
   10116  1.1  christos 
   10117  1.1  christos static bfd_boolean
   10118  1.1  christos elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
   10119  1.1  christos {
   10120  1.1  christos   if (note->type == NT_OPENBSD_PROCINFO)
   10121  1.1  christos     return elfcore_grok_openbsd_procinfo (abfd, note);
   10122  1.1  christos 
   10123  1.1  christos   if (note->type == NT_OPENBSD_REGS)
   10124  1.1  christos     return elfcore_make_note_pseudosection (abfd, ".reg", note);
   10125  1.1  christos 
   10126  1.1  christos   if (note->type == NT_OPENBSD_FPREGS)
   10127  1.1  christos     return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   10128  1.1  christos 
   10129  1.1  christos   if (note->type == NT_OPENBSD_XFPREGS)
   10130  1.1  christos     return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
   10131  1.1  christos 
   10132  1.1  christos   if (note->type == NT_OPENBSD_AUXV)
   10133  1.1  christos     {
   10134  1.1  christos       asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
   10135  1.1  christos 							   SEC_HAS_CONTENTS);
   10136  1.1  christos 
   10137  1.1  christos       if (sect == NULL)
   10138  1.1  christos 	return FALSE;
   10139  1.1  christos       sect->size = note->descsz;
   10140  1.1  christos       sect->filepos = note->descpos;
   10141  1.1  christos       sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   10142  1.1  christos 
   10143  1.1  christos       return TRUE;
   10144  1.1  christos     }
   10145  1.1  christos 
   10146  1.1  christos   if (note->type == NT_OPENBSD_WCOOKIE)
   10147  1.1  christos     {
   10148  1.1  christos       asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
   10149  1.1  christos 							   SEC_HAS_CONTENTS);
   10150  1.1  christos 
   10151  1.1  christos       if (sect == NULL)
   10152  1.1  christos 	return FALSE;
   10153  1.1  christos       sect->size = note->descsz;
   10154  1.1  christos       sect->filepos = note->descpos;
   10155  1.1  christos       sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   10156  1.1  christos 
   10157  1.1  christos       return TRUE;
   10158  1.1  christos     }
   10159  1.1  christos 
   10160  1.1  christos   return TRUE;
   10161  1.1  christos }
   10162  1.1  christos 
   10163  1.1  christos static bfd_boolean
   10164  1.1  christos elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
   10165  1.1  christos {
   10166  1.1  christos   void *ddata = note->descdata;
   10167  1.1  christos   char buf[100];
   10168  1.1  christos   char *name;
   10169  1.3  christos   asection *sect;
   10170  1.1  christos   short sig;
   10171  1.1  christos   unsigned flags;
   10172  1.1  christos 
   10173  1.1  christos   /* nto_procfs_status 'pid' field is at offset 0.  */
   10174  1.1  christos   elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
   10175  1.1  christos 
   10176  1.1  christos   /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
   10177  1.1  christos   *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
   10178  1.1  christos 
   10179  1.1  christos   /* nto_procfs_status 'flags' field is at offset 8.  */
   10180  1.3  christos   flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
   10181  1.3  christos 
   10182  1.1  christos   /* nto_procfs_status 'what' field is at offset 14.  */
   10183  1.1  christos   if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
   10184  1.1  christos     {
   10185  1.1  christos       elf_tdata (abfd)->core->signal = sig;
   10186  1.1  christos       elf_tdata (abfd)->core->lwpid = *tid;
   10187  1.1  christos     }
   10188  1.3  christos 
   10189  1.1  christos   /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
   10190  1.1  christos      do not come from signals so we make sure we set the current
   10191  1.1  christos      thread just in case.  */
   10192  1.1  christos   if (flags & 0x00000080)
   10193  1.1  christos     elf_tdata (abfd)->core->lwpid = *tid;
   10194  1.1  christos 
   10195  1.1  christos   /* Make a ".qnx_core_status/%d" section.  */
   10196  1.1  christos   sprintf (buf, ".qnx_core_status/%ld", *tid);
   10197  1.1  christos 
   10198  1.1  christos   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   10199  1.1  christos   if (name == NULL)
   10200  1.1  christos     return FALSE;
   10201  1.1  christos   strcpy (name, buf);
   10202  1.1  christos 
   10203  1.1  christos   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10204  1.1  christos   if (sect == NULL)
   10205  1.1  christos     return FALSE;
   10206  1.1  christos 
   10207  1.1  christos   sect->size            = note->descsz;
   10208  1.1  christos   sect->filepos         = note->descpos;
   10209  1.1  christos   sect->alignment_power = 2;
   10210  1.1  christos 
   10211  1.1  christos   return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
   10212  1.1  christos }
   10213  1.1  christos 
   10214  1.1  christos static bfd_boolean
   10215  1.1  christos elfcore_grok_nto_regs (bfd *abfd,
   10216  1.1  christos 		       Elf_Internal_Note *note,
   10217  1.1  christos 		       long tid,
   10218  1.1  christos 		       char *base)
   10219  1.1  christos {
   10220  1.1  christos   char buf[100];
   10221  1.1  christos   char *name;
   10222  1.1  christos   asection *sect;
   10223  1.1  christos 
   10224  1.1  christos   /* Make a "(base)/%d" section.  */
   10225  1.1  christos   sprintf (buf, "%s/%ld", base, tid);
   10226  1.1  christos 
   10227  1.1  christos   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   10228  1.1  christos   if (name == NULL)
   10229  1.1  christos     return FALSE;
   10230  1.1  christos   strcpy (name, buf);
   10231  1.1  christos 
   10232  1.1  christos   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10233  1.1  christos   if (sect == NULL)
   10234  1.1  christos     return FALSE;
   10235  1.1  christos 
   10236  1.3  christos   sect->size            = note->descsz;
   10237  1.1  christos   sect->filepos         = note->descpos;
   10238  1.1  christos   sect->alignment_power = 2;
   10239  1.1  christos 
   10240  1.1  christos   /* This is the current thread.  */
   10241  1.1  christos   if (elf_tdata (abfd)->core->lwpid == tid)
   10242  1.1  christos     return elfcore_maybe_make_sect (abfd, base, sect);
   10243  1.1  christos 
   10244  1.1  christos   return TRUE;
   10245  1.1  christos }
   10246  1.1  christos 
   10247  1.1  christos #define BFD_QNT_CORE_INFO	7
   10248  1.1  christos #define BFD_QNT_CORE_STATUS	8
   10249  1.1  christos #define BFD_QNT_CORE_GREG	9
   10250  1.1  christos #define BFD_QNT_CORE_FPREG	10
   10251  1.1  christos 
   10252  1.1  christos static bfd_boolean
   10253  1.1  christos elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
   10254  1.1  christos {
   10255  1.1  christos   /* Every GREG section has a STATUS section before it.  Store the
   10256  1.1  christos      tid from the previous call to pass down to the next gregs
   10257  1.1  christos      function.  */
   10258  1.1  christos   static long tid = 1;
   10259  1.1  christos 
   10260  1.1  christos   switch (note->type)
   10261  1.1  christos     {
   10262  1.1  christos     case BFD_QNT_CORE_INFO:
   10263  1.1  christos       return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
   10264  1.1  christos     case BFD_QNT_CORE_STATUS:
   10265  1.1  christos       return elfcore_grok_nto_status (abfd, note, &tid);
   10266  1.1  christos     case BFD_QNT_CORE_GREG:
   10267  1.1  christos       return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
   10268  1.1  christos     case BFD_QNT_CORE_FPREG:
   10269  1.1  christos       return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
   10270  1.1  christos     default:
   10271  1.1  christos       return TRUE;
   10272  1.1  christos     }
   10273  1.1  christos }
   10274  1.1  christos 
   10275  1.1  christos static bfd_boolean
   10276  1.1  christos elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
   10277  1.1  christos {
   10278  1.1  christos   char *name;
   10279  1.1  christos   asection *sect;
   10280  1.1  christos   size_t len;
   10281  1.1  christos 
   10282  1.1  christos   /* Use note name as section name.  */
   10283  1.1  christos   len = note->namesz;
   10284  1.1  christos   name = (char *) bfd_alloc (abfd, len);
   10285  1.1  christos   if (name == NULL)
   10286  1.1  christos     return FALSE;
   10287  1.1  christos   memcpy (name, note->namedata, len);
   10288  1.1  christos   name[len - 1] = '\0';
   10289  1.1  christos 
   10290  1.1  christos   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10291  1.1  christos   if (sect == NULL)
   10292  1.1  christos     return FALSE;
   10293  1.1  christos 
   10294  1.1  christos   sect->size            = note->descsz;
   10295  1.1  christos   sect->filepos         = note->descpos;
   10296  1.1  christos   sect->alignment_power = 1;
   10297  1.1  christos 
   10298  1.1  christos   return TRUE;
   10299  1.1  christos }
   10300  1.1  christos 
   10301  1.1  christos /* Function: elfcore_write_note
   10302  1.1  christos 
   10303  1.1  christos    Inputs:
   10304  1.1  christos      buffer to hold note, and current size of buffer
   10305  1.1  christos      name of note
   10306  1.1  christos      type of note
   10307  1.1  christos      data for note
   10308  1.1  christos      size of data for note
   10309  1.1  christos 
   10310  1.1  christos    Writes note to end of buffer.  ELF64 notes are written exactly as
   10311  1.1  christos    for ELF32, despite the current (as of 2006) ELF gabi specifying
   10312  1.1  christos    that they ought to have 8-byte namesz and descsz field, and have
   10313  1.1  christos    8-byte alignment.  Other writers, eg. Linux kernel, do the same.
   10314  1.1  christos 
   10315  1.1  christos    Return:
   10316  1.1  christos    Pointer to realloc'd buffer, *BUFSIZ updated.  */
   10317  1.1  christos 
   10318  1.1  christos char *
   10319  1.1  christos elfcore_write_note (bfd *abfd,
   10320  1.1  christos 		    char *buf,
   10321  1.1  christos 		    int *bufsiz,
   10322  1.1  christos 		    const char *name,
   10323  1.1  christos 		    int type,
   10324  1.1  christos 		    const void *input,
   10325  1.1  christos 		    int size)
   10326  1.1  christos {
   10327  1.1  christos   Elf_External_Note *xnp;
   10328  1.1  christos   size_t namesz;
   10329  1.1  christos   size_t newspace;
   10330  1.1  christos   char *dest;
   10331  1.1  christos 
   10332  1.1  christos   namesz = 0;
   10333  1.1  christos   if (name != NULL)
   10334  1.1  christos     namesz = strlen (name) + 1;
   10335  1.1  christos 
   10336  1.1  christos   newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
   10337  1.1  christos 
   10338  1.1  christos   buf = (char *) realloc (buf, *bufsiz + newspace);
   10339  1.1  christos   if (buf == NULL)
   10340  1.1  christos     return buf;
   10341  1.1  christos   dest = buf + *bufsiz;
   10342  1.1  christos   *bufsiz += newspace;
   10343  1.1  christos   xnp = (Elf_External_Note *) dest;
   10344  1.1  christos   H_PUT_32 (abfd, namesz, xnp->namesz);
   10345  1.1  christos   H_PUT_32 (abfd, size, xnp->descsz);
   10346  1.1  christos   H_PUT_32 (abfd, type, xnp->type);
   10347  1.1  christos   dest = xnp->name;
   10348  1.1  christos   if (name != NULL)
   10349  1.1  christos     {
   10350  1.1  christos       memcpy (dest, name, namesz);
   10351  1.1  christos       dest += namesz;
   10352  1.1  christos       while (namesz & 3)
   10353  1.1  christos 	{
   10354  1.1  christos 	  *dest++ = '\0';
   10355  1.1  christos 	  ++namesz;
   10356  1.1  christos 	}
   10357  1.1  christos     }
   10358  1.1  christos   memcpy (dest, input, size);
   10359  1.1  christos   dest += size;
   10360  1.1  christos   while (size & 3)
   10361  1.1  christos     {
   10362  1.1  christos       *dest++ = '\0';
   10363  1.1  christos       ++size;
   10364  1.1  christos     }
   10365  1.1  christos   return buf;
   10366  1.1  christos }
   10367  1.1  christos 
   10368  1.1  christos char *
   10369  1.1  christos elfcore_write_prpsinfo (bfd  *abfd,
   10370  1.1  christos 			char *buf,
   10371  1.1  christos 			int  *bufsiz,
   10372  1.1  christos 			const char *fname,
   10373  1.1  christos 			const char *psargs)
   10374  1.1  christos {
   10375  1.1  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   10376  1.1  christos 
   10377  1.1  christos   if (bed->elf_backend_write_core_note != NULL)
   10378  1.1  christos     {
   10379  1.1  christos       char *ret;
   10380  1.1  christos       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   10381  1.3  christos 						 NT_PRPSINFO, fname, psargs);
   10382  1.1  christos       if (ret != NULL)
   10383  1.1  christos 	return ret;
   10384  1.1  christos     }
   10385  1.1  christos 
   10386  1.1  christos #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   10387  1.1  christos #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   10388  1.1  christos   if (bed->s->elfclass == ELFCLASS32)
   10389  1.1  christos     {
   10390  1.1  christos #if defined (HAVE_PSINFO32_T)
   10391  1.1  christos       psinfo32_t data;
   10392  1.1  christos       int note_type = NT_PSINFO;
   10393  1.1  christos #else
   10394  1.1  christos       prpsinfo32_t data;
   10395  1.1  christos       int note_type = NT_PRPSINFO;
   10396  1.1  christos #endif
   10397  1.3  christos 
   10398  1.1  christos       memset (&data, 0, sizeof (data));
   10399  1.1  christos       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   10400  1.1  christos       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   10401  1.1  christos       return elfcore_write_note (abfd, buf, bufsiz,
   10402  1.1  christos 				 "CORE", note_type, &data, sizeof (data));
   10403  1.1  christos     }
   10404  1.1  christos   else
   10405  1.1  christos #endif
   10406  1.1  christos     {
   10407  1.1  christos #if defined (HAVE_PSINFO_T)
   10408  1.1  christos       psinfo_t data;
   10409  1.1  christos       int note_type = NT_PSINFO;
   10410  1.1  christos #else
   10411  1.1  christos       prpsinfo_t data;
   10412  1.1  christos       int note_type = NT_PRPSINFO;
   10413  1.1  christos #endif
   10414  1.3  christos 
   10415  1.1  christos       memset (&data, 0, sizeof (data));
   10416  1.3  christos       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   10417  1.3  christos       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   10418  1.3  christos       return elfcore_write_note (abfd, buf, bufsiz,
   10419  1.3  christos 				 "CORE", note_type, &data, sizeof (data));
   10420  1.3  christos     }
   10421  1.3  christos #endif	/* PSINFO_T or PRPSINFO_T */
   10422  1.3  christos 
   10423  1.3  christos   free (buf);
   10424  1.3  christos   return NULL;
   10425  1.3  christos }
   10426  1.3  christos 
   10427  1.3  christos char *
   10428  1.3  christos elfcore_write_linux_prpsinfo32
   10429  1.8  christos   (bfd *abfd, char *buf, int *bufsiz,
   10430  1.3  christos    const struct elf_internal_linux_prpsinfo *prpsinfo)
   10431  1.3  christos {
   10432  1.3  christos   struct elf_external_linux_prpsinfo32 data;
   10433  1.3  christos 
   10434  1.3  christos   swap_linux_prpsinfo32_out (abfd, prpsinfo, &data);
   10435  1.3  christos   return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
   10436  1.3  christos 			     &data, sizeof (data));
   10437  1.3  christos }
   10438  1.3  christos 
   10439  1.3  christos char *
   10440  1.3  christos elfcore_write_linux_prpsinfo64
   10441  1.8  christos   (bfd *abfd, char *buf, int *bufsiz,
   10442  1.3  christos    const struct elf_internal_linux_prpsinfo *prpsinfo)
   10443  1.3  christos {
   10444  1.1  christos   struct elf_external_linux_prpsinfo64 data;
   10445  1.1  christos 
   10446  1.1  christos   swap_linux_prpsinfo64_out (abfd, prpsinfo, &data);
   10447  1.1  christos   return elfcore_write_note (abfd, buf, bufsiz,
   10448  1.1  christos 			     "CORE", NT_PRPSINFO, &data, sizeof (data));
   10449  1.1  christos }
   10450  1.1  christos 
   10451  1.1  christos char *
   10452  1.1  christos elfcore_write_prstatus (bfd *abfd,
   10453  1.1  christos 			char *buf,
   10454  1.1  christos 			int *bufsiz,
   10455  1.1  christos 			long pid,
   10456  1.1  christos 			int cursig,
   10457  1.1  christos 			const void *gregs)
   10458  1.1  christos {
   10459  1.1  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   10460  1.1  christos 
   10461  1.1  christos   if (bed->elf_backend_write_core_note != NULL)
   10462  1.1  christos     {
   10463  1.1  christos       char *ret;
   10464  1.1  christos       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   10465  1.1  christos 						 NT_PRSTATUS,
   10466  1.3  christos 						 pid, cursig, gregs);
   10467  1.1  christos       if (ret != NULL)
   10468  1.1  christos 	return ret;
   10469  1.1  christos     }
   10470  1.1  christos 
   10471  1.1  christos #if defined (HAVE_PRSTATUS_T)
   10472  1.1  christos #if defined (HAVE_PRSTATUS32_T)
   10473  1.1  christos   if (bed->s->elfclass == ELFCLASS32)
   10474  1.1  christos     {
   10475  1.1  christos       prstatus32_t prstat;
   10476  1.3  christos 
   10477  1.1  christos       memset (&prstat, 0, sizeof (prstat));
   10478  1.1  christos       prstat.pr_pid = pid;
   10479  1.1  christos       prstat.pr_cursig = cursig;
   10480  1.1  christos       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   10481  1.1  christos       return elfcore_write_note (abfd, buf, bufsiz, "CORE",
   10482  1.1  christos 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   10483  1.1  christos     }
   10484  1.1  christos   else
   10485  1.1  christos #endif
   10486  1.1  christos     {
   10487  1.1  christos       prstatus_t prstat;
   10488  1.3  christos 
   10489  1.1  christos       memset (&prstat, 0, sizeof (prstat));
   10490  1.1  christos       prstat.pr_pid = pid;
   10491  1.3  christos       prstat.pr_cursig = cursig;
   10492  1.3  christos       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   10493  1.3  christos       return elfcore_write_note (abfd, buf, bufsiz, "CORE",
   10494  1.3  christos 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   10495  1.1  christos     }
   10496  1.1  christos #endif /* HAVE_PRSTATUS_T */
   10497  1.1  christos 
   10498  1.1  christos   free (buf);
   10499  1.1  christos   return NULL;
   10500  1.1  christos }
   10501  1.1  christos 
   10502  1.1  christos #if defined (HAVE_LWPSTATUS_T)
   10503  1.1  christos char *
   10504  1.1  christos elfcore_write_lwpstatus (bfd *abfd,
   10505  1.1  christos 			 char *buf,
   10506  1.1  christos 			 int *bufsiz,
   10507  1.1  christos 			 long pid,
   10508  1.1  christos 			 int cursig,
   10509  1.1  christos 			 const void *gregs)
   10510  1.1  christos {
   10511  1.1  christos   lwpstatus_t lwpstat;
   10512  1.1  christos   const char *note_name = "CORE";
   10513  1.5  christos 
   10514  1.1  christos   memset (&lwpstat, 0, sizeof (lwpstat));
   10515  1.1  christos   lwpstat.pr_lwpid  = pid >> 16;
   10516  1.1  christos   lwpstat.pr_cursig = cursig;
   10517  1.1  christos #if defined (HAVE_LWPSTATUS_T_PR_REG)
   10518  1.1  christos   memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
   10519  1.1  christos #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   10520  1.1  christos #if !defined(gregs)
   10521  1.1  christos   memcpy (lwpstat.pr_context.uc_mcontext.gregs,
   10522  1.1  christos 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
   10523  1.1  christos #else
   10524  1.1  christos   memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
   10525  1.1  christos 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
   10526  1.1  christos #endif
   10527  1.1  christos #endif
   10528  1.1  christos   return elfcore_write_note (abfd, buf, bufsiz, note_name,
   10529  1.1  christos 			     NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
   10530  1.1  christos }
   10531  1.1  christos #endif /* HAVE_LWPSTATUS_T */
   10532  1.1  christos 
   10533  1.1  christos #if defined (HAVE_PSTATUS_T)
   10534  1.1  christos char *
   10535  1.1  christos elfcore_write_pstatus (bfd *abfd,
   10536  1.1  christos 		       char *buf,
   10537  1.1  christos 		       int *bufsiz,
   10538  1.1  christos 		       long pid,
   10539  1.1  christos 		       int cursig ATTRIBUTE_UNUSED,
   10540  1.1  christos 		       const void *gregs ATTRIBUTE_UNUSED)
   10541  1.1  christos {
   10542  1.1  christos   const char *note_name = "CORE";
   10543  1.1  christos #if defined (HAVE_PSTATUS32_T)
   10544  1.1  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   10545  1.1  christos 
   10546  1.1  christos   if (bed->s->elfclass == ELFCLASS32)
   10547  1.1  christos     {
   10548  1.1  christos       pstatus32_t pstat;
   10549  1.1  christos 
   10550  1.1  christos       memset (&pstat, 0, sizeof (pstat));
   10551  1.1  christos       pstat.pr_pid = pid & 0xffff;
   10552  1.1  christos       buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
   10553  1.1  christos 				NT_PSTATUS, &pstat, sizeof (pstat));
   10554  1.1  christos       return buf;
   10555  1.1  christos     }
   10556  1.1  christos   else
   10557  1.1  christos #endif
   10558  1.1  christos     {
   10559  1.1  christos       pstatus_t pstat;
   10560  1.1  christos 
   10561  1.1  christos       memset (&pstat, 0, sizeof (pstat));
   10562  1.1  christos       pstat.pr_pid = pid & 0xffff;
   10563  1.1  christos       buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
   10564  1.1  christos 				NT_PSTATUS, &pstat, sizeof (pstat));
   10565  1.1  christos       return buf;
   10566  1.1  christos     }
   10567  1.1  christos }
   10568  1.1  christos #endif /* HAVE_PSTATUS_T */
   10569  1.1  christos 
   10570  1.1  christos char *
   10571  1.1  christos elfcore_write_prfpreg (bfd *abfd,
   10572  1.1  christos 		       char *buf,
   10573  1.1  christos 		       int *bufsiz,
   10574  1.1  christos 		       const void *fpregs,
   10575  1.1  christos 		       int size)
   10576  1.1  christos {
   10577  1.1  christos   const char *note_name = "CORE";
   10578  1.1  christos   return elfcore_write_note (abfd, buf, bufsiz,
   10579  1.1  christos 			     note_name, NT_FPREGSET, fpregs, size);
   10580  1.1  christos }
   10581  1.1  christos 
   10582  1.1  christos char *
   10583  1.1  christos elfcore_write_prxfpreg (bfd *abfd,
   10584  1.1  christos 			char *buf,
   10585  1.1  christos 			int *bufsiz,
   10586  1.1  christos 			const void *xfpregs,
   10587  1.1  christos 			int size)
   10588  1.1  christos {
   10589  1.1  christos   char *note_name = "LINUX";
   10590  1.1  christos   return elfcore_write_note (abfd, buf, bufsiz,
   10591  1.1  christos 			     note_name, NT_PRXFPREG, xfpregs, size);
   10592  1.1  christos }
   10593  1.6  christos 
   10594  1.6  christos char *
   10595  1.6  christos elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
   10596  1.6  christos 			 const void *xfpregs, int size)
   10597  1.6  christos {
   10598  1.1  christos   char *note_name;
   10599  1.1  christos   if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
   10600  1.1  christos     note_name = "FreeBSD";
   10601  1.1  christos   else
   10602  1.1  christos     note_name = "LINUX";
   10603  1.1  christos   return elfcore_write_note (abfd, buf, bufsiz,
   10604  1.1  christos 			     note_name, NT_X86_XSTATE, xfpregs, size);
   10605  1.1  christos }
   10606  1.1  christos 
   10607  1.1  christos char *
   10608  1.1  christos elfcore_write_ppc_vmx (bfd *abfd,
   10609  1.1  christos 		       char *buf,
   10610  1.1  christos 		       int *bufsiz,
   10611  1.1  christos 		       const void *ppc_vmx,
   10612  1.1  christos 		       int size)
   10613  1.1  christos {
   10614  1.1  christos   char *note_name = "LINUX";
   10615  1.1  christos   return elfcore_write_note (abfd, buf, bufsiz,
   10616  1.1  christos 			     note_name, NT_PPC_VMX, ppc_vmx, size);
   10617  1.1  christos }
   10618  1.1  christos 
   10619  1.1  christos char *
   10620  1.1  christos elfcore_write_ppc_vsx (bfd *abfd,
   10621  1.1  christos                        char *buf,
   10622  1.1  christos                        int *bufsiz,
   10623  1.1  christos                        const void *ppc_vsx,
   10624  1.1  christos                        int size)
   10625  1.1  christos {
   10626  1.1  christos   char *note_name = "LINUX";
   10627  1.1  christos   return elfcore_write_note (abfd, buf, bufsiz,
   10628  1.1  christos                              note_name, NT_PPC_VSX, ppc_vsx, size);
   10629  1.1  christos }
   10630  1.1  christos 
   10631  1.1  christos static char *
   10632  1.1  christos elfcore_write_s390_high_gprs (bfd *abfd,
   10633  1.1  christos 			      char *buf,
   10634  1.1  christos 			      int *bufsiz,
   10635  1.1  christos 			      const void *s390_high_gprs,
   10636  1.1  christos 			      int size)
   10637  1.1  christos {
   10638  1.1  christos   char *note_name = "LINUX";
   10639  1.1  christos   return elfcore_write_note (abfd, buf, bufsiz,
   10640  1.1  christos                              note_name, NT_S390_HIGH_GPRS,
   10641  1.1  christos 			     s390_high_gprs, size);
   10642  1.1  christos }
   10643  1.1  christos 
   10644  1.1  christos char *
   10645  1.1  christos elfcore_write_s390_timer (bfd *abfd,
   10646  1.1  christos                           char *buf,
   10647  1.1  christos                           int *bufsiz,
   10648  1.1  christos                           const void *s390_timer,
   10649  1.1  christos                           int size)
   10650  1.1  christos {
   10651  1.1  christos   char *note_name = "LINUX";
   10652  1.1  christos   return elfcore_write_note (abfd, buf, bufsiz,
   10653  1.1  christos                              note_name, NT_S390_TIMER, s390_timer, size);
   10654  1.1  christos }
   10655  1.1  christos 
   10656  1.1  christos char *
   10657  1.1  christos elfcore_write_s390_todcmp (bfd *abfd,
   10658  1.1  christos                            char *buf,
   10659  1.1  christos                            int *bufsiz,
   10660  1.1  christos                            const void *s390_todcmp,
   10661  1.1  christos                            int size)
   10662  1.1  christos {
   10663  1.1  christos   char *note_name = "LINUX";
   10664  1.1  christos   return elfcore_write_note (abfd, buf, bufsiz,
   10665  1.1  christos                              note_name, NT_S390_TODCMP, s390_todcmp, size);
   10666  1.1  christos }
   10667  1.1  christos 
   10668  1.1  christos char *
   10669  1.1  christos elfcore_write_s390_todpreg (bfd *abfd,
   10670  1.1  christos                             char *buf,
   10671  1.1  christos                             int *bufsiz,
   10672  1.1  christos                             const void *s390_todpreg,
   10673  1.1  christos                             int size)
   10674  1.1  christos {
   10675  1.1  christos   char *note_name = "LINUX";
   10676  1.1  christos   return elfcore_write_note (abfd, buf, bufsiz,
   10677  1.1  christos                              note_name, NT_S390_TODPREG, s390_todpreg, size);
   10678  1.1  christos }
   10679  1.1  christos 
   10680  1.1  christos char *
   10681  1.1  christos elfcore_write_s390_ctrs (bfd *abfd,
   10682  1.1  christos                          char *buf,
   10683  1.1  christos                          int *bufsiz,
   10684  1.1  christos                          const void *s390_ctrs,
   10685  1.1  christos                          int size)
   10686  1.1  christos {
   10687  1.1  christos   char *note_name = "LINUX";
   10688  1.1  christos   return elfcore_write_note (abfd, buf, bufsiz,
   10689  1.1  christos                              note_name, NT_S390_CTRS, s390_ctrs, size);
   10690  1.1  christos }
   10691  1.1  christos 
   10692  1.1  christos char *
   10693  1.1  christos elfcore_write_s390_prefix (bfd *abfd,
   10694  1.1  christos                            char *buf,
   10695  1.1  christos                            int *bufsiz,
   10696  1.1  christos                            const void *s390_prefix,
   10697  1.1  christos                            int size)
   10698  1.1  christos {
   10699  1.1  christos   char *note_name = "LINUX";
   10700  1.3  christos   return elfcore_write_note (abfd, buf, bufsiz,
   10701  1.3  christos                              note_name, NT_S390_PREFIX, s390_prefix, size);
   10702  1.3  christos }
   10703  1.3  christos 
   10704  1.3  christos char *
   10705  1.3  christos elfcore_write_s390_last_break (bfd *abfd,
   10706  1.3  christos 			       char *buf,
   10707  1.3  christos 			       int *bufsiz,
   10708  1.3  christos 			       const void *s390_last_break,
   10709  1.3  christos 			       int size)
   10710  1.3  christos {
   10711  1.3  christos   char *note_name = "LINUX";
   10712  1.3  christos   return elfcore_write_note (abfd, buf, bufsiz,
   10713  1.3  christos                              note_name, NT_S390_LAST_BREAK,
   10714  1.3  christos 			     s390_last_break, size);
   10715  1.3  christos }
   10716  1.3  christos 
   10717  1.3  christos char *
   10718  1.3  christos elfcore_write_s390_system_call (bfd *abfd,
   10719  1.3  christos 				char *buf,
   10720  1.3  christos 				int *bufsiz,
   10721  1.3  christos 				const void *s390_system_call,
   10722  1.3  christos 				int size)
   10723  1.3  christos {
   10724  1.3  christos   char *note_name = "LINUX";
   10725  1.3  christos   return elfcore_write_note (abfd, buf, bufsiz,
   10726  1.3  christos                              note_name, NT_S390_SYSTEM_CALL,
   10727  1.3  christos 			     s390_system_call, size);
   10728  1.3  christos }
   10729  1.3  christos 
   10730  1.3  christos char *
   10731  1.3  christos elfcore_write_s390_tdb (bfd *abfd,
   10732  1.3  christos 			char *buf,
   10733  1.3  christos 			int *bufsiz,
   10734  1.3  christos 			const void *s390_tdb,
   10735  1.3  christos 			int size)
   10736  1.3  christos {
   10737  1.3  christos   char *note_name = "LINUX";
   10738  1.6  christos   return elfcore_write_note (abfd, buf, bufsiz,
   10739  1.6  christos                              note_name, NT_S390_TDB, s390_tdb, size);
   10740  1.6  christos }
   10741  1.6  christos 
   10742  1.6  christos char *
   10743  1.6  christos elfcore_write_s390_vxrs_low (bfd *abfd,
   10744  1.6  christos 			     char *buf,
   10745  1.6  christos 			     int *bufsiz,
   10746  1.6  christos 			     const void *s390_vxrs_low,
   10747  1.6  christos 			     int size)
   10748  1.6  christos {
   10749  1.6  christos   char *note_name = "LINUX";
   10750  1.6  christos   return elfcore_write_note (abfd, buf, bufsiz,
   10751  1.6  christos 			     note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
   10752  1.6  christos }
   10753  1.6  christos 
   10754  1.6  christos char *
   10755  1.6  christos elfcore_write_s390_vxrs_high (bfd *abfd,
   10756  1.6  christos 			     char *buf,
   10757  1.6  christos 			     int *bufsiz,
   10758  1.6  christos 			     const void *s390_vxrs_high,
   10759  1.6  christos 			     int size)
   10760  1.6  christos {
   10761  1.6  christos   char *note_name = "LINUX";
   10762  1.6  christos   return elfcore_write_note (abfd, buf, bufsiz,
   10763  1.3  christos 			     note_name, NT_S390_VXRS_HIGH,
   10764  1.3  christos 			     s390_vxrs_high, size);
   10765  1.3  christos }
   10766  1.3  christos 
   10767  1.3  christos char *
   10768  1.3  christos elfcore_write_arm_vfp (bfd *abfd,
   10769  1.3  christos 		       char *buf,
   10770  1.3  christos 		       int *bufsiz,
   10771  1.3  christos 		       const void *arm_vfp,
   10772  1.3  christos 		       int size)
   10773  1.3  christos {
   10774  1.3  christos   char *note_name = "LINUX";
   10775  1.3  christos   return elfcore_write_note (abfd, buf, bufsiz,
   10776  1.3  christos 			     note_name, NT_ARM_VFP, arm_vfp, size);
   10777  1.3  christos }
   10778  1.3  christos 
   10779  1.3  christos char *
   10780  1.3  christos elfcore_write_aarch_tls (bfd *abfd,
   10781  1.3  christos 		       char *buf,
   10782  1.3  christos 		       int *bufsiz,
   10783  1.3  christos 		       const void *aarch_tls,
   10784  1.3  christos 		       int size)
   10785  1.3  christos {
   10786  1.3  christos   char *note_name = "LINUX";
   10787  1.3  christos   return elfcore_write_note (abfd, buf, bufsiz,
   10788  1.3  christos 			     note_name, NT_ARM_TLS, aarch_tls, size);
   10789  1.3  christos }
   10790  1.3  christos 
   10791  1.3  christos char *
   10792  1.3  christos elfcore_write_aarch_hw_break (bfd *abfd,
   10793  1.3  christos 			    char *buf,
   10794  1.3  christos 			    int *bufsiz,
   10795  1.3  christos 			    const void *aarch_hw_break,
   10796  1.3  christos 			    int size)
   10797  1.3  christos {
   10798  1.3  christos   char *note_name = "LINUX";
   10799  1.3  christos   return elfcore_write_note (abfd, buf, bufsiz,
   10800  1.3  christos 			     note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
   10801  1.3  christos }
   10802  1.3  christos 
   10803  1.3  christos char *
   10804  1.3  christos elfcore_write_aarch_hw_watch (bfd *abfd,
   10805  1.3  christos 			    char *buf,
   10806  1.3  christos 			    int *bufsiz,
   10807  1.3  christos 			    const void *aarch_hw_watch,
   10808  1.3  christos 			    int size)
   10809  1.3  christos {
   10810  1.3  christos   char *note_name = "LINUX";
   10811  1.1  christos   return elfcore_write_note (abfd, buf, bufsiz,
   10812  1.1  christos 			     note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
   10813  1.1  christos }
   10814  1.1  christos 
   10815  1.1  christos char *
   10816  1.1  christos elfcore_write_register_note (bfd *abfd,
   10817  1.1  christos 			     char *buf,
   10818  1.1  christos 			     int *bufsiz,
   10819  1.1  christos 			     const char *section,
   10820  1.1  christos 			     const void *data,
   10821  1.1  christos 			     int size)
   10822  1.1  christos {
   10823  1.1  christos   if (strcmp (section, ".reg2") == 0)
   10824  1.1  christos     return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
   10825  1.1  christos   if (strcmp (section, ".reg-xfp") == 0)
   10826  1.1  christos     return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
   10827  1.1  christos   if (strcmp (section, ".reg-xstate") == 0)
   10828  1.1  christos     return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
   10829  1.1  christos   if (strcmp (section, ".reg-ppc-vmx") == 0)
   10830  1.1  christos     return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
   10831  1.1  christos   if (strcmp (section, ".reg-ppc-vsx") == 0)
   10832  1.1  christos     return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
   10833  1.1  christos   if (strcmp (section, ".reg-s390-high-gprs") == 0)
   10834  1.1  christos     return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
   10835  1.1  christos   if (strcmp (section, ".reg-s390-timer") == 0)
   10836  1.1  christos     return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
   10837  1.1  christos   if (strcmp (section, ".reg-s390-todcmp") == 0)
   10838  1.1  christos     return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
   10839  1.1  christos   if (strcmp (section, ".reg-s390-todpreg") == 0)
   10840  1.3  christos     return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
   10841  1.3  christos   if (strcmp (section, ".reg-s390-ctrs") == 0)
   10842  1.3  christos     return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
   10843  1.3  christos   if (strcmp (section, ".reg-s390-prefix") == 0)
   10844  1.3  christos     return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
   10845  1.3  christos   if (strcmp (section, ".reg-s390-last-break") == 0)
   10846  1.6  christos     return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
   10847  1.6  christos   if (strcmp (section, ".reg-s390-system-call") == 0)
   10848  1.6  christos     return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
   10849  1.6  christos   if (strcmp (section, ".reg-s390-tdb") == 0)
   10850  1.3  christos     return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
   10851  1.3  christos   if (strcmp (section, ".reg-s390-vxrs-low") == 0)
   10852  1.3  christos     return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
   10853  1.3  christos   if (strcmp (section, ".reg-s390-vxrs-high") == 0)
   10854  1.3  christos     return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
   10855  1.3  christos   if (strcmp (section, ".reg-arm-vfp") == 0)
   10856  1.3  christos     return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
   10857  1.3  christos   if (strcmp (section, ".reg-aarch-tls") == 0)
   10858  1.1  christos     return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
   10859  1.1  christos   if (strcmp (section, ".reg-aarch-hw-break") == 0)
   10860  1.1  christos     return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
   10861  1.1  christos   if (strcmp (section, ".reg-aarch-hw-watch") == 0)
   10862  1.1  christos     return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
   10863  1.1  christos   return NULL;
   10864  1.1  christos }
   10865  1.1  christos 
   10866  1.1  christos static bfd_boolean
   10867  1.1  christos elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
   10868  1.1  christos {
   10869  1.1  christos   char *p;
   10870  1.1  christos 
   10871  1.1  christos   p = buf;
   10872  1.1  christos   while (p < buf + size)
   10873  1.1  christos     {
   10874  1.1  christos       /* FIXME: bad alignment assumption.  */
   10875  1.1  christos       Elf_External_Note *xnp = (Elf_External_Note *) p;
   10876  1.1  christos       Elf_Internal_Note in;
   10877  1.1  christos 
   10878  1.1  christos       if (offsetof (Elf_External_Note, name) > buf - p + size)
   10879  1.1  christos 	return FALSE;
   10880  1.1  christos 
   10881  1.1  christos       in.type = H_GET_32 (abfd, xnp->type);
   10882  1.1  christos 
   10883  1.1  christos       in.namesz = H_GET_32 (abfd, xnp->namesz);
   10884  1.1  christos       in.namedata = xnp->name;
   10885  1.1  christos       if (in.namesz > buf - in.namedata + size)
   10886  1.1  christos 	return FALSE;
   10887  1.1  christos 
   10888  1.1  christos       in.descsz = H_GET_32 (abfd, xnp->descsz);
   10889  1.1  christos       in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
   10890  1.1  christos       in.descpos = offset + (in.descdata - buf);
   10891  1.1  christos       if (in.descsz != 0
   10892  1.1  christos 	  && (in.descdata >= buf + size
   10893  1.1  christos 	      || in.descsz > buf - in.descdata + size))
   10894  1.1  christos 	return FALSE;
   10895  1.1  christos 
   10896  1.1  christos       switch (bfd_get_format (abfd))
   10897  1.5  christos         {
   10898  1.5  christos 	default:
   10899  1.5  christos 	  return TRUE;
   10900  1.1  christos 
   10901  1.5  christos 	case bfd_core:
   10902  1.5  christos 	  {
   10903  1.5  christos #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
   10904  1.1  christos 	    struct
   10905  1.5  christos 	    {
   10906  1.1  christos 	      const char * string;
   10907  1.5  christos 	      size_t len;
   10908  1.8  christos 	      bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
   10909  1.5  christos 	    }
   10910  1.5  christos 	    grokers[] =
   10911  1.5  christos 	    {
   10912  1.5  christos 	      GROKER_ELEMENT ("", elfcore_grok_note),
   10913  1.5  christos 	      GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
   10914  1.5  christos 	      GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
   10915  1.5  christos 	      GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
   10916  1.5  christos 	      GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
   10917  1.5  christos 	      GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note)
   10918  1.5  christos 	    };
   10919  1.5  christos #undef GROKER_ELEMENT
   10920  1.5  christos 	    int i;
   10921  1.5  christos 
   10922  1.5  christos 	    for (i = ARRAY_SIZE (grokers); i--;)
   10923  1.5  christos 	      {
   10924  1.5  christos 		if (in.namesz >= grokers[i].len
   10925  1.5  christos 		    && strncmp (in.namedata, grokers[i].string,
   10926  1.5  christos 				grokers[i].len) == 0)
   10927  1.5  christos 		  {
   10928  1.5  christos 		    if (! grokers[i].func (abfd, & in))
   10929  1.5  christos 		      return FALSE;
   10930  1.1  christos 		    break;
   10931  1.1  christos 		  }
   10932  1.1  christos 	      }
   10933  1.1  christos 	    break;
   10934  1.1  christos 	  }
   10935  1.1  christos 
   10936  1.1  christos 	case bfd_object:
   10937  1.3  christos 	  if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
   10938  1.3  christos 	    {
   10939  1.3  christos 	      if (! elfobj_grok_gnu_note (abfd, &in))
   10940  1.3  christos 		return FALSE;
   10941  1.3  christos 	    }
   10942  1.3  christos 	  else if (in.namesz == sizeof "stapsdt"
   10943  1.1  christos 		   && strcmp (in.namedata, "stapsdt") == 0)
   10944  1.1  christos 	    {
   10945  1.1  christos 	      if (! elfobj_grok_stapsdt_note (abfd, &in))
   10946  1.1  christos 		return FALSE;
   10947  1.1  christos 	    }
   10948  1.1  christos 	  break;
   10949  1.1  christos 	}
   10950  1.1  christos 
   10951  1.1  christos       p = in.descdata + BFD_ALIGN (in.descsz, 4);
   10952  1.1  christos     }
   10953  1.1  christos 
   10954  1.1  christos   return TRUE;
   10955  1.1  christos }
   10956  1.1  christos 
   10957  1.1  christos static bfd_boolean
   10958  1.1  christos elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
   10959  1.1  christos {
   10960  1.1  christos   char *buf;
   10961  1.1  christos 
   10962  1.1  christos   if (size <= 0)
   10963  1.5  christos     return TRUE;
   10964  1.1  christos 
   10965  1.1  christos   if (bfd_seek (abfd, offset, SEEK_SET) != 0)
   10966  1.1  christos     return FALSE;
   10967  1.5  christos 
   10968  1.5  christos   buf = (char *) bfd_malloc (size + 1);
   10969  1.5  christos   if (buf == NULL)
   10970  1.5  christos     return FALSE;
   10971  1.1  christos 
   10972  1.1  christos   /* PR 17512: file: ec08f814
   10973  1.1  christos      0-termintate the buffer so that string searches will not overflow.  */
   10974  1.1  christos   buf[size] = 0;
   10975  1.1  christos 
   10976  1.1  christos   if (bfd_bread (buf, size, abfd) != size
   10977  1.1  christos       || !elf_parse_notes (abfd, buf, size, offset))
   10978  1.1  christos     {
   10979  1.1  christos       free (buf);
   10980  1.1  christos       return FALSE;
   10981  1.1  christos     }
   10982  1.1  christos 
   10983  1.1  christos   free (buf);
   10984  1.1  christos   return TRUE;
   10985  1.1  christos }
   10986  1.1  christos 
   10987  1.1  christos /* Providing external access to the ELF program header table.  */
   10989  1.1  christos 
   10990  1.1  christos /* Return an upper bound on the number of bytes required to store a
   10991  1.1  christos    copy of ABFD's program header table entries.  Return -1 if an error
   10992  1.1  christos    occurs; bfd_get_error will return an appropriate code.  */
   10993  1.1  christos 
   10994  1.1  christos long
   10995  1.1  christos bfd_get_elf_phdr_upper_bound (bfd *abfd)
   10996  1.1  christos {
   10997  1.1  christos   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   10998  1.1  christos     {
   10999  1.1  christos       bfd_set_error (bfd_error_wrong_format);
   11000  1.1  christos       return -1;
   11001  1.1  christos     }
   11002  1.1  christos 
   11003  1.1  christos   return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
   11004  1.1  christos }
   11005  1.1  christos 
   11006  1.1  christos /* Copy ABFD's program header table entries to *PHDRS.  The entries
   11007  1.1  christos    will be stored as an array of Elf_Internal_Phdr structures, as
   11008  1.1  christos    defined in include/elf/internal.h.  To find out how large the
   11009  1.1  christos    buffer needs to be, call bfd_get_elf_phdr_upper_bound.
   11010  1.1  christos 
   11011  1.1  christos    Return the number of program header table entries read, or -1 if an
   11012  1.1  christos    error occurs; bfd_get_error will return an appropriate code.  */
   11013  1.1  christos 
   11014  1.1  christos int
   11015  1.1  christos bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
   11016  1.1  christos {
   11017  1.1  christos   int num_phdrs;
   11018  1.1  christos 
   11019  1.1  christos   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   11020  1.1  christos     {
   11021  1.1  christos       bfd_set_error (bfd_error_wrong_format);
   11022  1.1  christos       return -1;
   11023  1.1  christos     }
   11024  1.1  christos 
   11025  1.1  christos   num_phdrs = elf_elfheader (abfd)->e_phnum;
   11026  1.1  christos   memcpy (phdrs, elf_tdata (abfd)->phdr,
   11027  1.4  christos 	  num_phdrs * sizeof (Elf_Internal_Phdr));
   11028  1.4  christos 
   11029  1.4  christos   return num_phdrs;
   11030  1.1  christos }
   11031  1.1  christos 
   11032  1.1  christos enum elf_reloc_type_class
   11033  1.1  christos _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   11034  1.1  christos 			   const asection *rel_sec ATTRIBUTE_UNUSED,
   11035  1.1  christos 			   const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
   11036  1.1  christos {
   11037  1.1  christos   return reloc_class_normal;
   11038  1.1  christos }
   11039  1.1  christos 
   11040  1.1  christos /* For RELA architectures, return the relocation value for a
   11041  1.1  christos    relocation against a local symbol.  */
   11042  1.1  christos 
   11043  1.1  christos bfd_vma
   11044  1.1  christos _bfd_elf_rela_local_sym (bfd *abfd,
   11045  1.1  christos 			 Elf_Internal_Sym *sym,
   11046  1.1  christos 			 asection **psec,
   11047  1.1  christos 			 Elf_Internal_Rela *rel)
   11048  1.1  christos {
   11049  1.1  christos   asection *sec = *psec;
   11050  1.1  christos   bfd_vma relocation;
   11051  1.3  christos 
   11052  1.1  christos   relocation = (sec->output_section->vma
   11053  1.1  christos 		+ sec->output_offset
   11054  1.1  christos 		+ sym->st_value);
   11055  1.1  christos   if ((sec->flags & SEC_MERGE)
   11056  1.1  christos       && ELF_ST_TYPE (sym->st_info) == STT_SECTION
   11057  1.1  christos       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
   11058  1.1  christos     {
   11059  1.1  christos       rel->r_addend =
   11060  1.1  christos 	_bfd_merged_section_offset (abfd, psec,
   11061  1.1  christos 				    elf_section_data (sec)->sec_info,
   11062  1.1  christos 				    sym->st_value + rel->r_addend);
   11063  1.1  christos       if (sec != *psec)
   11064  1.1  christos 	{
   11065  1.1  christos 	  /* If we have changed the section, and our original section is
   11066  1.1  christos 	     marked with SEC_EXCLUDE, it means that the original
   11067  1.1  christos 	     SEC_MERGE section has been completely subsumed in some
   11068  1.1  christos 	     other SEC_MERGE section.  In this case, we need to leave
   11069  1.1  christos 	     some info around for --emit-relocs.  */
   11070  1.1  christos 	  if ((sec->flags & SEC_EXCLUDE) != 0)
   11071  1.1  christos 	    sec->kept_section = *psec;
   11072  1.1  christos 	  sec = *psec;
   11073  1.1  christos 	}
   11074  1.1  christos       rel->r_addend -= relocation;
   11075  1.1  christos       rel->r_addend += sec->output_section->vma + sec->output_offset;
   11076  1.1  christos     }
   11077  1.1  christos   return relocation;
   11078  1.1  christos }
   11079  1.1  christos 
   11080  1.1  christos bfd_vma
   11081  1.1  christos _bfd_elf_rel_local_sym (bfd *abfd,
   11082  1.3  christos 			Elf_Internal_Sym *sym,
   11083  1.1  christos 			asection **psec,
   11084  1.1  christos 			bfd_vma addend)
   11085  1.1  christos {
   11086  1.1  christos   asection *sec = *psec;
   11087  1.1  christos 
   11088  1.1  christos   if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
   11089  1.1  christos     return sym->st_value + addend;
   11090  1.8  christos 
   11091  1.8  christos   return _bfd_merged_section_offset (abfd, psec,
   11092  1.8  christos 				     elf_section_data (sec)->sec_info,
   11093  1.8  christos 				     sym->st_value + addend);
   11094  1.8  christos }
   11095  1.8  christos 
   11096  1.1  christos /* Adjust an address within a section.  Given OFFSET within SEC, return
   11097  1.1  christos    the new offset within the section, based upon changes made to the
   11098  1.1  christos    section.  Returns -1 if the offset is now invalid.
   11099  1.1  christos    The offset (in abnd out) is in target sized bytes, however big a
   11100  1.1  christos    byte may be.  */
   11101  1.1  christos 
   11102  1.1  christos bfd_vma
   11103  1.1  christos _bfd_elf_section_offset (bfd *abfd,
   11104  1.3  christos 			 struct bfd_link_info *info,
   11105  1.1  christos 			 asection *sec,
   11106  1.1  christos 			 bfd_vma offset)
   11107  1.3  christos {
   11108  1.1  christos   switch (sec->sec_info_type)
   11109  1.8  christos     {
   11110  1.1  christos     case SEC_INFO_TYPE_STABS:
   11111  1.3  christos       return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
   11112  1.3  christos 				       offset);
   11113  1.8  christos     case SEC_INFO_TYPE_EH_FRAME:
   11114  1.3  christos       return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
   11115  1.3  christos 
   11116  1.8  christos     default:
   11117  1.8  christos       if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
   11118  1.8  christos 	{
   11119  1.8  christos 	  /* Reverse the offset.  */
   11120  1.3  christos 	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11121  1.1  christos 	  bfd_size_type address_size = bed->s->arch_size / 8;
   11122  1.1  christos 
   11123  1.1  christos 	  /* address_size and sec->size are in octets.  Convert
   11124  1.1  christos 	     to bytes before subtracting the original offset.  */
   11125  1.1  christos 	  offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset;
   11126  1.1  christos 	}
   11127  1.1  christos       return offset;
   11128  1.1  christos     }
   11129  1.1  christos }
   11130  1.1  christos 
   11131  1.1  christos /* Create a new BFD as if by bfd_openr.  Rather than opening a file,
   11133  1.1  christos    reconstruct an ELF file by reading the segments out of remote memory
   11134  1.1  christos    based on the ELF file header at EHDR_VMA and the ELF program headers it
   11135  1.1  christos    points to.  If not null, *LOADBASEP is filled in with the difference
   11136  1.1  christos    between the VMAs from which the segments were read, and the VMAs the
   11137  1.1  christos    file headers (and hence BFD's idea of each section's VMA) put them at.
   11138  1.1  christos 
   11139  1.1  christos    The function TARGET_READ_MEMORY is called to copy LEN bytes from the
   11140  1.1  christos    remote memory at target address VMA into the local buffer at MYADDR; it
   11141  1.1  christos    should return zero on success or an `errno' code on failure.  TEMPL must
   11142  1.5  christos    be a BFD for an ELF target with the word size and byte order found in
   11143  1.1  christos    the remote memory.  */
   11144  1.3  christos 
   11145  1.1  christos bfd *
   11146  1.1  christos bfd_elf_bfd_from_remote_memory
   11147  1.5  christos   (bfd *templ,
   11148  1.1  christos    bfd_vma ehdr_vma,
   11149  1.1  christos    bfd_size_type size,
   11150  1.1  christos    bfd_vma *loadbasep,
   11151  1.1  christos    int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
   11152  1.1  christos {
   11153  1.1  christos   return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
   11154  1.1  christos     (templ, ehdr_vma, size, loadbasep, target_read_memory);
   11155  1.1  christos }
   11156  1.1  christos 
   11157  1.1  christos long
   11159  1.1  christos _bfd_elf_get_synthetic_symtab (bfd *abfd,
   11160  1.1  christos 			       long symcount ATTRIBUTE_UNUSED,
   11161  1.1  christos 			       asymbol **syms ATTRIBUTE_UNUSED,
   11162  1.1  christos 			       long dynsymcount,
   11163  1.1  christos 			       asymbol **dynsyms,
   11164  1.1  christos 			       asymbol **ret)
   11165  1.1  christos {
   11166  1.1  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11167  1.1  christos   asection *relplt;
   11168  1.1  christos   asymbol *s;
   11169  1.1  christos   const char *relplt_name;
   11170  1.1  christos   bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
   11171  1.1  christos   arelent *p;
   11172  1.1  christos   long count, i, n;
   11173  1.1  christos   size_t size;
   11174  1.1  christos   Elf_Internal_Shdr *hdr;
   11175  1.1  christos   char *names;
   11176  1.1  christos   asection *plt;
   11177  1.1  christos 
   11178  1.1  christos   *ret = NULL;
   11179  1.1  christos 
   11180  1.1  christos   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
   11181  1.1  christos     return 0;
   11182  1.1  christos 
   11183  1.1  christos   if (dynsymcount <= 0)
   11184  1.1  christos     return 0;
   11185  1.1  christos 
   11186  1.1  christos   if (!bed->plt_sym_val)
   11187  1.1  christos     return 0;
   11188  1.1  christos 
   11189  1.1  christos   relplt_name = bed->relplt_name;
   11190  1.1  christos   if (relplt_name == NULL)
   11191  1.1  christos     relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
   11192  1.1  christos   relplt = bfd_get_section_by_name (abfd, relplt_name);
   11193  1.1  christos   if (relplt == NULL)
   11194  1.1  christos     return 0;
   11195  1.1  christos 
   11196  1.1  christos   hdr = &elf_section_data (relplt)->this_hdr;
   11197  1.1  christos   if (hdr->sh_link != elf_dynsymtab (abfd)
   11198  1.1  christos       || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
   11199  1.1  christos     return 0;
   11200  1.1  christos 
   11201  1.1  christos   plt = bfd_get_section_by_name (abfd, ".plt");
   11202  1.1  christos   if (plt == NULL)
   11203  1.1  christos     return 0;
   11204  1.1  christos 
   11205  1.1  christos   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   11206  1.1  christos   if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
   11207  1.1  christos     return -1;
   11208  1.1  christos 
   11209  1.1  christos   count = relplt->size / hdr->sh_entsize;
   11210  1.1  christos   size = count * sizeof (asymbol);
   11211  1.1  christos   p = relplt->relocation;
   11212  1.1  christos   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   11213  1.1  christos     {
   11214  1.1  christos       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
   11215  1.1  christos       if (p->addend != 0)
   11216  1.1  christos 	{
   11217  1.1  christos #ifdef BFD64
   11218  1.1  christos 	  size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
   11219  1.1  christos #else
   11220  1.1  christos 	  size += sizeof ("+0x") - 1 + 8;
   11221  1.1  christos #endif
   11222  1.1  christos 	}
   11223  1.1  christos     }
   11224  1.1  christos 
   11225  1.1  christos   s = *ret = (asymbol *) bfd_malloc (size);
   11226  1.1  christos   if (s == NULL)
   11227  1.1  christos     return -1;
   11228  1.1  christos 
   11229  1.1  christos   names = (char *) (s + count);
   11230  1.1  christos   p = relplt->relocation;
   11231  1.1  christos   n = 0;
   11232  1.1  christos   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   11233  1.1  christos     {
   11234  1.1  christos       size_t len;
   11235  1.1  christos       bfd_vma addr;
   11236  1.1  christos 
   11237  1.1  christos       addr = bed->plt_sym_val (i, plt, p);
   11238  1.1  christos       if (addr == (bfd_vma) -1)
   11239  1.1  christos 	continue;
   11240  1.1  christos 
   11241  1.1  christos       *s = **p->sym_ptr_ptr;
   11242  1.1  christos       /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   11243  1.1  christos 	 we are defining a symbol, ensure one of them is set.  */
   11244  1.1  christos       if ((s->flags & BSF_LOCAL) == 0)
   11245  1.1  christos 	s->flags |= BSF_GLOBAL;
   11246  1.1  christos       s->flags |= BSF_SYNTHETIC;
   11247  1.1  christos       s->section = plt;
   11248  1.1  christos       s->value = addr - plt->vma;
   11249  1.3  christos       s->name = names;
   11250  1.1  christos       s->udata.p = NULL;
   11251  1.1  christos       len = strlen ((*p->sym_ptr_ptr)->name);
   11252  1.1  christos       memcpy (names, (*p->sym_ptr_ptr)->name, len);
   11253  1.1  christos       names += len;
   11254  1.1  christos       if (p->addend != 0)
   11255  1.1  christos 	{
   11256  1.1  christos 	  char buf[30], *a;
   11257  1.1  christos 
   11258  1.1  christos 	  memcpy (names, "+0x", sizeof ("+0x") - 1);
   11259  1.1  christos 	  names += sizeof ("+0x") - 1;
   11260  1.1  christos 	  bfd_sprintf_vma (abfd, buf, p->addend);
   11261  1.1  christos 	  for (a = buf; *a == '0'; ++a)
   11262  1.1  christos 	    ;
   11263  1.1  christos 	  len = strlen (a);
   11264  1.1  christos 	  memcpy (names, a, len);
   11265  1.1  christos 	  names += len;
   11266  1.1  christos 	}
   11267  1.9  christos       memcpy (names, "@plt", sizeof ("@plt"));
   11268  1.9  christos       names += sizeof ("@plt");
   11269  1.9  christos       ++s, ++n;
   11270  1.1  christos     }
   11271  1.9  christos 
   11272  1.9  christos   return n;
   11273  1.1  christos }
   11274  1.1  christos 
   11275  1.4  christos /* It is only used by x86-64 so far.
   11276  1.4  christos    ??? This repeats *COM* id of zero.  sec->id is supposed to be unique,
   11277  1.1  christos    but current usage would allow all of _bfd_std_section to be zero.  t*/
   11278  1.1  christos asection _bfd_elf_large_com_section
   11279  1.1  christos   = BFD_FAKE_SECTION (_bfd_elf_large_com_section, NULL,
   11280  1.1  christos 		      "LARGE_COMMON", 0, SEC_IS_COMMON);
   11281  1.1  christos 
   11282  1.1  christos void
   11283  1.1  christos _bfd_elf_post_process_headers (bfd * abfd,
   11284  1.1  christos 			       struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
   11285  1.3  christos {
   11286  1.3  christos   Elf_Internal_Ehdr * i_ehdrp;	/* ELF file header, internal form.  */
   11287  1.1  christos 
   11288  1.3  christos   i_ehdrp = elf_elfheader (abfd);
   11289  1.3  christos 
   11290  1.1  christos   i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
   11291  1.1  christos 
   11292  1.1  christos   /* To make things simpler for the loader on Linux systems we set the
   11293  1.1  christos      osabi field to ELFOSABI_GNU if the binary contains symbols of
   11294  1.1  christos      the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding.  */
   11295  1.1  christos   if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
   11296  1.1  christos       && elf_tdata (abfd)->has_gnu_symbols)
   11297  1.1  christos     i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
   11298  1.1  christos }
   11299  1.1  christos 
   11300  1.1  christos 
   11301  1.1  christos /* Return TRUE for ELF symbol types that represent functions.
   11302  1.1  christos    This is the default version of this function, which is sufficient for
   11303  1.3  christos    most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
   11304  1.3  christos 
   11305  1.3  christos bfd_boolean
   11306  1.3  christos _bfd_elf_is_function_type (unsigned int type)
   11307  1.3  christos {
   11308  1.3  christos   return (type == STT_FUNC
   11309  1.3  christos 	  || type == STT_GNU_IFUNC);
   11310  1.3  christos }
   11311  1.3  christos 
   11312  1.3  christos /* If the ELF symbol SYM might be a function in SEC, return the
   11313  1.3  christos    function size and set *CODE_OFF to the function's entry point,
   11314  1.3  christos    otherwise return zero.  */
   11315  1.3  christos 
   11316  1.3  christos bfd_size_type
   11317  1.3  christos _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
   11318  1.3  christos 			     bfd_vma *code_off)
   11319  1.3  christos {
   11320  1.3  christos   bfd_size_type size;
   11321  1.3  christos 
   11322  1.3  christos   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
   11323  1.3  christos 		     | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
   11324  1.3  christos       || sym->section != sec)
   11325  1.3  christos     return 0;
   11326  1.3  christos 
   11327                  *code_off = sym->value;
   11328                  size = 0;
   11329                  if (!(sym->flags & BSF_SYNTHETIC))
   11330                    size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
   11331                  if (size == 0)
   11332                    size = 1;
   11333                  return size;
   11334                }
   11335