elf-s390-common.c 11 KB

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