Home | History | Annotate | Line # | Download | only in bfd
elf-s390-common.c revision 1.1.1.6
      1 /* IBM S/390-specific support for ELF 32 and 64 bit functions
      2    Copyright (C) 2000-2019 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 /* Return true if .got.plt is supposed to be emitted after .got.  */
     34 
     35 static inline bfd_boolean
     36 s390_gotplt_after_got_p (struct bfd_link_info *info)
     37 {
     38   struct elf_s390_link_hash_table *htab = elf_s390_hash_table (info);
     39 
     40   if (!htab->elf.sgot || !htab->elf.sgotplt)
     41     return TRUE;
     42 
     43   if (htab->elf.sgot->output_section == htab->elf.sgotplt->output_section)
     44     {
     45       if (htab->elf.sgot->output_offset < htab->elf.sgotplt->output_offset)
     46 	return TRUE;
     47     }
     48   else
     49     {
     50       if (htab->elf.sgot->output_section->vma
     51 	  <= htab->elf.sgotplt->output_section->vma)
     52 	return TRUE;
     53     }
     54   return FALSE;
     55 }
     56 
     57 /* Return the value of the _GLOBAL_OFFSET_TABLE_ symbol.  */
     58 
     59 static inline bfd_vma
     60 s390_got_pointer (struct bfd_link_info *info)
     61 {
     62   struct elf_s390_link_hash_table *htab = elf_s390_hash_table (info);
     63   bfd_vma got_pointer;
     64 
     65   BFD_ASSERT (htab && htab->elf.hgot);
     66 
     67   got_pointer = (htab->elf.hgot->root.u.def.section->output_section->vma
     68 		 + htab->elf.hgot->root.u.def.section->output_offset);
     69   /* Our ABI requires the GOT pointer to point at the very beginning
     70      of the global offset table.  */
     71   BFD_ASSERT (got_pointer
     72 	      <= (htab->elf.sgot->output_section->vma
     73 		  + htab->elf.sgot->output_offset));
     74   BFD_ASSERT (got_pointer
     75 	      <= (htab->elf.sgotplt->output_section->vma
     76 		  + htab->elf.sgotplt->output_offset));
     77 
     78   return got_pointer;
     79 }
     80 
     81 
     82 /* Return the offset of the .got versus _GLOBAL_OFFSET_TABLE_.  */
     83 
     84 static inline bfd_vma
     85 s390_got_offset (struct bfd_link_info *info)
     86 {
     87   struct elf_s390_link_hash_table *htab = elf_s390_hash_table (info);
     88 
     89   /* The absolute address of the .got in the target image.  */
     90   bfd_vma got_address = (htab->elf.sgot->output_section->vma
     91 			 + htab->elf.sgot->output_offset);
     92 
     93   /* GOT offset must not be negative.  */
     94   BFD_ASSERT (s390_got_pointer (info) <= got_address);
     95   return got_address - s390_got_pointer (info);
     96 }
     97 
     98 /* Return the offset of the .got.plt versus _GLOBAL_OFFSET_TABLE_.  */
     99 
    100 static inline bfd_vma
    101 s390_gotplt_offset (struct bfd_link_info *info)
    102 {
    103   struct elf_s390_link_hash_table *htab = elf_s390_hash_table (info);
    104 
    105   /* The absolute address of the .got.plt in the target image.  */
    106   bfd_vma gotplt_address = (htab->elf.sgotplt->output_section->vma
    107 			    + htab->elf.sgotplt->output_offset);
    108 
    109   /* GOT offset must not be negative.  */
    110   BFD_ASSERT (s390_got_pointer (info) <= gotplt_address);
    111   return gotplt_address - s390_got_pointer (info);
    112 }
    113 
    114 /* Create sections needed by STT_GNU_IFUNC symbol.  */
    115 
    116 static bfd_boolean
    117 s390_elf_create_ifunc_sections (bfd *abfd, struct bfd_link_info *info)
    118 {
    119   flagword flags;
    120   asection *s;
    121   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    122   struct elf_link_hash_table *htab = elf_hash_table (info);
    123 
    124   if (htab->iplt != NULL)
    125     return TRUE;
    126 
    127   flags = bed->dynamic_sec_flags;
    128 
    129   if (bfd_link_pic (info))
    130     {
    131       s = bfd_make_section_with_flags (abfd, ".rela.ifunc",
    132 				       flags | SEC_READONLY);
    133       if (s == NULL
    134 	  || ! bfd_set_section_alignment (abfd, s,
    135 					  bed->s->log_file_align))
    136 	return FALSE;
    137       htab->irelifunc = s;
    138     }
    139 
    140   /* Create .iplt, .rel[a].iplt, and .igot.plt.  */
    141   s = bfd_make_section_with_flags (abfd, ".iplt",
    142 				   flags | SEC_CODE | SEC_READONLY);
    143   if (s == NULL
    144       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
    145     return FALSE;
    146   htab->iplt = s;
    147 
    148   s = bfd_make_section_with_flags (abfd, ".rela.iplt", flags | SEC_READONLY);
    149   if (s == NULL
    150       || ! bfd_set_section_alignment (abfd, s,
    151 				      bed->s->log_file_align))
    152     return FALSE;
    153   htab->irelplt = s;
    154 
    155   s = bfd_make_section_with_flags (abfd, ".igot.plt", flags);
    156   if (s == NULL
    157       || !bfd_set_section_alignment (abfd, s,
    158 				     bed->s->log_file_align))
    159     return FALSE;
    160   htab->igotplt = s;
    161 
    162   return TRUE;
    163 }
    164 
    165 
    166 /* Allocate space in .plt, .got and associated reloc sections for
    167    dynamic relocs against a STT_GNU_IFUNC symbol definition.  */
    168 
    169 static bfd_boolean
    170 s390_elf_allocate_ifunc_dyn_relocs (struct bfd_link_info *info,
    171 				    struct elf_link_hash_entry *h)
    172 {
    173   struct elf_dyn_relocs *p;
    174   struct elf_link_hash_table *htab;
    175   struct elf_s390_link_hash_entry *eh = (struct elf_s390_link_hash_entry*)h;
    176   struct elf_dyn_relocs **head = &eh->dyn_relocs;
    177 
    178   htab = elf_hash_table (info);
    179   eh->ifunc_resolver_address = h->root.u.def.value;
    180   eh->ifunc_resolver_section = h->root.u.def.section;
    181 
    182   /* Support garbage collection against STT_GNU_IFUNC symbols.  */
    183   if (h->plt.refcount <= 0 && h->got.refcount <= 0)
    184     {
    185       /* When building shared library, we need to handle the case
    186 	 where it is marked with regular reference, but not non-GOT
    187 	 reference.  It may happen if we didn't see STT_GNU_IFUNC
    188 	 symbol at the time when checking relocations.  */
    189       if (bfd_link_pic (info)
    190 	  && !h->non_got_ref
    191 	  && h->ref_regular)
    192 	for (p = *head; p != NULL; p = p->next)
    193 	  if (p->count)
    194 	    {
    195 	      h->non_got_ref = 1;
    196 	      goto keep;
    197 	    }
    198 
    199       h->got = htab->init_got_offset;
    200       h->plt = htab->init_plt_offset;
    201       *head = NULL;
    202       return TRUE;
    203     }
    204 
    205   /* Return and discard space for dynamic relocations against it if
    206      it is never referenced in a non-shared object.  */
    207   if (!h->ref_regular)
    208     {
    209       if (h->plt.refcount > 0
    210 	  || h->got.refcount > 0)
    211 	abort ();
    212       h->got = htab->init_got_offset;
    213       h->plt = htab->init_plt_offset;
    214       *head = NULL;
    215       return TRUE;
    216     }
    217 
    218 keep:
    219   /* Without checking h->plt.refcount here we allocate a PLT slot.
    220      When setting plt.refcount in check_relocs it might not have been
    221      known that this will be an IFUNC symol.  */
    222   h->plt.offset = htab->iplt->size;
    223   h->needs_plt = 1;
    224   htab->iplt->size += PLT_ENTRY_SIZE;
    225   htab->igotplt->size += GOT_ENTRY_SIZE;
    226   htab->irelplt->size += RELA_ENTRY_SIZE;
    227   htab->irelplt->reloc_count++;
    228 
    229   /* In order to make pointer equality work with IFUNC symbols defined
    230      in a non-PIE executable and referenced in a shared lib, we turn
    231      the symbol into a STT_FUNC symbol and make the symbol value to
    232      point to the IPLT slot.  That way the referencing shared lib will
    233      always get the PLT slot address when resolving the respective
    234      R_390_GLOB_DAT/R_390_64 relocs on that symbol.  */
    235   if (bfd_link_pde (info)
    236       && h->def_regular
    237       && h->ref_dynamic)
    238     {
    239       h->root.u.def.section = htab->iplt;
    240       h->root.u.def.value = h->plt.offset;
    241       h->size = PLT_ENTRY_SIZE;
    242       h->type = STT_FUNC;
    243     }
    244 
    245   if (!bfd_link_pic (info))
    246     *head = NULL;
    247 
    248   /* Finally, allocate space.  */
    249   p = *head;
    250   if (p != NULL)
    251     {
    252       bfd_size_type count = 0;
    253       do
    254 	{
    255 	  count += p->count;
    256 	  p = p->next;
    257 	}
    258       while (p != NULL);
    259       htab->irelifunc->size += count * RELA_ENTRY_SIZE;
    260     }
    261 
    262   /* Decide whether the got.iplt slot can be used.  This has to be
    263      avoided if the values in the GOT slots could differ for pointer
    264      equality reasons.  */
    265   if (h->got.refcount <= 0
    266       || (bfd_link_pic (info)
    267 	  && (h->dynindx == -1 || h->forced_local))
    268       || bfd_link_pie (info)
    269       || htab->sgot == NULL)
    270     {
    271       /* Use .got.iplt.  */
    272       h->got.offset = (bfd_vma) -1;
    273     }
    274   else
    275     {
    276       h->got.offset = htab->sgot->size;
    277       htab->sgot->size += GOT_ENTRY_SIZE;
    278       if (bfd_link_pic (info))
    279 	htab->srelgot->size += RELA_ENTRY_SIZE;
    280     }
    281 
    282   return TRUE;
    283 }
    284 
    285 static bfd_boolean
    286 elf_s390_allocate_local_syminfo (bfd *abfd, Elf_Internal_Shdr *symtab_hdr)
    287 {
    288   bfd_size_type size;
    289 
    290   size = symtab_hdr->sh_info;
    291   size *= (sizeof (bfd_signed_vma)	 /* local got */
    292 	   + sizeof (struct plt_entry)	 /* local plt */
    293 	   + sizeof(char));		 /* local tls type */
    294   elf_local_got_refcounts (abfd) = ((bfd_signed_vma *)
    295 				    bfd_zalloc (abfd, size));
    296   if (elf_local_got_refcounts (abfd) == NULL)
    297     return FALSE;
    298   elf_s390_local_plt (abfd)
    299     = (struct plt_entry*)(elf_local_got_refcounts (abfd)
    300 			  + symtab_hdr->sh_info);
    301   elf_s390_local_got_tls_type (abfd)
    302     = (char *) (elf_s390_local_plt (abfd) + symtab_hdr->sh_info);
    303 
    304   return TRUE;
    305 }
    306 
    307 /* Whether to sort relocs output by ld -r or ld --emit-relocs, by
    308    r_offset.  Don't do so for code sections.  We want to keep ordering
    309    of GDCALL / PLT32DBL for TLS optimizations as is.  On the other
    310    hand, elf-eh-frame.c processing requires .eh_frame relocs to be
    311    sorted.  */
    312 
    313 static bfd_boolean
    314 elf_s390_elf_sort_relocs_p (asection *sec)
    315 {
    316   return (sec->flags & SEC_CODE) == 0;
    317 }
    318 
    319 /* Merge object attributes from IBFD into OBFD.  Raise an error if
    320    there are conflicting attributes.  */
    321 static bfd_boolean
    322 elf_s390_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
    323 {
    324   bfd *obfd = info->output_bfd;
    325   obj_attribute *in_attr, *in_attrs;
    326   obj_attribute *out_attr, *out_attrs;
    327 
    328   if (!elf_known_obj_attributes_proc (obfd)[0].i)
    329     {
    330       /* This is the first object.  Copy the attributes.  */
    331       _bfd_elf_copy_obj_attributes (ibfd, obfd);
    332 
    333       /* Use the Tag_null value to indicate the attributes have been
    334 	 initialized.  */
    335       elf_known_obj_attributes_proc (obfd)[0].i = 1;
    336 
    337       return TRUE;
    338     }
    339 
    340   in_attrs = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
    341   out_attrs = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
    342 
    343   /* Check for conflicting Tag_GNU_S390_ABI_Vector attributes and
    344      merge non-conflicting ones.  */
    345   in_attr = &in_attrs[Tag_GNU_S390_ABI_Vector];
    346   out_attr = &out_attrs[Tag_GNU_S390_ABI_Vector];
    347 
    348   if (in_attr->i > 2)
    349     _bfd_error_handler
    350       /* xgettext:c-format */
    351       (_("warning: %pB uses unknown vector ABI %d"), ibfd,
    352        in_attr->i);
    353   else if (out_attr->i > 2)
    354     _bfd_error_handler
    355       /* xgettext:c-format */
    356       (_("warning: %pB uses unknown vector ABI %d"), obfd,
    357        out_attr->i);
    358   else if (in_attr->i != out_attr->i)
    359     {
    360       out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
    361 
    362       if (in_attr->i && out_attr->i)
    363 	{
    364 	  const char abi_str[3][9] = { "none", "software", "hardware" };
    365 
    366 	  _bfd_error_handler
    367 	    /* xgettext:c-format */
    368 	    (_("warning: %pB uses vector %s ABI, %pB uses %s ABI"),
    369 	     ibfd, abi_str[in_attr->i], obfd, abi_str[out_attr->i]);
    370 	}
    371       if (in_attr->i > out_attr->i)
    372 	out_attr->i = in_attr->i;
    373     }
    374 
    375   /* Merge Tag_compatibility attributes and any common GNU ones.  */
    376   _bfd_elf_merge_object_attributes (ibfd, info);
    377 
    378   return TRUE;
    379 }
    380