elf-ifunc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /* ELF STT_GNU_IFUNC support.
  2. Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "bfdlink.h"
  19. #include "libbfd.h"
  20. #define ARCH_SIZE 0
  21. #include "elf-bfd.h"
  22. #include "safe-ctype.h"
  23. #include "libiberty.h"
  24. #include "objalloc.h"
  25. /* Create sections needed by STT_GNU_IFUNC symbol. */
  26. bool
  27. _bfd_elf_create_ifunc_sections (bfd *abfd, struct bfd_link_info *info)
  28. {
  29. flagword flags, pltflags;
  30. asection *s;
  31. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  32. struct elf_link_hash_table *htab = elf_hash_table (info);
  33. if (htab->irelifunc != NULL || htab->iplt != NULL)
  34. return true;
  35. flags = bed->dynamic_sec_flags;
  36. pltflags = flags;
  37. if (bed->plt_not_loaded)
  38. /* We do not clear SEC_ALLOC here because we still want the OS to
  39. allocate space for the section; it's just that there's nothing
  40. to read in from the object file. */
  41. pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
  42. else
  43. pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
  44. if (bed->plt_readonly)
  45. pltflags |= SEC_READONLY;
  46. if (bfd_link_pic (info))
  47. {
  48. /* We need to create .rel[a].ifunc for PIC objects. */
  49. const char *rel_sec = (bed->rela_plts_and_copies_p
  50. ? ".rela.ifunc" : ".rel.ifunc");
  51. s = bfd_make_section_with_flags (abfd, rel_sec,
  52. flags | SEC_READONLY);
  53. if (s == NULL
  54. || !bfd_set_section_alignment (s, bed->s->log_file_align))
  55. return false;
  56. htab->irelifunc = s;
  57. }
  58. else
  59. {
  60. /* We need to create .iplt, .rel[a].iplt, .igot and .igot.plt
  61. for static executables. */
  62. s = bfd_make_section_with_flags (abfd, ".iplt", pltflags);
  63. if (s == NULL
  64. || !bfd_set_section_alignment (s, bed->plt_alignment))
  65. return false;
  66. htab->iplt = s;
  67. s = bfd_make_section_with_flags (abfd,
  68. (bed->rela_plts_and_copies_p
  69. ? ".rela.iplt" : ".rel.iplt"),
  70. flags | SEC_READONLY);
  71. if (s == NULL
  72. || !bfd_set_section_alignment (s, bed->s->log_file_align))
  73. return false;
  74. htab->irelplt = s;
  75. /* We don't need the .igot section if we have the .igot.plt
  76. section. */
  77. if (bed->want_got_plt)
  78. s = bfd_make_section_with_flags (abfd, ".igot.plt", flags);
  79. else
  80. s = bfd_make_section_with_flags (abfd, ".igot", flags);
  81. if (s == NULL
  82. || !bfd_set_section_alignment (s, bed->s->log_file_align))
  83. return false;
  84. htab->igotplt = s;
  85. }
  86. return true;
  87. }
  88. /* Allocate space in .plt, .got and associated reloc sections for
  89. dynamic relocs against a STT_GNU_IFUNC symbol definition. */
  90. bool
  91. _bfd_elf_allocate_ifunc_dyn_relocs (struct bfd_link_info *info,
  92. struct elf_link_hash_entry *h,
  93. struct elf_dyn_relocs **head,
  94. unsigned int plt_entry_size,
  95. unsigned int plt_header_size,
  96. unsigned int got_entry_size,
  97. bool avoid_plt)
  98. {
  99. asection *plt, *gotplt, *relplt;
  100. struct elf_dyn_relocs *p;
  101. unsigned int sizeof_reloc;
  102. const struct elf_backend_data *bed;
  103. struct elf_link_hash_table *htab;
  104. /* If AVOID_PLT is TRUE, don't use PLT if possible. */
  105. bool use_plt = !avoid_plt || h->plt.refcount > 0;
  106. bool need_dynreloc = !use_plt || bfd_link_pic (info);
  107. /* When a PIC object references a STT_GNU_IFUNC symbol defined
  108. in executable or it isn't referenced via PLT, the address of
  109. the resolved function may be used. But in non-PIC executable,
  110. the address of its .plt slot may be used. Pointer equality may
  111. not work correctly. PIE or non-PLT reference should be used if
  112. pointer equality is required here.
  113. If STT_GNU_IFUNC symbol is defined in position-dependent executable,
  114. backend should change it to the normal function and set its address
  115. to its PLT entry which should be resolved by R_*_IRELATIVE at
  116. run-time. All external references should be resolved to its PLT in
  117. executable. */
  118. if (!need_dynreloc
  119. && !(bfd_link_pde (info) && h->def_regular)
  120. && (h->dynindx != -1
  121. || info->export_dynamic)
  122. && h->pointer_equality_needed)
  123. {
  124. info->callbacks->einfo
  125. /* xgettext:c-format */
  126. (_("%F%P: dynamic STT_GNU_IFUNC symbol `%s' with pointer "
  127. "equality in `%pB' can not be used when making an "
  128. "executable; recompile with -fPIE and relink with -pie\n"),
  129. h->root.root.string,
  130. h->root.u.def.section->owner);
  131. bfd_set_error (bfd_error_bad_value);
  132. return false;
  133. }
  134. htab = elf_hash_table (info);
  135. /* When the symbol is marked with regular reference, if PLT isn't used
  136. or we are building a PIC object, we must keep dynamic relocation
  137. if there is non-GOT reference and use PLT if there is PC-relative
  138. reference. */
  139. if (need_dynreloc && h->ref_regular)
  140. {
  141. bool keep = false;
  142. for (p = *head; p != NULL; p = p->next)
  143. if (p->count)
  144. {
  145. h->non_got_ref = 1;
  146. /* Need dynamic relocations for non-GOT reference. */
  147. keep = true;
  148. if (p->pc_count)
  149. {
  150. /* Must use PLT for PC-relative reference. */
  151. use_plt = true;
  152. need_dynreloc = bfd_link_pic (info);
  153. break;
  154. }
  155. }
  156. if (keep)
  157. goto keep;
  158. }
  159. /* Support garbage collection against STT_GNU_IFUNC symbols. */
  160. if (h->plt.refcount <= 0 && h->got.refcount <= 0)
  161. {
  162. h->got = htab->init_got_offset;
  163. h->plt = htab->init_plt_offset;
  164. *head = NULL;
  165. return true;
  166. }
  167. /* Return and discard space for dynamic relocations against it if
  168. it is never referenced. */
  169. if (!h->ref_regular)
  170. {
  171. if (h->plt.refcount > 0
  172. || h->got.refcount > 0)
  173. abort ();
  174. h->got = htab->init_got_offset;
  175. h->plt = htab->init_plt_offset;
  176. *head = NULL;
  177. return true;
  178. }
  179. keep:
  180. bed = get_elf_backend_data (info->output_bfd);
  181. if (bed->rela_plts_and_copies_p)
  182. sizeof_reloc = bed->s->sizeof_rela;
  183. else
  184. sizeof_reloc = bed->s->sizeof_rel;
  185. /* When building a static executable, use .iplt, .igot.plt and
  186. .rel[a].iplt sections for STT_GNU_IFUNC symbols. */
  187. if (htab->splt != NULL)
  188. {
  189. plt = htab->splt;
  190. gotplt = htab->sgotplt;
  191. relplt = htab->srelplt;
  192. /* If this is the first .plt entry and PLT is used, make room for
  193. the special first entry. */
  194. if (plt->size == 0 && use_plt)
  195. plt->size += plt_header_size;
  196. }
  197. else
  198. {
  199. plt = htab->iplt;
  200. gotplt = htab->igotplt;
  201. relplt = htab->irelplt;
  202. }
  203. if (use_plt)
  204. {
  205. /* Don't update value of STT_GNU_IFUNC symbol to PLT. We need
  206. the original value for R_*_IRELATIVE. */
  207. h->plt.offset = plt->size;
  208. /* Make room for this entry in the .plt/.iplt section. */
  209. plt->size += plt_entry_size;
  210. /* We also need to make an entry in the .got.plt/.got.iplt section,
  211. which will be placed in the .got section by the linker script. */
  212. gotplt->size += got_entry_size;
  213. }
  214. /* We also need to make an entry in the .rel[a].plt/.rel[a].iplt
  215. section for GOTPLT relocation if PLT is used. */
  216. if (use_plt)
  217. {
  218. relplt->size += sizeof_reloc;
  219. relplt->reloc_count++;
  220. }
  221. /* We need dynamic relocation for STT_GNU_IFUNC symbol only when
  222. there is a non-GOT reference in a PIC object or PLT isn't used. */
  223. if (!need_dynreloc || !h->non_got_ref)
  224. *head = NULL;
  225. /* Finally, allocate space. */
  226. p = *head;
  227. if (p != NULL)
  228. {
  229. bfd_size_type count = 0;
  230. do
  231. {
  232. count += p->count;
  233. p = p->next;
  234. }
  235. while (p != NULL);
  236. htab->ifunc_resolvers = count != 0;
  237. /* Dynamic relocations are stored in
  238. 1. .rel[a].ifunc section in PIC object.
  239. 2. .rel[a].got section in dynamic executable.
  240. 3. .rel[a].iplt section in static executable. */
  241. if (bfd_link_pic (info))
  242. htab->irelifunc->size += count * sizeof_reloc;
  243. else if (htab->splt != NULL)
  244. htab->srelgot->size += count * sizeof_reloc;
  245. else
  246. {
  247. relplt->size += count * sizeof_reloc;
  248. relplt->reloc_count += count;
  249. }
  250. }
  251. /* For STT_GNU_IFUNC symbol, .got.plt has the real function address
  252. and .got has the PLT entry adddress. We will load the GOT entry
  253. with the PLT entry in finish_dynamic_symbol if it is used. For
  254. branch, it uses .got.plt. For symbol value, if PLT is used,
  255. 1. Use .got.plt in a PIC object if it is forced local or not
  256. dynamic.
  257. 2. Use .got.plt in a non-PIC object if pointer equality isn't
  258. needed.
  259. 3. Use .got.plt in PIE.
  260. 4. Use .got.plt if .got isn't used.
  261. 5. Otherwise use .got so that it can be shared among different
  262. objects at run-time.
  263. If PLT isn't used, always use .got for symbol value.
  264. We only need to relocate .got entry in PIC object or in dynamic
  265. executable without PLT. */
  266. if (use_plt
  267. && (h->got.refcount <= 0
  268. || (bfd_link_pic (info)
  269. && (h->dynindx == -1
  270. || h->forced_local))
  271. || (!bfd_link_pic (info)
  272. && !h->pointer_equality_needed)
  273. || bfd_link_pie (info)
  274. || htab->sgot == NULL))
  275. {
  276. /* Use .got.plt. */
  277. h->got.offset = (bfd_vma) -1;
  278. }
  279. else
  280. {
  281. if (!use_plt)
  282. {
  283. /* PLT isn't used. */
  284. h->plt.offset = (bfd_vma) -1;
  285. }
  286. if (h->got.refcount <= 0)
  287. {
  288. /* GOT isn't need when there are only relocations for static
  289. pointers. */
  290. h->got.offset = (bfd_vma) -1;
  291. }
  292. else
  293. {
  294. h->got.offset = htab->sgot->size;
  295. htab->sgot->size += got_entry_size;
  296. /* Need to relocate the GOT entry in a PIC object or PLT isn't
  297. used. Otherwise, the GOT entry will be filled with the PLT
  298. entry and dynamic GOT relocation isn't needed. */
  299. if (need_dynreloc)
  300. {
  301. /* For non-static executable, dynamic GOT relocation is in
  302. .rel[a].got section, but for static executable, it is
  303. in .rel[a].iplt section. */
  304. if (htab->splt != NULL)
  305. htab->srelgot->size += sizeof_reloc;
  306. else
  307. {
  308. relplt->size += sizeof_reloc;
  309. relplt->reloc_count++;
  310. }
  311. }
  312. }
  313. }
  314. return true;
  315. }