elf-s390-common.c 9.6 KB

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