Don't generate PLT for IFUNC GOT/pointer reference
[deliverable/binutils-gdb.git] / bfd / elf-ifunc.c
1 /* ELF STT_GNU_IFUNC support.
2 Copyright (C) 2009-2016 Free Software Foundation, Inc.
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
33 bfd_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
56 if (bfd_link_pic (info))
57 {
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");
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 }
105
106 /* Allocate space in .plt, .got and associated reloc sections for
107 dynamic relocs against a STT_GNU_IFUNC symbol definition. */
108
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)
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;
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);
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
134 pointer equality is required here. */
135 if (!need_dynreloc
136 && (h->dynindx != -1
137 || info->export_dynamic)
138 && h->pointer_equality_needed)
139 {
140 info->callbacks->einfo
141 (_("%F%P: dynamic STT_GNU_IFUNC symbol `%s' with pointer "
142 "equality in `%B' can not be used when making an "
143 "executable; recompile with -fPIE and relink with -pie\n"),
144 h->root.root.string,
145 h->root.u.def.section->owner);
146 bfd_set_error (bfd_error_bad_value);
147 return FALSE;
148 }
149
150 htab = elf_hash_table (info);
151
152 /* When the symbol is marked with regular reference, if PLT isn't used
153 or we are building a PIC object, we must keep dynamic relocation
154 if there is non-GOT reference and use PLT if there is PC-relative
155 reference. */
156 if (need_dynreloc && h->ref_regular)
157 {
158 bfd_boolean keep = FALSE;
159 for (p = *head; p != NULL; p = p->next)
160 if (p->count)
161 {
162 h->non_got_ref = 1;
163 /* Need dynamic relocations for non-GOT reference. */
164 keep = TRUE;
165 if (p->pc_count)
166 {
167 /* Must use PLT for PC-relative reference. */
168 use_plt = TRUE;
169 need_dynreloc = bfd_link_pic (info);
170 break;
171 }
172 }
173 if (keep)
174 goto keep;
175 }
176
177 /* Support garbage collection against STT_GNU_IFUNC symbols. */
178 if (h->plt.refcount <= 0 && h->got.refcount <= 0)
179 {
180 h->got = htab->init_got_offset;
181 h->plt = htab->init_plt_offset;
182 *head = NULL;
183 return TRUE;
184 }
185
186 /* Return and discard space for dynamic relocations against it if
187 it is never referenced. */
188 if (!h->ref_regular)
189 {
190 if (h->plt.refcount > 0
191 || h->got.refcount > 0)
192 abort ();
193 h->got = htab->init_got_offset;
194 h->plt = htab->init_plt_offset;
195 *head = NULL;
196 return TRUE;
197 }
198
199 keep:
200 bed = get_elf_backend_data (info->output_bfd);
201 if (bed->rela_plts_and_copies_p)
202 sizeof_reloc = bed->s->sizeof_rela;
203 else
204 sizeof_reloc = bed->s->sizeof_rel;
205
206 /* When building a static executable, use .iplt, .igot.plt and
207 .rel[a].iplt sections for STT_GNU_IFUNC symbols. */
208 if (htab->splt != NULL)
209 {
210 plt = htab->splt;
211 gotplt = htab->sgotplt;
212 relplt = htab->srelplt;
213
214 /* If this is the first .plt entry and PLT is used, make room for
215 the special first entry. */
216 if (plt->size == 0 && use_plt)
217 plt->size += plt_header_size;
218 }
219 else
220 {
221 plt = htab->iplt;
222 gotplt = htab->igotplt;
223 relplt = htab->irelplt;
224 }
225
226 if (use_plt)
227 {
228 /* Don't update value of STT_GNU_IFUNC symbol to PLT. We need
229 the original value for R_*_IRELATIVE. */
230 h->plt.offset = plt->size;
231
232 /* Make room for this entry in the .plt/.iplt section. */
233 plt->size += plt_entry_size;
234
235 /* We also need to make an entry in the .got.plt/.got.iplt section,
236 which will be placed in the .got section by the linker script. */
237 gotplt->size += got_entry_size;
238 }
239
240 /* We also need to make an entry in the .rel[a].plt/.rel[a].iplt
241 section for GOTPLT relocation if PLT is used. */
242 if (use_plt)
243 {
244 relplt->size += sizeof_reloc;
245 relplt->reloc_count++;
246 }
247
248 /* We need dynamic relocation for STT_GNU_IFUNC symbol only when
249 there is a non-GOT reference in a PIC object or PLT isn't used. */
250 if (!need_dynreloc || !h->non_got_ref)
251 *head = NULL;
252
253 readonly_dynrelocs_against_ifunc = FALSE;
254
255 /* Finally, allocate space. */
256 p = *head;
257 if (p != NULL)
258 {
259 bfd_size_type count = 0;
260 do
261 {
262 if (!readonly_dynrelocs_against_ifunc)
263 {
264 asection *s = p->sec->output_section;
265 if (s != NULL && (s->flags & SEC_READONLY) != 0)
266 readonly_dynrelocs_against_ifunc = TRUE;
267 }
268 count += p->count;
269 p = p->next;
270 }
271 while (p != NULL);
272
273 /* Dynamic relocations are stored in
274 1. .rel[a].ifunc section in PIC object.
275 2. .rel[a].got section in dynamic executable.
276 3. .rel[a].iplt section in static executable. */
277 if (bfd_link_pic (info))
278 htab->irelifunc->size += count * sizeof_reloc;
279 else if (htab->splt != NULL)
280 htab->srelgot->size += count * sizeof_reloc;
281 else
282 {
283 relplt->size += count * sizeof_reloc;
284 relplt->reloc_count += count;
285 }
286 }
287
288 if (readonly_dynrelocs_against_ifunc_p)
289 *readonly_dynrelocs_against_ifunc_p = readonly_dynrelocs_against_ifunc;
290
291 /* For STT_GNU_IFUNC symbol, .got.plt has the real function address
292 and .got has the PLT entry adddress. We will load the GOT entry
293 with the PLT entry in finish_dynamic_symbol if it is used. For
294 branch, it uses .got.plt. For symbol value, if PLT is used,
295 1. Use .got.plt in a PIC object if it is forced local or not
296 dynamic.
297 2. Use .got.plt in a non-PIC object if pointer equality isn't
298 needed.
299 3. Use .got.plt in PIE.
300 4. Use .got.plt if .got isn't used.
301 5. Otherwise use .got so that it can be shared among different
302 objects at run-time.
303 If PLT isn't used, always use .got for symbol value.
304 We only need to relocate .got entry in PIC object or in dynamic
305 executable without PLT. */
306 if (use_plt
307 && (h->got.refcount <= 0
308 || (bfd_link_pic (info)
309 && (h->dynindx == -1
310 || h->forced_local))
311 || (!bfd_link_pic (info)
312 && !h->pointer_equality_needed)
313 || bfd_link_pie (info)
314 || htab->sgot == NULL))
315 {
316 /* Use .got.plt. */
317 h->got.offset = (bfd_vma) -1;
318 }
319 else
320 {
321 if (!use_plt)
322 {
323 /* PLT isn't used. */
324 h->plt.offset = (bfd_vma) -1;
325 }
326 if (h->got.refcount <= 0)
327 {
328 /* GOT isn't need when there are only relocations for static
329 pointers. */
330 h->got.offset = (bfd_vma) -1;
331 }
332 else
333 {
334 h->got.offset = htab->sgot->size;
335 htab->sgot->size += got_entry_size;
336 /* Need to relocate the GOT entry in a PIC object or PLT isn't
337 used. Otherwise, the GOT entry will be filled with the PLT
338 entry and dynamic GOT relocation isn't needed. */
339 if (need_dynreloc)
340 {
341 /* For non-static executable, dynamic GOT relocation is in
342 .rel[a].got section, but for static executable, it is
343 in .rel[a].iplt section. */
344 if (htab->splt != NULL)
345 htab->srelgot->size += sizeof_reloc;
346 else
347 {
348 relplt->size += sizeof_reloc;
349 relplt->reloc_count++;
350 }
351 }
352 }
353 }
354
355 return TRUE;
356 }
357
358 /* Similar to _bfd_elf_get_synthetic_symtab, optimized for unsorted PLT
359 entries. PLT is the PLT section. PLT_SYM_VAL is a function pointer
360 which returns an array of PLT entry symbol values. */
361
362 long
363 _bfd_elf_ifunc_get_synthetic_symtab
364 (bfd *abfd, long symcount ATTRIBUTE_UNUSED,
365 asymbol **syms ATTRIBUTE_UNUSED, long dynsymcount, asymbol **dynsyms,
366 asymbol **ret, asection *plt,
367 bfd_vma *(*get_plt_sym_val) (bfd *, asymbol **, asection *, asection *))
368 {
369 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
370 asection *relplt;
371 asymbol *s;
372 const char *relplt_name;
373 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
374 arelent *p;
375 long count, i, n;
376 size_t size;
377 Elf_Internal_Shdr *hdr;
378 char *names;
379 bfd_vma *plt_sym_val;
380
381 *ret = NULL;
382
383 if (plt == NULL)
384 return 0;
385
386 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
387 return 0;
388
389 if (dynsymcount <= 0)
390 return 0;
391
392 relplt_name = bed->relplt_name;
393 if (relplt_name == NULL)
394 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
395 relplt = bfd_get_section_by_name (abfd, relplt_name);
396 if (relplt == NULL)
397 return 0;
398
399 hdr = &elf_section_data (relplt)->this_hdr;
400 if (hdr->sh_link != elf_dynsymtab (abfd)
401 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
402 return 0;
403
404 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
405 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
406 return -1;
407
408 count = relplt->size / hdr->sh_entsize;
409 size = count * sizeof (asymbol);
410 p = relplt->relocation;
411 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
412 {
413 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
414 if (p->addend != 0)
415 {
416 #ifdef BFD64
417 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
418 #else
419 size += sizeof ("+0x") - 1 + 8;
420 #endif
421 }
422 }
423
424 plt_sym_val = get_plt_sym_val (abfd, dynsyms, plt, relplt);
425 if (plt_sym_val == NULL)
426 return -1;
427
428 s = *ret = (asymbol *) bfd_malloc (size);
429 if (s == NULL)
430 {
431 free (plt_sym_val);
432 return -1;
433 }
434
435 names = (char *) (s + count);
436 p = relplt->relocation;
437 n = 0;
438 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
439 {
440 size_t len;
441 bfd_vma addr;
442
443 addr = plt_sym_val[i];
444 if (addr == (bfd_vma) -1)
445 continue;
446
447 *s = **p->sym_ptr_ptr;
448 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
449 we are defining a symbol, ensure one of them is set. */
450 if ((s->flags & BSF_LOCAL) == 0)
451 s->flags |= BSF_GLOBAL;
452 s->flags |= BSF_SYNTHETIC;
453 s->section = plt;
454 s->value = addr - plt->vma;
455 s->name = names;
456 s->udata.p = NULL;
457 len = strlen ((*p->sym_ptr_ptr)->name);
458 memcpy (names, (*p->sym_ptr_ptr)->name, len);
459 names += len;
460 if (p->addend != 0)
461 {
462 char buf[30], *a;
463
464 memcpy (names, "+0x", sizeof ("+0x") - 1);
465 names += sizeof ("+0x") - 1;
466 bfd_sprintf_vma (abfd, buf, p->addend);
467 for (a = buf; *a == '0'; ++a)
468 ;
469 len = strlen (a);
470 memcpy (names, a, len);
471 names += len;
472 }
473 memcpy (names, "@plt", sizeof ("@plt"));
474 names += sizeof ("@plt");
475 ++s, ++n;
476 }
477
478 free (plt_sym_val);
479
480 return n;
481 }
This page took 0.039941 seconds and 5 git commands to generate.