| 1 | /* IBM S/390-specific support for ELF 32 and 64 bit functions |
| 2 | Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, |
| 3 | 2011, 2012 Free Software Foundation, Inc. |
| 4 | Contributed by Andreas Krebbel. |
| 5 | |
| 6 | This file is part of BFD, the Binary File Descriptor library. |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA |
| 21 | 02110-1301, USA. */ |
| 22 | |
| 23 | |
| 24 | /* Return TRUE if H is an IFUNC symbol. Simply checking for the |
| 25 | symbol type might not be enough since it might get changed to |
| 26 | STT_FUNC for pointer equality reasons. */ |
| 27 | static inline bfd_boolean |
| 28 | s390_is_ifunc_symbol_p (struct elf_link_hash_entry *h) |
| 29 | { |
| 30 | struct elf_s390_link_hash_entry *eh = (struct elf_s390_link_hash_entry*)h; |
| 31 | return h->type == STT_GNU_IFUNC || eh->ifunc_resolver_address != 0; |
| 32 | } |
| 33 | |
| 34 | /* Create sections needed by STT_GNU_IFUNC symbol. */ |
| 35 | |
| 36 | static bfd_boolean |
| 37 | s390_elf_create_ifunc_sections (bfd *abfd, struct bfd_link_info *info) |
| 38 | { |
| 39 | flagword flags; |
| 40 | asection *s; |
| 41 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
| 42 | struct elf_link_hash_table *htab = elf_hash_table (info); |
| 43 | |
| 44 | if (htab->iplt != NULL) |
| 45 | return TRUE; |
| 46 | |
| 47 | flags = bed->dynamic_sec_flags; |
| 48 | |
| 49 | if (info->shared) |
| 50 | { |
| 51 | s = bfd_make_section_with_flags (abfd, ".rela.ifunc", |
| 52 | flags | SEC_READONLY); |
| 53 | if (s == NULL |
| 54 | || ! bfd_set_section_alignment (abfd, s, |
| 55 | bed->s->log_file_align)) |
| 56 | return FALSE; |
| 57 | htab->irelifunc = s; |
| 58 | } |
| 59 | |
| 60 | /* Create .iplt, .rel[a].iplt, and .igot.plt. */ |
| 61 | s = bfd_make_section_with_flags (abfd, ".iplt", |
| 62 | flags | SEC_CODE | SEC_READONLY); |
| 63 | if (s == NULL |
| 64 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) |
| 65 | return FALSE; |
| 66 | htab->iplt = s; |
| 67 | |
| 68 | s = bfd_make_section_with_flags (abfd, ".rela.iplt", flags | SEC_READONLY); |
| 69 | if (s == NULL |
| 70 | || ! bfd_set_section_alignment (abfd, s, |
| 71 | bed->s->log_file_align)) |
| 72 | return FALSE; |
| 73 | htab->irelplt = s; |
| 74 | |
| 75 | s = bfd_make_section_with_flags (abfd, ".igot.plt", flags); |
| 76 | if (s == NULL |
| 77 | || !bfd_set_section_alignment (abfd, s, |
| 78 | bed->s->log_file_align)) |
| 79 | return FALSE; |
| 80 | htab->igotplt = s; |
| 81 | |
| 82 | return TRUE; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /* Allocate space in .plt, .got and associated reloc sections for |
| 87 | dynamic relocs against a STT_GNU_IFUNC symbol definition. */ |
| 88 | |
| 89 | static bfd_boolean |
| 90 | s390_elf_allocate_ifunc_dyn_relocs (struct bfd_link_info *info, |
| 91 | struct elf_link_hash_entry *h, |
| 92 | struct elf_dyn_relocs **head) |
| 93 | { |
| 94 | struct elf_dyn_relocs *p; |
| 95 | struct elf_link_hash_table *htab; |
| 96 | struct elf_s390_link_hash_entry *eh = (struct elf_s390_link_hash_entry*)h; |
| 97 | |
| 98 | htab = elf_hash_table (info); |
| 99 | eh->ifunc_resolver_address = h->root.u.def.value; |
| 100 | eh->ifunc_resolver_section = h->root.u.def.section; |
| 101 | |
| 102 | /* Support garbage collection against STT_GNU_IFUNC symbols. */ |
| 103 | if (h->plt.refcount <= 0 && h->got.refcount <= 0) |
| 104 | { |
| 105 | /* When building shared library, we need to handle the case |
| 106 | where it is marked with regular reference, but not non-GOT |
| 107 | reference. It may happen if we didn't see STT_GNU_IFUNC |
| 108 | symbol at the time when checking relocations. */ |
| 109 | if (info->shared |
| 110 | && !h->non_got_ref |
| 111 | && h->ref_regular) |
| 112 | for (p = *head; p != NULL; p = p->next) |
| 113 | if (p->count) |
| 114 | { |
| 115 | h->non_got_ref = 1; |
| 116 | goto keep; |
| 117 | } |
| 118 | |
| 119 | h->got = htab->init_got_offset; |
| 120 | h->plt = htab->init_plt_offset; |
| 121 | *head = NULL; |
| 122 | return TRUE; |
| 123 | } |
| 124 | |
| 125 | /* Return and discard space for dynamic relocations against it if |
| 126 | it is never referenced in a non-shared object. */ |
| 127 | if (!h->ref_regular) |
| 128 | { |
| 129 | if (h->plt.refcount > 0 |
| 130 | || h->got.refcount > 0) |
| 131 | abort (); |
| 132 | h->got = htab->init_got_offset; |
| 133 | h->plt = htab->init_plt_offset; |
| 134 | *head = NULL; |
| 135 | return TRUE; |
| 136 | } |
| 137 | |
| 138 | keep: |
| 139 | /* Without checking h->plt.refcount here we allocate a PLT slot. |
| 140 | When setting plt.refcount in check_relocs it might not have been |
| 141 | known that this will be an IFUNC symol. */ |
| 142 | h->plt.offset = htab->iplt->size; |
| 143 | h->needs_plt = 1; |
| 144 | htab->iplt->size += PLT_ENTRY_SIZE; |
| 145 | htab->igotplt->size += GOT_ENTRY_SIZE; |
| 146 | htab->irelplt->size += RELA_ENTRY_SIZE; |
| 147 | htab->irelplt->reloc_count++; |
| 148 | |
| 149 | /* In order to make pointer equality work with IFUNC symbols defined |
| 150 | in a non-PIE executable and referenced in a shared lib, we turn |
| 151 | the symbol into a STT_FUNC symbol and make the symbol value to |
| 152 | point to the IPLT slot. That way the referencing shared lib will |
| 153 | always get the PLT slot address when resolving the respective |
| 154 | R_390_GLOB_DAT/R_390_64 relocs on that symbol. */ |
| 155 | if (info->executable && !info->shared && h->def_regular && h->ref_dynamic) |
| 156 | { |
| 157 | h->root.u.def.section = htab->iplt; |
| 158 | h->root.u.def.value = h->plt.offset; |
| 159 | h->size = PLT_ENTRY_SIZE; |
| 160 | h->type = STT_FUNC; |
| 161 | } |
| 162 | |
| 163 | /* We need dynamic relocation for STT_GNU_IFUNC symbol only when |
| 164 | there is a non-GOT reference in a shared object. */ |
| 165 | if (!info->shared || !h->non_got_ref) |
| 166 | *head = NULL; |
| 167 | |
| 168 | /* Finally, allocate space. */ |
| 169 | p = *head; |
| 170 | if (p != NULL) |
| 171 | { |
| 172 | bfd_size_type count = 0; |
| 173 | do |
| 174 | { |
| 175 | count += p->count; |
| 176 | p = p->next; |
| 177 | } |
| 178 | while (p != NULL); |
| 179 | htab->irelifunc->size += count * RELA_ENTRY_SIZE; |
| 180 | } |
| 181 | |
| 182 | /* Decide whether the got.iplt slot can be used. This has to be |
| 183 | avoided if the values in the GOT slots could differ for pointer |
| 184 | equality reasons. */ |
| 185 | if (h->got.refcount <= 0 |
| 186 | || (info->shared |
| 187 | && (h->dynindx == -1 || h->forced_local)) |
| 188 | || (info->executable && info->shared) |
| 189 | || htab->sgot == NULL) |
| 190 | { |
| 191 | /* Use .got.iplt. */ |
| 192 | h->got.offset = (bfd_vma) -1; |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | h->got.offset = htab->sgot->size; |
| 197 | htab->sgot->size += GOT_ENTRY_SIZE; |
| 198 | if (info->shared) |
| 199 | htab->srelgot->size += RELA_ENTRY_SIZE; |
| 200 | } |
| 201 | |
| 202 | return TRUE; |
| 203 | } |
| 204 | |
| 205 | static bfd_boolean |
| 206 | elf_s390_allocate_local_syminfo (bfd *abfd, Elf_Internal_Shdr *symtab_hdr) |
| 207 | { |
| 208 | bfd_size_type size; |
| 209 | |
| 210 | size = symtab_hdr->sh_info; |
| 211 | size *= (sizeof (bfd_signed_vma) /* local got */ |
| 212 | + sizeof (struct plt_entry) /* local plt */ |
| 213 | + sizeof(char)); /* local tls type */ |
| 214 | elf_local_got_refcounts (abfd) = ((bfd_signed_vma *) |
| 215 | bfd_zalloc (abfd, size)); |
| 216 | if (elf_local_got_refcounts (abfd) == NULL) |
| 217 | return FALSE; |
| 218 | elf_s390_local_plt (abfd) |
| 219 | = (struct plt_entry*)(elf_local_got_refcounts (abfd) |
| 220 | + symtab_hdr->sh_info); |
| 221 | elf_s390_local_got_tls_type (abfd) |
| 222 | = (char *) (elf_s390_local_plt (abfd) + symtab_hdr->sh_info); |
| 223 | |
| 224 | return TRUE; |
| 225 | } |
| 226 | |
| 227 | /* Pick ELFOSABI_GNU if IFUNC symbols are used. */ |
| 228 | |
| 229 | static bfd_boolean |
| 230 | elf_s390_add_symbol_hook (bfd *abfd, |
| 231 | struct bfd_link_info *info, |
| 232 | Elf_Internal_Sym *sym, |
| 233 | const char **namep ATTRIBUTE_UNUSED, |
| 234 | flagword *flagsp ATTRIBUTE_UNUSED, |
| 235 | asection **secp ATTRIBUTE_UNUSED, |
| 236 | bfd_vma *valp ATTRIBUTE_UNUSED) |
| 237 | { |
| 238 | if ((abfd->flags & DYNAMIC) == 0 |
| 239 | && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC) |
| 240 | elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE; |
| 241 | |
| 242 | return TRUE; |
| 243 | } |