Home | History | Annotate | Download | only in bfd
      1 /* IBM S/390-specific support for ELF 32 and 64 bit functions
      2    Copyright (C) 2000-2016 Free Software Foundation, Inc.
      3    Contributed by Andreas Krebbel.
      4 
      5    This file is part of BFD, the Binary File Descriptor library.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program; if not, write to the Free Software
     19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
     20    02110-1301, USA.  */
     21 
     22 
     23 /* Return TRUE if H is an IFUNC symbol.  Simply checking for the
     24    symbol type might not be enough since it might get changed to
     25    STT_FUNC for pointer equality reasons.  */
     26 static inline bfd_boolean
     27 s390_is_ifunc_symbol_p (struct elf_link_hash_entry *h)
     28 {
     29   struct elf_s390_link_hash_entry *eh = (struct elf_s390_link_hash_entry*)h;
     30   return h->type == STT_GNU_IFUNC || eh->ifunc_resolver_address != 0;
     31 }
     32 
     33 /* Create sections needed by STT_GNU_IFUNC symbol.  */
     34 
     35 static bfd_boolean
     36 s390_elf_create_ifunc_sections (bfd *abfd, struct bfd_link_info *info)
     37 {
     38   flagword flags;
     39   asection *s;
     40   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
     41   struct elf_link_hash_table *htab = elf_hash_table (info);
     42 
     43   if (htab->iplt != NULL)
     44     return TRUE;
     45 
     46   flags = bed->dynamic_sec_flags;
     47 
     48   if (bfd_link_pic (info))
     49     {
     50       s = bfd_make_section_with_flags (abfd, ".rela.ifunc",
     51 				       flags | SEC_READONLY);
     52       if (s == NULL
     53 	  || ! bfd_set_section_alignment (abfd, s,
     54 					  bed->s->log_file_align))
     55 	return FALSE;
     56       htab->irelifunc = s;
     57     }
     58 
     59   /* Create .iplt, .rel[a].iplt, and .igot.plt.  */
     60   s = bfd_make_section_with_flags (abfd, ".iplt",
     61 				   flags | SEC_CODE | SEC_READONLY);
     62   if (s == NULL
     63       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
     64     return FALSE;
     65   htab->iplt = s;
     66 
     67   s = bfd_make_section_with_flags (abfd, ".rela.iplt", flags | SEC_READONLY);
     68   if (s == NULL
     69       || ! bfd_set_section_alignment (abfd, s,
     70 				      bed->s->log_file_align))
     71     return FALSE;
     72   htab->irelplt = s;
     73 
     74   s = bfd_make_section_with_flags (abfd, ".igot.plt", flags);
     75   if (s == NULL
     76       || !bfd_set_section_alignment (abfd, s,
     77 				     bed->s->log_file_align))
     78     return FALSE;
     79   htab->igotplt = s;
     80 
     81   return TRUE;
     82 }
     83 
     84 
     85 /* Allocate space in .plt, .got and associated reloc sections for
     86    dynamic relocs against a STT_GNU_IFUNC symbol definition.  */
     87 
     88 static bfd_boolean
     89 s390_elf_allocate_ifunc_dyn_relocs (struct bfd_link_info *info,
     90 				    struct elf_link_hash_entry *h)
     91 {
     92   struct elf_dyn_relocs *p;
     93   struct elf_link_hash_table *htab;
     94   struct elf_s390_link_hash_entry *eh = (struct elf_s390_link_hash_entry*)h;
     95   struct elf_dyn_relocs **head = &eh->dyn_relocs;
     96 
     97   htab = elf_hash_table (info);
     98   eh->ifunc_resolver_address = h->root.u.def.value;
     99   eh->ifunc_resolver_section = h->root.u.def.section;
    100 
    101   /* Support garbage collection against STT_GNU_IFUNC symbols.  */
    102   if (h->plt.refcount <= 0 && h->got.refcount <= 0)
    103     {
    104       /* When building shared library, we need to handle the case
    105          where it is marked with regular reference, but not non-GOT
    106 	 reference.  It may happen if we didn't see STT_GNU_IFUNC
    107 	 symbol at the time when checking relocations.  */
    108       if (bfd_link_pic (info)
    109 	  && !h->non_got_ref
    110 	  && h->ref_regular)
    111 	for (p = *head; p != NULL; p = p->next)
    112 	  if (p->count)
    113 	    {
    114 	      h->non_got_ref = 1;
    115 	      goto keep;
    116 	    }
    117 
    118       h->got = htab->init_got_offset;
    119       h->plt = htab->init_plt_offset;
    120       *head = NULL;
    121       return TRUE;
    122     }
    123 
    124   /* Return and discard space for dynamic relocations against it if
    125      it is never referenced in a non-shared object.  */
    126   if (!h->ref_regular)
    127     {
    128       if (h->plt.refcount > 0
    129 	  || h->got.refcount > 0)
    130 	abort ();
    131       h->got = htab->init_got_offset;
    132       h->plt = htab->init_plt_offset;
    133       *head = NULL;
    134       return TRUE;
    135     }
    136 
    137 keep:
    138   /* Without checking h->plt.refcount here we allocate a PLT slot.
    139      When setting plt.refcount in check_relocs it might not have been
    140      known that this will be an IFUNC symol.  */
    141   h->plt.offset = htab->iplt->size;
    142   h->needs_plt = 1;
    143   htab->iplt->size += PLT_ENTRY_SIZE;
    144   htab->igotplt->size += GOT_ENTRY_SIZE;
    145   htab->irelplt->size += RELA_ENTRY_SIZE;
    146   htab->irelplt->reloc_count++;
    147 
    148   /* In order to make pointer equality work with IFUNC symbols defined
    149      in a non-PIE executable and referenced in a shared lib, we turn
    150      the symbol into a STT_FUNC symbol and make the symbol value to
    151      point to the IPLT slot.  That way the referencing shared lib will
    152      always get the PLT slot address when resolving the respective
    153      R_390_GLOB_DAT/R_390_64 relocs on that symbol.  */
    154   if (bfd_link_pde (info)
    155       && h->def_regular
    156       && h->ref_dynamic)
    157     {
    158       h->root.u.def.section = htab->iplt;
    159       h->root.u.def.value = h->plt.offset;
    160       h->size = PLT_ENTRY_SIZE;
    161       h->type = STT_FUNC;
    162     }
    163 
    164   /* We need dynamic relocation for STT_GNU_IFUNC symbol only when
    165      there is a non-GOT reference in a shared object.  */
    166   if (!bfd_link_pic (info) || !h->non_got_ref)
    167     *head = NULL;
    168 
    169   /* Finally, allocate space.  */
    170   p = *head;
    171   if (p != NULL)
    172     {
    173       bfd_size_type count = 0;
    174       do
    175 	{
    176 	  count += p->count;
    177 	  p = p->next;
    178 	}
    179       while (p != NULL);
    180       htab->irelifunc->size += count * RELA_ENTRY_SIZE;
    181     }
    182 
    183   /* Decide whether the got.iplt slot can be used.  This has to be
    184      avoided if the values in the GOT slots could differ for pointer
    185      equality reasons.  */
    186   if (h->got.refcount <= 0
    187       || (bfd_link_pic (info)
    188 	  && (h->dynindx == -1 || h->forced_local))
    189       || bfd_link_pie (info)
    190       || htab->sgot == NULL)
    191     {
    192       /* Use .got.iplt.  */
    193       h->got.offset = (bfd_vma) -1;
    194     }
    195   else
    196     {
    197       h->got.offset = htab->sgot->size;
    198       htab->sgot->size += GOT_ENTRY_SIZE;
    199       if (bfd_link_pic (info))
    200 	htab->srelgot->size += RELA_ENTRY_SIZE;
    201     }
    202 
    203   return TRUE;
    204 }
    205 
    206 static bfd_boolean
    207 elf_s390_allocate_local_syminfo (bfd *abfd, Elf_Internal_Shdr *symtab_hdr)
    208 {
    209   bfd_size_type size;
    210 
    211   size = symtab_hdr->sh_info;
    212   size *= (sizeof (bfd_signed_vma)       /* local got */
    213 	   + sizeof (struct plt_entry)   /* local plt */
    214 	   + sizeof(char));              /* local tls type */
    215   elf_local_got_refcounts (abfd) = ((bfd_signed_vma *)
    216 				    bfd_zalloc (abfd, size));
    217   if (elf_local_got_refcounts (abfd) == NULL)
    218     return FALSE;
    219   elf_s390_local_plt (abfd)
    220     = (struct plt_entry*)(elf_local_got_refcounts (abfd)
    221 			  + symtab_hdr->sh_info);
    222   elf_s390_local_got_tls_type (abfd)
    223     = (char *) (elf_s390_local_plt (abfd) + symtab_hdr->sh_info);
    224 
    225   return TRUE;
    226 }
    227 
    228 /* Pick ELFOSABI_GNU if IFUNC symbols are used.  */
    229 
    230 static bfd_boolean
    231 elf_s390_add_symbol_hook (bfd *abfd,
    232 			  struct bfd_link_info *info,
    233 			  Elf_Internal_Sym *sym,
    234 			  const char **namep ATTRIBUTE_UNUSED,
    235 			  flagword *flagsp ATTRIBUTE_UNUSED,
    236 			  asection **secp ATTRIBUTE_UNUSED,
    237 			  bfd_vma *valp ATTRIBUTE_UNUSED)
    238 {
    239   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
    240       && (abfd->flags & DYNAMIC) == 0
    241       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
    242     elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
    243 
    244   return TRUE;
    245 }
    246 
    247 /* Whether to sort relocs output by ld -r or ld --emit-relocs, by
    248    r_offset.  Don't do so for code sections.  We want to keep ordering
    249    of GDCALL / PLT32DBL for TLS optimizations as is.  On the other
    250    hand, elf-eh-frame.c processing requires .eh_frame relocs to be
    251    sorted.  */
    252 
    253 static bfd_boolean
    254 elf_s390_elf_sort_relocs_p (asection *sec)
    255 {
    256   return (sec->flags & SEC_CODE) == 0;
    257 }
    258 
    259 /* Merge object attributes from IBFD into OBFD.  Raise an error if
    260    there are conflicting attributes.  */
    261 static bfd_boolean
    262 elf_s390_merge_obj_attributes (bfd *ibfd, bfd *obfd)
    263 {
    264   obj_attribute *in_attr, *in_attrs;
    265   obj_attribute *out_attr, *out_attrs;
    266 
    267   if (!elf_known_obj_attributes_proc (obfd)[0].i)
    268     {
    269       /* This is the first object.  Copy the attributes.  */
    270       _bfd_elf_copy_obj_attributes (ibfd, obfd);
    271 
    272       /* Use the Tag_null value to indicate the attributes have been
    273 	 initialized.  */
    274       elf_known_obj_attributes_proc (obfd)[0].i = 1;
    275 
    276       return TRUE;
    277     }
    278 
    279   in_attrs = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
    280   out_attrs = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
    281 
    282   /* Check for conflicting Tag_GNU_S390_ABI_Vector attributes and
    283      merge non-conflicting ones.  */
    284   in_attr = &in_attrs[Tag_GNU_S390_ABI_Vector];
    285   out_attr = &out_attrs[Tag_GNU_S390_ABI_Vector];
    286 
    287   if (in_attr->i > 2)
    288     _bfd_error_handler
    289       (_("Warning: %B uses unknown vector ABI %d"), ibfd,
    290        in_attr->i);
    291   else if (out_attr->i > 2)
    292     _bfd_error_handler
    293       (_("Warning: %B uses unknown vector ABI %d"), obfd,
    294        out_attr->i);
    295   else if (in_attr->i != out_attr->i)
    296     {
    297       out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
    298 
    299       if (in_attr->i && out_attr->i)
    300 	{
    301 	  const char abi_str[3][9] = { "none", "software", "hardware" };
    302 
    303 	  _bfd_error_handler
    304 	    (_("Warning: %B uses vector %s ABI, %B uses %s ABI"),
    305 	     ibfd, obfd, abi_str[in_attr->i], abi_str[out_attr->i]);
    306 	}
    307       if (in_attr->i > out_attr->i)
    308 	out_attr->i = in_attr->i;
    309     }
    310 
    311   /* Merge Tag_compatibility attributes and any common GNU ones.  */
    312   _bfd_elf_merge_object_attributes (ibfd, obfd);
    313 
    314   return TRUE;
    315 }
    316