Home | History | Annotate | Line # | Download | only in bfd
elf.c revision 1.15
      1   1.1     skrll /* ELF executable support for BFD.
      2   1.1     skrll 
      3  1.15  christos    Copyright (C) 1993-2020 Free Software Foundation, Inc.
      4   1.1     skrll 
      5   1.1     skrll    This file is part of BFD, the Binary File Descriptor library.
      6   1.1     skrll 
      7   1.1     skrll    This program is free software; you can redistribute it and/or modify
      8   1.1     skrll    it under the terms of the GNU General Public License as published by
      9   1.1     skrll    the Free Software Foundation; either version 3 of the License, or
     10   1.1     skrll    (at your option) any later version.
     11   1.1     skrll 
     12   1.1     skrll    This program is distributed in the hope that it will be useful,
     13   1.1     skrll    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14   1.1     skrll    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15   1.1     skrll    GNU General Public License for more details.
     16   1.1     skrll 
     17   1.1     skrll    You should have received a copy of the GNU General Public License
     18   1.1     skrll    along with this program; if not, write to the Free Software
     19   1.1     skrll    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     20   1.1     skrll    MA 02110-1301, USA.  */
     21   1.1     skrll 
     22   1.1     skrll 
     23   1.1     skrll /*
     24   1.1     skrll SECTION
     25   1.1     skrll 	ELF backends
     26   1.1     skrll 
     27   1.1     skrll 	BFD support for ELF formats is being worked on.
     28   1.1     skrll 	Currently, the best supported back ends are for sparc and i386
     29   1.1     skrll 	(running svr4 or Solaris 2).
     30   1.1     skrll 
     31   1.1     skrll 	Documentation of the internals of the support code still needs
     32   1.1     skrll 	to be written.  The code is changing quickly enough that we
     33   1.1     skrll 	haven't bothered yet.  */
     34   1.1     skrll 
     35   1.1     skrll /* For sparc64-cross-sparc32.  */
     36   1.1     skrll #define _SYSCALL32
     37   1.1     skrll #include "sysdep.h"
     38  1.15  christos #include <limits.h>
     39   1.1     skrll #include "bfd.h"
     40   1.1     skrll #include "bfdlink.h"
     41   1.1     skrll #include "libbfd.h"
     42   1.1     skrll #define ARCH_SIZE 0
     43   1.1     skrll #include "elf-bfd.h"
     44   1.1     skrll #include "libiberty.h"
     45   1.1     skrll #include "safe-ctype.h"
     46   1.7  christos #include "elf-linux-core.h"
     47   1.1     skrll 
     48   1.3  christos #ifdef CORE_HEADER
     49   1.3  christos #include CORE_HEADER
     50   1.3  christos #endif
     51   1.3  christos 
     52   1.1     skrll static int elf_sort_sections (const void *, const void *);
     53   1.1     skrll static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
     54   1.6  christos static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ;
     55   1.1     skrll static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
     56   1.9  christos 				    file_ptr offset, size_t align);
     57   1.1     skrll 
     58   1.1     skrll /* Swap version information in and out.  The version information is
     59   1.1     skrll    currently size independent.  If that ever changes, this code will
     60   1.1     skrll    need to move into elfcode.h.  */
     61   1.1     skrll 
     62   1.1     skrll /* Swap in a Verdef structure.  */
     63   1.1     skrll 
     64   1.1     skrll void
     65   1.1     skrll _bfd_elf_swap_verdef_in (bfd *abfd,
     66   1.1     skrll 			 const Elf_External_Verdef *src,
     67   1.1     skrll 			 Elf_Internal_Verdef *dst)
     68   1.1     skrll {
     69   1.1     skrll   dst->vd_version = H_GET_16 (abfd, src->vd_version);
     70   1.1     skrll   dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
     71   1.1     skrll   dst->vd_ndx     = H_GET_16 (abfd, src->vd_ndx);
     72   1.1     skrll   dst->vd_cnt     = H_GET_16 (abfd, src->vd_cnt);
     73   1.1     skrll   dst->vd_hash    = H_GET_32 (abfd, src->vd_hash);
     74   1.1     skrll   dst->vd_aux     = H_GET_32 (abfd, src->vd_aux);
     75   1.1     skrll   dst->vd_next    = H_GET_32 (abfd, src->vd_next);
     76   1.1     skrll }
     77   1.1     skrll 
     78   1.1     skrll /* Swap out a Verdef structure.  */
     79   1.1     skrll 
     80   1.1     skrll void
     81   1.1     skrll _bfd_elf_swap_verdef_out (bfd *abfd,
     82   1.1     skrll 			  const Elf_Internal_Verdef *src,
     83   1.1     skrll 			  Elf_External_Verdef *dst)
     84   1.1     skrll {
     85   1.1     skrll   H_PUT_16 (abfd, src->vd_version, dst->vd_version);
     86   1.1     skrll   H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
     87   1.1     skrll   H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
     88   1.1     skrll   H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
     89   1.1     skrll   H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
     90   1.1     skrll   H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
     91   1.1     skrll   H_PUT_32 (abfd, src->vd_next, dst->vd_next);
     92   1.1     skrll }
     93   1.1     skrll 
     94   1.1     skrll /* Swap in a Verdaux structure.  */
     95   1.1     skrll 
     96   1.1     skrll void
     97   1.1     skrll _bfd_elf_swap_verdaux_in (bfd *abfd,
     98   1.1     skrll 			  const Elf_External_Verdaux *src,
     99   1.1     skrll 			  Elf_Internal_Verdaux *dst)
    100   1.1     skrll {
    101   1.1     skrll   dst->vda_name = H_GET_32 (abfd, src->vda_name);
    102   1.1     skrll   dst->vda_next = H_GET_32 (abfd, src->vda_next);
    103   1.1     skrll }
    104   1.1     skrll 
    105   1.1     skrll /* Swap out a Verdaux structure.  */
    106   1.1     skrll 
    107   1.1     skrll void
    108   1.1     skrll _bfd_elf_swap_verdaux_out (bfd *abfd,
    109   1.1     skrll 			   const Elf_Internal_Verdaux *src,
    110   1.1     skrll 			   Elf_External_Verdaux *dst)
    111   1.1     skrll {
    112   1.1     skrll   H_PUT_32 (abfd, src->vda_name, dst->vda_name);
    113   1.1     skrll   H_PUT_32 (abfd, src->vda_next, dst->vda_next);
    114   1.1     skrll }
    115   1.1     skrll 
    116   1.1     skrll /* Swap in a Verneed structure.  */
    117   1.1     skrll 
    118   1.1     skrll void
    119   1.1     skrll _bfd_elf_swap_verneed_in (bfd *abfd,
    120   1.1     skrll 			  const Elf_External_Verneed *src,
    121   1.1     skrll 			  Elf_Internal_Verneed *dst)
    122   1.1     skrll {
    123   1.1     skrll   dst->vn_version = H_GET_16 (abfd, src->vn_version);
    124   1.1     skrll   dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
    125   1.1     skrll   dst->vn_file    = H_GET_32 (abfd, src->vn_file);
    126   1.1     skrll   dst->vn_aux     = H_GET_32 (abfd, src->vn_aux);
    127   1.1     skrll   dst->vn_next    = H_GET_32 (abfd, src->vn_next);
    128   1.1     skrll }
    129   1.1     skrll 
    130   1.1     skrll /* Swap out a Verneed structure.  */
    131   1.1     skrll 
    132   1.1     skrll void
    133   1.1     skrll _bfd_elf_swap_verneed_out (bfd *abfd,
    134   1.1     skrll 			   const Elf_Internal_Verneed *src,
    135   1.1     skrll 			   Elf_External_Verneed *dst)
    136   1.1     skrll {
    137   1.1     skrll   H_PUT_16 (abfd, src->vn_version, dst->vn_version);
    138   1.1     skrll   H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
    139   1.1     skrll   H_PUT_32 (abfd, src->vn_file, dst->vn_file);
    140   1.1     skrll   H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
    141   1.1     skrll   H_PUT_32 (abfd, src->vn_next, dst->vn_next);
    142   1.1     skrll }
    143   1.1     skrll 
    144   1.1     skrll /* Swap in a Vernaux structure.  */
    145   1.1     skrll 
    146   1.1     skrll void
    147   1.1     skrll _bfd_elf_swap_vernaux_in (bfd *abfd,
    148   1.1     skrll 			  const Elf_External_Vernaux *src,
    149   1.1     skrll 			  Elf_Internal_Vernaux *dst)
    150   1.1     skrll {
    151   1.1     skrll   dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
    152   1.1     skrll   dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
    153   1.1     skrll   dst->vna_other = H_GET_16 (abfd, src->vna_other);
    154   1.1     skrll   dst->vna_name  = H_GET_32 (abfd, src->vna_name);
    155   1.1     skrll   dst->vna_next  = H_GET_32 (abfd, src->vna_next);
    156   1.1     skrll }
    157   1.1     skrll 
    158   1.1     skrll /* Swap out a Vernaux structure.  */
    159   1.1     skrll 
    160   1.1     skrll void
    161   1.1     skrll _bfd_elf_swap_vernaux_out (bfd *abfd,
    162   1.1     skrll 			   const Elf_Internal_Vernaux *src,
    163   1.1     skrll 			   Elf_External_Vernaux *dst)
    164   1.1     skrll {
    165   1.1     skrll   H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
    166   1.1     skrll   H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
    167   1.1     skrll   H_PUT_16 (abfd, src->vna_other, dst->vna_other);
    168   1.1     skrll   H_PUT_32 (abfd, src->vna_name, dst->vna_name);
    169   1.1     skrll   H_PUT_32 (abfd, src->vna_next, dst->vna_next);
    170   1.1     skrll }
    171   1.1     skrll 
    172   1.1     skrll /* Swap in a Versym structure.  */
    173   1.1     skrll 
    174   1.1     skrll void
    175   1.1     skrll _bfd_elf_swap_versym_in (bfd *abfd,
    176   1.1     skrll 			 const Elf_External_Versym *src,
    177   1.1     skrll 			 Elf_Internal_Versym *dst)
    178   1.1     skrll {
    179   1.1     skrll   dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
    180   1.1     skrll }
    181   1.1     skrll 
    182   1.1     skrll /* Swap out a Versym structure.  */
    183   1.1     skrll 
    184   1.1     skrll void
    185   1.1     skrll _bfd_elf_swap_versym_out (bfd *abfd,
    186   1.1     skrll 			  const Elf_Internal_Versym *src,
    187   1.1     skrll 			  Elf_External_Versym *dst)
    188   1.1     skrll {
    189   1.1     skrll   H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
    190   1.1     skrll }
    191   1.1     skrll 
    192   1.1     skrll /* Standard ELF hash function.  Do not change this function; you will
    193   1.1     skrll    cause invalid hash tables to be generated.  */
    194   1.1     skrll 
    195   1.1     skrll unsigned long
    196   1.1     skrll bfd_elf_hash (const char *namearg)
    197   1.1     skrll {
    198   1.1     skrll   const unsigned char *name = (const unsigned char *) namearg;
    199   1.1     skrll   unsigned long h = 0;
    200   1.1     skrll   unsigned long g;
    201   1.1     skrll   int ch;
    202   1.1     skrll 
    203   1.1     skrll   while ((ch = *name++) != '\0')
    204   1.1     skrll     {
    205   1.1     skrll       h = (h << 4) + ch;
    206   1.1     skrll       if ((g = (h & 0xf0000000)) != 0)
    207   1.1     skrll 	{
    208   1.1     skrll 	  h ^= g >> 24;
    209   1.1     skrll 	  /* The ELF ABI says `h &= ~g', but this is equivalent in
    210   1.1     skrll 	     this case and on some machines one insn instead of two.  */
    211   1.1     skrll 	  h ^= g;
    212   1.1     skrll 	}
    213   1.1     skrll     }
    214   1.1     skrll   return h & 0xffffffff;
    215   1.1     skrll }
    216   1.1     skrll 
    217   1.1     skrll /* DT_GNU_HASH hash function.  Do not change this function; you will
    218   1.1     skrll    cause invalid hash tables to be generated.  */
    219   1.1     skrll 
    220   1.1     skrll unsigned long
    221   1.1     skrll bfd_elf_gnu_hash (const char *namearg)
    222   1.1     skrll {
    223   1.1     skrll   const unsigned char *name = (const unsigned char *) namearg;
    224   1.1     skrll   unsigned long h = 5381;
    225   1.1     skrll   unsigned char ch;
    226   1.1     skrll 
    227   1.1     skrll   while ((ch = *name++) != '\0')
    228   1.1     skrll     h = (h << 5) + h + ch;
    229   1.1     skrll   return h & 0xffffffff;
    230   1.1     skrll }
    231   1.1     skrll 
    232   1.1     skrll /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
    233   1.1     skrll    the object_id field of an elf_obj_tdata field set to OBJECT_ID.  */
    234   1.1     skrll bfd_boolean
    235   1.1     skrll bfd_elf_allocate_object (bfd *abfd,
    236   1.1     skrll 			 size_t object_size,
    237   1.3  christos 			 enum elf_target_id object_id)
    238   1.1     skrll {
    239   1.1     skrll   BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
    240   1.1     skrll   abfd->tdata.any = bfd_zalloc (abfd, object_size);
    241   1.1     skrll   if (abfd->tdata.any == NULL)
    242   1.1     skrll     return FALSE;
    243   1.1     skrll 
    244   1.1     skrll   elf_object_id (abfd) = object_id;
    245   1.6  christos   if (abfd->direction != read_direction)
    246   1.6  christos     {
    247   1.6  christos       struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
    248   1.6  christos       if (o == NULL)
    249   1.6  christos 	return FALSE;
    250   1.6  christos       elf_tdata (abfd)->o = o;
    251   1.6  christos       elf_program_header_size (abfd) = (bfd_size_type) -1;
    252   1.6  christos     }
    253   1.1     skrll   return TRUE;
    254   1.1     skrll }
    255   1.1     skrll 
    256   1.1     skrll 
    257   1.1     skrll bfd_boolean
    258   1.3  christos bfd_elf_make_object (bfd *abfd)
    259   1.1     skrll {
    260   1.3  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    261   1.1     skrll   return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
    262   1.3  christos 				  bed->target_id);
    263   1.1     skrll }
    264   1.1     skrll 
    265   1.1     skrll bfd_boolean
    266   1.1     skrll bfd_elf_mkcorefile (bfd *abfd)
    267   1.1     skrll {
    268   1.1     skrll   /* I think this can be done just like an object file.  */
    269   1.6  christos   if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
    270   1.6  christos     return FALSE;
    271   1.6  christos   elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
    272   1.6  christos   return elf_tdata (abfd)->core != NULL;
    273   1.1     skrll }
    274   1.1     skrll 
    275  1.15  christos char *
    276   1.1     skrll bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
    277   1.1     skrll {
    278   1.1     skrll   Elf_Internal_Shdr **i_shdrp;
    279   1.1     skrll   bfd_byte *shstrtab = NULL;
    280   1.1     skrll   file_ptr offset;
    281   1.1     skrll   bfd_size_type shstrtabsize;
    282   1.1     skrll 
    283   1.1     skrll   i_shdrp = elf_elfsections (abfd);
    284   1.1     skrll   if (i_shdrp == 0
    285   1.1     skrll       || shindex >= elf_numsections (abfd)
    286   1.1     skrll       || i_shdrp[shindex] == 0)
    287   1.1     skrll     return NULL;
    288   1.1     skrll 
    289   1.1     skrll   shstrtab = i_shdrp[shindex]->contents;
    290   1.1     skrll   if (shstrtab == NULL)
    291   1.1     skrll     {
    292   1.1     skrll       /* No cached one, attempt to read, and cache what we read.  */
    293   1.1     skrll       offset = i_shdrp[shindex]->sh_offset;
    294   1.1     skrll       shstrtabsize = i_shdrp[shindex]->sh_size;
    295   1.1     skrll 
    296   1.1     skrll       /* Allocate and clear an extra byte at the end, to prevent crashes
    297   1.1     skrll 	 in case the string table is not terminated.  */
    298   1.1     skrll       if (shstrtabsize + 1 <= 1
    299  1.14       rin 	  || (bfd_get_file_size (abfd) > 0 /* not a character device */
    300  1.14       rin 		&& shstrtabsize > bfd_get_file_size (abfd))
    301   1.6  christos 	  || bfd_seek (abfd, offset, SEEK_SET) != 0
    302   1.6  christos 	  || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL)
    303   1.1     skrll 	shstrtab = NULL;
    304   1.1     skrll       else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
    305   1.1     skrll 	{
    306   1.1     skrll 	  if (bfd_get_error () != bfd_error_system_call)
    307   1.1     skrll 	    bfd_set_error (bfd_error_file_truncated);
    308   1.6  christos 	  bfd_release (abfd, shstrtab);
    309   1.1     skrll 	  shstrtab = NULL;
    310   1.1     skrll 	  /* Once we've failed to read it, make sure we don't keep
    311   1.1     skrll 	     trying.  Otherwise, we'll keep allocating space for
    312   1.1     skrll 	     the string table over and over.  */
    313   1.1     skrll 	  i_shdrp[shindex]->sh_size = 0;
    314   1.1     skrll 	}
    315   1.1     skrll       else
    316   1.1     skrll 	shstrtab[shstrtabsize] = '\0';
    317   1.1     skrll       i_shdrp[shindex]->contents = shstrtab;
    318   1.1     skrll     }
    319   1.1     skrll   return (char *) shstrtab;
    320   1.1     skrll }
    321   1.1     skrll 
    322   1.1     skrll char *
    323   1.1     skrll bfd_elf_string_from_elf_section (bfd *abfd,
    324   1.1     skrll 				 unsigned int shindex,
    325   1.1     skrll 				 unsigned int strindex)
    326   1.1     skrll {
    327   1.1     skrll   Elf_Internal_Shdr *hdr;
    328   1.1     skrll 
    329   1.1     skrll   if (strindex == 0)
    330   1.1     skrll     return "";
    331   1.1     skrll 
    332   1.1     skrll   if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
    333   1.1     skrll     return NULL;
    334   1.1     skrll 
    335   1.1     skrll   hdr = elf_elfsections (abfd)[shindex];
    336   1.1     skrll 
    337   1.6  christos   if (hdr->contents == NULL)
    338   1.6  christos     {
    339   1.6  christos       if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
    340   1.6  christos 	{
    341   1.6  christos 	  /* PR 17512: file: f057ec89.  */
    342   1.9  christos 	  /* xgettext:c-format */
    343  1.13  christos 	  _bfd_error_handler (_("%pB: attempt to load strings from"
    344   1.9  christos 				" a non-string section (number %d)"),
    345   1.6  christos 			      abfd, shindex);
    346   1.6  christos 	  return NULL;
    347   1.6  christos 	}
    348   1.6  christos 
    349   1.6  christos       if (bfd_elf_get_str_section (abfd, shindex) == NULL)
    350   1.6  christos 	return NULL;
    351   1.6  christos     }
    352  1.15  christos   else
    353  1.15  christos     {
    354  1.15  christos       /* PR 24273: The string section's contents may have already
    355  1.15  christos 	 been loaded elsewhere, eg because a corrupt file has the
    356  1.15  christos 	 string section index in the ELF header pointing at a group
    357  1.15  christos 	 section.  So be paranoid, and test that the last byte of
    358  1.15  christos 	 the section is zero.  */
    359  1.15  christos       if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
    360  1.15  christos 	return NULL;
    361  1.15  christos     }
    362   1.1     skrll 
    363   1.1     skrll   if (strindex >= hdr->sh_size)
    364   1.1     skrll     {
    365   1.1     skrll       unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
    366   1.9  christos       _bfd_error_handler
    367   1.9  christos 	/* xgettext:c-format */
    368  1.13  christos 	(_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
    369  1.13  christos 	 abfd, strindex, (uint64_t) hdr->sh_size,
    370   1.1     skrll 	 (shindex == shstrndx && strindex == hdr->sh_name
    371   1.1     skrll 	  ? ".shstrtab"
    372   1.1     skrll 	  : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
    373   1.3  christos       return NULL;
    374   1.1     skrll     }
    375   1.1     skrll 
    376   1.1     skrll   return ((char *) hdr->contents) + strindex;
    377   1.1     skrll }
    378   1.1     skrll 
    379   1.1     skrll /* Read and convert symbols to internal format.
    380   1.1     skrll    SYMCOUNT specifies the number of symbols to read, starting from
    381   1.1     skrll    symbol SYMOFFSET.  If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
    382   1.1     skrll    are non-NULL, they are used to store the internal symbols, external
    383   1.1     skrll    symbols, and symbol section index extensions, respectively.
    384   1.1     skrll    Returns a pointer to the internal symbol buffer (malloced if necessary)
    385   1.1     skrll    or NULL if there were no symbols or some kind of problem.  */
    386   1.1     skrll 
    387   1.1     skrll Elf_Internal_Sym *
    388   1.1     skrll bfd_elf_get_elf_syms (bfd *ibfd,
    389   1.1     skrll 		      Elf_Internal_Shdr *symtab_hdr,
    390   1.1     skrll 		      size_t symcount,
    391   1.1     skrll 		      size_t symoffset,
    392   1.1     skrll 		      Elf_Internal_Sym *intsym_buf,
    393   1.1     skrll 		      void *extsym_buf,
    394   1.1     skrll 		      Elf_External_Sym_Shndx *extshndx_buf)
    395   1.1     skrll {
    396   1.1     skrll   Elf_Internal_Shdr *shndx_hdr;
    397   1.1     skrll   void *alloc_ext;
    398   1.1     skrll   const bfd_byte *esym;
    399   1.1     skrll   Elf_External_Sym_Shndx *alloc_extshndx;
    400   1.1     skrll   Elf_External_Sym_Shndx *shndx;
    401   1.1     skrll   Elf_Internal_Sym *alloc_intsym;
    402   1.1     skrll   Elf_Internal_Sym *isym;
    403   1.1     skrll   Elf_Internal_Sym *isymend;
    404   1.1     skrll   const struct elf_backend_data *bed;
    405   1.1     skrll   size_t extsym_size;
    406   1.1     skrll   bfd_size_type amt;
    407   1.1     skrll   file_ptr pos;
    408   1.1     skrll 
    409   1.1     skrll   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
    410   1.1     skrll     abort ();
    411   1.1     skrll 
    412   1.1     skrll   if (symcount == 0)
    413   1.1     skrll     return intsym_buf;
    414   1.1     skrll 
    415   1.1     skrll   /* Normal syms might have section extension entries.  */
    416   1.1     skrll   shndx_hdr = NULL;
    417   1.6  christos   if (elf_symtab_shndx_list (ibfd) != NULL)
    418   1.6  christos     {
    419   1.6  christos       elf_section_list * entry;
    420   1.6  christos       Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
    421   1.6  christos 
    422   1.6  christos       /* Find an index section that is linked to this symtab section.  */
    423   1.6  christos       for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
    424   1.7  christos 	{
    425   1.7  christos 	  /* PR 20063.  */
    426   1.7  christos 	  if (entry->hdr.sh_link >= elf_numsections (ibfd))
    427   1.7  christos 	    continue;
    428   1.7  christos 
    429   1.7  christos 	  if (sections[entry->hdr.sh_link] == symtab_hdr)
    430   1.7  christos 	    {
    431   1.7  christos 	      shndx_hdr = & entry->hdr;
    432   1.7  christos 	      break;
    433   1.7  christos 	    };
    434   1.7  christos 	}
    435   1.6  christos 
    436   1.6  christos       if (shndx_hdr == NULL)
    437   1.6  christos 	{
    438   1.6  christos 	  if (symtab_hdr == & elf_symtab_hdr (ibfd))
    439   1.6  christos 	    /* Not really accurate, but this was how the old code used to work.  */
    440   1.6  christos 	    shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
    441   1.6  christos 	  /* Otherwise we do nothing.  The assumption is that
    442   1.6  christos 	     the index table will not be needed.  */
    443   1.6  christos 	}
    444   1.6  christos     }
    445   1.1     skrll 
    446   1.1     skrll   /* Read the symbols.  */
    447   1.1     skrll   alloc_ext = NULL;
    448   1.1     skrll   alloc_extshndx = NULL;
    449   1.1     skrll   alloc_intsym = NULL;
    450   1.1     skrll   bed = get_elf_backend_data (ibfd);
    451   1.1     skrll   extsym_size = bed->s->sizeof_sym;
    452   1.7  christos   amt = (bfd_size_type) symcount * extsym_size;
    453   1.1     skrll   pos = symtab_hdr->sh_offset + symoffset * extsym_size;
    454   1.1     skrll   if (extsym_buf == NULL)
    455   1.1     skrll     {
    456   1.1     skrll       alloc_ext = bfd_malloc2 (symcount, extsym_size);
    457   1.1     skrll       extsym_buf = alloc_ext;
    458   1.1     skrll     }
    459   1.1     skrll   if (extsym_buf == NULL
    460   1.1     skrll       || bfd_seek (ibfd, pos, SEEK_SET) != 0
    461   1.1     skrll       || bfd_bread (extsym_buf, amt, ibfd) != amt)
    462   1.1     skrll     {
    463   1.1     skrll       intsym_buf = NULL;
    464   1.1     skrll       goto out;
    465   1.1     skrll     }
    466   1.1     skrll 
    467   1.1     skrll   if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
    468   1.1     skrll     extshndx_buf = NULL;
    469   1.1     skrll   else
    470   1.1     skrll     {
    471   1.7  christos       amt = (bfd_size_type) symcount * sizeof (Elf_External_Sym_Shndx);
    472   1.1     skrll       pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
    473   1.1     skrll       if (extshndx_buf == NULL)
    474   1.1     skrll 	{
    475   1.3  christos 	  alloc_extshndx = (Elf_External_Sym_Shndx *)
    476   1.9  christos 	      bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx));
    477   1.1     skrll 	  extshndx_buf = alloc_extshndx;
    478   1.1     skrll 	}
    479   1.1     skrll       if (extshndx_buf == NULL
    480   1.1     skrll 	  || bfd_seek (ibfd, pos, SEEK_SET) != 0
    481   1.1     skrll 	  || bfd_bread (extshndx_buf, amt, ibfd) != amt)
    482   1.1     skrll 	{
    483   1.1     skrll 	  intsym_buf = NULL;
    484   1.1     skrll 	  goto out;
    485   1.1     skrll 	}
    486   1.1     skrll     }
    487   1.1     skrll 
    488   1.1     skrll   if (intsym_buf == NULL)
    489   1.1     skrll     {
    490   1.3  christos       alloc_intsym = (Elf_Internal_Sym *)
    491   1.9  christos 	  bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
    492   1.1     skrll       intsym_buf = alloc_intsym;
    493   1.1     skrll       if (intsym_buf == NULL)
    494   1.1     skrll 	goto out;
    495   1.1     skrll     }
    496   1.1     skrll 
    497   1.1     skrll   /* Convert the symbols to internal form.  */
    498   1.1     skrll   isymend = intsym_buf + symcount;
    499   1.3  christos   for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
    500   1.9  christos 	   shndx = extshndx_buf;
    501   1.1     skrll        isym < isymend;
    502   1.1     skrll        esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
    503   1.1     skrll     if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
    504   1.1     skrll       {
    505   1.1     skrll 	symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
    506   1.9  christos 	/* xgettext:c-format */
    507  1.13  christos 	_bfd_error_handler (_("%pB symbol number %lu references"
    508   1.9  christos 			      " nonexistent SHT_SYMTAB_SHNDX section"),
    509   1.9  christos 			    ibfd, (unsigned long) symoffset);
    510   1.1     skrll 	if (alloc_intsym != NULL)
    511   1.1     skrll 	  free (alloc_intsym);
    512   1.1     skrll 	intsym_buf = NULL;
    513   1.1     skrll 	goto out;
    514   1.1     skrll       }
    515   1.1     skrll 
    516   1.1     skrll  out:
    517   1.1     skrll   if (alloc_ext != NULL)
    518   1.1     skrll     free (alloc_ext);
    519   1.1     skrll   if (alloc_extshndx != NULL)
    520   1.1     skrll     free (alloc_extshndx);
    521   1.1     skrll 
    522   1.1     skrll   return intsym_buf;
    523   1.1     skrll }
    524   1.1     skrll 
    525   1.1     skrll /* Look up a symbol name.  */
    526   1.1     skrll const char *
    527   1.1     skrll bfd_elf_sym_name (bfd *abfd,
    528   1.1     skrll 		  Elf_Internal_Shdr *symtab_hdr,
    529   1.1     skrll 		  Elf_Internal_Sym *isym,
    530   1.1     skrll 		  asection *sym_sec)
    531   1.1     skrll {
    532   1.1     skrll   const char *name;
    533   1.1     skrll   unsigned int iname = isym->st_name;
    534   1.1     skrll   unsigned int shindex = symtab_hdr->sh_link;
    535   1.1     skrll 
    536   1.1     skrll   if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
    537   1.1     skrll       /* Check for a bogus st_shndx to avoid crashing.  */
    538   1.1     skrll       && isym->st_shndx < elf_numsections (abfd))
    539   1.1     skrll     {
    540   1.1     skrll       iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
    541   1.1     skrll       shindex = elf_elfheader (abfd)->e_shstrndx;
    542   1.1     skrll     }
    543   1.1     skrll 
    544   1.1     skrll   name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
    545   1.1     skrll   if (name == NULL)
    546   1.1     skrll     name = "(null)";
    547   1.1     skrll   else if (sym_sec && *name == '\0')
    548  1.15  christos     name = bfd_section_name (sym_sec);
    549   1.1     skrll 
    550   1.1     skrll   return name;
    551   1.1     skrll }
    552   1.1     skrll 
    553   1.1     skrll /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
    554   1.1     skrll    sections.  The first element is the flags, the rest are section
    555   1.1     skrll    pointers.  */
    556   1.1     skrll 
    557   1.1     skrll typedef union elf_internal_group {
    558   1.1     skrll   Elf_Internal_Shdr *shdr;
    559   1.1     skrll   unsigned int flags;
    560   1.1     skrll } Elf_Internal_Group;
    561   1.1     skrll 
    562   1.1     skrll /* Return the name of the group signature symbol.  Why isn't the
    563   1.1     skrll    signature just a string?  */
    564   1.1     skrll 
    565   1.1     skrll static const char *
    566   1.1     skrll group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
    567   1.1     skrll {
    568   1.1     skrll   Elf_Internal_Shdr *hdr;
    569   1.1     skrll   unsigned char esym[sizeof (Elf64_External_Sym)];
    570   1.1     skrll   Elf_External_Sym_Shndx eshndx;
    571   1.1     skrll   Elf_Internal_Sym isym;
    572   1.1     skrll 
    573   1.1     skrll   /* First we need to ensure the symbol table is available.  Make sure
    574   1.1     skrll      that it is a symbol table section.  */
    575   1.1     skrll   if (ghdr->sh_link >= elf_numsections (abfd))
    576   1.1     skrll     return NULL;
    577   1.1     skrll   hdr = elf_elfsections (abfd) [ghdr->sh_link];
    578   1.1     skrll   if (hdr->sh_type != SHT_SYMTAB
    579   1.1     skrll       || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
    580   1.1     skrll     return NULL;
    581   1.1     skrll 
    582   1.1     skrll   /* Go read the symbol.  */
    583   1.1     skrll   hdr = &elf_tdata (abfd)->symtab_hdr;
    584   1.1     skrll   if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
    585   1.1     skrll 			    &isym, esym, &eshndx) == NULL)
    586   1.1     skrll     return NULL;
    587   1.1     skrll 
    588   1.1     skrll   return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
    589   1.1     skrll }
    590   1.1     skrll 
    591   1.1     skrll /* Set next_in_group list pointer, and group name for NEWSECT.  */
    592   1.1     skrll 
    593   1.1     skrll static bfd_boolean
    594   1.1     skrll setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
    595   1.1     skrll {
    596   1.1     skrll   unsigned int num_group = elf_tdata (abfd)->num_group;
    597   1.1     skrll 
    598   1.1     skrll   /* If num_group is zero, read in all SHT_GROUP sections.  The count
    599   1.1     skrll      is set to -1 if there are no SHT_GROUP sections.  */
    600   1.1     skrll   if (num_group == 0)
    601   1.1     skrll     {
    602   1.1     skrll       unsigned int i, shnum;
    603   1.1     skrll 
    604   1.1     skrll       /* First count the number of groups.  If we have a SHT_GROUP
    605   1.1     skrll 	 section with just a flag word (ie. sh_size is 4), ignore it.  */
    606   1.1     skrll       shnum = elf_numsections (abfd);
    607   1.1     skrll       num_group = 0;
    608   1.1     skrll 
    609   1.6  christos #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize)	\
    610   1.1     skrll 	(   (shdr)->sh_type == SHT_GROUP		\
    611   1.6  christos 	 && (shdr)->sh_size >= minsize			\
    612   1.1     skrll 	 && (shdr)->sh_entsize == GRP_ENTRY_SIZE	\
    613   1.1     skrll 	 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
    614   1.1     skrll 
    615   1.1     skrll       for (i = 0; i < shnum; i++)
    616   1.1     skrll 	{
    617   1.1     skrll 	  Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
    618   1.1     skrll 
    619   1.6  christos 	  if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
    620   1.1     skrll 	    num_group += 1;
    621   1.1     skrll 	}
    622   1.1     skrll 
    623   1.1     skrll       if (num_group == 0)
    624   1.1     skrll 	{
    625   1.1     skrll 	  num_group = (unsigned) -1;
    626   1.1     skrll 	  elf_tdata (abfd)->num_group = num_group;
    627   1.9  christos 	  elf_tdata (abfd)->group_sect_ptr = NULL;
    628   1.1     skrll 	}
    629   1.1     skrll       else
    630   1.1     skrll 	{
    631   1.1     skrll 	  /* We keep a list of elf section headers for group sections,
    632   1.1     skrll 	     so we can find them quickly.  */
    633   1.1     skrll 	  bfd_size_type amt;
    634   1.1     skrll 
    635   1.1     skrll 	  elf_tdata (abfd)->num_group = num_group;
    636   1.3  christos 	  elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
    637   1.9  christos 	      bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
    638   1.1     skrll 	  if (elf_tdata (abfd)->group_sect_ptr == NULL)
    639   1.1     skrll 	    return FALSE;
    640  1.13  christos 	  memset (elf_tdata (abfd)->group_sect_ptr, 0,
    641  1.13  christos 		  num_group * sizeof (Elf_Internal_Shdr *));
    642   1.9  christos 	  num_group = 0;
    643   1.1     skrll 
    644   1.1     skrll 	  for (i = 0; i < shnum; i++)
    645   1.1     skrll 	    {
    646   1.1     skrll 	      Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
    647   1.1     skrll 
    648   1.6  christos 	      if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
    649   1.1     skrll 		{
    650   1.1     skrll 		  unsigned char *src;
    651   1.1     skrll 		  Elf_Internal_Group *dest;
    652   1.1     skrll 
    653   1.9  christos 		  /* Make sure the group section has a BFD section
    654   1.9  christos 		     attached to it.  */
    655   1.9  christos 		  if (!bfd_section_from_shdr (abfd, i))
    656   1.9  christos 		    return FALSE;
    657   1.9  christos 
    658   1.1     skrll 		  /* Add to list of sections.  */
    659   1.1     skrll 		  elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
    660   1.1     skrll 		  num_group += 1;
    661   1.1     skrll 
    662   1.1     skrll 		  /* Read the raw contents.  */
    663   1.1     skrll 		  BFD_ASSERT (sizeof (*dest) >= 4);
    664   1.1     skrll 		  amt = shdr->sh_size * sizeof (*dest) / 4;
    665   1.3  christos 		  shdr->contents = (unsigned char *)
    666  1.15  christos 		    bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
    667   1.1     skrll 		  /* PR binutils/4110: Handle corrupt group headers.  */
    668   1.1     skrll 		  if (shdr->contents == NULL)
    669   1.1     skrll 		    {
    670   1.1     skrll 		      _bfd_error_handler
    671   1.9  christos 			/* xgettext:c-format */
    672  1.13  christos 			(_("%pB: corrupt size field in group section"
    673  1.13  christos 			   " header: %#" PRIx64),
    674  1.13  christos 			 abfd, (uint64_t) shdr->sh_size);
    675   1.1     skrll 		      bfd_set_error (bfd_error_bad_value);
    676   1.6  christos 		      -- num_group;
    677   1.6  christos 		      continue;
    678   1.1     skrll 		    }
    679   1.1     skrll 
    680   1.1     skrll 		  memset (shdr->contents, 0, amt);
    681   1.1     skrll 
    682   1.1     skrll 		  if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
    683   1.1     skrll 		      || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
    684   1.1     skrll 			  != shdr->sh_size))
    685   1.6  christos 		    {
    686   1.6  christos 		      _bfd_error_handler
    687   1.9  christos 			/* xgettext:c-format */
    688  1.13  christos 			(_("%pB: invalid size field in group section"
    689  1.13  christos 			   " header: %#" PRIx64 ""),
    690  1.13  christos 			 abfd, (uint64_t) shdr->sh_size);
    691   1.6  christos 		      bfd_set_error (bfd_error_bad_value);
    692   1.6  christos 		      -- num_group;
    693   1.9  christos 		      /* PR 17510: If the group contents are even
    694   1.9  christos 			 partially corrupt, do not allow any of the
    695   1.9  christos 			 contents to be used.  */
    696   1.6  christos 		      memset (shdr->contents, 0, amt);
    697   1.6  christos 		      continue;
    698   1.6  christos 		    }
    699   1.1     skrll 
    700   1.1     skrll 		  /* Translate raw contents, a flag word followed by an
    701   1.1     skrll 		     array of elf section indices all in target byte order,
    702   1.1     skrll 		     to the flag word followed by an array of elf section
    703   1.1     skrll 		     pointers.  */
    704   1.1     skrll 		  src = shdr->contents + shdr->sh_size;
    705   1.1     skrll 		  dest = (Elf_Internal_Group *) (shdr->contents + amt);
    706   1.6  christos 
    707   1.1     skrll 		  while (1)
    708   1.1     skrll 		    {
    709   1.1     skrll 		      unsigned int idx;
    710   1.1     skrll 
    711   1.1     skrll 		      src -= 4;
    712   1.1     skrll 		      --dest;
    713   1.1     skrll 		      idx = H_GET_32 (abfd, src);
    714   1.1     skrll 		      if (src == shdr->contents)
    715   1.1     skrll 			{
    716   1.1     skrll 			  dest->flags = idx;
    717   1.1     skrll 			  if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
    718   1.1     skrll 			    shdr->bfd_section->flags
    719   1.1     skrll 			      |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
    720   1.1     skrll 			  break;
    721   1.1     skrll 			}
    722  1.13  christos 		      if (idx < shnum)
    723  1.13  christos 			{
    724  1.13  christos 			  dest->shdr = elf_elfsections (abfd)[idx];
    725  1.13  christos 			  /* PR binutils/23199: All sections in a
    726  1.13  christos 			     section group should be marked with
    727  1.13  christos 			     SHF_GROUP.  But some tools generate
    728  1.13  christos 			     broken objects without SHF_GROUP.  Fix
    729  1.13  christos 			     them up here.  */
    730  1.13  christos 			  dest->shdr->sh_flags |= SHF_GROUP;
    731  1.13  christos 			}
    732  1.13  christos 		      if (idx >= shnum
    733  1.13  christos 			  || dest->shdr->sh_type == SHT_GROUP)
    734   1.1     skrll 			{
    735   1.9  christos 			  _bfd_error_handler
    736  1.13  christos 			    (_("%pB: invalid entry in SHT_GROUP section [%u]"),
    737  1.13  christos 			       abfd, i);
    738  1.13  christos 			  dest->shdr = NULL;
    739   1.1     skrll 			}
    740   1.1     skrll 		    }
    741   1.1     skrll 		}
    742   1.1     skrll 	    }
    743   1.6  christos 
    744   1.6  christos 	  /* PR 17510: Corrupt binaries might contain invalid groups.  */
    745   1.6  christos 	  if (num_group != (unsigned) elf_tdata (abfd)->num_group)
    746   1.6  christos 	    {
    747   1.6  christos 	      elf_tdata (abfd)->num_group = num_group;
    748   1.6  christos 
    749   1.6  christos 	      /* If all groups are invalid then fail.  */
    750   1.6  christos 	      if (num_group == 0)
    751   1.6  christos 		{
    752   1.6  christos 		  elf_tdata (abfd)->group_sect_ptr = NULL;
    753   1.6  christos 		  elf_tdata (abfd)->num_group = num_group = -1;
    754   1.9  christos 		  _bfd_error_handler
    755  1.13  christos 		    (_("%pB: no valid group sections found"), abfd);
    756   1.6  christos 		  bfd_set_error (bfd_error_bad_value);
    757   1.6  christos 		}
    758   1.6  christos 	    }
    759   1.1     skrll 	}
    760   1.1     skrll     }
    761   1.1     skrll 
    762   1.1     skrll   if (num_group != (unsigned) -1)
    763   1.1     skrll     {
    764   1.9  christos       unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
    765   1.9  christos       unsigned int j;
    766   1.1     skrll 
    767   1.9  christos       for (j = 0; j < num_group; j++)
    768   1.1     skrll 	{
    769   1.9  christos 	  /* Begin search from previous found group.  */
    770   1.9  christos 	  unsigned i = (j + search_offset) % num_group;
    771   1.9  christos 
    772   1.1     skrll 	  Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
    773   1.9  christos 	  Elf_Internal_Group *idx;
    774   1.9  christos 	  bfd_size_type n_elt;
    775   1.9  christos 
    776   1.9  christos 	  if (shdr == NULL)
    777   1.9  christos 	    continue;
    778   1.9  christos 
    779   1.9  christos 	  idx = (Elf_Internal_Group *) shdr->contents;
    780   1.9  christos 	  if (idx == NULL || shdr->sh_size < 4)
    781   1.9  christos 	    {
    782   1.9  christos 	      /* See PR 21957 for a reproducer.  */
    783   1.9  christos 	      /* xgettext:c-format */
    784  1.13  christos 	      _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
    785   1.9  christos 				  abfd, shdr->bfd_section);
    786   1.9  christos 	      elf_tdata (abfd)->group_sect_ptr[i] = NULL;
    787   1.9  christos 	      bfd_set_error (bfd_error_bad_value);
    788   1.9  christos 	      return FALSE;
    789   1.9  christos 	    }
    790   1.9  christos 	  n_elt = shdr->sh_size / 4;
    791   1.1     skrll 
    792   1.1     skrll 	  /* Look through this group's sections to see if current
    793   1.1     skrll 	     section is a member.  */
    794   1.1     skrll 	  while (--n_elt != 0)
    795   1.1     skrll 	    if ((++idx)->shdr == hdr)
    796   1.1     skrll 	      {
    797   1.1     skrll 		asection *s = NULL;
    798   1.1     skrll 
    799   1.1     skrll 		/* We are a member of this group.  Go looking through
    800   1.1     skrll 		   other members to see if any others are linked via
    801   1.1     skrll 		   next_in_group.  */
    802   1.1     skrll 		idx = (Elf_Internal_Group *) shdr->contents;
    803   1.1     skrll 		n_elt = shdr->sh_size / 4;
    804   1.1     skrll 		while (--n_elt != 0)
    805  1.13  christos 		  if ((++idx)->shdr != NULL
    806  1.13  christos 		      && (s = idx->shdr->bfd_section) != NULL
    807   1.1     skrll 		      && elf_next_in_group (s) != NULL)
    808   1.1     skrll 		    break;
    809   1.1     skrll 		if (n_elt != 0)
    810   1.1     skrll 		  {
    811   1.1     skrll 		    /* Snarf the group name from other member, and
    812   1.1     skrll 		       insert current section in circular list.  */
    813   1.1     skrll 		    elf_group_name (newsect) = elf_group_name (s);
    814   1.1     skrll 		    elf_next_in_group (newsect) = elf_next_in_group (s);
    815   1.1     skrll 		    elf_next_in_group (s) = newsect;
    816   1.1     skrll 		  }
    817   1.1     skrll 		else
    818   1.1     skrll 		  {
    819   1.1     skrll 		    const char *gname;
    820   1.1     skrll 
    821   1.1     skrll 		    gname = group_signature (abfd, shdr);
    822   1.1     skrll 		    if (gname == NULL)
    823   1.1     skrll 		      return FALSE;
    824   1.1     skrll 		    elf_group_name (newsect) = gname;
    825   1.1     skrll 
    826   1.1     skrll 		    /* Start a circular list with one element.  */
    827   1.1     skrll 		    elf_next_in_group (newsect) = newsect;
    828   1.1     skrll 		  }
    829   1.1     skrll 
    830   1.1     skrll 		/* If the group section has been created, point to the
    831   1.1     skrll 		   new member.  */
    832   1.1     skrll 		if (shdr->bfd_section != NULL)
    833   1.1     skrll 		  elf_next_in_group (shdr->bfd_section) = newsect;
    834   1.1     skrll 
    835   1.9  christos 		elf_tdata (abfd)->group_search_offset = i;
    836   1.9  christos 		j = num_group - 1;
    837   1.1     skrll 		break;
    838   1.1     skrll 	      }
    839   1.1     skrll 	}
    840   1.1     skrll     }
    841   1.1     skrll 
    842   1.1     skrll   if (elf_group_name (newsect) == NULL)
    843   1.1     skrll     {
    844   1.9  christos       /* xgettext:c-format */
    845  1.13  christos       _bfd_error_handler (_("%pB: no group info for section '%pA'"),
    846   1.9  christos 			  abfd, newsect);
    847   1.6  christos       return FALSE;
    848   1.1     skrll     }
    849   1.1     skrll   return TRUE;
    850   1.1     skrll }
    851   1.1     skrll 
    852   1.1     skrll bfd_boolean
    853   1.1     skrll _bfd_elf_setup_sections (bfd *abfd)
    854   1.1     skrll {
    855   1.1     skrll   unsigned int i;
    856   1.1     skrll   unsigned int num_group = elf_tdata (abfd)->num_group;
    857   1.1     skrll   bfd_boolean result = TRUE;
    858   1.1     skrll   asection *s;
    859   1.1     skrll 
    860   1.1     skrll   /* Process SHF_LINK_ORDER.  */
    861   1.1     skrll   for (s = abfd->sections; s != NULL; s = s->next)
    862   1.1     skrll     {
    863   1.1     skrll       Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
    864   1.1     skrll       if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
    865   1.1     skrll 	{
    866   1.1     skrll 	  unsigned int elfsec = this_hdr->sh_link;
    867   1.1     skrll 	  /* FIXME: The old Intel compiler and old strip/objcopy may
    868   1.1     skrll 	     not set the sh_link or sh_info fields.  Hence we could
    869   1.1     skrll 	     get the situation where elfsec is 0.  */
    870   1.1     skrll 	  if (elfsec == 0)
    871   1.1     skrll 	    {
    872   1.1     skrll 	      const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    873   1.1     skrll 	      if (bed->link_order_error_handler)
    874   1.1     skrll 		bed->link_order_error_handler
    875   1.9  christos 		  /* xgettext:c-format */
    876  1.13  christos 		  (_("%pB: warning: sh_link not set for section `%pA'"),
    877   1.1     skrll 		   abfd, s);
    878   1.1     skrll 	    }
    879   1.1     skrll 	  else
    880   1.1     skrll 	    {
    881   1.3  christos 	      asection *linksec = NULL;
    882   1.1     skrll 
    883   1.1     skrll 	      if (elfsec < elf_numsections (abfd))
    884   1.1     skrll 		{
    885   1.1     skrll 		  this_hdr = elf_elfsections (abfd)[elfsec];
    886   1.3  christos 		  linksec = this_hdr->bfd_section;
    887   1.1     skrll 		}
    888   1.1     skrll 
    889   1.1     skrll 	      /* PR 1991, 2008:
    890   1.1     skrll 		 Some strip/objcopy may leave an incorrect value in
    891   1.1     skrll 		 sh_link.  We don't want to proceed.  */
    892   1.3  christos 	      if (linksec == NULL)
    893   1.1     skrll 		{
    894   1.9  christos 		  _bfd_error_handler
    895   1.9  christos 		    /* xgettext:c-format */
    896  1.13  christos 		    (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
    897   1.9  christos 		     s->owner, elfsec, s);
    898   1.1     skrll 		  result = FALSE;
    899   1.1     skrll 		}
    900   1.1     skrll 
    901   1.3  christos 	      elf_linked_to_section (s) = linksec;
    902   1.1     skrll 	    }
    903   1.1     skrll 	}
    904   1.9  christos       else if (this_hdr->sh_type == SHT_GROUP
    905   1.9  christos 	       && elf_next_in_group (s) == NULL)
    906   1.9  christos 	{
    907   1.9  christos 	  _bfd_error_handler
    908   1.9  christos 	    /* xgettext:c-format */
    909  1.13  christos 	    (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
    910   1.9  christos 	     abfd, elf_section_data (s)->this_idx);
    911   1.9  christos 	  result = FALSE;
    912   1.9  christos 	}
    913   1.1     skrll     }
    914   1.1     skrll 
    915   1.1     skrll   /* Process section groups.  */
    916   1.1     skrll   if (num_group == (unsigned) -1)
    917   1.1     skrll     return result;
    918   1.1     skrll 
    919   1.1     skrll   for (i = 0; i < num_group; i++)
    920   1.1     skrll     {
    921   1.1     skrll       Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
    922   1.6  christos       Elf_Internal_Group *idx;
    923   1.6  christos       unsigned int n_elt;
    924   1.6  christos 
    925   1.6  christos       /* PR binutils/18758: Beware of corrupt binaries with invalid group data.  */
    926   1.6  christos       if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
    927   1.6  christos 	{
    928   1.9  christos 	  _bfd_error_handler
    929   1.9  christos 	    /* xgettext:c-format */
    930  1.13  christos 	    (_("%pB: section group entry number %u is corrupt"),
    931   1.6  christos 	     abfd, i);
    932   1.6  christos 	  result = FALSE;
    933   1.6  christos 	  continue;
    934   1.6  christos 	}
    935   1.6  christos 
    936   1.6  christos       idx = (Elf_Internal_Group *) shdr->contents;
    937   1.6  christos       n_elt = shdr->sh_size / 4;
    938   1.1     skrll 
    939   1.1     skrll       while (--n_elt != 0)
    940   1.9  christos 	{
    941   1.9  christos 	  ++ idx;
    942   1.9  christos 
    943   1.9  christos 	  if (idx->shdr == NULL)
    944   1.9  christos 	    continue;
    945   1.9  christos 	  else if (idx->shdr->bfd_section)
    946   1.9  christos 	    elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
    947   1.9  christos 	  else if (idx->shdr->sh_type != SHT_RELA
    948   1.9  christos 		   && idx->shdr->sh_type != SHT_REL)
    949   1.9  christos 	    {
    950   1.9  christos 	      /* There are some unknown sections in the group.  */
    951   1.9  christos 	      _bfd_error_handler
    952   1.9  christos 		/* xgettext:c-format */
    953  1.13  christos 		(_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
    954   1.9  christos 		 abfd,
    955   1.9  christos 		 idx->shdr->sh_type,
    956   1.9  christos 		 bfd_elf_string_from_elf_section (abfd,
    957   1.9  christos 						  (elf_elfheader (abfd)
    958   1.9  christos 						   ->e_shstrndx),
    959   1.9  christos 						  idx->shdr->sh_name),
    960   1.9  christos 		 shdr->bfd_section);
    961   1.9  christos 	      result = FALSE;
    962   1.9  christos 	    }
    963   1.9  christos 	}
    964   1.1     skrll     }
    965   1.9  christos 
    966   1.1     skrll   return result;
    967   1.1     skrll }
    968   1.1     skrll 
    969   1.1     skrll bfd_boolean
    970   1.1     skrll bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
    971   1.1     skrll {
    972   1.1     skrll   return elf_next_in_group (sec) != NULL;
    973   1.1     skrll }
    974   1.1     skrll 
    975  1.15  christos const char *
    976  1.15  christos bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
    977  1.15  christos {
    978  1.15  christos   if (elf_sec_group (sec) != NULL)
    979  1.15  christos     return elf_group_name (sec);
    980  1.15  christos   return NULL;
    981  1.15  christos }
    982  1.15  christos 
    983   1.6  christos static char *
    984   1.6  christos convert_debug_to_zdebug (bfd *abfd, const char *name)
    985   1.6  christos {
    986   1.6  christos   unsigned int len = strlen (name);
    987   1.6  christos   char *new_name = bfd_alloc (abfd, len + 2);
    988   1.6  christos   if (new_name == NULL)
    989   1.6  christos     return NULL;
    990   1.6  christos   new_name[0] = '.';
    991   1.6  christos   new_name[1] = 'z';
    992   1.6  christos   memcpy (new_name + 2, name + 1, len);
    993   1.6  christos   return new_name;
    994   1.6  christos }
    995   1.6  christos 
    996   1.6  christos static char *
    997   1.6  christos convert_zdebug_to_debug (bfd *abfd, const char *name)
    998   1.6  christos {
    999   1.6  christos   unsigned int len = strlen (name);
   1000   1.6  christos   char *new_name = bfd_alloc (abfd, len);
   1001   1.6  christos   if (new_name == NULL)
   1002   1.6  christos     return NULL;
   1003   1.6  christos   new_name[0] = '.';
   1004   1.6  christos   memcpy (new_name + 1, name + 2, len - 1);
   1005   1.6  christos   return new_name;
   1006   1.6  christos }
   1007   1.6  christos 
   1008  1.15  christos /* This a copy of lto_section defined in GCC (lto-streamer.h).  */
   1009  1.15  christos 
   1010  1.15  christos struct lto_section
   1011  1.15  christos {
   1012  1.15  christos   int16_t major_version;
   1013  1.15  christos   int16_t minor_version;
   1014  1.15  christos   unsigned char slim_object;
   1015  1.15  christos 
   1016  1.15  christos   /* Flags is a private field that is not defined publicly.  */
   1017  1.15  christos   uint16_t flags;
   1018  1.15  christos };
   1019  1.15  christos 
   1020   1.1     skrll /* Make a BFD section from an ELF section.  We store a pointer to the
   1021   1.1     skrll    BFD section in the bfd_section field of the header.  */
   1022   1.1     skrll 
   1023   1.1     skrll bfd_boolean
   1024   1.1     skrll _bfd_elf_make_section_from_shdr (bfd *abfd,
   1025   1.1     skrll 				 Elf_Internal_Shdr *hdr,
   1026   1.1     skrll 				 const char *name,
   1027   1.1     skrll 				 int shindex)
   1028   1.1     skrll {
   1029   1.1     skrll   asection *newsect;
   1030   1.1     skrll   flagword flags;
   1031   1.1     skrll   const struct elf_backend_data *bed;
   1032   1.1     skrll 
   1033   1.1     skrll   if (hdr->bfd_section != NULL)
   1034   1.3  christos     return TRUE;
   1035   1.1     skrll 
   1036   1.1     skrll   newsect = bfd_make_section_anyway (abfd, name);
   1037   1.1     skrll   if (newsect == NULL)
   1038   1.1     skrll     return FALSE;
   1039   1.1     skrll 
   1040   1.1     skrll   hdr->bfd_section = newsect;
   1041   1.1     skrll   elf_section_data (newsect)->this_hdr = *hdr;
   1042   1.1     skrll   elf_section_data (newsect)->this_idx = shindex;
   1043   1.1     skrll 
   1044   1.1     skrll   /* Always use the real type/flags.  */
   1045   1.1     skrll   elf_section_type (newsect) = hdr->sh_type;
   1046   1.1     skrll   elf_section_flags (newsect) = hdr->sh_flags;
   1047   1.1     skrll 
   1048   1.1     skrll   newsect->filepos = hdr->sh_offset;
   1049   1.1     skrll 
   1050  1.15  christos   if (!bfd_set_section_vma (newsect, hdr->sh_addr)
   1051  1.15  christos       || !bfd_set_section_size (newsect, hdr->sh_size)
   1052  1.15  christos       || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign)))
   1053   1.1     skrll     return FALSE;
   1054   1.1     skrll 
   1055   1.1     skrll   flags = SEC_NO_FLAGS;
   1056   1.1     skrll   if (hdr->sh_type != SHT_NOBITS)
   1057   1.1     skrll     flags |= SEC_HAS_CONTENTS;
   1058   1.1     skrll   if (hdr->sh_type == SHT_GROUP)
   1059   1.9  christos     flags |= SEC_GROUP;
   1060   1.1     skrll   if ((hdr->sh_flags & SHF_ALLOC) != 0)
   1061   1.1     skrll     {
   1062   1.1     skrll       flags |= SEC_ALLOC;
   1063   1.1     skrll       if (hdr->sh_type != SHT_NOBITS)
   1064   1.1     skrll 	flags |= SEC_LOAD;
   1065   1.1     skrll     }
   1066   1.1     skrll   if ((hdr->sh_flags & SHF_WRITE) == 0)
   1067   1.1     skrll     flags |= SEC_READONLY;
   1068   1.1     skrll   if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
   1069   1.1     skrll     flags |= SEC_CODE;
   1070   1.1     skrll   else if ((flags & SEC_LOAD) != 0)
   1071   1.1     skrll     flags |= SEC_DATA;
   1072   1.1     skrll   if ((hdr->sh_flags & SHF_MERGE) != 0)
   1073   1.1     skrll     {
   1074   1.1     skrll       flags |= SEC_MERGE;
   1075   1.1     skrll       newsect->entsize = hdr->sh_entsize;
   1076   1.1     skrll     }
   1077   1.7  christos   if ((hdr->sh_flags & SHF_STRINGS) != 0)
   1078   1.7  christos     flags |= SEC_STRINGS;
   1079   1.1     skrll   if (hdr->sh_flags & SHF_GROUP)
   1080   1.1     skrll     if (!setup_group (abfd, hdr, newsect))
   1081   1.1     skrll       return FALSE;
   1082   1.1     skrll   if ((hdr->sh_flags & SHF_TLS) != 0)
   1083   1.1     skrll     flags |= SEC_THREAD_LOCAL;
   1084   1.3  christos   if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
   1085   1.3  christos     flags |= SEC_EXCLUDE;
   1086   1.1     skrll 
   1087  1.15  christos   switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
   1088  1.15  christos     {
   1089  1.15  christos       /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
   1090  1.15  christos 	 but binutils as of 2019-07-23 did not set the EI_OSABI header
   1091  1.15  christos 	 byte.  */
   1092  1.15  christos     case ELFOSABI_NONE:
   1093  1.15  christos     case ELFOSABI_GNU:
   1094  1.15  christos     case ELFOSABI_FREEBSD:
   1095  1.15  christos       if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
   1096  1.15  christos 	elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
   1097  1.15  christos       break;
   1098  1.15  christos     }
   1099  1.15  christos 
   1100   1.1     skrll   if ((flags & SEC_ALLOC) == 0)
   1101   1.1     skrll     {
   1102   1.1     skrll       /* The debugging sections appear to be recognized only by name,
   1103   1.1     skrll 	 not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
   1104   1.1     skrll       if (name [0] == '.')
   1105   1.1     skrll 	{
   1106  1.15  christos 	  if (strncmp (name, ".debug", 6) == 0
   1107  1.15  christos 	      || strncmp (name, ".gnu.linkonce.wi.", 17) == 0
   1108  1.15  christos 	      || strncmp (name, ".zdebug", 7) == 0)
   1109  1.15  christos 	    flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
   1110  1.15  christos 	  else if (strncmp (name, GNU_BUILD_ATTRS_SECTION_NAME, 21) == 0
   1111  1.15  christos 		   || strncmp (name, ".note.gnu", 9) == 0)
   1112  1.15  christos 	    flags |= SEC_ELF_OCTETS;
   1113  1.15  christos 	  else if (strncmp (name, ".line", 5) == 0
   1114  1.15  christos 		   || strncmp (name, ".stab", 5) == 0
   1115  1.15  christos 		   || strcmp (name, ".gdb_index") == 0)
   1116   1.1     skrll 	    flags |= SEC_DEBUGGING;
   1117   1.1     skrll 	}
   1118   1.1     skrll     }
   1119   1.1     skrll 
   1120   1.1     skrll   /* As a GNU extension, if the name begins with .gnu.linkonce, we
   1121   1.1     skrll      only link a single copy of the section.  This is used to support
   1122   1.1     skrll      g++.  g++ will emit each template expansion in its own section.
   1123   1.1     skrll      The symbols will be defined as weak, so that multiple definitions
   1124   1.1     skrll      are permitted.  The GNU linker extension is to actually discard
   1125   1.1     skrll      all but one of the sections.  */
   1126   1.1     skrll   if (CONST_STRNEQ (name, ".gnu.linkonce")
   1127   1.1     skrll       && elf_next_in_group (newsect) == NULL)
   1128   1.1     skrll     flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
   1129   1.1     skrll 
   1130   1.1     skrll   bed = get_elf_backend_data (abfd);
   1131   1.1     skrll   if (bed->elf_backend_section_flags)
   1132   1.1     skrll     if (! bed->elf_backend_section_flags (&flags, hdr))
   1133   1.1     skrll       return FALSE;
   1134   1.1     skrll 
   1135  1.15  christos   if (!bfd_set_section_flags (newsect, flags))
   1136   1.1     skrll     return FALSE;
   1137   1.1     skrll 
   1138   1.1     skrll   /* We do not parse the PT_NOTE segments as we are interested even in the
   1139   1.1     skrll      separate debug info files which may have the segments offsets corrupted.
   1140   1.1     skrll      PT_NOTEs from the core files are currently not parsed using BFD.  */
   1141   1.1     skrll   if (hdr->sh_type == SHT_NOTE)
   1142   1.1     skrll     {
   1143   1.1     skrll       bfd_byte *contents;
   1144   1.1     skrll 
   1145   1.1     skrll       if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
   1146   1.1     skrll 	return FALSE;
   1147   1.1     skrll 
   1148   1.9  christos       elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
   1149   1.9  christos 		       hdr->sh_offset, hdr->sh_addralign);
   1150   1.1     skrll       free (contents);
   1151   1.1     skrll     }
   1152   1.1     skrll 
   1153   1.1     skrll   if ((flags & SEC_ALLOC) != 0)
   1154   1.1     skrll     {
   1155   1.1     skrll       Elf_Internal_Phdr *phdr;
   1156   1.1     skrll       unsigned int i, nload;
   1157   1.1     skrll 
   1158   1.1     skrll       /* Some ELF linkers produce binaries with all the program header
   1159   1.1     skrll 	 p_paddr fields zero.  If we have such a binary with more than
   1160   1.1     skrll 	 one PT_LOAD header, then leave the section lma equal to vma
   1161   1.1     skrll 	 so that we don't create sections with overlapping lma.  */
   1162   1.1     skrll       phdr = elf_tdata (abfd)->phdr;
   1163   1.1     skrll       for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
   1164   1.1     skrll 	if (phdr->p_paddr != 0)
   1165   1.1     skrll 	  break;
   1166   1.1     skrll 	else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
   1167   1.1     skrll 	  ++nload;
   1168   1.1     skrll       if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
   1169   1.1     skrll 	return TRUE;
   1170   1.1     skrll 
   1171   1.1     skrll       phdr = elf_tdata (abfd)->phdr;
   1172   1.1     skrll       for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
   1173   1.1     skrll 	{
   1174   1.3  christos 	  if (((phdr->p_type == PT_LOAD
   1175   1.3  christos 		&& (hdr->sh_flags & SHF_TLS) == 0)
   1176   1.3  christos 	       || phdr->p_type == PT_TLS)
   1177   1.3  christos 	      && ELF_SECTION_IN_SEGMENT (hdr, phdr))
   1178   1.1     skrll 	    {
   1179   1.1     skrll 	      if ((flags & SEC_LOAD) == 0)
   1180   1.1     skrll 		newsect->lma = (phdr->p_paddr
   1181   1.1     skrll 				+ hdr->sh_addr - phdr->p_vaddr);
   1182   1.1     skrll 	      else
   1183   1.1     skrll 		/* We used to use the same adjustment for SEC_LOAD
   1184   1.1     skrll 		   sections, but that doesn't work if the segment
   1185   1.1     skrll 		   is packed with code from multiple VMAs.
   1186   1.1     skrll 		   Instead we calculate the section LMA based on
   1187   1.1     skrll 		   the segment LMA.  It is assumed that the
   1188   1.1     skrll 		   segment will contain sections with contiguous
   1189   1.1     skrll 		   LMAs, even if the VMAs are not.  */
   1190   1.1     skrll 		newsect->lma = (phdr->p_paddr
   1191   1.1     skrll 				+ hdr->sh_offset - phdr->p_offset);
   1192   1.1     skrll 
   1193   1.1     skrll 	      /* With contiguous segments, we can't tell from file
   1194   1.1     skrll 		 offsets whether a section with zero size should
   1195   1.1     skrll 		 be placed at the end of one segment or the
   1196   1.1     skrll 		 beginning of the next.  Decide based on vaddr.  */
   1197   1.1     skrll 	      if (hdr->sh_addr >= phdr->p_vaddr
   1198   1.1     skrll 		  && (hdr->sh_addr + hdr->sh_size
   1199   1.1     skrll 		      <= phdr->p_vaddr + phdr->p_memsz))
   1200   1.1     skrll 		break;
   1201   1.1     skrll 	    }
   1202   1.1     skrll 	}
   1203   1.1     skrll     }
   1204   1.1     skrll 
   1205   1.3  christos   /* Compress/decompress DWARF debug sections with names: .debug_* and
   1206   1.3  christos      .zdebug_*, after the section flags is set.  */
   1207   1.3  christos   if ((flags & SEC_DEBUGGING)
   1208   1.3  christos       && ((name[1] == 'd' && name[6] == '_')
   1209   1.3  christos 	  || (name[1] == 'z' && name[7] == '_')))
   1210   1.3  christos     {
   1211   1.3  christos       enum { nothing, compress, decompress } action = nothing;
   1212   1.6  christos       int compression_header_size;
   1213   1.6  christos       bfd_size_type uncompressed_size;
   1214  1.15  christos       unsigned int uncompressed_align_power;
   1215   1.6  christos       bfd_boolean compressed
   1216   1.6  christos 	= bfd_is_section_compressed_with_header (abfd, newsect,
   1217   1.6  christos 						 &compression_header_size,
   1218  1.15  christos 						 &uncompressed_size,
   1219  1.15  christos 						 &uncompressed_align_power);
   1220   1.6  christos       if (compressed)
   1221   1.3  christos 	{
   1222   1.3  christos 	  /* Compressed section.  Check if we should decompress.  */
   1223   1.3  christos 	  if ((abfd->flags & BFD_DECOMPRESS))
   1224   1.3  christos 	    action = decompress;
   1225   1.3  christos 	}
   1226   1.6  christos 
   1227   1.6  christos       /* Compress the uncompressed section or convert from/to .zdebug*
   1228   1.6  christos 	 section.  Check if we should compress.  */
   1229   1.6  christos       if (action == nothing)
   1230   1.6  christos 	{
   1231   1.6  christos 	  if (newsect->size != 0
   1232   1.6  christos 	      && (abfd->flags & BFD_COMPRESS)
   1233   1.6  christos 	      && compression_header_size >= 0
   1234   1.6  christos 	      && uncompressed_size > 0
   1235   1.6  christos 	      && (!compressed
   1236   1.6  christos 		  || ((compression_header_size > 0)
   1237   1.6  christos 		      != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
   1238   1.3  christos 	    action = compress;
   1239   1.6  christos 	  else
   1240   1.6  christos 	    return TRUE;
   1241   1.3  christos 	}
   1242   1.1     skrll 
   1243   1.6  christos       if (action == compress)
   1244   1.3  christos 	{
   1245   1.3  christos 	  if (!bfd_init_section_compress_status (abfd, newsect))
   1246   1.3  christos 	    {
   1247   1.9  christos 	      _bfd_error_handler
   1248   1.9  christos 		/* xgettext:c-format */
   1249  1.13  christos 		(_("%pB: unable to initialize compress status for section %s"),
   1250   1.3  christos 		 abfd, name);
   1251   1.3  christos 	      return FALSE;
   1252   1.3  christos 	    }
   1253   1.6  christos 	}
   1254   1.6  christos       else
   1255   1.6  christos 	{
   1256   1.3  christos 	  if (!bfd_init_section_decompress_status (abfd, newsect))
   1257   1.3  christos 	    {
   1258   1.9  christos 	      _bfd_error_handler
   1259   1.9  christos 		/* xgettext:c-format */
   1260  1.13  christos 		(_("%pB: unable to initialize decompress status for section %s"),
   1261   1.3  christos 		 abfd, name);
   1262   1.3  christos 	      return FALSE;
   1263   1.3  christos 	    }
   1264   1.6  christos 	}
   1265   1.1     skrll 
   1266   1.6  christos       if (abfd->is_linker_input)
   1267   1.6  christos 	{
   1268   1.6  christos 	  if (name[1] == 'z'
   1269   1.6  christos 	      && (action == decompress
   1270   1.6  christos 		  || (action == compress
   1271   1.6  christos 		      && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
   1272   1.6  christos 	    {
   1273   1.6  christos 	      /* Convert section name from .zdebug_* to .debug_* so
   1274   1.6  christos 		 that linker will consider this section as a debug
   1275   1.6  christos 		 section.  */
   1276   1.6  christos 	      char *new_name = convert_zdebug_to_debug (abfd, name);
   1277   1.3  christos 	      if (new_name == NULL)
   1278   1.3  christos 		return FALSE;
   1279  1.15  christos 	      bfd_rename_section (newsect, new_name);
   1280   1.3  christos 	    }
   1281   1.1     skrll 	}
   1282   1.6  christos       else
   1283   1.6  christos 	/* For objdump, don't rename the section.  For objcopy, delay
   1284   1.6  christos 	   section rename to elf_fake_sections.  */
   1285   1.6  christos 	newsect->flags |= SEC_ELF_RENAME;
   1286   1.1     skrll     }
   1287   1.3  christos 
   1288  1.15  christos   /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
   1289  1.15  christos      section.  */
   1290  1.15  christos   const char *lto_section_name = ".gnu.lto_.lto.";
   1291  1.15  christos   if (strncmp (name, lto_section_name, strlen (lto_section_name)) == 0)
   1292  1.15  christos     {
   1293  1.15  christos       struct lto_section lsection;
   1294  1.15  christos       if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
   1295  1.15  christos 				    sizeof (struct lto_section)))
   1296  1.15  christos 	abfd->lto_slim_object = lsection.slim_object;
   1297  1.15  christos     }
   1298  1.15  christos 
   1299   1.3  christos   return TRUE;
   1300   1.1     skrll }
   1301   1.1     skrll 
   1302   1.7  christos const char *const bfd_elf_section_type_names[] =
   1303   1.7  christos {
   1304   1.1     skrll   "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
   1305   1.1     skrll   "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
   1306   1.1     skrll   "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
   1307   1.1     skrll };
   1308   1.1     skrll 
   1309   1.1     skrll /* ELF relocs are against symbols.  If we are producing relocatable
   1310   1.1     skrll    output, and the reloc is against an external symbol, and nothing
   1311   1.1     skrll    has given us any additional addend, the resulting reloc will also
   1312   1.1     skrll    be against the same symbol.  In such a case, we don't want to
   1313   1.1     skrll    change anything about the way the reloc is handled, since it will
   1314   1.1     skrll    all be done at final link time.  Rather than put special case code
   1315   1.1     skrll    into bfd_perform_relocation, all the reloc types use this howto
   1316   1.1     skrll    function.  It just short circuits the reloc if producing
   1317   1.1     skrll    relocatable output against an external symbol.  */
   1318   1.1     skrll 
   1319   1.1     skrll bfd_reloc_status_type
   1320   1.1     skrll bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
   1321   1.1     skrll 		       arelent *reloc_entry,
   1322   1.1     skrll 		       asymbol *symbol,
   1323   1.1     skrll 		       void *data ATTRIBUTE_UNUSED,
   1324   1.1     skrll 		       asection *input_section,
   1325   1.1     skrll 		       bfd *output_bfd,
   1326   1.1     skrll 		       char **error_message ATTRIBUTE_UNUSED)
   1327   1.1     skrll {
   1328   1.1     skrll   if (output_bfd != NULL
   1329   1.1     skrll       && (symbol->flags & BSF_SECTION_SYM) == 0
   1330   1.1     skrll       && (! reloc_entry->howto->partial_inplace
   1331   1.1     skrll 	  || reloc_entry->addend == 0))
   1332   1.1     skrll     {
   1333   1.1     skrll       reloc_entry->address += input_section->output_offset;
   1334   1.1     skrll       return bfd_reloc_ok;
   1335   1.1     skrll     }
   1336   1.1     skrll 
   1337   1.1     skrll   return bfd_reloc_continue;
   1338   1.1     skrll }
   1339   1.1     skrll 
   1340   1.7  christos /* Returns TRUE if section A matches section B.
   1342   1.7  christos    Names, addresses and links may be different, but everything else
   1343   1.7  christos    should be the same.  */
   1344   1.7  christos 
   1345   1.7  christos static bfd_boolean
   1346   1.7  christos section_match (const Elf_Internal_Shdr * a,
   1347   1.7  christos 	       const Elf_Internal_Shdr * b)
   1348  1.15  christos {
   1349  1.15  christos   if (a->sh_type != b->sh_type
   1350  1.15  christos       || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
   1351  1.15  christos       || a->sh_addralign != b->sh_addralign
   1352  1.15  christos       || a->sh_entsize != b->sh_entsize)
   1353  1.15  christos     return FALSE;
   1354  1.15  christos   if (a->sh_type == SHT_SYMTAB
   1355  1.15  christos       || a->sh_type == SHT_STRTAB)
   1356  1.15  christos     return TRUE;
   1357   1.7  christos   return a->sh_size == b->sh_size;
   1358   1.7  christos }
   1359   1.7  christos 
   1360   1.7  christos /* Find a section in OBFD that has the same characteristics
   1361   1.7  christos    as IHEADER.  Return the index of this section or SHN_UNDEF if
   1362   1.7  christos    none can be found.  Check's section HINT first, as this is likely
   1363   1.7  christos    to be the correct section.  */
   1364   1.7  christos 
   1365   1.9  christos static unsigned int
   1366   1.9  christos find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
   1367   1.7  christos 	   const unsigned int hint)
   1368   1.7  christos {
   1369   1.7  christos   Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
   1370   1.7  christos   unsigned int i;
   1371   1.9  christos 
   1372   1.9  christos   BFD_ASSERT (iheader != NULL);
   1373   1.9  christos 
   1374   1.9  christos   /* See PR 20922 for a reproducer of the NULL test.  */
   1375   1.9  christos   if (hint < elf_numsections (obfd)
   1376   1.9  christos       && oheaders[hint] != NULL
   1377   1.7  christos       && section_match (oheaders[hint], iheader))
   1378   1.7  christos     return hint;
   1379   1.7  christos 
   1380   1.7  christos   for (i = 1; i < elf_numsections (obfd); i++)
   1381   1.7  christos     {
   1382   1.7  christos       Elf_Internal_Shdr * oheader = oheaders[i];
   1383   1.9  christos 
   1384   1.9  christos       if (oheader == NULL)
   1385   1.7  christos 	continue;
   1386   1.7  christos       if (section_match (oheader, iheader))
   1387   1.7  christos 	/* FIXME: Do we care if there is a potential for
   1388   1.7  christos 	   multiple matches ?  */
   1389   1.7  christos 	return i;
   1390   1.7  christos     }
   1391   1.7  christos 
   1392   1.7  christos   return SHN_UNDEF;
   1393   1.7  christos }
   1394   1.7  christos 
   1395   1.7  christos /* PR 19938: Attempt to set the ELF section header fields of an OS or
   1396   1.7  christos    Processor specific section, based upon a matching input section.
   1397   1.9  christos    Returns TRUE upon success, FALSE otherwise.  */
   1398   1.7  christos 
   1399   1.7  christos static bfd_boolean
   1400   1.7  christos copy_special_section_fields (const bfd *ibfd,
   1401   1.7  christos 			     bfd *obfd,
   1402   1.7  christos 			     const Elf_Internal_Shdr *iheader,
   1403   1.7  christos 			     Elf_Internal_Shdr *oheader,
   1404   1.7  christos 			     const unsigned int secnum)
   1405   1.7  christos {
   1406   1.7  christos   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
   1407   1.7  christos   const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
   1408   1.7  christos   bfd_boolean changed = FALSE;
   1409   1.7  christos   unsigned int sh_link;
   1410   1.7  christos 
   1411   1.7  christos   if (oheader->sh_type == SHT_NOBITS)
   1412   1.7  christos     {
   1413   1.7  christos       /* This is a feature for objcopy --only-keep-debug:
   1414   1.7  christos 	 When a section's type is changed to NOBITS, we preserve
   1415   1.7  christos 	 the sh_link and sh_info fields so that they can be
   1416   1.7  christos 	 matched up with the original.
   1417   1.7  christos 
   1418   1.7  christos 	 Note: Strictly speaking these assignments are wrong.
   1419   1.7  christos 	 The sh_link and sh_info fields should point to the
   1420   1.7  christos 	 relevent sections in the output BFD, which may not be in
   1421   1.7  christos 	 the same location as they were in the input BFD.  But
   1422   1.7  christos 	 the whole point of this action is to preserve the
   1423   1.7  christos 	 original values of the sh_link and sh_info fields, so
   1424   1.7  christos 	 that they can be matched up with the section headers in
   1425   1.7  christos 	 the original file.  So strictly speaking we may be
   1426   1.7  christos 	 creating an invalid ELF file, but it is only for a file
   1427   1.7  christos 	 that just contains debug info and only for sections
   1428   1.7  christos 	 without any contents.  */
   1429   1.7  christos       if (oheader->sh_link == 0)
   1430   1.7  christos 	oheader->sh_link = iheader->sh_link;
   1431   1.7  christos       if (oheader->sh_info == 0)
   1432   1.7  christos 	oheader->sh_info = iheader->sh_info;
   1433   1.7  christos       return TRUE;
   1434   1.7  christos     }
   1435   1.7  christos 
   1436   1.7  christos   /* Allow the target a chance to decide how these fields should be set.  */
   1437   1.7  christos   if (bed->elf_backend_copy_special_section_fields != NULL
   1438   1.7  christos       && bed->elf_backend_copy_special_section_fields
   1439   1.7  christos       (ibfd, obfd, iheader, oheader))
   1440   1.7  christos     return TRUE;
   1441   1.7  christos 
   1442   1.7  christos   /* We have an iheader which might match oheader, and which has non-zero
   1443   1.7  christos      sh_info and/or sh_link fields.  Attempt to follow those links and find
   1444   1.7  christos      the section in the output bfd which corresponds to the linked section
   1445   1.7  christos      in the input bfd.  */
   1446   1.7  christos   if (iheader->sh_link != SHN_UNDEF)
   1447   1.9  christos     {
   1448   1.9  christos       /* See PR 20931 for a reproducer.  */
   1449   1.9  christos       if (iheader->sh_link >= elf_numsections (ibfd))
   1450   1.9  christos 	{
   1451   1.9  christos 	  _bfd_error_handler
   1452  1.13  christos 	    /* xgettext:c-format */
   1453   1.9  christos 	    (_("%pB: invalid sh_link field (%d) in section number %d"),
   1454   1.9  christos 	     ibfd, iheader->sh_link, secnum);
   1455   1.9  christos 	  return FALSE;
   1456   1.9  christos 	}
   1457   1.7  christos 
   1458   1.7  christos       sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
   1459   1.7  christos       if (sh_link != SHN_UNDEF)
   1460   1.7  christos 	{
   1461   1.7  christos 	  oheader->sh_link = sh_link;
   1462   1.7  christos 	  changed = TRUE;
   1463   1.7  christos 	}
   1464   1.7  christos       else
   1465   1.7  christos 	/* FIXME: Should we install iheader->sh_link
   1466   1.9  christos 	   if we could not find a match ?  */
   1467   1.9  christos 	_bfd_error_handler
   1468  1.13  christos 	  /* xgettext:c-format */
   1469   1.7  christos 	  (_("%pB: failed to find link section for section %d"), obfd, secnum);
   1470   1.7  christos     }
   1471   1.7  christos 
   1472   1.7  christos   if (iheader->sh_info)
   1473   1.7  christos     {
   1474   1.7  christos       /* The sh_info field can hold arbitrary information, but if the
   1475   1.7  christos 	 SHF_LINK_INFO flag is set then it should be interpreted as a
   1476   1.7  christos 	 section index.  */
   1477   1.7  christos       if (iheader->sh_flags & SHF_INFO_LINK)
   1478   1.7  christos 	{
   1479   1.7  christos 	  sh_link = find_link (obfd, iheaders[iheader->sh_info],
   1480   1.7  christos 			       iheader->sh_info);
   1481   1.7  christos 	  if (sh_link != SHN_UNDEF)
   1482   1.7  christos 	    oheader->sh_flags |= SHF_INFO_LINK;
   1483   1.7  christos 	}
   1484   1.7  christos       else
   1485   1.7  christos 	/* No idea what it means - just copy it.  */
   1486   1.7  christos 	sh_link = iheader->sh_info;
   1487   1.7  christos 
   1488   1.7  christos       if (sh_link != SHN_UNDEF)
   1489   1.7  christos 	{
   1490   1.7  christos 	  oheader->sh_info = sh_link;
   1491   1.7  christos 	  changed = TRUE;
   1492   1.7  christos 	}
   1493   1.9  christos       else
   1494   1.9  christos 	_bfd_error_handler
   1495  1.13  christos 	  /* xgettext:c-format */
   1496   1.7  christos 	  (_("%pB: failed to find info section for section %d"), obfd, secnum);
   1497   1.7  christos     }
   1498   1.7  christos 
   1499   1.7  christos   return changed;
   1500   1.9  christos }
   1501   1.1     skrll 
   1502   1.1     skrll /* Copy the program header and other data from one object module to
   1503   1.1     skrll    another.  */
   1504   1.1     skrll 
   1505   1.1     skrll bfd_boolean
   1506   1.1     skrll _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   1507   1.7  christos {
   1508   1.7  christos   const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
   1509   1.7  christos   Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
   1510   1.7  christos   const struct elf_backend_data *bed;
   1511   1.7  christos   unsigned int i;
   1512   1.1     skrll 
   1513   1.7  christos   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   1514   1.1     skrll     || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   1515   1.1     skrll     return TRUE;
   1516   1.6  christos 
   1517   1.6  christos   if (!elf_flags_init (obfd))
   1518   1.6  christos     {
   1519   1.6  christos       elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
   1520   1.6  christos       elf_flags_init (obfd) = TRUE;
   1521   1.1     skrll     }
   1522   1.1     skrll 
   1523   1.6  christos   elf_gp (obfd) = elf_gp (ibfd);
   1524   1.6  christos 
   1525   1.6  christos   /* Also copy the EI_OSABI field.  */
   1526   1.6  christos   elf_elfheader (obfd)->e_ident[EI_OSABI] =
   1527   1.1     skrll     elf_elfheader (ibfd)->e_ident[EI_OSABI];
   1528   1.7  christos 
   1529   1.7  christos   /* If set, copy the EI_ABIVERSION field.  */
   1530   1.7  christos   if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
   1531   1.7  christos     elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
   1532   1.9  christos       = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
   1533   1.1     skrll 
   1534   1.1     skrll   /* Copy object attributes.  */
   1535   1.6  christos   _bfd_elf_copy_obj_attributes (ibfd, obfd);
   1536   1.7  christos 
   1537   1.7  christos   if (iheaders == NULL || oheaders == NULL)
   1538   1.7  christos     return TRUE;
   1539   1.7  christos 
   1540   1.6  christos   bed = get_elf_backend_data (obfd);
   1541   1.7  christos 
   1542   1.7  christos   /* Possibly copy other fields in the section header.  */
   1543   1.6  christos   for (i = 1; i < elf_numsections (obfd); i++)
   1544   1.7  christos     {
   1545   1.7  christos       unsigned int j;
   1546   1.7  christos       Elf_Internal_Shdr * oheader = oheaders[i];
   1547   1.7  christos 
   1548   1.7  christos       /* Ignore ordinary sections.  SHT_NOBITS sections are considered however
   1549   1.7  christos 	 because of a special case need for generating separate debug info
   1550   1.7  christos 	 files.  See below for more details.  */
   1551   1.7  christos       if (oheader == NULL
   1552   1.7  christos 	  || (oheader->sh_type != SHT_NOBITS
   1553   1.7  christos 	      && oheader->sh_type < SHT_LOOS))
   1554   1.7  christos 	continue;
   1555   1.7  christos 
   1556   1.7  christos       /* Ignore empty sections, and sections whose
   1557   1.7  christos 	 fields have already been initialised.  */
   1558   1.7  christos       if (oheader->sh_size == 0
   1559   1.7  christos 	  || (oheader->sh_info != 0 && oheader->sh_link != 0))
   1560   1.7  christos 	continue;
   1561   1.7  christos 
   1562   1.7  christos       /* Scan for the matching section in the input bfd.
   1563   1.7  christos 	 First we try for a direct mapping between the input and output sections.  */
   1564   1.7  christos       for (j = 1; j < elf_numsections (ibfd); j++)
   1565   1.7  christos 	{
   1566   1.7  christos 	  const Elf_Internal_Shdr * iheader = iheaders[j];
   1567   1.7  christos 
   1568   1.7  christos 	  if (iheader == NULL)
   1569   1.7  christos 	    continue;
   1570   1.7  christos 
   1571   1.7  christos 	  if (oheader->bfd_section != NULL
   1572   1.7  christos 	      && iheader->bfd_section != NULL
   1573   1.7  christos 	      && iheader->bfd_section->output_section != NULL
   1574   1.7  christos 	      && iheader->bfd_section->output_section == oheader->bfd_section)
   1575   1.7  christos 	    {
   1576   1.7  christos 	      /* We have found a connection from the input section to the
   1577   1.7  christos 		 output section.  Attempt to copy the header fields.  If
   1578   1.7  christos 		 this fails then do not try any further sections - there
   1579   1.7  christos 		 should only be a one-to-one mapping between input and output. */
   1580   1.7  christos 	      if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
   1581   1.7  christos 		j = elf_numsections (ibfd);
   1582   1.7  christos 	      break;
   1583   1.7  christos 	    }
   1584   1.7  christos 	}
   1585   1.7  christos 
   1586   1.7  christos       if (j < elf_numsections (ibfd))
   1587   1.6  christos 	continue;
   1588   1.7  christos 
   1589   1.7  christos       /* That failed.  So try to deduce the corresponding input section.
   1590   1.7  christos 	 Unfortunately we cannot compare names as the output string table
   1591   1.7  christos 	 is empty, so instead we check size, address and type.  */
   1592   1.6  christos       for (j = 1; j < elf_numsections (ibfd); j++)
   1593   1.7  christos 	{
   1594   1.6  christos 	  const Elf_Internal_Shdr * iheader = iheaders[j];
   1595   1.7  christos 
   1596   1.6  christos 	  if (iheader == NULL)
   1597   1.6  christos 	    continue;
   1598   1.7  christos 
   1599   1.7  christos 	  /* Try matching fields in the input section's header.
   1600   1.7  christos 	     Since --only-keep-debug turns all non-debug sections into
   1601   1.7  christos 	     SHT_NOBITS sections, the output SHT_NOBITS type matches any
   1602   1.7  christos 	     input type.  */
   1603   1.7  christos 	  if ((oheader->sh_type == SHT_NOBITS
   1604   1.7  christos 	       || iheader->sh_type == oheader->sh_type)
   1605   1.7  christos 	      && (iheader->sh_flags & ~ SHF_INFO_LINK)
   1606   1.7  christos 	      == (oheader->sh_flags & ~ SHF_INFO_LINK)
   1607   1.7  christos 	      && iheader->sh_addralign == oheader->sh_addralign
   1608   1.7  christos 	      && iheader->sh_entsize == oheader->sh_entsize
   1609   1.7  christos 	      && iheader->sh_size == oheader->sh_size
   1610   1.7  christos 	      && iheader->sh_addr == oheader->sh_addr
   1611   1.7  christos 	      && (iheader->sh_info != oheader->sh_info
   1612   1.7  christos 		  || iheader->sh_link != oheader->sh_link))
   1613   1.7  christos 	    {
   1614   1.7  christos 	      if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
   1615   1.6  christos 		break;
   1616   1.6  christos 	    }
   1617   1.7  christos 	}
   1618   1.7  christos 
   1619   1.7  christos       if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
   1620   1.7  christos 	{
   1621   1.7  christos 	  /* Final attempt.  Call the backend copy function
   1622   1.7  christos 	     with a NULL input section.  */
   1623   1.7  christos 	  if (bed->elf_backend_copy_special_section_fields != NULL)
   1624   1.7  christos 	    bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader);
   1625   1.6  christos 	}
   1626   1.6  christos     }
   1627   1.1     skrll 
   1628   1.1     skrll   return TRUE;
   1629   1.1     skrll }
   1630   1.1     skrll 
   1631   1.1     skrll static const char *
   1632   1.1     skrll get_segment_type (unsigned int p_type)
   1633   1.1     skrll {
   1634   1.1     skrll   const char *pt;
   1635   1.1     skrll   switch (p_type)
   1636   1.1     skrll     {
   1637   1.1     skrll     case PT_NULL: pt = "NULL"; break;
   1638   1.1     skrll     case PT_LOAD: pt = "LOAD"; break;
   1639   1.1     skrll     case PT_DYNAMIC: pt = "DYNAMIC"; break;
   1640   1.1     skrll     case PT_INTERP: pt = "INTERP"; break;
   1641   1.1     skrll     case PT_NOTE: pt = "NOTE"; break;
   1642   1.1     skrll     case PT_SHLIB: pt = "SHLIB"; break;
   1643   1.1     skrll     case PT_PHDR: pt = "PHDR"; break;
   1644   1.1     skrll     case PT_TLS: pt = "TLS"; break;
   1645   1.1     skrll     case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
   1646   1.1     skrll     case PT_GNU_STACK: pt = "STACK"; break;
   1647   1.1     skrll     case PT_GNU_RELRO: pt = "RELRO"; break;
   1648   1.1     skrll     default: pt = NULL; break;
   1649   1.1     skrll     }
   1650   1.1     skrll   return pt;
   1651   1.1     skrll }
   1652   1.1     skrll 
   1653   1.1     skrll /* Print out the program headers.  */
   1654   1.1     skrll 
   1655   1.1     skrll bfd_boolean
   1656   1.1     skrll _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
   1657   1.3  christos {
   1658   1.1     skrll   FILE *f = (FILE *) farg;
   1659   1.1     skrll   Elf_Internal_Phdr *p;
   1660   1.1     skrll   asection *s;
   1661   1.1     skrll   bfd_byte *dynbuf = NULL;
   1662   1.1     skrll 
   1663   1.1     skrll   p = elf_tdata (abfd)->phdr;
   1664   1.1     skrll   if (p != NULL)
   1665   1.1     skrll     {
   1666   1.1     skrll       unsigned int i, c;
   1667   1.1     skrll 
   1668   1.1     skrll       fprintf (f, _("\nProgram Header:\n"));
   1669   1.1     skrll       c = elf_elfheader (abfd)->e_phnum;
   1670   1.1     skrll       for (i = 0; i < c; i++, p++)
   1671   1.1     skrll 	{
   1672   1.1     skrll 	  const char *pt = get_segment_type (p->p_type);
   1673   1.1     skrll 	  char buf[20];
   1674   1.1     skrll 
   1675   1.1     skrll 	  if (pt == NULL)
   1676   1.1     skrll 	    {
   1677   1.1     skrll 	      sprintf (buf, "0x%lx", p->p_type);
   1678   1.1     skrll 	      pt = buf;
   1679   1.1     skrll 	    }
   1680   1.1     skrll 	  fprintf (f, "%8s off    0x", pt);
   1681   1.1     skrll 	  bfd_fprintf_vma (abfd, f, p->p_offset);
   1682   1.1     skrll 	  fprintf (f, " vaddr 0x");
   1683   1.1     skrll 	  bfd_fprintf_vma (abfd, f, p->p_vaddr);
   1684   1.1     skrll 	  fprintf (f, " paddr 0x");
   1685   1.1     skrll 	  bfd_fprintf_vma (abfd, f, p->p_paddr);
   1686   1.1     skrll 	  fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
   1687   1.1     skrll 	  fprintf (f, "         filesz 0x");
   1688   1.1     skrll 	  bfd_fprintf_vma (abfd, f, p->p_filesz);
   1689   1.1     skrll 	  fprintf (f, " memsz 0x");
   1690   1.1     skrll 	  bfd_fprintf_vma (abfd, f, p->p_memsz);
   1691   1.1     skrll 	  fprintf (f, " flags %c%c%c",
   1692   1.1     skrll 		   (p->p_flags & PF_R) != 0 ? 'r' : '-',
   1693   1.1     skrll 		   (p->p_flags & PF_W) != 0 ? 'w' : '-',
   1694   1.1     skrll 		   (p->p_flags & PF_X) != 0 ? 'x' : '-');
   1695   1.1     skrll 	  if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
   1696   1.1     skrll 	    fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
   1697   1.1     skrll 	  fprintf (f, "\n");
   1698   1.1     skrll 	}
   1699   1.1     skrll     }
   1700   1.1     skrll 
   1701   1.1     skrll   s = bfd_get_section_by_name (abfd, ".dynamic");
   1702   1.1     skrll   if (s != NULL)
   1703   1.1     skrll     {
   1704   1.1     skrll       unsigned int elfsec;
   1705   1.1     skrll       unsigned long shlink;
   1706   1.1     skrll       bfd_byte *extdyn, *extdynend;
   1707   1.1     skrll       size_t extdynsize;
   1708   1.1     skrll       void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   1709   1.1     skrll 
   1710   1.1     skrll       fprintf (f, _("\nDynamic Section:\n"));
   1711   1.1     skrll 
   1712   1.1     skrll       if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
   1713   1.1     skrll 	goto error_return;
   1714   1.1     skrll 
   1715   1.1     skrll       elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   1716   1.1     skrll       if (elfsec == SHN_BAD)
   1717   1.1     skrll 	goto error_return;
   1718   1.1     skrll       shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   1719   1.1     skrll 
   1720   1.1     skrll       extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   1721   1.1     skrll       swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   1722   1.1     skrll 
   1723   1.6  christos       extdyn = dynbuf;
   1724   1.6  christos       /* PR 17512: file: 6f427532.  */
   1725   1.6  christos       if (s->size < extdynsize)
   1726   1.1     skrll 	goto error_return;
   1727   1.6  christos       extdynend = extdyn + s->size;
   1728   1.9  christos       /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
   1729   1.6  christos 	 Fix range check.  */
   1730   1.1     skrll       for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
   1731   1.1     skrll 	{
   1732   1.1     skrll 	  Elf_Internal_Dyn dyn;
   1733   1.1     skrll 	  const char *name = "";
   1734   1.1     skrll 	  char ab[20];
   1735   1.1     skrll 	  bfd_boolean stringp;
   1736   1.1     skrll 	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   1737   1.1     skrll 
   1738   1.1     skrll 	  (*swap_dyn_in) (abfd, extdyn, &dyn);
   1739   1.1     skrll 
   1740   1.1     skrll 	  if (dyn.d_tag == DT_NULL)
   1741   1.1     skrll 	    break;
   1742   1.1     skrll 
   1743   1.1     skrll 	  stringp = FALSE;
   1744   1.1     skrll 	  switch (dyn.d_tag)
   1745   1.1     skrll 	    {
   1746   1.1     skrll 	    default:
   1747   1.1     skrll 	      if (bed->elf_backend_get_target_dtag)
   1748   1.1     skrll 		name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
   1749   1.1     skrll 
   1750   1.1     skrll 	      if (!strcmp (name, ""))
   1751   1.9  christos 		{
   1752   1.1     skrll 		  sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
   1753   1.1     skrll 		  name = ab;
   1754   1.1     skrll 		}
   1755   1.1     skrll 	      break;
   1756   1.1     skrll 
   1757   1.1     skrll 	    case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
   1758   1.1     skrll 	    case DT_PLTRELSZ: name = "PLTRELSZ"; break;
   1759   1.1     skrll 	    case DT_PLTGOT: name = "PLTGOT"; break;
   1760   1.1     skrll 	    case DT_HASH: name = "HASH"; break;
   1761   1.1     skrll 	    case DT_STRTAB: name = "STRTAB"; break;
   1762   1.1     skrll 	    case DT_SYMTAB: name = "SYMTAB"; break;
   1763   1.1     skrll 	    case DT_RELA: name = "RELA"; break;
   1764   1.1     skrll 	    case DT_RELASZ: name = "RELASZ"; break;
   1765   1.1     skrll 	    case DT_RELAENT: name = "RELAENT"; break;
   1766   1.1     skrll 	    case DT_STRSZ: name = "STRSZ"; break;
   1767   1.1     skrll 	    case DT_SYMENT: name = "SYMENT"; break;
   1768   1.1     skrll 	    case DT_INIT: name = "INIT"; break;
   1769   1.1     skrll 	    case DT_FINI: name = "FINI"; break;
   1770   1.1     skrll 	    case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
   1771   1.1     skrll 	    case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
   1772   1.1     skrll 	    case DT_SYMBOLIC: name = "SYMBOLIC"; break;
   1773   1.1     skrll 	    case DT_REL: name = "REL"; break;
   1774   1.1     skrll 	    case DT_RELSZ: name = "RELSZ"; break;
   1775   1.1     skrll 	    case DT_RELENT: name = "RELENT"; break;
   1776   1.1     skrll 	    case DT_PLTREL: name = "PLTREL"; break;
   1777   1.1     skrll 	    case DT_DEBUG: name = "DEBUG"; break;
   1778   1.1     skrll 	    case DT_TEXTREL: name = "TEXTREL"; break;
   1779   1.1     skrll 	    case DT_JMPREL: name = "JMPREL"; break;
   1780   1.1     skrll 	    case DT_BIND_NOW: name = "BIND_NOW"; break;
   1781   1.1     skrll 	    case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
   1782   1.1     skrll 	    case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
   1783   1.1     skrll 	    case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
   1784   1.1     skrll 	    case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
   1785   1.1     skrll 	    case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
   1786   1.1     skrll 	    case DT_FLAGS: name = "FLAGS"; break;
   1787   1.1     skrll 	    case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
   1788   1.1     skrll 	    case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
   1789   1.1     skrll 	    case DT_CHECKSUM: name = "CHECKSUM"; break;
   1790   1.1     skrll 	    case DT_PLTPADSZ: name = "PLTPADSZ"; break;
   1791   1.1     skrll 	    case DT_MOVEENT: name = "MOVEENT"; break;
   1792   1.1     skrll 	    case DT_MOVESZ: name = "MOVESZ"; break;
   1793   1.1     skrll 	    case DT_FEATURE: name = "FEATURE"; break;
   1794   1.1     skrll 	    case DT_POSFLAG_1: name = "POSFLAG_1"; break;
   1795   1.1     skrll 	    case DT_SYMINSZ: name = "SYMINSZ"; break;
   1796   1.1     skrll 	    case DT_SYMINENT: name = "SYMINENT"; break;
   1797   1.1     skrll 	    case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
   1798   1.1     skrll 	    case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
   1799   1.1     skrll 	    case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
   1800   1.1     skrll 	    case DT_PLTPAD: name = "PLTPAD"; break;
   1801   1.1     skrll 	    case DT_MOVETAB: name = "MOVETAB"; break;
   1802   1.1     skrll 	    case DT_SYMINFO: name = "SYMINFO"; break;
   1803   1.1     skrll 	    case DT_RELACOUNT: name = "RELACOUNT"; break;
   1804   1.1     skrll 	    case DT_RELCOUNT: name = "RELCOUNT"; break;
   1805   1.1     skrll 	    case DT_FLAGS_1: name = "FLAGS_1"; break;
   1806   1.1     skrll 	    case DT_VERSYM: name = "VERSYM"; break;
   1807   1.1     skrll 	    case DT_VERDEF: name = "VERDEF"; break;
   1808   1.1     skrll 	    case DT_VERDEFNUM: name = "VERDEFNUM"; break;
   1809   1.1     skrll 	    case DT_VERNEED: name = "VERNEED"; break;
   1810   1.1     skrll 	    case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
   1811   1.1     skrll 	    case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
   1812   1.1     skrll 	    case DT_USED: name = "USED"; break;
   1813   1.1     skrll 	    case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
   1814   1.1     skrll 	    case DT_GNU_HASH: name = "GNU_HASH"; break;
   1815   1.1     skrll 	    }
   1816   1.1     skrll 
   1817   1.1     skrll 	  fprintf (f, "  %-20s ", name);
   1818   1.1     skrll 	  if (! stringp)
   1819   1.1     skrll 	    {
   1820   1.1     skrll 	      fprintf (f, "0x");
   1821   1.1     skrll 	      bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
   1822   1.1     skrll 	    }
   1823   1.1     skrll 	  else
   1824   1.1     skrll 	    {
   1825   1.1     skrll 	      const char *string;
   1826   1.1     skrll 	      unsigned int tagv = dyn.d_un.d_val;
   1827   1.1     skrll 
   1828   1.1     skrll 	      string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   1829   1.1     skrll 	      if (string == NULL)
   1830   1.1     skrll 		goto error_return;
   1831   1.1     skrll 	      fprintf (f, "%s", string);
   1832   1.1     skrll 	    }
   1833   1.1     skrll 	  fprintf (f, "\n");
   1834   1.1     skrll 	}
   1835   1.1     skrll 
   1836   1.1     skrll       free (dynbuf);
   1837   1.1     skrll       dynbuf = NULL;
   1838   1.1     skrll     }
   1839   1.1     skrll 
   1840   1.1     skrll   if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
   1841   1.1     skrll       || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
   1842   1.1     skrll     {
   1843   1.1     skrll       if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
   1844   1.1     skrll 	return FALSE;
   1845   1.1     skrll     }
   1846   1.1     skrll 
   1847   1.1     skrll   if (elf_dynverdef (abfd) != 0)
   1848   1.1     skrll     {
   1849   1.1     skrll       Elf_Internal_Verdef *t;
   1850   1.1     skrll 
   1851   1.1     skrll       fprintf (f, _("\nVersion definitions:\n"));
   1852   1.1     skrll       for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
   1853   1.1     skrll 	{
   1854   1.1     skrll 	  fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
   1855   1.1     skrll 		   t->vd_flags, t->vd_hash,
   1856   1.1     skrll 		   t->vd_nodename ? t->vd_nodename : "<corrupt>");
   1857   1.1     skrll 	  if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
   1858   1.1     skrll 	    {
   1859   1.1     skrll 	      Elf_Internal_Verdaux *a;
   1860   1.1     skrll 
   1861   1.1     skrll 	      fprintf (f, "\t");
   1862   1.1     skrll 	      for (a = t->vd_auxptr->vda_nextptr;
   1863   1.1     skrll 		   a != NULL;
   1864   1.1     skrll 		   a = a->vda_nextptr)
   1865   1.1     skrll 		fprintf (f, "%s ",
   1866   1.1     skrll 			 a->vda_nodename ? a->vda_nodename : "<corrupt>");
   1867   1.1     skrll 	      fprintf (f, "\n");
   1868   1.1     skrll 	    }
   1869   1.1     skrll 	}
   1870   1.1     skrll     }
   1871   1.1     skrll 
   1872   1.1     skrll   if (elf_dynverref (abfd) != 0)
   1873   1.1     skrll     {
   1874   1.1     skrll       Elf_Internal_Verneed *t;
   1875   1.1     skrll 
   1876   1.1     skrll       fprintf (f, _("\nVersion References:\n"));
   1877   1.1     skrll       for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
   1878   1.1     skrll 	{
   1879   1.1     skrll 	  Elf_Internal_Vernaux *a;
   1880   1.1     skrll 
   1881   1.1     skrll 	  fprintf (f, _("  required from %s:\n"),
   1882   1.1     skrll 		   t->vn_filename ? t->vn_filename : "<corrupt>");
   1883   1.1     skrll 	  for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   1884   1.1     skrll 	    fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
   1885   1.1     skrll 		     a->vna_flags, a->vna_other,
   1886   1.1     skrll 		     a->vna_nodename ? a->vna_nodename : "<corrupt>");
   1887   1.1     skrll 	}
   1888   1.1     skrll     }
   1889   1.1     skrll 
   1890   1.1     skrll   return TRUE;
   1891   1.1     skrll 
   1892   1.1     skrll  error_return:
   1893   1.1     skrll   if (dynbuf != NULL)
   1894   1.1     skrll     free (dynbuf);
   1895   1.1     skrll   return FALSE;
   1896   1.1     skrll }
   1897   1.6  christos 
   1898   1.6  christos /* Get version string.  */
   1899   1.6  christos 
   1900   1.6  christos const char *
   1901   1.6  christos _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
   1902   1.6  christos 				    bfd_boolean *hidden)
   1903   1.6  christos {
   1904   1.6  christos   const char *version_string = NULL;
   1905   1.6  christos   if (elf_dynversym (abfd) != 0
   1906   1.6  christos       && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
   1907   1.6  christos     {
   1908   1.6  christos       unsigned int vernum = ((elf_symbol_type *) symbol)->version;
   1909   1.6  christos 
   1910   1.6  christos       *hidden = (vernum & VERSYM_HIDDEN) != 0;
   1911   1.6  christos       vernum &= VERSYM_VERSION;
   1912   1.6  christos 
   1913   1.6  christos       if (vernum == 0)
   1914  1.13  christos 	version_string = "";
   1915  1.13  christos       else if (vernum == 1
   1916  1.13  christos 	       && (vernum > elf_tdata (abfd)->cverdefs
   1917  1.13  christos 		   || (elf_tdata (abfd)->verdef[0].vd_flags
   1918   1.6  christos 		       == VER_FLG_BASE)))
   1919   1.6  christos 	version_string = "Base";
   1920   1.6  christos       else if (vernum <= elf_tdata (abfd)->cverdefs)
   1921   1.6  christos 	version_string =
   1922   1.6  christos 	  elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
   1923   1.6  christos       else
   1924   1.6  christos 	{
   1925   1.6  christos 	  Elf_Internal_Verneed *t;
   1926  1.15  christos 
   1927   1.6  christos 	  version_string = _("<corrupt>");
   1928   1.6  christos 	  for (t = elf_tdata (abfd)->verref;
   1929   1.6  christos 	       t != NULL;
   1930   1.6  christos 	       t = t->vn_nextref)
   1931   1.6  christos 	    {
   1932   1.6  christos 	      Elf_Internal_Vernaux *a;
   1933   1.6  christos 
   1934   1.6  christos 	      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   1935   1.6  christos 		{
   1936   1.6  christos 		  if (a->vna_other == vernum)
   1937   1.6  christos 		    {
   1938   1.6  christos 		      version_string = a->vna_nodename;
   1939   1.6  christos 		      break;
   1940   1.6  christos 		    }
   1941   1.6  christos 		}
   1942   1.6  christos 	    }
   1943   1.6  christos 	}
   1944   1.6  christos     }
   1945   1.6  christos   return version_string;
   1946   1.6  christos }
   1947   1.1     skrll 
   1948   1.1     skrll /* Display ELF-specific fields of a symbol.  */
   1949   1.1     skrll 
   1950   1.1     skrll void
   1951   1.1     skrll bfd_elf_print_symbol (bfd *abfd,
   1952   1.1     skrll 		      void *filep,
   1953   1.1     skrll 		      asymbol *symbol,
   1954   1.1     skrll 		      bfd_print_symbol_type how)
   1955   1.3  christos {
   1956   1.1     skrll   FILE *file = (FILE *) filep;
   1957   1.1     skrll   switch (how)
   1958   1.1     skrll     {
   1959   1.1     skrll     case bfd_print_symbol_name:
   1960   1.1     skrll       fprintf (file, "%s", symbol->name);
   1961   1.1     skrll       break;
   1962   1.1     skrll     case bfd_print_symbol_more:
   1963   1.1     skrll       fprintf (file, "elf ");
   1964   1.9  christos       bfd_fprintf_vma (abfd, file, symbol->value);
   1965   1.1     skrll       fprintf (file, " %x", symbol->flags);
   1966   1.1     skrll       break;
   1967   1.1     skrll     case bfd_print_symbol_all:
   1968   1.1     skrll       {
   1969   1.1     skrll 	const char *section_name;
   1970   1.1     skrll 	const char *name = NULL;
   1971   1.1     skrll 	const struct elf_backend_data *bed;
   1972   1.1     skrll 	unsigned char st_other;
   1973   1.6  christos 	bfd_vma val;
   1974   1.6  christos 	const char *version_string;
   1975   1.1     skrll 	bfd_boolean hidden;
   1976   1.1     skrll 
   1977   1.1     skrll 	section_name = symbol->section ? symbol->section->name : "(*none*)";
   1978   1.1     skrll 
   1979   1.1     skrll 	bed = get_elf_backend_data (abfd);
   1980   1.1     skrll 	if (bed->elf_backend_print_symbol_all)
   1981   1.1     skrll 	  name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
   1982   1.1     skrll 
   1983   1.1     skrll 	if (name == NULL)
   1984   1.1     skrll 	  {
   1985   1.1     skrll 	    name = symbol->name;
   1986   1.1     skrll 	    bfd_print_symbol_vandf (abfd, file, symbol);
   1987   1.1     skrll 	  }
   1988   1.1     skrll 
   1989   1.1     skrll 	fprintf (file, " %s\t", section_name);
   1990   1.1     skrll 	/* Print the "other" value for a symbol.  For common symbols,
   1991   1.1     skrll 	   we've already printed the size; now print the alignment.
   1992   1.1     skrll 	   For other symbols, we have no specified alignment, and
   1993   1.1     skrll 	   we've printed the address; now print the size.  */
   1994   1.1     skrll 	if (symbol->section && bfd_is_com_section (symbol->section))
   1995   1.1     skrll 	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
   1996   1.1     skrll 	else
   1997   1.1     skrll 	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
   1998   1.1     skrll 	bfd_fprintf_vma (abfd, file, val);
   1999   1.1     skrll 
   2000   1.6  christos 	/* If we have version information, print it.  */
   2001   1.6  christos 	version_string = _bfd_elf_get_symbol_version_string (abfd,
   2002   1.6  christos 							     symbol,
   2003   1.6  christos 							     &hidden);
   2004   1.1     skrll 	if (version_string)
   2005   1.6  christos 	  {
   2006   1.1     skrll 	    if (!hidden)
   2007   1.1     skrll 	      fprintf (file, "  %-11s", version_string);
   2008   1.1     skrll 	    else
   2009   1.1     skrll 	      {
   2010   1.1     skrll 		int i;
   2011   1.1     skrll 
   2012   1.1     skrll 		fprintf (file, " (%s)", version_string);
   2013   1.1     skrll 		for (i = 10 - strlen (version_string); i > 0; --i)
   2014   1.1     skrll 		  putc (' ', file);
   2015   1.1     skrll 	      }
   2016   1.1     skrll 	  }
   2017   1.1     skrll 
   2018   1.1     skrll 	/* If the st_other field is not zero, print it.  */
   2019   1.1     skrll 	st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
   2020   1.1     skrll 
   2021   1.1     skrll 	switch (st_other)
   2022   1.1     skrll 	  {
   2023   1.1     skrll 	  case 0: break;
   2024   1.1     skrll 	  case STV_INTERNAL:  fprintf (file, " .internal");  break;
   2025   1.1     skrll 	  case STV_HIDDEN:    fprintf (file, " .hidden");    break;
   2026   1.1     skrll 	  case STV_PROTECTED: fprintf (file, " .protected"); break;
   2027   1.1     skrll 	  default:
   2028   1.1     skrll 	    /* Some other non-defined flags are also present, so print
   2029   1.1     skrll 	       everything hex.  */
   2030   1.1     skrll 	    fprintf (file, " 0x%02x", (unsigned int) st_other);
   2031   1.1     skrll 	  }
   2032   1.1     skrll 
   2033   1.1     skrll 	fprintf (file, " %s", name);
   2034   1.1     skrll       }
   2035   1.1     skrll       break;
   2036   1.1     skrll     }
   2037   1.1     skrll }
   2038   1.1     skrll 
   2039   1.1     skrll /* ELF .o/exec file reading */
   2041   1.1     skrll 
   2042   1.1     skrll /* Create a new bfd section from an ELF section header.  */
   2043   1.1     skrll 
   2044   1.1     skrll bfd_boolean
   2045   1.1     skrll bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
   2046   1.1     skrll {
   2047   1.1     skrll   Elf_Internal_Shdr *hdr;
   2048   1.1     skrll   Elf_Internal_Ehdr *ehdr;
   2049   1.6  christos   const struct elf_backend_data *bed;
   2050   1.6  christos   const char *name;
   2051   1.6  christos   bfd_boolean ret = TRUE;
   2052   1.6  christos   static bfd_boolean * sections_being_created = NULL;
   2053   1.1     skrll   static bfd * sections_being_created_abfd = NULL;
   2054   1.1     skrll   static unsigned int nesting = 0;
   2055   1.1     skrll 
   2056   1.1     skrll   if (shindex >= elf_numsections (abfd))
   2057   1.6  christos     return FALSE;
   2058   1.6  christos 
   2059   1.6  christos   if (++ nesting > 3)
   2060  1.15  christos     {
   2061   1.6  christos       /* PR17512: A corrupt ELF binary might contain a recursive group of
   2062   1.6  christos 	 sections, with each the string indices pointing to the next in the
   2063   1.6  christos 	 loop.  Detect this here, by refusing to load a section that we are
   2064   1.6  christos 	 already in the process of loading.  We only trigger this test if
   2065   1.6  christos 	 we have nested at least three sections deep as normal ELF binaries
   2066   1.6  christos 	 can expect to recurse at least once.
   2067   1.6  christos 
   2068   1.6  christos 	 FIXME: It would be better if this array was attached to the bfd,
   2069   1.6  christos 	 rather than being held in a static pointer.  */
   2070   1.6  christos 
   2071   1.6  christos       if (sections_being_created_abfd != abfd)
   2072   1.6  christos 	sections_being_created = NULL;
   2073   1.6  christos       if (sections_being_created == NULL)
   2074  1.15  christos 	{
   2075   1.6  christos 	  sections_being_created = (bfd_boolean *)
   2076   1.6  christos 	    bfd_zalloc2 (abfd, elf_numsections (abfd), sizeof (bfd_boolean));
   2077   1.6  christos 	  sections_being_created_abfd = abfd;
   2078   1.6  christos 	}
   2079   1.9  christos       if (sections_being_created [shindex])
   2080  1.13  christos 	{
   2081   1.6  christos 	  _bfd_error_handler
   2082   1.6  christos 	    (_("%pB: warning: loop in section dependencies detected"), abfd);
   2083   1.6  christos 	  return FALSE;
   2084   1.6  christos 	}
   2085   1.6  christos       sections_being_created [shindex] = TRUE;
   2086   1.1     skrll     }
   2087   1.1     skrll 
   2088   1.1     skrll   hdr = elf_elfsections (abfd)[shindex];
   2089   1.1     skrll   ehdr = elf_elfheader (abfd);
   2090   1.1     skrll   name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
   2091   1.6  christos 					  hdr->sh_name);
   2092   1.1     skrll   if (name == NULL)
   2093   1.1     skrll     goto fail;
   2094   1.1     skrll 
   2095   1.1     skrll   bed = get_elf_backend_data (abfd);
   2096   1.1     skrll   switch (hdr->sh_type)
   2097   1.1     skrll     {
   2098   1.6  christos     case SHT_NULL:
   2099   1.1     skrll       /* Inactive section. Throw it away.  */
   2100   1.6  christos       goto success;
   2101   1.6  christos 
   2102   1.6  christos     case SHT_PROGBITS:		/* Normal section with contents.  */
   2103   1.6  christos     case SHT_NOBITS:		/* .bss section.  */
   2104   1.1     skrll     case SHT_HASH:		/* .hash section.  */
   2105   1.1     skrll     case SHT_NOTE:		/* .note section.  */
   2106   1.1     skrll     case SHT_INIT_ARRAY:	/* .init_array section.  */
   2107   1.1     skrll     case SHT_FINI_ARRAY:	/* .fini_array section.  */
   2108   1.1     skrll     case SHT_PREINIT_ARRAY:	/* .preinit_array section.  */
   2109   1.6  christos     case SHT_GNU_LIBLIST:	/* .gnu.liblist section.  */
   2110   1.6  christos     case SHT_GNU_HASH:		/* .gnu.hash section.  */
   2111   1.1     skrll       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2112   1.1     skrll       goto success;
   2113   1.1     skrll 
   2114   1.6  christos     case SHT_DYNAMIC:	/* Dynamic linking information.  */
   2115   1.6  christos       if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2116   1.3  christos 	goto fail;
   2117   1.3  christos 
   2118   1.3  christos       if (hdr->sh_link > elf_numsections (abfd))
   2119   1.3  christos 	{
   2120   1.3  christos 	  /* PR 10478: Accept Solaris binaries with a sh_link
   2121   1.3  christos 	     field set to SHN_BEFORE or SHN_AFTER.  */
   2122   1.3  christos 	  switch (bfd_get_arch (abfd))
   2123   1.3  christos 	    {
   2124   1.3  christos 	    case bfd_arch_i386:
   2125   1.3  christos 	    case bfd_arch_sparc:
   2126   1.3  christos 	      if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
   2127   1.3  christos 		  || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
   2128   1.3  christos 		break;
   2129   1.6  christos 	      /* Otherwise fall through.  */
   2130   1.3  christos 	    default:
   2131   1.3  christos 	      goto fail;
   2132   1.3  christos 	    }
   2133   1.6  christos 	}
   2134   1.3  christos       else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
   2135   1.1     skrll 	goto fail;
   2136   1.1     skrll       else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
   2137   1.1     skrll 	{
   2138   1.1     skrll 	  Elf_Internal_Shdr *dynsymhdr;
   2139   1.1     skrll 
   2140   1.1     skrll 	  /* The shared libraries distributed with hpux11 have a bogus
   2141   1.1     skrll 	     sh_link field for the ".dynamic" section.  Find the
   2142   1.1     skrll 	     string table for the ".dynsym" section instead.  */
   2143   1.1     skrll 	  if (elf_dynsymtab (abfd) != 0)
   2144   1.1     skrll 	    {
   2145   1.1     skrll 	      dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
   2146   1.1     skrll 	      hdr->sh_link = dynsymhdr->sh_link;
   2147   1.1     skrll 	    }
   2148   1.1     skrll 	  else
   2149   1.1     skrll 	    {
   2150   1.1     skrll 	      unsigned int i, num_sec;
   2151   1.1     skrll 
   2152   1.1     skrll 	      num_sec = elf_numsections (abfd);
   2153   1.1     skrll 	      for (i = 1; i < num_sec; i++)
   2154   1.1     skrll 		{
   2155   1.1     skrll 		  dynsymhdr = elf_elfsections (abfd)[i];
   2156   1.1     skrll 		  if (dynsymhdr->sh_type == SHT_DYNSYM)
   2157   1.1     skrll 		    {
   2158   1.1     skrll 		      hdr->sh_link = dynsymhdr->sh_link;
   2159   1.1     skrll 		      break;
   2160   1.1     skrll 		    }
   2161   1.1     skrll 		}
   2162   1.6  christos 	    }
   2163   1.1     skrll 	}
   2164   1.6  christos       goto success;
   2165   1.1     skrll 
   2166   1.6  christos     case SHT_SYMTAB:		/* A symbol table.  */
   2167   1.1     skrll       if (elf_onesymtab (abfd) == shindex)
   2168   1.1     skrll 	goto success;
   2169   1.6  christos 
   2170   1.6  christos       if (hdr->sh_entsize != bed->s->sizeof_sym)
   2171   1.2     skrll 	goto fail;
   2172   1.5  christos 
   2173   1.5  christos       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
   2174   1.6  christos 	{
   2175   1.5  christos 	  if (hdr->sh_size != 0)
   2176   1.5  christos 	    goto fail;
   2177   1.5  christos 	  /* Some assemblers erroneously set sh_info to one with a
   2178   1.5  christos 	     zero sh_size.  ld sees this as a global symbol count
   2179   1.6  christos 	     of (unsigned) -1.  Fix it here.  */
   2180   1.6  christos 	  hdr->sh_info = 0;
   2181   1.6  christos 	  goto success;
   2182   1.6  christos 	}
   2183   1.6  christos 
   2184   1.6  christos       /* PR 18854: A binary might contain more than one symbol table.
   2185   1.6  christos 	 Unusual, but possible.  Warn, but continue.  */
   2186   1.9  christos       if (elf_onesymtab (abfd) != 0)
   2187   1.9  christos 	{
   2188  1.13  christos 	  _bfd_error_handler
   2189   1.9  christos 	    /* xgettext:c-format */
   2190   1.6  christos 	    (_("%pB: warning: multiple symbol tables detected"
   2191   1.6  christos 	       " - ignoring the table in section %u"),
   2192   1.5  christos 	     abfd, shindex);
   2193   1.1     skrll 	  goto success;
   2194   1.6  christos 	}
   2195   1.6  christos       elf_onesymtab (abfd) = shindex;
   2196   1.1     skrll       elf_symtab_hdr (abfd) = *hdr;
   2197   1.1     skrll       elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
   2198   1.1     skrll       abfd->flags |= HAS_SYMS;
   2199   1.1     skrll 
   2200   1.1     skrll       /* Sometimes a shared object will map in the symbol table.  If
   2201   1.1     skrll 	 SHF_ALLOC is set, and this is a shared object, then we also
   2202   1.1     skrll 	 treat this section as a BFD section.  We can not base the
   2203   1.1     skrll 	 decision purely on SHF_ALLOC, because that flag is sometimes
   2204   1.1     skrll 	 set in a relocatable object file, which would confuse the
   2205   1.1     skrll 	 linker.  */
   2206   1.1     skrll       if ((hdr->sh_flags & SHF_ALLOC) != 0
   2207   1.1     skrll 	  && (abfd->flags & DYNAMIC) != 0
   2208   1.6  christos 	  && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2209   1.1     skrll 						shindex))
   2210   1.1     skrll 	goto fail;
   2211   1.1     skrll 
   2212   1.1     skrll       /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
   2213   1.6  christos 	 can't read symbols without that section loaded as well.  It
   2214   1.6  christos 	 is most likely specified by the next section header.  */
   2215   1.6  christos       {
   2216   1.6  christos 	elf_section_list * entry;
   2217   1.6  christos 	unsigned int i, num_sec;
   2218   1.6  christos 
   2219   1.6  christos 	for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
   2220   1.6  christos 	  if (entry->hdr.sh_link == shindex)
   2221   1.6  christos 	    goto success;
   2222   1.6  christos 
   2223   1.6  christos 	num_sec = elf_numsections (abfd);
   2224   1.6  christos 	for (i = shindex + 1; i < num_sec; i++)
   2225   1.6  christos 	  {
   2226   1.6  christos 	    Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2227   1.6  christos 
   2228   1.6  christos 	    if (hdr2->sh_type == SHT_SYMTAB_SHNDX
   2229   1.6  christos 		&& hdr2->sh_link == shindex)
   2230   1.1     skrll 	      break;
   2231   1.6  christos 	  }
   2232   1.6  christos 
   2233   1.1     skrll 	if (i == num_sec)
   2234   1.1     skrll 	  for (i = 1; i < shindex; i++)
   2235   1.6  christos 	    {
   2236   1.1     skrll 	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2237   1.1     skrll 
   2238   1.1     skrll 	      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
   2239   1.1     skrll 		  && hdr2->sh_link == shindex)
   2240   1.1     skrll 		break;
   2241   1.6  christos 	    }
   2242   1.6  christos 
   2243   1.6  christos 	if (i != shindex)
   2244   1.6  christos 	  ret = bfd_section_from_shdr (abfd, i);
   2245   1.6  christos 	/* else FIXME: we have failed to find the symbol table - should we issue an error ? */
   2246   1.6  christos 	goto success;
   2247   1.6  christos       }
   2248   1.1     skrll 
   2249   1.6  christos     case SHT_DYNSYM:		/* A dynamic symbol table.  */
   2250   1.1     skrll       if (elf_dynsymtab (abfd) == shindex)
   2251   1.1     skrll 	goto success;
   2252   1.6  christos 
   2253   1.6  christos       if (hdr->sh_entsize != bed->s->sizeof_sym)
   2254   1.5  christos 	goto fail;
   2255   1.5  christos 
   2256   1.5  christos       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
   2257   1.6  christos 	{
   2258   1.6  christos 	  if (hdr->sh_size != 0)
   2259   1.5  christos 	    goto fail;
   2260   1.5  christos 
   2261   1.5  christos 	  /* Some linkers erroneously set sh_info to one with a
   2262   1.5  christos 	     zero sh_size.  ld sees this as a global symbol count
   2263   1.6  christos 	     of (unsigned) -1.  Fix it here.  */
   2264   1.6  christos 	  hdr->sh_info = 0;
   2265   1.6  christos 	  goto success;
   2266   1.6  christos 	}
   2267   1.6  christos 
   2268   1.6  christos       /* PR 18854: A binary might contain more than one dynamic symbol table.
   2269   1.6  christos 	 Unusual, but possible.  Warn, but continue.  */
   2270   1.9  christos       if (elf_dynsymtab (abfd) != 0)
   2271   1.9  christos 	{
   2272  1.13  christos 	  _bfd_error_handler
   2273   1.9  christos 	    /* xgettext:c-format */
   2274   1.6  christos 	    (_("%pB: warning: multiple dynamic symbol tables detected"
   2275   1.6  christos 	       " - ignoring the table in section %u"),
   2276   1.5  christos 	     abfd, shindex);
   2277   1.1     skrll 	  goto success;
   2278   1.1     skrll 	}
   2279   1.1     skrll       elf_dynsymtab (abfd) = shindex;
   2280   1.1     skrll       elf_tdata (abfd)->dynsymtab_hdr = *hdr;
   2281   1.1     skrll       elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   2282   1.1     skrll       abfd->flags |= HAS_SYMS;
   2283   1.1     skrll 
   2284   1.6  christos       /* Besides being a symbol table, we also treat this as a regular
   2285   1.6  christos 	 section, so that objcopy can handle it.  */
   2286   1.1     skrll       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2287   1.6  christos       goto success;
   2288   1.6  christos 
   2289   1.6  christos     case SHT_SYMTAB_SHNDX:	/* Symbol section indices when >64k sections.  */
   2290   1.1     skrll       {
   2291   1.6  christos 	elf_section_list * entry;
   2292   1.6  christos 
   2293   1.6  christos 	for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
   2294   1.9  christos 	  if (entry->ndx == shindex)
   2295  1.15  christos 	    goto success;
   2296   1.6  christos 
   2297   1.6  christos 	entry = bfd_alloc (abfd, sizeof (*entry));
   2298   1.6  christos 	if (entry == NULL)
   2299   1.6  christos 	  goto fail;
   2300   1.6  christos 	entry->ndx = shindex;
   2301   1.6  christos 	entry->hdr = * hdr;
   2302   1.6  christos 	entry->next = elf_symtab_shndx_list (abfd);
   2303   1.6  christos 	elf_symtab_shndx_list (abfd) = entry;
   2304   1.6  christos 	elf_elfsections (abfd)[shindex] = & entry->hdr;
   2305   1.1     skrll 	goto success;
   2306   1.6  christos       }
   2307   1.1     skrll 
   2308   1.6  christos     case SHT_STRTAB:		/* A string table.  */
   2309   1.6  christos       if (hdr->bfd_section != NULL)
   2310   1.1     skrll 	goto success;
   2311   1.1     skrll 
   2312   1.1     skrll       if (ehdr->e_shstrndx == shindex)
   2313   1.1     skrll 	{
   2314   1.6  christos 	  elf_tdata (abfd)->shstrtab_hdr = *hdr;
   2315   1.1     skrll 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
   2316   1.6  christos 	  goto success;
   2317   1.1     skrll 	}
   2318   1.1     skrll 
   2319   1.1     skrll       if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
   2320   1.1     skrll 	{
   2321   1.1     skrll 	symtab_strtab:
   2322   1.6  christos 	  elf_tdata (abfd)->strtab_hdr = *hdr;
   2323   1.1     skrll 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
   2324   1.6  christos 	  goto success;
   2325   1.1     skrll 	}
   2326   1.1     skrll 
   2327   1.1     skrll       if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
   2328   1.1     skrll 	{
   2329   1.1     skrll 	dynsymtab_strtab:
   2330   1.1     skrll 	  elf_tdata (abfd)->dynstrtab_hdr = *hdr;
   2331   1.1     skrll 	  hdr = &elf_tdata (abfd)->dynstrtab_hdr;
   2332   1.1     skrll 	  elf_elfsections (abfd)[shindex] = hdr;
   2333   1.6  christos 	  /* We also treat this as a regular section, so that objcopy
   2334   1.6  christos 	     can handle it.  */
   2335   1.6  christos 	  ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2336   1.1     skrll 						 shindex);
   2337   1.1     skrll 	  goto success;
   2338   1.1     skrll 	}
   2339   1.1     skrll 
   2340   1.1     skrll       /* If the string table isn't one of the above, then treat it as a
   2341   1.1     skrll 	 regular section.  We need to scan all the headers to be sure,
   2342   1.1     skrll 	 just in case this strtab section appeared before the above.  */
   2343   1.1     skrll       if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
   2344   1.1     skrll 	{
   2345   1.1     skrll 	  unsigned int i, num_sec;
   2346   1.1     skrll 
   2347   1.1     skrll 	  num_sec = elf_numsections (abfd);
   2348   1.1     skrll 	  for (i = 1; i < num_sec; i++)
   2349   1.1     skrll 	    {
   2350   1.1     skrll 	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2351   1.1     skrll 	      if (hdr2->sh_link == shindex)
   2352   1.1     skrll 		{
   2353   1.6  christos 		  /* Prevent endless recursion on broken objects.  */
   2354   1.1     skrll 		  if (i == shindex)
   2355   1.6  christos 		    goto fail;
   2356   1.1     skrll 		  if (! bfd_section_from_shdr (abfd, i))
   2357   1.1     skrll 		    goto fail;
   2358   1.1     skrll 		  if (elf_onesymtab (abfd) == i)
   2359   1.1     skrll 		    goto symtab_strtab;
   2360   1.1     skrll 		  if (elf_dynsymtab (abfd) == i)
   2361   1.1     skrll 		    goto dynsymtab_strtab;
   2362   1.1     skrll 		}
   2363   1.6  christos 	    }
   2364   1.6  christos 	}
   2365   1.1     skrll       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2366   1.1     skrll       goto success;
   2367   1.1     skrll 
   2368   1.1     skrll     case SHT_REL:
   2369   1.1     skrll     case SHT_RELA:
   2370   1.1     skrll       /* *These* do a lot of work -- but build no sections!  */
   2371   1.3  christos       {
   2372   1.1     skrll 	asection *target_sect;
   2373   1.3  christos 	Elf_Internal_Shdr *hdr2, **p_hdr;
   2374   1.1     skrll 	unsigned int num_sec = elf_numsections (abfd);
   2375   1.1     skrll 	struct bfd_elf_section_data *esdt;
   2376   1.1     skrll 
   2377   1.1     skrll 	if (hdr->sh_entsize
   2378   1.6  christos 	    != (bfd_size_type) (hdr->sh_type == SHT_REL
   2379   1.1     skrll 				? bed->s->sizeof_rel : bed->s->sizeof_rela))
   2380   1.1     skrll 	  goto fail;
   2381   1.1     skrll 
   2382   1.1     skrll 	/* Check for a bogus link to avoid crashing.  */
   2383   1.9  christos 	if (hdr->sh_link >= num_sec)
   2384   1.9  christos 	  {
   2385  1.13  christos 	    _bfd_error_handler
   2386   1.9  christos 	      /* xgettext:c-format */
   2387   1.6  christos 	      (_("%pB: invalid link %u for reloc section %s (index %u)"),
   2388   1.6  christos 	       abfd, hdr->sh_link, name, shindex);
   2389   1.6  christos 	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2390   1.1     skrll 						   shindex);
   2391   1.1     skrll 	    goto success;
   2392   1.1     skrll 	  }
   2393   1.1     skrll 
   2394   1.1     skrll 	/* For some incomprehensible reason Oracle distributes
   2395   1.1     skrll 	   libraries for Solaris in which some of the objects have
   2396   1.1     skrll 	   bogus sh_link fields.  It would be nice if we could just
   2397   1.1     skrll 	   reject them, but, unfortunately, some people need to use
   2398   1.3  christos 	   them.  We scan through the section headers; if we find only
   2399   1.3  christos 	   one suitable symbol table, we clobber the sh_link to point
   2400   1.3  christos 	   to it.  I hope this doesn't break anything.
   2401   1.3  christos 
   2402   1.3  christos 	   Don't do it on executable nor shared library.  */
   2403   1.1     skrll 	if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
   2404   1.1     skrll 	    && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
   2405   1.1     skrll 	    && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
   2406   1.1     skrll 	  {
   2407   1.1     skrll 	    unsigned int scan;
   2408   1.1     skrll 	    int found;
   2409   1.1     skrll 
   2410   1.1     skrll 	    found = 0;
   2411   1.1     skrll 	    for (scan = 1; scan < num_sec; scan++)
   2412   1.1     skrll 	      {
   2413   1.1     skrll 		if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
   2414   1.1     skrll 		    || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
   2415   1.1     skrll 		  {
   2416   1.1     skrll 		    if (found != 0)
   2417   1.1     skrll 		      {
   2418   1.1     skrll 			found = 0;
   2419   1.1     skrll 			break;
   2420   1.1     skrll 		      }
   2421   1.1     skrll 		    found = scan;
   2422   1.1     skrll 		  }
   2423   1.1     skrll 	      }
   2424   1.1     skrll 	    if (found != 0)
   2425   1.1     skrll 	      hdr->sh_link = found;
   2426   1.1     skrll 	  }
   2427   1.1     skrll 
   2428   1.1     skrll 	/* Get the symbol table.  */
   2429   1.1     skrll 	if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
   2430   1.6  christos 	     || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
   2431   1.1     skrll 	    && ! bfd_section_from_shdr (abfd, hdr->sh_link))
   2432  1.15  christos 	  goto fail;
   2433  1.15  christos 
   2434  1.15  christos 	/* If this is an alloc section in an executable or shared
   2435  1.15  christos 	   library, or the reloc section does not use the main symbol
   2436  1.15  christos 	   table we don't treat it as a reloc section.  BFD can't
   2437  1.15  christos 	   adequately represent such a section, so at least for now,
   2438  1.15  christos 	   we don't try.  We just present it as a normal section.  We
   2439  1.15  christos 	   also can't use it as a reloc section if it points to the
   2440  1.15  christos 	   null section, an invalid section, another reloc section, or
   2441  1.15  christos 	   its sh_link points to the null section.  */
   2442   1.3  christos 	if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
   2443  1.15  christos 	     && (hdr->sh_flags & SHF_ALLOC) != 0)
   2444   1.1     skrll 	    || hdr->sh_link == SHN_UNDEF
   2445   1.1     skrll 	    || hdr->sh_link != elf_onesymtab (abfd)
   2446   1.1     skrll 	    || hdr->sh_info == SHN_UNDEF
   2447   1.1     skrll 	    || hdr->sh_info >= num_sec
   2448   1.6  christos 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
   2449   1.6  christos 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
   2450   1.6  christos 	  {
   2451   1.6  christos 	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2452   1.6  christos 						   shindex);
   2453   1.1     skrll 	    goto success;
   2454   1.1     skrll 	  }
   2455   1.6  christos 
   2456   1.6  christos 	if (! bfd_section_from_shdr (abfd, hdr->sh_info))
   2457   1.1     skrll 	  goto fail;
   2458   1.1     skrll 
   2459   1.6  christos 	target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
   2460   1.1     skrll 	if (target_sect == NULL)
   2461   1.3  christos 	  goto fail;
   2462   1.3  christos 
   2463   1.3  christos 	esdt = elf_section_data (target_sect);
   2464   1.1     skrll 	if (hdr->sh_type == SHT_RELA)
   2465   1.3  christos 	  p_hdr = &esdt->rela.hdr;
   2466   1.3  christos 	else
   2467  1.15  christos 	  p_hdr = &esdt->rel.hdr;
   2468  1.15  christos 
   2469  1.15  christos 	/* PR 17512: file: 0b4f81b7.
   2470   1.6  christos 	   Also see PR 24456, for a file which deliberately has two reloc
   2471  1.15  christos 	   sections.  */
   2472  1.15  christos 	if (*p_hdr != NULL)
   2473  1.15  christos 	  {
   2474  1.15  christos 	    _bfd_error_handler
   2475  1.15  christos 	      /* xgettext:c-format */
   2476  1.15  christos 	      (_("%pB: warning: multiple relocation sections for section %pA \
   2477  1.15  christos found - ignoring all but the first"),
   2478  1.15  christos 	       abfd, target_sect);
   2479   1.7  christos 	    goto success;
   2480   1.3  christos 	  }
   2481   1.6  christos 	hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
   2482   1.1     skrll 	if (hdr2 == NULL)
   2483   1.3  christos 	  goto fail;
   2484   1.1     skrll 	*hdr2 = *hdr;
   2485   1.9  christos 	*p_hdr = hdr2;
   2486   1.9  christos 	elf_elfsections (abfd)[shindex] = hdr2;
   2487   1.1     skrll 	target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
   2488   1.1     skrll 				     * bed->s->int_rels_per_ext_rel);
   2489   1.1     skrll 	target_sect->flags |= SEC_RELOC;
   2490   1.1     skrll 	target_sect->relocation = NULL;
   2491   1.1     skrll 	target_sect->rel_filepos = hdr->sh_offset;
   2492   1.1     skrll 	/* In the section to which the relocations apply, mark whether
   2493   1.3  christos 	   its relocations are of the REL or RELA variety.  */
   2494   1.3  christos 	if (hdr->sh_size != 0)
   2495   1.3  christos 	  {
   2496   1.3  christos 	    if (hdr->sh_type == SHT_RELA)
   2497   1.1     skrll 	      target_sect->use_rela_p = 1;
   2498   1.6  christos 	  }
   2499   1.1     skrll 	abfd->flags |= HAS_RELOC;
   2500   1.1     skrll 	goto success;
   2501   1.1     skrll       }
   2502   1.1     skrll 
   2503   1.1     skrll     case SHT_GNU_verdef:
   2504   1.6  christos       elf_dynverdef (abfd) = shindex;
   2505   1.6  christos       elf_tdata (abfd)->dynverdef_hdr = *hdr;
   2506   1.1     skrll       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2507   1.1     skrll       goto success;
   2508   1.1     skrll 
   2509   1.6  christos     case SHT_GNU_versym:
   2510   1.6  christos       if (hdr->sh_entsize != sizeof (Elf_External_Versym))
   2511   1.1     skrll 	goto fail;
   2512   1.1     skrll 
   2513   1.6  christos       elf_dynversym (abfd) = shindex;
   2514   1.6  christos       elf_tdata (abfd)->dynversym_hdr = *hdr;
   2515   1.1     skrll       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2516   1.1     skrll       goto success;
   2517   1.1     skrll 
   2518   1.1     skrll     case SHT_GNU_verneed:
   2519   1.6  christos       elf_dynverref (abfd) = shindex;
   2520   1.6  christos       elf_tdata (abfd)->dynverref_hdr = *hdr;
   2521   1.1     skrll       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2522   1.1     skrll       goto success;
   2523   1.6  christos 
   2524   1.1     skrll     case SHT_SHLIB:
   2525   1.1     skrll       goto success;
   2526   1.6  christos 
   2527   1.6  christos     case SHT_GROUP:
   2528   1.6  christos       if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
   2529   1.1     skrll 	goto fail;
   2530   1.6  christos 
   2531   1.6  christos       if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2532   1.6  christos 	goto fail;
   2533   1.1     skrll 
   2534   1.1     skrll       goto success;
   2535   1.1     skrll 
   2536   1.1     skrll     default:
   2537   1.1     skrll       /* Possibly an attributes section.  */
   2538   1.1     skrll       if (hdr->sh_type == SHT_GNU_ATTRIBUTES
   2539   1.1     skrll 	  || hdr->sh_type == bed->obj_attrs_section_type)
   2540   1.6  christos 	{
   2541   1.1     skrll 	  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2542   1.6  christos 	    goto fail;
   2543   1.1     skrll 	  _bfd_elf_parse_attributes (abfd, hdr);
   2544   1.1     skrll 	  goto success;
   2545   1.1     skrll 	}
   2546   1.1     skrll 
   2547   1.6  christos       /* Check for any processor-specific section types.  */
   2548   1.1     skrll       if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
   2549   1.1     skrll 	goto success;
   2550   1.1     skrll 
   2551   1.1     skrll       if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
   2552   1.1     skrll 	{
   2553   1.1     skrll 	  if ((hdr->sh_flags & SHF_ALLOC) != 0)
   2554   1.9  christos 	    /* FIXME: How to properly handle allocated section reserved
   2555   1.9  christos 	       for applications?  */
   2556  1.13  christos 	    _bfd_error_handler
   2557   1.9  christos 	      /* xgettext:c-format */
   2558   1.1     skrll 	      (_("%pB: unknown type [%#x] section `%s'"),
   2559   1.6  christos 	       abfd, hdr->sh_type, name);
   2560   1.6  christos 	  else
   2561   1.6  christos 	    {
   2562   1.6  christos 	      /* Allow sections reserved for applications.  */
   2563   1.6  christos 	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2564   1.6  christos 						     shindex);
   2565   1.1     skrll 	      goto success;
   2566   1.1     skrll 	    }
   2567   1.1     skrll 	}
   2568   1.1     skrll       else if (hdr->sh_type >= SHT_LOPROC
   2569   1.9  christos 	       && hdr->sh_type <= SHT_HIPROC)
   2570   1.9  christos 	/* FIXME: We should handle this section.  */
   2571  1.13  christos 	_bfd_error_handler
   2572   1.9  christos 	  /* xgettext:c-format */
   2573   1.1     skrll 	  (_("%pB: unknown type [%#x] section `%s'"),
   2574   1.1     skrll 	   abfd, hdr->sh_type, name);
   2575   1.1     skrll       else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
   2576   1.1     skrll 	{
   2577   1.1     skrll 	  /* Unrecognised OS-specific sections.  */
   2578   1.1     skrll 	  if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
   2579   1.1     skrll 	    /* SHF_OS_NONCONFORMING indicates that special knowledge is
   2580   1.9  christos 	       required to correctly process the section and the file should
   2581   1.9  christos 	       be rejected with an error message.  */
   2582  1.13  christos 	    _bfd_error_handler
   2583   1.9  christos 	      /* xgettext:c-format */
   2584   1.1     skrll 	      (_("%pB: unknown type [%#x] section `%s'"),
   2585   1.6  christos 	       abfd, hdr->sh_type, name);
   2586   1.6  christos 	  else
   2587   1.6  christos 	    {
   2588   1.6  christos 	      /* Otherwise it should be processed.  */
   2589   1.6  christos 	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2590   1.1     skrll 	      goto success;
   2591   1.1     skrll 	    }
   2592   1.1     skrll 	}
   2593   1.9  christos       else
   2594   1.9  christos 	/* FIXME: We should handle this section.  */
   2595  1.13  christos 	_bfd_error_handler
   2596   1.9  christos 	  /* xgettext:c-format */
   2597   1.1     skrll 	  (_("%pB: unknown type [%#x] section `%s'"),
   2598   1.6  christos 	   abfd, hdr->sh_type, name);
   2599   1.1     skrll 
   2600   1.1     skrll       goto fail;
   2601   1.6  christos     }
   2602   1.6  christos 
   2603   1.6  christos  fail:
   2604   1.6  christos   ret = FALSE;
   2605   1.6  christos  success:
   2606   1.6  christos   if (sections_being_created && sections_being_created_abfd == abfd)
   2607   1.6  christos     sections_being_created [shindex] = FALSE;
   2608   1.6  christos   if (-- nesting == 0)
   2609   1.6  christos     {
   2610   1.6  christos       sections_being_created = NULL;
   2611   1.6  christos       sections_being_created_abfd = abfd;
   2612   1.1     skrll     }
   2613   1.1     skrll   return ret;
   2614   1.3  christos }
   2615   1.1     skrll 
   2616   1.3  christos /* Return the local symbol specified by ABFD, R_SYMNDX.  */
   2617   1.3  christos 
   2618   1.3  christos Elf_Internal_Sym *
   2619   1.3  christos bfd_sym_from_r_symndx (struct sym_cache *cache,
   2620   1.1     skrll 		       bfd *abfd,
   2621   1.1     skrll 		       unsigned long r_symndx)
   2622   1.1     skrll {
   2623   1.1     skrll   unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
   2624   1.1     skrll 
   2625   1.1     skrll   if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
   2626   1.1     skrll     {
   2627   1.1     skrll       Elf_Internal_Shdr *symtab_hdr;
   2628   1.1     skrll       unsigned char esym[sizeof (Elf64_External_Sym)];
   2629   1.1     skrll       Elf_External_Sym_Shndx eshndx;
   2630   1.1     skrll 
   2631   1.3  christos       symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   2632   1.1     skrll       if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
   2633   1.1     skrll 				&cache->sym[ent], esym, &eshndx) == NULL)
   2634   1.1     skrll 	return NULL;
   2635   1.1     skrll 
   2636   1.1     skrll       if (cache->abfd != abfd)
   2637   1.1     skrll 	{
   2638   1.1     skrll 	  memset (cache->indx, -1, sizeof (cache->indx));
   2639   1.1     skrll 	  cache->abfd = abfd;
   2640   1.1     skrll 	}
   2641   1.1     skrll       cache->indx[ent] = r_symndx;
   2642   1.3  christos     }
   2643   1.1     skrll 
   2644   1.1     skrll   return &cache->sym[ent];
   2645   1.1     skrll }
   2646   1.1     skrll 
   2647   1.1     skrll /* Given an ELF section number, retrieve the corresponding BFD
   2648   1.1     skrll    section.  */
   2649   1.3  christos 
   2650   1.1     skrll asection *
   2651   1.3  christos bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
   2652   1.1     skrll {
   2653   1.3  christos   if (sec_index >= elf_numsections (abfd))
   2654   1.1     skrll     return NULL;
   2655   1.1     skrll   return elf_elfsections (abfd)[sec_index]->bfd_section;
   2656   1.1     skrll }
   2657   1.1     skrll 
   2658   1.1     skrll static const struct bfd_elf_special_section special_sections_b[] =
   2659   1.9  christos {
   2660   1.1     skrll   { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   2661   1.1     skrll   { NULL,		    0,	0, 0,		 0 }
   2662   1.1     skrll };
   2663   1.1     skrll 
   2664   1.1     skrll static const struct bfd_elf_special_section special_sections_c[] =
   2665  1.15  christos {
   2666   1.9  christos   { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
   2667   1.1     skrll   { STRING_COMMA_LEN (".ctf"),	0, SHT_PROGBITS,    0 },
   2668   1.1     skrll   { NULL,			0, 0, 0,	    0 }
   2669   1.1     skrll };
   2670   1.1     skrll 
   2671   1.9  christos static const struct bfd_elf_special_section special_sections_d[] =
   2672   1.9  christos {
   2673   1.6  christos   { STRING_COMMA_LEN (".data"),		-2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2674   1.6  christos   { STRING_COMMA_LEN (".data1"),	 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2675   1.6  christos   /* There are more DWARF sections than these, but they needn't be added here
   2676   1.9  christos      unless you have to cope with broken compilers that don't emit section
   2677   1.9  christos      attributes or you want to help the user writing assembler.  */
   2678   1.9  christos   { STRING_COMMA_LEN (".debug"),	 0, SHT_PROGBITS, 0 },
   2679   1.9  christos   { STRING_COMMA_LEN (".debug_line"),	 0, SHT_PROGBITS, 0 },
   2680   1.1     skrll   { STRING_COMMA_LEN (".debug_info"),	 0, SHT_PROGBITS, 0 },
   2681   1.9  christos   { STRING_COMMA_LEN (".debug_abbrev"),	 0, SHT_PROGBITS, 0 },
   2682   1.9  christos   { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
   2683   1.9  christos   { STRING_COMMA_LEN (".dynamic"),	 0, SHT_DYNAMIC,  SHF_ALLOC },
   2684   1.9  christos   { STRING_COMMA_LEN (".dynstr"),	 0, SHT_STRTAB,	  SHF_ALLOC },
   2685   1.1     skrll   { STRING_COMMA_LEN (".dynsym"),	 0, SHT_DYNSYM,	  SHF_ALLOC },
   2686   1.1     skrll   { NULL,		       0,	 0, 0,		  0 }
   2687   1.1     skrll };
   2688   1.1     skrll 
   2689   1.9  christos static const struct bfd_elf_special_section special_sections_f[] =
   2690   1.9  christos {
   2691   1.9  christos   { STRING_COMMA_LEN (".fini"),	       0, SHT_PROGBITS,	  SHF_ALLOC + SHF_EXECINSTR },
   2692   1.1     skrll   { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
   2693   1.1     skrll   { NULL,			   0 , 0, 0,		  0 }
   2694   1.1     skrll };
   2695   1.1     skrll 
   2696   1.1     skrll static const struct bfd_elf_special_section special_sections_g[] =
   2697   1.9  christos {
   2698   1.9  christos   { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
   2699   1.9  christos   { STRING_COMMA_LEN (".gnu.lto_"),	  -1, SHT_PROGBITS,    SHF_EXCLUDE },
   2700   1.1     skrll   { STRING_COMMA_LEN (".got"),		   0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
   2701   1.1     skrll   { STRING_COMMA_LEN (".gnu.version"),	   0, SHT_GNU_versym,  0 },
   2702   1.9  christos   { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
   2703   1.9  christos   { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
   2704   1.9  christos   { STRING_COMMA_LEN (".gnu.liblist"),	   0, SHT_GNU_LIBLIST, SHF_ALLOC },
   2705   1.9  christos   { STRING_COMMA_LEN (".gnu.conflict"),	   0, SHT_RELA,	       SHF_ALLOC },
   2706   1.1     skrll   { STRING_COMMA_LEN (".gnu.hash"),	   0, SHT_GNU_HASH,    SHF_ALLOC },
   2707   1.1     skrll   { NULL,			 0,	   0, 0,	       0 }
   2708   1.1     skrll };
   2709   1.1     skrll 
   2710   1.9  christos static const struct bfd_elf_special_section special_sections_h[] =
   2711   1.9  christos {
   2712   1.1     skrll   { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,	 SHF_ALLOC },
   2713   1.1     skrll   { NULL,		     0, 0, 0,		 0 }
   2714   1.1     skrll };
   2715   1.1     skrll 
   2716   1.9  christos static const struct bfd_elf_special_section special_sections_i[] =
   2717   1.9  christos {
   2718   1.9  christos   { STRING_COMMA_LEN (".init"),	       0, SHT_PROGBITS,	  SHF_ALLOC + SHF_EXECINSTR },
   2719   1.9  christos   { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
   2720   1.1     skrll   { STRING_COMMA_LEN (".interp"),      0, SHT_PROGBITS,	  0 },
   2721   1.1     skrll   { NULL,		       0,      0, 0,		  0 }
   2722   1.1     skrll };
   2723   1.1     skrll 
   2724   1.1     skrll static const struct bfd_elf_special_section special_sections_l[] =
   2725   1.9  christos {
   2726   1.1     skrll   { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
   2727   1.1     skrll   { NULL,		     0, 0, 0,		 0 }
   2728   1.1     skrll };
   2729   1.1     skrll 
   2730   1.1     skrll static const struct bfd_elf_special_section special_sections_n[] =
   2731   1.9  christos {
   2732   1.9  christos   { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
   2733   1.1     skrll   { STRING_COMMA_LEN (".note"),		 -1, SHT_NOTE,	   0 },
   2734   1.1     skrll   { NULL,		     0,		  0, 0,		   0 }
   2735   1.1     skrll };
   2736   1.1     skrll 
   2737   1.9  christos static const struct bfd_elf_special_section special_sections_p[] =
   2738   1.9  christos {
   2739   1.9  christos   { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
   2740   1.1     skrll   { STRING_COMMA_LEN (".plt"),		  0, SHT_PROGBITS,	SHF_ALLOC + SHF_EXECINSTR },
   2741   1.1     skrll   { NULL,		    0,		  0, 0,			0 }
   2742   1.1     skrll };
   2743   1.1     skrll 
   2744   1.1     skrll static const struct bfd_elf_special_section special_sections_r[] =
   2745   1.1     skrll {
   2746   1.9  christos   { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
   2747   1.9  christos   { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
   2748   1.9  christos   { STRING_COMMA_LEN (".rela"),	  -1, SHT_RELA,	    0 },
   2749   1.1     skrll   { STRING_COMMA_LEN (".rel"),	  -1, SHT_REL,	    0 },
   2750   1.1     skrll   { NULL,		    0,	   0, 0,	    0 }
   2751   1.1     skrll };
   2752   1.1     skrll 
   2753   1.1     skrll static const struct bfd_elf_special_section special_sections_s[] =
   2754   1.1     skrll {
   2755   1.1     skrll   { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
   2756   1.1     skrll   { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
   2757   1.1     skrll   { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
   2758   1.1     skrll   /* See struct bfd_elf_special_section declaration for the semantics of
   2759   1.9  christos      this special case where .prefix_length != strlen (.prefix).  */
   2760   1.1     skrll   { ".stabstr",			5,  3, SHT_STRTAB, 0 },
   2761   1.1     skrll   { NULL,			0,  0, 0,	   0 }
   2762   1.1     skrll };
   2763   1.1     skrll 
   2764   1.9  christos static const struct bfd_elf_special_section special_sections_t[] =
   2765   1.9  christos {
   2766   1.1     skrll   { STRING_COMMA_LEN (".text"),	 -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
   2767   1.9  christos   { STRING_COMMA_LEN (".tbss"),	 -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
   2768   1.1     skrll   { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
   2769   1.1     skrll   { NULL,		      0,  0, 0,		   0 }
   2770   1.1     skrll };
   2771   1.1     skrll 
   2772   1.9  christos static const struct bfd_elf_special_section special_sections_z[] =
   2773   1.9  christos {
   2774   1.1     skrll   { STRING_COMMA_LEN (".zdebug_line"),	  0, SHT_PROGBITS, 0 },
   2775   1.1     skrll   { STRING_COMMA_LEN (".zdebug_info"),	  0, SHT_PROGBITS, 0 },
   2776   1.9  christos   { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
   2777   1.1     skrll   { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
   2778   1.1     skrll   { NULL,		      0,  0, 0,		   0 }
   2779   1.5  christos };
   2780   1.1     skrll 
   2781   1.1     skrll static const struct bfd_elf_special_section * const special_sections[] =
   2782   1.1     skrll {
   2783   1.1     skrll   special_sections_b,		/* 'b' */
   2784   1.1     skrll   special_sections_c,		/* 'c' */
   2785   1.1     skrll   special_sections_d,		/* 'd' */
   2786   1.1     skrll   NULL,				/* 'e' */
   2787   1.1     skrll   special_sections_f,		/* 'f' */
   2788   1.1     skrll   special_sections_g,		/* 'g' */
   2789   1.1     skrll   special_sections_h,		/* 'h' */
   2790   1.1     skrll   special_sections_i,		/* 'i' */
   2791   1.1     skrll   NULL,				/* 'j' */
   2792   1.1     skrll   NULL,				/* 'k' */
   2793   1.1     skrll   special_sections_l,		/* 'l' */
   2794   1.1     skrll   NULL,				/* 'm' */
   2795   1.1     skrll   special_sections_n,		/* 'n' */
   2796   1.1     skrll   NULL,				/* 'o' */
   2797   1.1     skrll   special_sections_p,		/* 'p' */
   2798   1.1     skrll   NULL,				/* 'q' */
   2799   1.1     skrll   special_sections_r,		/* 'r' */
   2800   1.1     skrll   special_sections_s,		/* 's' */
   2801   1.1     skrll   special_sections_t,		/* 't' */
   2802   1.1     skrll   NULL,				/* 'u' */
   2803   1.1     skrll   NULL,				/* 'v' */
   2804   1.1     skrll   NULL,				/* 'w' */
   2805   1.1     skrll   NULL,				/* 'x' */
   2806   1.1     skrll   NULL,				/* 'y' */
   2807   1.1     skrll   special_sections_z		/* 'z' */
   2808   1.1     skrll };
   2809   1.1     skrll 
   2810   1.1     skrll const struct bfd_elf_special_section *
   2811   1.1     skrll _bfd_elf_get_special_section (const char *name,
   2812   1.1     skrll 			      const struct bfd_elf_special_section *spec,
   2813   1.1     skrll 			      unsigned int rela)
   2814   1.1     skrll {
   2815   1.1     skrll   int i;
   2816   1.1     skrll   int len;
   2817   1.1     skrll 
   2818   1.1     skrll   len = strlen (name);
   2819   1.1     skrll 
   2820   1.1     skrll   for (i = 0; spec[i].prefix != NULL; i++)
   2821   1.1     skrll     {
   2822   1.1     skrll       int suffix_len;
   2823   1.1     skrll       int prefix_len = spec[i].prefix_length;
   2824   1.1     skrll 
   2825   1.1     skrll       if (len < prefix_len)
   2826   1.1     skrll 	continue;
   2827   1.1     skrll       if (memcmp (name, spec[i].prefix, prefix_len) != 0)
   2828   1.1     skrll 	continue;
   2829   1.1     skrll 
   2830   1.1     skrll       suffix_len = spec[i].suffix_length;
   2831   1.1     skrll       if (suffix_len <= 0)
   2832   1.1     skrll 	{
   2833   1.1     skrll 	  if (name[prefix_len] != 0)
   2834   1.1     skrll 	    {
   2835   1.1     skrll 	      if (suffix_len == 0)
   2836   1.1     skrll 		continue;
   2837   1.1     skrll 	      if (name[prefix_len] != '.'
   2838   1.1     skrll 		  && (suffix_len == -2
   2839   1.1     skrll 		      || (rela && spec[i].type == SHT_REL)))
   2840   1.1     skrll 		continue;
   2841   1.1     skrll 	    }
   2842   1.1     skrll 	}
   2843   1.1     skrll       else
   2844   1.1     skrll 	{
   2845   1.1     skrll 	  if (len < prefix_len + suffix_len)
   2846   1.1     skrll 	    continue;
   2847   1.1     skrll 	  if (memcmp (name + len - suffix_len,
   2848   1.1     skrll 		      spec[i].prefix + prefix_len,
   2849   1.1     skrll 		      suffix_len) != 0)
   2850   1.1     skrll 	    continue;
   2851   1.1     skrll 	}
   2852   1.1     skrll       return &spec[i];
   2853   1.1     skrll     }
   2854   1.1     skrll 
   2855   1.1     skrll   return NULL;
   2856   1.1     skrll }
   2857   1.1     skrll 
   2858   1.1     skrll const struct bfd_elf_special_section *
   2859   1.1     skrll _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
   2860   1.1     skrll {
   2861   1.1     skrll   int i;
   2862   1.1     skrll   const struct bfd_elf_special_section *spec;
   2863   1.1     skrll   const struct elf_backend_data *bed;
   2864   1.1     skrll 
   2865   1.1     skrll   /* See if this is one of the special sections.  */
   2866   1.1     skrll   if (sec->name == NULL)
   2867   1.1     skrll     return NULL;
   2868   1.1     skrll 
   2869   1.1     skrll   bed = get_elf_backend_data (abfd);
   2870   1.1     skrll   spec = bed->special_sections;
   2871   1.1     skrll   if (spec)
   2872   1.1     skrll     {
   2873   1.1     skrll       spec = _bfd_elf_get_special_section (sec->name,
   2874   1.1     skrll 					   bed->special_sections,
   2875   1.1     skrll 					   sec->use_rela_p);
   2876   1.1     skrll       if (spec != NULL)
   2877   1.1     skrll 	return spec;
   2878   1.1     skrll     }
   2879   1.1     skrll 
   2880   1.1     skrll   if (sec->name[0] != '.')
   2881   1.1     skrll     return NULL;
   2882   1.1     skrll 
   2883   1.1     skrll   i = sec->name[1] - 'b';
   2884   1.1     skrll   if (i < 0 || i > 'z' - 'b')
   2885   1.1     skrll     return NULL;
   2886   1.1     skrll 
   2887   1.1     skrll   spec = special_sections[i];
   2888   1.1     skrll 
   2889   1.1     skrll   if (spec == NULL)
   2890   1.1     skrll     return NULL;
   2891   1.1     skrll 
   2892   1.1     skrll   return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
   2893   1.1     skrll }
   2894   1.1     skrll 
   2895   1.1     skrll bfd_boolean
   2896   1.1     skrll _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
   2897   1.1     skrll {
   2898   1.1     skrll   struct bfd_elf_section_data *sdata;
   2899   1.1     skrll   const struct elf_backend_data *bed;
   2900   1.1     skrll   const struct bfd_elf_special_section *ssect;
   2901   1.1     skrll 
   2902   1.1     skrll   sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
   2903   1.3  christos   if (sdata == NULL)
   2904   1.9  christos     {
   2905   1.1     skrll       sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
   2906   1.1     skrll 							  sizeof (*sdata));
   2907   1.1     skrll       if (sdata == NULL)
   2908   1.1     skrll 	return FALSE;
   2909   1.1     skrll       sec->used_by_bfd = sdata;
   2910   1.1     skrll     }
   2911   1.1     skrll 
   2912   1.1     skrll   /* Indicate whether or not this section should use RELA relocations.  */
   2913   1.1     skrll   bed = get_elf_backend_data (abfd);
   2914   1.1     skrll   sec->use_rela_p = bed->default_use_rela_p;
   2915   1.1     skrll 
   2916   1.1     skrll   /* When we read a file, we don't need to set ELF section type and
   2917   1.1     skrll      flags.  They will be overridden in _bfd_elf_make_section_from_shdr
   2918   1.1     skrll      anyway.  We will set ELF section type and flags for all linker
   2919   1.5  christos      created sections.  If user specifies BFD section flags, we will
   2920   1.5  christos      set ELF section type and flags based on BFD section flags in
   2921   1.5  christos      elf_fake_sections.  Special handling for .init_array/.fini_array
   2922   1.5  christos      output sections since they may contain .ctors/.dtors input
   2923   1.5  christos      sections.  We don't want _bfd_elf_init_private_section_data to
   2924   1.1     skrll      copy ELF section type from .ctors/.dtors input sections.  */
   2925   1.1     skrll   if (abfd->direction != read_direction
   2926   1.1     skrll       || (sec->flags & SEC_LINKER_CREATED) != 0)
   2927   1.5  christos     {
   2928   1.5  christos       ssect = (*bed->get_sec_type_attr) (abfd, sec);
   2929   1.5  christos       if (ssect != NULL
   2930   1.5  christos 	  && (!sec->flags
   2931   1.5  christos 	      || (sec->flags & SEC_LINKER_CREATED) != 0
   2932   1.1     skrll 	      || ssect->type == SHT_INIT_ARRAY
   2933   1.1     skrll 	      || ssect->type == SHT_FINI_ARRAY))
   2934   1.1     skrll 	{
   2935   1.1     skrll 	  elf_section_type (sec) = ssect->type;
   2936   1.1     skrll 	  elf_section_flags (sec) = ssect->attr;
   2937   1.1     skrll 	}
   2938   1.1     skrll     }
   2939   1.1     skrll 
   2940   1.1     skrll   return _bfd_generic_new_section_hook (abfd, sec);
   2941   1.1     skrll }
   2942   1.1     skrll 
   2943   1.1     skrll /* Create a new bfd section from an ELF program header.
   2944   1.1     skrll 
   2945   1.1     skrll    Since program segments have no names, we generate a synthetic name
   2946   1.1     skrll    of the form segment<NUM>, where NUM is generally the index in the
   2947   1.1     skrll    program header table.  For segments that are split (see below) we
   2948   1.1     skrll    generate the names segment<NUM>a and segment<NUM>b.
   2949   1.1     skrll 
   2950   1.1     skrll    Note that some program segments may have a file size that is different than
   2951   1.1     skrll    (less than) the memory size.  All this means is that at execution the
   2952   1.1     skrll    system must allocate the amount of memory specified by the memory size,
   2953   1.1     skrll    but only initialize it with the first "file size" bytes read from the
   2954   1.1     skrll    file.  This would occur for example, with program segments consisting
   2955   1.1     skrll    of combined data+bss.
   2956   1.1     skrll 
   2957   1.1     skrll    To handle the above situation, this routine generates TWO bfd sections
   2958   1.1     skrll    for the single program segment.  The first has the length specified by
   2959   1.1     skrll    the file size of the segment, and the second has the length specified
   2960   1.1     skrll    by the difference between the two sizes.  In effect, the segment is split
   2961   1.1     skrll    into its initialized and uninitialized parts.
   2962   1.1     skrll 
   2963   1.1     skrll  */
   2964   1.1     skrll 
   2965   1.1     skrll bfd_boolean
   2966   1.3  christos _bfd_elf_make_section_from_phdr (bfd *abfd,
   2967   1.3  christos 				 Elf_Internal_Phdr *hdr,
   2968   1.1     skrll 				 int hdr_index,
   2969   1.1     skrll 				 const char *type_name)
   2970   1.1     skrll {
   2971   1.1     skrll   asection *newsect;
   2972   1.1     skrll   char *name;
   2973   1.1     skrll   char namebuf[64];
   2974   1.1     skrll   size_t len;
   2975   1.1     skrll   int split;
   2976   1.1     skrll 
   2977   1.1     skrll   split = ((hdr->p_memsz > 0)
   2978   1.1     skrll 	    && (hdr->p_filesz > 0)
   2979   1.1     skrll 	    && (hdr->p_memsz > hdr->p_filesz));
   2980   1.1     skrll 
   2981   1.3  christos   if (hdr->p_filesz > 0)
   2982   1.1     skrll     {
   2983   1.3  christos       sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
   2984   1.1     skrll       len = strlen (namebuf) + 1;
   2985   1.1     skrll       name = (char *) bfd_alloc (abfd, len);
   2986   1.1     skrll       if (!name)
   2987   1.1     skrll 	return FALSE;
   2988   1.1     skrll       memcpy (name, namebuf, len);
   2989   1.1     skrll       newsect = bfd_make_section (abfd, name);
   2990   1.1     skrll       if (newsect == NULL)
   2991   1.1     skrll 	return FALSE;
   2992   1.1     skrll       newsect->vma = hdr->p_vaddr;
   2993   1.1     skrll       newsect->lma = hdr->p_paddr;
   2994   1.1     skrll       newsect->size = hdr->p_filesz;
   2995   1.1     skrll       newsect->filepos = hdr->p_offset;
   2996   1.1     skrll       newsect->flags |= SEC_HAS_CONTENTS;
   2997   1.1     skrll       newsect->alignment_power = bfd_log2 (hdr->p_align);
   2998   1.1     skrll       if (hdr->p_type == PT_LOAD)
   2999   1.1     skrll 	{
   3000   1.1     skrll 	  newsect->flags |= SEC_ALLOC;
   3001   1.1     skrll 	  newsect->flags |= SEC_LOAD;
   3002   1.1     skrll 	  if (hdr->p_flags & PF_X)
   3003   1.1     skrll 	    {
   3004   1.1     skrll 	      /* FIXME: all we known is that it has execute PERMISSION,
   3005   1.1     skrll 		 may be data.  */
   3006   1.1     skrll 	      newsect->flags |= SEC_CODE;
   3007   1.1     skrll 	    }
   3008   1.1     skrll 	}
   3009   1.1     skrll       if (!(hdr->p_flags & PF_W))
   3010   1.1     skrll 	{
   3011   1.1     skrll 	  newsect->flags |= SEC_READONLY;
   3012   1.1     skrll 	}
   3013   1.1     skrll     }
   3014   1.1     skrll 
   3015   1.1     skrll   if (hdr->p_memsz > hdr->p_filesz)
   3016   1.1     skrll     {
   3017   1.3  christos       bfd_vma align;
   3018   1.1     skrll 
   3019   1.3  christos       sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
   3020   1.1     skrll       len = strlen (namebuf) + 1;
   3021   1.1     skrll       name = (char *) bfd_alloc (abfd, len);
   3022   1.1     skrll       if (!name)
   3023   1.1     skrll 	return FALSE;
   3024   1.1     skrll       memcpy (name, namebuf, len);
   3025   1.1     skrll       newsect = bfd_make_section (abfd, name);
   3026   1.1     skrll       if (newsect == NULL)
   3027   1.1     skrll 	return FALSE;
   3028   1.1     skrll       newsect->vma = hdr->p_vaddr + hdr->p_filesz;
   3029   1.1     skrll       newsect->lma = hdr->p_paddr + hdr->p_filesz;
   3030   1.1     skrll       newsect->size = hdr->p_memsz - hdr->p_filesz;
   3031   1.1     skrll       newsect->filepos = hdr->p_offset + hdr->p_filesz;
   3032   1.1     skrll       align = newsect->vma & -newsect->vma;
   3033   1.1     skrll       if (align == 0 || align > hdr->p_align)
   3034   1.1     skrll 	align = hdr->p_align;
   3035   1.1     skrll       newsect->alignment_power = bfd_log2 (align);
   3036   1.1     skrll       if (hdr->p_type == PT_LOAD)
   3037   1.1     skrll 	{
   3038   1.1     skrll 	  /* Hack for gdb.  Segments that have not been modified do
   3039   1.1     skrll 	     not have their contents written to a core file, on the
   3040   1.1     skrll 	     assumption that a debugger can find the contents in the
   3041   1.1     skrll 	     executable.  We flag this case by setting the fake
   3042   1.1     skrll 	     section size to zero.  Note that "real" bss sections will
   3043   1.1     skrll 	     always have their contents dumped to the core file.  */
   3044   1.1     skrll 	  if (bfd_get_format (abfd) == bfd_core)
   3045   1.1     skrll 	    newsect->size = 0;
   3046   1.1     skrll 	  newsect->flags |= SEC_ALLOC;
   3047   1.1     skrll 	  if (hdr->p_flags & PF_X)
   3048   1.1     skrll 	    newsect->flags |= SEC_CODE;
   3049   1.1     skrll 	}
   3050   1.1     skrll       if (!(hdr->p_flags & PF_W))
   3051   1.1     skrll 	newsect->flags |= SEC_READONLY;
   3052   1.1     skrll     }
   3053   1.1     skrll 
   3054   1.1     skrll   return TRUE;
   3055  1.15  christos }
   3056  1.15  christos 
   3057  1.15  christos static bfd_boolean
   3058  1.15  christos _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
   3059  1.15  christos {
   3060  1.15  christos   /* The return value is ignored.  Build-ids are considered optional.  */
   3061  1.15  christos   if (templ->xvec->flavour == bfd_target_elf_flavour)
   3062  1.15  christos     return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
   3063  1.15  christos       (templ, offset);
   3064  1.15  christos   return FALSE;
   3065   1.1     skrll }
   3066   1.3  christos 
   3067   1.1     skrll bfd_boolean
   3068   1.1     skrll bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
   3069   1.1     skrll {
   3070   1.1     skrll   const struct elf_backend_data *bed;
   3071   1.1     skrll 
   3072   1.1     skrll   switch (hdr->p_type)
   3073   1.3  christos     {
   3074   1.1     skrll     case PT_NULL:
   3075   1.1     skrll       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
   3076  1.15  christos 
   3077  1.15  christos     case PT_LOAD:
   3078  1.15  christos       if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
   3079  1.15  christos 	return FALSE;
   3080  1.15  christos       if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
   3081   1.1     skrll 	_bfd_elf_core_find_build_id (abfd, hdr->p_offset);
   3082   1.1     skrll       return TRUE;
   3083   1.3  christos 
   3084   1.1     skrll     case PT_DYNAMIC:
   3085   1.1     skrll       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
   3086   1.3  christos 
   3087   1.1     skrll     case PT_INTERP:
   3088   1.1     skrll       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
   3089   1.3  christos 
   3090   1.1     skrll     case PT_NOTE:
   3091   1.9  christos       if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
   3092   1.9  christos 	return FALSE;
   3093   1.1     skrll       if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
   3094   1.1     skrll 			    hdr->p_align))
   3095   1.1     skrll 	return FALSE;
   3096   1.1     skrll       return TRUE;
   3097   1.3  christos 
   3098   1.1     skrll     case PT_SHLIB:
   3099   1.1     skrll       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
   3100   1.3  christos 
   3101   1.1     skrll     case PT_PHDR:
   3102   1.1     skrll       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
   3103   1.3  christos 
   3104   1.1     skrll     case PT_GNU_EH_FRAME:
   3105   1.1     skrll       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
   3106   1.1     skrll 					      "eh_frame_hdr");
   3107   1.3  christos 
   3108   1.1     skrll     case PT_GNU_STACK:
   3109   1.1     skrll       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
   3110   1.3  christos 
   3111   1.1     skrll     case PT_GNU_RELRO:
   3112   1.1     skrll       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
   3113   1.1     skrll 
   3114   1.1     skrll     default:
   3115   1.3  christos       /* Check for any processor-specific program segment types.  */
   3116   1.1     skrll       bed = get_elf_backend_data (abfd);
   3117   1.1     skrll       return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
   3118   1.1     skrll     }
   3119   1.3  christos }
   3120   1.3  christos 
   3121   1.3  christos /* Return the REL_HDR for SEC, assuming there is only a single one, either
   3122   1.3  christos    REL or RELA.  */
   3123   1.3  christos 
   3124   1.3  christos Elf_Internal_Shdr *
   3125   1.3  christos _bfd_elf_single_rel_hdr (asection *sec)
   3126   1.3  christos {
   3127   1.3  christos   if (elf_section_data (sec)->rel.hdr)
   3128   1.3  christos     {
   3129   1.3  christos       BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
   3130   1.3  christos       return elf_section_data (sec)->rel.hdr;
   3131   1.3  christos     }
   3132   1.3  christos   else
   3133   1.3  christos     return elf_section_data (sec)->rela.hdr;
   3134   1.6  christos }
   3135   1.6  christos 
   3136   1.6  christos static bfd_boolean
   3137   1.6  christos _bfd_elf_set_reloc_sh_name (bfd *abfd,
   3138   1.6  christos 			    Elf_Internal_Shdr *rel_hdr,
   3139   1.6  christos 			    const char *sec_name,
   3140   1.6  christos 			    bfd_boolean use_rela_p)
   3141   1.6  christos {
   3142   1.6  christos   char *name = (char *) bfd_alloc (abfd,
   3143   1.6  christos 				   sizeof ".rela" + strlen (sec_name));
   3144   1.6  christos   if (name == NULL)
   3145   1.6  christos     return FALSE;
   3146   1.6  christos 
   3147   1.6  christos   sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
   3148   1.6  christos   rel_hdr->sh_name =
   3149   1.6  christos     (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
   3150   1.6  christos 					FALSE);
   3151   1.6  christos   if (rel_hdr->sh_name == (unsigned int) -1)
   3152   1.6  christos     return FALSE;
   3153   1.6  christos 
   3154   1.6  christos   return TRUE;
   3155   1.3  christos }
   3156   1.3  christos 
   3157   1.3  christos /* Allocate and initialize a section-header for a new reloc section,
   3158   1.3  christos    containing relocations against ASECT.  It is stored in RELDATA.  If
   3159   1.1     skrll    USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
   3160   1.6  christos    relocations.  */
   3161   1.1     skrll 
   3162   1.3  christos static bfd_boolean
   3163   1.6  christos _bfd_elf_init_reloc_shdr (bfd *abfd,
   3164   1.6  christos 			  struct bfd_elf_section_reloc_data *reldata,
   3165   1.6  christos 			  const char *sec_name,
   3166   1.1     skrll 			  bfd_boolean use_rela_p,
   3167   1.3  christos 			  bfd_boolean delay_st_name_p)
   3168   1.1     skrll {
   3169   1.3  christos   Elf_Internal_Shdr *rel_hdr;
   3170   1.3  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3171   1.7  christos 
   3172   1.3  christos   BFD_ASSERT (reldata->hdr == NULL);
   3173   1.1     skrll   rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
   3174   1.6  christos   reldata->hdr = rel_hdr;
   3175   1.6  christos 
   3176   1.6  christos   if (delay_st_name_p)
   3177   1.6  christos     rel_hdr->sh_name = (unsigned int) -1;
   3178   1.1     skrll   else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
   3179   1.1     skrll 					use_rela_p))
   3180   1.1     skrll     return FALSE;
   3181   1.1     skrll   rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
   3182   1.1     skrll   rel_hdr->sh_entsize = (use_rela_p
   3183   1.1     skrll 			 ? bed->s->sizeof_rela
   3184   1.1     skrll 			 : bed->s->sizeof_rel);
   3185   1.1     skrll   rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   3186   1.1     skrll   rel_hdr->sh_flags = 0;
   3187   1.1     skrll   rel_hdr->sh_addr = 0;
   3188   1.1     skrll   rel_hdr->sh_size = 0;
   3189   1.1     skrll   rel_hdr->sh_offset = 0;
   3190   1.1     skrll 
   3191   1.1     skrll   return TRUE;
   3192   1.3  christos }
   3193   1.3  christos 
   3194   1.3  christos /* Return the default section type based on the passed in section flags.  */
   3195   1.3  christos 
   3196   1.3  christos int
   3197  1.15  christos bfd_elf_get_default_section_type (flagword flags)
   3198   1.3  christos {
   3199   1.3  christos   if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
   3200   1.3  christos       && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   3201   1.3  christos     return SHT_NOBITS;
   3202   1.3  christos   return SHT_PROGBITS;
   3203   1.3  christos }
   3204   1.3  christos 
   3205   1.3  christos struct fake_section_arg
   3206   1.3  christos {
   3207   1.3  christos   struct bfd_link_info *link_info;
   3208   1.3  christos   bfd_boolean failed;
   3209   1.1     skrll };
   3210   1.1     skrll 
   3211   1.1     skrll /* Set up an ELF internal section header for a section.  */
   3212   1.3  christos 
   3213   1.1     skrll static void
   3214   1.3  christos elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
   3215   1.1     skrll {
   3216   1.3  christos   struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
   3217   1.1     skrll   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3218   1.1     skrll   struct bfd_elf_section_data *esd = elf_section_data (asect);
   3219   1.6  christos   Elf_Internal_Shdr *this_hdr;
   3220   1.6  christos   unsigned int sh_type;
   3221   1.1     skrll   const char *name = asect->name;
   3222   1.3  christos   bfd_boolean delay_st_name_p = FALSE;
   3223   1.1     skrll 
   3224   1.1     skrll   if (arg->failed)
   3225   1.1     skrll     {
   3226   1.1     skrll       /* We already failed; just get out of the bfd_map_over_sections
   3227   1.1     skrll 	 loop.  */
   3228   1.1     skrll       return;
   3229   1.3  christos     }
   3230   1.1     skrll 
   3231   1.6  christos   this_hdr = &esd->this_hdr;
   3232   1.6  christos 
   3233   1.6  christos   if (arg->link_info)
   3234   1.6  christos     {
   3235   1.6  christos       /* ld: compress DWARF debug sections with names: .debug_*.  */
   3236   1.6  christos       if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
   3237   1.6  christos 	  && (asect->flags & SEC_DEBUGGING)
   3238   1.6  christos 	  && name[1] == 'd'
   3239   1.6  christos 	  && name[6] == '_')
   3240   1.6  christos 	{
   3241   1.6  christos 	  /* Set SEC_ELF_COMPRESS to indicate this section should be
   3242   1.6  christos 	     compressed.  */
   3243   1.9  christos 	  asect->flags |= SEC_ELF_COMPRESS;
   3244   1.6  christos 
   3245   1.6  christos 	  /* If this section will be compressed, delay adding section
   3246   1.6  christos 	     name to section name section after it is compressed in
   3247   1.6  christos 	     _bfd_elf_assign_file_positions_for_non_load.  */
   3248   1.6  christos 	  delay_st_name_p = TRUE;
   3249   1.6  christos 	}
   3250   1.6  christos     }
   3251   1.6  christos   else if ((asect->flags & SEC_ELF_RENAME))
   3252   1.6  christos     {
   3253   1.6  christos       /* objcopy: rename output DWARF debug section.  */
   3254   1.6  christos       if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
   3255   1.6  christos 	{
   3256   1.6  christos 	  /* When we decompress or compress with SHF_COMPRESSED,
   3257   1.6  christos 	     convert section name from .zdebug_* to .debug_* if
   3258   1.6  christos 	     needed.  */
   3259   1.6  christos 	  if (name[1] == 'z')
   3260   1.6  christos 	    {
   3261   1.6  christos 	      char *new_name = convert_zdebug_to_debug (abfd, name);
   3262   1.6  christos 	      if (new_name == NULL)
   3263   1.6  christos 		{
   3264   1.6  christos 		  arg->failed = TRUE;
   3265   1.6  christos 		  return;
   3266   1.6  christos 		}
   3267   1.6  christos 	      name = new_name;
   3268   1.6  christos 	    }
   3269   1.6  christos 	}
   3270   1.6  christos       else if (asect->compress_status == COMPRESS_SECTION_DONE)
   3271   1.6  christos 	{
   3272   1.6  christos 	  /* PR binutils/18087: Compression does not always make a
   3273   1.6  christos 	     section smaller.  So only rename the section when
   3274   1.6  christos 	     compression has actually taken place.  If input section
   3275   1.6  christos 	     name is .zdebug_*, we should never compress it again.  */
   3276   1.6  christos 	  char *new_name = convert_debug_to_zdebug (abfd, name);
   3277   1.6  christos 	  if (new_name == NULL)
   3278   1.6  christos 	    {
   3279   1.6  christos 	      arg->failed = TRUE;
   3280   1.6  christos 	      return;
   3281   1.6  christos 	    }
   3282   1.6  christos 	  BFD_ASSERT (name[1] != 'z');
   3283   1.6  christos 	  name = new_name;
   3284   1.6  christos 	}
   3285   1.6  christos     }
   3286   1.6  christos 
   3287   1.6  christos   if (delay_st_name_p)
   3288   1.1     skrll     this_hdr->sh_name = (unsigned int) -1;
   3289   1.6  christos   else
   3290   1.6  christos     {
   3291   1.6  christos       this_hdr->sh_name
   3292   1.6  christos 	= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   3293   1.6  christos 					      name, FALSE);
   3294   1.6  christos       if (this_hdr->sh_name == (unsigned int) -1)
   3295   1.6  christos 	{
   3296   1.6  christos 	  arg->failed = TRUE;
   3297   1.1     skrll 	  return;
   3298   1.1     skrll 	}
   3299   1.1     skrll     }
   3300   1.1     skrll 
   3301   1.1     skrll   /* Don't clear sh_flags. Assembler may set additional bits.  */
   3302   1.1     skrll 
   3303   1.1     skrll   if ((asect->flags & SEC_ALLOC) != 0
   3304   1.1     skrll       || asect->user_set_vma)
   3305   1.1     skrll     this_hdr->sh_addr = asect->vma;
   3306   1.1     skrll   else
   3307   1.1     skrll     this_hdr->sh_addr = 0;
   3308   1.1     skrll 
   3309   1.1     skrll   this_hdr->sh_offset = 0;
   3310   1.6  christos   this_hdr->sh_size = asect->size;
   3311   1.6  christos   this_hdr->sh_link = 0;
   3312   1.6  christos   /* PR 17512: file: 0eb809fe, 8b0535ee.  */
   3313   1.9  christos   if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
   3314   1.9  christos     {
   3315  1.13  christos       _bfd_error_handler
   3316   1.9  christos 	/* xgettext:c-format */
   3317   1.6  christos 	(_("%pB: error: alignment power %d of section `%pA' is too big"),
   3318   1.6  christos 	 abfd, asect->alignment_power, asect);
   3319   1.6  christos       arg->failed = TRUE;
   3320   1.1     skrll       return;
   3321   1.1     skrll     }
   3322   1.1     skrll   this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
   3323   1.1     skrll   /* The sh_entsize and sh_info fields may have been set already by
   3324   1.1     skrll      copy_private_section_data.  */
   3325   1.1     skrll 
   3326   1.1     skrll   this_hdr->bfd_section = asect;
   3327   1.1     skrll   this_hdr->contents = NULL;
   3328   1.1     skrll 
   3329   1.1     skrll   /* If the section type is unspecified, we set it based on
   3330   1.1     skrll      asect->flags.  */
   3331   1.1     skrll   if ((asect->flags & SEC_GROUP) != 0)
   3332   1.3  christos     sh_type = SHT_GROUP;
   3333   1.1     skrll   else
   3334   1.1     skrll     sh_type = bfd_elf_get_default_section_type (asect->flags);
   3335   1.1     skrll 
   3336   1.1     skrll   if (this_hdr->sh_type == SHT_NULL)
   3337   1.1     skrll     this_hdr->sh_type = sh_type;
   3338   1.1     skrll   else if (this_hdr->sh_type == SHT_NOBITS
   3339   1.1     skrll 	   && sh_type == SHT_PROGBITS
   3340   1.1     skrll 	   && (asect->flags & SEC_ALLOC) != 0)
   3341   1.1     skrll     {
   3342   1.1     skrll       /* Warn if we are changing a NOBITS section to PROGBITS, but
   3343   1.1     skrll 	 allow the link to proceed.  This can happen when users link
   3344   1.9  christos 	 non-bss input sections to bss output sections, or emit data
   3345  1.13  christos 	 to a bss output section via a linker script.  */
   3346   1.1     skrll       _bfd_error_handler
   3347   1.1     skrll 	(_("warning: section `%pA' type changed to PROGBITS"), asect);
   3348   1.1     skrll       this_hdr->sh_type = sh_type;
   3349   1.1     skrll     }
   3350   1.1     skrll 
   3351   1.1     skrll   switch (this_hdr->sh_type)
   3352   1.1     skrll     {
   3353   1.1     skrll     default:
   3354   1.1     skrll       break;
   3355   1.7  christos 
   3356   1.7  christos     case SHT_STRTAB:
   3357   1.7  christos     case SHT_NOTE:
   3358   1.7  christos     case SHT_NOBITS:
   3359   1.7  christos     case SHT_PROGBITS:
   3360   1.1     skrll       break;
   3361   1.1     skrll 
   3362   1.1     skrll     case SHT_INIT_ARRAY:
   3363   1.7  christos     case SHT_FINI_ARRAY:
   3364   1.1     skrll     case SHT_PREINIT_ARRAY:
   3365   1.1     skrll       this_hdr->sh_entsize = bed->s->arch_size / 8;
   3366   1.1     skrll       break;
   3367   1.1     skrll 
   3368   1.1     skrll     case SHT_HASH:
   3369   1.1     skrll       this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
   3370   1.1     skrll       break;
   3371   1.1     skrll 
   3372   1.1     skrll     case SHT_DYNSYM:
   3373   1.1     skrll       this_hdr->sh_entsize = bed->s->sizeof_sym;
   3374   1.1     skrll       break;
   3375   1.1     skrll 
   3376   1.1     skrll     case SHT_DYNAMIC:
   3377   1.1     skrll       this_hdr->sh_entsize = bed->s->sizeof_dyn;
   3378   1.1     skrll       break;
   3379   1.1     skrll 
   3380   1.1     skrll     case SHT_RELA:
   3381   1.1     skrll       if (get_elf_backend_data (abfd)->may_use_rela_p)
   3382   1.1     skrll 	this_hdr->sh_entsize = bed->s->sizeof_rela;
   3383   1.1     skrll       break;
   3384   1.1     skrll 
   3385   1.1     skrll      case SHT_REL:
   3386   1.1     skrll       if (get_elf_backend_data (abfd)->may_use_rel_p)
   3387   1.1     skrll 	this_hdr->sh_entsize = bed->s->sizeof_rel;
   3388   1.1     skrll       break;
   3389   1.1     skrll 
   3390   1.1     skrll      case SHT_GNU_versym:
   3391   1.1     skrll       this_hdr->sh_entsize = sizeof (Elf_External_Versym);
   3392   1.1     skrll       break;
   3393   1.1     skrll 
   3394   1.1     skrll      case SHT_GNU_verdef:
   3395   1.1     skrll       this_hdr->sh_entsize = 0;
   3396   1.1     skrll       /* objcopy or strip will copy over sh_info, but may not set
   3397   1.1     skrll 	 cverdefs.  The linker will set cverdefs, but sh_info will be
   3398   1.1     skrll 	 zero.  */
   3399   1.1     skrll       if (this_hdr->sh_info == 0)
   3400   1.1     skrll 	this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
   3401   1.1     skrll       else
   3402   1.1     skrll 	BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
   3403   1.1     skrll 		    || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
   3404   1.1     skrll       break;
   3405   1.1     skrll 
   3406   1.1     skrll     case SHT_GNU_verneed:
   3407   1.1     skrll       this_hdr->sh_entsize = 0;
   3408   1.1     skrll       /* objcopy or strip will copy over sh_info, but may not set
   3409   1.1     skrll 	 cverrefs.  The linker will set cverrefs, but sh_info will be
   3410   1.1     skrll 	 zero.  */
   3411   1.1     skrll       if (this_hdr->sh_info == 0)
   3412   1.1     skrll 	this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
   3413   1.1     skrll       else
   3414   1.1     skrll 	BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
   3415   1.1     skrll 		    || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
   3416   1.1     skrll       break;
   3417   1.1     skrll 
   3418   1.1     skrll     case SHT_GROUP:
   3419   1.1     skrll       this_hdr->sh_entsize = GRP_ENTRY_SIZE;
   3420   1.1     skrll       break;
   3421   1.1     skrll 
   3422   1.1     skrll     case SHT_GNU_HASH:
   3423   1.1     skrll       this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
   3424   1.1     skrll       break;
   3425   1.1     skrll     }
   3426   1.1     skrll 
   3427   1.1     skrll   if ((asect->flags & SEC_ALLOC) != 0)
   3428   1.1     skrll     this_hdr->sh_flags |= SHF_ALLOC;
   3429   1.1     skrll   if ((asect->flags & SEC_READONLY) == 0)
   3430   1.1     skrll     this_hdr->sh_flags |= SHF_WRITE;
   3431   1.1     skrll   if ((asect->flags & SEC_CODE) != 0)
   3432   1.1     skrll     this_hdr->sh_flags |= SHF_EXECINSTR;
   3433   1.1     skrll   if ((asect->flags & SEC_MERGE) != 0)
   3434   1.1     skrll     {
   3435   1.1     skrll       this_hdr->sh_flags |= SHF_MERGE;
   3436   1.7  christos       this_hdr->sh_entsize = asect->entsize;
   3437   1.7  christos     }
   3438   1.1     skrll   if ((asect->flags & SEC_STRINGS) != 0)
   3439   1.1     skrll     this_hdr->sh_flags |= SHF_STRINGS;
   3440   1.1     skrll   if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
   3441   1.1     skrll     this_hdr->sh_flags |= SHF_GROUP;
   3442   1.1     skrll   if ((asect->flags & SEC_THREAD_LOCAL) != 0)
   3443   1.1     skrll     {
   3444   1.1     skrll       this_hdr->sh_flags |= SHF_TLS;
   3445   1.1     skrll       if (asect->size == 0
   3446   1.1     skrll 	  && (asect->flags & SEC_HAS_CONTENTS) == 0)
   3447   1.1     skrll 	{
   3448   1.1     skrll 	  struct bfd_link_order *o = asect->map_tail.link_order;
   3449   1.1     skrll 
   3450   1.1     skrll 	  this_hdr->sh_size = 0;
   3451   1.1     skrll 	  if (o != NULL)
   3452   1.1     skrll 	    {
   3453   1.1     skrll 	      this_hdr->sh_size = o->offset + o->size;
   3454   1.1     skrll 	      if (this_hdr->sh_size != 0)
   3455   1.1     skrll 		this_hdr->sh_type = SHT_NOBITS;
   3456   1.1     skrll 	    }
   3457   1.3  christos 	}
   3458   1.3  christos     }
   3459   1.3  christos   if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
   3460   1.3  christos     this_hdr->sh_flags |= SHF_EXCLUDE;
   3461   1.3  christos 
   3462   1.3  christos   /* If the section has relocs, set up a section header for the
   3463   1.3  christos      SHT_REL[A] section.  If two relocation sections are required for
   3464   1.3  christos      this section, it is up to the processor-specific back-end to
   3465   1.3  christos      create the other.  */
   3466   1.3  christos   if ((asect->flags & SEC_RELOC) != 0)
   3467   1.3  christos     {
   3468   1.3  christos       /* When doing a relocatable link, create both REL and RELA sections if
   3469   1.3  christos 	 needed.  */
   3470   1.3  christos       if (arg->link_info
   3471   1.6  christos 	  /* Do the normal setup if we wouldn't create any sections here.  */
   3472   1.6  christos 	  && esd->rel.count + esd->rela.count > 0
   3473   1.3  christos 	  && (bfd_link_relocatable (arg->link_info)
   3474   1.3  christos 	      || arg->link_info->emitrelocations))
   3475   1.9  christos 	{
   3476   1.9  christos 	  if (esd->rel.count && esd->rel.hdr == NULL
   3477   1.3  christos 	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
   3478   1.3  christos 					    FALSE, delay_st_name_p))
   3479   1.3  christos 	    {
   3480   1.3  christos 	      arg->failed = TRUE;
   3481   1.3  christos 	      return;
   3482   1.9  christos 	    }
   3483   1.9  christos 	  if (esd->rela.count && esd->rela.hdr == NULL
   3484   1.3  christos 	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
   3485   1.3  christos 					    TRUE, delay_st_name_p))
   3486   1.3  christos 	    {
   3487   1.3  christos 	      arg->failed = TRUE;
   3488   1.3  christos 	      return;
   3489   1.3  christos 	    }
   3490   1.3  christos 	}
   3491   1.3  christos       else if (!_bfd_elf_init_reloc_shdr (abfd,
   3492   1.6  christos 					  (asect->use_rela_p
   3493   1.6  christos 					   ? &esd->rela : &esd->rel),
   3494   1.6  christos 					  name,
   3495   1.9  christos 					  asect->use_rela_p,
   3496   1.3  christos 					  delay_st_name_p))
   3497   1.9  christos 	{
   3498   1.9  christos 	  arg->failed = TRUE;
   3499   1.3  christos 	  return;
   3500   1.1     skrll 	}
   3501   1.1     skrll     }
   3502   1.1     skrll 
   3503   1.1     skrll   /* Check for processor-specific section types.  */
   3504   1.1     skrll   sh_type = this_hdr->sh_type;
   3505   1.9  christos   if (bed->elf_backend_fake_sections
   3506   1.9  christos       && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
   3507   1.9  christos     {
   3508   1.9  christos       arg->failed = TRUE;
   3509   1.1     skrll       return;
   3510   1.1     skrll     }
   3511   1.1     skrll 
   3512   1.1     skrll   if (sh_type == SHT_NOBITS && asect->size != 0)
   3513   1.1     skrll     {
   3514   1.1     skrll       /* Don't change the header type from NOBITS if we are being
   3515   1.1     skrll 	 called for objcopy --only-keep-debug.  */
   3516   1.1     skrll       this_hdr->sh_type = sh_type;
   3517   1.1     skrll     }
   3518   1.2     skrll }
   3519   1.2     skrll 
   3520   1.2     skrll /* Fill in the contents of a SHT_GROUP section.  Called from
   3521   1.2     skrll    _bfd_elf_compute_section_file_positions for gas, objcopy, and
   3522   1.1     skrll    when ELF targets use the generic linker, ld.  Called for ld -r
   3523   1.1     skrll    from bfd_elf_final_link.  */
   3524   1.1     skrll 
   3525   1.1     skrll void
   3526   1.3  christos bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
   3527   1.1     skrll {
   3528   1.1     skrll   bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
   3529   1.1     skrll   asection *elt, *first;
   3530   1.1     skrll   unsigned char *loc;
   3531   1.1     skrll   bfd_boolean gas;
   3532   1.1     skrll 
   3533  1.15  christos   /* Ignore linker created group section.  See elfNN_ia64_object_p in
   3534  1.15  christos      elfxx-ia64.c.  */
   3535   1.1     skrll   if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
   3536   1.1     skrll       || sec->size == 0
   3537   1.1     skrll       || *failedptr)
   3538   1.2     skrll     return;
   3539   1.2     skrll 
   3540   1.2     skrll   if (elf_section_data (sec)->this_hdr.sh_info == 0)
   3541   1.2     skrll     {
   3542   1.2     skrll       unsigned long symindx = 0;
   3543   1.2     skrll 
   3544   1.2     skrll       /* elf_group_id will have been set up by objcopy and the
   3545   1.2     skrll 	 generic linker.  */
   3546   1.2     skrll       if (elf_group_id (sec) != NULL)
   3547   1.2     skrll 	symindx = elf_group_id (sec)->udata.i;
   3548   1.2     skrll 
   3549   1.2     skrll       if (symindx == 0)
   3550   1.2     skrll 	{
   3551   1.2     skrll 	  /* If called from the assembler, swap_out_syms will have set up
   3552   1.2     skrll 	     elf_section_syms.  */
   3553   1.2     skrll 	  BFD_ASSERT (elf_section_syms (abfd) != NULL);
   3554   1.2     skrll 	  symindx = elf_section_syms (abfd)[sec->index]->udata.i;
   3555   1.2     skrll 	}
   3556   1.2     skrll       elf_section_data (sec)->this_hdr.sh_info = symindx;
   3557   1.2     skrll     }
   3558   1.2     skrll   else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
   3559   1.2     skrll     {
   3560   1.2     skrll       /* The ELF backend linker sets sh_info to -2 when the group
   3561   1.9  christos 	 signature symbol is global, and thus the index can't be
   3562   1.9  christos 	 set until all local symbols are output.  */
   3563   1.9  christos       asection *igroup;
   3564   1.9  christos       struct bfd_elf_section_data *sec_data;
   3565   1.2     skrll       unsigned long symndx;
   3566   1.2     skrll       unsigned long extsymoff;
   3567   1.9  christos       struct elf_link_hash_entry *h;
   3568   1.9  christos 
   3569   1.9  christos       /* The point of this little dance to the first SHF_GROUP section
   3570   1.9  christos 	 then back to the SHT_GROUP section is that this gets us to
   3571   1.9  christos 	 the SHT_GROUP in the input object.  */
   3572   1.9  christos       igroup = elf_sec_group (elf_next_in_group (sec));
   3573   1.9  christos       sec_data = elf_section_data (igroup);
   3574   1.2     skrll       symndx = sec_data->this_hdr.sh_info;
   3575   1.2     skrll       extsymoff = 0;
   3576   1.2     skrll       if (!elf_bad_symtab (igroup->owner))
   3577   1.2     skrll 	{
   3578   1.2     skrll 	  Elf_Internal_Shdr *symtab_hdr;
   3579   1.2     skrll 
   3580   1.2     skrll 	  symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
   3581   1.2     skrll 	  extsymoff = symtab_hdr->sh_info;
   3582   1.2     skrll 	}
   3583   1.2     skrll       h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
   3584   1.2     skrll       while (h->root.type == bfd_link_hash_indirect
   3585   1.2     skrll 	     || h->root.type == bfd_link_hash_warning)
   3586   1.2     skrll 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3587   1.1     skrll 
   3588   1.1     skrll       elf_section_data (sec)->this_hdr.sh_info = h->indx;
   3589   1.1     skrll     }
   3590   1.1     skrll 
   3591   1.1     skrll   /* The contents won't be allocated for "ld -r" or objcopy.  */
   3592   1.1     skrll   gas = TRUE;
   3593   1.1     skrll   if (sec->contents == NULL)
   3594   1.3  christos     {
   3595   1.1     skrll       gas = FALSE;
   3596   1.1     skrll       sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
   3597   1.1     skrll 
   3598   1.1     skrll       /* Arrange for the section to be written out.  */
   3599   1.1     skrll       elf_section_data (sec)->this_hdr.contents = sec->contents;
   3600   1.1     skrll       if (sec->contents == NULL)
   3601   1.1     skrll 	{
   3602   1.1     skrll 	  *failedptr = TRUE;
   3603   1.1     skrll 	  return;
   3604   1.1     skrll 	}
   3605   1.1     skrll     }
   3606   1.1     skrll 
   3607   1.1     skrll   loc = sec->contents + sec->size;
   3608   1.1     skrll 
   3609   1.1     skrll   /* Get the pointer to the first section in the group that gas
   3610   1.1     skrll      squirreled away here.  objcopy arranges for this to be set to the
   3611   1.1     skrll      start of the input section group.  */
   3612   1.1     skrll   first = elt = elf_next_in_group (sec);
   3613   1.1     skrll 
   3614   1.1     skrll   /* First element is a flag word.  Rest of section is elf section
   3615   1.1     skrll      indices for all the sections of the group.  Write them backwards
   3616   1.1     skrll      just to keep the group in the same order as given in .section
   3617   1.1     skrll      directives, not that it matters.  */
   3618   1.1     skrll   while (elt != NULL)
   3619   1.1     skrll     {
   3620   1.1     skrll       asection *s;
   3621   1.1     skrll 
   3622   1.1     skrll       s = elt;
   3623   1.3  christos       if (!gas)
   3624   1.3  christos 	s = s->output_section;
   3625   1.3  christos       if (s != NULL
   3626   1.9  christos 	  && !bfd_is_abs_section (s))
   3627   1.9  christos 	{
   3628   1.3  christos 	  struct bfd_elf_section_data *elf_sec = elf_section_data (s);
   3629   1.9  christos 	  struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
   3630   1.9  christos 
   3631   1.9  christos 	  if (elf_sec->rel.hdr != NULL
   3632   1.9  christos 	      && (gas
   3633   1.9  christos 		  || (input_elf_sec->rel.hdr != NULL
   3634   1.9  christos 		      && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
   3635   1.9  christos 	    {
   3636   1.9  christos 	      elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
   3637   1.9  christos 	      loc -= 4;
   3638   1.9  christos 	      H_PUT_32 (abfd, elf_sec->rel.idx, loc);
   3639   1.9  christos 	    }
   3640   1.9  christos 	  if (elf_sec->rela.hdr != NULL
   3641   1.9  christos 	      && (gas
   3642   1.9  christos 		  || (input_elf_sec->rela.hdr != NULL
   3643   1.9  christos 		      && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
   3644   1.9  christos 	    {
   3645   1.9  christos 	      elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
   3646   1.9  christos 	      loc -= 4;
   3647   1.3  christos 	      H_PUT_32 (abfd, elf_sec->rela.idx, loc);
   3648   1.9  christos 	    }
   3649   1.3  christos 	  loc -= 4;
   3650   1.1     skrll 	  H_PUT_32 (abfd, elf_sec->this_idx, loc);
   3651   1.1     skrll 	}
   3652   1.1     skrll       elt = elf_next_in_group (elt);
   3653   1.1     skrll       if (elt == first)
   3654   1.1     skrll 	break;
   3655   1.9  christos     }
   3656   1.9  christos 
   3657   1.1     skrll   loc -= 4;
   3658   1.1     skrll   BFD_ASSERT (loc == sec->contents);
   3659   1.1     skrll 
   3660   1.1     skrll   H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
   3661   1.9  christos }
   3662   1.9  christos 
   3663   1.9  christos /* Given NAME, the name of a relocation section stripped of its
   3664   1.6  christos    .rel/.rela prefix, return the section in ABFD to which the
   3665   1.6  christos    relocations apply.  */
   3666   1.9  christos 
   3667   1.9  christos asection *
   3668   1.9  christos _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
   3669   1.9  christos {
   3670   1.9  christos   /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
   3671   1.9  christos      section likely apply to .got.plt or .got section.  */
   3672   1.9  christos   if (get_elf_backend_data (abfd)->want_got_plt
   3673   1.9  christos       && strcmp (name, ".plt") == 0)
   3674   1.9  christos     {
   3675   1.9  christos       asection *sec;
   3676   1.9  christos 
   3677   1.9  christos       name = ".got.plt";
   3678   1.9  christos       sec = bfd_get_section_by_name (abfd, name);
   3679   1.9  christos       if (sec != NULL)
   3680   1.9  christos 	return sec;
   3681   1.9  christos       name = ".got";
   3682   1.9  christos     }
   3683   1.9  christos 
   3684   1.9  christos   return bfd_get_section_by_name (abfd, name);
   3685   1.9  christos }
   3686   1.9  christos 
   3687   1.9  christos /* Return the section to which RELOC_SEC applies.  */
   3688   1.9  christos 
   3689   1.6  christos static asection *
   3690   1.6  christos elf_get_reloc_section (asection *reloc_sec)
   3691   1.6  christos {
   3692   1.6  christos   const char *name;
   3693   1.9  christos   unsigned int type;
   3694   1.6  christos   bfd *abfd;
   3695   1.6  christos   const struct elf_backend_data *bed;
   3696   1.6  christos 
   3697   1.6  christos   type = elf_section_data (reloc_sec)->this_hdr.sh_type;
   3698   1.6  christos   if (type != SHT_REL && type != SHT_RELA)
   3699   1.6  christos     return NULL;
   3700   1.6  christos 
   3701   1.9  christos   /* We look up the section the relocs apply to by name.  */
   3702   1.9  christos   name = reloc_sec->name;
   3703   1.9  christos   if (strncmp (name, ".rel", 4) != 0)
   3704   1.9  christos     return NULL;
   3705   1.9  christos   name += 4;
   3706   1.6  christos   if (type == SHT_RELA && *name++ != 'a')
   3707   1.6  christos     return NULL;
   3708   1.9  christos 
   3709   1.9  christos   abfd = reloc_sec->owner;
   3710   1.6  christos   bed = get_elf_backend_data (abfd);
   3711   1.6  christos   return bed->get_reloc_section (abfd, name);
   3712   1.1     skrll }
   3713   1.1     skrll 
   3714   1.1     skrll /* Assign all ELF section numbers.  The dummy first section is handled here
   3715   1.1     skrll    too.  The link/info pointers for the standard section types are filled
   3716   1.1     skrll    in here too, while we're at it.  */
   3717   1.1     skrll 
   3718   1.1     skrll static bfd_boolean
   3719   1.1     skrll assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
   3720   1.1     skrll {
   3721   1.6  christos   struct elf_obj_tdata *t = elf_tdata (abfd);
   3722   1.1     skrll   asection *sec;
   3723   1.1     skrll   unsigned int section_number;
   3724   1.3  christos   Elf_Internal_Shdr **i_shdrp;
   3725   1.1     skrll   struct bfd_elf_section_data *d;
   3726   1.1     skrll   bfd_boolean need_symtab;
   3727   1.1     skrll 
   3728   1.1     skrll   section_number = 1;
   3729   1.1     skrll 
   3730   1.1     skrll   _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
   3731   1.9  christos 
   3732   1.1     skrll   /* SHT_GROUP sections are in relocatable files only.  */
   3733   1.7  christos   if (link_info == NULL || !link_info->resolve_section_groups)
   3734   1.7  christos     {
   3735   1.1     skrll       size_t reloc_count = 0;
   3736   1.1     skrll 
   3737   1.1     skrll       /* Put SHT_GROUP sections first.  */
   3738   1.1     skrll       for (sec = abfd->sections; sec != NULL; sec = sec->next)
   3739   1.1     skrll 	{
   3740   1.1     skrll 	  d = elf_section_data (sec);
   3741   1.1     skrll 
   3742   1.1     skrll 	  if (d->this_hdr.sh_type == SHT_GROUP)
   3743   1.1     skrll 	    {
   3744   1.1     skrll 	      if (sec->flags & SEC_LINKER_CREATED)
   3745   1.1     skrll 		{
   3746   1.1     skrll 		  /* Remove the linker created SHT_GROUP sections.  */
   3747   1.1     skrll 		  bfd_section_list_remove (abfd, sec);
   3748   1.1     skrll 		  abfd->section_count--;
   3749   1.1     skrll 		}
   3750   1.1     skrll 	      else
   3751   1.7  christos 		d->this_idx = section_number++;
   3752   1.7  christos 	    }
   3753   1.7  christos 
   3754   1.1     skrll 	  /* Count relocations.  */
   3755   1.7  christos 	  reloc_count += sec->reloc_count;
   3756   1.7  christos 	}
   3757   1.7  christos 
   3758   1.7  christos       /* Clear HAS_RELOC if there are no relocations.  */
   3759   1.1     skrll       if (reloc_count == 0)
   3760   1.1     skrll 	abfd->flags &= ~HAS_RELOC;
   3761   1.1     skrll     }
   3762   1.1     skrll 
   3763   1.1     skrll   for (sec = abfd->sections; sec; sec = sec->next)
   3764   1.1     skrll     {
   3765   1.1     skrll       d = elf_section_data (sec);
   3766   1.1     skrll 
   3767   1.6  christos       if (d->this_hdr.sh_type != SHT_GROUP)
   3768   1.6  christos 	d->this_idx = section_number++;
   3769   1.3  christos       if (d->this_hdr.sh_name != (unsigned int) -1)
   3770   1.1     skrll 	_bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
   3771   1.3  christos       if (d->rel.hdr)
   3772   1.6  christos 	{
   3773   1.6  christos 	  d->rel.idx = section_number++;
   3774   1.1     skrll 	  if (d->rel.hdr->sh_name != (unsigned int) -1)
   3775   1.3  christos 	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
   3776   1.3  christos 	}
   3777   1.1     skrll       else
   3778   1.3  christos 	d->rel.idx = 0;
   3779   1.1     skrll 
   3780   1.3  christos       if (d->rela.hdr)
   3781   1.6  christos 	{
   3782   1.6  christos 	  d->rela.idx = section_number++;
   3783   1.1     skrll 	  if (d->rela.hdr->sh_name != (unsigned int) -1)
   3784   1.1     skrll 	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
   3785   1.3  christos 	}
   3786   1.1     skrll       else
   3787   1.1     skrll 	d->rela.idx = 0;
   3788   1.3  christos     }
   3789   1.3  christos 
   3790   1.3  christos   need_symtab = (bfd_get_symcount (abfd) > 0
   3791   1.3  christos 		|| (link_info == NULL
   3792   1.3  christos 		    && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
   3793   1.1     skrll 			== HAS_RELOC)));
   3794   1.6  christos   if (need_symtab)
   3795   1.1     skrll     {
   3796   1.1     skrll       elf_onesymtab (abfd) = section_number++;
   3797   1.1     skrll       _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
   3798  1.15  christos       if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
   3799   1.6  christos 	{
   3800   1.6  christos 	  elf_section_list *entry;
   3801   1.6  christos 
   3802  1.15  christos 	  BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
   3803   1.6  christos 
   3804   1.6  christos 	  entry = bfd_zalloc (abfd, sizeof (*entry));
   3805   1.6  christos 	  entry->ndx = section_number++;
   3806   1.1     skrll 	  elf_symtab_shndx_list (abfd) = entry;
   3807   1.1     skrll 	  entry->hdr.sh_name
   3808   1.6  christos 	    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   3809   1.1     skrll 						  ".symtab_shndx", FALSE);
   3810   1.1     skrll 	  if (entry->hdr.sh_name == (unsigned int) -1)
   3811   1.6  christos 	    return FALSE;
   3812   1.1     skrll 	}
   3813   1.1     skrll       elf_strtab_sec (abfd) = section_number++;
   3814   1.1     skrll       _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
   3815   1.9  christos     }
   3816   1.9  christos 
   3817   1.9  christos   elf_shstrtab_sec (abfd) = section_number++;
   3818   1.9  christos   _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
   3819   1.5  christos   elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
   3820   1.5  christos 
   3821   1.9  christos   if (section_number >= SHN_LORESERVE)
   3822  1.13  christos     {
   3823   1.5  christos       /* xgettext:c-format */
   3824   1.5  christos       _bfd_error_handler (_("%pB: too many sections: %u"),
   3825   1.5  christos 			  abfd, section_number);
   3826   1.5  christos       return FALSE;
   3827   1.1     skrll     }
   3828   1.1     skrll 
   3829   1.1     skrll   elf_numsections (abfd) = section_number;
   3830   1.1     skrll   elf_elfheader (abfd)->e_shnum = section_number;
   3831   1.1     skrll 
   3832   1.3  christos   /* Set up the list of section header pointers, in agreement with the
   3833   1.9  christos      indices.  */
   3834   1.1     skrll   i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
   3835   1.1     skrll 						sizeof (Elf_Internal_Shdr *));
   3836   1.1     skrll   if (i_shdrp == NULL)
   3837   1.3  christos     return FALSE;
   3838   1.9  christos 
   3839   1.1     skrll   i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
   3840   1.1     skrll 						 sizeof (Elf_Internal_Shdr));
   3841   1.1     skrll   if (i_shdrp[0] == NULL)
   3842   1.1     skrll     {
   3843   1.1     skrll       bfd_release (abfd, i_shdrp);
   3844   1.1     skrll       return FALSE;
   3845   1.1     skrll     }
   3846   1.1     skrll 
   3847   1.6  christos   elf_elfsections (abfd) = i_shdrp;
   3848   1.3  christos 
   3849   1.1     skrll   i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
   3850   1.6  christos   if (need_symtab)
   3851   1.1     skrll     {
   3852   1.1     skrll       i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
   3853   1.6  christos       if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
   3854   1.6  christos 	{
   3855   1.6  christos 	  elf_section_list * entry = elf_symtab_shndx_list (abfd);
   3856   1.6  christos 	  BFD_ASSERT (entry != NULL);
   3857   1.1     skrll 	  i_shdrp[entry->ndx] = & entry->hdr;
   3858   1.6  christos 	  entry->hdr.sh_link = elf_onesymtab (abfd);
   3859   1.6  christos 	}
   3860   1.1     skrll       i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
   3861   1.1     skrll       t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
   3862   1.1     skrll     }
   3863   1.1     skrll 
   3864   1.1     skrll   for (sec = abfd->sections; sec; sec = sec->next)
   3865   1.1     skrll     {
   3866   1.3  christos       asection *s;
   3867   1.3  christos 
   3868   1.1     skrll       d = elf_section_data (sec);
   3869   1.3  christos 
   3870   1.3  christos       i_shdrp[d->this_idx] = &d->this_hdr;
   3871   1.3  christos       if (d->rel.idx != 0)
   3872   1.3  christos 	i_shdrp[d->rel.idx] = d->rel.hdr;
   3873   1.1     skrll       if (d->rela.idx != 0)
   3874   1.1     skrll 	i_shdrp[d->rela.idx] = d->rela.hdr;
   3875   1.1     skrll 
   3876   1.1     skrll       /* Fill in the sh_link and sh_info fields while we're at it.  */
   3877   1.1     skrll 
   3878   1.1     skrll       /* sh_link of a reloc section is the section index of the symbol
   3879   1.3  christos 	 table.  sh_info is the section index of the section to which
   3880   1.1     skrll 	 the relocation entries apply.  */
   3881   1.6  christos       if (d->rel.idx != 0)
   3882   1.3  christos 	{
   3883   1.6  christos 	  d->rel.hdr->sh_link = elf_onesymtab (abfd);
   3884   1.1     skrll 	  d->rel.hdr->sh_info = d->this_idx;
   3885   1.3  christos 	  d->rel.hdr->sh_flags |= SHF_INFO_LINK;
   3886   1.1     skrll 	}
   3887   1.6  christos       if (d->rela.idx != 0)
   3888   1.3  christos 	{
   3889   1.6  christos 	  d->rela.hdr->sh_link = elf_onesymtab (abfd);
   3890   1.1     skrll 	  d->rela.hdr->sh_info = d->this_idx;
   3891   1.1     skrll 	  d->rela.hdr->sh_flags |= SHF_INFO_LINK;
   3892   1.1     skrll 	}
   3893   1.1     skrll 
   3894   1.1     skrll       /* We need to set up sh_link for SHF_LINK_ORDER.  */
   3895   1.1     skrll       if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
   3896   1.1     skrll 	{
   3897   1.1     skrll 	  s = elf_linked_to_section (sec);
   3898   1.1     skrll 	  if (s)
   3899   1.1     skrll 	    {
   3900   1.1     skrll 	      /* elf_linked_to_section points to the input section.  */
   3901   1.1     skrll 	      if (link_info != NULL)
   3902   1.5  christos 		{
   3903   1.1     skrll 		  /* Check discarded linkonce section.  */
   3904   1.1     skrll 		  if (discarded_section (s))
   3905   1.9  christos 		    {
   3906   1.9  christos 		      asection *kept;
   3907  1.13  christos 		      _bfd_error_handler
   3908  1.13  christos 			/* xgettext:c-format */
   3909   1.1     skrll 			(_("%pB: sh_link of section `%pA' points to"
   3910   1.1     skrll 			   " discarded section `%pA' of `%pB'"),
   3911   1.1     skrll 			 abfd, d->this_hdr.bfd_section,
   3912   1.1     skrll 			 s, s->owner);
   3913   1.1     skrll 		      /* Point to the kept section if it has the same
   3914   1.1     skrll 			 size as the discarded one.  */
   3915   1.1     skrll 		      kept = _bfd_elf_check_kept_section (s, link_info);
   3916   1.1     skrll 		      if (kept == NULL)
   3917   1.1     skrll 			{
   3918   1.1     skrll 			  bfd_set_error (bfd_error_bad_value);
   3919   1.1     skrll 			  return FALSE;
   3920   1.1     skrll 			}
   3921   1.1     skrll 		      s = kept;
   3922   1.1     skrll 		    }
   3923   1.1     skrll 
   3924   1.1     skrll 		  s = s->output_section;
   3925   1.1     skrll 		  BFD_ASSERT (s != NULL);
   3926   1.1     skrll 		}
   3927   1.1     skrll 	      else
   3928   1.1     skrll 		{
   3929   1.1     skrll 		  /* Handle objcopy. */
   3930   1.9  christos 		  if (s->output_section == NULL)
   3931   1.9  christos 		    {
   3932  1.13  christos 		      _bfd_error_handler
   3933  1.13  christos 			/* xgettext:c-format */
   3934   1.1     skrll 			(_("%pB: sh_link of section `%pA' points to"
   3935   1.1     skrll 			   " removed section `%pA' of `%pB'"),
   3936   1.1     skrll 			 abfd, d->this_hdr.bfd_section, s, s->owner);
   3937   1.1     skrll 		      bfd_set_error (bfd_error_bad_value);
   3938   1.1     skrll 		      return FALSE;
   3939   1.1     skrll 		    }
   3940   1.1     skrll 		  s = s->output_section;
   3941   1.1     skrll 		}
   3942   1.1     skrll 	      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3943   1.1     skrll 	    }
   3944   1.1     skrll 	  else
   3945   1.1     skrll 	    {
   3946   1.1     skrll 	      /* PR 290:
   3947   1.1     skrll 		 The Intel C compiler generates SHT_IA_64_UNWIND with
   3948   1.1     skrll 		 SHF_LINK_ORDER.  But it doesn't set the sh_link or
   3949   1.1     skrll 		 sh_info fields.  Hence we could get the situation
   3950   1.1     skrll 		 where s is NULL.  */
   3951   1.1     skrll 	      const struct elf_backend_data *bed
   3952   1.1     skrll 		= get_elf_backend_data (abfd);
   3953   1.9  christos 	      if (bed->link_order_error_handler)
   3954  1.13  christos 		bed->link_order_error_handler
   3955   1.1     skrll 		  /* xgettext:c-format */
   3956   1.1     skrll 		  (_("%pB: warning: sh_link not set for section `%pA'"),
   3957   1.1     skrll 		   abfd, sec);
   3958   1.1     skrll 	    }
   3959   1.1     skrll 	}
   3960   1.1     skrll 
   3961   1.1     skrll       switch (d->this_hdr.sh_type)
   3962   1.1     skrll 	{
   3963   1.1     skrll 	case SHT_REL:
   3964   1.1     skrll 	case SHT_RELA:
   3965   1.1     skrll 	  /* A reloc section which we are treating as a normal BFD
   3966   1.1     skrll 	     section.  sh_link is the section index of the symbol
   3967   1.1     skrll 	     table.  sh_info is the section index of the section to
   3968   1.1     skrll 	     which the relocation entries apply.  We assume that an
   3969   1.1     skrll 	     allocated reloc section uses the dynamic symbol table.
   3970   1.1     skrll 	     FIXME: How can we be sure?  */
   3971   1.1     skrll 	  s = bfd_get_section_by_name (abfd, ".dynsym");
   3972   1.1     skrll 	  if (s != NULL)
   3973   1.9  christos 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3974   1.1     skrll 
   3975   1.6  christos 	  s = elf_get_reloc_section (sec);
   3976   1.6  christos 	  if (s != NULL)
   3977   1.6  christos 	    {
   3978   1.6  christos 	      d->this_hdr.sh_info = elf_section_data (s)->this_idx;
   3979   1.1     skrll 	      d->this_hdr.sh_flags |= SHF_INFO_LINK;
   3980   1.1     skrll 	    }
   3981   1.1     skrll 	  break;
   3982   1.1     skrll 
   3983   1.1     skrll 	case SHT_STRTAB:
   3984   1.1     skrll 	  /* We assume that a section named .stab*str is a stabs
   3985   1.1     skrll 	     string section.  We look for a section with the same name
   3986   1.1     skrll 	     but without the trailing ``str'', and set its sh_link
   3987   1.1     skrll 	     field to point to this section.  */
   3988   1.1     skrll 	  if (CONST_STRNEQ (sec->name, ".stab")
   3989   1.1     skrll 	      && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
   3990   1.1     skrll 	    {
   3991   1.1     skrll 	      size_t len;
   3992   1.1     skrll 	      char *alc;
   3993   1.3  christos 
   3994   1.1     skrll 	      len = strlen (sec->name);
   3995   1.1     skrll 	      alc = (char *) bfd_malloc (len - 2);
   3996   1.1     skrll 	      if (alc == NULL)
   3997   1.1     skrll 		return FALSE;
   3998   1.1     skrll 	      memcpy (alc, sec->name, len - 3);
   3999   1.1     skrll 	      alc[len - 3] = '\0';
   4000   1.1     skrll 	      s = bfd_get_section_by_name (abfd, alc);
   4001   1.1     skrll 	      free (alc);
   4002   1.1     skrll 	      if (s != NULL)
   4003   1.1     skrll 		{
   4004   1.1     skrll 		  elf_section_data (s)->this_hdr.sh_link = d->this_idx;
   4005   1.1     skrll 
   4006   1.1     skrll 		  /* This is a .stab section.  */
   4007   1.1     skrll 		  if (elf_section_data (s)->this_hdr.sh_entsize == 0)
   4008   1.1     skrll 		    elf_section_data (s)->this_hdr.sh_entsize
   4009   1.1     skrll 		      = 4 + 2 * bfd_get_arch_size (abfd) / 8;
   4010   1.1     skrll 		}
   4011   1.1     skrll 	    }
   4012   1.1     skrll 	  break;
   4013   1.1     skrll 
   4014   1.1     skrll 	case SHT_DYNAMIC:
   4015   1.1     skrll 	case SHT_DYNSYM:
   4016   1.1     skrll 	case SHT_GNU_verneed:
   4017   1.1     skrll 	case SHT_GNU_verdef:
   4018   1.1     skrll 	  /* sh_link is the section header index of the string table
   4019   1.1     skrll 	     used for the dynamic entries, or the symbol table, or the
   4020   1.1     skrll 	     version strings.  */
   4021   1.1     skrll 	  s = bfd_get_section_by_name (abfd, ".dynstr");
   4022   1.1     skrll 	  if (s != NULL)
   4023   1.1     skrll 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   4024   1.1     skrll 	  break;
   4025   1.1     skrll 
   4026   1.1     skrll 	case SHT_GNU_LIBLIST:
   4027   1.1     skrll 	  /* sh_link is the section header index of the prelink library
   4028   1.1     skrll 	     list used for the dynamic entries, or the symbol table, or
   4029   1.1     skrll 	     the version strings.  */
   4030   1.1     skrll 	  s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
   4031   1.1     skrll 					     ? ".dynstr" : ".gnu.libstr");
   4032   1.1     skrll 	  if (s != NULL)
   4033   1.1     skrll 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   4034   1.1     skrll 	  break;
   4035   1.1     skrll 
   4036   1.1     skrll 	case SHT_HASH:
   4037   1.1     skrll 	case SHT_GNU_HASH:
   4038   1.1     skrll 	case SHT_GNU_versym:
   4039   1.1     skrll 	  /* sh_link is the section header index of the symbol table
   4040   1.1     skrll 	     this hash table or version table is for.  */
   4041   1.1     skrll 	  s = bfd_get_section_by_name (abfd, ".dynsym");
   4042   1.1     skrll 	  if (s != NULL)
   4043   1.1     skrll 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   4044   1.1     skrll 	  break;
   4045   1.6  christos 
   4046   1.1     skrll 	case SHT_GROUP:
   4047   1.1     skrll 	  d->this_hdr.sh_link = elf_onesymtab (abfd);
   4048   1.1     skrll 	}
   4049   1.6  christos     }
   4050   1.6  christos 
   4051   1.6  christos   /* Delay setting sh_name to _bfd_elf_write_object_contents so that
   4052   1.6  christos      _bfd_elf_assign_file_positions_for_non_load can convert DWARF
   4053   1.1     skrll      debug section name from .debug_* to .zdebug_* if needed.  */
   4054   1.1     skrll 
   4055   1.1     skrll   return TRUE;
   4056   1.1     skrll }
   4057   1.1     skrll 
   4058   1.1     skrll static bfd_boolean
   4059   1.1     skrll sym_is_global (bfd *abfd, asymbol *sym)
   4060   1.1     skrll {
   4061   1.1     skrll   /* If the backend has a special mapping, use it.  */
   4062   1.1     skrll   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4063   1.1     skrll   if (bed->elf_backend_sym_is_global)
   4064   1.3  christos     return (*bed->elf_backend_sym_is_global) (abfd, sym);
   4065  1.15  christos 
   4066  1.15  christos   return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
   4067   1.1     skrll 	  || bfd_is_und_section (bfd_asymbol_section (sym))
   4068   1.1     skrll 	  || bfd_is_com_section (bfd_asymbol_section (sym)));
   4069   1.9  christos }
   4070   1.9  christos 
   4071   1.9  christos /* Filter global symbols of ABFD to include in the import library.  All
   4072   1.9  christos    SYMCOUNT symbols of ABFD can be examined from their pointers in
   4073   1.9  christos    SYMS.  Pointers of symbols to keep should be stored contiguously at
   4074   1.9  christos    the beginning of that array.
   4075   1.9  christos 
   4076   1.9  christos    Returns the number of symbols to keep.  */
   4077   1.9  christos 
   4078   1.9  christos unsigned int
   4079   1.9  christos _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
   4080   1.9  christos 				asymbol **syms, long symcount)
   4081   1.9  christos {
   4082   1.9  christos   long src_count, dst_count = 0;
   4083   1.9  christos 
   4084   1.9  christos   for (src_count = 0; src_count < symcount; src_count++)
   4085   1.9  christos     {
   4086   1.9  christos       asymbol *sym = syms[src_count];
   4087   1.9  christos       char *name = (char *) bfd_asymbol_name (sym);
   4088   1.9  christos       struct bfd_link_hash_entry *h;
   4089   1.9  christos 
   4090   1.9  christos       if (!sym_is_global (abfd, sym))
   4091   1.9  christos 	continue;
   4092   1.9  christos 
   4093   1.9  christos       h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
   4094   1.9  christos       if (h == NULL)
   4095   1.9  christos 	continue;
   4096   1.9  christos       if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
   4097   1.9  christos 	continue;
   4098   1.9  christos       if (h->linker_def || h->ldscript_def)
   4099   1.9  christos 	continue;
   4100   1.9  christos 
   4101   1.9  christos       syms[dst_count++] = sym;
   4102   1.9  christos     }
   4103   1.9  christos 
   4104   1.9  christos   syms[dst_count] = NULL;
   4105   1.9  christos 
   4106   1.9  christos   return dst_count;
   4107   1.1     skrll }
   4108   1.5  christos 
   4109   1.1     skrll /* Don't output section symbols for sections that are not going to be
   4110   1.1     skrll    output, that are duplicates or there is no BFD section.  */
   4111   1.1     skrll 
   4112   1.1     skrll static bfd_boolean
   4113   1.5  christos ignore_section_sym (bfd *abfd, asymbol *sym)
   4114   1.5  christos {
   4115  1.13  christos   elf_symbol_type *type_ptr;
   4116  1.13  christos 
   4117  1.13  christos   if (sym == NULL)
   4118   1.5  christos     return FALSE;
   4119   1.5  christos 
   4120   1.5  christos   if ((sym->flags & BSF_SECTION_SYM) == 0)
   4121  1.13  christos     return FALSE;
   4122  1.13  christos 
   4123  1.13  christos   if (sym->section == NULL)
   4124   1.5  christos     return TRUE;
   4125   1.5  christos 
   4126   1.5  christos   type_ptr = elf_symbol_from (abfd, sym);
   4127   1.5  christos   return ((type_ptr != NULL
   4128   1.5  christos 	   && type_ptr->internal_elf_sym.st_shndx != 0
   4129  1.13  christos 	   && bfd_is_abs_section (sym->section))
   4130  1.13  christos 	  || !(sym->section->owner == abfd
   4131   1.5  christos 	       || (sym->section->output_section != NULL
   4132   1.5  christos 		   && sym->section->output_section->owner == abfd
   4133   1.1     skrll 		   && sym->section->output_offset == 0)
   4134   1.1     skrll 	       || bfd_is_abs_section (sym->section)));
   4135   1.5  christos }
   4136   1.5  christos 
   4137   1.5  christos /* Map symbol from it's internal number to the external number, moving
   4138   1.1     skrll    all local symbols to be at the head of the list.  */
   4139   1.6  christos 
   4140   1.1     skrll static bfd_boolean
   4141   1.1     skrll elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
   4142   1.1     skrll {
   4143   1.1     skrll   unsigned int symcount = bfd_get_symcount (abfd);
   4144   1.1     skrll   asymbol **syms = bfd_get_outsymbols (abfd);
   4145   1.1     skrll   asymbol **sect_syms;
   4146   1.1     skrll   unsigned int num_locals = 0;
   4147   1.1     skrll   unsigned int num_globals = 0;
   4148   1.6  christos   unsigned int num_locals2 = 0;
   4149   1.1     skrll   unsigned int num_globals2 = 0;
   4150   1.1     skrll   unsigned int max_index = 0;
   4151   1.1     skrll   unsigned int idx;
   4152   1.1     skrll   asection *asect;
   4153   1.1     skrll   asymbol **new_syms;
   4154   1.1     skrll 
   4155   1.1     skrll #ifdef DEBUG
   4156   1.1     skrll   fprintf (stderr, "elf_map_symbols\n");
   4157   1.1     skrll   fflush (stderr);
   4158   1.1     skrll #endif
   4159   1.1     skrll 
   4160   1.1     skrll   for (asect = abfd->sections; asect; asect = asect->next)
   4161   1.1     skrll     {
   4162   1.1     skrll       if (max_index < asect->index)
   4163   1.1     skrll 	max_index = asect->index;
   4164   1.1     skrll     }
   4165   1.3  christos 
   4166   1.1     skrll   max_index++;
   4167   1.1     skrll   sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
   4168   1.1     skrll   if (sect_syms == NULL)
   4169   1.1     skrll     return FALSE;
   4170   1.1     skrll   elf_section_syms (abfd) = sect_syms;
   4171   1.1     skrll   elf_num_section_syms (abfd) = max_index;
   4172   1.1     skrll 
   4173   1.1     skrll   /* Init sect_syms entries for any section symbols we have already
   4174   1.1     skrll      decided to output.  */
   4175   1.1     skrll   for (idx = 0; idx < symcount; idx++)
   4176   1.1     skrll     {
   4177   1.1     skrll       asymbol *sym = syms[idx];
   4178   1.1     skrll 
   4179   1.5  christos       if ((sym->flags & BSF_SECTION_SYM) != 0
   4180   1.5  christos 	  && sym->value == 0
   4181   1.1     skrll 	  && !ignore_section_sym (abfd, sym)
   4182   1.1     skrll 	  && !bfd_is_abs_section (sym->section))
   4183   1.1     skrll 	{
   4184   1.1     skrll 	  asection *sec = sym->section;
   4185   1.1     skrll 
   4186   1.1     skrll 	  if (sec->owner != abfd)
   4187   1.1     skrll 	    sec = sec->output_section;
   4188   1.1     skrll 
   4189   1.1     skrll 	  sect_syms[sec->index] = syms[idx];
   4190   1.1     skrll 	}
   4191   1.1     skrll     }
   4192   1.1     skrll 
   4193   1.1     skrll   /* Classify all of the symbols.  */
   4194   1.5  christos   for (idx = 0; idx < symcount; idx++)
   4195   1.5  christos     {
   4196   1.5  christos       if (sym_is_global (abfd, syms[idx]))
   4197   1.1     skrll 	num_globals++;
   4198   1.1     skrll       else if (!ignore_section_sym (abfd, syms[idx]))
   4199   1.1     skrll 	num_locals++;
   4200   1.1     skrll     }
   4201   1.1     skrll 
   4202   1.1     skrll   /* We will be adding a section symbol for each normal BFD section.  Most
   4203   1.1     skrll      sections will already have a section symbol in outsymbols, but
   4204   1.1     skrll      eg. SHT_GROUP sections will not, and we need the section symbol mapped
   4205   1.1     skrll      at least in that case.  */
   4206   1.1     skrll   for (asect = abfd->sections; asect; asect = asect->next)
   4207   1.1     skrll     {
   4208   1.1     skrll       if (sect_syms[asect->index] == NULL)
   4209   1.1     skrll 	{
   4210   1.1     skrll 	  if (!sym_is_global (abfd, asect->symbol))
   4211   1.1     skrll 	    num_locals++;
   4212   1.1     skrll 	  else
   4213   1.1     skrll 	    num_globals++;
   4214   1.1     skrll 	}
   4215   1.1     skrll     }
   4216   1.3  christos 
   4217   1.9  christos   /* Now sort the symbols so the local symbols are first.  */
   4218   1.1     skrll   new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
   4219   1.1     skrll 				      sizeof (asymbol *));
   4220   1.1     skrll 
   4221   1.1     skrll   if (new_syms == NULL)
   4222   1.1     skrll     return FALSE;
   4223   1.1     skrll 
   4224   1.1     skrll   for (idx = 0; idx < symcount; idx++)
   4225   1.1     skrll     {
   4226   1.1     skrll       asymbol *sym = syms[idx];
   4227   1.5  christos       unsigned int i;
   4228   1.5  christos 
   4229   1.5  christos       if (sym_is_global (abfd, sym))
   4230   1.1     skrll 	i = num_locals + num_globals2++;
   4231   1.1     skrll       else if (!ignore_section_sym (abfd, sym))
   4232   1.5  christos 	i = num_locals2++;
   4233   1.1     skrll       else
   4234   1.1     skrll 	continue;
   4235   1.1     skrll       new_syms[i] = sym;
   4236   1.1     skrll       sym->udata.i = i + 1;
   4237   1.1     skrll     }
   4238   1.1     skrll   for (asect = abfd->sections; asect; asect = asect->next)
   4239   1.1     skrll     {
   4240   1.1     skrll       if (sect_syms[asect->index] == NULL)
   4241   1.1     skrll 	{
   4242   1.1     skrll 	  asymbol *sym = asect->symbol;
   4243   1.1     skrll 	  unsigned int i;
   4244   1.1     skrll 
   4245   1.1     skrll 	  sect_syms[asect->index] = sym;
   4246   1.1     skrll 	  if (!sym_is_global (abfd, sym))
   4247   1.1     skrll 	    i = num_locals2++;
   4248   1.1     skrll 	  else
   4249   1.1     skrll 	    i = num_locals + num_globals2++;
   4250   1.1     skrll 	  new_syms[i] = sym;
   4251   1.1     skrll 	  sym->udata.i = i + 1;
   4252   1.1     skrll 	}
   4253   1.1     skrll     }
   4254   1.1     skrll 
   4255   1.6  christos   bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
   4256   1.1     skrll 
   4257   1.1     skrll   *pnum_locals = num_locals;
   4258   1.1     skrll   return TRUE;
   4259   1.1     skrll }
   4260   1.1     skrll 
   4261   1.1     skrll /* Align to the maximum file alignment that could be required for any
   4262   1.1     skrll    ELF data structure.  */
   4263   1.1     skrll 
   4264   1.1     skrll static inline file_ptr
   4265   1.1     skrll align_file_position (file_ptr off, int align)
   4266   1.1     skrll {
   4267   1.1     skrll   return (off + align - 1) & ~(align - 1);
   4268   1.1     skrll }
   4269   1.1     skrll 
   4270   1.1     skrll /* Assign a file position to a section, optionally aligning to the
   4271   1.1     skrll    required section alignment.  */
   4272   1.1     skrll 
   4273   1.1     skrll file_ptr
   4274   1.1     skrll _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
   4275   1.1     skrll 					   file_ptr offset,
   4276   1.1     skrll 					   bfd_boolean align)
   4277   1.1     skrll {
   4278   1.1     skrll   if (align && i_shdrp->sh_addralign > 1)
   4279   1.1     skrll     offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
   4280   1.1     skrll   i_shdrp->sh_offset = offset;
   4281   1.1     skrll   if (i_shdrp->bfd_section != NULL)
   4282   1.1     skrll     i_shdrp->bfd_section->filepos = offset;
   4283   1.1     skrll   if (i_shdrp->sh_type != SHT_NOBITS)
   4284   1.1     skrll     offset += i_shdrp->sh_size;
   4285   1.1     skrll   return offset;
   4286   1.1     skrll }
   4287   1.1     skrll 
   4288   1.1     skrll /* Compute the file positions we are going to put the sections at, and
   4289   1.1     skrll    otherwise prepare to begin writing out the ELF file.  If LINK_INFO
   4290   1.1     skrll    is not NULL, this is being called by the ELF backend linker.  */
   4291   1.1     skrll 
   4292   1.1     skrll bfd_boolean
   4293   1.1     skrll _bfd_elf_compute_section_file_positions (bfd *abfd,
   4294   1.1     skrll 					 struct bfd_link_info *link_info)
   4295   1.3  christos {
   4296   1.1     skrll   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4297   1.6  christos   struct fake_section_arg fsargs;
   4298   1.1     skrll   bfd_boolean failed;
   4299   1.3  christos   struct elf_strtab_hash *strtab = NULL;
   4300   1.1     skrll   Elf_Internal_Shdr *shstrtab_hdr;
   4301   1.1     skrll   bfd_boolean need_symtab;
   4302   1.1     skrll 
   4303   1.1     skrll   if (abfd->output_has_begun)
   4304   1.1     skrll     return TRUE;
   4305   1.1     skrll 
   4306   1.1     skrll   /* Do any elf backend specific processing first.  */
   4307   1.1     skrll   if (bed->elf_backend_begin_write_processing)
   4308  1.15  christos     (*bed->elf_backend_begin_write_processing) (abfd, link_info);
   4309   1.1     skrll 
   4310   1.1     skrll   if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
   4311   1.3  christos     return FALSE;
   4312   1.3  christos 
   4313   1.3  christos   fsargs.failed = FALSE;
   4314   1.3  christos   fsargs.link_info = link_info;
   4315   1.1     skrll   bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
   4316   1.1     skrll   if (fsargs.failed)
   4317   1.1     skrll     return FALSE;
   4318   1.1     skrll 
   4319   1.1     skrll   if (!assign_section_numbers (abfd, link_info))
   4320   1.1     skrll     return FALSE;
   4321   1.3  christos 
   4322   1.3  christos   /* The backend linker builds symbol table information itself.  */
   4323   1.3  christos   need_symtab = (link_info == NULL
   4324   1.3  christos 		 && (bfd_get_symcount (abfd) > 0
   4325   1.3  christos 		     || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
   4326   1.1     skrll 			 == HAS_RELOC)));
   4327   1.1     skrll   if (need_symtab)
   4328   1.1     skrll     {
   4329   1.1     skrll       /* Non-zero if doing a relocatable link.  */
   4330   1.1     skrll       int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
   4331   1.1     skrll 
   4332   1.1     skrll       if (! swap_out_syms (abfd, &strtab, relocatable_p))
   4333   1.1     skrll 	return FALSE;
   4334   1.3  christos     }
   4335   1.1     skrll 
   4336   1.1     skrll   failed = FALSE;
   4337   1.1     skrll   if (link_info == NULL)
   4338   1.1     skrll     {
   4339   1.1     skrll       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
   4340   1.1     skrll       if (failed)
   4341   1.1     skrll 	return FALSE;
   4342   1.1     skrll     }
   4343  1.15  christos 
   4344   1.1     skrll   shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
   4345   1.7  christos   /* sh_name was set in init_file_header.  */
   4346   1.1     skrll   shstrtab_hdr->sh_type = SHT_STRTAB;
   4347   1.6  christos   shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   4348   1.1     skrll   shstrtab_hdr->sh_addr = 0;
   4349   1.1     skrll   /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
   4350   1.1     skrll   shstrtab_hdr->sh_entsize = 0;
   4351   1.6  christos   shstrtab_hdr->sh_link = 0;
   4352   1.1     skrll   shstrtab_hdr->sh_info = 0;
   4353   1.1     skrll   /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
   4354   1.1     skrll   shstrtab_hdr->sh_addralign = 1;
   4355   1.1     skrll 
   4356   1.1     skrll   if (!assign_file_positions_except_relocs (abfd, link_info))
   4357   1.3  christos     return FALSE;
   4358   1.1     skrll 
   4359   1.1     skrll   if (need_symtab)
   4360   1.1     skrll     {
   4361   1.1     skrll       file_ptr off;
   4362   1.6  christos       Elf_Internal_Shdr *hdr;
   4363   1.1     skrll 
   4364   1.6  christos       off = elf_next_file_pos (abfd);
   4365   1.1     skrll 
   4366   1.1     skrll       hdr = & elf_symtab_hdr (abfd);
   4367   1.6  christos       off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
   4368   1.6  christos 
   4369   1.6  christos       if (elf_symtab_shndx_list (abfd) != NULL)
   4370   1.6  christos 	{
   4371   1.6  christos 	  hdr = & elf_symtab_shndx_list (abfd)->hdr;
   4372   1.6  christos 	  if (hdr->sh_size != 0)
   4373   1.6  christos 	    off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
   4374   1.1     skrll 	  /* FIXME: What about other symtab_shndx sections in the list ?  */
   4375   1.1     skrll 	}
   4376   1.1     skrll 
   4377   1.1     skrll       hdr = &elf_tdata (abfd)->strtab_hdr;
   4378   1.6  christos       off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
   4379   1.1     skrll 
   4380   1.1     skrll       elf_next_file_pos (abfd) = off;
   4381   1.1     skrll 
   4382   1.1     skrll       /* Now that we know where the .strtab section goes, write it
   4383   1.6  christos 	 out.  */
   4384   1.1     skrll       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   4385   1.6  christos 	  || ! _bfd_elf_strtab_emit (abfd, strtab))
   4386   1.1     skrll 	return FALSE;
   4387   1.1     skrll       _bfd_elf_strtab_free (strtab);
   4388   1.1     skrll     }
   4389   1.1     skrll 
   4390   1.1     skrll   abfd->output_has_begun = TRUE;
   4391   1.1     skrll 
   4392   1.1     skrll   return TRUE;
   4393   1.1     skrll }
   4394   1.1     skrll 
   4395   1.1     skrll /* Make an initial estimate of the size of the program header.  If we
   4396   1.1     skrll    get the number wrong here, we'll redo section placement.  */
   4397   1.1     skrll 
   4398   1.1     skrll static bfd_size_type
   4399   1.1     skrll get_program_header_size (bfd *abfd, struct bfd_link_info *info)
   4400  1.12     joerg {
   4401   1.1     skrll   size_t segs;
   4402   1.1     skrll   asection *s, *s2;
   4403   1.1     skrll   const struct elf_backend_data *bed;
   4404   1.1     skrll 
   4405   1.1     skrll   /* Assume we will need exactly two PT_LOAD segments: one for text
   4406   1.1     skrll      and one for data.  */
   4407   1.1     skrll   segs = 2;
   4408  1.12     joerg 
   4409  1.15  christos   s = bfd_get_section_by_name (abfd, ".interp");
   4410   1.1     skrll   s2 = bfd_get_section_by_name (abfd, ".dynamic");
   4411  1.12     joerg   if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
   4412   1.1     skrll     {
   4413   1.1     skrll       ++segs;
   4414  1.12     joerg     }
   4415   1.1     skrll 
   4416   1.1     skrll   if (s2 != NULL && (s2->flags & SEC_LOAD) != 0)
   4417   1.1     skrll     {
   4418   1.1     skrll       /* We need a PT_DYNAMIC segment.  */
   4419   1.1     skrll       ++segs;
   4420  1.12     joerg     }
   4421  1.12     joerg 
   4422  1.12     joerg   if ((s != NULL && (s->flags & SEC_LOAD) != 0) ||
   4423  1.12     joerg       (s2 != NULL && (s2->flags & SEC_LOAD) != 0))
   4424  1.12     joerg     {
   4425  1.12     joerg       /*
   4426  1.12     joerg        * If either a PT_INTERP or PT_DYNAMIC segment is created,
   4427  1.12     joerg        * also create a PT_PHDR segment.
   4428  1.12     joerg        */
   4429  1.12     joerg       ++segs;
   4430   1.1     skrll     }
   4431   1.1     skrll 
   4432   1.1     skrll   if (info != NULL && info->relro)
   4433   1.1     skrll     {
   4434   1.1     skrll       /* We need a PT_GNU_RELRO segment.  */
   4435   1.1     skrll       ++segs;
   4436   1.6  christos     }
   4437   1.1     skrll 
   4438   1.1     skrll   if (elf_eh_frame_hdr (abfd))
   4439   1.1     skrll     {
   4440   1.1     skrll       /* We need a PT_GNU_EH_FRAME segment.  */
   4441   1.1     skrll       ++segs;
   4442   1.6  christos     }
   4443   1.1     skrll 
   4444   1.1     skrll   if (elf_stack_flags (abfd))
   4445   1.1     skrll     {
   4446   1.1     skrll       /* We need a PT_GNU_STACK segment.  */
   4447   1.1     skrll       ++segs;
   4448  1.15  christos     }
   4449  1.15  christos 
   4450  1.15  christos   s = bfd_get_section_by_name (abfd,
   4451  1.15  christos 			       NOTE_GNU_PROPERTY_SECTION_NAME);
   4452  1.15  christos   if (s != NULL && s->size != 0)
   4453  1.15  christos     {
   4454  1.15  christos       /* We need a PT_GNU_PROPERTY segment.  */
   4455  1.15  christos       ++segs;
   4456   1.1     skrll     }
   4457   1.1     skrll 
   4458   1.1     skrll   for (s = abfd->sections; s != NULL; s = s->next)
   4459  1.15  christos     {
   4460   1.1     skrll       if ((s->flags & SEC_LOAD) != 0
   4461  1.15  christos 	  && elf_section_type (s) == SHT_NOTE)
   4462   1.1     skrll 	{
   4463   1.1     skrll 	  unsigned int alignment_power;
   4464  1.15  christos 	  /* We need a PT_NOTE segment.  */
   4465  1.15  christos 	  ++segs;
   4466  1.15  christos 	  /* Try to create just one PT_NOTE segment for all adjacent
   4467  1.15  christos 	     loadable SHT_NOTE sections.  gABI requires that within a
   4468  1.15  christos 	     PT_NOTE segment (and also inside of each SHT_NOTE section)
   4469  1.15  christos 	     each note should have the same alignment.  So we check
   4470  1.15  christos 	     whether the sections are correctly aligned.  */
   4471  1.15  christos 	  alignment_power = s->alignment_power;
   4472  1.15  christos 	  while (s->next != NULL
   4473  1.15  christos 		 && s->next->alignment_power == alignment_power
   4474  1.15  christos 		 && (s->next->flags & SEC_LOAD) != 0
   4475   1.1     skrll 		 && elf_section_type (s->next) == SHT_NOTE)
   4476   1.1     skrll 	    s = s->next;
   4477   1.1     skrll 	}
   4478   1.1     skrll     }
   4479   1.1     skrll 
   4480   1.1     skrll   for (s = abfd->sections; s != NULL; s = s->next)
   4481   1.1     skrll     {
   4482   1.1     skrll       if (s->flags & SEC_THREAD_LOCAL)
   4483   1.1     skrll 	{
   4484   1.1     skrll 	  /* We need a PT_TLS segment.  */
   4485   1.1     skrll 	  ++segs;
   4486   1.1     skrll 	  break;
   4487   1.1     skrll 	}
   4488   1.1     skrll     }
   4489   1.9  christos 
   4490  1.15  christos   bed = get_elf_backend_data (abfd);
   4491  1.15  christos 
   4492  1.15  christos   if ((abfd->flags & D_PAGED) != 0
   4493  1.15  christos       && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
   4494  1.15  christos     {
   4495  1.15  christos       /* Add a PT_GNU_MBIND segment for each mbind section.  */
   4496  1.15  christos       unsigned int page_align_power = bfd_log2 (bed->commonpagesize);
   4497  1.15  christos       for (s = abfd->sections; s != NULL; s = s->next)
   4498  1.15  christos 	if (elf_section_flags (s) & SHF_GNU_MBIND)
   4499  1.15  christos 	  {
   4500  1.15  christos 	    if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
   4501  1.15  christos 	      {
   4502  1.15  christos 		_bfd_error_handler
   4503  1.15  christos 		  /* xgettext:c-format */
   4504  1.15  christos 		  (_("%pB: GNU_MBIND section `%pA' has invalid "
   4505  1.15  christos 		     "sh_info field: %d"),
   4506  1.15  christos 		   abfd, s, elf_section_data (s)->this_hdr.sh_info);
   4507  1.15  christos 		continue;
   4508  1.15  christos 	      }
   4509  1.15  christos 	    /* Align mbind section to page size.  */
   4510  1.15  christos 	    if (s->alignment_power < page_align_power)
   4511  1.15  christos 	      s->alignment_power = page_align_power;
   4512  1.15  christos 	    segs ++;
   4513   1.9  christos 	  }
   4514  1.15  christos     }
   4515  1.15  christos 
   4516   1.1     skrll   /* Let the backend count up any program headers it might need.  */
   4517   1.1     skrll   if (bed->elf_backend_additional_program_headers)
   4518   1.1     skrll     {
   4519   1.1     skrll       int a;
   4520   1.1     skrll 
   4521   1.1     skrll       a = (*bed->elf_backend_additional_program_headers) (abfd, info);
   4522   1.1     skrll       if (a == -1)
   4523   1.1     skrll 	abort ();
   4524   1.1     skrll       segs += a;
   4525   1.1     skrll     }
   4526   1.1     skrll 
   4527   1.1     skrll   return segs * bed->s->sizeof_phdr;
   4528   1.1     skrll }
   4529   1.1     skrll 
   4530   1.1     skrll /* Find the segment that contains the output_section of section.  */
   4531   1.1     skrll 
   4532   1.1     skrll Elf_Internal_Phdr *
   4533   1.1     skrll _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
   4534   1.1     skrll {
   4535   1.1     skrll   struct elf_segment_map *m;
   4536   1.6  christos   Elf_Internal_Phdr *p;
   4537   1.1     skrll 
   4538   1.1     skrll   for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
   4539   1.1     skrll        m != NULL;
   4540   1.1     skrll        m = m->next, p++)
   4541   1.1     skrll     {
   4542   1.1     skrll       int i;
   4543   1.1     skrll 
   4544   1.1     skrll       for (i = m->count - 1; i >= 0; i--)
   4545   1.1     skrll 	if (m->sections[i] == section)
   4546   1.1     skrll 	  return p;
   4547   1.1     skrll     }
   4548   1.1     skrll 
   4549   1.1     skrll   return NULL;
   4550   1.1     skrll }
   4551   1.1     skrll 
   4552   1.1     skrll /* Create a mapping from a set of sections to a program segment.  */
   4553   1.1     skrll 
   4554   1.1     skrll static struct elf_segment_map *
   4555   1.1     skrll make_mapping (bfd *abfd,
   4556   1.1     skrll 	      asection **sections,
   4557   1.1     skrll 	      unsigned int from,
   4558   1.1     skrll 	      unsigned int to,
   4559   1.1     skrll 	      bfd_boolean phdr)
   4560   1.1     skrll {
   4561   1.1     skrll   struct elf_segment_map *m;
   4562   1.1     skrll   unsigned int i;
   4563   1.1     skrll   asection **hdrpp;
   4564  1.15  christos   bfd_size_type amt;
   4565  1.15  christos 
   4566   1.3  christos   amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   4567   1.1     skrll   amt += (to - from) * sizeof (asection *);
   4568   1.1     skrll   m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4569   1.1     skrll   if (m == NULL)
   4570   1.1     skrll     return NULL;
   4571   1.1     skrll   m->next = NULL;
   4572   1.1     skrll   m->p_type = PT_LOAD;
   4573   1.1     skrll   for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
   4574   1.1     skrll     m->sections[i - from] = *hdrpp;
   4575   1.1     skrll   m->count = to - from;
   4576   1.1     skrll 
   4577   1.1     skrll   if (from == 0 && phdr)
   4578   1.1     skrll     {
   4579   1.1     skrll       /* Include the headers in the first PT_LOAD segment.  */
   4580   1.1     skrll       m->includes_filehdr = 1;
   4581   1.1     skrll       m->includes_phdrs = 1;
   4582   1.1     skrll     }
   4583   1.1     skrll 
   4584   1.1     skrll   return m;
   4585   1.1     skrll }
   4586   1.1     skrll 
   4587   1.1     skrll /* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
   4588   1.1     skrll    on failure.  */
   4589   1.1     skrll 
   4590   1.1     skrll struct elf_segment_map *
   4591   1.1     skrll _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
   4592   1.1     skrll {
   4593   1.3  christos   struct elf_segment_map *m;
   4594   1.9  christos 
   4595   1.1     skrll   m = (struct elf_segment_map *) bfd_zalloc (abfd,
   4596   1.1     skrll 					     sizeof (struct elf_segment_map));
   4597   1.1     skrll   if (m == NULL)
   4598   1.1     skrll     return NULL;
   4599   1.1     skrll   m->next = NULL;
   4600   1.1     skrll   m->p_type = PT_DYNAMIC;
   4601   1.1     skrll   m->count = 1;
   4602   1.1     skrll   m->sections[0] = dynsec;
   4603   1.1     skrll 
   4604   1.1     skrll   return m;
   4605   1.1     skrll }
   4606   1.1     skrll 
   4607   1.1     skrll /* Possibly add or remove segments from the segment map.  */
   4608   1.1     skrll 
   4609   1.1     skrll static bfd_boolean
   4610   1.1     skrll elf_modify_segment_map (bfd *abfd,
   4611   1.1     skrll 			struct bfd_link_info *info,
   4612   1.1     skrll 			bfd_boolean remove_empty_load)
   4613   1.1     skrll {
   4614   1.1     skrll   struct elf_segment_map **m;
   4615   1.1     skrll   const struct elf_backend_data *bed;
   4616   1.1     skrll 
   4617   1.1     skrll   /* The placement algorithm assumes that non allocated sections are
   4618   1.1     skrll      not in PT_LOAD segments.  We ensure this here by removing such
   4619   1.1     skrll      sections from the segment map.  We also remove excluded
   4620   1.6  christos      sections.  Finally, any PT_LOAD segment without sections is
   4621   1.1     skrll      removed.  */
   4622   1.1     skrll   m = &elf_seg_map (abfd);
   4623   1.1     skrll   while (*m)
   4624   1.1     skrll     {
   4625   1.1     skrll       unsigned int i, new_count;
   4626   1.1     skrll 
   4627   1.1     skrll       for (new_count = 0, i = 0; i < (*m)->count; i++)
   4628   1.1     skrll 	{
   4629   1.1     skrll 	  if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
   4630   1.1     skrll 	      && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
   4631   1.1     skrll 		  || (*m)->p_type != PT_LOAD))
   4632   1.1     skrll 	    {
   4633   1.1     skrll 	      (*m)->sections[new_count] = (*m)->sections[i];
   4634   1.1     skrll 	      new_count++;
   4635   1.1     skrll 	    }
   4636   1.1     skrll 	}
   4637   1.9  christos       (*m)->count = new_count;
   4638   1.9  christos 
   4639   1.9  christos       if (remove_empty_load
   4640   1.9  christos 	  && (*m)->p_type == PT_LOAD
   4641   1.1     skrll 	  && (*m)->count == 0
   4642   1.1     skrll 	  && !(*m)->includes_phdrs)
   4643   1.1     skrll 	*m = (*m)->next;
   4644   1.1     skrll       else
   4645   1.1     skrll 	m = &(*m)->next;
   4646   1.1     skrll     }
   4647   1.1     skrll 
   4648   1.1     skrll   bed = get_elf_backend_data (abfd);
   4649   1.1     skrll   if (bed->elf_backend_modify_segment_map != NULL)
   4650   1.1     skrll     {
   4651   1.1     skrll       if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
   4652   1.1     skrll 	return FALSE;
   4653   1.1     skrll     }
   4654   1.1     skrll 
   4655   1.1     skrll   return TRUE;
   4656  1.13  christos }
   4657  1.13  christos 
   4658  1.13  christos #define IS_TBSS(s) \
   4659   1.1     skrll   ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
   4660   1.1     skrll 
   4661   1.1     skrll /* Set up a mapping from BFD sections to program segments.  */
   4662   1.1     skrll 
   4663   1.1     skrll bfd_boolean
   4664   1.1     skrll _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
   4665   1.1     skrll {
   4666   1.1     skrll   unsigned int count;
   4667   1.1     skrll   struct elf_segment_map *m;
   4668   1.1     skrll   asection **sections = NULL;
   4669   1.1     skrll   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4670   1.6  christos   bfd_boolean no_user_phdrs;
   4671   1.5  christos 
   4672   1.5  christos   no_user_phdrs = elf_seg_map (abfd) == NULL;
   4673   1.5  christos 
   4674   1.5  christos   if (info != NULL)
   4675   1.1     skrll     info->user_phdrs = !no_user_phdrs;
   4676   1.1     skrll 
   4677   1.1     skrll   if (no_user_phdrs && bfd_count_sections (abfd) != 0)
   4678   1.1     skrll     {
   4679   1.1     skrll       asection *s;
   4680   1.1     skrll       unsigned int i;
   4681   1.1     skrll       struct elf_segment_map *mfirst;
   4682   1.1     skrll       struct elf_segment_map **pm;
   4683  1.15  christos       asection *last_hdr;
   4684   1.1     skrll       bfd_vma last_size;
   4685   1.1     skrll       unsigned int hdr_index;
   4686  1.15  christos       bfd_vma maxpagesize;
   4687   1.1     skrll       asection **hdrpp;
   4688   1.9  christos       bfd_boolean phdr_in_segment;
   4689   1.1     skrll       bfd_boolean writable;
   4690   1.1     skrll       bfd_boolean executable;
   4691   1.9  christos       int tls_count = 0;
   4692   1.1     skrll       asection *first_tls = NULL;
   4693   1.1     skrll       asection *first_mbind = NULL;
   4694   1.3  christos       asection *dynsec, *eh_frame_hdr;
   4695  1.15  christos       bfd_size_type amt;
   4696   1.1     skrll       bfd_vma addr_mask, wrap_to = 0;
   4697   1.1     skrll       bfd_size_type phdr_size;
   4698   1.1     skrll 
   4699   1.3  christos       /* Select the allocated sections, and sort them.  */
   4700   1.9  christos 
   4701   1.1     skrll       sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
   4702   1.1     skrll 					    sizeof (asection *));
   4703   1.1     skrll       if (sections == NULL)
   4704   1.3  christos 	goto error_return;
   4705   1.3  christos 
   4706   1.3  christos       /* Calculate top address, avoiding undefined behaviour of shift
   4707   1.3  christos 	 left operator when shift count is equal to size of type
   4708   1.3  christos 	 being shifted.  */
   4709   1.3  christos       addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
   4710   1.1     skrll       addr_mask = (addr_mask << 1) + 1;
   4711   1.1     skrll 
   4712   1.1     skrll       i = 0;
   4713   1.1     skrll       for (s = abfd->sections; s != NULL; s = s->next)
   4714   1.1     skrll 	{
   4715  1.15  christos 	  if ((s->flags & SEC_ALLOC) != 0)
   4716  1.15  christos 	    {
   4717  1.15  christos 	      /* target_index is unused until bfd_elf_final_link
   4718  1.15  christos 		 starts output of section symbols.  Use it to make
   4719   1.1     skrll 		 qsort stable.  */
   4720   1.1     skrll 	      s->target_index = i;
   4721   1.3  christos 	      sections[i] = s;
   4722   1.3  christos 	      ++i;
   4723   1.3  christos 	      /* A wrapping section potentially clashes with header.  */
   4724   1.1     skrll 	      if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
   4725   1.1     skrll 		wrap_to = (s->lma + s->size) & addr_mask;
   4726   1.1     skrll 	    }
   4727   1.1     skrll 	}
   4728   1.1     skrll       BFD_ASSERT (i <= bfd_count_sections (abfd));
   4729   1.1     skrll       count = i;
   4730   1.1     skrll 
   4731  1.15  christos       qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
   4732  1.15  christos 
   4733  1.15  christos       phdr_size = elf_program_header_size (abfd);
   4734  1.15  christos       if (phdr_size == (bfd_size_type) -1)
   4735  1.15  christos 	phdr_size = get_program_header_size (abfd, info);
   4736  1.15  christos       phdr_size += bed->s->sizeof_ehdr;
   4737  1.15  christos       maxpagesize = bed->maxpagesize;
   4738  1.15  christos       if (maxpagesize == 0)
   4739  1.15  christos 	maxpagesize = 1;
   4740  1.15  christos       phdr_in_segment = info != NULL && info->load_phdrs;
   4741  1.15  christos       if (count != 0
   4742  1.15  christos 	  && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
   4743  1.15  christos 	      >= (phdr_size & (maxpagesize - 1))))
   4744  1.15  christos 	/* For compatibility with old scripts that may not be using
   4745  1.15  christos 	   SIZEOF_HEADERS, add headers when it looks like space has
   4746  1.15  christos 	   been left for them.  */
   4747   1.1     skrll 	phdr_in_segment = TRUE;
   4748   1.1     skrll 
   4749   1.1     skrll       /* Build the mapping.  */
   4750   1.1     skrll       mfirst = NULL;
   4751   1.1     skrll       pm = &mfirst;
   4752   1.1     skrll 
   4753   1.1     skrll       /* If we have a .interp section, then create a PT_PHDR segment for
   4754   1.1     skrll 	 the program headers and a PT_INTERP segment for the .interp
   4755  1.12     joerg 	 section.  */
   4756  1.15  christos       s = bfd_get_section_by_name (abfd, ".interp");
   4757  1.12     joerg       if (s != NULL && (s->flags & SEC_LOAD) == 0)
   4758  1.12     joerg       if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
   4759  1.12     joerg 	s = NULL;
   4760  1.12     joerg       dynsec = bfd_get_section_by_name (abfd, ".dynamic");
   4761  1.12     joerg       if (dynsec != NULL && (dynsec->flags & SEC_LOAD) == 0)
   4762  1.12     joerg 	dynsec = NULL;
   4763   1.1     skrll 
   4764   1.1     skrll       if (s != NULL || dynsec != NULL)
   4765   1.3  christos 	{
   4766   1.1     skrll 	  amt = sizeof (struct elf_segment_map);
   4767   1.1     skrll 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4768   1.1     skrll 	  if (m == NULL)
   4769   1.1     skrll 	    goto error_return;
   4770   1.9  christos 	  m->next = NULL;
   4771   1.1     skrll 	  m->p_type = PT_PHDR;
   4772   1.1     skrll 	  m->p_flags = PF_R;
   4773  1.15  christos 	  m->p_flags_valid = 1;
   4774   1.1     skrll 	  m->includes_phdrs = 1;
   4775   1.1     skrll 	  phdr_in_segment = TRUE;
   4776  1.12     joerg 	  *pm = m;
   4777   1.1     skrll 	  pm = &m->next;
   4778  1.12     joerg 	}
   4779  1.12     joerg 
   4780   1.1     skrll       if (s != NULL)
   4781   1.3  christos 	{
   4782   1.1     skrll 	  amt = sizeof (struct elf_segment_map);
   4783   1.1     skrll 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4784   1.1     skrll 	  if (m == NULL)
   4785   1.1     skrll 	    goto error_return;
   4786   1.1     skrll 	  m->next = NULL;
   4787   1.1     skrll 	  m->p_type = PT_INTERP;
   4788   1.1     skrll 	  m->count = 1;
   4789   1.1     skrll 	  m->sections[0] = s;
   4790   1.1     skrll 
   4791   1.1     skrll 	  *pm = m;
   4792   1.1     skrll 	  pm = &m->next;
   4793   1.1     skrll 	}
   4794   1.1     skrll 
   4795   1.1     skrll       /* Look through the sections.  We put sections in the same program
   4796   1.1     skrll 	 segment when the start of the second section can be placed within
   4797   1.1     skrll 	 a few bytes of the end of the first section.  */
   4798  1.15  christos       last_hdr = NULL;
   4799   1.1     skrll       last_size = 0;
   4800   1.9  christos       hdr_index = 0;
   4801   1.1     skrll       writable = FALSE;
   4802  1.15  christos       executable = FALSE;
   4803  1.15  christos 
   4804  1.15  christos       if ((abfd->flags & D_PAGED) == 0)
   4805   1.1     skrll 	phdr_in_segment = FALSE;
   4806   1.1     skrll 
   4807   1.1     skrll       /* Deal with -Ttext or something similar such that the first section
   4808   1.1     skrll 	 is not adjacent to the program headers.  This is an
   4809  1.15  christos 	 approximation, since at this point we don't know exactly how many
   4810   1.1     skrll 	 program headers we will need.  */
   4811  1.15  christos       if (phdr_in_segment && count > 0)
   4812  1.15  christos 	{
   4813   1.1     skrll 	  bfd_vma phdr_lma;
   4814  1.15  christos 	  bfd_boolean separate_phdr = FALSE;
   4815  1.15  christos 
   4816  1.15  christos 	  phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
   4817  1.15  christos 	  if (info != NULL
   4818  1.15  christos 	      && info->separate_code
   4819  1.15  christos 	      && (sections[0]->flags & SEC_CODE) != 0)
   4820  1.15  christos 	    {
   4821  1.15  christos 	      /* If data sections should be separate from code and
   4822  1.15  christos 		 thus not executable, and the first section is
   4823  1.15  christos 		 executable then put the file and program headers in
   4824  1.15  christos 		 their own PT_LOAD.  */
   4825  1.15  christos 	      separate_phdr = TRUE;
   4826  1.15  christos 	      if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
   4827  1.15  christos 		   == (sections[0]->lma & addr_mask & -maxpagesize)))
   4828  1.15  christos 		{
   4829  1.15  christos 		  /* The file and program headers are currently on the
   4830  1.15  christos 		     same page as the first section.  Put them on the
   4831  1.15  christos 		     previous page if we can.  */
   4832  1.15  christos 		  if (phdr_lma >= maxpagesize)
   4833  1.15  christos 		    phdr_lma -= maxpagesize;
   4834  1.15  christos 		  else
   4835  1.15  christos 		    separate_phdr = FALSE;
   4836  1.15  christos 		}
   4837  1.15  christos 	    }
   4838  1.15  christos 	  if ((sections[0]->lma & addr_mask) < phdr_lma
   4839  1.15  christos 	      || (sections[0]->lma & addr_mask) < phdr_size)
   4840  1.15  christos 	    /* If file and program headers would be placed at the end
   4841  1.15  christos 	       of memory then it's probably better to omit them.  */
   4842  1.15  christos 	    phdr_in_segment = FALSE;
   4843  1.15  christos 	  else if (phdr_lma < wrap_to)
   4844  1.15  christos 	    /* If a section wraps around to where we'll be placing
   4845  1.15  christos 	       file and program headers, then the headers will be
   4846  1.15  christos 	       overwritten.  */
   4847  1.15  christos 	    phdr_in_segment = FALSE;
   4848  1.15  christos 	  else if (separate_phdr)
   4849  1.15  christos 	    {
   4850  1.15  christos 	      m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
   4851  1.15  christos 	      if (m == NULL)
   4852  1.15  christos 		goto error_return;
   4853  1.15  christos 	      m->p_paddr = phdr_lma;
   4854  1.15  christos 	      m->p_vaddr_offset
   4855  1.15  christos 		= (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
   4856  1.15  christos 	      m->p_paddr_valid = 1;
   4857  1.15  christos 	      *pm = m;
   4858   1.9  christos 	      pm = &m->next;
   4859   1.1     skrll 	      phdr_in_segment = FALSE;
   4860   1.1     skrll 	    }
   4861   1.1     skrll 	}
   4862   1.1     skrll 
   4863   1.1     skrll       for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
   4864   1.1     skrll 	{
   4865   1.1     skrll 	  asection *hdr;
   4866   1.1     skrll 	  bfd_boolean new_segment;
   4867   1.1     skrll 
   4868   1.1     skrll 	  hdr = *hdrpp;
   4869   1.1     skrll 
   4870   1.1     skrll 	  /* See if this section and the last one will fit in the same
   4871   1.1     skrll 	     segment.  */
   4872   1.1     skrll 
   4873   1.1     skrll 	  if (last_hdr == NULL)
   4874   1.1     skrll 	    {
   4875   1.1     skrll 	      /* If we don't have a segment yet, then we don't need a new
   4876   1.1     skrll 		 one (we build the last one after this loop).  */
   4877   1.1     skrll 	      new_segment = FALSE;
   4878   1.1     skrll 	    }
   4879   1.1     skrll 	  else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
   4880   1.1     skrll 	    {
   4881   1.1     skrll 	      /* If this section has a different relation between the
   4882   1.1     skrll 		 virtual address and the load address, then we need a new
   4883   1.1     skrll 		 segment.  */
   4884   1.3  christos 	      new_segment = TRUE;
   4885   1.3  christos 	    }
   4886   1.3  christos 	  else if (hdr->lma < last_hdr->lma + last_size
   4887   1.3  christos 		   || last_hdr->lma + last_size < last_hdr->lma)
   4888   1.3  christos 	    {
   4889   1.3  christos 	      /* If this section has a load address that makes it overlap
   4890   1.3  christos 		 the previous section, then we need a new segment.  */
   4891  1.13  christos 	      new_segment = TRUE;
   4892  1.13  christos 	    }
   4893  1.13  christos 	  else if ((abfd->flags & D_PAGED) != 0
   4894  1.13  christos 		   && (((last_hdr->lma + last_size - 1) & -maxpagesize)
   4895  1.13  christos 		       == (hdr->lma & -maxpagesize)))
   4896  1.13  christos 	    {
   4897  1.13  christos 	      /* If we are demand paged then we can't map two disk
   4898  1.13  christos 		 pages onto the same memory page.  */
   4899   1.1     skrll 	      new_segment = FALSE;
   4900   1.1     skrll 	    }
   4901   1.1     skrll 	  /* In the next test we have to be careful when last_hdr->lma is close
   4902   1.1     skrll 	     to the end of the address space.  If the aligned address wraps
   4903   1.1     skrll 	     around to the start of the address space, then there are no more
   4904  1.13  christos 	     pages left in memory and it is OK to assume that the current
   4905  1.13  christos 	     section can be included in the current segment.  */
   4906  1.13  christos 	  else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
   4907  1.13  christos 		    + maxpagesize > last_hdr->lma)
   4908   1.1     skrll 		   && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
   4909   1.1     skrll 		       + maxpagesize <= hdr->lma))
   4910   1.1     skrll 	    {
   4911   1.1     skrll 	      /* If putting this section in this segment would force us to
   4912   1.1     skrll 		 skip a page in the segment, then we need a new segment.  */
   4913   1.1     skrll 	      new_segment = TRUE;
   4914  1.13  christos 	    }
   4915   1.6  christos 	  else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
   4916   1.6  christos 		   && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
   4917   1.6  christos 	    {
   4918   1.6  christos 	      /* We don't want to put a loaded section after a
   4919  1.13  christos 		 nonloaded (ie. bss style) section in the same segment
   4920   1.1     skrll 		 as that will force the non-loaded section to be loaded.
   4921   1.1     skrll 		 Consider .tbss sections as loaded for this purpose.  */
   4922   1.1     skrll 	      new_segment = TRUE;
   4923   1.1     skrll 	    }
   4924   1.1     skrll 	  else if ((abfd->flags & D_PAGED) == 0)
   4925   1.1     skrll 	    {
   4926   1.1     skrll 	      /* If the file is not demand paged, which means that we
   4927   1.1     skrll 		 don't require the sections to be correctly aligned in the
   4928   1.1     skrll 		 file, then there is no other reason for a new segment.  */
   4929   1.9  christos 	      new_segment = FALSE;
   4930   1.9  christos 	    }
   4931   1.9  christos 	  else if (info != NULL
   4932   1.9  christos 		   && info->separate_code
   4933   1.9  christos 		   && executable != ((hdr->flags & SEC_CODE) != 0))
   4934   1.9  christos 	    {
   4935   1.1     skrll 	      new_segment = TRUE;
   4936  1.13  christos 	    }
   4937   1.1     skrll 	  else if (! writable
   4938   1.1     skrll 		   && (hdr->flags & SEC_READONLY) == 0)
   4939  1.13  christos 	    {
   4940   1.1     skrll 	      /* We don't want to put a writable section in a read only
   4941   1.1     skrll 		 segment.  */
   4942   1.1     skrll 	      new_segment = TRUE;
   4943   1.1     skrll 	    }
   4944   1.1     skrll 	  else
   4945   1.1     skrll 	    {
   4946   1.1     skrll 	      /* Otherwise, we can use the same segment.  */
   4947   1.1     skrll 	      new_segment = FALSE;
   4948   1.1     skrll 	    }
   4949   1.1     skrll 
   4950   1.1     skrll 	  /* Allow interested parties a chance to override our decision.  */
   4951   1.1     skrll 	  if (last_hdr != NULL
   4952   1.1     skrll 	      && info != NULL
   4953   1.1     skrll 	      && info->callbacks->override_segment_assignment != NULL)
   4954   1.1     skrll 	    new_segment
   4955   1.1     skrll 	      = info->callbacks->override_segment_assignment (info, abfd, hdr,
   4956   1.1     skrll 							      last_hdr,
   4957   1.1     skrll 							      new_segment);
   4958   1.1     skrll 
   4959   1.1     skrll 	  if (! new_segment)
   4960   1.1     skrll 	    {
   4961   1.9  christos 	      if ((hdr->flags & SEC_READONLY) == 0)
   4962   1.9  christos 		writable = TRUE;
   4963   1.1     skrll 	      if ((hdr->flags & SEC_CODE) != 0)
   4964   1.1     skrll 		executable = TRUE;
   4965  1.13  christos 	      last_hdr = hdr;
   4966   1.1     skrll 	      /* .tbss sections effectively have zero size.  */
   4967   1.1     skrll 	      last_size = !IS_TBSS (hdr) ? hdr->size : 0;
   4968   1.1     skrll 	      continue;
   4969   1.1     skrll 	    }
   4970  1.15  christos 
   4971   1.1     skrll 	  /* We need a new program segment.  We must create a new program
   4972  1.15  christos 	     header holding all the sections from hdr_index until hdr.  */
   4973   1.1     skrll 
   4974   1.1     skrll 	  m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
   4975   1.1     skrll 	  if (m == NULL)
   4976   1.1     skrll 	    goto error_return;
   4977   1.1     skrll 
   4978   1.1     skrll 	  *pm = m;
   4979   1.1     skrll 	  pm = &m->next;
   4980   1.1     skrll 
   4981   1.1     skrll 	  if ((hdr->flags & SEC_READONLY) == 0)
   4982   1.1     skrll 	    writable = TRUE;
   4983   1.1     skrll 	  else
   4984   1.9  christos 	    writable = FALSE;
   4985   1.9  christos 
   4986   1.9  christos 	  if ((hdr->flags & SEC_CODE) == 0)
   4987   1.9  christos 	    executable = FALSE;
   4988   1.9  christos 	  else
   4989   1.1     skrll 	    executable = TRUE;
   4990   1.1     skrll 
   4991  1.13  christos 	  last_hdr = hdr;
   4992  1.15  christos 	  /* .tbss sections effectively have zero size.  */
   4993   1.1     skrll 	  last_size = !IS_TBSS (hdr) ? hdr->size : 0;
   4994   1.1     skrll 	  hdr_index = i;
   4995   1.1     skrll 	  phdr_in_segment = FALSE;
   4996   1.3  christos 	}
   4997   1.3  christos 
   4998   1.3  christos       /* Create a final PT_LOAD program segment, but not if it's just
   4999  1.15  christos 	 for .tbss.  */
   5000  1.13  christos       if (last_hdr != NULL
   5001   1.1     skrll 	  && (i - hdr_index != 1
   5002  1.15  christos 	      || !IS_TBSS (last_hdr)))
   5003   1.1     skrll 	{
   5004   1.1     skrll 	  m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
   5005   1.1     skrll 	  if (m == NULL)
   5006   1.1     skrll 	    goto error_return;
   5007   1.1     skrll 
   5008   1.1     skrll 	  *pm = m;
   5009   1.1     skrll 	  pm = &m->next;
   5010   1.1     skrll 	}
   5011   1.1     skrll 
   5012   1.1     skrll       /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
   5013   1.1     skrll       if (dynsec != NULL)
   5014   1.1     skrll 	{
   5015   1.1     skrll 	  m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
   5016   1.1     skrll 	  if (m == NULL)
   5017   1.1     skrll 	    goto error_return;
   5018   1.1     skrll 	  *pm = m;
   5019   1.1     skrll 	  pm = &m->next;
   5020  1.15  christos 	}
   5021   1.1     skrll 
   5022   1.1     skrll       /* For each batch of consecutive loadable SHT_NOTE  sections,
   5023   1.1     skrll 	 add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
   5024  1.15  christos 	 because if we link together nonloadable .note sections and
   5025   1.1     skrll 	 loadable .note sections, we will generate two .note sections
   5026   1.1     skrll 	 in the output file.  */
   5027   1.1     skrll       for (s = abfd->sections; s != NULL; s = s->next)
   5028  1.15  christos 	{
   5029   1.1     skrll 	  if ((s->flags & SEC_LOAD) != 0
   5030   1.1     skrll 	      && elf_section_type (s) == SHT_NOTE)
   5031  1.15  christos 	    {
   5032   1.3  christos 	      asection *s2;
   5033   1.3  christos 	      unsigned int alignment_power = s->alignment_power;
   5034  1.15  christos 
   5035  1.15  christos 	      count = 1;
   5036  1.15  christos 	      for (s2 = s; s2->next != NULL; s2 = s2->next)
   5037  1.15  christos 		{
   5038  1.15  christos 		  if (s2->next->alignment_power == alignment_power
   5039  1.15  christos 		      && (s2->next->flags & SEC_LOAD) != 0
   5040  1.15  christos 		      && elf_section_type (s2->next) == SHT_NOTE
   5041  1.15  christos 		      && align_power (s2->lma + s2->size,
   5042  1.15  christos 				      alignment_power)
   5043  1.15  christos 		      == s2->next->lma)
   5044  1.15  christos 		    count++;
   5045  1.15  christos 		  else
   5046  1.15  christos 		    break;
   5047  1.15  christos 		}
   5048   1.3  christos 	      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   5049   1.1     skrll 	      amt += count * sizeof (asection *);
   5050   1.1     skrll 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5051   1.1     skrll 	      if (m == NULL)
   5052   1.1     skrll 		goto error_return;
   5053   1.1     skrll 	      m->next = NULL;
   5054   1.1     skrll 	      m->p_type = PT_NOTE;
   5055   1.1     skrll 	      m->count = count;
   5056   1.1     skrll 	      while (count > 1)
   5057   1.1     skrll 		{
   5058   1.1     skrll 		  m->sections[m->count - count--] = s;
   5059   1.1     skrll 		  BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   5060   1.1     skrll 		  s = s->next;
   5061   1.1     skrll 		}
   5062   1.1     skrll 	      m->sections[m->count - 1] = s;
   5063   1.1     skrll 	      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   5064   1.1     skrll 	      *pm = m;
   5065   1.1     skrll 	      pm = &m->next;
   5066   1.1     skrll 	    }
   5067   1.1     skrll 	  if (s->flags & SEC_THREAD_LOCAL)
   5068   1.1     skrll 	    {
   5069   1.1     skrll 	      if (! tls_count)
   5070   1.1     skrll 		first_tls = s;
   5071   1.9  christos 	      tls_count++;
   5072   1.9  christos 	    }
   5073   1.9  christos 	  if (first_mbind == NULL
   5074   1.1     skrll 	      && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
   5075   1.1     skrll 	    first_mbind = s;
   5076   1.1     skrll 	}
   5077   1.1     skrll 
   5078   1.1     skrll       /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
   5079  1.15  christos       if (tls_count > 0)
   5080  1.15  christos 	{
   5081   1.3  christos 	  amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   5082   1.1     skrll 	  amt += tls_count * sizeof (asection *);
   5083   1.1     skrll 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5084   1.1     skrll 	  if (m == NULL)
   5085   1.1     skrll 	    goto error_return;
   5086   1.1     skrll 	  m->next = NULL;
   5087   1.1     skrll 	  m->p_type = PT_TLS;
   5088   1.1     skrll 	  m->count = tls_count;
   5089   1.1     skrll 	  /* Mandated PF_R.  */
   5090   1.6  christos 	  m->p_flags = PF_R;
   5091   1.3  christos 	  m->p_flags_valid = 1;
   5092   1.1     skrll 	  s = first_tls;
   5093   1.6  christos 	  for (i = 0; i < (unsigned int) tls_count; ++i)
   5094   1.6  christos 	    {
   5095   1.6  christos 	      if ((s->flags & SEC_THREAD_LOCAL) == 0)
   5096  1.13  christos 		{
   5097   1.6  christos 		  _bfd_error_handler
   5098   1.6  christos 		    (_("%pB: TLS sections are not adjacent:"), abfd);
   5099   1.6  christos 		  s = first_tls;
   5100   1.6  christos 		  i = 0;
   5101   1.6  christos 		  while (i < (unsigned int) tls_count)
   5102   1.6  christos 		    {
   5103  1.13  christos 		      if ((s->flags & SEC_THREAD_LOCAL) != 0)
   5104   1.6  christos 			{
   5105   1.6  christos 			  _bfd_error_handler (_("	    TLS: %pA"), s);
   5106   1.6  christos 			  i++;
   5107  1.13  christos 			}
   5108   1.6  christos 		      else
   5109   1.6  christos 			_bfd_error_handler (_("	non-TLS: %pA"), s);
   5110   1.6  christos 		      s = s->next;
   5111   1.6  christos 		    }
   5112   1.6  christos 		  bfd_set_error (bfd_error_bad_value);
   5113   1.6  christos 		  goto error_return;
   5114   1.6  christos 		}
   5115   1.1     skrll 	      m->sections[i] = s;
   5116   1.1     skrll 	      s = s->next;
   5117   1.1     skrll 	    }
   5118   1.1     skrll 
   5119   1.1     skrll 	  *pm = m;
   5120   1.1     skrll 	  pm = &m->next;
   5121  1.15  christos 	}
   5122  1.15  christos 
   5123  1.15  christos       if (first_mbind
   5124   1.9  christos 	  && (abfd->flags & D_PAGED) != 0
   5125   1.9  christos 	  && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
   5126  1.15  christos 	for (s = first_mbind; s != NULL; s = s->next)
   5127   1.9  christos 	  if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
   5128   1.9  christos 	      && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
   5129   1.9  christos 	    {
   5130   1.9  christos 	      /* Mandated PF_R.  */
   5131   1.9  christos 	      unsigned long p_flags = PF_R;
   5132   1.9  christos 	      if ((s->flags & SEC_READONLY) == 0)
   5133   1.9  christos 		p_flags |= PF_W;
   5134   1.9  christos 	      if ((s->flags & SEC_CODE) != 0)
   5135   1.9  christos 		p_flags |= PF_X;
   5136   1.9  christos 
   5137   1.9  christos 	      amt = sizeof (struct elf_segment_map) + sizeof (asection *);
   5138   1.9  christos 	      m = bfd_zalloc (abfd, amt);
   5139   1.9  christos 	      if (m == NULL)
   5140   1.9  christos 		goto error_return;
   5141   1.9  christos 	      m->next = NULL;
   5142   1.9  christos 	      m->p_type = (PT_GNU_MBIND_LO
   5143   1.9  christos 			   + elf_section_data (s)->this_hdr.sh_info);
   5144   1.9  christos 	      m->count = 1;
   5145   1.9  christos 	      m->p_flags_valid = 1;
   5146   1.9  christos 	      m->sections[0] = s;
   5147   1.9  christos 	      m->p_flags = p_flags;
   5148   1.9  christos 
   5149   1.9  christos 	      *pm = m;
   5150   1.9  christos 	      pm = &m->next;
   5151  1.15  christos 	    }
   5152  1.15  christos 
   5153  1.15  christos       s = bfd_get_section_by_name (abfd,
   5154  1.15  christos 				   NOTE_GNU_PROPERTY_SECTION_NAME);
   5155  1.15  christos       if (s != NULL && s->size != 0)
   5156  1.15  christos 	{
   5157  1.15  christos 	  amt = sizeof (struct elf_segment_map) + sizeof (asection *);
   5158  1.15  christos 	  m = bfd_zalloc (abfd, amt);
   5159  1.15  christos 	  if (m == NULL)
   5160  1.15  christos 	    goto error_return;
   5161  1.15  christos 	  m->next = NULL;
   5162  1.15  christos 	  m->p_type = PT_GNU_PROPERTY;
   5163  1.15  christos 	  m->count = 1;
   5164  1.15  christos 	  m->p_flags_valid = 1;
   5165  1.15  christos 	  m->sections[0] = s;
   5166  1.15  christos 	  m->p_flags = PF_R;
   5167  1.15  christos 	  *pm = m;
   5168  1.15  christos 	  pm = &m->next;
   5169   1.1     skrll 	}
   5170   1.1     skrll 
   5171   1.6  christos       /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
   5172   1.1     skrll 	 segment.  */
   5173   1.1     skrll       eh_frame_hdr = elf_eh_frame_hdr (abfd);
   5174   1.1     skrll       if (eh_frame_hdr != NULL
   5175   1.1     skrll 	  && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
   5176   1.3  christos 	{
   5177   1.1     skrll 	  amt = sizeof (struct elf_segment_map);
   5178   1.1     skrll 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5179   1.1     skrll 	  if (m == NULL)
   5180   1.1     skrll 	    goto error_return;
   5181   1.1     skrll 	  m->next = NULL;
   5182   1.1     skrll 	  m->p_type = PT_GNU_EH_FRAME;
   5183   1.1     skrll 	  m->count = 1;
   5184   1.1     skrll 	  m->sections[0] = eh_frame_hdr->output_section;
   5185   1.1     skrll 
   5186   1.1     skrll 	  *pm = m;
   5187   1.1     skrll 	  pm = &m->next;
   5188   1.6  christos 	}
   5189   1.1     skrll 
   5190   1.1     skrll       if (elf_stack_flags (abfd))
   5191   1.3  christos 	{
   5192   1.1     skrll 	  amt = sizeof (struct elf_segment_map);
   5193   1.1     skrll 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5194   1.1     skrll 	  if (m == NULL)
   5195   1.1     skrll 	    goto error_return;
   5196   1.6  christos 	  m->next = NULL;
   5197   1.6  christos 	  m->p_type = PT_GNU_STACK;
   5198   1.1     skrll 	  m->p_flags = elf_stack_flags (abfd);
   5199   1.6  christos 	  m->p_align = bed->stack_align;
   5200   1.6  christos 	  m->p_flags_valid = 1;
   5201   1.6  christos 	  m->p_align_valid = m->p_align != 0;
   5202   1.6  christos 	  if (info->stacksize > 0)
   5203   1.6  christos 	    {
   5204   1.6  christos 	      m->p_size = info->stacksize;
   5205   1.1     skrll 	      m->p_size_valid = 1;
   5206   1.1     skrll 	    }
   5207   1.1     skrll 
   5208   1.1     skrll 	  *pm = m;
   5209   1.1     skrll 	  pm = &m->next;
   5210   1.1     skrll 	}
   5211   1.1     skrll 
   5212   1.1     skrll       if (info != NULL && info->relro)
   5213   1.1     skrll 	{
   5214   1.5  christos 	  for (m = mfirst; m != NULL; m = m->next)
   5215   1.5  christos 	    {
   5216   1.5  christos 	      if (m->p_type == PT_LOAD
   5217   1.5  christos 		  && m->count != 0
   5218   1.5  christos 		  && m->sections[0]->vma >= info->relro_start
   5219   1.5  christos 		  && m->sections[0]->vma < info->relro_end)
   5220   1.5  christos 		{
   5221   1.5  christos 		  i = m->count;
   5222   1.5  christos 		  while (--i != (unsigned) -1)
   5223   1.5  christos 		    if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
   5224   1.5  christos 			== (SEC_LOAD | SEC_HAS_CONTENTS))
   5225   1.6  christos 		      break;
   5226   1.1     skrll 
   5227   1.1     skrll 		  if (i != (unsigned) -1)
   5228   1.5  christos 		    break;
   5229   1.1     skrll 		}
   5230   1.1     skrll 	    }
   5231   1.1     skrll 
   5232   1.1     skrll 	  /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
   5233   1.1     skrll 	  if (m != NULL)
   5234   1.3  christos 	    {
   5235   1.1     skrll 	      amt = sizeof (struct elf_segment_map);
   5236   1.1     skrll 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5237   1.1     skrll 	      if (m == NULL)
   5238   1.1     skrll 		goto error_return;
   5239   1.1     skrll 	      m->next = NULL;
   5240   1.1     skrll 	      m->p_type = PT_GNU_RELRO;
   5241   1.1     skrll 	      *pm = m;
   5242   1.1     skrll 	      pm = &m->next;
   5243   1.1     skrll 	    }
   5244   1.1     skrll 	}
   5245   1.6  christos 
   5246   1.1     skrll       free (sections);
   5247   1.1     skrll       elf_seg_map (abfd) = mfirst;
   5248   1.1     skrll     }
   5249   1.1     skrll 
   5250   1.1     skrll   if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
   5251   1.6  christos     return FALSE;
   5252   1.1     skrll 
   5253   1.6  christos   for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
   5254   1.1     skrll     ++count;
   5255   1.1     skrll   elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
   5256   1.1     skrll 
   5257   1.1     skrll   return TRUE;
   5258   1.1     skrll 
   5259   1.1     skrll  error_return:
   5260   1.1     skrll   if (sections != NULL)
   5261   1.1     skrll     free (sections);
   5262   1.1     skrll   return FALSE;
   5263   1.1     skrll }
   5264   1.1     skrll 
   5265   1.1     skrll /* Sort sections by address.  */
   5266   1.1     skrll 
   5267   1.1     skrll static int
   5268   1.1     skrll elf_sort_sections (const void *arg1, const void *arg2)
   5269   1.1     skrll {
   5270   1.1     skrll   const asection *sec1 = *(const asection **) arg1;
   5271   1.1     skrll   const asection *sec2 = *(const asection **) arg2;
   5272   1.1     skrll   bfd_size_type size1, size2;
   5273   1.1     skrll 
   5274   1.1     skrll   /* Sort by LMA first, since this is the address used to
   5275   1.1     skrll      place the section into a segment.  */
   5276   1.1     skrll   if (sec1->lma < sec2->lma)
   5277   1.1     skrll     return -1;
   5278   1.1     skrll   else if (sec1->lma > sec2->lma)
   5279   1.1     skrll     return 1;
   5280   1.1     skrll 
   5281   1.1     skrll   /* Then sort by VMA.  Normally the LMA and the VMA will be
   5282   1.1     skrll      the same, and this will do nothing.  */
   5283   1.1     skrll   if (sec1->vma < sec2->vma)
   5284   1.1     skrll     return -1;
   5285   1.1     skrll   else if (sec1->vma > sec2->vma)
   5286   1.1     skrll     return 1;
   5287   1.1     skrll 
   5288   1.1     skrll   /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
   5289   1.1     skrll 
   5290   1.1     skrll #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
   5291   1.1     skrll 
   5292  1.15  christos   if (TOEND (sec1))
   5293   1.1     skrll     {
   5294   1.1     skrll       if (!TOEND (sec2))
   5295   1.1     skrll 	return 1;
   5296   1.1     skrll     }
   5297   1.1     skrll   else if (TOEND (sec2))
   5298   1.1     skrll     return -1;
   5299   1.1     skrll 
   5300   1.1     skrll #undef TOEND
   5301   1.1     skrll 
   5302   1.1     skrll   /* Sort by size, to put zero sized sections
   5303   1.1     skrll      before others at the same address.  */
   5304   1.1     skrll 
   5305   1.1     skrll   size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
   5306   1.1     skrll   size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
   5307   1.1     skrll 
   5308   1.1     skrll   if (size1 < size2)
   5309   1.1     skrll     return -1;
   5310   1.1     skrll   if (size1 > size2)
   5311   1.1     skrll     return 1;
   5312   1.1     skrll 
   5313   1.1     skrll   return sec1->target_index - sec2->target_index;
   5314  1.15  christos }
   5315  1.15  christos 
   5316  1.15  christos /* This qsort comparison functions sorts PT_LOAD segments first and
   5317  1.15  christos    by p_paddr, for assign_file_positions_for_load_sections.  */
   5318  1.15  christos 
   5319  1.15  christos static int
   5320  1.15  christos elf_sort_segments (const void *arg1, const void *arg2)
   5321  1.15  christos {
   5322  1.15  christos   const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
   5323  1.15  christos   const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
   5324  1.15  christos 
   5325  1.15  christos   if (m1->p_type != m2->p_type)
   5326  1.15  christos     {
   5327  1.15  christos       if (m1->p_type == PT_NULL)
   5328  1.15  christos 	return 1;
   5329  1.15  christos       if (m2->p_type == PT_NULL)
   5330  1.15  christos 	return -1;
   5331  1.15  christos       return m1->p_type < m2->p_type ? -1 : 1;
   5332  1.15  christos     }
   5333  1.15  christos   if (m1->includes_filehdr != m2->includes_filehdr)
   5334  1.15  christos     return m1->includes_filehdr ? -1 : 1;
   5335  1.15  christos   if (m1->no_sort_lma != m2->no_sort_lma)
   5336  1.15  christos     return m1->no_sort_lma ? -1 : 1;
   5337  1.15  christos   if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
   5338  1.15  christos     {
   5339  1.15  christos       bfd_vma lma1, lma2;
   5340  1.15  christos       lma1 = 0;
   5341  1.15  christos       if (m1->p_paddr_valid)
   5342  1.15  christos 	lma1 = m1->p_paddr;
   5343  1.15  christos       else if (m1->count != 0)
   5344  1.15  christos 	lma1 = m1->sections[0]->lma + m1->p_vaddr_offset;
   5345  1.15  christos       lma2 = 0;
   5346  1.15  christos       if (m2->p_paddr_valid)
   5347  1.15  christos 	lma2 = m2->p_paddr;
   5348  1.15  christos       else if (m2->count != 0)
   5349  1.15  christos 	lma2 = m2->sections[0]->lma + m2->p_vaddr_offset;
   5350  1.15  christos       if (lma1 != lma2)
   5351  1.15  christos 	return lma1 < lma2 ? -1 : 1;
   5352  1.15  christos     }
   5353  1.15  christos   if (m1->idx != m2->idx)
   5354  1.15  christos     return m1->idx < m2->idx ? -1 : 1;
   5355  1.15  christos   return 0;
   5356   1.1     skrll }
   5357   1.1     skrll 
   5358   1.1     skrll /* Ian Lance Taylor writes:
   5359   1.1     skrll 
   5360   1.1     skrll    We shouldn't be using % with a negative signed number.  That's just
   5361   1.1     skrll    not good.  We have to make sure either that the number is not
   5362   1.1     skrll    negative, or that the number has an unsigned type.  When the types
   5363   1.1     skrll    are all the same size they wind up as unsigned.  When file_ptr is a
   5364   1.1     skrll    larger signed type, the arithmetic winds up as signed long long,
   5365   1.1     skrll    which is wrong.
   5366   1.1     skrll 
   5367   1.1     skrll    What we're trying to say here is something like ``increase OFF by
   5368   1.1     skrll    the least amount that will cause it to be equal to the VMA modulo
   5369   1.1     skrll    the page size.''  */
   5370   1.1     skrll /* In other words, something like:
   5371   1.1     skrll 
   5372   1.1     skrll    vma_offset = m->sections[0]->vma % bed->maxpagesize;
   5373   1.1     skrll    off_offset = off % bed->maxpagesize;
   5374   1.1     skrll    if (vma_offset < off_offset)
   5375   1.1     skrll      adjustment = vma_offset + bed->maxpagesize - off_offset;
   5376   1.1     skrll    else
   5377   1.9  christos      adjustment = vma_offset - off_offset;
   5378   1.1     skrll 
   5379   1.1     skrll    which can be collapsed into the expression below.  */
   5380   1.1     skrll 
   5381   1.1     skrll static file_ptr
   5382   1.6  christos vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
   5383   1.6  christos {
   5384   1.6  christos   /* PR binutils/16199: Handle an alignment of zero.  */
   5385   1.1     skrll   if (maxpagesize == 0)
   5386   1.1     skrll     maxpagesize = 1;
   5387   1.1     skrll   return ((vma - off) % maxpagesize);
   5388   1.1     skrll }
   5389   1.1     skrll 
   5390   1.1     skrll static void
   5391   1.1     skrll print_segment_map (const struct elf_segment_map *m)
   5392   1.1     skrll {
   5393   1.1     skrll   unsigned int j;
   5394   1.1     skrll   const char *pt = get_segment_type (m->p_type);
   5395   1.1     skrll   char buf[32];
   5396   1.1     skrll 
   5397   1.1     skrll   if (pt == NULL)
   5398   1.1     skrll     {
   5399   1.1     skrll       if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
   5400   1.1     skrll 	sprintf (buf, "LOPROC+%7.7x",
   5401   1.1     skrll 		 (unsigned int) (m->p_type - PT_LOPROC));
   5402   1.1     skrll       else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
   5403   1.1     skrll 	sprintf (buf, "LOOS+%7.7x",
   5404   1.1     skrll 		 (unsigned int) (m->p_type - PT_LOOS));
   5405   1.1     skrll       else
   5406   1.1     skrll 	snprintf (buf, sizeof (buf), "%8.8x",
   5407   1.1     skrll 		  (unsigned int) m->p_type);
   5408   1.3  christos       pt = buf;
   5409   1.1     skrll     }
   5410   1.1     skrll   fflush (stdout);
   5411   1.1     skrll   fprintf (stderr, "%s:", pt);
   5412   1.1     skrll   for (j = 0; j < m->count; j++)
   5413   1.3  christos     fprintf (stderr, " %s", m->sections [j]->name);
   5414   1.3  christos   putc ('\n',stderr);
   5415   1.3  christos   fflush (stderr);
   5416   1.3  christos }
   5417   1.3  christos 
   5418   1.3  christos static bfd_boolean
   5419   1.3  christos write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
   5420   1.3  christos {
   5421   1.3  christos   void *buf;
   5422   1.3  christos   bfd_boolean ret;
   5423   1.3  christos 
   5424   1.3  christos   if (bfd_seek (abfd, pos, SEEK_SET) != 0)
   5425   1.3  christos     return FALSE;
   5426   1.3  christos   buf = bfd_zmalloc (len);
   5427   1.3  christos   if (buf == NULL)
   5428   1.3  christos     return FALSE;
   5429   1.3  christos   ret = bfd_bwrite (buf, len, abfd) == len;
   5430   1.1     skrll   free (buf);
   5431   1.1     skrll   return ret;
   5432   1.1     skrll }
   5433   1.1     skrll 
   5434   1.1     skrll /* Assign file positions to the sections based on the mapping from
   5435   1.1     skrll    sections to segments.  This function also sets up some fields in
   5436   1.1     skrll    the file header.  */
   5437   1.1     skrll 
   5438   1.1     skrll static bfd_boolean
   5439   1.1     skrll assign_file_positions_for_load_sections (bfd *abfd,
   5440   1.1     skrll 					 struct bfd_link_info *link_info)
   5441   1.1     skrll {
   5442  1.15  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   5443   1.1     skrll   struct elf_segment_map *m;
   5444   1.1     skrll   struct elf_segment_map *phdr_load_seg;
   5445   1.1     skrll   Elf_Internal_Phdr *phdrs;
   5446   1.1     skrll   Elf_Internal_Phdr *p;
   5447  1.15  christos   file_ptr off;
   5448   1.1     skrll   bfd_size_type maxpagesize;
   5449  1.15  christos   unsigned int alloc, actual;
   5450   1.1     skrll   unsigned int i, j;
   5451   1.1     skrll   struct elf_segment_map **sorted_seg_map;
   5452   1.1     skrll 
   5453   1.1     skrll   if (link_info == NULL
   5454   1.1     skrll       && !_bfd_elf_map_sections_to_segments (abfd, link_info))
   5455   1.1     skrll     return FALSE;
   5456   1.6  christos 
   5457  1.15  christos   alloc = 0;
   5458   1.1     skrll   for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   5459   1.5  christos     m->idx = alloc++;
   5460   1.5  christos 
   5461   1.5  christos   if (alloc)
   5462   1.5  christos     {
   5463   1.5  christos       elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
   5464   1.5  christos       elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
   5465   1.5  christos     }
   5466   1.5  christos   else
   5467   1.5  christos     {
   5468   1.5  christos       /* PR binutils/12467.  */
   5469   1.5  christos       elf_elfheader (abfd)->e_phoff = 0;
   5470   1.5  christos       elf_elfheader (abfd)->e_phentsize = 0;
   5471   1.1     skrll     }
   5472   1.1     skrll 
   5473   1.6  christos   elf_elfheader (abfd)->e_phnum = alloc;
   5474  1.15  christos 
   5475  1.15  christos   if (elf_program_header_size (abfd) == (bfd_size_type) -1)
   5476  1.15  christos     {
   5477  1.15  christos       actual = alloc;
   5478   1.1     skrll       elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
   5479  1.15  christos     }
   5480  1.15  christos   else
   5481  1.15  christos     {
   5482  1.15  christos       actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
   5483  1.15  christos       BFD_ASSERT (elf_program_header_size (abfd)
   5484  1.15  christos 		  == actual * bed->s->sizeof_phdr);
   5485   1.1     skrll       BFD_ASSERT (actual >= alloc);
   5486   1.1     skrll     }
   5487   1.1     skrll 
   5488   1.6  christos   if (alloc == 0)
   5489   1.1     skrll     {
   5490   1.1     skrll       elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
   5491   1.1     skrll       return TRUE;
   5492   1.6  christos     }
   5493   1.2     skrll 
   5494   1.2     skrll   /* We're writing the size in elf_program_header_size (abfd),
   5495   1.6  christos      see assign_file_positions_except_relocs, so make sure we have
   5496   1.6  christos      that amount allocated, with trailing space cleared.
   5497   1.2     skrll      The variable alloc contains the computed need, while
   5498   1.2     skrll      elf_program_header_size (abfd) contains the size used for the
   5499   1.2     skrll      layout.
   5500   1.2     skrll      See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
   5501  1.15  christos      where the layout is forced to according to a larger size in the
   5502  1.15  christos      last iterations for the testcase ld-elf/header.  */
   5503  1.15  christos   phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
   5504   1.1     skrll 			     + alloc * sizeof (*sorted_seg_map)));
   5505   1.1     skrll   sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
   5506   1.1     skrll   elf_tdata (abfd)->phdr = phdrs;
   5507   1.1     skrll   if (phdrs == NULL)
   5508  1.15  christos     return FALSE;
   5509  1.15  christos 
   5510  1.15  christos   for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
   5511  1.15  christos     {
   5512  1.15  christos       sorted_seg_map[j] = m;
   5513  1.15  christos       /* If elf_segment_map is not from map_sections_to_segments, the
   5514  1.15  christos 	 sections may not be correctly ordered.  NOTE: sorting should
   5515  1.15  christos 	 not be done to the PT_NOTE section of a corefile, which may
   5516  1.15  christos 	 contain several pseudo-sections artificially created by bfd.
   5517  1.15  christos 	 Sorting these pseudo-sections breaks things badly.  */
   5518  1.15  christos       if (m->count > 1
   5519  1.15  christos 	  && !(elf_elfheader (abfd)->e_type == ET_CORE
   5520  1.15  christos 	       && m->p_type == PT_NOTE))
   5521  1.15  christos 	{
   5522  1.15  christos 	  for (i = 0; i < m->count; i++)
   5523  1.15  christos 	    m->sections[i]->target_index = i;
   5524  1.15  christos 	  qsort (m->sections, (size_t) m->count, sizeof (asection *),
   5525  1.15  christos 		 elf_sort_sections);
   5526  1.15  christos 	}
   5527  1.15  christos     }
   5528  1.15  christos   if (alloc > 1)
   5529  1.15  christos     qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
   5530   1.1     skrll 	   elf_sort_segments);
   5531   1.1     skrll 
   5532   1.1     skrll   maxpagesize = 1;
   5533   1.1     skrll   if ((abfd->flags & D_PAGED) != 0)
   5534  1.15  christos     maxpagesize = bed->maxpagesize;
   5535   1.1     skrll 
   5536  1.15  christos   /* Sections must map to file offsets past the ELF file header.  */
   5537  1.15  christos   off = bed->s->sizeof_ehdr;
   5538  1.15  christos   /* And if one of the PT_LOAD headers doesn't include the program
   5539  1.15  christos      headers then we'll be mapping program headers in the usual
   5540  1.15  christos      position after the ELF file header.  */
   5541  1.15  christos   phdr_load_seg = NULL;
   5542  1.15  christos   for (j = 0; j < alloc; j++)
   5543  1.15  christos     {
   5544  1.15  christos       m = sorted_seg_map[j];
   5545  1.15  christos       if (m->p_type != PT_LOAD)
   5546  1.15  christos 	break;
   5547  1.15  christos       if (m->includes_phdrs)
   5548  1.15  christos 	{
   5549  1.15  christos 	  phdr_load_seg = m;
   5550  1.15  christos 	  break;
   5551  1.15  christos 	}
   5552  1.15  christos     }
   5553   1.1     skrll   if (phdr_load_seg == NULL)
   5554  1.15  christos     off += actual * bed->s->sizeof_phdr;
   5555   1.1     skrll 
   5556   1.1     skrll   for (j = 0; j < alloc; j++)
   5557   1.1     skrll     {
   5558   1.1     skrll       asection **secpp;
   5559   1.1     skrll       bfd_vma off_adjust;
   5560   1.1     skrll       bfd_boolean no_contents;
   5561   1.1     skrll 
   5562   1.1     skrll       /* An ELF segment (described by Elf_Internal_Phdr) may contain a
   5563   1.1     skrll 	 number of sections with contents contributing to both p_filesz
   5564   1.1     skrll 	 and p_memsz, followed by a number of sections with no contents
   5565  1.15  christos 	 that just contribute to p_memsz.  In this loop, OFF tracks next
   5566  1.15  christos 	 available file offset for PT_LOAD and PT_NOTE segments.  */
   5567   1.1     skrll       m = sorted_seg_map[j];
   5568   1.1     skrll       p = phdrs + m->idx;
   5569   1.1     skrll       p->p_type = m->p_type;
   5570   1.1     skrll       p->p_flags = m->p_flags;
   5571  1.15  christos 
   5572   1.1     skrll       if (m->count == 0)
   5573  1.15  christos 	p->p_vaddr = m->p_vaddr_offset;
   5574   1.1     skrll       else
   5575   1.1     skrll 	p->p_vaddr = m->sections[0]->vma + m->p_vaddr_offset;
   5576   1.1     skrll 
   5577   1.1     skrll       if (m->p_paddr_valid)
   5578   1.1     skrll 	p->p_paddr = m->p_paddr;
   5579   1.1     skrll       else if (m->count == 0)
   5580  1.15  christos 	p->p_paddr = 0;
   5581   1.1     skrll       else
   5582   1.1     skrll 	p->p_paddr = m->sections[0]->lma + m->p_vaddr_offset;
   5583   1.1     skrll 
   5584   1.1     skrll       if (p->p_type == PT_LOAD
   5585   1.1     skrll 	  && (abfd->flags & D_PAGED) != 0)
   5586   1.1     skrll 	{
   5587   1.1     skrll 	  /* p_align in demand paged PT_LOAD segments effectively stores
   5588   1.1     skrll 	     the maximum page size.  When copying an executable with
   5589   1.1     skrll 	     objcopy, we set m->p_align from the input file.  Use this
   5590   1.1     skrll 	     value for maxpagesize rather than bed->maxpagesize, which
   5591   1.1     skrll 	     may be different.  Note that we use maxpagesize for PT_TLS
   5592   1.1     skrll 	     segment alignment later in this function, so we are relying
   5593   1.1     skrll 	     on at least one PT_LOAD segment appearing before a PT_TLS
   5594   1.1     skrll 	     segment.  */
   5595   1.1     skrll 	  if (m->p_align_valid)
   5596   1.1     skrll 	    maxpagesize = m->p_align;
   5597   1.1     skrll 
   5598   1.1     skrll 	  p->p_align = maxpagesize;
   5599   1.1     skrll 	}
   5600   1.1     skrll       else if (m->p_align_valid)
   5601   1.1     skrll 	p->p_align = m->p_align;
   5602  1.15  christos       else if (m->count == 0)
   5603  1.15  christos 	p->p_align = 1 << bed->s->log_file_align;
   5604  1.15  christos 
   5605  1.15  christos       if (m == phdr_load_seg)
   5606  1.15  christos 	{
   5607  1.15  christos 	  if (!m->includes_filehdr)
   5608  1.15  christos 	    p->p_offset = off;
   5609   1.1     skrll 	  off += actual * bed->s->sizeof_phdr;
   5610   1.1     skrll 	}
   5611   1.1     skrll 
   5612   1.1     skrll       no_contents = FALSE;
   5613   1.1     skrll       off_adjust = 0;
   5614   1.1     skrll       if (p->p_type == PT_LOAD
   5615   1.1     skrll 	  && m->count > 0)
   5616   1.1     skrll 	{
   5617   1.1     skrll 	  bfd_size_type align;
   5618   1.1     skrll 	  unsigned int align_power = 0;
   5619   1.1     skrll 
   5620   1.1     skrll 	  if (m->p_align_valid)
   5621   1.1     skrll 	    align = p->p_align;
   5622   1.1     skrll 	  else
   5623   1.1     skrll 	    {
   5624   1.1     skrll 	      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   5625   1.1     skrll 		{
   5626  1.15  christos 		  unsigned int secalign;
   5627   1.1     skrll 
   5628   1.1     skrll 		  secalign = bfd_section_alignment (*secpp);
   5629   1.1     skrll 		  if (secalign > align_power)
   5630   1.1     skrll 		    align_power = secalign;
   5631   1.1     skrll 		}
   5632   1.1     skrll 	      align = (bfd_size_type) 1 << align_power;
   5633   1.1     skrll 	      if (align < maxpagesize)
   5634   1.1     skrll 		align = maxpagesize;
   5635   1.1     skrll 	    }
   5636   1.1     skrll 
   5637   1.1     skrll 	  for (i = 0; i < m->count; i++)
   5638   1.1     skrll 	    if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   5639   1.1     skrll 	      /* If we aren't making room for this section, then
   5640   1.1     skrll 		 it must be SHT_NOBITS regardless of what we've
   5641   1.1     skrll 		 set via struct bfd_elf_special_section.  */
   5642   1.1     skrll 	      elf_section_type (m->sections[i]) = SHT_NOBITS;
   5643   1.1     skrll 
   5644   1.1     skrll 	  /* Find out whether this segment contains any loadable
   5645   1.1     skrll 	     sections.  */
   5646   1.1     skrll 	  no_contents = TRUE;
   5647   1.1     skrll 	  for (i = 0; i < m->count; i++)
   5648   1.1     skrll 	    if (elf_section_type (m->sections[i]) != SHT_NOBITS)
   5649   1.1     skrll 	      {
   5650   1.1     skrll 		no_contents = FALSE;
   5651   1.1     skrll 		break;
   5652   1.3  christos 	      }
   5653   1.9  christos 
   5654   1.9  christos 	  off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
   5655   1.9  christos 
   5656   1.9  christos 	  /* Broken hardware and/or kernel require that files do not
   5657  1.15  christos 	     map the same page with different permissions on some hppa
   5658  1.15  christos 	     processors.  */
   5659   1.9  christos 	  if (j != 0
   5660   1.9  christos 	      && (abfd->flags & D_PAGED) != 0
   5661   1.9  christos 	      && bed->no_page_alias
   5662   1.9  christos 	      && (off & (maxpagesize - 1)) != 0
   5663   1.1     skrll 	      && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize))
   5664   1.1     skrll 	    off_adjust += maxpagesize;
   5665   1.1     skrll 	  off += off_adjust;
   5666   1.1     skrll 	  if (no_contents)
   5667   1.1     skrll 	    {
   5668   1.1     skrll 	      /* We shouldn't need to align the segment on disk since
   5669   1.1     skrll 		 the segment doesn't need file space, but the gABI
   5670   1.1     skrll 		 arguably requires the alignment and glibc ld.so
   5671   1.1     skrll 		 checks it.  So to comply with the alignment
   5672   1.1     skrll 		 requirement but not waste file space, we adjust
   5673   1.1     skrll 		 p_offset for just this segment.  (OFF_ADJUST is
   5674   1.1     skrll 		 subtracted from OFF later.)  This may put p_offset
   5675   1.1     skrll 		 past the end of file, but that shouldn't matter.  */
   5676   1.1     skrll 	    }
   5677   1.1     skrll 	  else
   5678   1.1     skrll 	    off_adjust = 0;
   5679   1.1     skrll 	}
   5680   1.1     skrll       /* Make sure the .dynamic section is the first section in the
   5681   1.1     skrll 	 PT_DYNAMIC segment.  */
   5682   1.1     skrll       else if (p->p_type == PT_DYNAMIC
   5683   1.1     skrll 	       && m->count > 1
   5684   1.1     skrll 	       && strcmp (m->sections[0]->name, ".dynamic") != 0)
   5685  1.13  christos 	{
   5686   1.9  christos 	  _bfd_error_handler
   5687   1.1     skrll 	    (_("%pB: The first section in the PT_DYNAMIC segment"
   5688   1.1     skrll 	       " is not the .dynamic section"),
   5689   1.1     skrll 	     abfd);
   5690   1.1     skrll 	  bfd_set_error (bfd_error_bad_value);
   5691   1.1     skrll 	  return FALSE;
   5692   1.1     skrll 	}
   5693   1.1     skrll       /* Set the note section type to SHT_NOTE.  */
   5694   1.1     skrll       else if (p->p_type == PT_NOTE)
   5695   1.1     skrll 	for (i = 0; i < m->count; i++)
   5696   1.1     skrll 	  elf_section_type (m->sections[i]) = SHT_NOTE;
   5697   1.1     skrll 
   5698   1.1     skrll       if (m->includes_filehdr)
   5699   1.1     skrll 	{
   5700   1.1     skrll 	  if (!m->p_flags_valid)
   5701   1.1     skrll 	    p->p_flags |= PF_R;
   5702  1.15  christos 	  p->p_filesz = bed->s->sizeof_ehdr;
   5703   1.1     skrll 	  p->p_memsz = bed->s->sizeof_ehdr;
   5704  1.15  christos 	  if (p->p_type == PT_LOAD)
   5705   1.1     skrll 	    {
   5706  1.15  christos 	      if (m->count > 0)
   5707  1.15  christos 		{
   5708  1.15  christos 		  if (p->p_vaddr < (bfd_vma) off
   5709  1.15  christos 		      || (!m->p_paddr_valid
   5710  1.15  christos 			  && p->p_paddr < (bfd_vma) off))
   5711  1.15  christos 		    {
   5712  1.15  christos 		      _bfd_error_handler
   5713  1.15  christos 			(_("%pB: not enough room for program headers,"
   5714  1.15  christos 			   " try linking with -N"),
   5715  1.15  christos 			 abfd);
   5716  1.15  christos 		      bfd_set_error (bfd_error_bad_value);
   5717  1.15  christos 		      return FALSE;
   5718  1.15  christos 		    }
   5719  1.15  christos 		  p->p_vaddr -= off;
   5720   1.1     skrll 		  if (!m->p_paddr_valid)
   5721  1.15  christos 		    p->p_paddr -= off;
   5722  1.15  christos 		}
   5723  1.15  christos 	    }
   5724  1.15  christos 	  else if (sorted_seg_map[0]->includes_filehdr)
   5725  1.15  christos 	    {
   5726   1.1     skrll 	      Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
   5727  1.15  christos 	      p->p_vaddr = filehdr->p_vaddr;
   5728   1.1     skrll 	      if (!m->p_paddr_valid)
   5729   1.1     skrll 		p->p_paddr = filehdr->p_paddr;
   5730   1.1     skrll 	    }
   5731   1.1     skrll 	}
   5732   1.1     skrll 
   5733   1.1     skrll       if (m->includes_phdrs)
   5734   1.1     skrll 	{
   5735  1.15  christos 	  if (!m->p_flags_valid)
   5736  1.15  christos 	    p->p_flags |= PF_R;
   5737   1.1     skrll 	  p->p_filesz += actual * bed->s->sizeof_phdr;
   5738   1.1     skrll 	  p->p_memsz += actual * bed->s->sizeof_phdr;
   5739  1.15  christos 	  if (!m->includes_filehdr)
   5740  1.15  christos 	    {
   5741  1.15  christos 	      if (p->p_type == PT_LOAD)
   5742  1.15  christos 		{
   5743  1.15  christos 		  elf_elfheader (abfd)->e_phoff = p->p_offset;
   5744  1.15  christos 		  if (m->count > 0)
   5745  1.15  christos 		    {
   5746  1.15  christos 		      p->p_vaddr -= off - p->p_offset;
   5747  1.15  christos 		      if (!m->p_paddr_valid)
   5748  1.15  christos 			p->p_paddr -= off - p->p_offset;
   5749  1.15  christos 		    }
   5750   1.1     skrll 		}
   5751  1.15  christos 	      else if (phdr_load_seg != NULL)
   5752  1.15  christos 		{
   5753  1.15  christos 		  Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
   5754  1.15  christos 		  bfd_vma phdr_off = 0;
   5755  1.15  christos 		  if (phdr_load_seg->includes_filehdr)
   5756   1.1     skrll 		    phdr_off = bed->s->sizeof_ehdr;
   5757  1.15  christos 		  p->p_vaddr = phdr->p_vaddr + phdr_off;
   5758  1.15  christos 		  if (!m->p_paddr_valid)
   5759   1.1     skrll 		    p->p_paddr = phdr->p_paddr + phdr_off;
   5760  1.15  christos 		  p->p_offset = phdr->p_offset + phdr_off;
   5761  1.15  christos 		}
   5762   1.2     skrll 	      else
   5763   1.1     skrll 		p->p_offset = bed->s->sizeof_ehdr;
   5764   1.1     skrll 	    }
   5765   1.1     skrll 	}
   5766   1.1     skrll 
   5767   1.1     skrll       if (p->p_type == PT_LOAD
   5768   1.1     skrll 	  || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
   5769  1.15  christos 	{
   5770  1.15  christos 	  if (!m->includes_filehdr && !m->includes_phdrs)
   5771  1.15  christos 	    {
   5772  1.15  christos 	      p->p_offset = off;
   5773  1.15  christos 	      if (no_contents)
   5774  1.15  christos 		{
   5775  1.15  christos 		  /* Put meaningless p_offset for PT_LOAD segments
   5776  1.15  christos 		     without file contents somewhere within the first
   5777  1.15  christos 		     page, in an attempt to not point past EOF.  */
   5778  1.15  christos 		  bfd_size_type align = maxpagesize;
   5779  1.15  christos 		  if (align < p->p_align)
   5780  1.15  christos 		    align = p->p_align;
   5781  1.15  christos 		  if (align < 1)
   5782  1.15  christos 		    align = 1;
   5783  1.15  christos 		  p->p_offset = off % align;
   5784   1.1     skrll 		}
   5785   1.1     skrll 	    }
   5786   1.1     skrll 	  else
   5787   1.1     skrll 	    {
   5788   1.1     skrll 	      file_ptr adjust;
   5789   1.1     skrll 
   5790   1.1     skrll 	      adjust = off - (p->p_offset + p->p_filesz);
   5791   1.1     skrll 	      if (!no_contents)
   5792   1.1     skrll 		p->p_filesz += adjust;
   5793   1.1     skrll 	      p->p_memsz += adjust;
   5794   1.1     skrll 	    }
   5795   1.1     skrll 	}
   5796   1.1     skrll 
   5797   1.1     skrll       /* Set up p_filesz, p_memsz, p_align and p_flags from the section
   5798   1.1     skrll 	 maps.  Set filepos for sections in PT_LOAD segments, and in
   5799   1.1     skrll 	 core files, for sections in PT_NOTE segments.
   5800   1.1     skrll 	 assign_file_positions_for_non_load_sections will set filepos
   5801   1.1     skrll 	 for other sections and update p_filesz for other segments.  */
   5802   1.1     skrll       for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   5803   1.1     skrll 	{
   5804   1.1     skrll 	  asection *sec;
   5805   1.1     skrll 	  bfd_size_type align;
   5806   1.1     skrll 	  Elf_Internal_Shdr *this_hdr;
   5807   1.1     skrll 
   5808  1.15  christos 	  sec = *secpp;
   5809   1.1     skrll 	  this_hdr = &elf_section_data (sec)->this_hdr;
   5810   1.1     skrll 	  align = (bfd_size_type) 1 << bfd_section_alignment (sec);
   5811   1.1     skrll 
   5812   1.1     skrll 	  if ((p->p_type == PT_LOAD
   5813   1.1     skrll 	       || p->p_type == PT_TLS)
   5814   1.1     skrll 	      && (this_hdr->sh_type != SHT_NOBITS
   5815   1.1     skrll 		  || ((this_hdr->sh_flags & SHF_ALLOC) != 0
   5816   1.1     skrll 		      && ((this_hdr->sh_flags & SHF_TLS) == 0
   5817   1.3  christos 			  || p->p_type == PT_TLS))))
   5818   1.3  christos 	    {
   5819   1.3  christos 	      bfd_vma p_start = p->p_paddr;
   5820   1.3  christos 	      bfd_vma p_end = p_start + p->p_memsz;
   5821   1.3  christos 	      bfd_vma s_start = sec->lma;
   5822   1.3  christos 	      bfd_vma adjust = s_start - p_end;
   5823   1.3  christos 
   5824   1.3  christos 	      if (adjust != 0
   5825   1.1     skrll 		  && (s_start < p_end
   5826   1.9  christos 		      || p_end < p_start))
   5827   1.9  christos 		{
   5828  1.13  christos 		  _bfd_error_handler
   5829  1.13  christos 		    /* xgettext:c-format */
   5830   1.1     skrll 		    (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
   5831   1.3  christos 		     abfd, sec, (uint64_t) s_start, (uint64_t) p_end);
   5832   1.1     skrll 		  adjust = 0;
   5833   1.1     skrll 		  sec->lma = p_end;
   5834   1.1     skrll 		}
   5835   1.1     skrll 	      p->p_memsz += adjust;
   5836   1.1     skrll 
   5837  1.15  christos 	      if (this_hdr->sh_type != SHT_NOBITS)
   5838   1.3  christos 		{
   5839  1.15  christos 		  if (p->p_type == PT_LOAD)
   5840  1.15  christos 		    {
   5841  1.15  christos 		      if (p->p_filesz + adjust < p->p_memsz)
   5842  1.15  christos 			{
   5843  1.15  christos 			  /* We have a PROGBITS section following NOBITS ones.
   5844  1.15  christos 			     Allocate file space for the NOBITS section(s) and
   5845  1.15  christos 			     zero it.  */
   5846  1.15  christos 			  adjust = p->p_memsz - p->p_filesz;
   5847  1.15  christos 			  if (!write_zeros (abfd, off, adjust))
   5848  1.15  christos 			    return FALSE;
   5849   1.3  christos 			}
   5850   1.1     skrll 		      off += adjust;
   5851   1.1     skrll 		    }
   5852   1.1     skrll 		  p->p_filesz += adjust;
   5853   1.1     skrll 		}
   5854   1.1     skrll 	    }
   5855   1.1     skrll 
   5856   1.1     skrll 	  if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
   5857   1.1     skrll 	    {
   5858   1.1     skrll 	      /* The section at i == 0 is the one that actually contains
   5859   1.1     skrll 		 everything.  */
   5860   1.1     skrll 	      if (i == 0)
   5861   1.1     skrll 		{
   5862   1.1     skrll 		  this_hdr->sh_offset = sec->filepos = off;
   5863   1.1     skrll 		  off += this_hdr->sh_size;
   5864   1.1     skrll 		  p->p_filesz = this_hdr->sh_size;
   5865   1.1     skrll 		  p->p_memsz = 0;
   5866   1.1     skrll 		  p->p_align = 1;
   5867   1.1     skrll 		}
   5868   1.1     skrll 	      else
   5869   1.1     skrll 		{
   5870   1.1     skrll 		  /* The rest are fake sections that shouldn't be written.  */
   5871   1.1     skrll 		  sec->filepos = 0;
   5872   1.1     skrll 		  sec->size = 0;
   5873   1.1     skrll 		  sec->flags = 0;
   5874   1.1     skrll 		  continue;
   5875   1.1     skrll 		}
   5876   1.1     skrll 	    }
   5877   1.1     skrll 	  else
   5878   1.1     skrll 	    {
   5879   1.1     skrll 	      if (p->p_type == PT_LOAD)
   5880   1.1     skrll 		{
   5881   1.1     skrll 		  this_hdr->sh_offset = sec->filepos = off;
   5882   1.1     skrll 		  if (this_hdr->sh_type != SHT_NOBITS)
   5883   1.3  christos 		    off += this_hdr->sh_size;
   5884   1.3  christos 		}
   5885   1.3  christos 	      else if (this_hdr->sh_type == SHT_NOBITS
   5886   1.3  christos 		       && (this_hdr->sh_flags & SHF_TLS) != 0
   5887   1.3  christos 		       && this_hdr->sh_offset == 0)
   5888   1.3  christos 		{
   5889   1.3  christos 		  /* This is a .tbss section that didn't get a PT_LOAD.
   5890   1.3  christos 		     (See _bfd_elf_map_sections_to_segments "Create a
   5891   1.3  christos 		     final PT_LOAD".)  Set sh_offset to the value it
   5892   1.3  christos 		     would have if we had created a zero p_filesz and
   5893   1.3  christos 		     p_memsz PT_LOAD header for the section.  This
   5894   1.3  christos 		     also makes the PT_TLS header have the same
   5895   1.3  christos 		     p_offset value.  */
   5896   1.3  christos 		  bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
   5897   1.3  christos 							  off, align);
   5898   1.1     skrll 		  this_hdr->sh_offset = sec->filepos = off + adjust;
   5899   1.1     skrll 		}
   5900   1.1     skrll 
   5901   1.1     skrll 	      if (this_hdr->sh_type != SHT_NOBITS)
   5902   1.1     skrll 		{
   5903   1.1     skrll 		  p->p_filesz += this_hdr->sh_size;
   5904   1.1     skrll 		  /* A load section without SHF_ALLOC is something like
   5905   1.1     skrll 		     a note section in a PT_NOTE segment.  These take
   5906   1.1     skrll 		     file space but are not loaded into memory.  */
   5907   1.1     skrll 		  if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   5908   1.1     skrll 		    p->p_memsz += this_hdr->sh_size;
   5909   1.1     skrll 		}
   5910   1.1     skrll 	      else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   5911   1.1     skrll 		{
   5912   1.1     skrll 		  if (p->p_type == PT_TLS)
   5913   1.1     skrll 		    p->p_memsz += this_hdr->sh_size;
   5914   1.1     skrll 
   5915   1.1     skrll 		  /* .tbss is special.  It doesn't contribute to p_memsz of
   5916   1.1     skrll 		     normal segments.  */
   5917   1.1     skrll 		  else if ((this_hdr->sh_flags & SHF_TLS) == 0)
   5918   1.1     skrll 		    p->p_memsz += this_hdr->sh_size;
   5919   1.1     skrll 		}
   5920   1.1     skrll 
   5921   1.1     skrll 	      if (align > p->p_align
   5922   1.1     skrll 		  && !m->p_align_valid
   5923   1.1     skrll 		  && (p->p_type != PT_LOAD
   5924   1.1     skrll 		      || (abfd->flags & D_PAGED) == 0))
   5925   1.1     skrll 		p->p_align = align;
   5926   1.1     skrll 	    }
   5927   1.1     skrll 
   5928   1.1     skrll 	  if (!m->p_flags_valid)
   5929   1.1     skrll 	    {
   5930   1.1     skrll 	      p->p_flags |= PF_R;
   5931   1.1     skrll 	      if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
   5932   1.1     skrll 		p->p_flags |= PF_X;
   5933   1.1     skrll 	      if ((this_hdr->sh_flags & SHF_WRITE) != 0)
   5934   1.1     skrll 		p->p_flags |= PF_W;
   5935   1.6  christos 	    }
   5936   1.1     skrll 	}
   5937   1.1     skrll 
   5938  1.15  christos       off -= off_adjust;
   5939  1.15  christos 
   5940  1.15  christos       /* PR ld/20815 - Check that the program header segment, if
   5941  1.15  christos 	 present, will be loaded into memory.  */
   5942  1.15  christos       if (p->p_type == PT_PHDR
   5943  1.15  christos 	  && phdr_load_seg == NULL
   5944  1.15  christos 	  && !(bed->elf_backend_allow_non_load_phdr != NULL
   5945  1.15  christos 	       && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
   5946  1.15  christos 	{
   5947  1.15  christos 	  /* The fix for this error is usually to edit the linker script being
   5948  1.15  christos 	     used and set up the program headers manually.  Either that or
   5949  1.15  christos 	     leave room for the headers at the start of the SECTIONS.  */
   5950  1.15  christos 	  _bfd_error_handler (_("%pB: error: PHDR segment not covered"
   5951  1.15  christos 				" by LOAD segment"),
   5952  1.15  christos 			      abfd);
   5953  1.15  christos 	  return FALSE;
   5954   1.1     skrll 	}
   5955   1.1     skrll 
   5956   1.1     skrll       /* Check that all sections are in a PT_LOAD segment.
   5957   1.3  christos 	 Don't check funky gdb generated core files.  */
   5958   1.3  christos       if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
   5959   1.1     skrll 	{
   5960   1.3  christos 	  bfd_boolean check_vma = TRUE;
   5961   1.3  christos 
   5962   1.3  christos 	  for (i = 1; i < m->count; i++)
   5963   1.3  christos 	    if (m->sections[i]->vma == m->sections[i - 1]->vma
   5964   1.3  christos 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
   5965   1.3  christos 				       ->this_hdr), p) != 0
   5966   1.1     skrll 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
   5967   1.3  christos 				       ->this_hdr), p) != 0)
   5968   1.3  christos 	      {
   5969   1.3  christos 		/* Looks like we have overlays packed into the segment.  */
   5970   1.1     skrll 		check_vma = FALSE;
   5971   1.3  christos 		break;
   5972   1.3  christos 	      }
   5973   1.3  christos 
   5974   1.3  christos 	  for (i = 0; i < m->count; i++)
   5975   1.3  christos 	    {
   5976   1.3  christos 	      Elf_Internal_Shdr *this_hdr;
   5977   1.3  christos 	      asection *sec;
   5978   1.3  christos 
   5979   1.3  christos 	      sec = m->sections[i];
   5980   1.3  christos 	      this_hdr = &(elf_section_data(sec)->this_hdr);
   5981   1.3  christos 	      if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
   5982   1.9  christos 		  && !ELF_TBSS_SPECIAL (this_hdr, p))
   5983   1.9  christos 		{
   5984  1.13  christos 		  _bfd_error_handler
   5985   1.3  christos 		    /* xgettext:c-format */
   5986   1.3  christos 		    (_("%pB: section `%pA' can't be allocated in segment %d"),
   5987   1.3  christos 		     abfd, sec, j);
   5988   1.3  christos 		  print_segment_map (m);
   5989   1.3  christos 		}
   5990   1.1     skrll 	    }
   5991   1.1     skrll 	}
   5992   1.6  christos     }
   5993  1.15  christos 
   5994  1.15  christos   elf_next_file_pos (abfd) = off;
   5995  1.15  christos 
   5996  1.15  christos   if (link_info != NULL
   5997  1.15  christos       && phdr_load_seg != NULL
   5998  1.15  christos       && phdr_load_seg->includes_filehdr)
   5999  1.15  christos     {
   6000  1.15  christos       /* There is a segment that contains both the file headers and the
   6001  1.15  christos 	 program headers, so provide a symbol __ehdr_start pointing there.
   6002  1.15  christos 	 A program can use this to examine itself robustly.  */
   6003  1.15  christos 
   6004  1.15  christos       struct elf_link_hash_entry *hash
   6005  1.15  christos 	= elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
   6006  1.15  christos 				FALSE, FALSE, TRUE);
   6007  1.15  christos       /* If the symbol was referenced and not defined, define it.  */
   6008  1.15  christos       if (hash != NULL
   6009  1.15  christos 	  && (hash->root.type == bfd_link_hash_new
   6010  1.15  christos 	      || hash->root.type == bfd_link_hash_undefined
   6011  1.15  christos 	      || hash->root.type == bfd_link_hash_undefweak
   6012  1.15  christos 	      || hash->root.type == bfd_link_hash_common))
   6013  1.15  christos 	{
   6014  1.15  christos 	  asection *s = NULL;
   6015  1.15  christos 	  bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr;
   6016  1.15  christos 
   6017  1.15  christos 	  if (phdr_load_seg->count != 0)
   6018  1.15  christos 	    /* The segment contains sections, so use the first one.  */
   6019  1.15  christos 	    s = phdr_load_seg->sections[0];
   6020  1.15  christos 	  else
   6021  1.15  christos 	    /* Use the first (i.e. lowest-addressed) section in any segment.  */
   6022  1.15  christos 	    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   6023  1.15  christos 	      if (m->p_type == PT_LOAD && m->count != 0)
   6024  1.15  christos 		{
   6025  1.15  christos 		  s = m->sections[0];
   6026  1.15  christos 		  break;
   6027  1.15  christos 		}
   6028  1.15  christos 
   6029  1.15  christos 	  if (s != NULL)
   6030  1.15  christos 	    {
   6031  1.15  christos 	      hash->root.u.def.value = filehdr_vaddr - s->vma;
   6032  1.15  christos 	      hash->root.u.def.section = s;
   6033  1.15  christos 	    }
   6034  1.15  christos 	  else
   6035  1.15  christos 	    {
   6036  1.15  christos 	      hash->root.u.def.value = filehdr_vaddr;
   6037  1.15  christos 	      hash->root.u.def.section = bfd_abs_section_ptr;
   6038  1.15  christos 	    }
   6039  1.15  christos 
   6040  1.15  christos 	  hash->root.type = bfd_link_hash_defined;
   6041  1.15  christos 	  hash->def_regular = 1;
   6042  1.15  christos 	  hash->non_elf = 0;
   6043  1.15  christos 	}
   6044  1.15  christos     }
   6045  1.15  christos 
   6046  1.15  christos   return TRUE;
   6047  1.15  christos }
   6048  1.15  christos 
   6049  1.15  christos /* Determine if a bfd is a debuginfo file.  Unfortunately there
   6050  1.15  christos    is no defined method for detecting such files, so we have to
   6051  1.15  christos    use heuristics instead.  */
   6052  1.15  christos 
   6053  1.15  christos bfd_boolean
   6054  1.15  christos is_debuginfo_file (bfd *abfd)
   6055  1.15  christos {
   6056  1.15  christos   if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   6057  1.15  christos     return FALSE;
   6058  1.15  christos 
   6059  1.15  christos   Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
   6060  1.15  christos   Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
   6061  1.15  christos   Elf_Internal_Shdr **headerp;
   6062  1.15  christos 
   6063  1.15  christos   for (headerp = start_headers; headerp < end_headers; headerp ++)
   6064  1.15  christos     {
   6065  1.15  christos       Elf_Internal_Shdr *header = * headerp;
   6066  1.15  christos 
   6067  1.15  christos       /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
   6068  1.15  christos 	 The only allocated sections are SHT_NOBITS or SHT_NOTES.  */
   6069  1.15  christos       if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
   6070  1.15  christos 	  && header->sh_type != SHT_NOBITS
   6071  1.15  christos 	  && header->sh_type != SHT_NOTE)
   6072  1.15  christos 	return FALSE;
   6073   1.1     skrll     }
   6074   1.1     skrll 
   6075   1.1     skrll   return TRUE;
   6076  1.15  christos }
   6077  1.15  christos 
   6078   1.1     skrll /* Assign file positions for the other sections, except for compressed debugging
   6079   1.1     skrll    and other sections assigned in _bfd_elf_assign_file_positions_for_non_load().  */
   6080   1.1     skrll 
   6081   1.1     skrll static bfd_boolean
   6082   1.1     skrll assign_file_positions_for_non_load_sections (bfd *abfd,
   6083   1.1     skrll 					     struct bfd_link_info *link_info)
   6084   1.1     skrll {
   6085   1.6  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6086   1.1     skrll   Elf_Internal_Shdr **i_shdrpp;
   6087   1.1     skrll   Elf_Internal_Shdr **hdrpp, **end_hdrpp;
   6088   1.1     skrll   Elf_Internal_Phdr *phdrs;
   6089   1.1     skrll   Elf_Internal_Phdr *p;
   6090   1.1     skrll   struct elf_segment_map *m;
   6091   1.1     skrll   file_ptr off;
   6092   1.6  christos 
   6093   1.6  christos   i_shdrpp = elf_elfsections (abfd);
   6094   1.6  christos   end_hdrpp = i_shdrpp + elf_numsections (abfd);
   6095   1.1     skrll   off = elf_next_file_pos (abfd);
   6096   1.1     skrll   for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
   6097   1.1     skrll     {
   6098   1.1     skrll       Elf_Internal_Shdr *hdr;
   6099   1.1     skrll 
   6100   1.1     skrll       hdr = *hdrpp;
   6101   1.1     skrll       if (hdr->bfd_section != NULL
   6102   1.1     skrll 	  && (hdr->bfd_section->filepos != 0
   6103   1.1     skrll 	      || (hdr->sh_type == SHT_NOBITS
   6104   1.1     skrll 		  && hdr->contents == NULL)))
   6105   1.1     skrll 	BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
   6106  1.15  christos       else if ((hdr->sh_flags & SHF_ALLOC) != 0)
   6107  1.15  christos 	{
   6108  1.15  christos 	  if (hdr->sh_size != 0
   6109  1.15  christos 	      /* PR 24717 - debuginfo files are known to be not strictly
   6110  1.15  christos 		 compliant with the ELF standard.  In particular they often
   6111  1.15  christos 		 have .note.gnu.property sections that are outside of any
   6112  1.15  christos 		 loadable segment.  This is not a problem for such files,
   6113   1.9  christos 		 so do not warn about them.  */
   6114   1.9  christos 	      && ! is_debuginfo_file (abfd))
   6115  1.13  christos 	    _bfd_error_handler
   6116   1.5  christos 	      /* xgettext:c-format */
   6117   1.5  christos 	      (_("%pB: warning: allocated section `%s' not in segment"),
   6118   1.5  christos 	       abfd,
   6119   1.5  christos 	       (hdr->bfd_section == NULL
   6120   1.1     skrll 		? "*unknown*"
   6121   1.1     skrll 		: hdr->bfd_section->name));
   6122   1.1     skrll 	  /* We don't need to page align empty sections.  */
   6123   1.1     skrll 	  if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
   6124   1.1     skrll 	    off += vma_page_aligned_bias (hdr->sh_addr, off,
   6125   1.1     skrll 					  bed->maxpagesize);
   6126   1.1     skrll 	  else
   6127   1.1     skrll 	    off += vma_page_aligned_bias (hdr->sh_addr, off,
   6128   1.1     skrll 					  hdr->sh_addralign);
   6129   1.1     skrll 	  off = _bfd_elf_assign_file_position_for_section (hdr, off,
   6130   1.1     skrll 							   FALSE);
   6131   1.1     skrll 	}
   6132  1.15  christos       else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   6133  1.15  christos 		&& hdr->bfd_section == NULL)
   6134   1.6  christos 	       /* We don't know the offset of these sections yet: their size has
   6135  1.15  christos 		  not been decided.  */
   6136  1.15  christos 	       || (hdr->bfd_section != NULL
   6137  1.15  christos 		   && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
   6138   1.6  christos 		       || (bfd_section_is_ctf (hdr->bfd_section)
   6139   1.6  christos 			   && abfd->is_linker_output)))
   6140   1.6  christos 	       || hdr == i_shdrpp[elf_onesymtab (abfd)]
   6141   1.6  christos 	       || (elf_symtab_shndx_list (abfd) != NULL
   6142   1.6  christos 		   && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   6143   1.1     skrll 	       || hdr == i_shdrpp[elf_strtab_sec (abfd)]
   6144   1.1     skrll 	       || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
   6145   1.1     skrll 	hdr->sh_offset = -1;
   6146   1.1     skrll       else
   6147  1.15  christos 	off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
   6148   1.1     skrll     }
   6149   1.1     skrll   elf_next_file_pos (abfd) = off;
   6150   1.1     skrll 
   6151   1.1     skrll   /* Now that we have set the section file positions, we can set up
   6152   1.6  christos      the file positions for the non PT_LOAD segments.  */
   6153   1.1     skrll   phdrs = elf_tdata (abfd)->phdr;
   6154   1.1     skrll   for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
   6155   1.1     skrll     {
   6156  1.13  christos       if (p->p_type == PT_GNU_RELRO)
   6157  1.13  christos 	{
   6158   1.1     skrll 	  bfd_vma start, end;
   6159   1.1     skrll 	  bfd_boolean ok;
   6160   1.1     skrll 
   6161   1.1     skrll 	  if (link_info != NULL)
   6162  1.13  christos 	    {
   6163  1.13  christos 	      /* During linking the range of the RELRO segment is passed
   6164  1.13  christos 		 in link_info.  Note that there may be padding between
   6165  1.13  christos 		 relro_start and the first RELRO section.  */
   6166  1.13  christos 	      start = link_info->relro_start;
   6167  1.13  christos 	      end = link_info->relro_end;
   6168  1.13  christos 	    }
   6169  1.13  christos 	  else if (m->count != 0)
   6170  1.13  christos 	    {
   6171  1.13  christos 	      if (!m->p_size_valid)
   6172  1.13  christos 		abort ();
   6173  1.13  christos 	      start = m->sections[0]->vma;
   6174  1.13  christos 	      end = start + m->p_size;
   6175  1.13  christos 	    }
   6176  1.13  christos 	  else
   6177  1.13  christos 	    {
   6178  1.13  christos 	      start = 0;
   6179  1.13  christos 	      end = 0;
   6180  1.13  christos 	    }
   6181  1.13  christos 
   6182  1.13  christos 	  ok = FALSE;
   6183  1.13  christos 	  if (start < end)
   6184  1.13  christos 	    {
   6185  1.13  christos 	      struct elf_segment_map *lm;
   6186  1.13  christos 	      const Elf_Internal_Phdr *lp;
   6187  1.13  christos 	      unsigned int i;
   6188  1.13  christos 
   6189   1.6  christos 	      /* Find a LOAD segment containing a section in the RELRO
   6190   1.5  christos 		 segment.  */
   6191   1.5  christos 	      for (lm = elf_seg_map (abfd), lp = phdrs;
   6192   1.1     skrll 		   lm != NULL;
   6193   1.1     skrll 		   lm = lm->next, lp++)
   6194   1.5  christos 		{
   6195  1.13  christos 		  if (lp->p_type == PT_LOAD
   6196  1.13  christos 		      && lm->count != 0
   6197  1.13  christos 		      && (lm->sections[lm->count - 1]->vma
   6198  1.13  christos 			  + (!IS_TBSS (lm->sections[lm->count - 1])
   6199  1.13  christos 			     ? lm->sections[lm->count - 1]->size
   6200   1.1     skrll 			     : 0)) > start
   6201   1.1     skrll 		      && lm->sections[0]->vma < end)
   6202   1.5  christos 		    break;
   6203  1.13  christos 		}
   6204   1.1     skrll 
   6205  1.13  christos 	      if (lm != NULL)
   6206  1.13  christos 		{
   6207  1.13  christos 		  /* Find the section starting the RELRO segment.  */
   6208  1.13  christos 		  for (i = 0; i < lm->count; i++)
   6209  1.13  christos 		    {
   6210  1.13  christos 		      asection *s = lm->sections[i];
   6211  1.13  christos 		      if (s->vma >= start
   6212  1.13  christos 			  && s->vma < end
   6213  1.13  christos 			  && s->size != 0)
   6214  1.13  christos 			break;
   6215  1.13  christos 		    }
   6216  1.13  christos 
   6217  1.13  christos 		  if (i < lm->count)
   6218  1.13  christos 		    {
   6219  1.13  christos 		      p->p_vaddr = lm->sections[i]->vma;
   6220  1.13  christos 		      p->p_paddr = lm->sections[i]->lma;
   6221  1.13  christos 		      p->p_offset = lm->sections[i]->filepos;
   6222  1.13  christos 		      p->p_memsz = end - p->p_vaddr;
   6223  1.13  christos 		      p->p_filesz = p->p_memsz;
   6224  1.13  christos 
   6225  1.13  christos 		      /* The RELRO segment typically ends a few bytes
   6226  1.13  christos 			 into .got.plt but other layouts are possible.
   6227  1.13  christos 			 In cases where the end does not match any
   6228  1.13  christos 			 loaded section (for instance is in file
   6229  1.13  christos 			 padding), trim p_filesz back to correspond to
   6230  1.13  christos 			 the end of loaded section contents.  */
   6231  1.13  christos 		      if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
   6232  1.13  christos 			p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
   6233  1.13  christos 
   6234  1.13  christos 		      /* Preserve the alignment and flags if they are
   6235  1.13  christos 			 valid.  The gold linker generates RW/4 for
   6236  1.13  christos 			 the PT_GNU_RELRO section.  It is better for
   6237  1.13  christos 			 objcopy/strip to honor these attributes
   6238  1.13  christos 			 otherwise gdb will choke when using separate
   6239  1.13  christos 			 debug files.  */
   6240  1.13  christos 		      if (!m->p_align_valid)
   6241  1.13  christos 			p->p_align = 1;
   6242  1.13  christos 		      if (!m->p_flags_valid)
   6243  1.13  christos 			p->p_flags = PF_R;
   6244   1.1     skrll 		      ok = TRUE;
   6245   1.1     skrll 		    }
   6246  1.13  christos 		}
   6247  1.13  christos 	    }
   6248  1.13  christos 	  if (link_info != NULL)
   6249  1.13  christos 	    BFD_ASSERT (ok);
   6250   1.1     skrll 	  if (!ok)
   6251   1.6  christos 	    memset (p, 0, sizeof *p);
   6252   1.6  christos 	}
   6253   1.6  christos       else if (p->p_type == PT_GNU_STACK)
   6254   1.6  christos 	{
   6255   1.6  christos 	  if (m->p_size_valid)
   6256   1.1     skrll 	    p->p_memsz = m->p_size;
   6257   1.1     skrll 	}
   6258   1.6  christos       else if (m->count != 0)
   6259   1.9  christos 	{
   6260   1.1     skrll 	  unsigned int i;
   6261   1.1     skrll 
   6262   1.1     skrll 	  if (p->p_type != PT_LOAD
   6263   1.1     skrll 	      && (p->p_type != PT_NOTE
   6264   1.9  christos 		  || bfd_get_format (abfd) != bfd_core))
   6265   1.9  christos 	    {
   6266   1.9  christos 	      /* A user specified segment layout may include a PHDR
   6267   1.9  christos 		 segment that overlaps with a LOAD segment...  */
   6268   1.9  christos 	      if (p->p_type == PT_PHDR)
   6269   1.9  christos 		{
   6270   1.9  christos 		  m->count = 0;
   6271   1.9  christos 		  continue;
   6272   1.6  christos 		}
   6273   1.6  christos 
   6274   1.6  christos 	      if (m->includes_filehdr || m->includes_phdrs)
   6275   1.9  christos 		{
   6276  1.13  christos 		  /* PR 17512: file: 2195325e.  */
   6277   1.9  christos 		  _bfd_error_handler
   6278   1.9  christos 		    (_("%pB: error: non-load segment %d includes file header "
   6279   1.6  christos 		       "and/or program header"),
   6280   1.6  christos 		     abfd, (int) (p - phdrs));
   6281   1.1     skrll 		  return FALSE;
   6282   1.3  christos 		}
   6283   1.1     skrll 
   6284   1.3  christos 	      p->p_filesz = 0;
   6285   1.3  christos 	      p->p_offset = m->sections[0]->filepos;
   6286   1.3  christos 	      for (i = m->count; i-- != 0;)
   6287   1.3  christos 		{
   6288   1.3  christos 		  asection *sect = m->sections[i];
   6289   1.3  christos 		  Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
   6290   1.3  christos 		  if (hdr->sh_type != SHT_NOBITS)
   6291   1.3  christos 		    {
   6292   1.3  christos 		      p->p_filesz = (sect->filepos - m->sections[0]->filepos
   6293   1.3  christos 				     + hdr->sh_size);
   6294   1.3  christos 		      break;
   6295   1.1     skrll 		    }
   6296   1.1     skrll 		}
   6297   1.1     skrll 	    }
   6298   1.1     skrll 	}
   6299   1.1     skrll     }
   6300   1.1     skrll 
   6301   1.1     skrll   return TRUE;
   6302   1.6  christos }
   6303   1.6  christos 
   6304   1.6  christos static elf_section_list *
   6305   1.6  christos find_section_in_list (unsigned int i, elf_section_list * list)
   6306   1.6  christos {
   6307   1.6  christos   for (;list != NULL; list = list->next)
   6308   1.6  christos     if (list->ndx == i)
   6309   1.6  christos       break;
   6310   1.6  christos   return list;
   6311   1.1     skrll }
   6312   1.1     skrll 
   6313   1.1     skrll /* Work out the file positions of all the sections.  This is called by
   6314   1.1     skrll    _bfd_elf_compute_section_file_positions.  All the section sizes and
   6315   1.1     skrll    VMAs must be known before this is called.
   6316  1.15  christos 
   6317  1.15  christos    Reloc sections come in two flavours: Those processed specially as
   6318  1.15  christos    "side-channel" data attached to a section to which they apply, and those that
   6319  1.15  christos    bfd doesn't process as relocations.  The latter sort are stored in a normal
   6320  1.15  christos    bfd section by bfd_section_from_shdr.  We don't consider the former sort
   6321  1.15  christos    here, unless they form part of the loadable image.  Reloc sections not
   6322   1.1     skrll    assigned here (and compressed debugging sections and CTF sections which
   6323   1.1     skrll    nothing else in the file can rely upon) will be handled later by
   6324   1.1     skrll    assign_file_positions_for_relocs.
   6325   1.1     skrll 
   6326   1.1     skrll    We also don't set the positions of the .symtab and .strtab here.  */
   6327   1.1     skrll 
   6328   1.1     skrll static bfd_boolean
   6329   1.1     skrll assign_file_positions_except_relocs (bfd *abfd,
   6330   1.1     skrll 				     struct bfd_link_info *link_info)
   6331   1.1     skrll {
   6332   1.1     skrll   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   6333  1.15  christos   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
   6334   1.1     skrll   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6335   1.1     skrll   unsigned int alloc;
   6336   1.1     skrll 
   6337   1.1     skrll   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
   6338   1.1     skrll       && bfd_get_format (abfd) != bfd_core)
   6339   1.1     skrll     {
   6340   1.1     skrll       Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
   6341   1.1     skrll       unsigned int num_sec = elf_numsections (abfd);
   6342   1.6  christos       Elf_Internal_Shdr **hdrpp;
   6343   1.1     skrll       unsigned int i;
   6344   1.1     skrll       file_ptr off;
   6345   1.1     skrll 
   6346   1.1     skrll       /* Start after the ELF header.  */
   6347   1.1     skrll       off = i_ehdrp->e_ehsize;
   6348   1.1     skrll 
   6349   1.1     skrll       /* We are not creating an executable, which means that we are
   6350   1.1     skrll 	 not creating a program header, and that the actual order of
   6351   1.1     skrll 	 the sections in the file is unimportant.  */
   6352   1.1     skrll       for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
   6353   1.1     skrll 	{
   6354   1.1     skrll 	  Elf_Internal_Shdr *hdr;
   6355   1.1     skrll 
   6356   1.1     skrll 	  hdr = *hdrpp;
   6357  1.15  christos 	  if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   6358  1.15  christos 	       && hdr->bfd_section == NULL)
   6359   1.6  christos 	      /* Do not assign offsets for these sections yet: we don't know
   6360  1.15  christos 		 their sizes.  */
   6361  1.15  christos 	      || (hdr->bfd_section != NULL
   6362  1.15  christos 		  && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
   6363   1.6  christos 		      || (bfd_section_is_ctf (hdr->bfd_section)
   6364   1.6  christos 			  && abfd->is_linker_output)))
   6365   1.6  christos 	      || i == elf_onesymtab (abfd)
   6366   1.6  christos 	      || (elf_symtab_shndx_list (abfd) != NULL
   6367   1.6  christos 		  && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   6368   1.1     skrll 	      || i == elf_strtab_sec (abfd)
   6369   1.1     skrll 	      || i == elf_shstrtab_sec (abfd))
   6370   1.1     skrll 	    {
   6371   1.1     skrll 	      hdr->sh_offset = -1;
   6372   1.1     skrll 	    }
   6373   1.1     skrll 	  else
   6374   1.6  christos 	    off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
   6375   1.6  christos 	}
   6376  1.15  christos 
   6377   1.1     skrll       elf_next_file_pos (abfd) = off;
   6378   1.1     skrll       elf_program_header_size (abfd) = 0;
   6379   1.1     skrll     }
   6380   1.1     skrll   else
   6381   1.1     skrll     {
   6382   1.1     skrll       /* Assign file positions for the loaded sections based on the
   6383   1.1     skrll 	 assignment of sections to segments.  */
   6384   1.1     skrll       if (!assign_file_positions_for_load_sections (abfd, link_info))
   6385   1.1     skrll 	return FALSE;
   6386   1.1     skrll 
   6387   1.1     skrll       /* And for non-load sections.  */
   6388  1.15  christos       if (!assign_file_positions_for_non_load_sections (abfd, link_info))
   6389   1.1     skrll 	return FALSE;
   6390  1.15  christos     }
   6391  1.15  christos 
   6392   1.1     skrll   if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
   6393  1.15  christos     return FALSE;
   6394  1.15  christos 
   6395  1.15  christos   /* Write out the program headers.  */
   6396  1.15  christos   alloc = i_ehdrp->e_phnum;
   6397  1.15  christos   if (alloc != 0)
   6398   1.1     skrll     {
   6399   1.1     skrll       if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
   6400   1.1     skrll 	  || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
   6401   1.1     skrll 	return FALSE;
   6402   1.1     skrll     }
   6403   1.1     skrll 
   6404   1.1     skrll   return TRUE;
   6405  1.15  christos }
   6406  1.15  christos 
   6407  1.15  christos bfd_boolean
   6408   1.1     skrll _bfd_elf_init_file_header (bfd *abfd,
   6409   1.3  christos 			   struct bfd_link_info *info ATTRIBUTE_UNUSED)
   6410   1.1     skrll {
   6411   1.1     skrll   Elf_Internal_Ehdr *i_ehdrp;	/* Elf file header, internal form.  */
   6412   1.1     skrll   struct elf_strtab_hash *shstrtab;
   6413   1.1     skrll   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6414   1.1     skrll 
   6415   1.1     skrll   i_ehdrp = elf_elfheader (abfd);
   6416   1.1     skrll 
   6417   1.1     skrll   shstrtab = _bfd_elf_strtab_init ();
   6418   1.1     skrll   if (shstrtab == NULL)
   6419   1.1     skrll     return FALSE;
   6420   1.1     skrll 
   6421   1.1     skrll   elf_shstrtab (abfd) = shstrtab;
   6422   1.1     skrll 
   6423   1.1     skrll   i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
   6424   1.1     skrll   i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
   6425   1.1     skrll   i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
   6426   1.1     skrll   i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
   6427   1.1     skrll 
   6428   1.1     skrll   i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
   6429   1.1     skrll   i_ehdrp->e_ident[EI_DATA] =
   6430   1.1     skrll     bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
   6431   1.1     skrll   i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
   6432   1.1     skrll 
   6433   1.1     skrll   if ((abfd->flags & DYNAMIC) != 0)
   6434   1.1     skrll     i_ehdrp->e_type = ET_DYN;
   6435   1.1     skrll   else if ((abfd->flags & EXEC_P) != 0)
   6436   1.1     skrll     i_ehdrp->e_type = ET_EXEC;
   6437   1.1     skrll   else if (bfd_get_format (abfd) == bfd_core)
   6438   1.1     skrll     i_ehdrp->e_type = ET_CORE;
   6439   1.1     skrll   else
   6440   1.1     skrll     i_ehdrp->e_type = ET_REL;
   6441   1.1     skrll 
   6442   1.1     skrll   switch (bfd_get_arch (abfd))
   6443   1.1     skrll     {
   6444   1.1     skrll     case bfd_arch_unknown:
   6445   1.1     skrll       i_ehdrp->e_machine = EM_NONE;
   6446   1.1     skrll       break;
   6447   1.1     skrll 
   6448   1.1     skrll       /* There used to be a long list of cases here, each one setting
   6449   1.1     skrll 	 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
   6450   1.1     skrll 	 in the corresponding bfd definition.  To avoid duplication,
   6451   1.1     skrll 	 the switch was removed.  Machines that need special handling
   6452   1.1     skrll 	 can generally do it in elf_backend_final_write_processing(),
   6453   1.1     skrll 	 unless they need the information earlier than the final write.
   6454   1.1     skrll 	 Such need can generally be supplied by replacing the tests for
   6455   1.1     skrll 	 e_machine with the conditions used to determine it.  */
   6456   1.1     skrll     default:
   6457   1.1     skrll       i_ehdrp->e_machine = bed->elf_machine_code;
   6458   1.1     skrll     }
   6459   1.1     skrll 
   6460   1.1     skrll   i_ehdrp->e_version = bed->s->ev_current;
   6461   1.1     skrll   i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
   6462   1.1     skrll 
   6463   1.1     skrll   /* No program header, for now.  */
   6464   1.1     skrll   i_ehdrp->e_phoff = 0;
   6465   1.1     skrll   i_ehdrp->e_phentsize = 0;
   6466   1.1     skrll   i_ehdrp->e_phnum = 0;
   6467   1.1     skrll 
   6468   1.1     skrll   /* Each bfd section is section header entry.  */
   6469   1.1     skrll   i_ehdrp->e_entry = bfd_get_start_address (abfd);
   6470   1.1     skrll   i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
   6471   1.1     skrll 
   6472   1.1     skrll   elf_tdata (abfd)->symtab_hdr.sh_name =
   6473   1.1     skrll     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
   6474   1.1     skrll   elf_tdata (abfd)->strtab_hdr.sh_name =
   6475   1.1     skrll     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
   6476   1.1     skrll   elf_tdata (abfd)->shstrtab_hdr.sh_name =
   6477   1.6  christos     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
   6478   1.1     skrll   if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
   6479   1.1     skrll       || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
   6480   1.1     skrll       || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
   6481   1.1     skrll     return FALSE;
   6482   1.1     skrll 
   6483   1.1     skrll   return TRUE;
   6484  1.15  christos }
   6485  1.15  christos 
   6486  1.15  christos /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
   6487  1.15  christos 
   6488  1.15  christos    FIXME: We used to have code here to sort the PT_LOAD segments into
   6489  1.15  christos    ascending order, as per the ELF spec.  But this breaks some programs,
   6490  1.15  christos    including the Linux kernel.  But really either the spec should be
   6491  1.15  christos    changed or the programs updated.  */
   6492  1.15  christos 
   6493  1.15  christos bfd_boolean
   6494  1.15  christos _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
   6495  1.15  christos {
   6496  1.15  christos   if (link_info != NULL && bfd_link_pie (link_info))
   6497  1.15  christos     {
   6498  1.15  christos       Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
   6499  1.15  christos       unsigned int num_segments = i_ehdrp->e_phnum;
   6500  1.15  christos       struct elf_obj_tdata *tdata = elf_tdata (obfd);
   6501  1.15  christos       Elf_Internal_Phdr *segment = tdata->phdr;
   6502  1.15  christos       Elf_Internal_Phdr *end_segment = &segment[num_segments];
   6503  1.15  christos 
   6504  1.15  christos       /* Find the lowest p_vaddr in PT_LOAD segments.  */
   6505  1.15  christos       bfd_vma p_vaddr = (bfd_vma) -1;
   6506  1.15  christos       for (; segment < end_segment; segment++)
   6507  1.15  christos 	if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
   6508  1.15  christos 	  p_vaddr = segment->p_vaddr;
   6509  1.15  christos 
   6510  1.15  christos       /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
   6511  1.15  christos 	 segments is non-zero.  */
   6512  1.15  christos       if (p_vaddr)
   6513  1.15  christos 	i_ehdrp->e_type = ET_EXEC;
   6514  1.15  christos     }
   6515  1.15  christos   return TRUE;
   6516   1.1     skrll }
   6517   1.6  christos 
   6518   1.1     skrll /* Assign file positions for all the reloc sections which are not part
   6519   1.6  christos    of the loadable file image, and the file position of section headers.  */
   6520   1.6  christos 
   6521   1.1     skrll static bfd_boolean
   6522   1.1     skrll _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
   6523   1.6  christos {
   6524   1.6  christos   file_ptr off;
   6525   1.6  christos   Elf_Internal_Shdr **shdrpp, **end_shdrpp;
   6526   1.6  christos   Elf_Internal_Shdr *shdrp;
   6527   1.1     skrll   Elf_Internal_Ehdr *i_ehdrp;
   6528   1.6  christos   const struct elf_backend_data *bed;
   6529   1.1     skrll 
   6530   1.6  christos   off = elf_next_file_pos (abfd);
   6531   1.6  christos 
   6532   1.6  christos   shdrpp = elf_elfsections (abfd);
   6533   1.1     skrll   end_shdrpp = shdrpp + elf_numsections (abfd);
   6534   1.6  christos   for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
   6535   1.6  christos     {
   6536   1.6  christos       shdrp = *shdrpp;
   6537   1.6  christos       if (shdrp->sh_offset == -1)
   6538   1.6  christos 	{
   6539   1.6  christos 	  asection *sec = shdrp->bfd_section;
   6540  1.15  christos 	  bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
   6541   1.6  christos 				|| shdrp->sh_type == SHT_RELA);
   6542  1.15  christos 	  bfd_boolean is_ctf = sec && bfd_section_is_ctf (sec);
   6543   1.6  christos 	  if (is_rel
   6544   1.6  christos 	      || is_ctf
   6545  1.15  christos 	      || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
   6546   1.6  christos 	    {
   6547   1.6  christos 	      if (!is_rel && !is_ctf)
   6548   1.6  christos 		{
   6549   1.6  christos 		  const char *name = sec->name;
   6550   1.6  christos 		  struct bfd_elf_section_data *d;
   6551   1.6  christos 
   6552   1.6  christos 		  /* Compress DWARF debug sections.  */
   6553   1.6  christos 		  if (!bfd_compress_section (abfd, sec,
   6554   1.1     skrll 					     shdrp->contents))
   6555   1.6  christos 		    return FALSE;
   6556   1.6  christos 
   6557   1.6  christos 		  if (sec->compress_status == COMPRESS_SECTION_DONE
   6558   1.6  christos 		      && (abfd->flags & BFD_COMPRESS_GABI) == 0)
   6559   1.6  christos 		    {
   6560   1.6  christos 		      /* If section is compressed with zlib-gnu, convert
   6561   1.6  christos 			 section name from .debug_* to .zdebug_*.  */
   6562   1.6  christos 		      char *new_name
   6563   1.6  christos 			= convert_debug_to_zdebug (abfd, name);
   6564   1.6  christos 		      if (new_name == NULL)
   6565   1.6  christos 			return FALSE;
   6566   1.9  christos 		      name = new_name;
   6567   1.6  christos 		    }
   6568   1.6  christos 		  /* Add section name to section name section.  */
   6569   1.6  christos 		  if (shdrp->sh_name != (unsigned int) -1)
   6570   1.6  christos 		    abort ();
   6571   1.6  christos 		  shdrp->sh_name
   6572   1.6  christos 		    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   6573   1.6  christos 							  name, FALSE);
   6574   1.9  christos 		  d = elf_section_data (sec);
   6575   1.6  christos 
   6576   1.6  christos 		  /* Add reloc section name to section name section.  */
   6577   1.6  christos 		  if (d->rel.hdr
   6578   1.6  christos 		      && !_bfd_elf_set_reloc_sh_name (abfd,
   6579   1.6  christos 						      d->rel.hdr,
   6580   1.6  christos 						      name, FALSE))
   6581   1.6  christos 		    return FALSE;
   6582   1.6  christos 		  if (d->rela.hdr
   6583   1.6  christos 		      && !_bfd_elf_set_reloc_sh_name (abfd,
   6584   1.6  christos 						      d->rela.hdr,
   6585   1.6  christos 						      name, TRUE))
   6586   1.6  christos 		    return FALSE;
   6587   1.6  christos 
   6588   1.6  christos 		  /* Update section size and contents.  */
   6589   1.6  christos 		  shdrp->sh_size = sec->size;
   6590   1.6  christos 		  shdrp->contents = sec->contents;
   6591  1.15  christos 		  shdrp->bfd_section->contents = NULL;
   6592  1.15  christos 		}
   6593  1.15  christos 	      else if (is_ctf)
   6594  1.15  christos 		{
   6595  1.15  christos 		  /* Update section size and contents.	*/
   6596  1.15  christos 		  shdrp->sh_size = sec->size;
   6597  1.15  christos 		  shdrp->contents = sec->contents;
   6598   1.6  christos 		}
   6599   1.6  christos 
   6600   1.6  christos 	      off = _bfd_elf_assign_file_position_for_section (shdrp,
   6601   1.6  christos 							       off,
   6602   1.6  christos 							       TRUE);
   6603   1.1     skrll 	    }
   6604   1.1     skrll 	}
   6605   1.6  christos     }
   6606   1.6  christos 
   6607   1.6  christos   /* Place section name section after DWARF debug sections have been
   6608   1.6  christos      compressed.  */
   6609   1.6  christos   _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
   6610   1.6  christos   shdrp = &elf_tdata (abfd)->shstrtab_hdr;
   6611   1.6  christos   shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
   6612   1.6  christos   off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
   6613   1.6  christos 
   6614   1.6  christos   /* Place the section headers.  */
   6615   1.6  christos   i_ehdrp = elf_elfheader (abfd);
   6616   1.6  christos   bed = get_elf_backend_data (abfd);
   6617   1.6  christos   off = align_file_position (off, 1 << bed->s->log_file_align);
   6618   1.6  christos   i_ehdrp->e_shoff = off;
   6619   1.6  christos   off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
   6620   1.6  christos   elf_next_file_pos (abfd) = off;
   6621   1.1     skrll 
   6622   1.1     skrll   return TRUE;
   6623   1.1     skrll }
   6624   1.1     skrll 
   6625   1.1     skrll bfd_boolean
   6626   1.1     skrll _bfd_elf_write_object_contents (bfd *abfd)
   6627   1.1     skrll {
   6628   1.1     skrll   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6629   1.1     skrll   Elf_Internal_Shdr **i_shdrp;
   6630   1.6  christos   bfd_boolean failed;
   6631   1.1     skrll   unsigned int count, num_sec;
   6632   1.1     skrll   struct elf_obj_tdata *t;
   6633   1.1     skrll 
   6634   1.1     skrll   if (! abfd->output_has_begun
   6635  1.15  christos       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   6636  1.15  christos     return FALSE;
   6637  1.15  christos   /* Do not rewrite ELF data when the BFD has been opened for update.
   6638  1.15  christos      abfd->output_has_begun was set to TRUE on opening, so creation of new
   6639  1.15  christos      sections, and modification of existing section sizes was restricted.
   6640  1.15  christos      This means the ELF header, program headers and section headers can't have
   6641  1.15  christos      changed.
   6642  1.15  christos      If the contents of any sections has been modified, then those changes have
   6643  1.15  christos      already been written to the BFD.  */
   6644  1.15  christos   else if (abfd->direction == both_direction)
   6645  1.15  christos     {
   6646  1.15  christos       BFD_ASSERT (abfd->output_has_begun);
   6647   1.1     skrll       return TRUE;
   6648   1.1     skrll     }
   6649   1.1     skrll 
   6650   1.1     skrll   i_shdrp = elf_elfsections (abfd);
   6651   1.1     skrll 
   6652   1.1     skrll   failed = FALSE;
   6653   1.1     skrll   bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
   6654   1.1     skrll   if (failed)
   6655   1.6  christos     return FALSE;
   6656   1.6  christos 
   6657   1.1     skrll   if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
   6658   1.1     skrll     return FALSE;
   6659   1.1     skrll 
   6660   1.1     skrll   /* After writing the headers, we need to write the sections too...  */
   6661   1.1     skrll   num_sec = elf_numsections (abfd);
   6662   1.6  christos   for (count = 1; count < num_sec; count++)
   6663   1.6  christos     {
   6664   1.6  christos       i_shdrp[count]->sh_name
   6665   1.1     skrll 	= _bfd_elf_strtab_offset (elf_shstrtab (abfd),
   6666  1.13  christos 				  i_shdrp[count]->sh_name);
   6667  1.13  christos       if (bed->elf_backend_section_processing)
   6668   1.1     skrll 	if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
   6669   1.1     skrll 	  return FALSE;
   6670   1.1     skrll       if (i_shdrp[count]->contents)
   6671   1.1     skrll 	{
   6672   1.1     skrll 	  bfd_size_type amt = i_shdrp[count]->sh_size;
   6673   1.1     skrll 
   6674   1.1     skrll 	  if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
   6675   1.1     skrll 	      || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
   6676   1.1     skrll 	    return FALSE;
   6677   1.1     skrll 	}
   6678   1.1     skrll     }
   6679   1.6  christos 
   6680   1.1     skrll   /* Write out the section header names.  */
   6681   1.6  christos   t = elf_tdata (abfd);
   6682   1.1     skrll   if (elf_shstrtab (abfd) != NULL
   6683   1.1     skrll       && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
   6684   1.1     skrll 	  || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
   6685  1.15  christos     return FALSE;
   6686  1.15  christos 
   6687   1.1     skrll   if (!(*bed->elf_backend_final_write_processing) (abfd))
   6688   1.1     skrll     return FALSE;
   6689   1.1     skrll 
   6690   1.1     skrll   if (!bed->s->write_shdrs_and_ehdr (abfd))
   6691   1.1     skrll     return FALSE;
   6692   1.6  christos 
   6693   1.6  christos   /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
   6694   1.1     skrll   if (t->o->build_id.after_write_object_contents != NULL)
   6695   1.1     skrll     return (*t->o->build_id.after_write_object_contents) (abfd);
   6696   1.1     skrll 
   6697   1.1     skrll   return TRUE;
   6698   1.1     skrll }
   6699   1.1     skrll 
   6700   1.1     skrll bfd_boolean
   6701   1.1     skrll _bfd_elf_write_corefile_contents (bfd *abfd)
   6702   1.1     skrll {
   6703   1.1     skrll   /* Hopefully this can be done just like an object file.  */
   6704   1.1     skrll   return _bfd_elf_write_object_contents (abfd);
   6705   1.1     skrll }
   6706   1.1     skrll 
   6707   1.1     skrll /* Given a section, search the header to find them.  */
   6708   1.1     skrll 
   6709   1.1     skrll unsigned int
   6710   1.1     skrll _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
   6711   1.3  christos {
   6712   1.1     skrll   const struct elf_backend_data *bed;
   6713   1.1     skrll   unsigned int sec_index;
   6714   1.1     skrll 
   6715   1.1     skrll   if (elf_section_data (asect) != NULL
   6716   1.1     skrll       && elf_section_data (asect)->this_idx != 0)
   6717   1.1     skrll     return elf_section_data (asect)->this_idx;
   6718   1.3  christos 
   6719   1.1     skrll   if (bfd_is_abs_section (asect))
   6720   1.3  christos     sec_index = SHN_ABS;
   6721   1.1     skrll   else if (bfd_is_com_section (asect))
   6722   1.3  christos     sec_index = SHN_COMMON;
   6723   1.1     skrll   else if (bfd_is_und_section (asect))
   6724   1.3  christos     sec_index = SHN_UNDEF;
   6725   1.1     skrll   else
   6726   1.1     skrll     sec_index = SHN_BAD;
   6727   1.1     skrll 
   6728   1.1     skrll   bed = get_elf_backend_data (abfd);
   6729   1.3  christos   if (bed->elf_backend_section_from_bfd_section)
   6730   1.1     skrll     {
   6731   1.1     skrll       int retval = sec_index;
   6732   1.1     skrll 
   6733   1.1     skrll       if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
   6734   1.1     skrll 	return retval;
   6735   1.3  christos     }
   6736   1.1     skrll 
   6737   1.1     skrll   if (sec_index == SHN_BAD)
   6738   1.3  christos     bfd_set_error (bfd_error_nonrepresentable_section);
   6739   1.1     skrll 
   6740   1.1     skrll   return sec_index;
   6741   1.1     skrll }
   6742   1.1     skrll 
   6743   1.1     skrll /* Given a BFD symbol, return the index in the ELF symbol table, or -1
   6744   1.1     skrll    on error.  */
   6745   1.1     skrll 
   6746   1.1     skrll int
   6747   1.1     skrll _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
   6748   1.1     skrll {
   6749   1.1     skrll   asymbol *asym_ptr = *asym_ptr_ptr;
   6750   1.1     skrll   int idx;
   6751   1.1     skrll   flagword flags = asym_ptr->flags;
   6752   1.1     skrll 
   6753   1.1     skrll   /* When gas creates relocations against local labels, it creates its
   6754   1.1     skrll      own symbol for the section, but does put the symbol into the
   6755   1.1     skrll      symbol chain, so udata is 0.  When the linker is generating
   6756   1.1     skrll      relocatable output, this section symbol may be for one of the
   6757   1.1     skrll      input sections rather than the output section.  */
   6758   1.1     skrll   if (asym_ptr->udata.i == 0
   6759   1.1     skrll       && (flags & BSF_SECTION_SYM)
   6760   1.1     skrll       && asym_ptr->section)
   6761   1.1     skrll     {
   6762   1.1     skrll       asection *sec;
   6763   1.1     skrll       int indx;
   6764   1.1     skrll 
   6765   1.1     skrll       sec = asym_ptr->section;
   6766   1.1     skrll       if (sec->owner != abfd && sec->output_section != NULL)
   6767   1.1     skrll 	sec = sec->output_section;
   6768   1.1     skrll       if (sec->owner == abfd
   6769   1.1     skrll 	  && (indx = sec->index) < elf_num_section_syms (abfd)
   6770   1.1     skrll 	  && elf_section_syms (abfd)[indx] != NULL)
   6771   1.1     skrll 	asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
   6772   1.1     skrll     }
   6773   1.1     skrll 
   6774   1.1     skrll   idx = asym_ptr->udata.i;
   6775   1.1     skrll 
   6776   1.1     skrll   if (idx == 0)
   6777   1.1     skrll     {
   6778   1.9  christos       /* This case can occur when using --strip-symbol on a symbol
   6779   1.9  christos 	 which is used in a relocation entry.  */
   6780  1.13  christos       _bfd_error_handler
   6781   1.1     skrll 	/* xgettext:c-format */
   6782   1.1     skrll 	(_("%pB: symbol `%s' required but not present"),
   6783   1.1     skrll 	 abfd, bfd_asymbol_name (asym_ptr));
   6784   1.1     skrll       bfd_set_error (bfd_error_no_symbols);
   6785   1.1     skrll       return -1;
   6786   1.1     skrll     }
   6787   1.1     skrll 
   6788   1.1     skrll #if DEBUG & 4
   6789   1.9  christos   {
   6790   1.9  christos     fprintf (stderr,
   6791   1.1     skrll 	     "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
   6792   1.1     skrll 	     (long) asym_ptr, asym_ptr->name, idx, flags);
   6793   1.1     skrll     fflush (stderr);
   6794   1.1     skrll   }
   6795   1.1     skrll #endif
   6796   1.1     skrll 
   6797   1.1     skrll   return idx;
   6798   1.1     skrll }
   6799   1.1     skrll 
   6800   1.1     skrll /* Rewrite program header information.  */
   6801   1.1     skrll 
   6802   1.1     skrll static bfd_boolean
   6803   1.1     skrll rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
   6804   1.1     skrll {
   6805   1.1     skrll   Elf_Internal_Ehdr *iehdr;
   6806   1.1     skrll   struct elf_segment_map *map;
   6807   1.1     skrll   struct elf_segment_map *map_first;
   6808   1.1     skrll   struct elf_segment_map **pointer_to_map;
   6809   1.1     skrll   Elf_Internal_Phdr *segment;
   6810   1.1     skrll   asection *section;
   6811   1.1     skrll   unsigned int i;
   6812   1.1     skrll   unsigned int num_segments;
   6813   1.1     skrll   bfd_boolean phdr_included = FALSE;
   6814   1.1     skrll   bfd_boolean p_paddr_valid;
   6815   1.1     skrll   bfd_vma maxpagesize;
   6816   1.1     skrll   struct elf_segment_map *phdr_adjust_seg = NULL;
   6817   1.1     skrll   unsigned int phdr_adjust_num = 0;
   6818   1.1     skrll   const struct elf_backend_data *bed;
   6819   1.1     skrll 
   6820   1.1     skrll   bed = get_elf_backend_data (ibfd);
   6821   1.1     skrll   iehdr = elf_elfheader (ibfd);
   6822   1.1     skrll 
   6823   1.1     skrll   map_first = NULL;
   6824   1.1     skrll   pointer_to_map = &map_first;
   6825   1.1     skrll 
   6826   1.1     skrll   num_segments = elf_elfheader (ibfd)->e_phnum;
   6827   1.1     skrll   maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
   6828   1.1     skrll 
   6829   1.1     skrll   /* Returns the end address of the segment + 1.  */
   6830   1.1     skrll #define SEGMENT_END(segment, start)					\
   6831   1.1     skrll   (start + (segment->p_memsz > segment->p_filesz			\
   6832   1.1     skrll 	    ? segment->p_memsz : segment->p_filesz))
   6833   1.1     skrll 
   6834   1.1     skrll #define SECTION_SIZE(section, segment)					\
   6835   1.1     skrll   (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL))		\
   6836   1.1     skrll     != SEC_THREAD_LOCAL || segment->p_type == PT_TLS)			\
   6837   1.1     skrll    ? section->size : 0)
   6838   1.1     skrll 
   6839   1.1     skrll   /* Returns TRUE if the given section is contained within
   6840   1.1     skrll      the given segment.  VMA addresses are compared.  */
   6841   1.1     skrll #define IS_CONTAINED_BY_VMA(section, segment)				\
   6842   1.1     skrll   (section->vma >= segment->p_vaddr					\
   6843   1.1     skrll    && (section->vma + SECTION_SIZE (section, segment)			\
   6844   1.1     skrll        <= (SEGMENT_END (segment, segment->p_vaddr))))
   6845   1.1     skrll 
   6846   1.1     skrll   /* Returns TRUE if the given section is contained within
   6847   1.1     skrll      the given segment.  LMA addresses are compared.  */
   6848  1.15  christos #define IS_CONTAINED_BY_LMA(section, segment, base)			\
   6849   1.1     skrll   (section->lma >= base							\
   6850   1.1     skrll    && (section->lma + SECTION_SIZE (section, segment) >= section->lma)	\
   6851   1.1     skrll    && (section->lma + SECTION_SIZE (section, segment)			\
   6852   1.1     skrll        <= SEGMENT_END (segment, base)))
   6853   1.1     skrll 
   6854   1.1     skrll   /* Handle PT_NOTE segment.  */
   6855   1.1     skrll #define IS_NOTE(p, s)							\
   6856   1.1     skrll   (p->p_type == PT_NOTE							\
   6857   1.1     skrll    && elf_section_type (s) == SHT_NOTE					\
   6858   1.1     skrll    && (bfd_vma) s->filepos >= p->p_offset				\
   6859   1.1     skrll    && ((bfd_vma) s->filepos + s->size					\
   6860   1.1     skrll        <= p->p_offset + p->p_filesz))
   6861   1.1     skrll 
   6862   1.1     skrll   /* Special case: corefile "NOTE" section containing regs, prpsinfo
   6863   1.1     skrll      etc.  */
   6864   1.1     skrll #define IS_COREFILE_NOTE(p, s)						\
   6865   1.1     skrll   (IS_NOTE (p, s)							\
   6866   1.1     skrll    && bfd_get_format (ibfd) == bfd_core					\
   6867   1.1     skrll    && s->vma == 0							\
   6868   1.1     skrll    && s->lma == 0)
   6869   1.1     skrll 
   6870   1.1     skrll   /* The complicated case when p_vaddr is 0 is to handle the Solaris
   6871   1.1     skrll      linker, which generates a PT_INTERP section with p_vaddr and
   6872   1.1     skrll      p_memsz set to 0.  */
   6873   1.1     skrll #define IS_SOLARIS_PT_INTERP(p, s)					\
   6874   1.1     skrll   (p->p_vaddr == 0							\
   6875   1.1     skrll    && p->p_paddr == 0							\
   6876   1.1     skrll    && p->p_memsz == 0							\
   6877   1.1     skrll    && p->p_filesz > 0							\
   6878   1.1     skrll    && (s->flags & SEC_HAS_CONTENTS) != 0				\
   6879   1.1     skrll    && s->size > 0							\
   6880   1.1     skrll    && (bfd_vma) s->filepos >= p->p_offset				\
   6881   1.1     skrll    && ((bfd_vma) s->filepos + s->size					\
   6882   1.1     skrll        <= p->p_offset + p->p_filesz))
   6883   1.1     skrll 
   6884   1.1     skrll   /* Decide if the given section should be included in the given segment.
   6885   1.1     skrll      A section will be included if:
   6886   1.1     skrll        1. It is within the address space of the segment -- we use the LMA
   6887   1.5  christos 	  if that is set for the segment and the VMA otherwise,
   6888   1.1     skrll        2. It is an allocated section or a NOTE section in a PT_NOTE
   6889   1.1     skrll 	  segment.
   6890   1.1     skrll        3. There is an output section associated with it,
   6891   1.1     skrll        4. The section has not already been allocated to a previous segment.
   6892   1.1     skrll        5. PT_GNU_STACK segments do not include any sections.
   6893   1.1     skrll        6. PT_TLS segment includes only SHF_TLS sections.
   6894   1.1     skrll        7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
   6895   1.1     skrll        8. PT_DYNAMIC should not contain empty sections at the beginning
   6896   1.1     skrll 	  (with the possible exception of .dynamic).  */
   6897   1.1     skrll #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed)		\
   6898   1.1     skrll   ((((segment->p_paddr							\
   6899   1.1     skrll       ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr)	\
   6900   1.1     skrll       : IS_CONTAINED_BY_VMA (section, segment))				\
   6901   1.1     skrll      && (section->flags & SEC_ALLOC) != 0)				\
   6902   1.1     skrll     || IS_NOTE (segment, section))					\
   6903   1.1     skrll    && segment->p_type != PT_GNU_STACK					\
   6904   1.1     skrll    && (segment->p_type != PT_TLS					\
   6905   1.1     skrll        || (section->flags & SEC_THREAD_LOCAL))				\
   6906   1.1     skrll    && (segment->p_type == PT_LOAD					\
   6907   1.1     skrll        || segment->p_type == PT_TLS					\
   6908   1.1     skrll        || (section->flags & SEC_THREAD_LOCAL) == 0)			\
   6909   1.1     skrll    && (segment->p_type != PT_DYNAMIC					\
   6910   1.1     skrll        || SECTION_SIZE (section, segment) > 0				\
   6911   1.1     skrll        || (segment->p_paddr						\
   6912  1.15  christos 	   ? segment->p_paddr != section->lma				\
   6913  1.13  christos 	   : segment->p_vaddr != section->vma)				\
   6914   1.1     skrll        || (strcmp (bfd_section_name (section), ".dynamic") == 0))	\
   6915   1.1     skrll    && (segment->p_type != PT_LOAD || !section->segment_mark))
   6916   1.1     skrll 
   6917   1.1     skrll /* If the output section of a section in the input segment is NULL,
   6918   1.1     skrll    it is removed from the corresponding output segment.   */
   6919   1.1     skrll #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed)		\
   6920   1.1     skrll   (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)		\
   6921   1.1     skrll    && section->output_section != NULL)
   6922   1.1     skrll 
   6923   1.1     skrll   /* Returns TRUE iff seg1 starts after the end of seg2.  */
   6924   1.1     skrll #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)			\
   6925   1.1     skrll   (seg1->field >= SEGMENT_END (seg2, seg2->field))
   6926   1.1     skrll 
   6927   1.1     skrll   /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
   6928   1.1     skrll      their VMA address ranges and their LMA address ranges overlap.
   6929   1.1     skrll      It is possible to have overlapping VMA ranges without overlapping LMA
   6930   1.1     skrll      ranges.  RedBoot images for example can have both .data and .bss mapped
   6931   1.1     skrll      to the same VMA range, but with the .data section mapped to a different
   6932   1.1     skrll      LMA.  */
   6933   1.1     skrll #define SEGMENT_OVERLAPS(seg1, seg2)					\
   6934   1.1     skrll   (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)			\
   6935   1.1     skrll 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))			\
   6936   1.1     skrll    && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)			\
   6937   1.1     skrll 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
   6938   1.1     skrll 
   6939   1.1     skrll   /* Initialise the segment mark field.  */
   6940   1.1     skrll   for (section = ibfd->sections; section != NULL; section = section->next)
   6941   1.1     skrll     section->segment_mark = FALSE;
   6942   1.1     skrll 
   6943   1.1     skrll   /* The Solaris linker creates program headers in which all the
   6944   1.1     skrll      p_paddr fields are zero.  When we try to objcopy or strip such a
   6945   1.1     skrll      file, we get confused.  Check for this case, and if we find it
   6946   1.1     skrll      don't set the p_paddr_valid fields.  */
   6947   1.1     skrll   p_paddr_valid = FALSE;
   6948   1.1     skrll   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   6949   1.1     skrll        i < num_segments;
   6950   1.1     skrll        i++, segment++)
   6951   1.1     skrll     if (segment->p_paddr != 0)
   6952   1.1     skrll       {
   6953   1.1     skrll 	p_paddr_valid = TRUE;
   6954   1.1     skrll 	break;
   6955   1.1     skrll       }
   6956   1.1     skrll 
   6957   1.1     skrll   /* Scan through the segments specified in the program header
   6958   1.1     skrll      of the input BFD.  For this first scan we look for overlaps
   6959   1.1     skrll      in the loadable segments.  These can be created by weird
   6960   1.1     skrll      parameters to objcopy.  Also, fix some solaris weirdness.  */
   6961   1.1     skrll   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   6962   1.1     skrll        i < num_segments;
   6963   1.1     skrll        i++, segment++)
   6964   1.1     skrll     {
   6965   1.1     skrll       unsigned int j;
   6966   1.1     skrll       Elf_Internal_Phdr *segment2;
   6967   1.1     skrll 
   6968   1.1     skrll       if (segment->p_type == PT_INTERP)
   6969   1.1     skrll 	for (section = ibfd->sections; section; section = section->next)
   6970   1.1     skrll 	  if (IS_SOLARIS_PT_INTERP (segment, section))
   6971   1.1     skrll 	    {
   6972   1.1     skrll 	      /* Mininal change so that the normal section to segment
   6973   1.1     skrll 		 assignment code will work.  */
   6974   1.1     skrll 	      segment->p_vaddr = section->vma;
   6975   1.1     skrll 	      break;
   6976   1.1     skrll 	    }
   6977   1.1     skrll 
   6978   1.1     skrll       if (segment->p_type != PT_LOAD)
   6979   1.1     skrll 	{
   6980   1.1     skrll 	  /* Remove PT_GNU_RELRO segment.  */
   6981   1.1     skrll 	  if (segment->p_type == PT_GNU_RELRO)
   6982   1.1     skrll 	    segment->p_type = PT_NULL;
   6983   1.1     skrll 	  continue;
   6984   1.1     skrll 	}
   6985   1.1     skrll 
   6986   1.1     skrll       /* Determine if this segment overlaps any previous segments.  */
   6987   1.1     skrll       for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
   6988   1.1     skrll 	{
   6989   1.1     skrll 	  bfd_signed_vma extra_length;
   6990   1.1     skrll 
   6991   1.1     skrll 	  if (segment2->p_type != PT_LOAD
   6992   1.1     skrll 	      || !SEGMENT_OVERLAPS (segment, segment2))
   6993   1.1     skrll 	    continue;
   6994   1.1     skrll 
   6995   1.1     skrll 	  /* Merge the two segments together.  */
   6996   1.1     skrll 	  if (segment2->p_vaddr < segment->p_vaddr)
   6997   1.1     skrll 	    {
   6998   1.1     skrll 	      /* Extend SEGMENT2 to include SEGMENT and then delete
   6999   1.1     skrll 		 SEGMENT.  */
   7000   1.1     skrll 	      extra_length = (SEGMENT_END (segment, segment->p_vaddr)
   7001   1.1     skrll 			      - SEGMENT_END (segment2, segment2->p_vaddr));
   7002   1.1     skrll 
   7003   1.1     skrll 	      if (extra_length > 0)
   7004   1.1     skrll 		{
   7005   1.1     skrll 		  segment2->p_memsz += extra_length;
   7006   1.1     skrll 		  segment2->p_filesz += extra_length;
   7007   1.1     skrll 		}
   7008   1.1     skrll 
   7009   1.1     skrll 	      segment->p_type = PT_NULL;
   7010   1.1     skrll 
   7011   1.1     skrll 	      /* Since we have deleted P we must restart the outer loop.  */
   7012   1.1     skrll 	      i = 0;
   7013   1.1     skrll 	      segment = elf_tdata (ibfd)->phdr;
   7014   1.1     skrll 	      break;
   7015   1.1     skrll 	    }
   7016   1.1     skrll 	  else
   7017   1.1     skrll 	    {
   7018   1.1     skrll 	      /* Extend SEGMENT to include SEGMENT2 and then delete
   7019   1.1     skrll 		 SEGMENT2.  */
   7020   1.1     skrll 	      extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
   7021   1.1     skrll 			      - SEGMENT_END (segment, segment->p_vaddr));
   7022   1.1     skrll 
   7023   1.1     skrll 	      if (extra_length > 0)
   7024   1.1     skrll 		{
   7025   1.1     skrll 		  segment->p_memsz += extra_length;
   7026   1.1     skrll 		  segment->p_filesz += extra_length;
   7027   1.1     skrll 		}
   7028   1.1     skrll 
   7029   1.1     skrll 	      segment2->p_type = PT_NULL;
   7030   1.1     skrll 	    }
   7031   1.1     skrll 	}
   7032   1.1     skrll     }
   7033   1.1     skrll 
   7034   1.1     skrll   /* The second scan attempts to assign sections to segments.  */
   7035   1.1     skrll   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7036   1.1     skrll        i < num_segments;
   7037   1.1     skrll        i++, segment++)
   7038   1.1     skrll     {
   7039   1.1     skrll       unsigned int section_count;
   7040   1.1     skrll       asection **sections;
   7041  1.13  christos       asection *output_section;
   7042  1.13  christos       unsigned int isec;
   7043   1.1     skrll       asection *matching_lma;
   7044   1.1     skrll       asection *suggested_lma;
   7045   1.1     skrll       unsigned int j;
   7046   1.1     skrll       bfd_size_type amt;
   7047   1.1     skrll       asection *first_section;
   7048   1.1     skrll 
   7049   1.1     skrll       if (segment->p_type == PT_NULL)
   7050   1.1     skrll 	continue;
   7051   1.1     skrll 
   7052   1.1     skrll       first_section = NULL;
   7053   1.1     skrll       /* Compute how many sections might be placed into this segment.  */
   7054   1.1     skrll       for (section = ibfd->sections, section_count = 0;
   7055   1.1     skrll 	   section != NULL;
   7056   1.1     skrll 	   section = section->next)
   7057   1.1     skrll 	{
   7058   1.1     skrll 	  /* Find the first section in the input segment, which may be
   7059   1.1     skrll 	     removed from the corresponding output segment.   */
   7060   1.1     skrll 	  if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
   7061   1.1     skrll 	    {
   7062   1.1     skrll 	      if (first_section == NULL)
   7063   1.1     skrll 		first_section = section;
   7064   1.1     skrll 	      if (section->output_section != NULL)
   7065   1.1     skrll 		++section_count;
   7066   1.1     skrll 	    }
   7067   1.1     skrll 	}
   7068   1.1     skrll 
   7069  1.15  christos       /* Allocate a segment map big enough to contain
   7070  1.15  christos 	 all of the sections we have selected.  */
   7071   1.3  christos       amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   7072   1.1     skrll       amt += (bfd_size_type) section_count * sizeof (asection *);
   7073   1.1     skrll       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7074   1.1     skrll       if (map == NULL)
   7075   1.1     skrll 	return FALSE;
   7076   1.1     skrll 
   7077   1.1     skrll       /* Initialise the fields of the segment map.  Default to
   7078   1.1     skrll 	 using the physical address of the segment in the input BFD.  */
   7079   1.1     skrll       map->next = NULL;
   7080   1.1     skrll       map->p_type = segment->p_type;
   7081   1.1     skrll       map->p_flags = segment->p_flags;
   7082   1.1     skrll       map->p_flags_valid = 1;
   7083   1.1     skrll 
   7084   1.1     skrll       /* If the first section in the input segment is removed, there is
   7085   1.1     skrll 	 no need to preserve segment physical address in the corresponding
   7086   1.1     skrll 	 output segment.  */
   7087   1.1     skrll       if (!first_section || first_section->output_section != NULL)
   7088   1.1     skrll 	{
   7089   1.1     skrll 	  map->p_paddr = segment->p_paddr;
   7090   1.1     skrll 	  map->p_paddr_valid = p_paddr_valid;
   7091   1.1     skrll 	}
   7092   1.1     skrll 
   7093   1.1     skrll       /* Determine if this segment contains the ELF file header
   7094   1.1     skrll 	 and if it contains the program headers themselves.  */
   7095   1.1     skrll       map->includes_filehdr = (segment->p_offset == 0
   7096   1.1     skrll 			       && segment->p_filesz >= iehdr->e_ehsize);
   7097   1.1     skrll       map->includes_phdrs = 0;
   7098   1.1     skrll 
   7099   1.1     skrll       if (!phdr_included || segment->p_type != PT_LOAD)
   7100   1.1     skrll 	{
   7101   1.1     skrll 	  map->includes_phdrs =
   7102   1.1     skrll 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   7103   1.1     skrll 	     && (segment->p_offset + segment->p_filesz
   7104   1.1     skrll 		 >= ((bfd_vma) iehdr->e_phoff
   7105   1.1     skrll 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   7106   1.1     skrll 
   7107   1.1     skrll 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   7108   1.1     skrll 	    phdr_included = TRUE;
   7109   1.1     skrll 	}
   7110   1.1     skrll 
   7111   1.1     skrll       if (section_count == 0)
   7112   1.1     skrll 	{
   7113   1.1     skrll 	  /* Special segments, such as the PT_PHDR segment, may contain
   7114   1.9  christos 	     no sections, but ordinary, loadable segments should contain
   7115   1.9  christos 	     something.  They are allowed by the ELF spec however, so only
   7116   1.9  christos 	     a warning is produced.
   7117   1.9  christos 	     There is however the valid use case of embedded systems which
   7118   1.9  christos 	     have segments with p_filesz of 0 and a p_memsz > 0 to initialize
   7119   1.9  christos 	     flash memory with zeros.  No warning is shown for that case.  */
   7120   1.9  christos 	  if (segment->p_type == PT_LOAD
   7121  1.13  christos 	      && (segment->p_filesz > 0 || segment->p_memsz == 0))
   7122  1.13  christos 	    /* xgettext:c-format */
   7123  1.13  christos 	    _bfd_error_handler
   7124  1.13  christos 	      (_("%pB: warning: empty loadable segment detected"
   7125   1.1     skrll 		 " at vaddr=%#" PRIx64 ", is this intentional?"),
   7126  1.15  christos 	       ibfd, (uint64_t) segment->p_vaddr);
   7127   1.1     skrll 
   7128   1.1     skrll 	  map->p_vaddr_offset = segment->p_vaddr;
   7129   1.1     skrll 	  map->count = 0;
   7130   1.1     skrll 	  *pointer_to_map = map;
   7131   1.1     skrll 	  pointer_to_map = &map->next;
   7132   1.1     skrll 
   7133   1.1     skrll 	  continue;
   7134   1.1     skrll 	}
   7135   1.1     skrll 
   7136   1.1     skrll       /* Now scan the sections in the input BFD again and attempt
   7137   1.1     skrll 	 to add their corresponding output sections to the segment map.
   7138   1.1     skrll 	 The problem here is how to handle an output section which has
   7139   1.1     skrll 	 been moved (ie had its LMA changed).  There are four possibilities:
   7140   1.1     skrll 
   7141   1.1     skrll 	 1. None of the sections have been moved.
   7142   1.1     skrll 	    In this case we can continue to use the segment LMA from the
   7143   1.1     skrll 	    input BFD.
   7144   1.1     skrll 
   7145   1.1     skrll 	 2. All of the sections have been moved by the same amount.
   7146   1.1     skrll 	    In this case we can change the segment's LMA to match the LMA
   7147   1.1     skrll 	    of the first section.
   7148   1.1     skrll 
   7149   1.1     skrll 	 3. Some of the sections have been moved, others have not.
   7150   1.1     skrll 	    In this case those sections which have not been moved can be
   7151   1.1     skrll 	    placed in the current segment which will have to have its size,
   7152   1.1     skrll 	    and possibly its LMA changed, and a new segment or segments will
   7153   1.1     skrll 	    have to be created to contain the other sections.
   7154   1.1     skrll 
   7155   1.1     skrll 	 4. The sections have been moved, but not by the same amount.
   7156   1.1     skrll 	    In this case we can change the segment's LMA to match the LMA
   7157   1.1     skrll 	    of the first section and we will have to create a new segment
   7158   1.1     skrll 	    or segments to contain the other sections.
   7159   1.1     skrll 
   7160   1.1     skrll 	 In order to save time, we allocate an array to hold the section
   7161   1.1     skrll 	 pointers that we are interested in.  As these sections get assigned
   7162   1.3  christos 	 to a segment, they are removed from this array.  */
   7163   1.1     skrll 
   7164   1.1     skrll       sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
   7165   1.1     skrll       if (sections == NULL)
   7166   1.1     skrll 	return FALSE;
   7167   1.1     skrll 
   7168   1.1     skrll       /* Step One: Scan for segment vs section LMA conflicts.
   7169   1.1     skrll 	 Also add the sections to the section array allocated above.
   7170   1.1     skrll 	 Also add the sections to the current segment.  In the common
   7171   1.1     skrll 	 case, where the sections have not been moved, this means that
   7172   1.1     skrll 	 we have completely filled the segment, and there is nothing
   7173  1.13  christos 	 more to do.  */
   7174  1.13  christos       isec = 0;
   7175   1.1     skrll       matching_lma = NULL;
   7176   1.7  christos       suggested_lma = NULL;
   7177   1.1     skrll 
   7178   1.1     skrll       for (section = first_section, j = 0;
   7179   1.1     skrll 	   section != NULL;
   7180   1.1     skrll 	   section = section->next)
   7181   1.1     skrll 	{
   7182   1.1     skrll 	  if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
   7183   1.1     skrll 	    {
   7184   1.1     skrll 	      output_section = section->output_section;
   7185   1.1     skrll 
   7186   1.1     skrll 	      sections[j++] = section;
   7187   1.1     skrll 
   7188   1.1     skrll 	      /* The Solaris native linker always sets p_paddr to 0.
   7189   1.1     skrll 		 We try to catch that case here, and set it to the
   7190   1.1     skrll 		 correct value.  Note - some backends require that
   7191   1.1     skrll 		 p_paddr be left as zero.  */
   7192   1.1     skrll 	      if (!p_paddr_valid
   7193   1.1     skrll 		  && segment->p_vaddr != 0
   7194   1.1     skrll 		  && !bed->want_p_paddr_set_to_zero
   7195  1.13  christos 		  && isec == 0
   7196  1.13  christos 		  && output_section->lma != 0
   7197  1.13  christos 		  && (align_power (segment->p_vaddr
   7198  1.13  christos 				   + (map->includes_filehdr
   7199  1.13  christos 				      ? iehdr->e_ehsize : 0)
   7200  1.13  christos 				   + (map->includes_phdrs
   7201  1.13  christos 				      ? iehdr->e_phnum * iehdr->e_phentsize
   7202  1.13  christos 				      : 0),
   7203   1.1     skrll 				   output_section->alignment_power)
   7204   1.1     skrll 		      == output_section->vma))
   7205   1.1     skrll 		map->p_paddr = segment->p_vaddr;
   7206   1.1     skrll 
   7207   1.1     skrll 	      /* Match up the physical address of the segment with the
   7208   1.1     skrll 		 LMA address of the output section.  */
   7209   1.1     skrll 	      if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
   7210   1.1     skrll 		  || IS_COREFILE_NOTE (segment, section)
   7211   1.1     skrll 		  || (bed->want_p_paddr_set_to_zero
   7212  1.13  christos 		      && IS_CONTAINED_BY_VMA (output_section, segment)))
   7213  1.13  christos 		{
   7214  1.13  christos 		  if (matching_lma == NULL
   7215   1.1     skrll 		      || output_section->lma < matching_lma->lma)
   7216   1.1     skrll 		    matching_lma = output_section;
   7217   1.1     skrll 
   7218   1.1     skrll 		  /* We assume that if the section fits within the segment
   7219   1.1     skrll 		     then it does not overlap any other section within that
   7220   1.1     skrll 		     segment.  */
   7221  1.13  christos 		  map->sections[isec++] = output_section;
   7222  1.13  christos 		}
   7223   1.1     skrll 	      else if (suggested_lma == NULL)
   7224   1.1     skrll 		suggested_lma = output_section;
   7225   1.1     skrll 
   7226   1.1     skrll 	      if (j == section_count)
   7227   1.1     skrll 		break;
   7228   1.1     skrll 	    }
   7229   1.1     skrll 	}
   7230   1.1     skrll 
   7231   1.1     skrll       BFD_ASSERT (j == section_count);
   7232   1.1     skrll 
   7233   1.1     skrll       /* Step Two: Adjust the physical address of the current segment,
   7234   1.1     skrll 	 if necessary.  */
   7235   1.1     skrll       if (isec == section_count)
   7236   1.1     skrll 	{
   7237   1.1     skrll 	  /* All of the sections fitted within the segment as currently
   7238   1.1     skrll 	     specified.  This is the default case.  Add the segment to
   7239   1.1     skrll 	     the list of built segments and carry on to process the next
   7240   1.1     skrll 	     program header in the input BFD.  */
   7241   1.1     skrll 	  map->count = section_count;
   7242   1.1     skrll 	  *pointer_to_map = map;
   7243   1.1     skrll 	  pointer_to_map = &map->next;
   7244  1.15  christos 
   7245  1.15  christos 	  if (p_paddr_valid
   7246  1.15  christos 	      && !bed->want_p_paddr_set_to_zero)
   7247  1.15  christos 	    {
   7248  1.15  christos 	      bfd_vma hdr_size = 0;
   7249  1.15  christos 	      if (map->includes_filehdr)
   7250  1.15  christos 		hdr_size = iehdr->e_ehsize;
   7251  1.15  christos 	      if (map->includes_phdrs)
   7252  1.15  christos 		hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
   7253  1.15  christos 
   7254  1.15  christos 	      /* Account for padding before the first section in the
   7255  1.15  christos 		 segment.  */
   7256   1.1     skrll 	      map->p_vaddr_offset = map->p_paddr + hdr_size - matching_lma->lma;
   7257   1.1     skrll 	    }
   7258   1.1     skrll 
   7259   1.1     skrll 	  free (sections);
   7260   1.1     skrll 	  continue;
   7261   1.1     skrll 	}
   7262  1.13  christos       else
   7263  1.13  christos 	{
   7264  1.13  christos 	  /* Change the current segment's physical address to match
   7265  1.13  christos 	     the LMA of the first section that fitted, or if no
   7266  1.13  christos 	     section fitted, the first section.  */
   7267  1.13  christos 	  if (matching_lma == NULL)
   7268  1.13  christos 	    matching_lma = suggested_lma;
   7269   1.1     skrll 
   7270   1.1     skrll 	  map->p_paddr = matching_lma->lma;
   7271   1.1     skrll 
   7272  1.13  christos 	  /* Offset the segment physical address from the lma
   7273   1.1     skrll 	     to allow for space taken up by elf headers.  */
   7274  1.13  christos 	  if (map->includes_phdrs)
   7275  1.13  christos 	    {
   7276  1.13  christos 	      map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
   7277  1.13  christos 
   7278  1.13  christos 	      /* iehdr->e_phnum is just an estimate of the number
   7279  1.13  christos 		 of program headers that we will need.  Make a note
   7280  1.13  christos 		 here of the number we used and the segment we chose
   7281  1.13  christos 		 to hold these headers, so that we can adjust the
   7282  1.13  christos 		 offset when we know the correct value.  */
   7283   1.1     skrll 	      phdr_adjust_num = iehdr->e_phnum;
   7284   1.1     skrll 	      phdr_adjust_seg = map;
   7285  1.13  christos 	    }
   7286   1.1     skrll 
   7287  1.13  christos 	  if (map->includes_filehdr)
   7288  1.13  christos 	    {
   7289  1.13  christos 	      bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
   7290  1.13  christos 	      map->p_paddr -= iehdr->e_ehsize;
   7291  1.13  christos 	      /* We've subtracted off the size of headers from the
   7292  1.13  christos 		 first section lma, but there may have been some
   7293  1.13  christos 		 alignment padding before that section too.  Try to
   7294  1.13  christos 		 account for that by adjusting the segment lma down to
   7295  1.13  christos 		 the same alignment.  */
   7296  1.13  christos 	      if (segment->p_align != 0 && segment->p_align < align)
   7297   1.1     skrll 		align = segment->p_align;
   7298   1.1     skrll 	      map->p_paddr &= -align;
   7299   1.1     skrll 	    }
   7300   1.1     skrll 	}
   7301   1.1     skrll 
   7302   1.1     skrll       /* Step Three: Loop over the sections again, this time assigning
   7303   1.1     skrll 	 those that fit to the current segment and removing them from the
   7304   1.1     skrll 	 sections array; but making sure not to leave large gaps.  Once all
   7305   1.1     skrll 	 possible sections have been assigned to the current segment it is
   7306   1.1     skrll 	 added to the list of built segments and if sections still remain
   7307   1.1     skrll 	 to be assigned, a new segment is constructed before repeating
   7308   1.1     skrll 	 the loop.  */
   7309   1.1     skrll       isec = 0;
   7310   1.1     skrll       do
   7311  1.13  christos 	{
   7312   1.1     skrll 	  map->count = 0;
   7313   1.1     skrll 	  suggested_lma = NULL;
   7314   1.1     skrll 
   7315   1.1     skrll 	  /* Fill the current segment with sections that fit.  */
   7316   1.1     skrll 	  for (j = 0; j < section_count; j++)
   7317   1.1     skrll 	    {
   7318   1.1     skrll 	      section = sections[j];
   7319   1.1     skrll 
   7320   1.1     skrll 	      if (section == NULL)
   7321   1.1     skrll 		continue;
   7322   1.1     skrll 
   7323   1.1     skrll 	      output_section = section->output_section;
   7324   1.1     skrll 
   7325   1.1     skrll 	      BFD_ASSERT (output_section != NULL);
   7326   1.1     skrll 
   7327   1.1     skrll 	      if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
   7328   1.1     skrll 		  || IS_COREFILE_NOTE (segment, section))
   7329   1.1     skrll 		{
   7330   1.1     skrll 		  if (map->count == 0)
   7331   1.1     skrll 		    {
   7332   1.1     skrll 		      /* If the first section in a segment does not start at
   7333  1.13  christos 			 the beginning of the segment, then something is
   7334  1.13  christos 			 wrong.  */
   7335  1.13  christos 		      if (align_power (map->p_paddr
   7336  1.13  christos 				       + (map->includes_filehdr
   7337  1.13  christos 					  ? iehdr->e_ehsize : 0)
   7338  1.13  christos 				       + (map->includes_phdrs
   7339  1.13  christos 					  ? iehdr->e_phnum * iehdr->e_phentsize
   7340  1.13  christos 					  : 0),
   7341  1.15  christos 				       output_section->alignment_power)
   7342   1.1     skrll 			  != output_section->lma)
   7343   1.1     skrll 			goto sorry;
   7344   1.1     skrll 		    }
   7345   1.1     skrll 		  else
   7346   1.1     skrll 		    {
   7347   1.1     skrll 		      asection *prev_sec;
   7348   1.1     skrll 
   7349   1.1     skrll 		      prev_sec = map->sections[map->count - 1];
   7350   1.1     skrll 
   7351   1.1     skrll 		      /* If the gap between the end of the previous section
   7352   1.1     skrll 			 and the start of this section is more than
   7353   1.1     skrll 			 maxpagesize then we need to start a new segment.  */
   7354   1.1     skrll 		      if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
   7355   1.1     skrll 				      maxpagesize)
   7356   1.1     skrll 			   < BFD_ALIGN (output_section->lma, maxpagesize))
   7357   1.1     skrll 			  || (prev_sec->lma + prev_sec->size
   7358  1.13  christos 			      > output_section->lma))
   7359  1.13  christos 			{
   7360   1.1     skrll 			  if (suggested_lma == NULL)
   7361   1.1     skrll 			    suggested_lma = output_section;
   7362   1.1     skrll 
   7363   1.1     skrll 			  continue;
   7364   1.1     skrll 			}
   7365   1.1     skrll 		    }
   7366   1.1     skrll 
   7367   1.1     skrll 		  map->sections[map->count++] = output_section;
   7368  1.13  christos 		  ++isec;
   7369  1.13  christos 		  sections[j] = NULL;
   7370   1.1     skrll 		  if (segment->p_type == PT_LOAD)
   7371  1.13  christos 		    section->segment_mark = TRUE;
   7372  1.13  christos 		}
   7373   1.1     skrll 	      else if (suggested_lma == NULL)
   7374   1.1     skrll 		suggested_lma = output_section;
   7375  1.15  christos 	    }
   7376  1.15  christos 
   7377  1.15  christos 	  /* PR 23932.  A corrupt input file may contain sections that cannot
   7378  1.15  christos 	     be assigned to any segment - because for example they have a
   7379  1.15  christos 	     negative size - or segments that do not contain any sections.  */
   7380  1.15  christos 	  if (map->count == 0)
   7381  1.15  christos 	    {
   7382  1.15  christos 	    sorry:
   7383  1.15  christos 	      bfd_set_error (bfd_error_sorry);
   7384  1.15  christos 	      free (sections);
   7385   1.1     skrll 	      return FALSE;
   7386   1.1     skrll 	    }
   7387   1.1     skrll 
   7388   1.1     skrll 	  /* Add the current segment to the list of built segments.  */
   7389   1.1     skrll 	  *pointer_to_map = map;
   7390   1.1     skrll 	  pointer_to_map = &map->next;
   7391   1.1     skrll 
   7392   1.1     skrll 	  if (isec < section_count)
   7393   1.1     skrll 	    {
   7394   1.1     skrll 	      /* We still have not allocated all of the sections to
   7395  1.15  christos 		 segments.  Create a new segment here, initialise it
   7396  1.15  christos 		 and carry on looping.  */
   7397   1.5  christos 	      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   7398   1.1     skrll 	      amt += (bfd_size_type) section_count * sizeof (asection *);
   7399   1.1     skrll 	      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7400   1.1     skrll 	      if (map == NULL)
   7401   1.1     skrll 		{
   7402   1.1     skrll 		  free (sections);
   7403   1.1     skrll 		  return FALSE;
   7404   1.1     skrll 		}
   7405   1.1     skrll 
   7406   1.1     skrll 	      /* Initialise the fields of the segment map.  Set the physical
   7407   1.1     skrll 		 physical address to the LMA of the first section that has
   7408   1.1     skrll 		 not yet been assigned.  */
   7409   1.1     skrll 	      map->next = NULL;
   7410   1.1     skrll 	      map->p_type = segment->p_type;
   7411  1.13  christos 	      map->p_flags = segment->p_flags;
   7412   1.1     skrll 	      map->p_flags_valid = 1;
   7413   1.1     skrll 	      map->p_paddr = suggested_lma->lma;
   7414   1.1     skrll 	      map->p_paddr_valid = p_paddr_valid;
   7415   1.1     skrll 	      map->includes_filehdr = 0;
   7416   1.1     skrll 	      map->includes_phdrs = 0;
   7417   1.1     skrll 	    }
   7418   1.1     skrll 	}
   7419   1.1     skrll       while (isec < section_count);
   7420   1.1     skrll 
   7421   1.1     skrll       free (sections);
   7422   1.6  christos     }
   7423   1.1     skrll 
   7424   1.1     skrll   elf_seg_map (obfd) = map_first;
   7425   1.1     skrll 
   7426   1.1     skrll   /* If we had to estimate the number of program headers that were
   7427   1.1     skrll      going to be needed, then check our estimate now and adjust
   7428   1.1     skrll      the offset if necessary.  */
   7429   1.1     skrll   if (phdr_adjust_seg != NULL)
   7430   1.1     skrll     {
   7431   1.1     skrll       unsigned int count;
   7432   1.1     skrll 
   7433   1.1     skrll       for (count = 0, map = map_first; map != NULL; map = map->next)
   7434   1.1     skrll 	count++;
   7435   1.1     skrll 
   7436   1.1     skrll       if (count > phdr_adjust_num)
   7437  1.13  christos 	phdr_adjust_seg->p_paddr
   7438  1.13  christos 	  -= (count - phdr_adjust_num) * iehdr->e_phentsize;
   7439  1.13  christos 
   7440  1.13  christos       for (map = map_first; map != NULL; map = map->next)
   7441  1.13  christos 	if (map->p_type == PT_PHDR)
   7442  1.13  christos 	  {
   7443  1.13  christos 	    bfd_vma adjust
   7444  1.13  christos 	      = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
   7445  1.13  christos 	    map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
   7446   1.1     skrll 	    break;
   7447   1.1     skrll 	  }
   7448   1.1     skrll     }
   7449   1.1     skrll 
   7450   1.1     skrll #undef SEGMENT_END
   7451   1.1     skrll #undef SECTION_SIZE
   7452   1.1     skrll #undef IS_CONTAINED_BY_VMA
   7453   1.1     skrll #undef IS_CONTAINED_BY_LMA
   7454   1.1     skrll #undef IS_NOTE
   7455   1.1     skrll #undef IS_COREFILE_NOTE
   7456   1.1     skrll #undef IS_SOLARIS_PT_INTERP
   7457   1.1     skrll #undef IS_SECTION_IN_INPUT_SEGMENT
   7458   1.1     skrll #undef INCLUDE_SECTION_IN_SEGMENT
   7459   1.1     skrll #undef SEGMENT_AFTER_SEGMENT
   7460   1.1     skrll #undef SEGMENT_OVERLAPS
   7461   1.1     skrll   return TRUE;
   7462   1.1     skrll }
   7463   1.1     skrll 
   7464   1.1     skrll /* Copy ELF program header information.  */
   7465   1.1     skrll 
   7466   1.1     skrll static bfd_boolean
   7467   1.1     skrll copy_elf_program_header (bfd *ibfd, bfd *obfd)
   7468   1.1     skrll {
   7469   1.1     skrll   Elf_Internal_Ehdr *iehdr;
   7470   1.1     skrll   struct elf_segment_map *map;
   7471   1.1     skrll   struct elf_segment_map *map_first;
   7472   1.1     skrll   struct elf_segment_map **pointer_to_map;
   7473   1.1     skrll   Elf_Internal_Phdr *segment;
   7474   1.1     skrll   unsigned int i;
   7475   1.1     skrll   unsigned int num_segments;
   7476   1.1     skrll   bfd_boolean phdr_included = FALSE;
   7477   1.1     skrll   bfd_boolean p_paddr_valid;
   7478   1.1     skrll 
   7479   1.1     skrll   iehdr = elf_elfheader (ibfd);
   7480   1.1     skrll 
   7481   1.1     skrll   map_first = NULL;
   7482   1.1     skrll   pointer_to_map = &map_first;
   7483   1.1     skrll 
   7484   1.1     skrll   /* If all the segment p_paddr fields are zero, don't set
   7485   1.1     skrll      map->p_paddr_valid.  */
   7486   1.1     skrll   p_paddr_valid = FALSE;
   7487   1.1     skrll   num_segments = elf_elfheader (ibfd)->e_phnum;
   7488   1.1     skrll   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7489   1.1     skrll        i < num_segments;
   7490   1.1     skrll        i++, segment++)
   7491   1.1     skrll     if (segment->p_paddr != 0)
   7492   1.1     skrll       {
   7493   1.1     skrll 	p_paddr_valid = TRUE;
   7494   1.1     skrll 	break;
   7495   1.1     skrll       }
   7496   1.1     skrll 
   7497   1.1     skrll   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7498   1.1     skrll        i < num_segments;
   7499   1.1     skrll        i++, segment++)
   7500   1.1     skrll     {
   7501   1.1     skrll       asection *section;
   7502   1.1     skrll       unsigned int section_count;
   7503   1.1     skrll       bfd_size_type amt;
   7504   1.3  christos       Elf_Internal_Shdr *this_hdr;
   7505   1.1     skrll       asection *first_section = NULL;
   7506   1.1     skrll       asection *lowest_section;
   7507   1.1     skrll 
   7508   1.1     skrll       /* Compute how many sections are in this segment.  */
   7509   1.1     skrll       for (section = ibfd->sections, section_count = 0;
   7510   1.1     skrll 	   section != NULL;
   7511   1.1     skrll 	   section = section->next)
   7512   1.3  christos 	{
   7513   1.1     skrll 	  this_hdr = &(elf_section_data(section)->this_hdr);
   7514   1.3  christos 	  if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   7515   1.3  christos 	    {
   7516   1.1     skrll 	      if (first_section == NULL)
   7517   1.1     skrll 		first_section = section;
   7518   1.1     skrll 	      section_count++;
   7519   1.1     skrll 	    }
   7520   1.1     skrll 	}
   7521   1.1     skrll 
   7522  1.15  christos       /* Allocate a segment map big enough to contain
   7523  1.15  christos 	 all of the sections we have selected.  */
   7524   1.3  christos       amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   7525   1.1     skrll       amt += (bfd_size_type) section_count * sizeof (asection *);
   7526   1.1     skrll       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7527   1.1     skrll       if (map == NULL)
   7528   1.1     skrll 	return FALSE;
   7529   1.1     skrll 
   7530   1.1     skrll       /* Initialize the fields of the output segment map with the
   7531   1.1     skrll 	 input segment.  */
   7532   1.1     skrll       map->next = NULL;
   7533   1.1     skrll       map->p_type = segment->p_type;
   7534   1.1     skrll       map->p_flags = segment->p_flags;
   7535   1.1     skrll       map->p_flags_valid = 1;
   7536   1.1     skrll       map->p_paddr = segment->p_paddr;
   7537   1.1     skrll       map->p_paddr_valid = p_paddr_valid;
   7538   1.1     skrll       map->p_align = segment->p_align;
   7539   1.1     skrll       map->p_align_valid = 1;
   7540   1.6  christos       map->p_vaddr_offset = 0;
   7541   1.6  christos 
   7542   1.1     skrll       if (map->p_type == PT_GNU_RELRO
   7543   1.1     skrll 	  || map->p_type == PT_GNU_STACK)
   7544   1.1     skrll 	{
   7545   1.1     skrll 	  /* The PT_GNU_RELRO segment may contain the first a few
   7546   1.6  christos 	     bytes in the .got.plt section even if the whole .got.plt
   7547   1.6  christos 	     section isn't in the PT_GNU_RELRO segment.  We won't
   7548   1.6  christos 	     change the size of the PT_GNU_RELRO segment.
   7549   1.3  christos 	     Similarly, PT_GNU_STACK size is significant on uclinux
   7550   1.1     skrll 	     systems.    */
   7551   1.1     skrll 	  map->p_size = segment->p_memsz;
   7552   1.1     skrll 	  map->p_size_valid = 1;
   7553   1.1     skrll 	}
   7554   1.1     skrll 
   7555   1.1     skrll       /* Determine if this segment contains the ELF file header
   7556   1.1     skrll 	 and if it contains the program headers themselves.  */
   7557   1.1     skrll       map->includes_filehdr = (segment->p_offset == 0
   7558   1.1     skrll 			       && segment->p_filesz >= iehdr->e_ehsize);
   7559   1.1     skrll 
   7560   1.1     skrll       map->includes_phdrs = 0;
   7561   1.1     skrll       if (! phdr_included || segment->p_type != PT_LOAD)
   7562   1.1     skrll 	{
   7563   1.1     skrll 	  map->includes_phdrs =
   7564   1.1     skrll 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   7565   1.1     skrll 	     && (segment->p_offset + segment->p_filesz
   7566   1.1     skrll 		 >= ((bfd_vma) iehdr->e_phoff
   7567   1.1     skrll 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   7568   1.1     skrll 
   7569   1.1     skrll 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   7570   1.1     skrll 	    phdr_included = TRUE;
   7571   1.6  christos 	}
   7572   1.1     skrll 
   7573   1.1     skrll       lowest_section = NULL;
   7574   1.1     skrll       if (section_count != 0)
   7575   1.1     skrll 	{
   7576   1.1     skrll 	  unsigned int isec = 0;
   7577   1.1     skrll 
   7578   1.1     skrll 	  for (section = first_section;
   7579   1.1     skrll 	       section != NULL;
   7580   1.1     skrll 	       section = section->next)
   7581   1.3  christos 	    {
   7582   1.1     skrll 	      this_hdr = &(elf_section_data(section)->this_hdr);
   7583   1.1     skrll 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   7584   1.3  christos 		{
   7585   1.3  christos 		  map->sections[isec++] = section->output_section;
   7586   1.3  christos 		  if ((section->flags & SEC_ALLOC) != 0)
   7587   1.3  christos 		    {
   7588   1.6  christos 		      bfd_vma seg_off;
   7589   1.6  christos 
   7590   1.6  christos 		      if (lowest_section == NULL
   7591   1.6  christos 			  || section->lma < lowest_section->lma)
   7592   1.3  christos 			lowest_section = section;
   7593   1.3  christos 
   7594   1.3  christos 		      /* Section lmas are set up from PT_LOAD header
   7595   1.3  christos 			 p_paddr in _bfd_elf_make_section_from_shdr.
   7596   1.3  christos 			 If this header has a p_paddr that disagrees
   7597   1.3  christos 			 with the section lma, flag the p_paddr as
   7598   1.3  christos 			 invalid.  */
   7599   1.3  christos 		      if ((section->flags & SEC_LOAD) != 0)
   7600   1.3  christos 			seg_off = this_hdr->sh_offset - segment->p_offset;
   7601   1.3  christos 		      else
   7602   1.3  christos 			seg_off = this_hdr->sh_addr - segment->p_vaddr;
   7603   1.3  christos 		      if (section->lma - segment->p_paddr != seg_off)
   7604   1.1     skrll 			map->p_paddr_valid = FALSE;
   7605   1.1     skrll 		    }
   7606   1.1     skrll 		  if (isec == section_count)
   7607   1.1     skrll 		    break;
   7608   1.1     skrll 		}
   7609   1.1     skrll 	    }
   7610  1.15  christos 	}
   7611  1.15  christos 
   7612  1.15  christos       if (section_count == 0)
   7613  1.15  christos 	map->p_vaddr_offset = segment->p_vaddr;
   7614  1.15  christos       else if (map->p_paddr_valid)
   7615  1.15  christos 	{
   7616  1.15  christos 	  /* Account for padding before the first section in the segment.  */
   7617  1.15  christos 	  bfd_vma hdr_size = 0;
   7618  1.15  christos 	  if (map->includes_filehdr)
   7619  1.15  christos 	    hdr_size = iehdr->e_ehsize;
   7620  1.15  christos 	  if (map->includes_phdrs)
   7621  1.15  christos 	    hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
   7622  1.15  christos 
   7623  1.15  christos 	  map->p_vaddr_offset = (map->p_paddr + hdr_size
   7624   1.3  christos 				 - (lowest_section ? lowest_section->lma : 0));
   7625   1.1     skrll 	}
   7626   1.1     skrll 
   7627   1.1     skrll       map->count = section_count;
   7628   1.1     skrll       *pointer_to_map = map;
   7629   1.1     skrll       pointer_to_map = &map->next;
   7630   1.6  christos     }
   7631   1.1     skrll 
   7632   1.1     skrll   elf_seg_map (obfd) = map_first;
   7633   1.1     skrll   return TRUE;
   7634   1.1     skrll }
   7635   1.1     skrll 
   7636   1.1     skrll /* Copy private BFD data.  This copies or rewrites ELF program header
   7637   1.1     skrll    information.  */
   7638   1.1     skrll 
   7639   1.1     skrll static bfd_boolean
   7640   1.1     skrll copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   7641   1.1     skrll {
   7642   1.1     skrll   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   7643   1.1     skrll       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   7644   1.1     skrll     return TRUE;
   7645   1.1     skrll 
   7646   1.1     skrll   if (elf_tdata (ibfd)->phdr == NULL)
   7647   1.1     skrll     return TRUE;
   7648   1.1     skrll 
   7649   1.1     skrll   if (ibfd->xvec == obfd->xvec)
   7650   1.1     skrll     {
   7651   1.1     skrll       /* Check to see if any sections in the input BFD
   7652   1.1     skrll 	 covered by ELF program header have changed.  */
   7653   1.1     skrll       Elf_Internal_Phdr *segment;
   7654   1.1     skrll       asection *section, *osec;
   7655   1.1     skrll       unsigned int i, num_segments;
   7656   1.1     skrll       Elf_Internal_Shdr *this_hdr;
   7657   1.1     skrll       const struct elf_backend_data *bed;
   7658   1.1     skrll 
   7659   1.1     skrll       bed = get_elf_backend_data (ibfd);
   7660   1.1     skrll 
   7661   1.1     skrll       /* Regenerate the segment map if p_paddr is set to 0.  */
   7662   1.1     skrll       if (bed->want_p_paddr_set_to_zero)
   7663   1.1     skrll 	goto rewrite;
   7664   1.1     skrll 
   7665   1.1     skrll       /* Initialize the segment mark field.  */
   7666   1.1     skrll       for (section = obfd->sections; section != NULL;
   7667   1.1     skrll 	   section = section->next)
   7668   1.1     skrll 	section->segment_mark = FALSE;
   7669   1.1     skrll 
   7670   1.1     skrll       num_segments = elf_elfheader (ibfd)->e_phnum;
   7671   1.1     skrll       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7672   1.1     skrll 	   i < num_segments;
   7673   1.1     skrll 	   i++, segment++)
   7674   1.1     skrll 	{
   7675   1.1     skrll 	  /* PR binutils/3535.  The Solaris linker always sets the p_paddr
   7676   1.1     skrll 	     and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
   7677   1.1     skrll 	     which severly confuses things, so always regenerate the segment
   7678   1.1     skrll 	     map in this case.  */
   7679   1.1     skrll 	  if (segment->p_paddr == 0
   7680   1.1     skrll 	      && segment->p_memsz == 0
   7681   1.1     skrll 	      && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
   7682   1.1     skrll 	    goto rewrite;
   7683   1.1     skrll 
   7684   1.1     skrll 	  for (section = ibfd->sections;
   7685   1.1     skrll 	       section != NULL; section = section->next)
   7686   1.1     skrll 	    {
   7687   1.1     skrll 	      /* We mark the output section so that we know it comes
   7688   1.1     skrll 		 from the input BFD.  */
   7689   1.1     skrll 	      osec = section->output_section;
   7690   1.1     skrll 	      if (osec)
   7691   1.1     skrll 		osec->segment_mark = TRUE;
   7692   1.1     skrll 
   7693   1.3  christos 	      /* Check if this section is covered by the segment.  */
   7694   1.1     skrll 	      this_hdr = &(elf_section_data(section)->this_hdr);
   7695   1.1     skrll 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   7696   1.1     skrll 		{
   7697   1.1     skrll 		  /* FIXME: Check if its output section is changed or
   7698   1.1     skrll 		     removed.  What else do we need to check?  */
   7699   1.1     skrll 		  if (osec == NULL
   7700   1.1     skrll 		      || section->flags != osec->flags
   7701   1.1     skrll 		      || section->lma != osec->lma
   7702   1.1     skrll 		      || section->vma != osec->vma
   7703   1.1     skrll 		      || section->size != osec->size
   7704   1.1     skrll 		      || section->rawsize != osec->rawsize
   7705   1.1     skrll 		      || section->alignment_power != osec->alignment_power)
   7706   1.1     skrll 		    goto rewrite;
   7707   1.1     skrll 		}
   7708   1.1     skrll 	    }
   7709   1.1     skrll 	}
   7710   1.1     skrll 
   7711   1.1     skrll       /* Check to see if any output section do not come from the
   7712   1.1     skrll 	 input BFD.  */
   7713   1.1     skrll       for (section = obfd->sections; section != NULL;
   7714   1.9  christos 	   section = section->next)
   7715   1.1     skrll 	{
   7716   1.1     skrll 	  if (!section->segment_mark)
   7717   1.1     skrll 	    goto rewrite;
   7718   1.1     skrll 	  else
   7719   1.1     skrll 	    section->segment_mark = FALSE;
   7720   1.1     skrll 	}
   7721   1.1     skrll 
   7722   1.1     skrll       return copy_elf_program_header (ibfd, obfd);
   7723   1.1     skrll     }
   7724   1.5  christos 
   7725   1.5  christos rewrite:
   7726   1.5  christos   if (ibfd->xvec == obfd->xvec)
   7727   1.5  christos     {
   7728   1.5  christos       /* When rewriting program header, set the output maxpagesize to
   7729   1.5  christos 	 the maximum alignment of input PT_LOAD segments.  */
   7730   1.5  christos       Elf_Internal_Phdr *segment;
   7731   1.5  christos       unsigned int i;
   7732   1.5  christos       unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
   7733   1.5  christos       bfd_vma maxpagesize = 0;
   7734   1.5  christos 
   7735   1.5  christos       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7736   1.5  christos 	   i < num_segments;
   7737   1.5  christos 	   i++, segment++)
   7738   1.6  christos 	if (segment->p_type == PT_LOAD
   7739   1.6  christos 	    && maxpagesize < segment->p_align)
   7740   1.6  christos 	  {
   7741   1.9  christos 	    /* PR 17512: file: f17299af.  */
   7742  1.13  christos 	    if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
   7743  1.13  christos 	      /* xgettext:c-format */
   7744  1.13  christos 	      _bfd_error_handler (_("%pB: warning: segment alignment of %#"
   7745   1.6  christos 				    PRIx64 " is too large"),
   7746   1.6  christos 				  ibfd, (uint64_t) segment->p_align);
   7747   1.6  christos 	    else
   7748   1.5  christos 	      maxpagesize = segment->p_align;
   7749   1.5  christos 	  }
   7750   1.5  christos 
   7751   1.5  christos       if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
   7752   1.5  christos 	bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
   7753   1.1     skrll     }
   7754   1.1     skrll 
   7755   1.1     skrll   return rewrite_elf_program_header (ibfd, obfd);
   7756   1.1     skrll }
   7757   1.1     skrll 
   7758   1.1     skrll /* Initialize private output section information from input section.  */
   7759   1.1     skrll 
   7760   1.1     skrll bfd_boolean
   7761   1.1     skrll _bfd_elf_init_private_section_data (bfd *ibfd,
   7762   1.1     skrll 				    asection *isec,
   7763   1.1     skrll 				    bfd *obfd,
   7764   1.1     skrll 				    asection *osec,
   7765   1.1     skrll 				    struct bfd_link_info *link_info)
   7766   1.1     skrll 
   7767   1.6  christos {
   7768   1.6  christos   Elf_Internal_Shdr *ihdr, *ohdr;
   7769   1.1     skrll   bfd_boolean final_link = (link_info != NULL
   7770   1.1     skrll 			    && !bfd_link_relocatable (link_info));
   7771   1.1     skrll 
   7772   1.1     skrll   if (ibfd->xvec->flavour != bfd_target_elf_flavour
   7773   1.1     skrll       || obfd->xvec->flavour != bfd_target_elf_flavour)
   7774   1.5  christos     return TRUE;
   7775   1.5  christos 
   7776   1.3  christos   BFD_ASSERT (elf_section_data (osec) != NULL);
   7777   1.3  christos 
   7778   1.3  christos   /* For objcopy and relocatable link, don't copy the output ELF
   7779   1.3  christos      section type from input if the output BFD section flags have been
   7780   1.1     skrll      set to something different.  For a final link allow some flags
   7781   1.3  christos      that the linker clears to differ.  */
   7782   1.3  christos   if (elf_section_type (osec) == SHT_NULL
   7783   1.3  christos       && (osec->flags == isec->flags
   7784   1.3  christos 	  || (final_link
   7785   1.1     skrll 	      && ((osec->flags ^ isec->flags)
   7786   1.1     skrll 		  & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
   7787   1.1     skrll     elf_section_type (osec) = elf_section_type (isec);
   7788   1.1     skrll 
   7789   1.1     skrll   /* FIXME: Is this correct for all OS/PROC specific flags?  */
   7790   1.1     skrll   elf_section_flags (osec) |= (elf_section_flags (isec)
   7791   1.9  christos 			       & (SHF_MASKOS | SHF_MASKPROC));
   7792  1.15  christos 
   7793  1.15  christos   /* Copy sh_info from input for mbind section.  */
   7794   1.9  christos   if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
   7795   1.9  christos       && elf_section_flags (isec) & SHF_GNU_MBIND)
   7796   1.9  christos     elf_section_data (osec)->this_hdr.sh_info
   7797   1.1     skrll       = elf_section_data (isec)->this_hdr.sh_info;
   7798   1.1     skrll 
   7799   1.1     skrll   /* Set things up for objcopy and relocatable link.  The output
   7800   1.1     skrll      SHT_GROUP section will have its elf_next_in_group pointing back
   7801   1.9  christos      to the input group members.  Ignore linker created group section.
   7802   1.9  christos      See elfNN_ia64_object_p in elfxx-ia64.c.  */
   7803   1.9  christos   if ((link_info == NULL
   7804   1.9  christos        || !link_info->resolve_section_groups)
   7805   1.9  christos       && (elf_sec_group (isec) == NULL
   7806   1.9  christos 	  || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
   7807   1.9  christos     {
   7808   1.9  christos       if (elf_section_flags (isec) & SHF_GROUP)
   7809   1.9  christos 	elf_section_flags (osec) |= SHF_GROUP;
   7810   1.9  christos       elf_next_in_group (osec) = elf_next_in_group (isec);
   7811   1.6  christos       elf_section_data (osec)->group = elf_section_data (isec)->group;
   7812   1.9  christos     }
   7813   1.9  christos 
   7814   1.9  christos   /* If not decompress, preserve SHF_COMPRESSED.  */
   7815   1.9  christos   if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
   7816   1.1     skrll     elf_section_flags (osec) |= (elf_section_flags (isec)
   7817   1.1     skrll 				 & SHF_COMPRESSED);
   7818   1.1     skrll 
   7819   1.1     skrll   ihdr = &elf_section_data (isec)->this_hdr;
   7820   1.1     skrll 
   7821   1.1     skrll   /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
   7822   1.1     skrll      don't use the output section of the linked-to section since it
   7823   1.1     skrll      may be NULL at this point.  */
   7824   1.1     skrll   if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
   7825   1.1     skrll     {
   7826   1.1     skrll       ohdr = &elf_section_data (osec)->this_hdr;
   7827   1.1     skrll       ohdr->sh_flags |= SHF_LINK_ORDER;
   7828   1.1     skrll       elf_linked_to_section (osec) = elf_linked_to_section (isec);
   7829   1.1     skrll     }
   7830   1.1     skrll 
   7831   1.1     skrll   osec->use_rela_p = isec->use_rela_p;
   7832   1.1     skrll 
   7833   1.1     skrll   return TRUE;
   7834   1.1     skrll }
   7835   1.1     skrll 
   7836   1.1     skrll /* Copy private section information.  This copies over the entsize
   7837   1.1     skrll    field, and sometimes the info field.  */
   7838   1.1     skrll 
   7839   1.1     skrll bfd_boolean
   7840   1.1     skrll _bfd_elf_copy_private_section_data (bfd *ibfd,
   7841   1.1     skrll 				    asection *isec,
   7842   1.1     skrll 				    bfd *obfd,
   7843   1.1     skrll 				    asection *osec)
   7844   1.1     skrll {
   7845   1.1     skrll   Elf_Internal_Shdr *ihdr, *ohdr;
   7846   1.1     skrll 
   7847   1.1     skrll   if (ibfd->xvec->flavour != bfd_target_elf_flavour
   7848   1.1     skrll       || obfd->xvec->flavour != bfd_target_elf_flavour)
   7849   1.1     skrll     return TRUE;
   7850   1.1     skrll 
   7851   1.1     skrll   ihdr = &elf_section_data (isec)->this_hdr;
   7852   1.1     skrll   ohdr = &elf_section_data (osec)->this_hdr;
   7853   1.1     skrll 
   7854   1.1     skrll   ohdr->sh_entsize = ihdr->sh_entsize;
   7855   1.1     skrll 
   7856   1.1     skrll   if (ihdr->sh_type == SHT_SYMTAB
   7857   1.1     skrll       || ihdr->sh_type == SHT_DYNSYM
   7858   1.1     skrll       || ihdr->sh_type == SHT_GNU_verneed
   7859   1.1     skrll       || ihdr->sh_type == SHT_GNU_verdef)
   7860   1.1     skrll     ohdr->sh_info = ihdr->sh_info;
   7861   1.1     skrll 
   7862   1.1     skrll   return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
   7863   1.1     skrll 					     NULL);
   7864   1.3  christos }
   7865   1.3  christos 
   7866   1.3  christos /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
   7867   1.3  christos    necessary if we are removing either the SHT_GROUP section or any of
   7868   1.3  christos    the group member sections.  DISCARDED is the value that a section's
   7869   1.3  christos    output_section has if the section will be discarded, NULL when this
   7870   1.1     skrll    function is called from objcopy, bfd_abs_section_ptr when called
   7871   1.1     skrll    from the linker.  */
   7872   1.3  christos 
   7873   1.1     skrll bfd_boolean
   7874   1.1     skrll _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
   7875   1.1     skrll {
   7876   1.1     skrll   asection *isec;
   7877   1.3  christos 
   7878   1.1     skrll   for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   7879   1.1     skrll     if (elf_section_type (isec) == SHT_GROUP)
   7880   1.1     skrll       {
   7881   1.3  christos 	asection *first = elf_next_in_group (isec);
   7882   1.3  christos 	asection *s = first;
   7883   1.1     skrll 	bfd_size_type removed = 0;
   7884   1.1     skrll 
   7885   1.3  christos 	while (s != NULL)
   7886   1.3  christos 	  {
   7887   1.3  christos 	    /* If this member section is being output but the
   7888   1.3  christos 	       SHT_GROUP section is not, then clear the group info
   7889   1.3  christos 	       set up by _bfd_elf_copy_private_section_data.  */
   7890   1.1     skrll 	    if (s->output_section != discarded
   7891   1.1     skrll 		&& isec->output_section == discarded)
   7892   1.1     skrll 	      {
   7893   1.1     skrll 		elf_section_flags (s->output_section) &= ~SHF_GROUP;
   7894   1.3  christos 		elf_group_name (s->output_section) = NULL;
   7895   1.3  christos 	      }
   7896   1.3  christos 	    /* Conversely, if the member section is not being output
   7897   1.3  christos 	       but the SHT_GROUP section is, then adjust its size.  */
   7898  1.13  christos 	    else if (s->output_section == discarded
   7899  1.13  christos 		     && isec->output_section != discarded)
   7900  1.13  christos 	      {
   7901  1.13  christos 		struct bfd_elf_section_data *elf_sec = elf_section_data (s);
   7902  1.13  christos 		removed += 4;
   7903  1.13  christos 		if (elf_sec->rel.hdr != NULL
   7904  1.13  christos 		    && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
   7905  1.13  christos 		  removed += 4;
   7906  1.13  christos 		if (elf_sec->rela.hdr != NULL
   7907  1.13  christos 		    && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
   7908   1.1     skrll 		  removed += 4;
   7909   1.1     skrll 	      }
   7910   1.1     skrll 	    s = elf_next_in_group (s);
   7911   1.1     skrll 	    if (s == first)
   7912   1.3  christos 	      break;
   7913   1.3  christos 	  }
   7914   1.3  christos 	if (removed != 0)
   7915   1.3  christos 	  {
   7916   1.3  christos 	    if (discarded != NULL)
   7917  1.13  christos 	      {
   7918   1.3  christos 		/* If we've been called for ld -r, then we need to
   7919   1.3  christos 		   adjust the input section size.  */
   7920   1.3  christos 		if (isec->rawsize == 0)
   7921  1.13  christos 		  isec->rawsize = isec->size;
   7922  1.13  christos 		isec->size = isec->rawsize - removed;
   7923  1.13  christos 		if (isec->size <= 4)
   7924  1.13  christos 		  {
   7925  1.13  christos 		    isec->size = 0;
   7926   1.3  christos 		    isec->flags |= SEC_EXCLUDE;
   7927   1.3  christos 		  }
   7928   1.3  christos 	      }
   7929   1.3  christos 	    else
   7930   1.3  christos 	      {
   7931   1.3  christos 		/* Adjust the output section size when called from
   7932  1.13  christos 		   objcopy. */
   7933  1.13  christos 		isec->output_section->size -= removed;
   7934  1.13  christos 		if (isec->output_section->size <= 4)
   7935  1.13  christos 		  {
   7936  1.13  christos 		    isec->output_section->size = 0;
   7937   1.3  christos 		    isec->output_section->flags |= SEC_EXCLUDE;
   7938   1.3  christos 		  }
   7939   1.1     skrll 	      }
   7940   1.1     skrll 	  }
   7941   1.1     skrll       }
   7942   1.1     skrll 
   7943   1.1     skrll   return TRUE;
   7944   1.3  christos }
   7945   1.3  christos 
   7946   1.3  christos /* Copy private header information.  */
   7947   1.3  christos 
   7948   1.3  christos bfd_boolean
   7949   1.3  christos _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
   7950   1.3  christos {
   7951   1.3  christos   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   7952   1.3  christos       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   7953   1.3  christos     return TRUE;
   7954   1.3  christos 
   7955   1.3  christos   /* Copy over private BFD data if it has not already been copied.
   7956   1.3  christos      This must be done here, rather than in the copy_private_bfd_data
   7957   1.3  christos      entry point, because the latter is called after the section
   7958   1.6  christos      contents have been set, which means that the program headers have
   7959   1.3  christos      already been worked out.  */
   7960   1.3  christos   if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
   7961   1.3  christos     {
   7962   1.3  christos       if (! copy_private_bfd_data (ibfd, obfd))
   7963   1.3  christos 	return FALSE;
   7964   1.3  christos     }
   7965   1.3  christos 
   7966   1.3  christos   return _bfd_elf_fixup_group_sections (ibfd, NULL);
   7967   1.1     skrll }
   7968   1.1     skrll 
   7969   1.1     skrll /* Copy private symbol information.  If this symbol is in a section
   7970   1.1     skrll    which we did not map into a BFD section, try to map the section
   7971   1.1     skrll    index correctly.  We use special macro definitions for the mapped
   7972   1.1     skrll    section indices; these definitions are interpreted by the
   7973   1.1     skrll    swap_out_syms function.  */
   7974   1.1     skrll 
   7975   1.1     skrll #define MAP_ONESYMTAB (SHN_HIOS + 1)
   7976   1.1     skrll #define MAP_DYNSYMTAB (SHN_HIOS + 2)
   7977   1.1     skrll #define MAP_STRTAB    (SHN_HIOS + 3)
   7978   1.1     skrll #define MAP_SHSTRTAB  (SHN_HIOS + 4)
   7979   1.1     skrll #define MAP_SYM_SHNDX (SHN_HIOS + 5)
   7980   1.1     skrll 
   7981   1.1     skrll bfd_boolean
   7982   1.1     skrll _bfd_elf_copy_private_symbol_data (bfd *ibfd,
   7983   1.1     skrll 				   asymbol *isymarg,
   7984   1.1     skrll 				   bfd *obfd,
   7985   1.1     skrll 				   asymbol *osymarg)
   7986   1.1     skrll {
   7987   1.1     skrll   elf_symbol_type *isym, *osym;
   7988   1.1     skrll 
   7989   1.1     skrll   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   7990   1.1     skrll       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   7991   1.1     skrll     return TRUE;
   7992   1.1     skrll 
   7993   1.1     skrll   isym = elf_symbol_from (ibfd, isymarg);
   7994   1.1     skrll   osym = elf_symbol_from (obfd, osymarg);
   7995   1.1     skrll 
   7996   1.1     skrll   if (isym != NULL
   7997   1.1     skrll       && isym->internal_elf_sym.st_shndx != 0
   7998   1.1     skrll       && osym != NULL
   7999   1.1     skrll       && bfd_is_abs_section (isym->symbol.section))
   8000   1.1     skrll     {
   8001   1.1     skrll       unsigned int shndx;
   8002   1.1     skrll 
   8003   1.1     skrll       shndx = isym->internal_elf_sym.st_shndx;
   8004   1.1     skrll       if (shndx == elf_onesymtab (ibfd))
   8005   1.1     skrll 	shndx = MAP_ONESYMTAB;
   8006   1.6  christos       else if (shndx == elf_dynsymtab (ibfd))
   8007   1.1     skrll 	shndx = MAP_DYNSYMTAB;
   8008   1.6  christos       else if (shndx == elf_strtab_sec (ibfd))
   8009   1.1     skrll 	shndx = MAP_STRTAB;
   8010   1.6  christos       else if (shndx == elf_shstrtab_sec (ibfd))
   8011   1.1     skrll 	shndx = MAP_SHSTRTAB;
   8012   1.1     skrll       else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
   8013   1.1     skrll 	shndx = MAP_SYM_SHNDX;
   8014   1.1     skrll       osym->internal_elf_sym.st_shndx = shndx;
   8015   1.1     skrll     }
   8016   1.1     skrll 
   8017   1.1     skrll   return TRUE;
   8018   1.1     skrll }
   8019   1.1     skrll 
   8020   1.1     skrll /* Swap out the symbols.  */
   8021   1.1     skrll 
   8022   1.6  christos static bfd_boolean
   8023   1.1     skrll swap_out_syms (bfd *abfd,
   8024   1.1     skrll 	       struct elf_strtab_hash **sttp,
   8025   1.1     skrll 	       int relocatable_p)
   8026   1.1     skrll {
   8027   1.1     skrll   const struct elf_backend_data *bed;
   8028   1.6  christos   int symcount;
   8029   1.1     skrll   asymbol **syms;
   8030   1.1     skrll   struct elf_strtab_hash *stt;
   8031   1.1     skrll   Elf_Internal_Shdr *symtab_hdr;
   8032   1.6  christos   Elf_Internal_Shdr *symtab_shndx_hdr;
   8033   1.1     skrll   Elf_Internal_Shdr *symstrtab_hdr;
   8034   1.1     skrll   struct elf_sym_strtab *symstrtab;
   8035   1.6  christos   bfd_byte *outbound_syms;
   8036   1.6  christos   bfd_byte *outbound_shndx;
   8037   1.1     skrll   unsigned long outbound_syms_index;
   8038   1.6  christos   unsigned long outbound_shndx_index;
   8039   1.1     skrll   int idx;
   8040   1.1     skrll   unsigned int num_locals;
   8041   1.1     skrll   bfd_size_type amt;
   8042   1.6  christos   bfd_boolean name_local_sections;
   8043   1.1     skrll 
   8044   1.1     skrll   if (!elf_map_symbols (abfd, &num_locals))
   8045   1.1     skrll     return FALSE;
   8046   1.6  christos 
   8047   1.1     skrll   /* Dump out the symtabs.  */
   8048   1.1     skrll   stt = _bfd_elf_strtab_init ();
   8049   1.1     skrll   if (stt == NULL)
   8050   1.1     skrll     return FALSE;
   8051   1.1     skrll 
   8052   1.1     skrll   bed = get_elf_backend_data (abfd);
   8053   1.1     skrll   symcount = bfd_get_symcount (abfd);
   8054   1.1     skrll   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   8055   1.1     skrll   symtab_hdr->sh_type = SHT_SYMTAB;
   8056   1.6  christos   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
   8057   1.1     skrll   symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
   8058   1.1     skrll   symtab_hdr->sh_info = num_locals + 1;
   8059   1.1     skrll   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   8060   1.1     skrll 
   8061   1.1     skrll   symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
   8062   1.6  christos   symstrtab_hdr->sh_type = SHT_STRTAB;
   8063  1.15  christos 
   8064  1.15  christos   /* Allocate buffer to swap out the .strtab section.  */
   8065   1.6  christos   symstrtab = (struct elf_sym_strtab *) bfd_malloc2 (symcount + 1,
   8066   1.6  christos 						     sizeof (*symstrtab));
   8067   1.6  christos   if (symstrtab == NULL)
   8068   1.6  christos     {
   8069   1.6  christos       _bfd_elf_strtab_free (stt);
   8070   1.6  christos       return FALSE;
   8071   1.3  christos     }
   8072   1.9  christos 
   8073   1.1     skrll   outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
   8074   1.1     skrll 					   bed->s->sizeof_sym);
   8075   1.6  christos   if (outbound_syms == NULL)
   8076   1.6  christos     {
   8077   1.6  christos error_return:
   8078   1.1     skrll       _bfd_elf_strtab_free (stt);
   8079   1.1     skrll       free (symstrtab);
   8080   1.1     skrll       return FALSE;
   8081   1.6  christos     }
   8082   1.1     skrll   symtab_hdr->contents = outbound_syms;
   8083   1.1     skrll   outbound_syms_index = 0;
   8084   1.6  christos 
   8085   1.6  christos   outbound_shndx = NULL;
   8086   1.6  christos   outbound_shndx_index = 0;
   8087   1.1     skrll 
   8088   1.6  christos   if (elf_symtab_shndx_list (abfd))
   8089   1.6  christos     {
   8090   1.1     skrll       symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
   8091   1.6  christos       if (symtab_shndx_hdr->sh_name != 0)
   8092   1.6  christos 	{
   8093   1.6  christos 	  amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
   8094   1.6  christos 	  outbound_shndx =  (bfd_byte *)
   8095   1.6  christos 	    bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
   8096   1.6  christos 	  if (outbound_shndx == NULL)
   8097   1.6  christos 	    goto error_return;
   8098   1.6  christos 
   8099   1.6  christos 	  symtab_shndx_hdr->contents = outbound_shndx;
   8100   1.6  christos 	  symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
   8101   1.6  christos 	  symtab_shndx_hdr->sh_size = amt;
   8102   1.1     skrll 	  symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
   8103   1.6  christos 	  symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
   8104   1.1     skrll 	}
   8105   1.1     skrll       /* FIXME: What about any other headers in the list ?  */
   8106   1.1     skrll     }
   8107   1.1     skrll 
   8108   1.1     skrll   /* Now generate the data (for "contents").  */
   8109   1.1     skrll   {
   8110   1.1     skrll     /* Fill in zeroth symbol and swap it out.  */
   8111   1.1     skrll     Elf_Internal_Sym sym;
   8112   1.1     skrll     sym.st_name = 0;
   8113   1.1     skrll     sym.st_value = 0;
   8114   1.1     skrll     sym.st_size = 0;
   8115   1.1     skrll     sym.st_info = 0;
   8116   1.5  christos     sym.st_other = 0;
   8117   1.6  christos     sym.st_shndx = SHN_UNDEF;
   8118   1.6  christos     sym.st_target_internal = 0;
   8119   1.6  christos     symstrtab[0].sym = sym;
   8120   1.6  christos     symstrtab[0].dest_index = outbound_syms_index;
   8121   1.1     skrll     symstrtab[0].destshndx_index = outbound_shndx_index;
   8122   1.6  christos     outbound_syms_index++;
   8123   1.1     skrll     if (outbound_shndx != NULL)
   8124   1.1     skrll       outbound_shndx_index++;
   8125   1.1     skrll   }
   8126   1.1     skrll 
   8127   1.1     skrll   name_local_sections
   8128   1.1     skrll     = (bed->elf_backend_name_local_section_symbols
   8129   1.1     skrll        && bed->elf_backend_name_local_section_symbols (abfd));
   8130   1.6  christos 
   8131   1.1     skrll   syms = bfd_get_outsymbols (abfd);
   8132   1.1     skrll   for (idx = 0; idx < symcount;)
   8133   1.1     skrll     {
   8134   1.1     skrll       Elf_Internal_Sym sym;
   8135   1.1     skrll       bfd_vma value = syms[idx]->value;
   8136   1.1     skrll       elf_symbol_type *type_ptr;
   8137   1.1     skrll       flagword flags = syms[idx]->flags;
   8138   1.1     skrll       int type;
   8139   1.1     skrll 
   8140   1.1     skrll       if (!name_local_sections
   8141   1.1     skrll 	  && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
   8142   1.6  christos 	{
   8143   1.1     skrll 	  /* Local section symbols have no name.  */
   8144   1.1     skrll 	  sym.st_name = (unsigned long) -1;
   8145   1.1     skrll 	}
   8146   1.6  christos       else
   8147   1.6  christos 	{
   8148   1.6  christos 	  /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
   8149   1.6  christos 	     to get the final offset for st_name.  */
   8150   1.6  christos 	  sym.st_name
   8151   1.1     skrll 	    = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
   8152   1.6  christos 						   FALSE);
   8153   1.1     skrll 	  if (sym.st_name == (unsigned long) -1)
   8154   1.1     skrll 	    goto error_return;
   8155   1.1     skrll 	}
   8156   1.1     skrll 
   8157   1.1     skrll       type_ptr = elf_symbol_from (abfd, syms[idx]);
   8158   1.1     skrll 
   8159   1.1     skrll       if ((flags & BSF_SECTION_SYM) == 0
   8160   1.1     skrll 	  && bfd_is_com_section (syms[idx]->section))
   8161   1.1     skrll 	{
   8162   1.1     skrll 	  /* ELF common symbols put the alignment into the `value' field,
   8163   1.1     skrll 	     and the size into the `size' field.  This is backwards from
   8164   1.1     skrll 	     how BFD handles it, so reverse it here.  */
   8165   1.1     skrll 	  sym.st_size = value;
   8166   1.1     skrll 	  if (type_ptr == NULL
   8167   1.1     skrll 	      || type_ptr->internal_elf_sym.st_value == 0)
   8168   1.1     skrll 	    sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
   8169   1.1     skrll 	  else
   8170   1.1     skrll 	    sym.st_value = type_ptr->internal_elf_sym.st_value;
   8171   1.1     skrll 	  sym.st_shndx = _bfd_elf_section_from_bfd_section
   8172   1.1     skrll 	    (abfd, syms[idx]->section);
   8173   1.1     skrll 	}
   8174   1.1     skrll       else
   8175   1.1     skrll 	{
   8176   1.1     skrll 	  asection *sec = syms[idx]->section;
   8177   1.1     skrll 	  unsigned int shndx;
   8178   1.1     skrll 
   8179   1.1     skrll 	  if (sec->output_section)
   8180   1.1     skrll 	    {
   8181   1.1     skrll 	      value += sec->output_offset;
   8182   1.1     skrll 	      sec = sec->output_section;
   8183   1.1     skrll 	    }
   8184   1.1     skrll 
   8185   1.1     skrll 	  /* Don't add in the section vma for relocatable output.  */
   8186   1.1     skrll 	  if (! relocatable_p)
   8187   1.1     skrll 	    value += sec->vma;
   8188   1.1     skrll 	  sym.st_value = value;
   8189   1.1     skrll 	  sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
   8190   1.1     skrll 
   8191   1.1     skrll 	  if (bfd_is_abs_section (sec)
   8192   1.1     skrll 	      && type_ptr != NULL
   8193   1.1     skrll 	      && type_ptr->internal_elf_sym.st_shndx != 0)
   8194   1.1     skrll 	    {
   8195   1.1     skrll 	      /* This symbol is in a real ELF section which we did
   8196   1.1     skrll 		 not create as a BFD section.  Undo the mapping done
   8197   1.1     skrll 		 by copy_private_symbol_data.  */
   8198   1.1     skrll 	      shndx = type_ptr->internal_elf_sym.st_shndx;
   8199   1.1     skrll 	      switch (shndx)
   8200   1.1     skrll 		{
   8201   1.1     skrll 		case MAP_ONESYMTAB:
   8202   1.1     skrll 		  shndx = elf_onesymtab (abfd);
   8203   1.1     skrll 		  break;
   8204   1.1     skrll 		case MAP_DYNSYMTAB:
   8205   1.1     skrll 		  shndx = elf_dynsymtab (abfd);
   8206   1.6  christos 		  break;
   8207   1.1     skrll 		case MAP_STRTAB:
   8208   1.1     skrll 		  shndx = elf_strtab_sec (abfd);
   8209   1.6  christos 		  break;
   8210   1.1     skrll 		case MAP_SHSTRTAB:
   8211   1.1     skrll 		  shndx = elf_shstrtab_sec (abfd);
   8212   1.6  christos 		  break;
   8213   1.6  christos 		case MAP_SYM_SHNDX:
   8214   1.1     skrll 		  if (elf_symtab_shndx_list (abfd))
   8215   1.1     skrll 		    shndx = elf_symtab_shndx_list (abfd)->ndx;
   8216   1.5  christos 		  break;
   8217   1.1     skrll 		default:
   8218   1.1     skrll 		  shndx = SHN_ABS;
   8219   1.1     skrll 		  break;
   8220   1.1     skrll 		}
   8221   1.1     skrll 	    }
   8222   1.1     skrll 	  else
   8223   1.1     skrll 	    {
   8224   1.1     skrll 	      shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   8225   1.1     skrll 
   8226   1.1     skrll 	      if (shndx == SHN_BAD)
   8227   1.1     skrll 		{
   8228   1.1     skrll 		  asection *sec2;
   8229   1.1     skrll 
   8230   1.1     skrll 		  /* Writing this would be a hell of a lot easier if
   8231   1.1     skrll 		     we had some decent documentation on bfd, and
   8232   1.1     skrll 		     knew what to expect of the library, and what to
   8233   1.1     skrll 		     demand of applications.  For example, it
   8234   1.1     skrll 		     appears that `objcopy' might not set the
   8235   1.1     skrll 		     section of a symbol to be a section that is
   8236   1.9  christos 		     actually in the output file.  */
   8237   1.9  christos 		  sec2 = bfd_get_section_by_name (abfd, sec->name);
   8238   1.9  christos 		  if (sec2 != NULL)
   8239   1.1     skrll 		    shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
   8240   1.9  christos 		  if (shndx == SHN_BAD)
   8241  1.13  christos 		    {
   8242  1.13  christos 		      /* xgettext:c-format */
   8243  1.13  christos 		      _bfd_error_handler
   8244  1.13  christos 			(_("unable to find equivalent output section"
   8245  1.13  christos 			   " for symbol '%s' from section '%s'"),
   8246   1.1     skrll 			 syms[idx]->name ? syms[idx]->name : "<Local sym>",
   8247   1.6  christos 			 sec->name);
   8248   1.1     skrll 		      bfd_set_error (bfd_error_invalid_operation);
   8249   1.1     skrll 		      goto error_return;
   8250   1.1     skrll 		    }
   8251   1.1     skrll 		}
   8252   1.1     skrll 	    }
   8253   1.1     skrll 
   8254   1.1     skrll 	  sym.st_shndx = shndx;
   8255   1.1     skrll 	}
   8256   1.1     skrll 
   8257   1.3  christos       if ((flags & BSF_THREAD_LOCAL) != 0)
   8258   1.3  christos 	type = STT_TLS;
   8259   1.1     skrll       else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
   8260   1.1     skrll 	type = STT_GNU_IFUNC;
   8261   1.1     skrll       else if ((flags & BSF_FUNCTION) != 0)
   8262   1.1     skrll 	type = STT_FUNC;
   8263   1.1     skrll       else if ((flags & BSF_OBJECT) != 0)
   8264   1.1     skrll 	type = STT_OBJECT;
   8265   1.1     skrll       else if ((flags & BSF_RELC) != 0)
   8266   1.1     skrll 	type = STT_RELC;
   8267   1.1     skrll       else if ((flags & BSF_SRELC) != 0)
   8268   1.1     skrll 	type = STT_SRELC;
   8269   1.1     skrll       else
   8270   1.1     skrll 	type = STT_NOTYPE;
   8271   1.1     skrll 
   8272   1.1     skrll       if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
   8273   1.1     skrll 	type = STT_TLS;
   8274   1.1     skrll 
   8275   1.1     skrll       /* Processor-specific types.  */
   8276   1.1     skrll       if (type_ptr != NULL
   8277   1.1     skrll 	  && bed->elf_backend_get_symbol_type)
   8278   1.1     skrll 	type = ((*bed->elf_backend_get_symbol_type)
   8279   1.1     skrll 		(&type_ptr->internal_elf_sym, type));
   8280   1.1     skrll 
   8281   1.1     skrll       if (flags & BSF_SECTION_SYM)
   8282   1.1     skrll 	{
   8283   1.1     skrll 	  if (flags & BSF_GLOBAL)
   8284   1.1     skrll 	    sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   8285   1.1     skrll 	  else
   8286   1.1     skrll 	    sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   8287   1.1     skrll 	}
   8288   1.7  christos       else if (bfd_is_com_section (syms[idx]->section))
   8289   1.7  christos 	{
   8290   1.7  christos 	  if (type != STT_TLS)
   8291   1.7  christos 	    {
   8292   1.7  christos 	      if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
   8293   1.7  christos 		type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
   8294   1.7  christos 			? STT_COMMON : STT_OBJECT);
   8295   1.7  christos 	      else
   8296   1.7  christos 		type = ((flags & BSF_ELF_COMMON) != 0
   8297   1.7  christos 			? STT_COMMON : STT_OBJECT);
   8298   1.1     skrll 	    }
   8299   1.1     skrll 	  sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
   8300   1.1     skrll 	}
   8301   1.1     skrll       else if (bfd_is_und_section (syms[idx]->section))
   8302   1.1     skrll 	sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
   8303   1.1     skrll 				    ? STB_WEAK
   8304   1.1     skrll 				    : STB_GLOBAL),
   8305   1.1     skrll 				   type);
   8306   1.1     skrll       else if (flags & BSF_FILE)
   8307   1.1     skrll 	sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   8308   1.1     skrll       else
   8309   1.1     skrll 	{
   8310   1.1     skrll 	  int bind = STB_LOCAL;
   8311   1.1     skrll 
   8312   1.3  christos 	  if (flags & BSF_LOCAL)
   8313   1.3  christos 	    bind = STB_LOCAL;
   8314   1.1     skrll 	  else if (flags & BSF_GNU_UNIQUE)
   8315   1.1     skrll 	    bind = STB_GNU_UNIQUE;
   8316   1.1     skrll 	  else if (flags & BSF_WEAK)
   8317   1.1     skrll 	    bind = STB_WEAK;
   8318   1.1     skrll 	  else if (flags & BSF_GLOBAL)
   8319   1.1     skrll 	    bind = STB_GLOBAL;
   8320   1.1     skrll 
   8321   1.1     skrll 	  sym.st_info = ELF_ST_INFO (bind, type);
   8322   1.1     skrll 	}
   8323   1.5  christos 
   8324   1.5  christos       if (type_ptr != NULL)
   8325   1.5  christos 	{
   8326   1.5  christos 	  sym.st_other = type_ptr->internal_elf_sym.st_other;
   8327   1.5  christos 	  sym.st_target_internal
   8328   1.1     skrll 	    = type_ptr->internal_elf_sym.st_target_internal;
   8329   1.5  christos 	}
   8330   1.5  christos       else
   8331   1.5  christos 	{
   8332   1.5  christos 	  sym.st_other = 0;
   8333   1.1     skrll 	  sym.st_target_internal = 0;
   8334   1.6  christos 	}
   8335   1.6  christos 
   8336   1.6  christos       idx++;
   8337   1.6  christos       symstrtab[idx].sym = sym;
   8338   1.6  christos       symstrtab[idx].dest_index = outbound_syms_index;
   8339   1.6  christos       symstrtab[idx].destshndx_index = outbound_shndx_index;
   8340   1.1     skrll 
   8341   1.6  christos       outbound_syms_index++;
   8342   1.1     skrll       if (outbound_shndx != NULL)
   8343   1.1     skrll 	outbound_shndx_index++;
   8344   1.6  christos     }
   8345   1.6  christos 
   8346   1.6  christos   /* Finalize the .strtab section.  */
   8347   1.6  christos   _bfd_elf_strtab_finalize (stt);
   8348   1.6  christos 
   8349   1.6  christos   /* Swap out the .strtab section.  */
   8350   1.6  christos   for (idx = 0; idx <= symcount; idx++)
   8351   1.6  christos     {
   8352   1.6  christos       struct elf_sym_strtab *elfsym = &symstrtab[idx];
   8353   1.6  christos       if (elfsym->sym.st_name == (unsigned long) -1)
   8354   1.6  christos 	elfsym->sym.st_name = 0;
   8355   1.6  christos       else
   8356   1.6  christos 	elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
   8357   1.6  christos 						      elfsym->sym.st_name);
   8358   1.6  christos       bed->s->swap_symbol_out (abfd, &elfsym->sym,
   8359   1.6  christos 			       (outbound_syms
   8360   1.6  christos 				+ (elfsym->dest_index
   8361   1.6  christos 				   * bed->s->sizeof_sym)),
   8362   1.6  christos 			       (outbound_shndx
   8363   1.6  christos 				+ (elfsym->destshndx_index
   8364   1.6  christos 				   * sizeof (Elf_External_Sym_Shndx))));
   8365   1.6  christos     }
   8366   1.1     skrll   free (symstrtab);
   8367   1.6  christos 
   8368   1.1     skrll   *sttp = stt;
   8369   1.7  christos   symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
   8370   1.1     skrll   symstrtab_hdr->sh_type = SHT_STRTAB;
   8371   1.1     skrll   symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   8372   1.1     skrll   symstrtab_hdr->sh_addr = 0;
   8373   1.1     skrll   symstrtab_hdr->sh_entsize = 0;
   8374   1.1     skrll   symstrtab_hdr->sh_link = 0;
   8375   1.1     skrll   symstrtab_hdr->sh_info = 0;
   8376   1.1     skrll   symstrtab_hdr->sh_addralign = 1;
   8377   1.1     skrll 
   8378   1.1     skrll   return TRUE;
   8379   1.1     skrll }
   8380   1.1     skrll 
   8381   1.1     skrll /* Return the number of bytes required to hold the symtab vector.
   8382   1.1     skrll 
   8383   1.1     skrll    Note that we base it on the count plus 1, since we will null terminate
   8384   1.1     skrll    the vector allocated based on this size.  However, the ELF symbol table
   8385   1.1     skrll    always has a dummy entry as symbol #0, so it ends up even.  */
   8386   1.1     skrll 
   8387   1.1     skrll long
   8388  1.15  christos _bfd_elf_get_symtab_upper_bound (bfd *abfd)
   8389   1.1     skrll {
   8390   1.1     skrll   bfd_size_type symcount;
   8391   1.1     skrll   long symtab_size;
   8392   1.1     skrll   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
   8393  1.15  christos 
   8394  1.15  christos   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   8395  1.15  christos   if (symcount >= LONG_MAX / sizeof (asymbol *))
   8396  1.15  christos     {
   8397  1.15  christos       bfd_set_error (bfd_error_file_too_big);
   8398   1.1     skrll       return -1;
   8399   1.1     skrll     }
   8400   1.1     skrll   symtab_size = (symcount + 1) * (sizeof (asymbol *));
   8401   1.1     skrll   if (symcount > 0)
   8402   1.1     skrll     symtab_size -= sizeof (asymbol *);
   8403   1.1     skrll 
   8404   1.1     skrll   return symtab_size;
   8405   1.1     skrll }
   8406   1.1     skrll 
   8407   1.1     skrll long
   8408  1.15  christos _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
   8409   1.1     skrll {
   8410   1.1     skrll   bfd_size_type symcount;
   8411   1.1     skrll   long symtab_size;
   8412   1.1     skrll   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   8413   1.1     skrll 
   8414   1.1     skrll   if (elf_dynsymtab (abfd) == 0)
   8415   1.1     skrll     {
   8416   1.1     skrll       bfd_set_error (bfd_error_invalid_operation);
   8417   1.1     skrll       return -1;
   8418   1.1     skrll     }
   8419  1.15  christos 
   8420  1.15  christos   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   8421  1.15  christos   if (symcount >= LONG_MAX / sizeof (asymbol *))
   8422  1.15  christos     {
   8423  1.15  christos       bfd_set_error (bfd_error_file_too_big);
   8424   1.1     skrll       return -1;
   8425   1.1     skrll     }
   8426   1.1     skrll   symtab_size = (symcount + 1) * (sizeof (asymbol *));
   8427   1.1     skrll   if (symcount > 0)
   8428   1.1     skrll     symtab_size -= sizeof (asymbol *);
   8429   1.1     skrll 
   8430   1.1     skrll   return symtab_size;
   8431   1.1     skrll }
   8432   1.1     skrll 
   8433   1.1     skrll long
   8434   1.1     skrll _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
   8435  1.15  christos 				sec_ptr asect)
   8436  1.15  christos {
   8437  1.15  christos #if SIZEOF_LONG == SIZEOF_INT
   8438  1.15  christos   if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
   8439  1.15  christos     {
   8440  1.15  christos       bfd_set_error (bfd_error_file_too_big);
   8441  1.15  christos       return -1;
   8442   1.1     skrll     }
   8443   1.1     skrll #endif
   8444   1.1     skrll   return (asect->reloc_count + 1) * sizeof (arelent *);
   8445   1.1     skrll }
   8446   1.1     skrll 
   8447   1.1     skrll /* Canonicalize the relocs.  */
   8448   1.1     skrll 
   8449   1.1     skrll long
   8450   1.1     skrll _bfd_elf_canonicalize_reloc (bfd *abfd,
   8451   1.1     skrll 			     sec_ptr section,
   8452   1.1     skrll 			     arelent **relptr,
   8453   1.1     skrll 			     asymbol **symbols)
   8454   1.1     skrll {
   8455   1.1     skrll   arelent *tblptr;
   8456   1.1     skrll   unsigned int i;
   8457   1.1     skrll   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8458   1.1     skrll 
   8459   1.1     skrll   if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
   8460   1.1     skrll     return -1;
   8461   1.1     skrll 
   8462   1.1     skrll   tblptr = section->relocation;
   8463   1.1     skrll   for (i = 0; i < section->reloc_count; i++)
   8464   1.1     skrll     *relptr++ = tblptr++;
   8465   1.1     skrll 
   8466   1.1     skrll   *relptr = NULL;
   8467   1.1     skrll 
   8468   1.1     skrll   return section->reloc_count;
   8469   1.1     skrll }
   8470   1.1     skrll 
   8471   1.1     skrll long
   8472   1.1     skrll _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
   8473   1.1     skrll {
   8474   1.1     skrll   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8475   1.1     skrll   long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
   8476  1.15  christos 
   8477   1.1     skrll   if (symcount >= 0)
   8478   1.1     skrll     abfd->symcount = symcount;
   8479   1.1     skrll   return symcount;
   8480   1.1     skrll }
   8481   1.1     skrll 
   8482   1.1     skrll long
   8483   1.1     skrll _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
   8484   1.1     skrll 				      asymbol **allocation)
   8485   1.1     skrll {
   8486   1.1     skrll   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8487   1.1     skrll   long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
   8488  1.15  christos 
   8489   1.1     skrll   if (symcount >= 0)
   8490   1.1     skrll     abfd->dynsymcount = symcount;
   8491   1.1     skrll   return symcount;
   8492   1.1     skrll }
   8493   1.1     skrll 
   8494   1.1     skrll /* Return the size required for the dynamic reloc entries.  Any loadable
   8495   1.1     skrll    section that was actually installed in the BFD, and has type SHT_REL
   8496   1.1     skrll    or SHT_RELA, and uses the dynamic symbol table, is considered to be a
   8497   1.1     skrll    dynamic reloc section.  */
   8498   1.1     skrll 
   8499   1.1     skrll long
   8500  1.15  christos _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
   8501   1.1     skrll {
   8502   1.1     skrll   bfd_size_type count;
   8503   1.1     skrll   asection *s;
   8504   1.1     skrll 
   8505   1.1     skrll   if (elf_dynsymtab (abfd) == 0)
   8506   1.1     skrll     {
   8507   1.1     skrll       bfd_set_error (bfd_error_invalid_operation);
   8508   1.1     skrll       return -1;
   8509  1.15  christos     }
   8510   1.1     skrll 
   8511   1.1     skrll   count = 1;
   8512   1.1     skrll   for (s = abfd->sections; s != NULL; s = s->next)
   8513   1.1     skrll     if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   8514  1.15  christos 	&& (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   8515  1.15  christos 	    || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
   8516  1.15  christos       {
   8517  1.15  christos 	count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
   8518  1.15  christos 	if (count > LONG_MAX / sizeof (arelent *))
   8519  1.15  christos 	  {
   8520  1.15  christos 	    bfd_set_error (bfd_error_file_too_big);
   8521  1.15  christos 	    return -1;
   8522  1.15  christos 	  }
   8523   1.1     skrll       }
   8524   1.1     skrll   return count * sizeof (arelent *);
   8525   1.1     skrll }
   8526   1.1     skrll 
   8527   1.1     skrll /* Canonicalize the dynamic relocation entries.  Note that we return the
   8528   1.1     skrll    dynamic relocations as a single block, although they are actually
   8529   1.1     skrll    associated with particular sections; the interface, which was
   8530   1.1     skrll    designed for SunOS style shared libraries, expects that there is only
   8531   1.1     skrll    one set of dynamic relocs.  Any loadable section that was actually
   8532   1.1     skrll    installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
   8533   1.1     skrll    dynamic symbol table, is considered to be a dynamic reloc section.  */
   8534   1.1     skrll 
   8535   1.1     skrll long
   8536   1.1     skrll _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
   8537   1.1     skrll 				     arelent **storage,
   8538   1.1     skrll 				     asymbol **syms)
   8539   1.1     skrll {
   8540   1.1     skrll   bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
   8541   1.1     skrll   asection *s;
   8542   1.1     skrll   long ret;
   8543   1.1     skrll 
   8544   1.1     skrll   if (elf_dynsymtab (abfd) == 0)
   8545   1.1     skrll     {
   8546   1.1     skrll       bfd_set_error (bfd_error_invalid_operation);
   8547   1.1     skrll       return -1;
   8548   1.1     skrll     }
   8549   1.1     skrll 
   8550   1.1     skrll   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   8551   1.1     skrll   ret = 0;
   8552   1.1     skrll   for (s = abfd->sections; s != NULL; s = s->next)
   8553   1.1     skrll     {
   8554   1.1     skrll       if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   8555   1.1     skrll 	  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   8556   1.1     skrll 	      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
   8557   1.1     skrll 	{
   8558   1.1     skrll 	  arelent *p;
   8559   1.1     skrll 	  long count, i;
   8560   1.1     skrll 
   8561   1.1     skrll 	  if (! (*slurp_relocs) (abfd, s, syms, TRUE))
   8562   1.1     skrll 	    return -1;
   8563   1.1     skrll 	  count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
   8564   1.1     skrll 	  p = s->relocation;
   8565   1.1     skrll 	  for (i = 0; i < count; i++)
   8566   1.1     skrll 	    *storage++ = p++;
   8567   1.1     skrll 	  ret += count;
   8568   1.1     skrll 	}
   8569   1.1     skrll     }
   8570   1.1     skrll 
   8571   1.1     skrll   *storage = NULL;
   8572   1.1     skrll 
   8573   1.1     skrll   return ret;
   8574   1.1     skrll }
   8575   1.1     skrll 
   8576   1.1     skrll /* Read in the version information.  */
   8578   1.1     skrll 
   8579   1.1     skrll bfd_boolean
   8580   1.1     skrll _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
   8581   1.1     skrll {
   8582   1.1     skrll   bfd_byte *contents = NULL;
   8583   1.1     skrll   unsigned int freeidx = 0;
   8584   1.1     skrll 
   8585   1.1     skrll   if (elf_dynverref (abfd) != 0)
   8586   1.1     skrll     {
   8587   1.1     skrll       Elf_Internal_Shdr *hdr;
   8588   1.1     skrll       Elf_External_Verneed *everneed;
   8589   1.1     skrll       Elf_Internal_Verneed *iverneed;
   8590   1.1     skrll       unsigned int i;
   8591   1.1     skrll       bfd_byte *contents_end;
   8592   1.9  christos 
   8593   1.9  christos       hdr = &elf_tdata (abfd)->dynverref_hdr;
   8594   1.1     skrll 
   8595   1.6  christos       if (hdr->sh_info == 0
   8596   1.9  christos 	  || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
   8597  1.13  christos 	{
   8598   1.6  christos error_return_bad_verref:
   8599   1.1     skrll 	  _bfd_error_handler
   8600   1.1     skrll 	    (_("%pB: .gnu.version_r invalid entry"), abfd);
   8601   1.1     skrll 	  bfd_set_error (bfd_error_bad_value);
   8602   1.1     skrll error_return_verref:
   8603   1.1     skrll 	  elf_tdata (abfd)->verref = NULL;
   8604   1.6  christos 	  elf_tdata (abfd)->cverrefs = 0;
   8605  1.15  christos 	  goto error_return;
   8606  1.15  christos 	}
   8607  1.15  christos 
   8608  1.15  christos       ufile_ptr filesize = bfd_get_file_size (abfd);
   8609  1.15  christos       if (filesize > 0 && filesize < hdr->sh_size)
   8610  1.15  christos 	{
   8611  1.15  christos 	  /* PR 24708: Avoid attempts to allocate a ridiculous amount
   8612  1.15  christos 	     of memory.  */
   8613  1.15  christos 	  bfd_set_error (bfd_error_no_memory);
   8614  1.15  christos 	  _bfd_error_handler
   8615  1.15  christos 	    /* xgettext:c-format */
   8616  1.15  christos 	    (_("error: %pB version reference section is too large (%#" PRIx64 " bytes)"),
   8617   1.6  christos 	     abfd, (uint64_t) hdr->sh_size);
   8618   1.6  christos 	  goto error_return_verref;
   8619   1.6  christos 	}
   8620   1.6  christos       contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
   8621   1.1     skrll       if (contents == NULL)
   8622   1.1     skrll 	goto error_return_verref;
   8623   1.1     skrll 
   8624   1.1     skrll       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   8625   1.6  christos 	  || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
   8626   1.9  christos 	goto error_return_verref;
   8627   1.6  christos 
   8628   1.6  christos       elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
   8629   1.1     skrll 	bfd_alloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
   8630   1.1     skrll 
   8631   1.1     skrll       if (elf_tdata (abfd)->verref == NULL)
   8632   1.1     skrll 	goto error_return_verref;
   8633   1.1     skrll 
   8634   1.1     skrll       BFD_ASSERT (sizeof (Elf_External_Verneed)
   8635   1.1     skrll 		  == sizeof (Elf_External_Vernaux));
   8636   1.1     skrll       contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
   8637   1.1     skrll       everneed = (Elf_External_Verneed *) contents;
   8638   1.1     skrll       iverneed = elf_tdata (abfd)->verref;
   8639   1.1     skrll       for (i = 0; i < hdr->sh_info; i++, iverneed++)
   8640   1.1     skrll 	{
   8641   1.1     skrll 	  Elf_External_Vernaux *evernaux;
   8642   1.1     skrll 	  Elf_Internal_Vernaux *ivernaux;
   8643   1.1     skrll 	  unsigned int j;
   8644   1.1     skrll 
   8645   1.1     skrll 	  _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
   8646   1.1     skrll 
   8647   1.1     skrll 	  iverneed->vn_bfd = abfd;
   8648   1.1     skrll 
   8649   1.1     skrll 	  iverneed->vn_filename =
   8650   1.6  christos 	    bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   8651   1.1     skrll 					     iverneed->vn_file);
   8652   1.1     skrll 	  if (iverneed->vn_filename == NULL)
   8653   1.1     skrll 	    goto error_return_bad_verref;
   8654   1.1     skrll 
   8655   1.1     skrll 	  if (iverneed->vn_cnt == 0)
   8656   1.3  christos 	    iverneed->vn_auxptr = NULL;
   8657   1.9  christos 	  else
   8658   1.9  christos 	    {
   8659   1.1     skrll 	      iverneed->vn_auxptr = (struct elf_internal_vernaux *)
   8660   1.1     skrll 		  bfd_alloc2 (abfd, iverneed->vn_cnt,
   8661   1.1     skrll 			      sizeof (Elf_Internal_Vernaux));
   8662   1.1     skrll 	      if (iverneed->vn_auxptr == NULL)
   8663   1.1     skrll 		goto error_return_verref;
   8664   1.1     skrll 	    }
   8665   1.6  christos 
   8666   1.1     skrll 	  if (iverneed->vn_aux
   8667   1.1     skrll 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   8668   1.1     skrll 	    goto error_return_bad_verref;
   8669   1.1     skrll 
   8670   1.1     skrll 	  evernaux = ((Elf_External_Vernaux *)
   8671   1.1     skrll 		      ((bfd_byte *) everneed + iverneed->vn_aux));
   8672   1.1     skrll 	  ivernaux = iverneed->vn_auxptr;
   8673   1.1     skrll 	  for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
   8674   1.1     skrll 	    {
   8675   1.1     skrll 	      _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
   8676   1.1     skrll 
   8677   1.1     skrll 	      ivernaux->vna_nodename =
   8678   1.6  christos 		bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   8679   1.6  christos 						 ivernaux->vna_name);
   8680   1.6  christos 	      if (ivernaux->vna_nodename == NULL)
   8681   1.6  christos 		goto error_return_bad_verref;
   8682   1.1     skrll 
   8683   1.6  christos 	      if (ivernaux->vna_other > freeidx)
   8684   1.6  christos 		freeidx = ivernaux->vna_other;
   8685   1.6  christos 
   8686   1.6  christos 	      ivernaux->vna_nextptr = NULL;
   8687   1.6  christos 	      if (ivernaux->vna_next == 0)
   8688   1.6  christos 		{
   8689   1.1     skrll 		  iverneed->vn_cnt = j + 1;
   8690   1.1     skrll 		  break;
   8691   1.1     skrll 		}
   8692   1.1     skrll 	      if (j + 1 < iverneed->vn_cnt)
   8693   1.1     skrll 		ivernaux->vna_nextptr = ivernaux + 1;
   8694   1.6  christos 
   8695   1.1     skrll 	      if (ivernaux->vna_next
   8696   1.1     skrll 		  > (size_t) (contents_end - (bfd_byte *) evernaux))
   8697   1.1     skrll 		goto error_return_bad_verref;
   8698   1.1     skrll 
   8699   1.1     skrll 	      evernaux = ((Elf_External_Vernaux *)
   8700   1.6  christos 			  ((bfd_byte *) evernaux + ivernaux->vna_next));
   8701   1.6  christos 	    }
   8702   1.6  christos 
   8703   1.1     skrll 	  iverneed->vn_nextref = NULL;
   8704   1.1     skrll 	  if (iverneed->vn_next == 0)
   8705   1.1     skrll 	    break;
   8706   1.1     skrll 	  if (i + 1 < hdr->sh_info)
   8707   1.1     skrll 	    iverneed->vn_nextref = iverneed + 1;
   8708   1.6  christos 
   8709   1.1     skrll 	  if (iverneed->vn_next
   8710   1.1     skrll 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   8711   1.1     skrll 	    goto error_return_bad_verref;
   8712   1.1     skrll 
   8713   1.6  christos 	  everneed = ((Elf_External_Verneed *)
   8714   1.1     skrll 		      ((bfd_byte *) everneed + iverneed->vn_next));
   8715   1.1     skrll 	}
   8716   1.1     skrll       elf_tdata (abfd)->cverrefs = i;
   8717   1.1     skrll 
   8718   1.1     skrll       free (contents);
   8719   1.1     skrll       contents = NULL;
   8720   1.1     skrll     }
   8721   1.1     skrll 
   8722   1.1     skrll   if (elf_dynverdef (abfd) != 0)
   8723   1.1     skrll     {
   8724   1.1     skrll       Elf_Internal_Shdr *hdr;
   8725   1.1     skrll       Elf_External_Verdef *everdef;
   8726   1.1     skrll       Elf_Internal_Verdef *iverdef;
   8727   1.1     skrll       Elf_Internal_Verdef *iverdefarr;
   8728   1.1     skrll       Elf_Internal_Verdef iverdefmem;
   8729   1.1     skrll       unsigned int i;
   8730   1.1     skrll       unsigned int maxidx;
   8731   1.1     skrll       bfd_byte *contents_end_def, *contents_end_aux;
   8732   1.6  christos 
   8733   1.6  christos       hdr = &elf_tdata (abfd)->dynverdef_hdr;
   8734   1.6  christos 
   8735   1.9  christos       if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
   8736  1.13  christos 	{
   8737   1.6  christos 	error_return_bad_verdef:
   8738   1.6  christos 	  _bfd_error_handler
   8739   1.6  christos 	    (_("%pB: .gnu.version_d invalid entry"), abfd);
   8740   1.6  christos 	  bfd_set_error (bfd_error_bad_value);
   8741   1.6  christos 	error_return_verdef:
   8742   1.6  christos 	  elf_tdata (abfd)->verdef = NULL;
   8743   1.6  christos 	  elf_tdata (abfd)->cverdefs = 0;
   8744   1.3  christos 	  goto error_return;
   8745   1.1     skrll 	}
   8746   1.6  christos 
   8747   1.1     skrll       contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
   8748   1.1     skrll       if (contents == NULL)
   8749   1.6  christos 	goto error_return_verdef;
   8750   1.1     skrll       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   8751   1.1     skrll 	  || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
   8752   1.1     skrll 	goto error_return_verdef;
   8753   1.1     skrll 
   8754   1.1     skrll       BFD_ASSERT (sizeof (Elf_External_Verdef)
   8755   1.1     skrll 		  >= sizeof (Elf_External_Verdaux));
   8756   1.1     skrll       contents_end_def = contents + hdr->sh_size
   8757   1.1     skrll 			 - sizeof (Elf_External_Verdef);
   8758   1.1     skrll       contents_end_aux = contents + hdr->sh_size
   8759   1.1     skrll 			 - sizeof (Elf_External_Verdaux);
   8760   1.1     skrll 
   8761   1.1     skrll       /* We know the number of entries in the section but not the maximum
   8762   1.1     skrll 	 index.  Therefore we have to run through all entries and find
   8763   1.1     skrll 	 the maximum.  */
   8764   1.1     skrll       everdef = (Elf_External_Verdef *) contents;
   8765   1.1     skrll       maxidx = 0;
   8766   1.1     skrll       for (i = 0; i < hdr->sh_info; ++i)
   8767   1.6  christos 	{
   8768   1.6  christos 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   8769   1.1     skrll 
   8770   1.1     skrll 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
   8771   1.1     skrll 	    goto error_return_bad_verdef;
   8772   1.6  christos 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
   8773   1.6  christos 	    maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
   8774   1.6  christos 
   8775   1.1     skrll 	  if (iverdefmem.vd_next == 0)
   8776   1.1     skrll 	    break;
   8777   1.6  christos 
   8778   1.1     skrll 	  if (iverdefmem.vd_next
   8779   1.1     skrll 	      > (size_t) (contents_end_def - (bfd_byte *) everdef))
   8780   1.1     skrll 	    goto error_return_bad_verdef;
   8781   1.1     skrll 
   8782   1.1     skrll 	  everdef = ((Elf_External_Verdef *)
   8783   1.1     skrll 		     ((bfd_byte *) everdef + iverdefmem.vd_next));
   8784   1.1     skrll 	}
   8785   1.1     skrll 
   8786   1.1     skrll       if (default_imported_symver)
   8787   1.1     skrll 	{
   8788   1.1     skrll 	  if (freeidx > maxidx)
   8789   1.1     skrll 	    maxidx = ++freeidx;
   8790   1.6  christos 	  else
   8791   1.3  christos 	    freeidx = ++maxidx;
   8792   1.6  christos 	}
   8793   1.1     skrll 
   8794   1.6  christos       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
   8795   1.1     skrll 	bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
   8796   1.1     skrll       if (elf_tdata (abfd)->verdef == NULL)
   8797   1.1     skrll 	goto error_return_verdef;
   8798   1.1     skrll 
   8799   1.1     skrll       elf_tdata (abfd)->cverdefs = maxidx;
   8800   1.1     skrll 
   8801   1.1     skrll       everdef = (Elf_External_Verdef *) contents;
   8802   1.1     skrll       iverdefarr = elf_tdata (abfd)->verdef;
   8803   1.1     skrll       for (i = 0; i < hdr->sh_info; i++)
   8804   1.1     skrll 	{
   8805   1.1     skrll 	  Elf_External_Verdaux *everdaux;
   8806   1.1     skrll 	  Elf_Internal_Verdaux *iverdaux;
   8807   1.1     skrll 	  unsigned int j;
   8808   1.1     skrll 
   8809   1.6  christos 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   8810   1.1     skrll 
   8811   1.1     skrll 	  if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
   8812   1.6  christos 	    goto error_return_bad_verdef;
   8813   1.1     skrll 
   8814   1.1     skrll 	  iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
   8815   1.1     skrll 	  memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
   8816   1.1     skrll 
   8817   1.1     skrll 	  iverdef->vd_bfd = abfd;
   8818   1.1     skrll 
   8819   1.1     skrll 	  if (iverdef->vd_cnt == 0)
   8820   1.3  christos 	    iverdef->vd_auxptr = NULL;
   8821   1.9  christos 	  else
   8822   1.9  christos 	    {
   8823   1.1     skrll 	      iverdef->vd_auxptr = (struct elf_internal_verdaux *)
   8824   1.1     skrll 		  bfd_alloc2 (abfd, iverdef->vd_cnt,
   8825   1.1     skrll 			      sizeof (Elf_Internal_Verdaux));
   8826   1.1     skrll 	      if (iverdef->vd_auxptr == NULL)
   8827   1.1     skrll 		goto error_return_verdef;
   8828   1.1     skrll 	    }
   8829   1.6  christos 
   8830   1.1     skrll 	  if (iverdef->vd_aux
   8831   1.1     skrll 	      > (size_t) (contents_end_aux - (bfd_byte *) everdef))
   8832   1.1     skrll 	    goto error_return_bad_verdef;
   8833   1.1     skrll 
   8834   1.1     skrll 	  everdaux = ((Elf_External_Verdaux *)
   8835   1.1     skrll 		      ((bfd_byte *) everdef + iverdef->vd_aux));
   8836   1.1     skrll 	  iverdaux = iverdef->vd_auxptr;
   8837   1.1     skrll 	  for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
   8838   1.1     skrll 	    {
   8839   1.1     skrll 	      _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
   8840   1.1     skrll 
   8841   1.1     skrll 	      iverdaux->vda_nodename =
   8842   1.6  christos 		bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   8843   1.1     skrll 						 iverdaux->vda_name);
   8844   1.6  christos 	      if (iverdaux->vda_nodename == NULL)
   8845   1.6  christos 		goto error_return_bad_verdef;
   8846   1.6  christos 
   8847   1.6  christos 	      iverdaux->vda_nextptr = NULL;
   8848   1.6  christos 	      if (iverdaux->vda_next == 0)
   8849   1.6  christos 		{
   8850   1.1     skrll 		  iverdef->vd_cnt = j + 1;
   8851   1.1     skrll 		  break;
   8852   1.1     skrll 		}
   8853   1.1     skrll 	      if (j + 1 < iverdef->vd_cnt)
   8854   1.1     skrll 		iverdaux->vda_nextptr = iverdaux + 1;
   8855   1.6  christos 
   8856   1.1     skrll 	      if (iverdaux->vda_next
   8857   1.1     skrll 		  > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
   8858   1.1     skrll 		goto error_return_bad_verdef;
   8859   1.1     skrll 
   8860   1.1     skrll 	      everdaux = ((Elf_External_Verdaux *)
   8861   1.6  christos 			  ((bfd_byte *) everdaux + iverdaux->vda_next));
   8862   1.1     skrll 	    }
   8863   1.1     skrll 
   8864   1.1     skrll 	  iverdef->vd_nodename = NULL;
   8865   1.6  christos 	  if (iverdef->vd_cnt)
   8866   1.6  christos 	    iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
   8867   1.6  christos 
   8868   1.1     skrll 	  iverdef->vd_nextdef = NULL;
   8869   1.1     skrll 	  if (iverdef->vd_next == 0)
   8870   1.1     skrll 	    break;
   8871   1.1     skrll 	  if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
   8872   1.1     skrll 	    iverdef->vd_nextdef = iverdef + 1;
   8873   1.1     skrll 
   8874   1.1     skrll 	  everdef = ((Elf_External_Verdef *)
   8875   1.1     skrll 		     ((bfd_byte *) everdef + iverdef->vd_next));
   8876   1.1     skrll 	}
   8877   1.1     skrll 
   8878   1.1     skrll       free (contents);
   8879   1.1     skrll       contents = NULL;
   8880   1.1     skrll     }
   8881   1.1     skrll   else if (default_imported_symver)
   8882   1.1     skrll     {
   8883   1.1     skrll       if (freeidx < 3)
   8884   1.1     skrll 	freeidx = 3;
   8885   1.3  christos       else
   8886   1.9  christos 	freeidx++;
   8887   1.1     skrll 
   8888   1.1     skrll       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
   8889   1.1     skrll 	  bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
   8890   1.1     skrll       if (elf_tdata (abfd)->verdef == NULL)
   8891   1.1     skrll 	goto error_return;
   8892   1.1     skrll 
   8893   1.1     skrll       elf_tdata (abfd)->cverdefs = freeidx;
   8894   1.1     skrll     }
   8895   1.1     skrll 
   8896   1.1     skrll   /* Create a default version based on the soname.  */
   8897   1.1     skrll   if (default_imported_symver)
   8898   1.1     skrll     {
   8899   1.6  christos       Elf_Internal_Verdef *iverdef;
   8900   1.1     skrll       Elf_Internal_Verdaux *iverdaux;
   8901   1.1     skrll 
   8902   1.1     skrll       iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
   8903   1.1     skrll 
   8904   1.1     skrll       iverdef->vd_version = VER_DEF_CURRENT;
   8905   1.1     skrll       iverdef->vd_flags = 0;
   8906   1.1     skrll       iverdef->vd_ndx = freeidx;
   8907   1.1     skrll       iverdef->vd_cnt = 1;
   8908   1.1     skrll 
   8909   1.1     skrll       iverdef->vd_bfd = abfd;
   8910   1.1     skrll 
   8911   1.1     skrll       iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
   8912   1.6  christos       if (iverdef->vd_nodename == NULL)
   8913   1.6  christos 	goto error_return_verdef;
   8914   1.1     skrll       iverdef->vd_nextdef = NULL;
   8915   1.1     skrll       iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
   8916   1.1     skrll 			    bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
   8917   1.1     skrll       if (iverdef->vd_auxptr == NULL)
   8918   1.1     skrll 	goto error_return_verdef;
   8919   1.1     skrll 
   8920   1.1     skrll       iverdaux = iverdef->vd_auxptr;
   8921   1.1     skrll       iverdaux->vda_nodename = iverdef->vd_nodename;
   8922   1.1     skrll     }
   8923   1.1     skrll 
   8924   1.1     skrll   return TRUE;
   8925   1.1     skrll 
   8926   1.1     skrll  error_return:
   8927   1.1     skrll   if (contents != NULL)
   8928   1.1     skrll     free (contents);
   8929   1.1     skrll   return FALSE;
   8930   1.1     skrll }
   8931   1.1     skrll 
   8932   1.1     skrll asymbol *
   8934  1.15  christos _bfd_elf_make_empty_symbol (bfd *abfd)
   8935   1.1     skrll {
   8936   1.1     skrll   elf_symbol_type *newsym;
   8937   1.6  christos 
   8938   1.6  christos   newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
   8939   1.1     skrll   if (!newsym)
   8940   1.1     skrll     return NULL;
   8941   1.1     skrll   newsym->symbol.the_bfd = abfd;
   8942   1.1     skrll   return &newsym->symbol;
   8943   1.1     skrll }
   8944   1.1     skrll 
   8945   1.1     skrll void
   8946   1.1     skrll _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
   8947   1.1     skrll 			  asymbol *symbol,
   8948   1.1     skrll 			  symbol_info *ret)
   8949   1.1     skrll {
   8950   1.1     skrll   bfd_symbol_info (symbol, ret);
   8951   1.1     skrll }
   8952   1.1     skrll 
   8953   1.1     skrll /* Return whether a symbol name implies a local symbol.  Most targets
   8954   1.1     skrll    use this function for the is_local_label_name entry point, but some
   8955   1.1     skrll    override it.  */
   8956   1.1     skrll 
   8957   1.1     skrll bfd_boolean
   8958   1.1     skrll _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
   8959   1.1     skrll 			      const char *name)
   8960   1.1     skrll {
   8961   1.1     skrll   /* Normal local symbols start with ``.L''.  */
   8962   1.1     skrll   if (name[0] == '.' && name[1] == 'L')
   8963   1.1     skrll     return TRUE;
   8964   1.1     skrll 
   8965   1.1     skrll   /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
   8966   1.1     skrll      DWARF debugging symbols starting with ``..''.  */
   8967   1.1     skrll   if (name[0] == '.' && name[1] == '.')
   8968   1.1     skrll     return TRUE;
   8969   1.1     skrll 
   8970   1.1     skrll   /* gcc will sometimes generate symbols beginning with ``_.L_'' when
   8971   1.1     skrll      emitting DWARF debugging output.  I suspect this is actually a
   8972   1.1     skrll      small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
   8973   1.1     skrll      ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
   8974   1.1     skrll      underscore to be emitted on some ELF targets).  For ease of use,
   8975   1.6  christos      we treat such symbols as local.  */
   8976   1.6  christos   if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
   8977   1.6  christos     return TRUE;
   8978   1.6  christos 
   8979   1.9  christos   /* Treat assembler generated fake symbols, dollar local labels and
   8980   1.6  christos      forward-backward labels (aka local labels) as locals.
   8981   1.6  christos      These labels have the form:
   8982   1.6  christos 
   8983   1.6  christos        L0^A.*				       (fake symbols)
   8984   1.6  christos 
   8985   1.6  christos        [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
   8986   1.6  christos 
   8987   1.6  christos      Versions which start with .L will have already been matched above,
   8988   1.6  christos      so we only need to match the rest.  */
   8989   1.6  christos   if (name[0] == 'L' && ISDIGIT (name[1]))
   8990   1.6  christos     {
   8991   1.6  christos       bfd_boolean ret = FALSE;
   8992   1.6  christos       const char * p;
   8993   1.6  christos       char c;
   8994   1.6  christos 
   8995   1.6  christos       for (p = name + 2; (c = *p); p++)
   8996   1.6  christos 	{
   8997   1.6  christos 	  if (c == 1 || c == 2)
   8998   1.6  christos 	    {
   8999   1.6  christos 	      if (c == 1 && p == name + 2)
   9000   1.6  christos 		/* A fake symbol.  */
   9001   1.6  christos 		return TRUE;
   9002   1.6  christos 
   9003   1.6  christos 	      /* FIXME: We are being paranoid here and treating symbols like
   9004   1.6  christos 		 L0^Bfoo as if there were non-local, on the grounds that the
   9005   1.6  christos 		 assembler will never generate them.  But can any symbol
   9006   1.6  christos 		 containing an ASCII value in the range 1-31 ever be anything
   9007   1.6  christos 		 other than some kind of local ?  */
   9008   1.6  christos 	      ret = TRUE;
   9009   1.6  christos 	    }
   9010   1.6  christos 
   9011   1.6  christos 	  if (! ISDIGIT (c))
   9012   1.6  christos 	    {
   9013   1.6  christos 	      ret = FALSE;
   9014   1.6  christos 	      break;
   9015   1.6  christos 	    }
   9016   1.1     skrll 	}
   9017   1.1     skrll       return ret;
   9018   1.1     skrll     }
   9019   1.1     skrll 
   9020   1.1     skrll   return FALSE;
   9021   1.1     skrll }
   9022   1.1     skrll 
   9023   1.1     skrll alent *
   9024   1.1     skrll _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
   9025   1.1     skrll 		     asymbol *symbol ATTRIBUTE_UNUSED)
   9026   1.1     skrll {
   9027   1.1     skrll   abort ();
   9028   1.1     skrll   return NULL;
   9029   1.1     skrll }
   9030   1.1     skrll 
   9031   1.1     skrll bfd_boolean
   9032   1.1     skrll _bfd_elf_set_arch_mach (bfd *abfd,
   9033   1.1     skrll 			enum bfd_architecture arch,
   9034   1.1     skrll 			unsigned long machine)
   9035   1.1     skrll {
   9036   1.1     skrll   /* If this isn't the right architecture for this backend, and this
   9037   1.1     skrll      isn't the generic backend, fail.  */
   9038   1.1     skrll   if (arch != get_elf_backend_data (abfd)->arch
   9039   1.1     skrll       && arch != bfd_arch_unknown
   9040   1.1     skrll       && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
   9041   1.1     skrll     return FALSE;
   9042   1.1     skrll 
   9043   1.1     skrll   return bfd_default_set_arch_mach (abfd, arch, machine);
   9044   1.1     skrll }
   9045   1.1     skrll 
   9046   1.1     skrll /* Find the nearest line to a particular section and offset,
   9047   1.6  christos    for error reporting.  */
   9048   1.1     skrll 
   9049   1.1     skrll bfd_boolean
   9050   1.1     skrll _bfd_elf_find_nearest_line (bfd *abfd,
   9051   1.1     skrll 			    asymbol **symbols,
   9052   1.6  christos 			    asection *section,
   9053   1.6  christos 			    bfd_vma offset,
   9054   1.5  christos 			    const char **filename_ptr,
   9055   1.1     skrll 			    const char **functionname_ptr,
   9056   1.1     skrll 			    unsigned int *line_ptr,
   9057   1.6  christos 			    unsigned int *discriminator_ptr)
   9058   1.1     skrll {
   9059   1.6  christos   bfd_boolean found;
   9060  1.15  christos 
   9061  1.15  christos   if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
   9062  1.15  christos 				     filename_ptr, functionname_ptr,
   9063  1.15  christos 				     line_ptr, discriminator_ptr,
   9064  1.15  christos 				     dwarf_debug_sections,
   9065  1.15  christos 				     &elf_tdata (abfd)->dwarf2_find_line_info))
   9066   1.1     skrll     return TRUE;
   9067   1.1     skrll 
   9068   1.6  christos   if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
   9069   1.6  christos 				     filename_ptr, functionname_ptr, line_ptr))
   9070   1.6  christos     {
   9071   1.1     skrll       if (!*functionname_ptr)
   9072   1.1     skrll 	_bfd_elf_find_function (abfd, symbols, section, offset,
   9073   1.1     skrll 				*filename_ptr ? NULL : filename_ptr,
   9074   1.1     skrll 				functionname_ptr);
   9075   1.1     skrll       return TRUE;
   9076   1.1     skrll     }
   9077   1.1     skrll 
   9078   1.1     skrll   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
   9079   1.1     skrll 					     &found, filename_ptr,
   9080   1.1     skrll 					     functionname_ptr, line_ptr,
   9081   1.1     skrll 					     &elf_tdata (abfd)->line_info))
   9082   1.1     skrll     return FALSE;
   9083   1.1     skrll   if (found && (*functionname_ptr || *line_ptr))
   9084   1.1     skrll     return TRUE;
   9085   1.6  christos 
   9086   1.6  christos   if (symbols == NULL)
   9087   1.1     skrll     return FALSE;
   9088   1.1     skrll 
   9089   1.1     skrll   if (! _bfd_elf_find_function (abfd, symbols, section, offset,
   9090   1.1     skrll 				filename_ptr, functionname_ptr))
   9091   1.1     skrll     return FALSE;
   9092   1.1     skrll 
   9093   1.1     skrll   *line_ptr = 0;
   9094   1.1     skrll   return TRUE;
   9095   1.1     skrll }
   9096   1.1     skrll 
   9097   1.1     skrll /* Find the line for a symbol.  */
   9098   1.1     skrll 
   9099   1.6  christos bfd_boolean
   9100   1.6  christos _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
   9101  1.15  christos 		    const char **filename_ptr, unsigned int *line_ptr)
   9102   1.6  christos {
   9103   1.1     skrll   return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
   9104   1.1     skrll 					filename_ptr, NULL, line_ptr, NULL,
   9105   1.1     skrll 					dwarf_debug_sections,
   9106   1.1     skrll 					&elf_tdata (abfd)->dwarf2_find_line_info);
   9107   1.1     skrll }
   9108   1.1     skrll 
   9109   1.1     skrll /* After a call to bfd_find_nearest_line, successive calls to
   9110   1.1     skrll    bfd_find_inliner_info can be used to get source information about
   9111   1.1     skrll    each level of function inlining that terminated at the address
   9112   1.1     skrll    passed to bfd_find_nearest_line.  Currently this is only supported
   9113   1.1     skrll    for DWARF2 with appropriate DWARF3 extensions. */
   9114   1.1     skrll 
   9115   1.1     skrll bfd_boolean
   9116   1.1     skrll _bfd_elf_find_inliner_info (bfd *abfd,
   9117   1.1     skrll 			    const char **filename_ptr,
   9118   1.1     skrll 			    const char **functionname_ptr,
   9119   1.1     skrll 			    unsigned int *line_ptr)
   9120   1.1     skrll {
   9121   1.1     skrll   bfd_boolean found;
   9122   1.1     skrll   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
   9123   1.1     skrll 					 functionname_ptr, line_ptr,
   9124   1.1     skrll 					 & elf_tdata (abfd)->dwarf2_find_line_info);
   9125   1.1     skrll   return found;
   9126   1.1     skrll }
   9127   1.1     skrll 
   9128   1.1     skrll int
   9129   1.1     skrll _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
   9130   1.6  christos {
   9131   1.1     skrll   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   9132   1.6  christos   int ret = bed->s->sizeof_ehdr;
   9133   1.1     skrll 
   9134   1.1     skrll   if (!bfd_link_relocatable (info))
   9135   1.1     skrll     {
   9136   1.1     skrll       bfd_size_type phdr_size = elf_program_header_size (abfd);
   9137   1.1     skrll 
   9138   1.1     skrll       if (phdr_size == (bfd_size_type) -1)
   9139   1.6  christos 	{
   9140   1.1     skrll 	  struct elf_segment_map *m;
   9141   1.1     skrll 
   9142   1.1     skrll 	  phdr_size = 0;
   9143   1.1     skrll 	  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   9144   1.1     skrll 	    phdr_size += bed->s->sizeof_phdr;
   9145   1.1     skrll 
   9146   1.6  christos 	  if (phdr_size == 0)
   9147   1.1     skrll 	    phdr_size = get_program_header_size (abfd, info);
   9148   1.1     skrll 	}
   9149   1.1     skrll 
   9150   1.1     skrll       elf_program_header_size (abfd) = phdr_size;
   9151   1.1     skrll       ret += phdr_size;
   9152   1.1     skrll     }
   9153   1.1     skrll 
   9154   1.1     skrll   return ret;
   9155   1.1     skrll }
   9156   1.1     skrll 
   9157   1.1     skrll bfd_boolean
   9158   1.1     skrll _bfd_elf_set_section_contents (bfd *abfd,
   9159   1.1     skrll 			       sec_ptr section,
   9160   1.1     skrll 			       const void *location,
   9161   1.6  christos 			       file_ptr offset,
   9162   1.1     skrll 			       bfd_size_type count)
   9163   1.1     skrll {
   9164   1.1     skrll   Elf_Internal_Shdr *hdr;
   9165   1.1     skrll   file_ptr pos;
   9166   1.1     skrll 
   9167   1.6  christos   if (! abfd->output_has_begun
   9168   1.6  christos       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   9169   1.6  christos     return FALSE;
   9170   1.1     skrll 
   9171   1.6  christos   if (!count)
   9172   1.6  christos     return TRUE;
   9173  1.15  christos 
   9174  1.15  christos   hdr = &elf_section_data (section)->this_hdr;
   9175  1.15  christos   if (hdr->sh_offset == (file_ptr) -1)
   9176  1.15  christos     {
   9177  1.15  christos       if (bfd_section_is_ctf (section))
   9178   1.6  christos 	/* Nothing to do with this section: the contents are generated
   9179   1.6  christos 	   later.  */
   9180   1.6  christos 	return TRUE;
   9181   1.6  christos 
   9182   1.6  christos       /* We must compress this section.  Write output to the buffer.  */
   9183   1.6  christos       unsigned char *contents = hdr->contents;
   9184   1.6  christos       if ((offset + count) > hdr->sh_size
   9185   1.6  christos 	  || (section->flags & SEC_ELF_COMPRESS) == 0
   9186   1.6  christos 	  || contents == NULL)
   9187   1.1     skrll 	abort ();
   9188   1.1     skrll       memcpy (contents + offset, location, count);
   9189   1.1     skrll       return TRUE;
   9190   1.1     skrll     }
   9191   1.1     skrll   pos = hdr->sh_offset + offset;
   9192   1.1     skrll   if (bfd_seek (abfd, pos, SEEK_SET) != 0
   9193   1.1     skrll       || bfd_bwrite (location, count, abfd) != count)
   9194   1.1     skrll     return FALSE;
   9195  1.13  christos 
   9196   1.1     skrll   return TRUE;
   9197   1.1     skrll }
   9198   1.1     skrll 
   9199   1.1     skrll bfd_boolean
   9200   1.1     skrll _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
   9201  1.13  christos 			   arelent *cache_ptr ATTRIBUTE_UNUSED,
   9202   1.1     skrll 			   Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
   9203   1.1     skrll {
   9204   1.1     skrll   abort ();
   9205   1.1     skrll   return FALSE;
   9206   1.1     skrll }
   9207   1.1     skrll 
   9208   1.1     skrll /* Try to convert a non-ELF reloc into an ELF one.  */
   9209   1.1     skrll 
   9210   1.1     skrll bfd_boolean
   9211   1.1     skrll _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
   9212   1.1     skrll {
   9213   1.1     skrll   /* Check whether we really have an ELF howto.  */
   9214   1.1     skrll 
   9215   1.1     skrll   if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
   9216   1.1     skrll     {
   9217   1.1     skrll       bfd_reloc_code_real_type code;
   9218   1.1     skrll       reloc_howto_type *howto;
   9219   1.1     skrll 
   9220   1.1     skrll       /* Alien reloc: Try to determine its type to replace it with an
   9221   1.1     skrll 	 equivalent ELF reloc.  */
   9222   1.1     skrll 
   9223   1.1     skrll       if (areloc->howto->pc_relative)
   9224   1.1     skrll 	{
   9225   1.1     skrll 	  switch (areloc->howto->bitsize)
   9226   1.1     skrll 	    {
   9227   1.1     skrll 	    case 8:
   9228   1.1     skrll 	      code = BFD_RELOC_8_PCREL;
   9229   1.1     skrll 	      break;
   9230   1.1     skrll 	    case 12:
   9231   1.1     skrll 	      code = BFD_RELOC_12_PCREL;
   9232   1.1     skrll 	      break;
   9233   1.1     skrll 	    case 16:
   9234   1.1     skrll 	      code = BFD_RELOC_16_PCREL;
   9235   1.1     skrll 	      break;
   9236   1.1     skrll 	    case 24:
   9237   1.1     skrll 	      code = BFD_RELOC_24_PCREL;
   9238   1.1     skrll 	      break;
   9239   1.1     skrll 	    case 32:
   9240   1.1     skrll 	      code = BFD_RELOC_32_PCREL;
   9241   1.1     skrll 	      break;
   9242   1.1     skrll 	    case 64:
   9243   1.1     skrll 	      code = BFD_RELOC_64_PCREL;
   9244   1.1     skrll 	      break;
   9245   1.1     skrll 	    default:
   9246   1.1     skrll 	      goto fail;
   9247  1.15  christos 	    }
   9248   1.1     skrll 
   9249   1.1     skrll 	  howto = bfd_reloc_type_lookup (abfd, code);
   9250   1.1     skrll 
   9251   1.1     skrll 	  if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
   9252   1.1     skrll 	    {
   9253   1.1     skrll 	      if (howto->pcrel_offset)
   9254   1.1     skrll 		areloc->addend += areloc->address;
   9255   1.1     skrll 	      else
   9256   1.1     skrll 		areloc->addend -= areloc->address; /* addend is unsigned!! */
   9257   1.1     skrll 	    }
   9258   1.1     skrll 	}
   9259   1.1     skrll       else
   9260   1.1     skrll 	{
   9261   1.1     skrll 	  switch (areloc->howto->bitsize)
   9262   1.1     skrll 	    {
   9263   1.1     skrll 	    case 8:
   9264   1.1     skrll 	      code = BFD_RELOC_8;
   9265   1.1     skrll 	      break;
   9266   1.1     skrll 	    case 14:
   9267   1.1     skrll 	      code = BFD_RELOC_14;
   9268   1.1     skrll 	      break;
   9269   1.1     skrll 	    case 16:
   9270   1.1     skrll 	      code = BFD_RELOC_16;
   9271   1.1     skrll 	      break;
   9272   1.1     skrll 	    case 26:
   9273   1.1     skrll 	      code = BFD_RELOC_26;
   9274   1.1     skrll 	      break;
   9275   1.1     skrll 	    case 32:
   9276   1.1     skrll 	      code = BFD_RELOC_32;
   9277   1.1     skrll 	      break;
   9278   1.1     skrll 	    case 64:
   9279   1.1     skrll 	      code = BFD_RELOC_64;
   9280   1.1     skrll 	      break;
   9281   1.1     skrll 	    default:
   9282   1.1     skrll 	      goto fail;
   9283   1.1     skrll 	    }
   9284   1.1     skrll 
   9285   1.1     skrll 	  howto = bfd_reloc_type_lookup (abfd, code);
   9286   1.1     skrll 	}
   9287   1.1     skrll 
   9288   1.1     skrll       if (howto)
   9289   1.1     skrll 	areloc->howto = howto;
   9290   1.1     skrll       else
   9291   1.1     skrll 	goto fail;
   9292   1.1     skrll     }
   9293  1.13  christos 
   9294  1.13  christos   return TRUE;
   9295  1.13  christos 
   9296  1.15  christos  fail:
   9297   1.1     skrll   /* xgettext:c-format */
   9298   1.1     skrll   _bfd_error_handler (_("%pB: %s unsupported"),
   9299   1.1     skrll 		      abfd, areloc->howto->name);
   9300   1.1     skrll   bfd_set_error (bfd_error_sorry);
   9301   1.1     skrll   return FALSE;
   9302   1.1     skrll }
   9303   1.5  christos 
   9304   1.5  christos bfd_boolean
   9305   1.1     skrll _bfd_elf_close_and_cleanup (bfd *abfd)
   9306   1.6  christos {
   9307   1.1     skrll   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   9308   1.5  christos   if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
   9309   1.1     skrll     {
   9310   1.1     skrll       if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
   9311   1.1     skrll 	_bfd_elf_strtab_free (elf_shstrtab (abfd));
   9312   1.1     skrll       _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
   9313   1.1     skrll     }
   9314   1.1     skrll 
   9315   1.1     skrll   return _bfd_generic_close_and_cleanup (abfd);
   9316   1.1     skrll }
   9317   1.1     skrll 
   9318   1.1     skrll /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
   9319   1.1     skrll    in the relocation's offset.  Thus we cannot allow any sort of sanity
   9320   1.1     skrll    range-checking to interfere.  There is nothing else to do in processing
   9321   1.1     skrll    this reloc.  */
   9322   1.1     skrll 
   9323   1.1     skrll bfd_reloc_status_type
   9324   1.1     skrll _bfd_elf_rel_vtable_reloc_fn
   9325   1.1     skrll   (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
   9326   1.1     skrll    struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
   9327   1.1     skrll    void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
   9328   1.1     skrll    bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
   9329   1.1     skrll {
   9330   1.1     skrll   return bfd_reloc_ok;
   9331   1.1     skrll }
   9332   1.1     skrll 
   9333   1.1     skrll /* Elf core file support.  Much of this only works on native
   9335   1.3  christos    toolchains, since we rely on knowing the
   9336   1.3  christos    machine-dependent procfs structure in order to pick
   9337   1.1     skrll    out details about the corefile.  */
   9338   1.1     skrll 
   9339   1.1     skrll #ifdef HAVE_SYS_PROCFS_H
   9340   1.3  christos /* Needed for new procfs interface on sparc-solaris.  */
   9341   1.3  christos # define _STRUCTURED_PROC 1
   9342   1.1     skrll # include <sys/procfs.h>
   9343   1.1     skrll #endif
   9344   1.1     skrll 
   9345   1.1     skrll /* Return a PID that identifies a "thread" for threaded cores, or the
   9346   1.3  christos    PID of the main process for non-threaded cores.  */
   9347   1.3  christos 
   9348   1.6  christos static int
   9349   1.3  christos elfcore_make_pid (bfd *abfd)
   9350   1.6  christos {
   9351   1.3  christos   int pid;
   9352   1.3  christos 
   9353   1.1     skrll   pid = elf_tdata (abfd)->core->lwpid;
   9354   1.1     skrll   if (pid == 0)
   9355   1.1     skrll     pid = elf_tdata (abfd)->core->pid;
   9356   1.1     skrll 
   9357   1.1     skrll   return pid;
   9358   1.1     skrll }
   9359   1.1     skrll 
   9360   1.1     skrll /* If there isn't a section called NAME, make one, using
   9361   1.1     skrll    data from SECT.  Note, this function will generate a
   9362   1.1     skrll    reference to NAME, so you shouldn't deallocate or
   9363   1.1     skrll    overwrite it.  */
   9364   1.1     skrll 
   9365   1.1     skrll static bfd_boolean
   9366   1.1     skrll elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
   9367   1.1     skrll {
   9368   1.1     skrll   asection *sect2;
   9369   1.1     skrll 
   9370   1.1     skrll   if (bfd_get_section_by_name (abfd, name) != NULL)
   9371   1.1     skrll     return TRUE;
   9372   1.1     skrll 
   9373   1.1     skrll   sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
   9374   1.1     skrll   if (sect2 == NULL)
   9375   1.1     skrll     return FALSE;
   9376   1.1     skrll 
   9377   1.1     skrll   sect2->size = sect->size;
   9378   1.1     skrll   sect2->filepos = sect->filepos;
   9379   1.1     skrll   sect2->alignment_power = sect->alignment_power;
   9380   1.1     skrll   return TRUE;
   9381   1.1     skrll }
   9382   1.1     skrll 
   9383   1.1     skrll /* Create a pseudosection containing SIZE bytes at FILEPOS.  This
   9384   1.9  christos    actually creates up to two pseudosections:
   9385   1.1     skrll    - For the single-threaded case, a section named NAME, unless
   9386   1.1     skrll      such a section already exists.
   9387   1.1     skrll    - For the multi-threaded case, a section named "NAME/PID", where
   9388   1.1     skrll      PID is elfcore_make_pid (abfd).
   9389   1.1     skrll    Both pseudosections have identical contents.  */
   9390   1.1     skrll bfd_boolean
   9391   1.1     skrll _bfd_elfcore_make_pseudosection (bfd *abfd,
   9392   1.1     skrll 				 char *name,
   9393   1.1     skrll 				 size_t size,
   9394   1.1     skrll 				 ufile_ptr filepos)
   9395   1.1     skrll {
   9396   1.1     skrll   char buf[100];
   9397   1.1     skrll   char *threaded_name;
   9398   1.1     skrll   size_t len;
   9399   1.1     skrll   asection *sect;
   9400   1.3  christos 
   9401   1.1     skrll   /* Build the section name.  */
   9402   1.1     skrll 
   9403   1.1     skrll   sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
   9404   1.1     skrll   len = strlen (buf) + 1;
   9405   1.1     skrll   threaded_name = (char *) bfd_alloc (abfd, len);
   9406   1.1     skrll   if (threaded_name == NULL)
   9407   1.1     skrll     return FALSE;
   9408   1.1     skrll   memcpy (threaded_name, buf, len);
   9409   1.1     skrll 
   9410   1.1     skrll   sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
   9411   1.1     skrll 					     SEC_HAS_CONTENTS);
   9412   1.1     skrll   if (sect == NULL)
   9413   1.1     skrll     return FALSE;
   9414   1.1     skrll   sect->size = size;
   9415   1.1     skrll   sect->filepos = filepos;
   9416   1.8  christos   sect->alignment_power = 2;
   9417   1.8  christos 
   9418  1.15  christos   return elfcore_maybe_make_sect (abfd, name, sect);
   9419   1.8  christos }
   9420   1.8  christos 
   9421  1.15  christos static bfd_boolean
   9422   1.8  christos elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
   9423   1.8  christos 				size_t offs)
   9424   1.8  christos {
   9425  1.15  christos   asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
   9426   1.8  christos 						       SEC_HAS_CONTENTS);
   9427   1.8  christos 
   9428   1.8  christos   if (sect == NULL)
   9429   1.8  christos     return FALSE;
   9430   1.8  christos 
   9431   1.8  christos   sect->size = note->descsz - offs;
   9432   1.8  christos   sect->filepos = note->descpos + offs;
   9433   1.1     skrll   sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   9434   1.1     skrll 
   9435   1.1     skrll   return TRUE;
   9436   1.1     skrll }
   9437   1.1     skrll 
   9438   1.1     skrll /* prstatus_t exists on:
   9439   1.1     skrll      solaris 2.5+
   9440   1.1     skrll      linux 2.[01] + glibc
   9441   1.1     skrll      unixware 4.2
   9442   1.1     skrll */
   9443   1.1     skrll 
   9444   1.1     skrll #if defined (HAVE_PRSTATUS_T)
   9445   1.1     skrll 
   9446   1.1     skrll static bfd_boolean
   9447   1.1     skrll elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   9448   1.1     skrll {
   9449   1.1     skrll   size_t size;
   9450   1.1     skrll   int offset;
   9451   1.1     skrll 
   9452   1.1     skrll   if (note->descsz == sizeof (prstatus_t))
   9453   1.1     skrll     {
   9454   1.1     skrll       prstatus_t prstat;
   9455   1.1     skrll 
   9456   1.1     skrll       size = sizeof (prstat.pr_reg);
   9457   1.6  christos       offset   = offsetof (prstatus_t, pr_reg);
   9458   1.6  christos       memcpy (&prstat, note->descdata, sizeof (prstat));
   9459   1.6  christos 
   9460   1.6  christos       /* Do not overwrite the core signal if it
   9461   1.1     skrll 	 has already been set by another thread.  */
   9462   1.1     skrll       if (elf_tdata (abfd)->core->signal == 0)
   9463   1.1     skrll 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   9464   1.1     skrll       if (elf_tdata (abfd)->core->pid == 0)
   9465   1.1     skrll 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   9466   1.1     skrll 
   9467   1.1     skrll       /* pr_who exists on:
   9468   1.1     skrll 	 solaris 2.5+
   9469   1.6  christos 	 unixware 4.2
   9470   1.3  christos 	 pr_who doesn't exist on:
   9471   1.6  christos 	 linux 2.[01]
   9472   1.1     skrll 	 */
   9473   1.1     skrll #if defined (HAVE_PRSTATUS_T_PR_WHO)
   9474   1.1     skrll       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   9475   1.1     skrll #else
   9476   1.1     skrll       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   9477   1.1     skrll #endif
   9478   1.1     skrll     }
   9479   1.1     skrll #if defined (HAVE_PRSTATUS32_T)
   9480   1.1     skrll   else if (note->descsz == sizeof (prstatus32_t))
   9481   1.1     skrll     {
   9482   1.1     skrll       /* 64-bit host, 32-bit corefile */
   9483   1.1     skrll       prstatus32_t prstat;
   9484   1.1     skrll 
   9485   1.1     skrll       size = sizeof (prstat.pr_reg);
   9486   1.6  christos       offset   = offsetof (prstatus32_t, pr_reg);
   9487   1.6  christos       memcpy (&prstat, note->descdata, sizeof (prstat));
   9488   1.6  christos 
   9489   1.6  christos       /* Do not overwrite the core signal if it
   9490   1.1     skrll 	 has already been set by another thread.  */
   9491   1.1     skrll       if (elf_tdata (abfd)->core->signal == 0)
   9492   1.1     skrll 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   9493   1.1     skrll       if (elf_tdata (abfd)->core->pid == 0)
   9494   1.1     skrll 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   9495   1.1     skrll 
   9496   1.1     skrll       /* pr_who exists on:
   9497   1.1     skrll 	 solaris 2.5+
   9498   1.6  christos 	 unixware 4.2
   9499   1.3  christos 	 pr_who doesn't exist on:
   9500   1.6  christos 	 linux 2.[01]
   9501   1.1     skrll 	 */
   9502   1.1     skrll #if defined (HAVE_PRSTATUS32_T_PR_WHO)
   9503   1.1     skrll       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   9504   1.1     skrll #else
   9505   1.1     skrll       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   9506   1.1     skrll #endif
   9507   1.1     skrll     }
   9508   1.1     skrll #endif /* HAVE_PRSTATUS32_T */
   9509   1.1     skrll   else
   9510   1.1     skrll     {
   9511   1.1     skrll       /* Fail - we don't know how to handle any other
   9512   1.1     skrll 	 note size (ie. data object type).  */
   9513   1.1     skrll       return TRUE;
   9514   1.1     skrll     }
   9515   1.1     skrll 
   9516   1.1     skrll   /* Make a ".reg/999" section and a ".reg" section.  */
   9517   1.1     skrll   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   9518   1.1     skrll 					  size, note->descpos + offset);
   9519   1.1     skrll }
   9520   1.1     skrll #endif /* defined (HAVE_PRSTATUS_T) */
   9521   1.1     skrll 
   9522   1.1     skrll /* Create a pseudosection containing the exact contents of NOTE.  */
   9523   1.1     skrll static bfd_boolean
   9524   1.1     skrll elfcore_make_note_pseudosection (bfd *abfd,
   9525   1.1     skrll 				 char *name,
   9526   1.1     skrll 				 Elf_Internal_Note *note)
   9527   1.1     skrll {
   9528   1.1     skrll   return _bfd_elfcore_make_pseudosection (abfd, name,
   9529   1.1     skrll 					  note->descsz, note->descpos);
   9530   1.1     skrll }
   9531   1.1     skrll 
   9532   1.1     skrll /* There isn't a consistent prfpregset_t across platforms,
   9533   1.1     skrll    but it doesn't matter, because we don't have to pick this
   9534   1.1     skrll    data structure apart.  */
   9535   1.1     skrll 
   9536   1.1     skrll static bfd_boolean
   9537   1.1     skrll elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
   9538   1.1     skrll {
   9539   1.1     skrll   return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   9540   1.1     skrll }
   9541   1.1     skrll 
   9542   1.1     skrll /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
   9543   1.1     skrll    type of NT_PRXFPREG.  Just include the whole note's contents
   9544   1.1     skrll    literally.  */
   9545   1.1     skrll 
   9546   1.1     skrll static bfd_boolean
   9547   1.3  christos elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
   9548   1.3  christos {
   9549   1.3  christos   return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
   9550   1.3  christos }
   9551   1.3  christos 
   9552   1.3  christos /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
   9553   1.3  christos    with a note type of NT_X86_XSTATE.  Just include the whole note's
   9554   1.3  christos    contents literally.  */
   9555   1.3  christos 
   9556   1.3  christos static bfd_boolean
   9557   1.1     skrll elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
   9558   1.1     skrll {
   9559   1.1     skrll   return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
   9560   1.1     skrll }
   9561   1.1     skrll 
   9562   1.1     skrll static bfd_boolean
   9563   1.1     skrll elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
   9564   1.1     skrll {
   9565   1.1     skrll   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
   9566   1.1     skrll }
   9567   1.1     skrll 
   9568   1.1     skrll static bfd_boolean
   9569   1.3  christos elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
   9570  1.15  christos {
   9571  1.15  christos   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
   9572  1.15  christos }
   9573  1.15  christos 
   9574  1.15  christos static bfd_boolean
   9575  1.15  christos elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
   9576  1.15  christos {
   9577  1.15  christos   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
   9578  1.15  christos }
   9579  1.15  christos 
   9580  1.15  christos static bfd_boolean
   9581  1.15  christos elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
   9582  1.15  christos {
   9583  1.15  christos   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
   9584  1.15  christos }
   9585  1.15  christos 
   9586  1.15  christos static bfd_boolean
   9587  1.15  christos elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
   9588  1.15  christos {
   9589  1.15  christos   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
   9590  1.15  christos }
   9591  1.15  christos 
   9592  1.15  christos static bfd_boolean
   9593  1.15  christos elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
   9594  1.15  christos {
   9595  1.15  christos   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
   9596  1.15  christos }
   9597  1.15  christos 
   9598  1.15  christos static bfd_boolean
   9599  1.15  christos elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
   9600  1.15  christos {
   9601  1.15  christos   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
   9602  1.15  christos }
   9603  1.15  christos 
   9604  1.15  christos static bfd_boolean
   9605  1.15  christos elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
   9606  1.15  christos {
   9607  1.15  christos   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
   9608  1.15  christos }
   9609  1.15  christos 
   9610  1.15  christos static bfd_boolean
   9611  1.15  christos elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
   9612  1.15  christos {
   9613  1.15  christos   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
   9614  1.15  christos }
   9615  1.15  christos 
   9616  1.15  christos static bfd_boolean
   9617  1.15  christos elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
   9618  1.15  christos {
   9619  1.15  christos   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
   9620  1.15  christos }
   9621  1.15  christos 
   9622  1.15  christos static bfd_boolean
   9623  1.15  christos elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
   9624  1.15  christos {
   9625  1.15  christos   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
   9626  1.15  christos }
   9627  1.15  christos 
   9628  1.15  christos static bfd_boolean
   9629  1.15  christos elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
   9630  1.15  christos {
   9631  1.15  christos   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
   9632  1.15  christos }
   9633  1.15  christos 
   9634  1.15  christos static bfd_boolean
   9635  1.15  christos elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
   9636  1.15  christos {
   9637  1.15  christos   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
   9638  1.15  christos }
   9639  1.15  christos 
   9640  1.15  christos static bfd_boolean
   9641  1.15  christos elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
   9642  1.15  christos {
   9643  1.15  christos   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
   9644  1.15  christos }
   9645  1.15  christos 
   9646  1.15  christos static bfd_boolean
   9647  1.15  christos elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
   9648   1.3  christos {
   9649   1.3  christos   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
   9650   1.3  christos }
   9651   1.3  christos 
   9652   1.3  christos static bfd_boolean
   9653   1.3  christos elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
   9654   1.3  christos {
   9655   1.3  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
   9656   1.3  christos }
   9657   1.3  christos 
   9658   1.3  christos static bfd_boolean
   9659   1.3  christos elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
   9660   1.3  christos {
   9661   1.3  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
   9662   1.3  christos }
   9663   1.3  christos 
   9664   1.3  christos static bfd_boolean
   9665   1.3  christos elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
   9666   1.3  christos {
   9667   1.3  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
   9668   1.3  christos }
   9669   1.3  christos 
   9670   1.3  christos static bfd_boolean
   9671   1.3  christos elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
   9672   1.3  christos {
   9673   1.3  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
   9674   1.3  christos }
   9675   1.3  christos 
   9676   1.3  christos static bfd_boolean
   9677   1.3  christos elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
   9678   1.3  christos {
   9679   1.3  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
   9680   1.3  christos }
   9681   1.3  christos 
   9682   1.3  christos static bfd_boolean
   9683   1.5  christos elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
   9684   1.5  christos {
   9685   1.5  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
   9686   1.5  christos }
   9687   1.5  christos 
   9688   1.5  christos static bfd_boolean
   9689   1.5  christos elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
   9690   1.5  christos {
   9691   1.5  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
   9692   1.5  christos }
   9693   1.5  christos 
   9694   1.5  christos static bfd_boolean
   9695   1.5  christos elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
   9696   1.6  christos {
   9697   1.6  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
   9698   1.6  christos }
   9699   1.6  christos 
   9700   1.6  christos static bfd_boolean
   9701   1.6  christos elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
   9702   1.6  christos {
   9703   1.6  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
   9704   1.6  christos }
   9705   1.6  christos 
   9706   1.6  christos static bfd_boolean
   9707   1.6  christos elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
   9708   1.6  christos {
   9709   1.6  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
   9710   1.6  christos }
   9711   1.6  christos 
   9712   1.6  christos static bfd_boolean
   9713   1.6  christos elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
   9714   1.9  christos {
   9715   1.9  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
   9716   1.9  christos }
   9717   1.9  christos 
   9718   1.9  christos static bfd_boolean
   9719   1.9  christos elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
   9720   1.9  christos {
   9721   1.9  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
   9722   1.9  christos }
   9723   1.9  christos 
   9724   1.9  christos static bfd_boolean
   9725   1.9  christos elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
   9726   1.5  christos {
   9727   1.5  christos   return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
   9728   1.5  christos }
   9729   1.5  christos 
   9730   1.5  christos static bfd_boolean
   9731   1.6  christos elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
   9732   1.6  christos {
   9733   1.6  christos   return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
   9734   1.6  christos }
   9735   1.6  christos 
   9736   1.6  christos static bfd_boolean
   9737   1.6  christos elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
   9738   1.6  christos {
   9739   1.6  christos   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
   9740   1.6  christos }
   9741   1.6  christos 
   9742   1.6  christos static bfd_boolean
   9743   1.6  christos elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
   9744   1.6  christos {
   9745   1.6  christos   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
   9746   1.6  christos }
   9747   1.6  christos 
   9748   1.6  christos static bfd_boolean
   9749  1.15  christos elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
   9750  1.15  christos {
   9751  1.15  christos   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
   9752  1.15  christos }
   9753  1.15  christos 
   9754  1.15  christos static bfd_boolean
   9755  1.15  christos elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
   9756  1.15  christos {
   9757  1.15  christos   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
   9758  1.15  christos }
   9759  1.15  christos 
   9760  1.15  christos static bfd_boolean
   9761   1.1     skrll elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
   9762   1.1     skrll {
   9763   1.1     skrll   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
   9764   1.1     skrll }
   9765   1.1     skrll 
   9766   1.1     skrll #if defined (HAVE_PRPSINFO_T)
   9767   1.1     skrll typedef prpsinfo_t   elfcore_psinfo_t;
   9768   1.1     skrll #if defined (HAVE_PRPSINFO32_T)		/* Sparc64 cross Sparc32 */
   9769   1.1     skrll typedef prpsinfo32_t elfcore_psinfo32_t;
   9770   1.1     skrll #endif
   9771   1.1     skrll #endif
   9772   1.1     skrll 
   9773   1.1     skrll #if defined (HAVE_PSINFO_T)
   9774   1.1     skrll typedef psinfo_t   elfcore_psinfo_t;
   9775   1.1     skrll #if defined (HAVE_PSINFO32_T)		/* Sparc64 cross Sparc32 */
   9776   1.1     skrll typedef psinfo32_t elfcore_psinfo32_t;
   9777   1.1     skrll #endif
   9778   1.1     skrll #endif
   9779   1.1     skrll 
   9780   1.1     skrll /* return a malloc'ed copy of a string at START which is at
   9781   1.1     skrll    most MAX bytes long, possibly without a terminating '\0'.
   9782   1.1     skrll    the copy will always have a terminating '\0'.  */
   9783   1.3  christos 
   9784   1.1     skrll char *
   9785   1.1     skrll _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
   9786   1.1     skrll {
   9787   1.1     skrll   char *dups;
   9788   1.1     skrll   char *end = (char *) memchr (start, '\0', max);
   9789   1.1     skrll   size_t len;
   9790   1.1     skrll 
   9791   1.3  christos   if (end == NULL)
   9792   1.1     skrll     len = max;
   9793   1.1     skrll   else
   9794   1.1     skrll     len = end - start;
   9795   1.1     skrll 
   9796   1.1     skrll   dups = (char *) bfd_alloc (abfd, len + 1);
   9797   1.1     skrll   if (dups == NULL)
   9798   1.1     skrll     return NULL;
   9799   1.1     skrll 
   9800   1.1     skrll   memcpy (dups, start, len);
   9801   1.1     skrll   dups[len] = '\0';
   9802   1.1     skrll 
   9803   1.1     skrll   return dups;
   9804   1.1     skrll }
   9805   1.1     skrll 
   9806   1.1     skrll #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   9807   1.1     skrll static bfd_boolean
   9808   1.1     skrll elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   9809   1.1     skrll {
   9810   1.1     skrll   if (note->descsz == sizeof (elfcore_psinfo_t))
   9811   1.3  christos     {
   9812   1.6  christos       elfcore_psinfo_t psinfo;
   9813   1.3  christos 
   9814   1.6  christos       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   9815   1.1     skrll 
   9816   1.1     skrll #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
   9817   1.1     skrll       elf_tdata (abfd)->core->pid = psinfo.pr_pid;
   9818   1.6  christos #endif
   9819   1.1     skrll       elf_tdata (abfd)->core->program
   9820   1.1     skrll 	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
   9821   1.1     skrll 				sizeof (psinfo.pr_fname));
   9822   1.1     skrll 
   9823   1.1     skrll       elf_tdata (abfd)->core->command
   9824   1.1     skrll 	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
   9825   1.1     skrll 				sizeof (psinfo.pr_psargs));
   9826   1.1     skrll     }
   9827   1.1     skrll #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   9828   1.1     skrll   else if (note->descsz == sizeof (elfcore_psinfo32_t))
   9829   1.1     skrll     {
   9830   1.3  christos       /* 64-bit host, 32-bit corefile */
   9831   1.6  christos       elfcore_psinfo32_t psinfo;
   9832   1.3  christos 
   9833   1.6  christos       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   9834   1.1     skrll 
   9835   1.1     skrll #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
   9836   1.1     skrll       elf_tdata (abfd)->core->pid = psinfo.pr_pid;
   9837   1.6  christos #endif
   9838   1.1     skrll       elf_tdata (abfd)->core->program
   9839   1.1     skrll 	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
   9840   1.1     skrll 				sizeof (psinfo.pr_fname));
   9841   1.1     skrll 
   9842   1.1     skrll       elf_tdata (abfd)->core->command
   9843   1.1     skrll 	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
   9844   1.1     skrll 				sizeof (psinfo.pr_psargs));
   9845   1.1     skrll     }
   9846   1.1     skrll #endif
   9847   1.1     skrll 
   9848   1.1     skrll   else
   9849   1.1     skrll     {
   9850   1.1     skrll       /* Fail - we don't know how to handle any other
   9851   1.1     skrll 	 note size (ie. data object type).  */
   9852   1.1     skrll       return TRUE;
   9853   1.1     skrll     }
   9854   1.1     skrll 
   9855   1.6  christos   /* Note that for some reason, a spurious space is tacked
   9856   1.1     skrll      onto the end of the args in some (at least one anyway)
   9857   1.1     skrll      implementations, so strip it off if it exists.  */
   9858   1.1     skrll 
   9859   1.1     skrll   {
   9860   1.1     skrll     char *command = elf_tdata (abfd)->core->command;
   9861   1.1     skrll     int n = strlen (command);
   9862   1.1     skrll 
   9863   1.1     skrll     if (0 < n && command[n - 1] == ' ')
   9864   1.1     skrll       command[n - 1] = '\0';
   9865   1.1     skrll   }
   9866   1.1     skrll 
   9867   1.1     skrll   return TRUE;
   9868   1.1     skrll }
   9869   1.1     skrll #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
   9870   1.1     skrll 
   9871   1.1     skrll #if defined (HAVE_PSTATUS_T)
   9872   1.1     skrll static bfd_boolean
   9873   1.1     skrll elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
   9874   1.1     skrll {
   9875   1.1     skrll   if (note->descsz == sizeof (pstatus_t)
   9876   1.1     skrll #if defined (HAVE_PXSTATUS_T)
   9877   1.1     skrll       || note->descsz == sizeof (pxstatus_t)
   9878   1.1     skrll #endif
   9879   1.1     skrll       )
   9880   1.6  christos     {
   9881   1.1     skrll       pstatus_t pstat;
   9882   1.1     skrll 
   9883   1.1     skrll       memcpy (&pstat, note->descdata, sizeof (pstat));
   9884   1.1     skrll 
   9885   1.1     skrll       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   9886   1.1     skrll     }
   9887   1.1     skrll #if defined (HAVE_PSTATUS32_T)
   9888   1.1     skrll   else if (note->descsz == sizeof (pstatus32_t))
   9889   1.1     skrll     {
   9890   1.6  christos       /* 64-bit host, 32-bit corefile */
   9891   1.1     skrll       pstatus32_t pstat;
   9892   1.1     skrll 
   9893   1.1     skrll       memcpy (&pstat, note->descdata, sizeof (pstat));
   9894   1.1     skrll 
   9895   1.1     skrll       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   9896   1.1     skrll     }
   9897   1.1     skrll #endif
   9898   1.1     skrll   /* Could grab some more details from the "representative"
   9899   1.1     skrll      lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
   9900   1.1     skrll      NT_LWPSTATUS note, presumably.  */
   9901   1.1     skrll 
   9902   1.1     skrll   return TRUE;
   9903   1.1     skrll }
   9904   1.1     skrll #endif /* defined (HAVE_PSTATUS_T) */
   9905   1.1     skrll 
   9906   1.1     skrll #if defined (HAVE_LWPSTATUS_T)
   9907   1.1     skrll static bfd_boolean
   9908   1.1     skrll elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
   9909   1.1     skrll {
   9910   1.1     skrll   lwpstatus_t lwpstat;
   9911   1.1     skrll   char buf[100];
   9912   1.1     skrll   char *name;
   9913   1.1     skrll   size_t len;
   9914   1.1     skrll   asection *sect;
   9915   1.1     skrll 
   9916   1.1     skrll   if (note->descsz != sizeof (lwpstat)
   9917   1.1     skrll #if defined (HAVE_LWPXSTATUS_T)
   9918   1.1     skrll       && note->descsz != sizeof (lwpxstatus_t)
   9919   1.1     skrll #endif
   9920   1.6  christos       )
   9921   1.3  christos     return TRUE;
   9922   1.3  christos 
   9923   1.6  christos   memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
   9924   1.6  christos 
   9925   1.1     skrll   elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
   9926   1.1     skrll   /* Do not overwrite the core signal if it has already been set by
   9927   1.1     skrll      another thread.  */
   9928   1.1     skrll   if (elf_tdata (abfd)->core->signal == 0)
   9929   1.1     skrll     elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
   9930   1.1     skrll 
   9931   1.1     skrll   /* Make a ".reg/999" section.  */
   9932   1.1     skrll 
   9933   1.1     skrll   sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
   9934   1.1     skrll   len = strlen (buf) + 1;
   9935   1.1     skrll   name = bfd_alloc (abfd, len);
   9936   1.1     skrll   if (name == NULL)
   9937   1.1     skrll     return FALSE;
   9938   1.1     skrll   memcpy (name, buf, len);
   9939   1.1     skrll 
   9940   1.1     skrll   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   9941   1.1     skrll   if (sect == NULL)
   9942   1.1     skrll     return FALSE;
   9943   1.1     skrll 
   9944   1.1     skrll #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   9945   1.1     skrll   sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
   9946   1.1     skrll   sect->filepos = note->descpos
   9947   1.1     skrll     + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
   9948   1.1     skrll #endif
   9949   1.1     skrll 
   9950   1.1     skrll #if defined (HAVE_LWPSTATUS_T_PR_REG)
   9951   1.1     skrll   sect->size = sizeof (lwpstat.pr_reg);
   9952   1.1     skrll   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
   9953   1.1     skrll #endif
   9954   1.1     skrll 
   9955   1.1     skrll   sect->alignment_power = 2;
   9956   1.1     skrll 
   9957   1.1     skrll   if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
   9958   1.1     skrll     return FALSE;
   9959   1.1     skrll 
   9960   1.1     skrll   /* Make a ".reg2/999" section */
   9961   1.1     skrll 
   9962   1.1     skrll   sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
   9963   1.1     skrll   len = strlen (buf) + 1;
   9964   1.1     skrll   name = bfd_alloc (abfd, len);
   9965   1.1     skrll   if (name == NULL)
   9966   1.1     skrll     return FALSE;
   9967   1.1     skrll   memcpy (name, buf, len);
   9968   1.1     skrll 
   9969   1.1     skrll   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   9970   1.1     skrll   if (sect == NULL)
   9971   1.1     skrll     return FALSE;
   9972   1.1     skrll 
   9973   1.1     skrll #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   9974   1.1     skrll   sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
   9975   1.1     skrll   sect->filepos = note->descpos
   9976   1.1     skrll     + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
   9977   1.1     skrll #endif
   9978   1.1     skrll 
   9979   1.1     skrll #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
   9980   1.1     skrll   sect->size = sizeof (lwpstat.pr_fpreg);
   9981   1.1     skrll   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
   9982   1.1     skrll #endif
   9983   1.1     skrll 
   9984   1.1     skrll   sect->alignment_power = 2;
   9985   1.1     skrll 
   9986   1.1     skrll   return elfcore_maybe_make_sect (abfd, ".reg2", sect);
   9987   1.1     skrll }
   9988   1.1     skrll #endif /* defined (HAVE_LWPSTATUS_T) */
   9989   1.1     skrll 
   9990   1.1     skrll static bfd_boolean
   9991   1.1     skrll elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
   9992   1.1     skrll {
   9993   1.1     skrll   char buf[30];
   9994   1.1     skrll   char *name;
   9995   1.1     skrll   size_t len;
   9996   1.1     skrll   asection *sect;
   9997   1.1     skrll   int type;
   9998   1.1     skrll   int is_active_thread;
   9999   1.1     skrll   bfd_vma base_addr;
   10000   1.1     skrll 
   10001   1.1     skrll   if (note->descsz < 728)
   10002   1.1     skrll     return TRUE;
   10003   1.1     skrll 
   10004   1.1     skrll   if (! CONST_STRNEQ (note->namedata, "win32"))
   10005   1.1     skrll     return TRUE;
   10006   1.1     skrll 
   10007   1.6  christos   type = bfd_get_32 (abfd, note->descdata);
   10008   1.1     skrll 
   10009   1.6  christos   switch (type)
   10010   1.1     skrll     {
   10011   1.6  christos     case 1 /* NOTE_INFO_PROCESS */:
   10012   1.1     skrll       /* FIXME: need to add ->core->command.  */
   10013   1.1     skrll       /* process_info.pid */
   10014   1.1     skrll       elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
   10015   1.1     skrll       /* process_info.signal */
   10016   1.1     skrll       elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
   10017   1.1     skrll       break;
   10018   1.1     skrll 
   10019   1.1     skrll     case 2 /* NOTE_INFO_THREAD */:
   10020   1.3  christos       /* Make a ".reg/999" section.  */
   10021   1.1     skrll       /* thread_info.tid */
   10022   1.1     skrll       sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
   10023   1.1     skrll 
   10024   1.1     skrll       len = strlen (buf) + 1;
   10025   1.1     skrll       name = (char *) bfd_alloc (abfd, len);
   10026   1.1     skrll       if (name == NULL)
   10027   1.1     skrll 	return FALSE;
   10028   1.1     skrll 
   10029   1.1     skrll       memcpy (name, buf, len);
   10030   1.1     skrll 
   10031   1.1     skrll       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10032   1.1     skrll       if (sect == NULL)
   10033   1.1     skrll 	return FALSE;
   10034   1.1     skrll 
   10035   1.1     skrll       /* sizeof (thread_info.thread_context) */
   10036   1.1     skrll       sect->size = 716;
   10037   1.1     skrll       /* offsetof (thread_info.thread_context) */
   10038   1.1     skrll       sect->filepos = note->descpos + 12;
   10039   1.1     skrll       sect->alignment_power = 2;
   10040   1.1     skrll 
   10041   1.1     skrll       /* thread_info.is_active_thread */
   10042   1.1     skrll       is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
   10043   1.1     skrll 
   10044   1.1     skrll       if (is_active_thread)
   10045   1.1     skrll 	if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
   10046   1.1     skrll 	  return FALSE;
   10047   1.1     skrll       break;
   10048   1.1     skrll 
   10049   1.1     skrll     case 3 /* NOTE_INFO_MODULE */:
   10050   1.1     skrll       /* Make a ".module/xxxxxxxx" section.  */
   10051   1.3  christos       /* module_info.base_address */
   10052   1.1     skrll       base_addr = bfd_get_32 (abfd, note->descdata + 4);
   10053   1.1     skrll       sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
   10054   1.1     skrll 
   10055   1.1     skrll       len = strlen (buf) + 1;
   10056   1.1     skrll       name = (char *) bfd_alloc (abfd, len);
   10057   1.1     skrll       if (name == NULL)
   10058   1.1     skrll 	return FALSE;
   10059   1.1     skrll 
   10060   1.1     skrll       memcpy (name, buf, len);
   10061   1.1     skrll 
   10062   1.1     skrll       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10063   1.1     skrll 
   10064   1.1     skrll       if (sect == NULL)
   10065   1.1     skrll 	return FALSE;
   10066   1.1     skrll 
   10067   1.1     skrll       sect->size = note->descsz;
   10068   1.1     skrll       sect->filepos = note->descpos;
   10069   1.1     skrll       sect->alignment_power = 2;
   10070   1.1     skrll       break;
   10071   1.1     skrll 
   10072   1.1     skrll     default:
   10073   1.1     skrll       return TRUE;
   10074   1.1     skrll     }
   10075   1.1     skrll 
   10076   1.1     skrll   return TRUE;
   10077   1.1     skrll }
   10078   1.1     skrll 
   10079   1.1     skrll static bfd_boolean
   10080   1.1     skrll elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
   10081   1.1     skrll {
   10082   1.1     skrll   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   10083   1.1     skrll 
   10084   1.1     skrll   switch (note->type)
   10085   1.1     skrll     {
   10086   1.1     skrll     default:
   10087   1.1     skrll       return TRUE;
   10088   1.1     skrll 
   10089   1.1     skrll     case NT_PRSTATUS:
   10090   1.1     skrll       if (bed->elf_backend_grok_prstatus)
   10091   1.1     skrll 	if ((*bed->elf_backend_grok_prstatus) (abfd, note))
   10092   1.1     skrll 	  return TRUE;
   10093   1.1     skrll #if defined (HAVE_PRSTATUS_T)
   10094   1.1     skrll       return elfcore_grok_prstatus (abfd, note);
   10095   1.1     skrll #else
   10096   1.1     skrll       return TRUE;
   10097   1.1     skrll #endif
   10098   1.1     skrll 
   10099   1.1     skrll #if defined (HAVE_PSTATUS_T)
   10100   1.1     skrll     case NT_PSTATUS:
   10101   1.1     skrll       return elfcore_grok_pstatus (abfd, note);
   10102   1.1     skrll #endif
   10103   1.1     skrll 
   10104   1.1     skrll #if defined (HAVE_LWPSTATUS_T)
   10105   1.1     skrll     case NT_LWPSTATUS:
   10106   1.1     skrll       return elfcore_grok_lwpstatus (abfd, note);
   10107   1.1     skrll #endif
   10108   1.1     skrll 
   10109   1.1     skrll     case NT_FPREGSET:		/* FIXME: rename to NT_PRFPREG */
   10110   1.1     skrll       return elfcore_grok_prfpreg (abfd, note);
   10111   1.1     skrll 
   10112   1.1     skrll     case NT_WIN32PSTATUS:
   10113   1.1     skrll       return elfcore_grok_win32pstatus (abfd, note);
   10114   1.1     skrll 
   10115   1.1     skrll     case NT_PRXFPREG:		/* Linux SSE extension */
   10116   1.1     skrll       if (note->namesz == 6
   10117   1.3  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10118   1.3  christos 	return elfcore_grok_prxfpreg (abfd, note);
   10119   1.3  christos       else
   10120   1.3  christos 	return TRUE;
   10121   1.3  christos 
   10122   1.3  christos     case NT_X86_XSTATE:		/* Linux XSAVE extension */
   10123   1.3  christos       if (note->namesz == 6
   10124   1.1     skrll 	  && strcmp (note->namedata, "LINUX") == 0)
   10125   1.1     skrll 	return elfcore_grok_xstatereg (abfd, note);
   10126   1.1     skrll       else
   10127   1.1     skrll 	return TRUE;
   10128   1.1     skrll 
   10129   1.1     skrll     case NT_PPC_VMX:
   10130   1.1     skrll       if (note->namesz == 6
   10131   1.1     skrll 	  && strcmp (note->namedata, "LINUX") == 0)
   10132   1.1     skrll 	return elfcore_grok_ppc_vmx (abfd, note);
   10133   1.9  christos       else
   10134   1.9  christos 	return TRUE;
   10135   1.1     skrll 
   10136   1.9  christos     case NT_PPC_VSX:
   10137   1.1     skrll       if (note->namesz == 6
   10138  1.15  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10139  1.15  christos 	return elfcore_grok_ppc_vsx (abfd, note);
   10140  1.15  christos       else
   10141  1.15  christos 	return TRUE;
   10142  1.15  christos 
   10143  1.15  christos     case NT_PPC_TAR:
   10144  1.15  christos       if (note->namesz == 6
   10145  1.15  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10146  1.15  christos 	return elfcore_grok_ppc_tar (abfd, note);
   10147  1.15  christos       else
   10148  1.15  christos 	return TRUE;
   10149  1.15  christos 
   10150  1.15  christos     case NT_PPC_PPR:
   10151  1.15  christos       if (note->namesz == 6
   10152  1.15  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10153  1.15  christos 	return elfcore_grok_ppc_ppr (abfd, note);
   10154  1.15  christos       else
   10155  1.15  christos 	return TRUE;
   10156  1.15  christos 
   10157  1.15  christos     case NT_PPC_DSCR:
   10158  1.15  christos       if (note->namesz == 6
   10159  1.15  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10160  1.15  christos 	return elfcore_grok_ppc_dscr (abfd, note);
   10161  1.15  christos       else
   10162  1.15  christos 	return TRUE;
   10163  1.15  christos 
   10164  1.15  christos     case NT_PPC_EBB:
   10165  1.15  christos       if (note->namesz == 6
   10166  1.15  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10167  1.15  christos 	return elfcore_grok_ppc_ebb (abfd, note);
   10168  1.15  christos       else
   10169  1.15  christos 	return TRUE;
   10170  1.15  christos 
   10171  1.15  christos     case NT_PPC_PMU:
   10172  1.15  christos       if (note->namesz == 6
   10173  1.15  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10174  1.15  christos 	return elfcore_grok_ppc_pmu (abfd, note);
   10175  1.15  christos       else
   10176  1.15  christos 	return TRUE;
   10177  1.15  christos 
   10178  1.15  christos     case NT_PPC_TM_CGPR:
   10179  1.15  christos       if (note->namesz == 6
   10180  1.15  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10181  1.15  christos 	return elfcore_grok_ppc_tm_cgpr (abfd, note);
   10182  1.15  christos       else
   10183  1.15  christos 	return TRUE;
   10184  1.15  christos 
   10185  1.15  christos     case NT_PPC_TM_CFPR:
   10186  1.15  christos       if (note->namesz == 6
   10187  1.15  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10188  1.15  christos 	return elfcore_grok_ppc_tm_cfpr (abfd, note);
   10189  1.15  christos       else
   10190  1.15  christos 	return TRUE;
   10191  1.15  christos 
   10192  1.15  christos     case NT_PPC_TM_CVMX:
   10193  1.15  christos       if (note->namesz == 6
   10194  1.15  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10195  1.15  christos 	return elfcore_grok_ppc_tm_cvmx (abfd, note);
   10196  1.15  christos       else
   10197  1.15  christos 	return TRUE;
   10198  1.15  christos 
   10199  1.15  christos     case NT_PPC_TM_CVSX:
   10200  1.15  christos       if (note->namesz == 6
   10201  1.15  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10202  1.15  christos 	return elfcore_grok_ppc_tm_cvsx (abfd, note);
   10203  1.15  christos       else
   10204  1.15  christos 	return TRUE;
   10205  1.15  christos 
   10206  1.15  christos     case NT_PPC_TM_SPR:
   10207  1.15  christos       if (note->namesz == 6
   10208  1.15  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10209  1.15  christos 	return elfcore_grok_ppc_tm_spr (abfd, note);
   10210  1.15  christos       else
   10211  1.15  christos 	return TRUE;
   10212  1.15  christos 
   10213  1.15  christos     case NT_PPC_TM_CTAR:
   10214  1.15  christos       if (note->namesz == 6
   10215  1.15  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10216  1.15  christos 	return elfcore_grok_ppc_tm_ctar (abfd, note);
   10217  1.15  christos       else
   10218  1.15  christos 	return TRUE;
   10219  1.15  christos 
   10220  1.15  christos     case NT_PPC_TM_CPPR:
   10221  1.15  christos       if (note->namesz == 6
   10222  1.15  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10223  1.15  christos 	return elfcore_grok_ppc_tm_cppr (abfd, note);
   10224  1.15  christos       else
   10225  1.15  christos 	return TRUE;
   10226  1.15  christos 
   10227  1.15  christos     case NT_PPC_TM_CDSCR:
   10228  1.15  christos       if (note->namesz == 6
   10229   1.3  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10230   1.3  christos 	return elfcore_grok_ppc_tm_cdscr (abfd, note);
   10231   1.9  christos       else
   10232   1.9  christos 	return TRUE;
   10233   1.3  christos 
   10234   1.9  christos     case NT_S390_HIGH_GPRS:
   10235   1.3  christos       if (note->namesz == 6
   10236   1.3  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10237   1.3  christos 	return elfcore_grok_s390_high_gprs (abfd, note);
   10238   1.9  christos       else
   10239   1.9  christos 	return TRUE;
   10240   1.3  christos 
   10241   1.9  christos     case NT_S390_TIMER:
   10242   1.3  christos       if (note->namesz == 6
   10243   1.3  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10244   1.3  christos 	return elfcore_grok_s390_timer (abfd, note);
   10245   1.9  christos       else
   10246   1.9  christos 	return TRUE;
   10247   1.3  christos 
   10248   1.9  christos     case NT_S390_TODCMP:
   10249   1.3  christos       if (note->namesz == 6
   10250   1.3  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10251   1.3  christos 	return elfcore_grok_s390_todcmp (abfd, note);
   10252   1.9  christos       else
   10253   1.9  christos 	return TRUE;
   10254   1.3  christos 
   10255   1.9  christos     case NT_S390_TODPREG:
   10256   1.3  christos       if (note->namesz == 6
   10257   1.3  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10258   1.3  christos 	return elfcore_grok_s390_todpreg (abfd, note);
   10259   1.9  christos       else
   10260   1.9  christos 	return TRUE;
   10261   1.3  christos 
   10262   1.9  christos     case NT_S390_CTRS:
   10263   1.3  christos       if (note->namesz == 6
   10264   1.3  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10265   1.3  christos 	return elfcore_grok_s390_ctrs (abfd, note);
   10266   1.9  christos       else
   10267   1.9  christos 	return TRUE;
   10268   1.3  christos 
   10269   1.9  christos     case NT_S390_PREFIX:
   10270   1.3  christos       if (note->namesz == 6
   10271   1.5  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10272   1.5  christos 	return elfcore_grok_s390_prefix (abfd, note);
   10273   1.9  christos       else
   10274   1.9  christos 	return TRUE;
   10275   1.5  christos 
   10276   1.9  christos     case NT_S390_LAST_BREAK:
   10277   1.5  christos       if (note->namesz == 6
   10278   1.5  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10279   1.5  christos 	return elfcore_grok_s390_last_break (abfd, note);
   10280   1.9  christos       else
   10281   1.9  christos 	return TRUE;
   10282   1.5  christos 
   10283   1.9  christos     case NT_S390_SYSTEM_CALL:
   10284   1.5  christos       if (note->namesz == 6
   10285   1.6  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10286   1.6  christos 	return elfcore_grok_s390_system_call (abfd, note);
   10287   1.9  christos       else
   10288   1.9  christos 	return TRUE;
   10289   1.6  christos 
   10290   1.9  christos     case NT_S390_TDB:
   10291   1.6  christos       if (note->namesz == 6
   10292   1.6  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10293   1.6  christos 	return elfcore_grok_s390_tdb (abfd, note);
   10294   1.6  christos       else
   10295   1.6  christos 	return TRUE;
   10296   1.6  christos 
   10297   1.6  christos     case NT_S390_VXRS_LOW:
   10298   1.6  christos       if (note->namesz == 6
   10299   1.6  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10300   1.6  christos 	return elfcore_grok_s390_vxrs_low (abfd, note);
   10301   1.6  christos       else
   10302   1.6  christos 	return TRUE;
   10303   1.6  christos 
   10304   1.6  christos     case NT_S390_VXRS_HIGH:
   10305   1.6  christos       if (note->namesz == 6
   10306   1.9  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10307   1.9  christos 	return elfcore_grok_s390_vxrs_high (abfd, note);
   10308   1.9  christos       else
   10309   1.9  christos 	return TRUE;
   10310   1.9  christos 
   10311   1.9  christos     case NT_S390_GS_CB:
   10312   1.9  christos       if (note->namesz == 6
   10313   1.9  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10314   1.9  christos 	return elfcore_grok_s390_gs_cb (abfd, note);
   10315   1.9  christos       else
   10316   1.9  christos 	return TRUE;
   10317   1.9  christos 
   10318   1.9  christos     case NT_S390_GS_BC:
   10319   1.9  christos       if (note->namesz == 6
   10320   1.5  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10321   1.5  christos 	return elfcore_grok_s390_gs_bc (abfd, note);
   10322   1.5  christos       else
   10323   1.5  christos 	return TRUE;
   10324   1.5  christos 
   10325   1.5  christos     case NT_ARM_VFP:
   10326   1.5  christos       if (note->namesz == 6
   10327   1.6  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10328   1.6  christos 	return elfcore_grok_arm_vfp (abfd, note);
   10329   1.6  christos       else
   10330   1.6  christos 	return TRUE;
   10331   1.6  christos 
   10332   1.6  christos     case NT_ARM_TLS:
   10333   1.6  christos       if (note->namesz == 6
   10334   1.6  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10335   1.6  christos 	return elfcore_grok_aarch_tls (abfd, note);
   10336   1.6  christos       else
   10337   1.6  christos 	return TRUE;
   10338   1.6  christos 
   10339   1.6  christos     case NT_ARM_HW_BREAK:
   10340   1.6  christos       if (note->namesz == 6
   10341   1.6  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10342   1.6  christos 	return elfcore_grok_aarch_hw_break (abfd, note);
   10343   1.6  christos       else
   10344   1.6  christos 	return TRUE;
   10345   1.6  christos 
   10346   1.6  christos     case NT_ARM_HW_WATCH:
   10347   1.6  christos       if (note->namesz == 6
   10348  1.15  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10349  1.15  christos 	return elfcore_grok_aarch_hw_watch (abfd, note);
   10350  1.15  christos       else
   10351  1.15  christos 	return TRUE;
   10352  1.15  christos 
   10353  1.15  christos     case NT_ARM_SVE:
   10354  1.15  christos       if (note->namesz == 6
   10355  1.15  christos 	  && strcmp (note->namedata, "LINUX") == 0)
   10356  1.15  christos 	return elfcore_grok_aarch_sve (abfd, note);
   10357  1.15  christos       else
   10358  1.15  christos 	return TRUE;
   10359  1.15  christos 
   10360  1.15  christos     case NT_ARM_PAC_MASK:
   10361  1.15  christos       if (note->namesz == 6
   10362   1.1     skrll 	  && strcmp (note->namedata, "LINUX") == 0)
   10363   1.1     skrll 	return elfcore_grok_aarch_pauth (abfd, note);
   10364   1.1     skrll       else
   10365   1.1     skrll 	return TRUE;
   10366   1.1     skrll 
   10367   1.1     skrll     case NT_PRPSINFO:
   10368   1.1     skrll     case NT_PSINFO:
   10369   1.1     skrll       if (bed->elf_backend_grok_psinfo)
   10370   1.1     skrll 	if ((*bed->elf_backend_grok_psinfo) (abfd, note))
   10371   1.1     skrll 	  return TRUE;
   10372   1.1     skrll #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   10373   1.1     skrll       return elfcore_grok_psinfo (abfd, note);
   10374   1.8  christos #else
   10375   1.6  christos       return TRUE;
   10376   1.6  christos #endif
   10377   1.6  christos 
   10378   1.6  christos     case NT_AUXV:
   10379   1.6  christos       return elfcore_make_auxv_note_section (abfd, note, 0);
   10380   1.6  christos 
   10381   1.6  christos     case NT_FILE:
   10382   1.6  christos       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
   10383   1.7  christos 					      note);
   10384   1.1     skrll 
   10385   1.1     skrll     case NT_SIGINFO:
   10386   1.1     skrll       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
   10387   1.1     skrll 					      note);
   10388   1.1     skrll 
   10389   1.1     skrll     }
   10390   1.6  christos }
   10391   1.6  christos 
   10392   1.6  christos static bfd_boolean
   10393   1.6  christos elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
   10394   1.6  christos {
   10395   1.6  christos   struct bfd_build_id* build_id;
   10396   1.6  christos 
   10397   1.1     skrll   if (note->descsz == 0)
   10398   1.1     skrll     return FALSE;
   10399   1.6  christos 
   10400   1.6  christos   build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
   10401   1.6  christos   if (build_id == NULL)
   10402   1.1     skrll     return FALSE;
   10403   1.1     skrll 
   10404   1.1     skrll   build_id->size = note->descsz;
   10405   1.1     skrll   memcpy (build_id->data, note->descdata, note->descsz);
   10406   1.1     skrll   abfd->build_id = build_id;
   10407   1.1     skrll 
   10408   1.1     skrll   return TRUE;
   10409   1.1     skrll }
   10410   1.1     skrll 
   10411   1.1     skrll static bfd_boolean
   10412   1.1     skrll elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
   10413   1.1     skrll {
   10414   1.9  christos   switch (note->type)
   10415   1.9  christos     {
   10416   1.9  christos     default:
   10417   1.1     skrll       return TRUE;
   10418   1.1     skrll 
   10419   1.1     skrll     case NT_GNU_PROPERTY_TYPE_0:
   10420   1.1     skrll       return _bfd_elf_parse_gnu_properties (abfd, note);
   10421   1.1     skrll 
   10422   1.1     skrll     case NT_GNU_BUILD_ID:
   10423   1.5  christos       return elfobj_grok_gnu_build_id (abfd, note);
   10424   1.5  christos     }
   10425   1.5  christos }
   10426  1.15  christos 
   10427  1.15  christos static bfd_boolean
   10428   1.5  christos elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
   10429   1.5  christos {
   10430   1.5  christos   struct sdt_note *cur =
   10431   1.5  christos     (struct sdt_note *) bfd_alloc (abfd,
   10432   1.5  christos 				   sizeof (struct sdt_note) + note->descsz);
   10433   1.5  christos 
   10434   1.5  christos   cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
   10435   1.5  christos   cur->size = (bfd_size_type) note->descsz;
   10436   1.5  christos   memcpy (cur->data, note->descdata, note->descsz);
   10437   1.5  christos 
   10438   1.5  christos   elf_tdata (abfd)->sdt_note_head = cur;
   10439   1.5  christos 
   10440   1.5  christos   return TRUE;
   10441   1.5  christos }
   10442   1.5  christos 
   10443   1.5  christos static bfd_boolean
   10444   1.5  christos elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
   10445   1.5  christos {
   10446   1.5  christos   switch (note->type)
   10447   1.5  christos     {
   10448   1.5  christos     case NT_STAPSDT:
   10449   1.5  christos       return elfobj_grok_stapsdt_note_1 (abfd, note);
   10450   1.5  christos 
   10451   1.5  christos     default:
   10452   1.7  christos       return TRUE;
   10453   1.7  christos     }
   10454   1.7  christos }
   10455   1.7  christos 
   10456   1.9  christos static bfd_boolean
   10457   1.9  christos elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
   10458   1.9  christos {
   10459   1.9  christos   size_t offset;
   10460   1.9  christos 
   10461   1.9  christos   switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
   10462   1.9  christos     {
   10463   1.9  christos     case ELFCLASS32:
   10464   1.9  christos       if (note->descsz < 108)
   10465   1.9  christos 	return FALSE;
   10466   1.9  christos       break;
   10467   1.9  christos 
   10468   1.9  christos     case ELFCLASS64:
   10469   1.9  christos       if (note->descsz < 120)
   10470   1.9  christos 	return FALSE;
   10471   1.9  christos       break;
   10472   1.9  christos 
   10473   1.7  christos     default:
   10474   1.7  christos       return FALSE;
   10475   1.9  christos     }
   10476   1.7  christos 
   10477   1.7  christos   /* Check for version 1 in pr_version.  */
   10478   1.7  christos   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   10479   1.9  christos     return FALSE;
   10480   1.9  christos 
   10481   1.9  christos   offset = 4;
   10482   1.7  christos 
   10483   1.7  christos   /* Skip over pr_psinfosz. */
   10484   1.7  christos   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
   10485   1.7  christos     offset += 4;
   10486   1.7  christos   else
   10487   1.7  christos     {
   10488   1.7  christos       offset += 4;	/* Padding before pr_psinfosz. */
   10489   1.7  christos       offset += 8;
   10490   1.7  christos     }
   10491   1.7  christos 
   10492   1.7  christos   /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
   10493   1.7  christos   elf_tdata (abfd)->core->program
   10494   1.7  christos     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
   10495   1.9  christos   offset += 17;
   10496   1.9  christos 
   10497   1.9  christos   /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
   10498   1.9  christos   elf_tdata (abfd)->core->command
   10499   1.9  christos     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
   10500   1.9  christos   offset += 81;
   10501   1.9  christos 
   10502   1.9  christos   /* Padding before pr_pid.  */
   10503   1.9  christos   offset += 2;
   10504   1.9  christos 
   10505   1.9  christos   /* The pr_pid field was added in version "1a".  */
   10506   1.7  christos   if (note->descsz < offset + 4)
   10507   1.7  christos     return TRUE;
   10508   1.7  christos 
   10509   1.7  christos   elf_tdata (abfd)->core->pid
   10510   1.7  christos     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   10511   1.7  christos 
   10512   1.7  christos   return TRUE;
   10513   1.7  christos }
   10514   1.7  christos 
   10515   1.9  christos static bfd_boolean
   10516   1.7  christos elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
   10517   1.9  christos {
   10518   1.9  christos   size_t offset;
   10519   1.9  christos   size_t size;
   10520   1.9  christos   size_t min_size;
   10521   1.9  christos 
   10522   1.9  christos   /* Compute offset of pr_getregsz, skipping over pr_statussz.
   10523   1.9  christos      Also compute minimum size of this note.  */
   10524   1.7  christos   switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
   10525   1.7  christos     {
   10526   1.9  christos     case ELFCLASS32:
   10527   1.9  christos       offset = 4 + 4;
   10528   1.9  christos       min_size = offset + (4 * 2) + 4 + 4 + 4;
   10529   1.7  christos       break;
   10530   1.7  christos 
   10531   1.7  christos     case ELFCLASS64:
   10532   1.7  christos       offset = 4 + 4 + 8;	/* Includes padding before pr_statussz.  */
   10533   1.7  christos       min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
   10534   1.7  christos       break;
   10535   1.9  christos 
   10536   1.9  christos     default:
   10537   1.9  christos       return FALSE;
   10538   1.9  christos     }
   10539   1.9  christos 
   10540   1.9  christos   if (note->descsz < min_size)
   10541   1.9  christos     return FALSE;
   10542   1.7  christos 
   10543   1.9  christos   /* Check for version 1 in pr_version.  */
   10544   1.9  christos   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   10545   1.9  christos     return FALSE;
   10546   1.9  christos 
   10547   1.9  christos   /* Extract size of pr_reg from pr_gregsetsz.  */
   10548   1.9  christos   /* Skip over pr_gregsetsz and pr_fpregsetsz.  */
   10549   1.7  christos   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
   10550   1.9  christos     {
   10551   1.9  christos       size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   10552   1.9  christos       offset += 4 * 2;
   10553   1.9  christos     }
   10554   1.7  christos   else
   10555   1.9  christos     {
   10556   1.7  christos       size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
   10557   1.7  christos       offset += 8 * 2;
   10558   1.9  christos     }
   10559   1.7  christos 
   10560   1.7  christos   /* Skip over pr_osreldate.  */
   10561   1.7  christos   offset += 4;
   10562   1.7  christos 
   10563   1.7  christos   /* Read signal from pr_cursig.  */
   10564   1.9  christos   if (elf_tdata (abfd)->core->signal == 0)
   10565   1.7  christos     elf_tdata (abfd)->core->signal
   10566   1.7  christos       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   10567   1.7  christos   offset += 4;
   10568   1.7  christos 
   10569   1.9  christos   /* Read TID from pr_pid.  */
   10570   1.9  christos   elf_tdata (abfd)->core->lwpid
   10571   1.7  christos       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   10572   1.7  christos   offset += 4;
   10573   1.9  christos 
   10574   1.9  christos   /* Padding before pr_reg.  */
   10575   1.9  christos   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
   10576   1.9  christos     offset += 4;
   10577   1.7  christos 
   10578   1.7  christos   /* Make sure that there is enough data remaining in the note.  */
   10579   1.7  christos   if ((note->descsz - offset) < size)
   10580   1.7  christos     return FALSE;
   10581   1.7  christos 
   10582   1.7  christos   /* Make a ".reg/999" section and a ".reg" section.  */
   10583   1.7  christos   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   10584   1.7  christos 					  size, note->descpos + offset);
   10585   1.9  christos }
   10586   1.9  christos 
   10587   1.7  christos static bfd_boolean
   10588   1.7  christos elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
   10589   1.7  christos {
   10590   1.9  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   10591   1.9  christos 
   10592   1.9  christos   switch (note->type)
   10593   1.7  christos     {
   10594   1.7  christos     case NT_PRSTATUS:
   10595   1.7  christos       if (bed->elf_backend_grok_freebsd_prstatus)
   10596   1.7  christos 	if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
   10597   1.7  christos 	  return TRUE;
   10598   1.7  christos       return elfcore_grok_freebsd_prstatus (abfd, note);
   10599   1.7  christos 
   10600   1.7  christos     case NT_FPREGSET:
   10601   1.7  christos       return elfcore_grok_prfpreg (abfd, note);
   10602   1.7  christos 
   10603   1.7  christos     case NT_PRPSINFO:
   10604   1.7  christos       return elfcore_grok_freebsd_psinfo (abfd, note);
   10605   1.7  christos 
   10606   1.7  christos     case NT_FREEBSD_THRMISC:
   10607   1.9  christos       if (note->namesz == 8)
   10608   1.9  christos 	return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
   10609   1.9  christos       else
   10610   1.9  christos 	return TRUE;
   10611   1.9  christos 
   10612   1.9  christos     case NT_FREEBSD_PROCSTAT_PROC:
   10613   1.9  christos       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
   10614   1.9  christos 					      note);
   10615   1.9  christos 
   10616   1.9  christos     case NT_FREEBSD_PROCSTAT_FILES:
   10617   1.9  christos       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
   10618   1.9  christos 					      note);
   10619   1.7  christos 
   10620   1.8  christos     case NT_FREEBSD_PROCSTAT_VMMAP:
   10621   1.7  christos       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
   10622   1.7  christos 					      note);
   10623   1.7  christos 
   10624   1.7  christos     case NT_FREEBSD_PROCSTAT_AUXV:
   10625   1.7  christos       return elfcore_make_auxv_note_section (abfd, note, 4);
   10626   1.7  christos 
   10627   1.7  christos     case NT_X86_XSTATE:
   10628   1.9  christos       if (note->namesz == 8)
   10629   1.9  christos 	return elfcore_grok_xstatereg (abfd, note);
   10630   1.9  christos       else
   10631   1.9  christos 	return TRUE;
   10632   1.9  christos 
   10633   1.9  christos     case NT_FREEBSD_PTLWPINFO:
   10634   1.9  christos       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
   10635   1.7  christos 					      note);
   10636   1.7  christos 
   10637   1.7  christos     case NT_ARM_VFP:
   10638   1.7  christos       return elfcore_grok_arm_vfp (abfd, note);
   10639   1.7  christos 
   10640   1.7  christos     default:
   10641   1.1     skrll       return TRUE;
   10642   1.1     skrll     }
   10643   1.1     skrll }
   10644   1.1     skrll 
   10645   1.1     skrll static bfd_boolean
   10646   1.1     skrll elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
   10647   1.1     skrll {
   10648   1.1     skrll   char *cp;
   10649   1.1     skrll 
   10650   1.1     skrll   cp = strchr (note->namedata, '@');
   10651   1.1     skrll   if (cp != NULL)
   10652   1.1     skrll     {
   10653   1.1     skrll       *lwpidp = atoi(cp + 1);
   10654   1.1     skrll       return TRUE;
   10655   1.1     skrll     }
   10656   1.1     skrll   return FALSE;
   10657   1.9  christos }
   10658   1.9  christos 
   10659   1.9  christos static bfd_boolean
   10660   1.1     skrll elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   10661   1.6  christos {
   10662   1.1     skrll   if (note->descsz <= 0x7c + 31)
   10663   1.1     skrll     return FALSE;
   10664   1.1     skrll 
   10665   1.6  christos   /* Signal number at offset 0x08. */
   10666   1.1     skrll   elf_tdata (abfd)->core->signal
   10667   1.1     skrll     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   10668   1.1     skrll 
   10669   1.6  christos   /* Process ID at offset 0x50. */
   10670   1.1     skrll   elf_tdata (abfd)->core->pid
   10671   1.1     skrll     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
   10672   1.1     skrll 
   10673   1.1     skrll   /* Command name at 0x7c (max 32 bytes, including nul). */
   10674   1.1     skrll   elf_tdata (abfd)->core->command
   10675   1.1     skrll     = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
   10676   1.8  christos 
   10677   1.1     skrll   return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
   10678   1.1     skrll 					  note);
   10679   1.1     skrll }
   10680   1.1     skrll 
   10681   1.1     skrll 
   10682   1.1     skrll static bfd_boolean
   10683   1.6  christos elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
   10684   1.1     skrll {
   10685   1.8  christos   int lwp;
   10686   1.1     skrll 
   10687   1.8  christos   if (elfcore_netbsd_get_lwpid (note, &lwp))
   10688   1.1     skrll     elf_tdata (abfd)->core->lwpid = lwp;
   10689   1.1     skrll 
   10690   1.1     skrll   switch (note->type)
   10691   1.1     skrll     {
   10692   1.8  christos     case NT_NETBSDCORE_PROCINFO:
   10693  1.15  christos       /* NetBSD-specific core "procinfo".  Note that we expect to
   10694   1.8  christos 	 find this note before any of the others, which is fine,
   10695   1.8  christos 	 since the kernel writes this note out first when it
   10696   1.8  christos 	 creates a core file.  */
   10697  1.15  christos       return elfcore_grok_netbsd_procinfo (abfd, note);
   10698   1.8  christos #ifdef NT_NETBSDCORE_AUXV
   10699   1.8  christos     case NT_NETBSDCORE_AUXV:
   10700   1.1     skrll       /* NetBSD-specific Elf Auxiliary Vector data. */
   10701   1.1     skrll       return elfcore_make_auxv_note_section (abfd, note, 4);
   10702   1.8  christos #endif
   10703   1.1     skrll     default:
   10704   1.1     skrll       break;
   10705   1.1     skrll     }
   10706   1.1     skrll 
   10707   1.1     skrll   /* As of March 2017 there are no other machine-independent notes
   10708   1.1     skrll      defined for NetBSD core files.  If the note type is less
   10709   1.1     skrll      than the start of the machine-dependent note types, we don't
   10710   1.1     skrll      understand it.  */
   10711   1.1     skrll 
   10712   1.1     skrll   if (note->type < NT_NETBSDCORE_FIRSTMACH)
   10713   1.1     skrll     return TRUE;
   10714   1.1     skrll 
   10715   1.1     skrll 
   10716   1.1     skrll   switch (bfd_get_arch (abfd))
   10717   1.1     skrll     {
   10718   1.1     skrll       /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
   10719   1.1     skrll 	 PT_GETFPREGS == mach+2.  */
   10720   1.1     skrll 
   10721   1.1     skrll     case bfd_arch_alpha:
   10722   1.1     skrll     case bfd_arch_sparc:
   10723   1.1     skrll       switch (note->type)
   10724   1.1     skrll 	{
   10725   1.1     skrll 	case NT_NETBSDCORE_FIRSTMACH+0:
   10726   1.1     skrll 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   10727   1.1     skrll 
   10728   1.1     skrll 	case NT_NETBSDCORE_FIRSTMACH+2:
   10729   1.1     skrll 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   10730   1.4       uwe 
   10731   1.4       uwe 	default:
   10732   1.4       uwe 	  return TRUE;
   10733   1.4       uwe 	}
   10734   1.4       uwe 
   10735   1.4       uwe       /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
   10736   1.4       uwe 	 There's also old PT___GETREGS40 == mach + 1 for old reg
   10737   1.4       uwe 	 structure which lacks GBR.  */
   10738   1.4       uwe 
   10739   1.4       uwe     case bfd_arch_sh:
   10740   1.4       uwe       switch (note->type)
   10741   1.4       uwe 	{
   10742   1.4       uwe 	case NT_NETBSDCORE_FIRSTMACH+3:
   10743   1.4       uwe 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   10744   1.4       uwe 
   10745   1.4       uwe 	case NT_NETBSDCORE_FIRSTMACH+5:
   10746   1.4       uwe 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   10747   1.1     skrll 
   10748   1.1     skrll 	default:
   10749   1.1     skrll 	  return TRUE;
   10750   1.1     skrll 	}
   10751   1.1     skrll 
   10752   1.1     skrll       /* On all other arch's, PT_GETREGS == mach+1 and
   10753   1.1     skrll 	 PT_GETFPREGS == mach+3.  */
   10754   1.1     skrll 
   10755   1.1     skrll     default:
   10756   1.1     skrll       switch (note->type)
   10757   1.1     skrll 	{
   10758   1.1     skrll 	case NT_NETBSDCORE_FIRSTMACH+1:
   10759   1.1     skrll 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   10760   1.1     skrll 
   10761   1.1     skrll 	case NT_NETBSDCORE_FIRSTMACH+3:
   10762   1.1     skrll 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   10763   1.1     skrll 
   10764   1.1     skrll 	default:
   10765   1.1     skrll 	  return TRUE;
   10766   1.1     skrll 	}
   10767   1.3  christos     }
   10768   1.3  christos     /* NOTREACHED */
   10769   1.9  christos }
   10770   1.9  christos 
   10771   1.9  christos static bfd_boolean
   10772   1.3  christos elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   10773   1.6  christos {
   10774   1.3  christos   if (note->descsz <= 0x48 + 31)
   10775   1.3  christos     return FALSE;
   10776   1.3  christos 
   10777   1.6  christos   /* Signal number at offset 0x08. */
   10778   1.3  christos   elf_tdata (abfd)->core->signal
   10779   1.3  christos     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   10780   1.3  christos 
   10781   1.6  christos   /* Process ID at offset 0x20. */
   10782   1.3  christos   elf_tdata (abfd)->core->pid
   10783   1.3  christos     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
   10784   1.3  christos 
   10785   1.3  christos   /* Command name at 0x48 (max 32 bytes, including nul). */
   10786   1.3  christos   elf_tdata (abfd)->core->command
   10787   1.3  christos     = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
   10788   1.3  christos 
   10789   1.3  christos   return TRUE;
   10790   1.3  christos }
   10791   1.3  christos 
   10792   1.3  christos static bfd_boolean
   10793   1.3  christos elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
   10794   1.3  christos {
   10795   1.3  christos   if (note->type == NT_OPENBSD_PROCINFO)
   10796   1.3  christos     return elfcore_grok_openbsd_procinfo (abfd, note);
   10797   1.3  christos 
   10798   1.3  christos   if (note->type == NT_OPENBSD_REGS)
   10799   1.3  christos     return elfcore_make_note_pseudosection (abfd, ".reg", note);
   10800   1.3  christos 
   10801   1.3  christos   if (note->type == NT_OPENBSD_FPREGS)
   10802   1.3  christos     return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   10803   1.8  christos 
   10804   1.3  christos   if (note->type == NT_OPENBSD_XFPREGS)
   10805   1.3  christos     return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
   10806   1.3  christos 
   10807   1.3  christos   if (note->type == NT_OPENBSD_AUXV)
   10808   1.3  christos     return elfcore_make_auxv_note_section (abfd, note, 0);
   10809   1.3  christos 
   10810   1.3  christos   if (note->type == NT_OPENBSD_WCOOKIE)
   10811   1.3  christos     {
   10812   1.3  christos       asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
   10813   1.3  christos 							   SEC_HAS_CONTENTS);
   10814   1.3  christos 
   10815   1.3  christos       if (sect == NULL)
   10816   1.3  christos 	return FALSE;
   10817   1.3  christos       sect->size = note->descsz;
   10818   1.3  christos       sect->filepos = note->descpos;
   10819   1.3  christos       sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   10820   1.3  christos 
   10821   1.3  christos       return TRUE;
   10822   1.3  christos     }
   10823   1.1     skrll 
   10824   1.1     skrll   return TRUE;
   10825   1.1     skrll }
   10826   1.1     skrll 
   10827   1.1     skrll static bfd_boolean
   10828   1.1     skrll elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
   10829   1.1     skrll {
   10830   1.1     skrll   void *ddata = note->descdata;
   10831   1.1     skrll   char buf[100];
   10832   1.9  christos   char *name;
   10833   1.9  christos   asection *sect;
   10834   1.9  christos   short sig;
   10835   1.1     skrll   unsigned flags;
   10836   1.6  christos 
   10837   1.1     skrll   if (note->descsz < 16)
   10838   1.1     skrll     return FALSE;
   10839   1.1     skrll 
   10840   1.1     skrll   /* nto_procfs_status 'pid' field is at offset 0.  */
   10841   1.1     skrll   elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
   10842   1.1     skrll 
   10843   1.1     skrll   /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
   10844   1.1     skrll   *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
   10845   1.1     skrll 
   10846   1.1     skrll   /* nto_procfs_status 'flags' field is at offset 8.  */
   10847   1.6  christos   flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
   10848   1.6  christos 
   10849   1.1     skrll   /* nto_procfs_status 'what' field is at offset 14.  */
   10850   1.1     skrll   if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
   10851   1.1     skrll     {
   10852   1.1     skrll       elf_tdata (abfd)->core->signal = sig;
   10853   1.1     skrll       elf_tdata (abfd)->core->lwpid = *tid;
   10854   1.1     skrll     }
   10855   1.6  christos 
   10856   1.1     skrll   /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
   10857   1.1     skrll      do not come from signals so we make sure we set the current
   10858   1.1     skrll      thread just in case.  */
   10859   1.1     skrll   if (flags & 0x00000080)
   10860   1.3  christos     elf_tdata (abfd)->core->lwpid = *tid;
   10861   1.1     skrll 
   10862   1.1     skrll   /* Make a ".qnx_core_status/%d" section.  */
   10863   1.1     skrll   sprintf (buf, ".qnx_core_status/%ld", *tid);
   10864   1.1     skrll 
   10865   1.1     skrll   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   10866   1.1     skrll   if (name == NULL)
   10867   1.1     skrll     return FALSE;
   10868   1.1     skrll   strcpy (name, buf);
   10869   1.9  christos 
   10870   1.9  christos   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10871   1.1     skrll   if (sect == NULL)
   10872   1.1     skrll     return FALSE;
   10873   1.1     skrll 
   10874   1.1     skrll   sect->size		= note->descsz;
   10875   1.1     skrll   sect->filepos		= note->descpos;
   10876   1.1     skrll   sect->alignment_power = 2;
   10877   1.1     skrll 
   10878   1.1     skrll   return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
   10879   1.1     skrll }
   10880   1.1     skrll 
   10881   1.1     skrll static bfd_boolean
   10882   1.1     skrll elfcore_grok_nto_regs (bfd *abfd,
   10883   1.1     skrll 		       Elf_Internal_Note *note,
   10884   1.1     skrll 		       long tid,
   10885   1.1     skrll 		       char *base)
   10886   1.1     skrll {
   10887   1.1     skrll   char buf[100];
   10888   1.1     skrll   char *name;
   10889   1.3  christos   asection *sect;
   10890   1.1     skrll 
   10891   1.1     skrll   /* Make a "(base)/%d" section.  */
   10892   1.1     skrll   sprintf (buf, "%s/%ld", base, tid);
   10893   1.1     skrll 
   10894   1.1     skrll   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   10895   1.1     skrll   if (name == NULL)
   10896   1.1     skrll     return FALSE;
   10897   1.1     skrll   strcpy (name, buf);
   10898   1.9  christos 
   10899   1.9  christos   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10900   1.1     skrll   if (sect == NULL)
   10901   1.1     skrll     return FALSE;
   10902   1.1     skrll 
   10903   1.6  christos   sect->size		= note->descsz;
   10904   1.1     skrll   sect->filepos		= note->descpos;
   10905   1.1     skrll   sect->alignment_power = 2;
   10906   1.1     skrll 
   10907   1.1     skrll   /* This is the current thread.  */
   10908   1.1     skrll   if (elf_tdata (abfd)->core->lwpid == tid)
   10909   1.1     skrll     return elfcore_maybe_make_sect (abfd, base, sect);
   10910   1.1     skrll 
   10911   1.1     skrll   return TRUE;
   10912   1.1     skrll }
   10913   1.1     skrll 
   10914   1.1     skrll #define BFD_QNT_CORE_INFO	7
   10915   1.1     skrll #define BFD_QNT_CORE_STATUS	8
   10916   1.1     skrll #define BFD_QNT_CORE_GREG	9
   10917   1.1     skrll #define BFD_QNT_CORE_FPREG	10
   10918   1.1     skrll 
   10919   1.1     skrll static bfd_boolean
   10920   1.1     skrll elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
   10921   1.1     skrll {
   10922   1.1     skrll   /* Every GREG section has a STATUS section before it.  Store the
   10923   1.1     skrll      tid from the previous call to pass down to the next gregs
   10924   1.1     skrll      function.  */
   10925   1.1     skrll   static long tid = 1;
   10926   1.1     skrll 
   10927   1.1     skrll   switch (note->type)
   10928   1.1     skrll     {
   10929   1.1     skrll     case BFD_QNT_CORE_INFO:
   10930   1.1     skrll       return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
   10931   1.1     skrll     case BFD_QNT_CORE_STATUS:
   10932   1.1     skrll       return elfcore_grok_nto_status (abfd, note, &tid);
   10933   1.1     skrll     case BFD_QNT_CORE_GREG:
   10934   1.1     skrll       return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
   10935   1.1     skrll     case BFD_QNT_CORE_FPREG:
   10936   1.1     skrll       return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
   10937   1.1     skrll     default:
   10938   1.1     skrll       return TRUE;
   10939   1.1     skrll     }
   10940   1.1     skrll }
   10941   1.1     skrll 
   10942   1.1     skrll static bfd_boolean
   10943   1.1     skrll elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
   10944   1.1     skrll {
   10945   1.1     skrll   char *name;
   10946   1.3  christos   asection *sect;
   10947   1.1     skrll   size_t len;
   10948   1.1     skrll 
   10949   1.1     skrll   /* Use note name as section name.  */
   10950   1.1     skrll   len = note->namesz;
   10951   1.1     skrll   name = (char *) bfd_alloc (abfd, len);
   10952   1.1     skrll   if (name == NULL)
   10953   1.1     skrll     return FALSE;
   10954   1.1     skrll   memcpy (name, note->namedata, len);
   10955   1.1     skrll   name[len - 1] = '\0';
   10956   1.9  christos 
   10957   1.9  christos   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10958   1.1     skrll   if (sect == NULL)
   10959   1.1     skrll     return FALSE;
   10960   1.1     skrll 
   10961   1.1     skrll   sect->size		= note->descsz;
   10962   1.1     skrll   sect->filepos		= note->descpos;
   10963   1.1     skrll   sect->alignment_power = 1;
   10964   1.1     skrll 
   10965   1.1     skrll   return TRUE;
   10966   1.1     skrll }
   10967   1.1     skrll 
   10968   1.1     skrll /* Function: elfcore_write_note
   10969   1.1     skrll 
   10970   1.1     skrll    Inputs:
   10971   1.1     skrll      buffer to hold note, and current size of buffer
   10972   1.1     skrll      name of note
   10973   1.1     skrll      type of note
   10974   1.1     skrll      data for note
   10975   1.1     skrll      size of data for note
   10976   1.1     skrll 
   10977   1.1     skrll    Writes note to end of buffer.  ELF64 notes are written exactly as
   10978   1.1     skrll    for ELF32, despite the current (as of 2006) ELF gabi specifying
   10979   1.1     skrll    that they ought to have 8-byte namesz and descsz field, and have
   10980   1.1     skrll    8-byte alignment.  Other writers, eg. Linux kernel, do the same.
   10981   1.1     skrll 
   10982   1.1     skrll    Return:
   10983   1.1     skrll    Pointer to realloc'd buffer, *BUFSIZ updated.  */
   10984   1.1     skrll 
   10985   1.1     skrll char *
   10986   1.1     skrll elfcore_write_note (bfd *abfd,
   10987   1.1     skrll 		    char *buf,
   10988   1.1     skrll 		    int *bufsiz,
   10989   1.1     skrll 		    const char *name,
   10990   1.1     skrll 		    int type,
   10991   1.1     skrll 		    const void *input,
   10992   1.1     skrll 		    int size)
   10993   1.1     skrll {
   10994   1.1     skrll   Elf_External_Note *xnp;
   10995   1.1     skrll   size_t namesz;
   10996   1.1     skrll   size_t newspace;
   10997   1.1     skrll   char *dest;
   10998   1.1     skrll 
   10999   1.1     skrll   namesz = 0;
   11000   1.3  christos   if (name != NULL)
   11001   1.1     skrll     namesz = strlen (name) + 1;
   11002   1.1     skrll 
   11003   1.1     skrll   newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
   11004   1.1     skrll 
   11005   1.1     skrll   buf = (char *) realloc (buf, *bufsiz + newspace);
   11006   1.1     skrll   if (buf == NULL)
   11007   1.1     skrll     return buf;
   11008   1.1     skrll   dest = buf + *bufsiz;
   11009   1.1     skrll   *bufsiz += newspace;
   11010   1.1     skrll   xnp = (Elf_External_Note *) dest;
   11011   1.1     skrll   H_PUT_32 (abfd, namesz, xnp->namesz);
   11012   1.1     skrll   H_PUT_32 (abfd, size, xnp->descsz);
   11013   1.1     skrll   H_PUT_32 (abfd, type, xnp->type);
   11014   1.1     skrll   dest = xnp->name;
   11015   1.1     skrll   if (name != NULL)
   11016   1.1     skrll     {
   11017   1.1     skrll       memcpy (dest, name, namesz);
   11018   1.1     skrll       dest += namesz;
   11019   1.1     skrll       while (namesz & 3)
   11020   1.1     skrll 	{
   11021   1.1     skrll 	  *dest++ = '\0';
   11022   1.1     skrll 	  ++namesz;
   11023   1.1     skrll 	}
   11024   1.1     skrll     }
   11025   1.1     skrll   memcpy (dest, input, size);
   11026   1.1     skrll   dest += size;
   11027   1.1     skrll   while (size & 3)
   11028   1.1     skrll     {
   11029   1.1     skrll       *dest++ = '\0';
   11030  1.13  christos       ++size;
   11031  1.13  christos     }
   11032  1.13  christos   return buf;
   11033  1.13  christos }
   11034  1.13  christos 
   11035  1.13  christos /* gcc-8 warns (*) on all the strncpy calls in this function about
   11036  1.13  christos    possible string truncation.  The "truncation" is not a bug.  We
   11037  1.13  christos    have an external representation of structs with fields that are not
   11038  1.13  christos    necessarily NULL terminated and corresponding internal
   11039  1.13  christos    representation fields that are one larger so that they can always
   11040  1.13  christos    be NULL terminated.
   11041  1.13  christos    gcc versions between 4.2 and 4.6 do not allow pragma control of
   11042  1.13  christos    diagnostics inside functions, giving a hard error if you try to use
   11043  1.13  christos    the finer control available with later versions.
   11044  1.13  christos    gcc prior to 4.2 warns about diagnostic push and pop.
   11045  1.13  christos    gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
   11046  1.13  christos    unless you also add #pragma GCC diagnostic ignored "-Wpragma".
   11047   1.1     skrll    (*) Depending on your system header files!  */
   11048   1.1     skrll #if GCC_VERSION >= 8000
   11049   1.1     skrll # pragma GCC diagnostic push
   11050   1.1     skrll # pragma GCC diagnostic ignored "-Wstringop-truncation"
   11051   1.1     skrll #endif
   11052   1.1     skrll char *
   11053   1.1     skrll elfcore_write_prpsinfo (bfd  *abfd,
   11054   1.1     skrll 			char *buf,
   11055   1.1     skrll 			int  *bufsiz,
   11056   1.1     skrll 			const char *fname,
   11057   1.1     skrll 			const char *psargs)
   11058   1.1     skrll {
   11059   1.1     skrll   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11060   1.1     skrll 
   11061   1.1     skrll   if (bed->elf_backend_write_core_note != NULL)
   11062   1.1     skrll     {
   11063   1.1     skrll       char *ret;
   11064   1.1     skrll       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   11065   1.5  christos 						 NT_PRPSINFO, fname, psargs);
   11066  1.13  christos       if (ret != NULL)
   11067   1.1     skrll 	return ret;
   11068   1.1     skrll     }
   11069  1.13  christos 
   11070   1.1     skrll #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   11071   1.1     skrll # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   11072  1.13  christos   if (bed->s->elfclass == ELFCLASS32)
   11073   1.1     skrll     {
   11074   1.1     skrll #  if defined (HAVE_PSINFO32_T)
   11075  1.13  christos       psinfo32_t data;
   11076   1.1     skrll       int note_type = NT_PSINFO;
   11077   1.1     skrll #  else
   11078   1.1     skrll       prpsinfo32_t data;
   11079   1.1     skrll       int note_type = NT_PRPSINFO;
   11080   1.1     skrll #  endif
   11081   1.5  christos 
   11082   1.1     skrll       memset (&data, 0, sizeof (data));
   11083   1.1     skrll       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   11084  1.13  christos       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   11085   1.1     skrll       return elfcore_write_note (abfd, buf, bufsiz,
   11086  1.13  christos 				 "CORE", note_type, &data, sizeof (data));
   11087   1.1     skrll     }
   11088   1.1     skrll   else
   11089  1.13  christos # endif
   11090   1.1     skrll     {
   11091   1.1     skrll # if defined (HAVE_PSINFO_T)
   11092  1.13  christos       psinfo_t data;
   11093   1.1     skrll       int note_type = NT_PSINFO;
   11094   1.1     skrll # else
   11095   1.1     skrll       prpsinfo_t data;
   11096   1.1     skrll       int note_type = NT_PRPSINFO;
   11097   1.1     skrll # endif
   11098   1.5  christos 
   11099   1.1     skrll       memset (&data, 0, sizeof (data));
   11100   1.5  christos       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   11101   1.5  christos       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   11102   1.5  christos       return elfcore_write_note (abfd, buf, bufsiz,
   11103   1.5  christos 				 "CORE", note_type, &data, sizeof (data));
   11104   1.1     skrll     }
   11105  1.13  christos #endif	/* PSINFO_T or PRPSINFO_T */
   11106  1.13  christos 
   11107  1.13  christos   free (buf);
   11108   1.1     skrll   return NULL;
   11109   1.1     skrll }
   11110   1.6  christos #if GCC_VERSION >= 8000
   11111   1.6  christos # pragma GCC diagnostic pop
   11112   1.6  christos #endif
   11113   1.6  christos 
   11114   1.9  christos char *
   11115   1.9  christos elfcore_write_linux_prpsinfo32
   11116   1.9  christos   (bfd *abfd, char *buf, int *bufsiz,
   11117   1.9  christos    const struct elf_internal_linux_prpsinfo *prpsinfo)
   11118   1.9  christos {
   11119   1.9  christos   if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
   11120   1.9  christos     {
   11121   1.9  christos       struct elf_external_linux_prpsinfo32_ugid16 data;
   11122   1.9  christos 
   11123   1.9  christos       swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
   11124   1.9  christos       return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
   11125   1.6  christos 				 &data, sizeof (data));
   11126   1.9  christos     }
   11127   1.9  christos   else
   11128   1.9  christos     {
   11129   1.9  christos       struct elf_external_linux_prpsinfo32_ugid32 data;
   11130   1.6  christos 
   11131   1.6  christos       swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
   11132   1.6  christos       return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
   11133   1.6  christos 				 &data, sizeof (data));
   11134   1.6  christos     }
   11135   1.6  christos }
   11136   1.6  christos 
   11137   1.9  christos char *
   11138   1.9  christos elfcore_write_linux_prpsinfo64
   11139   1.9  christos   (bfd *abfd, char *buf, int *bufsiz,
   11140   1.6  christos    const struct elf_internal_linux_prpsinfo *prpsinfo)
   11141   1.9  christos {
   11142   1.9  christos   if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
   11143   1.9  christos     {
   11144   1.9  christos       struct elf_external_linux_prpsinfo64_ugid16 data;
   11145   1.9  christos 
   11146   1.9  christos       swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
   11147   1.9  christos       return elfcore_write_note (abfd, buf, bufsiz,
   11148   1.9  christos 				 "CORE", NT_PRPSINFO, &data, sizeof (data));
   11149   1.9  christos     }
   11150   1.9  christos   else
   11151   1.9  christos     {
   11152   1.9  christos       struct elf_external_linux_prpsinfo64_ugid32 data;
   11153   1.6  christos 
   11154   1.6  christos       swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
   11155   1.6  christos       return elfcore_write_note (abfd, buf, bufsiz,
   11156   1.1     skrll 				 "CORE", NT_PRPSINFO, &data, sizeof (data));
   11157   1.1     skrll     }
   11158   1.1     skrll }
   11159   1.1     skrll 
   11160   1.1     skrll char *
   11161   1.1     skrll elfcore_write_prstatus (bfd *abfd,
   11162   1.1     skrll 			char *buf,
   11163   1.1     skrll 			int *bufsiz,
   11164   1.1     skrll 			long pid,
   11165   1.1     skrll 			int cursig,
   11166   1.1     skrll 			const void *gregs)
   11167   1.1     skrll {
   11168   1.1     skrll   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11169   1.1     skrll 
   11170   1.1     skrll   if (bed->elf_backend_write_core_note != NULL)
   11171   1.1     skrll     {
   11172   1.1     skrll       char *ret;
   11173   1.1     skrll       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   11174   1.1     skrll 						 NT_PRSTATUS,
   11175   1.5  christos 						 pid, cursig, gregs);
   11176   1.1     skrll       if (ret != NULL)
   11177   1.1     skrll 	return ret;
   11178   1.1     skrll     }
   11179   1.1     skrll 
   11180   1.1     skrll #if defined (HAVE_PRSTATUS_T)
   11181   1.1     skrll #if defined (HAVE_PRSTATUS32_T)
   11182   1.1     skrll   if (bed->s->elfclass == ELFCLASS32)
   11183   1.1     skrll     {
   11184   1.1     skrll       prstatus32_t prstat;
   11185   1.5  christos 
   11186   1.1     skrll       memset (&prstat, 0, sizeof (prstat));
   11187   1.1     skrll       prstat.pr_pid = pid;
   11188   1.1     skrll       prstat.pr_cursig = cursig;
   11189   1.1     skrll       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   11190   1.1     skrll       return elfcore_write_note (abfd, buf, bufsiz, "CORE",
   11191   1.1     skrll 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   11192   1.1     skrll     }
   11193   1.1     skrll   else
   11194   1.1     skrll #endif
   11195   1.1     skrll     {
   11196   1.1     skrll       prstatus_t prstat;
   11197   1.5  christos 
   11198   1.1     skrll       memset (&prstat, 0, sizeof (prstat));
   11199   1.1     skrll       prstat.pr_pid = pid;
   11200   1.5  christos       prstat.pr_cursig = cursig;
   11201   1.5  christos       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   11202   1.5  christos       return elfcore_write_note (abfd, buf, bufsiz, "CORE",
   11203   1.5  christos 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   11204   1.1     skrll     }
   11205   1.1     skrll #endif /* HAVE_PRSTATUS_T */
   11206   1.1     skrll 
   11207   1.1     skrll   free (buf);
   11208   1.1     skrll   return NULL;
   11209   1.1     skrll }
   11210   1.1     skrll 
   11211   1.1     skrll #if defined (HAVE_LWPSTATUS_T)
   11212   1.1     skrll char *
   11213   1.1     skrll elfcore_write_lwpstatus (bfd *abfd,
   11214   1.1     skrll 			 char *buf,
   11215   1.1     skrll 			 int *bufsiz,
   11216   1.1     skrll 			 long pid,
   11217   1.1     skrll 			 int cursig,
   11218   1.1     skrll 			 const void *gregs)
   11219   1.1     skrll {
   11220   1.1     skrll   lwpstatus_t lwpstat;
   11221   1.1     skrll   const char *note_name = "CORE";
   11222   1.6  christos 
   11223   1.1     skrll   memset (&lwpstat, 0, sizeof (lwpstat));
   11224   1.1     skrll   lwpstat.pr_lwpid  = pid >> 16;
   11225   1.1     skrll   lwpstat.pr_cursig = cursig;
   11226   1.1     skrll #if defined (HAVE_LWPSTATUS_T_PR_REG)
   11227   1.1     skrll   memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
   11228   1.1     skrll #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   11229   1.1     skrll #if !defined(gregs)
   11230   1.1     skrll   memcpy (lwpstat.pr_context.uc_mcontext.gregs,
   11231   1.1     skrll 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
   11232   1.1     skrll #else
   11233   1.1     skrll   memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
   11234   1.1     skrll 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
   11235   1.1     skrll #endif
   11236   1.1     skrll #endif
   11237   1.1     skrll   return elfcore_write_note (abfd, buf, bufsiz, note_name,
   11238   1.1     skrll 			     NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
   11239   1.1     skrll }
   11240   1.1     skrll #endif /* HAVE_LWPSTATUS_T */
   11241   1.1     skrll 
   11242   1.1     skrll #if defined (HAVE_PSTATUS_T)
   11243   1.1     skrll char *
   11244   1.1     skrll elfcore_write_pstatus (bfd *abfd,
   11245   1.1     skrll 		       char *buf,
   11246   1.1     skrll 		       int *bufsiz,
   11247   1.1     skrll 		       long pid,
   11248   1.1     skrll 		       int cursig ATTRIBUTE_UNUSED,
   11249   1.1     skrll 		       const void *gregs ATTRIBUTE_UNUSED)
   11250   1.1     skrll {
   11251   1.1     skrll   const char *note_name = "CORE";
   11252   1.1     skrll #if defined (HAVE_PSTATUS32_T)
   11253   1.1     skrll   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11254   1.1     skrll 
   11255   1.1     skrll   if (bed->s->elfclass == ELFCLASS32)
   11256   1.1     skrll     {
   11257   1.1     skrll       pstatus32_t pstat;
   11258   1.1     skrll 
   11259   1.1     skrll       memset (&pstat, 0, sizeof (pstat));
   11260   1.1     skrll       pstat.pr_pid = pid & 0xffff;
   11261   1.1     skrll       buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
   11262   1.1     skrll 				NT_PSTATUS, &pstat, sizeof (pstat));
   11263   1.1     skrll       return buf;
   11264   1.1     skrll     }
   11265   1.1     skrll   else
   11266   1.1     skrll #endif
   11267   1.1     skrll     {
   11268   1.1     skrll       pstatus_t pstat;
   11269   1.1     skrll 
   11270   1.1     skrll       memset (&pstat, 0, sizeof (pstat));
   11271   1.1     skrll       pstat.pr_pid = pid & 0xffff;
   11272   1.1     skrll       buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
   11273   1.1     skrll 				NT_PSTATUS, &pstat, sizeof (pstat));
   11274   1.1     skrll       return buf;
   11275   1.1     skrll     }
   11276   1.1     skrll }
   11277   1.1     skrll #endif /* HAVE_PSTATUS_T */
   11278   1.1     skrll 
   11279   1.1     skrll char *
   11280   1.1     skrll elfcore_write_prfpreg (bfd *abfd,
   11281   1.1     skrll 		       char *buf,
   11282   1.1     skrll 		       int *bufsiz,
   11283   1.1     skrll 		       const void *fpregs,
   11284   1.1     skrll 		       int size)
   11285   1.1     skrll {
   11286   1.1     skrll   const char *note_name = "CORE";
   11287   1.1     skrll   return elfcore_write_note (abfd, buf, bufsiz,
   11288   1.1     skrll 			     note_name, NT_FPREGSET, fpregs, size);
   11289   1.1     skrll }
   11290   1.1     skrll 
   11291   1.1     skrll char *
   11292   1.1     skrll elfcore_write_prxfpreg (bfd *abfd,
   11293   1.1     skrll 			char *buf,
   11294   1.1     skrll 			int *bufsiz,
   11295   1.1     skrll 			const void *xfpregs,
   11296   1.1     skrll 			int size)
   11297   1.1     skrll {
   11298   1.1     skrll   char *note_name = "LINUX";
   11299   1.3  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11300   1.3  christos 			     note_name, NT_PRXFPREG, xfpregs, size);
   11301   1.3  christos }
   11302   1.6  christos 
   11303   1.6  christos char *
   11304   1.6  christos elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
   11305   1.6  christos 			 const void *xfpregs, int size)
   11306   1.6  christos {
   11307   1.3  christos   char *note_name;
   11308   1.3  christos   if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
   11309   1.3  christos     note_name = "FreeBSD";
   11310   1.3  christos   else
   11311   1.3  christos     note_name = "LINUX";
   11312   1.1     skrll   return elfcore_write_note (abfd, buf, bufsiz,
   11313   1.1     skrll 			     note_name, NT_X86_XSTATE, xfpregs, size);
   11314   1.1     skrll }
   11315   1.1     skrll 
   11316   1.1     skrll char *
   11317   1.1     skrll elfcore_write_ppc_vmx (bfd *abfd,
   11318   1.1     skrll 		       char *buf,
   11319   1.1     skrll 		       int *bufsiz,
   11320   1.1     skrll 		       const void *ppc_vmx,
   11321   1.1     skrll 		       int size)
   11322   1.1     skrll {
   11323   1.1     skrll   char *note_name = "LINUX";
   11324   1.1     skrll   return elfcore_write_note (abfd, buf, bufsiz,
   11325   1.9  christos 			     note_name, NT_PPC_VMX, ppc_vmx, size);
   11326   1.9  christos }
   11327   1.9  christos 
   11328   1.9  christos char *
   11329   1.1     skrll elfcore_write_ppc_vsx (bfd *abfd,
   11330   1.1     skrll 		       char *buf,
   11331   1.1     skrll 		       int *bufsiz,
   11332   1.9  christos 		       const void *ppc_vsx,
   11333   1.1     skrll 		       int size)
   11334   1.1     skrll {
   11335  1.15  christos   char *note_name = "LINUX";
   11336  1.15  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11337  1.15  christos 			     note_name, NT_PPC_VSX, ppc_vsx, size);
   11338  1.15  christos }
   11339  1.15  christos 
   11340  1.15  christos char *
   11341  1.15  christos elfcore_write_ppc_tar (bfd *abfd,
   11342  1.15  christos 		       char *buf,
   11343  1.15  christos 		       int *bufsiz,
   11344  1.15  christos 		       const void *ppc_tar,
   11345  1.15  christos 		       int size)
   11346  1.15  christos {
   11347  1.15  christos   char *note_name = "LINUX";
   11348  1.15  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11349  1.15  christos 			     note_name, NT_PPC_TAR, ppc_tar, size);
   11350  1.15  christos }
   11351  1.15  christos 
   11352  1.15  christos char *
   11353  1.15  christos elfcore_write_ppc_ppr (bfd *abfd,
   11354  1.15  christos 		       char *buf,
   11355  1.15  christos 		       int *bufsiz,
   11356  1.15  christos 		       const void *ppc_ppr,
   11357  1.15  christos 		       int size)
   11358  1.15  christos {
   11359  1.15  christos   char *note_name = "LINUX";
   11360  1.15  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11361  1.15  christos 			     note_name, NT_PPC_PPR, ppc_ppr, size);
   11362  1.15  christos }
   11363  1.15  christos 
   11364  1.15  christos char *
   11365  1.15  christos elfcore_write_ppc_dscr (bfd *abfd,
   11366  1.15  christos 			char *buf,
   11367  1.15  christos 			int *bufsiz,
   11368  1.15  christos 			const void *ppc_dscr,
   11369  1.15  christos 			int size)
   11370  1.15  christos {
   11371  1.15  christos   char *note_name = "LINUX";
   11372  1.15  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11373  1.15  christos 			     note_name, NT_PPC_DSCR, ppc_dscr, size);
   11374  1.15  christos }
   11375  1.15  christos 
   11376  1.15  christos char *
   11377  1.15  christos elfcore_write_ppc_ebb (bfd *abfd,
   11378  1.15  christos 		       char *buf,
   11379  1.15  christos 		       int *bufsiz,
   11380  1.15  christos 		       const void *ppc_ebb,
   11381  1.15  christos 		       int size)
   11382  1.15  christos {
   11383  1.15  christos   char *note_name = "LINUX";
   11384  1.15  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11385  1.15  christos 			     note_name, NT_PPC_EBB, ppc_ebb, size);
   11386  1.15  christos }
   11387  1.15  christos 
   11388  1.15  christos char *
   11389  1.15  christos elfcore_write_ppc_pmu (bfd *abfd,
   11390  1.15  christos 		       char *buf,
   11391  1.15  christos 		       int *bufsiz,
   11392  1.15  christos 		       const void *ppc_pmu,
   11393  1.15  christos 		       int size)
   11394  1.15  christos {
   11395  1.15  christos   char *note_name = "LINUX";
   11396  1.15  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11397  1.15  christos 			     note_name, NT_PPC_PMU, ppc_pmu, size);
   11398  1.15  christos }
   11399  1.15  christos 
   11400  1.15  christos char *
   11401  1.15  christos elfcore_write_ppc_tm_cgpr (bfd *abfd,
   11402  1.15  christos 			   char *buf,
   11403  1.15  christos 			   int *bufsiz,
   11404  1.15  christos 			   const void *ppc_tm_cgpr,
   11405  1.15  christos 			   int size)
   11406  1.15  christos {
   11407  1.15  christos   char *note_name = "LINUX";
   11408  1.15  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11409  1.15  christos 			     note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
   11410  1.15  christos }
   11411  1.15  christos 
   11412  1.15  christos char *
   11413  1.15  christos elfcore_write_ppc_tm_cfpr (bfd *abfd,
   11414  1.15  christos 			   char *buf,
   11415  1.15  christos 			   int *bufsiz,
   11416  1.15  christos 			   const void *ppc_tm_cfpr,
   11417  1.15  christos 			   int size)
   11418  1.15  christos {
   11419  1.15  christos   char *note_name = "LINUX";
   11420  1.15  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11421  1.15  christos 			     note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
   11422  1.15  christos }
   11423  1.15  christos 
   11424  1.15  christos char *
   11425  1.15  christos elfcore_write_ppc_tm_cvmx (bfd *abfd,
   11426  1.15  christos 			   char *buf,
   11427  1.15  christos 			   int *bufsiz,
   11428  1.15  christos 			   const void *ppc_tm_cvmx,
   11429  1.15  christos 			   int size)
   11430  1.15  christos {
   11431  1.15  christos   char *note_name = "LINUX";
   11432  1.15  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11433  1.15  christos 			     note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
   11434  1.15  christos }
   11435  1.15  christos 
   11436  1.15  christos char *
   11437  1.15  christos elfcore_write_ppc_tm_cvsx (bfd *abfd,
   11438  1.15  christos 			   char *buf,
   11439  1.15  christos 			   int *bufsiz,
   11440  1.15  christos 			   const void *ppc_tm_cvsx,
   11441  1.15  christos 			   int size)
   11442  1.15  christos {
   11443  1.15  christos   char *note_name = "LINUX";
   11444  1.15  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11445  1.15  christos 			     note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
   11446  1.15  christos }
   11447  1.15  christos 
   11448  1.15  christos char *
   11449  1.15  christos elfcore_write_ppc_tm_spr (bfd *abfd,
   11450  1.15  christos 			  char *buf,
   11451  1.15  christos 			  int *bufsiz,
   11452  1.15  christos 			  const void *ppc_tm_spr,
   11453  1.15  christos 			  int size)
   11454  1.15  christos {
   11455  1.15  christos   char *note_name = "LINUX";
   11456  1.15  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11457  1.15  christos 			     note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
   11458  1.15  christos }
   11459  1.15  christos 
   11460  1.15  christos char *
   11461  1.15  christos elfcore_write_ppc_tm_ctar (bfd *abfd,
   11462  1.15  christos 			   char *buf,
   11463  1.15  christos 			   int *bufsiz,
   11464  1.15  christos 			   const void *ppc_tm_ctar,
   11465  1.15  christos 			   int size)
   11466  1.15  christos {
   11467  1.15  christos   char *note_name = "LINUX";
   11468  1.15  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11469  1.15  christos 			     note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
   11470  1.15  christos }
   11471  1.15  christos 
   11472  1.15  christos char *
   11473  1.15  christos elfcore_write_ppc_tm_cppr (bfd *abfd,
   11474  1.15  christos 			   char *buf,
   11475  1.15  christos 			   int *bufsiz,
   11476  1.15  christos 			   const void *ppc_tm_cppr,
   11477  1.15  christos 			   int size)
   11478  1.15  christos {
   11479  1.15  christos   char *note_name = "LINUX";
   11480  1.15  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11481  1.15  christos 			     note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
   11482  1.15  christos }
   11483  1.15  christos 
   11484  1.15  christos char *
   11485  1.15  christos elfcore_write_ppc_tm_cdscr (bfd *abfd,
   11486  1.15  christos 			    char *buf,
   11487  1.15  christos 			    int *bufsiz,
   11488  1.15  christos 			    const void *ppc_tm_cdscr,
   11489  1.15  christos 			    int size)
   11490  1.15  christos {
   11491   1.3  christos   char *note_name = "LINUX";
   11492   1.3  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11493   1.3  christos 			     note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
   11494   1.3  christos }
   11495   1.3  christos 
   11496   1.3  christos static char *
   11497   1.3  christos elfcore_write_s390_high_gprs (bfd *abfd,
   11498   1.3  christos 			      char *buf,
   11499   1.3  christos 			      int *bufsiz,
   11500   1.9  christos 			      const void *s390_high_gprs,
   11501   1.3  christos 			      int size)
   11502   1.3  christos {
   11503   1.3  christos   char *note_name = "LINUX";
   11504   1.3  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11505   1.3  christos 			     note_name, NT_S390_HIGH_GPRS,
   11506   1.9  christos 			     s390_high_gprs, size);
   11507   1.9  christos }
   11508   1.9  christos 
   11509   1.9  christos char *
   11510   1.3  christos elfcore_write_s390_timer (bfd *abfd,
   11511   1.3  christos 			  char *buf,
   11512   1.3  christos 			  int *bufsiz,
   11513   1.9  christos 			  const void *s390_timer,
   11514   1.3  christos 			  int size)
   11515   1.3  christos {
   11516   1.3  christos   char *note_name = "LINUX";
   11517   1.3  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11518   1.9  christos 			     note_name, NT_S390_TIMER, s390_timer, size);
   11519   1.9  christos }
   11520   1.9  christos 
   11521   1.9  christos char *
   11522   1.3  christos elfcore_write_s390_todcmp (bfd *abfd,
   11523   1.3  christos 			   char *buf,
   11524   1.3  christos 			   int *bufsiz,
   11525   1.9  christos 			   const void *s390_todcmp,
   11526   1.3  christos 			   int size)
   11527   1.3  christos {
   11528   1.3  christos   char *note_name = "LINUX";
   11529   1.3  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11530   1.9  christos 			     note_name, NT_S390_TODCMP, s390_todcmp, size);
   11531   1.9  christos }
   11532   1.9  christos 
   11533   1.9  christos char *
   11534   1.3  christos elfcore_write_s390_todpreg (bfd *abfd,
   11535   1.3  christos 			    char *buf,
   11536   1.3  christos 			    int *bufsiz,
   11537   1.9  christos 			    const void *s390_todpreg,
   11538   1.3  christos 			    int size)
   11539   1.3  christos {
   11540   1.3  christos   char *note_name = "LINUX";
   11541   1.3  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11542   1.9  christos 			     note_name, NT_S390_TODPREG, s390_todpreg, size);
   11543   1.9  christos }
   11544   1.9  christos 
   11545   1.9  christos char *
   11546   1.3  christos elfcore_write_s390_ctrs (bfd *abfd,
   11547   1.3  christos 			 char *buf,
   11548   1.3  christos 			 int *bufsiz,
   11549   1.9  christos 			 const void *s390_ctrs,
   11550   1.3  christos 			 int size)
   11551   1.3  christos {
   11552   1.3  christos   char *note_name = "LINUX";
   11553   1.3  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11554   1.9  christos 			     note_name, NT_S390_CTRS, s390_ctrs, size);
   11555   1.9  christos }
   11556   1.9  christos 
   11557   1.9  christos char *
   11558   1.3  christos elfcore_write_s390_prefix (bfd *abfd,
   11559   1.3  christos 			   char *buf,
   11560   1.3  christos 			   int *bufsiz,
   11561   1.9  christos 			   const void *s390_prefix,
   11562   1.3  christos 			   int size)
   11563   1.3  christos {
   11564   1.1     skrll   char *note_name = "LINUX";
   11565   1.5  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11566   1.5  christos 			     note_name, NT_S390_PREFIX, s390_prefix, size);
   11567   1.5  christos }
   11568   1.5  christos 
   11569   1.5  christos char *
   11570   1.5  christos elfcore_write_s390_last_break (bfd *abfd,
   11571   1.5  christos 			       char *buf,
   11572   1.5  christos 			       int *bufsiz,
   11573   1.9  christos 			       const void *s390_last_break,
   11574   1.5  christos 			       int size)
   11575   1.5  christos {
   11576   1.5  christos   char *note_name = "LINUX";
   11577   1.5  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11578   1.5  christos 			     note_name, NT_S390_LAST_BREAK,
   11579   1.5  christos 			     s390_last_break, size);
   11580   1.5  christos }
   11581   1.5  christos 
   11582   1.5  christos char *
   11583   1.5  christos elfcore_write_s390_system_call (bfd *abfd,
   11584   1.5  christos 				char *buf,
   11585   1.5  christos 				int *bufsiz,
   11586   1.9  christos 				const void *s390_system_call,
   11587   1.5  christos 				int size)
   11588   1.5  christos {
   11589   1.5  christos   char *note_name = "LINUX";
   11590   1.5  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11591   1.6  christos 			     note_name, NT_S390_SYSTEM_CALL,
   11592   1.6  christos 			     s390_system_call, size);
   11593   1.6  christos }
   11594   1.6  christos 
   11595   1.6  christos char *
   11596   1.6  christos elfcore_write_s390_tdb (bfd *abfd,
   11597   1.6  christos 			char *buf,
   11598   1.6  christos 			int *bufsiz,
   11599   1.9  christos 			const void *s390_tdb,
   11600   1.6  christos 			int size)
   11601   1.6  christos {
   11602   1.6  christos   char *note_name = "LINUX";
   11603   1.6  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11604   1.6  christos 			     note_name, NT_S390_TDB, s390_tdb, size);
   11605   1.6  christos }
   11606   1.6  christos 
   11607   1.6  christos char *
   11608   1.6  christos elfcore_write_s390_vxrs_low (bfd *abfd,
   11609   1.6  christos 			     char *buf,
   11610   1.6  christos 			     int *bufsiz,
   11611   1.6  christos 			     const void *s390_vxrs_low,
   11612   1.6  christos 			     int size)
   11613   1.6  christos {
   11614   1.6  christos   char *note_name = "LINUX";
   11615   1.6  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11616   1.6  christos 			     note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
   11617   1.6  christos }
   11618   1.6  christos 
   11619   1.6  christos char *
   11620   1.6  christos elfcore_write_s390_vxrs_high (bfd *abfd,
   11621   1.6  christos 			     char *buf,
   11622   1.6  christos 			     int *bufsiz,
   11623   1.6  christos 			     const void *s390_vxrs_high,
   11624   1.6  christos 			     int size)
   11625   1.6  christos {
   11626   1.6  christos   char *note_name = "LINUX";
   11627   1.6  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11628   1.9  christos 			     note_name, NT_S390_VXRS_HIGH,
   11629   1.9  christos 			     s390_vxrs_high, size);
   11630   1.9  christos }
   11631   1.9  christos 
   11632   1.9  christos char *
   11633   1.9  christos elfcore_write_s390_gs_cb (bfd *abfd,
   11634   1.9  christos 			  char *buf,
   11635   1.9  christos 			  int *bufsiz,
   11636   1.9  christos 			  const void *s390_gs_cb,
   11637   1.9  christos 			  int size)
   11638   1.9  christos {
   11639   1.9  christos   char *note_name = "LINUX";
   11640   1.9  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11641   1.9  christos 			     note_name, NT_S390_GS_CB,
   11642   1.9  christos 			     s390_gs_cb, size);
   11643   1.9  christos }
   11644   1.9  christos 
   11645   1.9  christos char *
   11646   1.9  christos elfcore_write_s390_gs_bc (bfd *abfd,
   11647   1.9  christos 			  char *buf,
   11648   1.9  christos 			  int *bufsiz,
   11649   1.9  christos 			  const void *s390_gs_bc,
   11650   1.9  christos 			  int size)
   11651   1.9  christos {
   11652   1.9  christos   char *note_name = "LINUX";
   11653   1.9  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11654   1.5  christos 			     note_name, NT_S390_GS_BC,
   11655   1.5  christos 			     s390_gs_bc, size);
   11656   1.5  christos }
   11657   1.5  christos 
   11658   1.5  christos char *
   11659   1.5  christos elfcore_write_arm_vfp (bfd *abfd,
   11660   1.5  christos 		       char *buf,
   11661   1.5  christos 		       int *bufsiz,
   11662   1.5  christos 		       const void *arm_vfp,
   11663   1.5  christos 		       int size)
   11664   1.5  christos {
   11665   1.5  christos   char *note_name = "LINUX";
   11666   1.6  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11667   1.6  christos 			     note_name, NT_ARM_VFP, arm_vfp, size);
   11668   1.6  christos }
   11669   1.6  christos 
   11670   1.6  christos char *
   11671   1.6  christos elfcore_write_aarch_tls (bfd *abfd,
   11672   1.6  christos 		       char *buf,
   11673   1.6  christos 		       int *bufsiz,
   11674   1.6  christos 		       const void *aarch_tls,
   11675   1.6  christos 		       int size)
   11676   1.6  christos {
   11677   1.6  christos   char *note_name = "LINUX";
   11678   1.6  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11679   1.6  christos 			     note_name, NT_ARM_TLS, aarch_tls, size);
   11680   1.6  christos }
   11681   1.6  christos 
   11682   1.6  christos char *
   11683   1.6  christos elfcore_write_aarch_hw_break (bfd *abfd,
   11684   1.6  christos 			    char *buf,
   11685   1.6  christos 			    int *bufsiz,
   11686   1.6  christos 			    const void *aarch_hw_break,
   11687   1.6  christos 			    int size)
   11688   1.6  christos {
   11689   1.6  christos   char *note_name = "LINUX";
   11690   1.6  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11691   1.6  christos 			     note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
   11692   1.6  christos }
   11693   1.6  christos 
   11694   1.6  christos char *
   11695   1.6  christos elfcore_write_aarch_hw_watch (bfd *abfd,
   11696   1.6  christos 			    char *buf,
   11697   1.6  christos 			    int *bufsiz,
   11698   1.6  christos 			    const void *aarch_hw_watch,
   11699   1.6  christos 			    int size)
   11700   1.6  christos {
   11701   1.6  christos   char *note_name = "LINUX";
   11702  1.15  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11703  1.15  christos 			     note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
   11704  1.15  christos }
   11705  1.15  christos 
   11706  1.15  christos char *
   11707  1.15  christos elfcore_write_aarch_sve (bfd *abfd,
   11708  1.15  christos 			 char *buf,
   11709  1.15  christos 			 int *bufsiz,
   11710  1.15  christos 			 const void *aarch_sve,
   11711  1.15  christos 			 int size)
   11712  1.15  christos {
   11713  1.15  christos   char *note_name = "LINUX";
   11714  1.15  christos   return elfcore_write_note (abfd, buf, bufsiz,
   11715  1.15  christos 			     note_name, NT_ARM_SVE, aarch_sve, size);
   11716  1.15  christos }
   11717  1.15  christos 
   11718  1.15  christos char *
   11719  1.15  christos elfcore_write_aarch_pauth (bfd *abfd,
   11720  1.15  christos 			   char *buf,
   11721  1.15  christos 			   int *bufsiz,
   11722  1.15  christos 			   const void *aarch_pauth,
   11723  1.15  christos 			   int size)
   11724  1.15  christos {
   11725  1.15  christos   char *note_name = "LINUX";
   11726   1.1     skrll   return elfcore_write_note (abfd, buf, bufsiz,
   11727   1.1     skrll 			     note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
   11728   1.1     skrll }
   11729   1.1     skrll 
   11730   1.1     skrll char *
   11731   1.1     skrll elfcore_write_register_note (bfd *abfd,
   11732   1.1     skrll 			     char *buf,
   11733   1.1     skrll 			     int *bufsiz,
   11734   1.1     skrll 			     const char *section,
   11735   1.1     skrll 			     const void *data,
   11736   1.1     skrll 			     int size)
   11737   1.3  christos {
   11738   1.3  christos   if (strcmp (section, ".reg2") == 0)
   11739   1.1     skrll     return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
   11740   1.1     skrll   if (strcmp (section, ".reg-xfp") == 0)
   11741   1.1     skrll     return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
   11742   1.1     skrll   if (strcmp (section, ".reg-xstate") == 0)
   11743  1.15  christos     return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
   11744  1.15  christos   if (strcmp (section, ".reg-ppc-vmx") == 0)
   11745  1.15  christos     return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
   11746  1.15  christos   if (strcmp (section, ".reg-ppc-vsx") == 0)
   11747  1.15  christos     return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
   11748  1.15  christos   if (strcmp (section, ".reg-ppc-tar") == 0)
   11749  1.15  christos     return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
   11750  1.15  christos   if (strcmp (section, ".reg-ppc-ppr") == 0)
   11751  1.15  christos     return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
   11752  1.15  christos   if (strcmp (section, ".reg-ppc-dscr") == 0)
   11753  1.15  christos     return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
   11754  1.15  christos   if (strcmp (section, ".reg-ppc-ebb") == 0)
   11755  1.15  christos     return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
   11756  1.15  christos   if (strcmp (section, ".reg-ppc-pmu") == 0)
   11757  1.15  christos     return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
   11758  1.15  christos   if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
   11759  1.15  christos     return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
   11760  1.15  christos   if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
   11761  1.15  christos     return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
   11762  1.15  christos   if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
   11763  1.15  christos     return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
   11764  1.15  christos   if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
   11765  1.15  christos     return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
   11766  1.15  christos   if (strcmp (section, ".reg-ppc-tm-spr") == 0)
   11767  1.15  christos     return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
   11768  1.15  christos   if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
   11769   1.3  christos     return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
   11770   1.3  christos   if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
   11771   1.3  christos     return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
   11772   1.3  christos   if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
   11773   1.3  christos     return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
   11774   1.3  christos   if (strcmp (section, ".reg-s390-high-gprs") == 0)
   11775   1.3  christos     return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
   11776   1.3  christos   if (strcmp (section, ".reg-s390-timer") == 0)
   11777   1.3  christos     return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
   11778   1.3  christos   if (strcmp (section, ".reg-s390-todcmp") == 0)
   11779   1.3  christos     return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
   11780   1.3  christos   if (strcmp (section, ".reg-s390-todpreg") == 0)
   11781   1.5  christos     return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
   11782   1.5  christos   if (strcmp (section, ".reg-s390-ctrs") == 0)
   11783   1.5  christos     return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
   11784   1.5  christos   if (strcmp (section, ".reg-s390-prefix") == 0)
   11785   1.6  christos     return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
   11786   1.6  christos   if (strcmp (section, ".reg-s390-last-break") == 0)
   11787   1.6  christos     return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
   11788   1.6  christos   if (strcmp (section, ".reg-s390-system-call") == 0)
   11789   1.6  christos     return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
   11790   1.6  christos   if (strcmp (section, ".reg-s390-tdb") == 0)
   11791   1.9  christos     return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
   11792   1.9  christos   if (strcmp (section, ".reg-s390-vxrs-low") == 0)
   11793   1.9  christos     return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
   11794   1.9  christos   if (strcmp (section, ".reg-s390-vxrs-high") == 0)
   11795   1.5  christos     return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
   11796   1.5  christos   if (strcmp (section, ".reg-s390-gs-cb") == 0)
   11797   1.6  christos     return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
   11798   1.6  christos   if (strcmp (section, ".reg-s390-gs-bc") == 0)
   11799   1.6  christos     return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
   11800   1.6  christos   if (strcmp (section, ".reg-arm-vfp") == 0)
   11801   1.6  christos     return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
   11802   1.6  christos   if (strcmp (section, ".reg-aarch-tls") == 0)
   11803  1.15  christos     return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
   11804  1.15  christos   if (strcmp (section, ".reg-aarch-hw-break") == 0)
   11805  1.15  christos     return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
   11806  1.15  christos   if (strcmp (section, ".reg-aarch-hw-watch") == 0)
   11807   1.1     skrll     return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
   11808   1.1     skrll   if (strcmp (section, ".reg-aarch-sve") == 0)
   11809   1.1     skrll     return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
   11810   1.1     skrll   if (strcmp (section, ".reg-aarch-pauth") == 0)
   11811   1.9  christos     return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
   11812   1.9  christos   return NULL;
   11813   1.1     skrll }
   11814   1.1     skrll 
   11815   1.1     skrll static bfd_boolean
   11816   1.9  christos elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
   11817   1.9  christos 		 size_t align)
   11818   1.9  christos {
   11819   1.9  christos   char *p;
   11820   1.9  christos 
   11821   1.9  christos   /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
   11822  1.13  christos      gABI specifies that PT_NOTE alignment should be aligned to 4
   11823  1.13  christos      bytes for 32-bit objects and to 8 bytes for 64-bit objects.  If
   11824   1.9  christos      align is less than 4, we use 4 byte alignment.   */
   11825   1.1     skrll   if (align < 4)
   11826   1.1     skrll     align = 4;
   11827   1.1     skrll   if (align != 4 && align != 8)
   11828   1.1     skrll     return FALSE;
   11829   1.1     skrll 
   11830   1.1     skrll   p = buf;
   11831   1.1     skrll   while (p < buf + size)
   11832   1.1     skrll     {
   11833   1.1     skrll       Elf_External_Note *xnp = (Elf_External_Note *) p;
   11834   1.1     skrll       Elf_Internal_Note in;
   11835   1.1     skrll 
   11836   1.1     skrll       if (offsetof (Elf_External_Note, name) > buf - p + size)
   11837   1.1     skrll 	return FALSE;
   11838   1.1     skrll 
   11839   1.1     skrll       in.type = H_GET_32 (abfd, xnp->type);
   11840   1.1     skrll 
   11841   1.1     skrll       in.namesz = H_GET_32 (abfd, xnp->namesz);
   11842   1.9  christos       in.namedata = xnp->name;
   11843   1.1     skrll       if (in.namesz > buf - in.namedata + size)
   11844   1.1     skrll 	return FALSE;
   11845   1.1     skrll 
   11846   1.1     skrll       in.descsz = H_GET_32 (abfd, xnp->descsz);
   11847   1.1     skrll       in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
   11848   1.1     skrll       in.descpos = offset + (in.descdata - buf);
   11849   1.1     skrll       if (in.descsz != 0
   11850   1.9  christos 	  && (in.descdata >= buf + size
   11851   1.1     skrll 	      || in.descsz > buf - in.descdata + size))
   11852   1.1     skrll 	return FALSE;
   11853   1.1     skrll 
   11854   1.1     skrll       switch (bfd_get_format (abfd))
   11855   1.6  christos 	{
   11856   1.6  christos 	default:
   11857   1.6  christos 	  return TRUE;
   11858   1.1     skrll 
   11859   1.6  christos 	case bfd_core:
   11860   1.6  christos 	  {
   11861   1.6  christos #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
   11862   1.1     skrll 	    struct
   11863   1.6  christos 	    {
   11864   1.3  christos 	      const char * string;
   11865   1.6  christos 	      size_t len;
   11866   1.7  christos 	      bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
   11867   1.6  christos 	    }
   11868   1.6  christos 	    grokers[] =
   11869   1.6  christos 	    {
   11870  1.15  christos 	      GROKER_ELEMENT ("", elfcore_grok_note),
   11871  1.15  christos 	      GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
   11872   1.6  christos 	      GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
   11873   1.6  christos 	      GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
   11874   1.6  christos 	      GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
   11875   1.6  christos 	      GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
   11876   1.6  christos 	      GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note)
   11877   1.6  christos 	    };
   11878   1.6  christos #undef GROKER_ELEMENT
   11879   1.6  christos 	    int i;
   11880   1.6  christos 
   11881   1.6  christos 	    for (i = ARRAY_SIZE (grokers); i--;)
   11882   1.6  christos 	      {
   11883   1.6  christos 		if (in.namesz >= grokers[i].len
   11884   1.6  christos 		    && strncmp (in.namedata, grokers[i].string,
   11885   1.6  christos 				grokers[i].len) == 0)
   11886   1.6  christos 		  {
   11887   1.6  christos 		    if (! grokers[i].func (abfd, & in))
   11888   1.6  christos 		      return FALSE;
   11889   1.1     skrll 		    break;
   11890   1.1     skrll 		  }
   11891   1.1     skrll 	      }
   11892   1.1     skrll 	    break;
   11893   1.1     skrll 	  }
   11894   1.1     skrll 
   11895   1.1     skrll 	case bfd_object:
   11896   1.5  christos 	  if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
   11897   1.5  christos 	    {
   11898   1.5  christos 	      if (! elfobj_grok_gnu_note (abfd, &in))
   11899   1.5  christos 		return FALSE;
   11900   1.5  christos 	    }
   11901   1.5  christos 	  else if (in.namesz == sizeof "stapsdt"
   11902   1.1     skrll 		   && strcmp (in.namedata, "stapsdt") == 0)
   11903   1.1     skrll 	    {
   11904   1.1     skrll 	      if (! elfobj_grok_stapsdt_note (abfd, &in))
   11905   1.9  christos 		return FALSE;
   11906   1.1     skrll 	    }
   11907   1.1     skrll 	  break;
   11908   1.1     skrll 	}
   11909   1.1     skrll 
   11910   1.1     skrll       p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
   11911  1.15  christos     }
   11912   1.9  christos 
   11913   1.9  christos   return TRUE;
   11914   1.1     skrll }
   11915   1.1     skrll 
   11916   1.1     skrll bfd_boolean
   11917   1.9  christos elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
   11918   1.1     skrll 		size_t align)
   11919   1.1     skrll {
   11920   1.1     skrll   char *buf;
   11921   1.1     skrll 
   11922   1.1     skrll   if (size == 0 || (size + 1) == 0)
   11923   1.6  christos     return TRUE;
   11924   1.1     skrll 
   11925   1.1     skrll   if (bfd_seek (abfd, offset, SEEK_SET) != 0)
   11926   1.1     skrll     return FALSE;
   11927   1.6  christos 
   11928   1.6  christos   buf = (char *) bfd_malloc (size + 1);
   11929   1.6  christos   if (buf == NULL)
   11930   1.6  christos     return FALSE;
   11931   1.1     skrll 
   11932   1.9  christos   /* PR 17512: file: ec08f814
   11933   1.1     skrll      0-termintate the buffer so that string searches will not overflow.  */
   11934   1.1     skrll   buf[size] = 0;
   11935   1.1     skrll 
   11936   1.1     skrll   if (bfd_bread (buf, size, abfd) != size
   11937   1.1     skrll       || !elf_parse_notes (abfd, buf, size, offset, align))
   11938   1.1     skrll     {
   11939   1.1     skrll       free (buf);
   11940   1.1     skrll       return FALSE;
   11941   1.1     skrll     }
   11942   1.1     skrll 
   11943   1.1     skrll   free (buf);
   11944   1.1     skrll   return TRUE;
   11945   1.1     skrll }
   11946   1.1     skrll 
   11947   1.1     skrll /* Providing external access to the ELF program header table.  */
   11949   1.1     skrll 
   11950   1.1     skrll /* Return an upper bound on the number of bytes required to store a
   11951   1.1     skrll    copy of ABFD's program header table entries.  Return -1 if an error
   11952   1.1     skrll    occurs; bfd_get_error will return an appropriate code.  */
   11953   1.1     skrll 
   11954   1.1     skrll long
   11955   1.1     skrll bfd_get_elf_phdr_upper_bound (bfd *abfd)
   11956   1.1     skrll {
   11957   1.1     skrll   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   11958   1.1     skrll     {
   11959   1.1     skrll       bfd_set_error (bfd_error_wrong_format);
   11960   1.1     skrll       return -1;
   11961   1.1     skrll     }
   11962   1.1     skrll 
   11963   1.1     skrll   return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
   11964   1.1     skrll }
   11965   1.1     skrll 
   11966   1.1     skrll /* Copy ABFD's program header table entries to *PHDRS.  The entries
   11967   1.1     skrll    will be stored as an array of Elf_Internal_Phdr structures, as
   11968   1.1     skrll    defined in include/elf/internal.h.  To find out how large the
   11969   1.1     skrll    buffer needs to be, call bfd_get_elf_phdr_upper_bound.
   11970   1.1     skrll 
   11971   1.1     skrll    Return the number of program header table entries read, or -1 if an
   11972   1.1     skrll    error occurs; bfd_get_error will return an appropriate code.  */
   11973   1.1     skrll 
   11974   1.1     skrll int
   11975   1.1     skrll bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
   11976   1.1     skrll {
   11977   1.1     skrll   int num_phdrs;
   11978   1.1     skrll 
   11979   1.1     skrll   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   11980  1.15  christos     {
   11981  1.15  christos       bfd_set_error (bfd_error_wrong_format);
   11982  1.15  christos       return -1;
   11983   1.1     skrll     }
   11984   1.1     skrll 
   11985   1.1     skrll   num_phdrs = elf_elfheader (abfd)->e_phnum;
   11986   1.1     skrll   if (num_phdrs != 0)
   11987   1.1     skrll     memcpy (phdrs, elf_tdata (abfd)->phdr,
   11988   1.6  christos 	    num_phdrs * sizeof (Elf_Internal_Phdr));
   11989   1.6  christos 
   11990   1.6  christos   return num_phdrs;
   11991   1.1     skrll }
   11992   1.1     skrll 
   11993   1.1     skrll enum elf_reloc_type_class
   11994   1.1     skrll _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   11995   1.1     skrll 			   const asection *rel_sec ATTRIBUTE_UNUSED,
   11996   1.1     skrll 			   const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
   11997   1.1     skrll {
   11998   1.1     skrll   return reloc_class_normal;
   11999   1.1     skrll }
   12000   1.1     skrll 
   12001   1.1     skrll /* For RELA architectures, return the relocation value for a
   12002   1.1     skrll    relocation against a local symbol.  */
   12003   1.1     skrll 
   12004   1.1     skrll bfd_vma
   12005   1.1     skrll _bfd_elf_rela_local_sym (bfd *abfd,
   12006   1.1     skrll 			 Elf_Internal_Sym *sym,
   12007   1.1     skrll 			 asection **psec,
   12008   1.1     skrll 			 Elf_Internal_Rela *rel)
   12009   1.1     skrll {
   12010   1.1     skrll   asection *sec = *psec;
   12011   1.1     skrll   bfd_vma relocation;
   12012   1.5  christos 
   12013   1.1     skrll   relocation = (sec->output_section->vma
   12014   1.1     skrll 		+ sec->output_offset
   12015   1.1     skrll 		+ sym->st_value);
   12016   1.1     skrll   if ((sec->flags & SEC_MERGE)
   12017   1.1     skrll       && ELF_ST_TYPE (sym->st_info) == STT_SECTION
   12018   1.1     skrll       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
   12019   1.1     skrll     {
   12020   1.1     skrll       rel->r_addend =
   12021   1.1     skrll 	_bfd_merged_section_offset (abfd, psec,
   12022   1.1     skrll 				    elf_section_data (sec)->sec_info,
   12023   1.1     skrll 				    sym->st_value + rel->r_addend);
   12024   1.1     skrll       if (sec != *psec)
   12025   1.1     skrll 	{
   12026   1.1     skrll 	  /* If we have changed the section, and our original section is
   12027   1.1     skrll 	     marked with SEC_EXCLUDE, it means that the original
   12028   1.1     skrll 	     SEC_MERGE section has been completely subsumed in some
   12029   1.1     skrll 	     other SEC_MERGE section.  In this case, we need to leave
   12030   1.1     skrll 	     some info around for --emit-relocs.  */
   12031   1.1     skrll 	  if ((sec->flags & SEC_EXCLUDE) != 0)
   12032   1.1     skrll 	    sec->kept_section = *psec;
   12033   1.1     skrll 	  sec = *psec;
   12034   1.1     skrll 	}
   12035   1.1     skrll       rel->r_addend -= relocation;
   12036   1.1     skrll       rel->r_addend += sec->output_section->vma + sec->output_offset;
   12037   1.1     skrll     }
   12038   1.1     skrll   return relocation;
   12039   1.1     skrll }
   12040   1.1     skrll 
   12041   1.1     skrll bfd_vma
   12042   1.1     skrll _bfd_elf_rel_local_sym (bfd *abfd,
   12043   1.5  christos 			Elf_Internal_Sym *sym,
   12044   1.1     skrll 			asection **psec,
   12045   1.1     skrll 			bfd_vma addend)
   12046   1.1     skrll {
   12047   1.1     skrll   asection *sec = *psec;
   12048   1.1     skrll 
   12049   1.1     skrll   if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
   12050   1.1     skrll     return sym->st_value + addend;
   12051   1.7  christos 
   12052   1.7  christos   return _bfd_merged_section_offset (abfd, psec,
   12053   1.7  christos 				     elf_section_data (sec)->sec_info,
   12054   1.7  christos 				     sym->st_value + addend);
   12055   1.7  christos }
   12056   1.7  christos 
   12057   1.1     skrll /* Adjust an address within a section.  Given OFFSET within SEC, return
   12058   1.1     skrll    the new offset within the section, based upon changes made to the
   12059   1.1     skrll    section.  Returns -1 if the offset is now invalid.
   12060   1.1     skrll    The offset (in abnd out) is in target sized bytes, however big a
   12061   1.1     skrll    byte may be.  */
   12062   1.1     skrll 
   12063   1.1     skrll bfd_vma
   12064   1.1     skrll _bfd_elf_section_offset (bfd *abfd,
   12065   1.5  christos 			 struct bfd_link_info *info,
   12066   1.1     skrll 			 asection *sec,
   12067   1.1     skrll 			 bfd_vma offset)
   12068   1.5  christos {
   12069   1.1     skrll   switch (sec->sec_info_type)
   12070   1.7  christos     {
   12071   1.1     skrll     case SEC_INFO_TYPE_STABS:
   12072   1.5  christos       return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
   12073   1.5  christos 				       offset);
   12074   1.7  christos     case SEC_INFO_TYPE_EH_FRAME:
   12075   1.5  christos       return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
   12076   1.5  christos 
   12077   1.7  christos     default:
   12078   1.7  christos       if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
   12079   1.7  christos 	{
   12080  1.15  christos 	  /* Reverse the offset.  */
   12081  1.15  christos 	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12082   1.5  christos 	  bfd_size_type address_size = bed->s->arch_size / 8;
   12083   1.1     skrll 
   12084   1.1     skrll 	  /* address_size and sec->size are in octets.  Convert
   12085   1.1     skrll 	     to bytes before subtracting the original offset.  */
   12086   1.1     skrll 	  offset = ((sec->size - address_size)
   12087   1.1     skrll 		    / bfd_octets_per_byte (abfd, sec) - offset);
   12088   1.1     skrll 	}
   12089   1.1     skrll       return offset;
   12090   1.1     skrll     }
   12091   1.1     skrll }
   12092   1.1     skrll 
   12093   1.1     skrll /* Create a new BFD as if by bfd_openr.  Rather than opening a file,
   12095   1.1     skrll    reconstruct an ELF file by reading the segments out of remote memory
   12096   1.1     skrll    based on the ELF file header at EHDR_VMA and the ELF program headers it
   12097   1.1     skrll    points to.  If not null, *LOADBASEP is filled in with the difference
   12098   1.1     skrll    between the VMAs from which the segments were read, and the VMAs the
   12099   1.1     skrll    file headers (and hence BFD's idea of each section's VMA) put them at.
   12100   1.1     skrll 
   12101   1.1     skrll    The function TARGET_READ_MEMORY is called to copy LEN bytes from the
   12102   1.1     skrll    remote memory at target address VMA into the local buffer at MYADDR; it
   12103   1.1     skrll    should return zero on success or an `errno' code on failure.  TEMPL must
   12104   1.6  christos    be a BFD for an ELF target with the word size and byte order found in
   12105   1.1     skrll    the remote memory.  */
   12106   1.5  christos 
   12107   1.1     skrll bfd *
   12108   1.1     skrll bfd_elf_bfd_from_remote_memory
   12109   1.6  christos   (bfd *templ,
   12110   1.1     skrll    bfd_vma ehdr_vma,
   12111   1.1     skrll    bfd_size_type size,
   12112   1.1     skrll    bfd_vma *loadbasep,
   12113   1.1     skrll    int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
   12114   1.1     skrll {
   12115   1.1     skrll   return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
   12116   1.1     skrll     (templ, ehdr_vma, size, loadbasep, target_read_memory);
   12117   1.1     skrll }
   12118   1.1     skrll 
   12119   1.1     skrll long
   12121   1.1     skrll _bfd_elf_get_synthetic_symtab (bfd *abfd,
   12122   1.1     skrll 			       long symcount ATTRIBUTE_UNUSED,
   12123   1.1     skrll 			       asymbol **syms ATTRIBUTE_UNUSED,
   12124   1.1     skrll 			       long dynsymcount,
   12125   1.1     skrll 			       asymbol **dynsyms,
   12126   1.1     skrll 			       asymbol **ret)
   12127   1.1     skrll {
   12128   1.1     skrll   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12129   1.1     skrll   asection *relplt;
   12130   1.1     skrll   asymbol *s;
   12131   1.1     skrll   const char *relplt_name;
   12132   1.1     skrll   bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
   12133   1.1     skrll   arelent *p;
   12134   1.1     skrll   long count, i, n;
   12135   1.1     skrll   size_t size;
   12136   1.1     skrll   Elf_Internal_Shdr *hdr;
   12137   1.1     skrll   char *names;
   12138   1.1     skrll   asection *plt;
   12139   1.1     skrll 
   12140   1.1     skrll   *ret = NULL;
   12141   1.1     skrll 
   12142   1.1     skrll   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
   12143   1.1     skrll     return 0;
   12144   1.1     skrll 
   12145   1.1     skrll   if (dynsymcount <= 0)
   12146   1.1     skrll     return 0;
   12147   1.1     skrll 
   12148   1.1     skrll   if (!bed->plt_sym_val)
   12149   1.1     skrll     return 0;
   12150   1.1     skrll 
   12151   1.1     skrll   relplt_name = bed->relplt_name;
   12152   1.1     skrll   if (relplt_name == NULL)
   12153   1.1     skrll     relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
   12154   1.1     skrll   relplt = bfd_get_section_by_name (abfd, relplt_name);
   12155   1.1     skrll   if (relplt == NULL)
   12156   1.1     skrll     return 0;
   12157   1.1     skrll 
   12158   1.1     skrll   hdr = &elf_section_data (relplt)->this_hdr;
   12159   1.1     skrll   if (hdr->sh_link != elf_dynsymtab (abfd)
   12160   1.1     skrll       || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
   12161   1.1     skrll     return 0;
   12162   1.1     skrll 
   12163   1.1     skrll   plt = bfd_get_section_by_name (abfd, ".plt");
   12164   1.1     skrll   if (plt == NULL)
   12165   1.1     skrll     return 0;
   12166   1.1     skrll 
   12167   1.3  christos   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   12168   1.3  christos   if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
   12169   1.3  christos     return -1;
   12170   1.3  christos 
   12171   1.3  christos   count = relplt->size / hdr->sh_entsize;
   12172   1.3  christos   size = count * sizeof (asymbol);
   12173   1.3  christos   p = relplt->relocation;
   12174   1.3  christos   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   12175   1.3  christos     {
   12176   1.3  christos       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
   12177   1.3  christos       if (p->addend != 0)
   12178   1.1     skrll 	{
   12179   1.3  christos #ifdef BFD64
   12180   1.1     skrll 	  size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
   12181   1.1     skrll #else
   12182   1.1     skrll 	  size += sizeof ("+0x") - 1 + 8;
   12183   1.1     skrll #endif
   12184   1.1     skrll 	}
   12185   1.1     skrll     }
   12186   1.1     skrll 
   12187   1.1     skrll   s = *ret = (asymbol *) bfd_malloc (size);
   12188   1.1     skrll   if (s == NULL)
   12189   1.1     skrll     return -1;
   12190   1.1     skrll 
   12191   1.1     skrll   names = (char *) (s + count);
   12192   1.1     skrll   p = relplt->relocation;
   12193   1.1     skrll   n = 0;
   12194   1.1     skrll   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   12195   1.1     skrll     {
   12196   1.1     skrll       size_t len;
   12197   1.1     skrll       bfd_vma addr;
   12198   1.1     skrll 
   12199   1.1     skrll       addr = bed->plt_sym_val (i, plt, p);
   12200   1.1     skrll       if (addr == (bfd_vma) -1)
   12201   1.1     skrll 	continue;
   12202   1.1     skrll 
   12203   1.1     skrll       *s = **p->sym_ptr_ptr;
   12204   1.1     skrll       /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   12205   1.1     skrll 	 we are defining a symbol, ensure one of them is set.  */
   12206   1.1     skrll       if ((s->flags & BSF_LOCAL) == 0)
   12207   1.1     skrll 	s->flags |= BSF_GLOBAL;
   12208   1.3  christos       s->flags |= BSF_SYNTHETIC;
   12209   1.3  christos       s->section = plt;
   12210   1.3  christos       s->value = addr - plt->vma;
   12211   1.5  christos       s->name = names;
   12212   1.3  christos       s->udata.p = NULL;
   12213   1.3  christos       len = strlen ((*p->sym_ptr_ptr)->name);
   12214   1.3  christos       memcpy (names, (*p->sym_ptr_ptr)->name, len);
   12215   1.3  christos       names += len;
   12216   1.3  christos       if (p->addend != 0)
   12217   1.3  christos 	{
   12218   1.3  christos 	  char buf[30], *a;
   12219   1.3  christos 
   12220   1.3  christos 	  memcpy (names, "+0x", sizeof ("+0x") - 1);
   12221   1.1     skrll 	  names += sizeof ("+0x") - 1;
   12222   1.1     skrll 	  bfd_sprintf_vma (abfd, buf, p->addend);
   12223   1.1     skrll 	  for (a = buf; *a == '0'; ++a)
   12224   1.1     skrll 	    ;
   12225   1.1     skrll 	  len = strlen (a);
   12226   1.1     skrll 	  memcpy (names, a, len);
   12227   1.1     skrll 	  names += len;
   12228   1.1     skrll 	}
   12229   1.9  christos       memcpy (names, "@plt", sizeof ("@plt"));
   12230   1.9  christos       names += sizeof ("@plt");
   12231   1.9  christos       ++s, ++n;
   12232   1.9  christos     }
   12233   1.9  christos 
   12234   1.1     skrll   return n;
   12235   1.9  christos }
   12236   1.9  christos 
   12237   1.1     skrll /* It is only used by x86-64 so far.
   12238  1.15  christos    ??? This repeats *COM* id of zero.  sec->id is supposed to be unique,
   12239  1.15  christos    but current usage would allow all of _bfd_std_section to be zero.  */
   12240   1.1     skrll static const asymbol lcomm_sym
   12241  1.15  christos   = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
   12242   1.1     skrll asection _bfd_elf_large_com_section
   12243   1.1     skrll   = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
   12244   1.1     skrll 		      "LARGE_COMMON", 0, SEC_IS_COMMON);
   12245  1.15  christos 
   12246  1.15  christos bfd_boolean
   12247   1.3  christos _bfd_elf_final_write_processing (bfd *abfd)
   12248  1.15  christos {
   12249  1.15  christos   Elf_Internal_Ehdr *i_ehdrp;	/* ELF file header, internal form.  */
   12250  1.15  christos 
   12251  1.15  christos   i_ehdrp = elf_elfheader (abfd);
   12252  1.15  christos 
   12253  1.15  christos   if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
   12254  1.15  christos     i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
   12255  1.15  christos 
   12256  1.15  christos   /* Set the osabi field to ELFOSABI_GNU if the binary contains
   12257  1.15  christos      SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or
   12258  1.15  christos      STB_GNU_UNIQUE binding.  */
   12259  1.15  christos   if (elf_tdata (abfd)->has_gnu_osabi != 0)
   12260  1.15  christos     {
   12261  1.15  christos       if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
   12262  1.15  christos 	i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
   12263  1.15  christos       else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
   12264  1.15  christos 	       && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
   12265  1.15  christos 	{
   12266  1.15  christos 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
   12267  1.15  christos 	    _bfd_error_handler (_("GNU_MBIND section is unsupported"));
   12268  1.15  christos 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
   12269   1.1     skrll 	    _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported"));
   12270   1.1     skrll 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
   12271   1.1     skrll 	    _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported"));
   12272   1.1     skrll 	  bfd_set_error (bfd_error_sorry);
   12273   1.1     skrll 	  return FALSE;
   12274   1.3  christos 	}
   12275   1.1     skrll     }
   12276   1.1     skrll   return TRUE;
   12277   1.1     skrll }
   12278   1.1     skrll 
   12279   1.3  christos 
   12280   1.3  christos /* Return TRUE for ELF symbol types that represent functions.
   12281   1.1     skrll    This is the default version of this function, which is sufficient for
   12282   1.5  christos    most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
   12283   1.5  christos 
   12284   1.5  christos bfd_boolean
   12285   1.5  christos _bfd_elf_is_function_type (unsigned int type)
   12286   1.5  christos {
   12287   1.5  christos   return (type == STT_FUNC
   12288   1.5  christos 	  || type == STT_GNU_IFUNC);
   12289   1.5  christos }
   12290   1.5  christos 
   12291   1.5  christos /* If the ELF symbol SYM might be a function in SEC, return the
   12292   1.5  christos    function size and set *CODE_OFF to the function's entry point,
   12293   1.5  christos    otherwise return zero.  */
   12294   1.5  christos 
   12295   1.5  christos bfd_size_type
   12296   1.5  christos _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
   12297   1.5  christos 			     bfd_vma *code_off)
   12298   1.5  christos {
   12299   1.5  christos   bfd_size_type size;
   12300   1.5  christos 
   12301   1.5  christos   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
   12302   1.5  christos 		     | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
   12303   1.5  christos       || sym->section != sec)
   12304   1.5  christos     return 0;
   12305   1.5  christos 
   12306                   *code_off = sym->value;
   12307                   size = 0;
   12308                   if (!(sym->flags & BSF_SYNTHETIC))
   12309                     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
   12310                   if (size == 0)
   12311                     size = 1;
   12312                   return size;
   12313                 }
   12314