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