1 /* IBM S/390-specific support for ELF 32 and 64 bit functions
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Contributed by Andreas Krebbel.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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
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)
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;
33 /* Return true if .got.plt is supposed to be emitted after .got. */
35 static inline bfd_boolean
36 s390_gotplt_after_got_p (struct bfd_link_info *info)
38 struct elf_s390_link_hash_table *htab = elf_s390_hash_table (info);
40 if (!htab->elf.sgot || !htab->elf.sgotplt)
43 if (htab->elf.sgot->output_section == htab->elf.sgotplt->output_section)
45 if (htab->elf.sgot->output_offset < htab->elf.sgotplt->output_offset)
50 if (htab->elf.sgot->output_section->vma
51 <= htab->elf.sgotplt->output_section->vma)
57 /* Return the value of the _GLOBAL_OFFSET_TABLE_ symbol. */
60 s390_got_pointer (struct bfd_link_info *info)
62 struct elf_s390_link_hash_table *htab = elf_s390_hash_table (info);
65 BFD_ASSERT (htab && htab->elf.hgot);
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));
82 /* Return the offset of the .got versus _GLOBAL_OFFSET_TABLE_. */
85 s390_got_offset (struct bfd_link_info *info)
87 struct elf_s390_link_hash_table *htab = elf_s390_hash_table (info);
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);
93 /* GOT offset must not be negative. */
94 BFD_ASSERT (s390_got_pointer (info) <= got_address);
95 return got_address - s390_got_pointer (info);
98 /* Return the offset of the .got.plt versus _GLOBAL_OFFSET_TABLE_. */
100 static inline bfd_vma
101 s390_gotplt_offset (struct bfd_link_info *info)
103 struct elf_s390_link_hash_table *htab = elf_s390_hash_table (info);
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);
109 /* GOT offset must not be negative. */
110 BFD_ASSERT (s390_got_pointer (info) <= gotplt_address);
111 return gotplt_address - s390_got_pointer (info);
114 /* Create sections needed by STT_GNU_IFUNC symbol. */
117 s390_elf_create_ifunc_sections (bfd *abfd, struct bfd_link_info *info)
121 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
122 struct elf_link_hash_table *htab = elf_hash_table (info);
124 if (htab->iplt != NULL)
127 flags = bed->dynamic_sec_flags;
129 if (bfd_link_pic (info))
131 s = bfd_make_section_with_flags (abfd, ".rela.ifunc",
132 flags | SEC_READONLY);
134 || ! bfd_set_section_alignment (abfd, s,
135 bed->s->log_file_align))
140 /* Create .iplt, .rel[a].iplt, and .igot.plt. */
141 s = bfd_make_section_with_flags (abfd, ".iplt",
142 flags | SEC_CODE | SEC_READONLY);
144 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
148 s = bfd_make_section_with_flags (abfd, ".rela.iplt", flags | SEC_READONLY);
150 || ! bfd_set_section_alignment (abfd, s,
151 bed->s->log_file_align))
155 s = bfd_make_section_with_flags (abfd, ".igot.plt", flags);
157 || !bfd_set_section_alignment (abfd, s,
158 bed->s->log_file_align))
166 /* Allocate space in .plt, .got and associated reloc sections for
167 dynamic relocs against a STT_GNU_IFUNC symbol definition. */
170 s390_elf_allocate_ifunc_dyn_relocs (struct bfd_link_info *info,
171 struct elf_link_hash_entry *h)
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;
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;
182 /* Support garbage collection against STT_GNU_IFUNC symbols. */
183 if (h->plt.refcount <= 0 && h->got.refcount <= 0)
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)
192 for (p = *head; p != NULL; p = p->next)
199 h->got = htab->init_got_offset;
200 h->plt = htab->init_plt_offset;
205 /* Return and discard space for dynamic relocations against it if
206 it is never referenced in a non-shared object. */
209 if (h->plt.refcount > 0
210 || h->got.refcount > 0)
212 h->got = htab->init_got_offset;
213 h->plt = htab->init_plt_offset;
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;
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++;
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)
239 h->root.u.def.section = htab->iplt;
240 h->root.u.def.value = h->plt.offset;
241 h->size = PLT_ENTRY_SIZE;
245 if (!bfd_link_pic (info))
248 /* Finally, allocate space. */
252 bfd_size_type count = 0;
259 htab->irelifunc->size += count * RELA_ENTRY_SIZE;
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
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)
272 h->got.offset = (bfd_vma) -1;
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;
286 elf_s390_allocate_local_syminfo (bfd *abfd, Elf_Internal_Shdr *symtab_hdr)
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)
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);
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
314 elf_s390_elf_sort_relocs_p (asection *sec)
316 return (sec->flags & SEC_CODE) == 0;
319 /* Merge object attributes from IBFD into OBFD. Raise an error if
320 there are conflicting attributes. */
322 elf_s390_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
324 bfd *obfd = info->output_bfd;
325 obj_attribute *in_attr, *in_attrs;
326 obj_attribute *out_attr, *out_attrs;
328 if (!elf_known_obj_attributes_proc (obfd)[0].i)
330 /* This is the first object. Copy the attributes. */
331 _bfd_elf_copy_obj_attributes (ibfd, obfd);
333 /* Use the Tag_null value to indicate the attributes have been
335 elf_known_obj_attributes_proc (obfd)[0].i = 1;
340 in_attrs = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
341 out_attrs = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
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];
350 /* xgettext:c-format */
351 (_("warning: %pB uses unknown vector ABI %d"), ibfd,
353 else if (out_attr->i > 2)
355 /* xgettext:c-format */
356 (_("warning: %pB uses unknown vector ABI %d"), obfd,
358 else if (in_attr->i != out_attr->i)
360 out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
362 if (in_attr->i && out_attr->i)
364 const char abi_str[3][9] = { "none", "software", "hardware" };
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]);
371 if (in_attr->i > out_attr->i)
372 out_attr->i = in_attr->i;
375 /* Merge Tag_compatibility attributes and any common GNU ones. */
376 _bfd_elf_merge_object_attributes (ibfd, info);