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