Update year range in copyright notice of binutils files
[deliverable/binutils-gdb.git] / bfd / elf-ifunc.c
CommitLineData
6de2ae4a 1/* ELF STT_GNU_IFUNC support.
82704155 2 Copyright (C) 2009-2019 Free Software Foundation, Inc.
6de2ae4a
L
3
4 This file is part of BFD, the Binary File Descriptor library.
5
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.
10
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.
15
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. */
20
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"
30
31/* Create sections needed by STT_GNU_IFUNC symbol. */
32
33bfd_boolean
34_bfd_elf_create_ifunc_sections (bfd *abfd, struct bfd_link_info *info)
35{
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);
40
41 if (htab->irelifunc != NULL || htab->iplt != NULL)
42 return TRUE;
43
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;
55
0e1862bb 56 if (bfd_link_pic (info))
6de2ae4a 57 {
233cc9c1 58 /* We need to create .rel[a].ifunc for PIC objects. */
6de2ae4a
L
59 const char *rel_sec = (bed->rela_plts_and_copies_p
60 ? ".rela.ifunc" : ".rel.ifunc");
61
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;
69 }
70 else
71 {
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;
79
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;
89
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;
101 }
102
103 return TRUE;
104}
e03a8ed8 105
e03a8ed8
L
106/* Allocate space in .plt, .got and associated reloc sections for
107 dynamic relocs against a STT_GNU_IFUNC symbol definition. */
108
109bfd_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,
2df3368d 113 bfd_boolean *readonly_dynrelocs_against_ifunc_p,
e03a8ed8 114 unsigned int plt_entry_size,
7b70956d 115 unsigned int plt_header_size,
233cc9c1
L
116 unsigned int got_entry_size,
117 bfd_boolean avoid_plt)
e03a8ed8
L
118{
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;
2df3368d 124 bfd_boolean readonly_dynrelocs_against_ifunc;
233cc9c1
L
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);
128
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
4ec09950
L
134 pointer equality is required here.
135
136 If STT_GNU_IFUNC symbol is defined in position-dependent executable,
137 backend should change it to the normal function and set its address
138 to its PLT entry which should be resolved by R_*_IRELATIVE at
139 run-time. All external references should be resolved to its PLT in
140 executable. */
233cc9c1 141 if (!need_dynreloc
4ec09950 142 && !(bfd_link_pde (info) && h->def_regular)
e03a8ed8
L
143 && (h->dynindx != -1
144 || info->export_dynamic)
145 && h->pointer_equality_needed)
146 {
68ffbac6 147 info->callbacks->einfo
695344c0 148 /* xgettext:c-format */
e03a8ed8 149 (_("%F%P: dynamic STT_GNU_IFUNC symbol `%s' with pointer "
871b3ab2 150 "equality in `%pB' can not be used when making an "
e03a8ed8
L
151 "executable; recompile with -fPIE and relink with -pie\n"),
152 h->root.root.string,
153 h->root.u.def.section->owner);
154 bfd_set_error (bfd_error_bad_value);
155 return FALSE;
156 }
157
004fb780
L
158 htab = elf_hash_table (info);
159
233cc9c1
L
160 /* When the symbol is marked with regular reference, if PLT isn't used
161 or we are building a PIC object, we must keep dynamic relocation
162 if there is non-GOT reference and use PLT if there is PC-relative
163 reference. */
164 if (need_dynreloc && h->ref_regular)
165 {
166 bfd_boolean keep = FALSE;
167 for (p = *head; p != NULL; p = p->next)
168 if (p->count)
169 {
170 h->non_got_ref = 1;
171 /* Need dynamic relocations for non-GOT reference. */
172 keep = TRUE;
173 if (p->pc_count)
174 {
175 /* Must use PLT for PC-relative reference. */
176 use_plt = TRUE;
177 need_dynreloc = bfd_link_pic (info);
178 break;
179 }
180 }
181 if (keep)
182 goto keep;
183 }
1622ff3b 184
bb1cb422
L
185 /* Support garbage collection against STT_GNU_IFUNC symbols. */
186 if (h->plt.refcount <= 0 && h->got.refcount <= 0)
187 {
7be86737
L
188 h->got = htab->init_got_offset;
189 h->plt = htab->init_plt_offset;
190 *head = NULL;
191 return TRUE;
bb1cb422
L
192 }
193
e03a8ed8 194 /* Return and discard space for dynamic relocations against it if
233cc9c1 195 it is never referenced. */
e03a8ed8
L
196 if (!h->ref_regular)
197 {
198 if (h->plt.refcount > 0
199 || h->got.refcount > 0)
200 abort ();
004fb780
L
201 h->got = htab->init_got_offset;
202 h->plt = htab->init_plt_offset;
e03a8ed8
L
203 *head = NULL;
204 return TRUE;
205 }
206
7be86737 207keep:
e03a8ed8
L
208 bed = get_elf_backend_data (info->output_bfd);
209 if (bed->rela_plts_and_copies_p)
210 sizeof_reloc = bed->s->sizeof_rela;
211 else
212 sizeof_reloc = bed->s->sizeof_rel;
213
e03a8ed8
L
214 /* When building a static executable, use .iplt, .igot.plt and
215 .rel[a].iplt sections for STT_GNU_IFUNC symbols. */
216 if (htab->splt != NULL)
217 {
218 plt = htab->splt;
219 gotplt = htab->sgotplt;
220 relplt = htab->srelplt;
221
233cc9c1
L
222 /* If this is the first .plt entry and PLT is used, make room for
223 the special first entry. */
224 if (plt->size == 0 && use_plt)
7b70956d 225 plt->size += plt_header_size;
e03a8ed8
L
226 }
227 else
228 {
229 plt = htab->iplt;
230 gotplt = htab->igotplt;
231 relplt = htab->irelplt;
232 }
233
233cc9c1
L
234 if (use_plt)
235 {
236 /* Don't update value of STT_GNU_IFUNC symbol to PLT. We need
237 the original value for R_*_IRELATIVE. */
238 h->plt.offset = plt->size;
e03a8ed8 239
233cc9c1
L
240 /* Make room for this entry in the .plt/.iplt section. */
241 plt->size += plt_entry_size;
e03a8ed8 242
233cc9c1
L
243 /* We also need to make an entry in the .got.plt/.got.iplt section,
244 which will be placed in the .got section by the linker script. */
245 gotplt->size += got_entry_size;
246 }
e03a8ed8
L
247
248 /* We also need to make an entry in the .rel[a].plt/.rel[a].iplt
233cc9c1
L
249 section for GOTPLT relocation if PLT is used. */
250 if (use_plt)
251 {
252 relplt->size += sizeof_reloc;
253 relplt->reloc_count++;
254 }
e03a8ed8
L
255
256 /* We need dynamic relocation for STT_GNU_IFUNC symbol only when
233cc9c1
L
257 there is a non-GOT reference in a PIC object or PLT isn't used. */
258 if (!need_dynreloc || !h->non_got_ref)
e03a8ed8
L
259 *head = NULL;
260
2df3368d
L
261 readonly_dynrelocs_against_ifunc = FALSE;
262
e03a8ed8 263 /* Finally, allocate space. */
7be86737
L
264 p = *head;
265 if (p != NULL)
266 {
267 bfd_size_type count = 0;
268 do
269 {
2df3368d
L
270 if (!readonly_dynrelocs_against_ifunc)
271 {
272 asection *s = p->sec->output_section;
273 if (s != NULL && (s->flags & SEC_READONLY) != 0)
274 readonly_dynrelocs_against_ifunc = TRUE;
275 }
7be86737
L
276 count += p->count;
277 p = p->next;
278 }
279 while (p != NULL);
233cc9c1
L
280
281 /* Dynamic relocations are stored in
282 1. .rel[a].ifunc section in PIC object.
283 2. .rel[a].got section in dynamic executable.
284 3. .rel[a].iplt section in static executable. */
285 if (bfd_link_pic (info))
286 htab->irelifunc->size += count * sizeof_reloc;
287 else if (htab->splt != NULL)
288 htab->srelgot->size += count * sizeof_reloc;
289 else
290 {
291 relplt->size += count * sizeof_reloc;
292 relplt->reloc_count += count;
293 }
7be86737 294 }
e03a8ed8 295
2df3368d
L
296 if (readonly_dynrelocs_against_ifunc_p)
297 *readonly_dynrelocs_against_ifunc_p = readonly_dynrelocs_against_ifunc;
298
7be86737 299 /* For STT_GNU_IFUNC symbol, .got.plt has the real function address
e03a8ed8
L
300 and .got has the PLT entry adddress. We will load the GOT entry
301 with the PLT entry in finish_dynamic_symbol if it is used. For
233cc9c1
L
302 branch, it uses .got.plt. For symbol value, if PLT is used,
303 1. Use .got.plt in a PIC object if it is forced local or not
e03a8ed8 304 dynamic.
233cc9c1 305 2. Use .got.plt in a non-PIC object if pointer equality isn't
e03a8ed8
L
306 needed.
307 3. Use .got.plt in PIE.
308 4. Use .got.plt if .got isn't used.
309 5. Otherwise use .got so that it can be shared among different
310 objects at run-time.
233cc9c1
L
311 If PLT isn't used, always use .got for symbol value.
312 We only need to relocate .got entry in PIC object or in dynamic
313 executable without PLT. */
314 if (use_plt
315 && (h->got.refcount <= 0
316 || (bfd_link_pic (info)
317 && (h->dynindx == -1
318 || h->forced_local))
319 || (!bfd_link_pic (info)
320 && !h->pointer_equality_needed)
321 || bfd_link_pie (info)
322 || htab->sgot == NULL))
e03a8ed8
L
323 {
324 /* Use .got.plt. */
325 h->got.offset = (bfd_vma) -1;
326 }
327 else
328 {
233cc9c1
L
329 if (!use_plt)
330 {
331 /* PLT isn't used. */
332 h->plt.offset = (bfd_vma) -1;
333 }
334 if (h->got.refcount <= 0)
335 {
336 /* GOT isn't need when there are only relocations for static
337 pointers. */
338 h->got.offset = (bfd_vma) -1;
339 }
340 else
341 {
342 h->got.offset = htab->sgot->size;
343 htab->sgot->size += got_entry_size;
344 /* Need to relocate the GOT entry in a PIC object or PLT isn't
345 used. Otherwise, the GOT entry will be filled with the PLT
346 entry and dynamic GOT relocation isn't needed. */
347 if (need_dynreloc)
348 {
349 /* For non-static executable, dynamic GOT relocation is in
350 .rel[a].got section, but for static executable, it is
351 in .rel[a].iplt section. */
352 if (htab->splt != NULL)
353 htab->srelgot->size += sizeof_reloc;
354 else
355 {
356 relplt->size += sizeof_reloc;
357 relplt->reloc_count++;
358 }
359 }
360 }
e03a8ed8
L
361 }
362
363 return TRUE;
364}
This page took 0.499408 seconds and 4 git commands to generate.