ns32k: remove dupplicate definition of input_line_pointer
[external/binutils.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 shared 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 {
118   asection *plt, *gotplt, *relplt;
119   struct elf_dyn_relocs *p;
120   unsigned int sizeof_reloc;
121   const struct elf_backend_data *bed;
122   struct elf_link_hash_table *htab;
123   bfd_boolean readonly_dynrelocs_against_ifunc;
124
125   /* When a shared library references a STT_GNU_IFUNC symbol defined
126      in executable, the address of the resolved function may be used.
127      But in non-shared executable, the address of its .plt slot may
128      be used.  Pointer equality may not work correctly.  PIE should
129      be used if pointer equality is required here.  */
130   if (!bfd_link_pic (info)
131       && (h->dynindx != -1
132           || info->export_dynamic)
133       && h->pointer_equality_needed)
134     {
135       info->callbacks->einfo
136         (_("%F%P: dynamic STT_GNU_IFUNC symbol `%s' with pointer "
137            "equality in `%B' can not be used when making an "
138            "executable; recompile with -fPIE and relink with -pie\n"),
139          h->root.root.string,
140          h->root.u.def.section->owner);
141       bfd_set_error (bfd_error_bad_value);
142       return FALSE;
143     }
144
145   htab = elf_hash_table (info);
146
147   /* When building shared library, we need to handle the case where it is
148      marked with regular reference, but not non-GOT reference since the
149      non-GOT reference bit may not be set here.  */
150   if (bfd_link_pic (info) && !h->non_got_ref && h->ref_regular)
151     for (p = *head; p != NULL; p = p->next)
152       if (p->count)
153         {
154           h->non_got_ref = 1;
155           goto keep;
156         }
157
158   /* Support garbage collection against STT_GNU_IFUNC symbols.  */
159   if (h->plt.refcount <= 0 && h->got.refcount <= 0)
160     {
161       h->got = htab->init_got_offset;
162       h->plt = htab->init_plt_offset;
163       *head = NULL;
164       return TRUE;
165     }
166
167   /* Return and discard space for dynamic relocations against it if
168      it is never referenced in a non-shared object.  */
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
180 keep:
181   bed = get_elf_backend_data (info->output_bfd);
182   if (bed->rela_plts_and_copies_p)
183     sizeof_reloc = bed->s->sizeof_rela;
184   else
185     sizeof_reloc = bed->s->sizeof_rel;
186
187   /* When building a static executable, use .iplt, .igot.plt and
188      .rel[a].iplt sections for STT_GNU_IFUNC symbols.  */
189   if (htab->splt != NULL)
190     {
191       plt = htab->splt;
192       gotplt = htab->sgotplt;
193       relplt = htab->srelplt;
194
195       /* If this is the first .plt entry, make room for the special
196          first entry.  */
197       if (plt->size == 0)
198         plt->size += plt_header_size;
199     }
200   else
201     {
202       plt = htab->iplt;
203       gotplt = htab->igotplt;
204       relplt = htab->irelplt;
205     }
206
207   /* Don't update value of STT_GNU_IFUNC symbol to PLT.  We need
208      the original value for R_*_IRELATIVE.  */
209   h->plt.offset = plt->size;
210
211   /* Make room for this entry in the .plt/.iplt section.  */
212   plt->size += plt_entry_size;
213
214   /* We also need to make an entry in the .got.plt/.got.iplt section,
215      which will be placed in the .got section by the linker script.  */
216   gotplt->size += got_entry_size;
217
218   /* We also need to make an entry in the .rel[a].plt/.rel[a].iplt
219      section.  */
220   relplt->size += sizeof_reloc;
221   relplt->reloc_count++;
222
223   /* We need dynamic relocation for STT_GNU_IFUNC symbol only when
224      there is a non-GOT reference in a shared object.  */
225   if (!bfd_link_pic (info)
226       || !h->non_got_ref)
227     *head = NULL;
228
229   readonly_dynrelocs_against_ifunc = FALSE;
230
231   /* Finally, allocate space.  */
232   p = *head;
233   if (p != NULL)
234     {
235       bfd_size_type count = 0;
236       do
237         {
238           if (!readonly_dynrelocs_against_ifunc)
239             {
240               asection *s = p->sec->output_section;
241               if (s != NULL && (s->flags & SEC_READONLY) != 0)
242                 readonly_dynrelocs_against_ifunc = TRUE;
243             }
244           count += p->count;
245           p = p->next;
246         }
247       while (p != NULL);
248       htab->irelifunc->size += count * sizeof_reloc;
249     }
250
251   if (readonly_dynrelocs_against_ifunc_p)
252     *readonly_dynrelocs_against_ifunc_p = readonly_dynrelocs_against_ifunc;
253
254   /* For STT_GNU_IFUNC symbol, .got.plt has the real function address
255      and .got has the PLT entry adddress.  We will load the GOT entry
256      with the PLT entry in finish_dynamic_symbol if it is used.  For
257      branch, it uses .got.plt.  For symbol value,
258      1. Use .got.plt in a shared object if it is forced local or not
259      dynamic.
260      2. Use .got.plt in a non-shared object if pointer equality isn't
261      needed.
262      3. Use .got.plt in PIE.
263      4. Use .got.plt if .got isn't used.
264      5. Otherwise use .got so that it can be shared among different
265      objects at run-time.
266      We only need to relocate .got entry in shared object.  */
267   if (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       h->got.offset = htab->sgot->size;
282       htab->sgot->size += got_entry_size;
283       if (bfd_link_pic (info))
284         htab->srelgot->size += sizeof_reloc;
285     }
286
287   return TRUE;
288 }
289
290 /* Similar to _bfd_elf_get_synthetic_symtab, optimized for unsorted PLT
291    entries.  PLT is the PLT section.  PLT_SYM_VAL is a function pointer
292    which returns an array of PLT entry symbol values.  */
293
294 long
295 _bfd_elf_ifunc_get_synthetic_symtab
296   (bfd *abfd, long symcount ATTRIBUTE_UNUSED,
297    asymbol **syms ATTRIBUTE_UNUSED, long dynsymcount, asymbol **dynsyms,
298    asymbol **ret, asection *plt,
299    bfd_vma *(*get_plt_sym_val) (bfd *, asymbol **, asection *, asection *))
300 {
301   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
302   asection *relplt;
303   asymbol *s;
304   const char *relplt_name;
305   bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
306   arelent *p;
307   long count, i, n;
308   size_t size;
309   Elf_Internal_Shdr *hdr;
310   char *names;
311   bfd_vma *plt_sym_val;
312
313   *ret = NULL;
314
315   if (plt == NULL)
316     return 0;
317
318   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
319     return 0;
320
321   if (dynsymcount <= 0)
322     return 0;
323
324   relplt_name = bed->relplt_name;
325   if (relplt_name == NULL)
326     relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
327   relplt = bfd_get_section_by_name (abfd, relplt_name);
328   if (relplt == NULL)
329     return 0;
330
331   hdr = &elf_section_data (relplt)->this_hdr;
332   if (hdr->sh_link != elf_dynsymtab (abfd)
333       || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
334     return 0;
335
336   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
337   if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
338     return -1;
339
340   count = relplt->size / hdr->sh_entsize;
341   size = count * sizeof (asymbol);
342   p = relplt->relocation;
343   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
344     {
345       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
346       if (p->addend != 0)
347         {
348 #ifdef BFD64
349           size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
350 #else
351           size += sizeof ("+0x") - 1 + 8;
352 #endif
353         }
354     }
355
356   plt_sym_val = get_plt_sym_val (abfd, dynsyms, plt, relplt);
357   if (plt_sym_val == NULL)
358     return -1;
359
360   s = *ret = (asymbol *) bfd_malloc (size);
361   if (s == NULL)
362     {
363       free (plt_sym_val);
364       return -1;
365     }
366
367   names = (char *) (s + count);
368   p = relplt->relocation;
369   n = 0;
370   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
371     {
372       size_t len;
373       bfd_vma addr;
374
375       addr = plt_sym_val[i];
376       if (addr == (bfd_vma) -1)
377         continue;
378
379       *s = **p->sym_ptr_ptr;
380       /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
381          we are defining a symbol, ensure one of them is set.  */
382       if ((s->flags & BSF_LOCAL) == 0)
383         s->flags |= BSF_GLOBAL;
384       s->flags |= BSF_SYNTHETIC;
385       s->section = plt;
386       s->value = addr - plt->vma;
387       s->name = names;
388       s->udata.p = NULL;
389       len = strlen ((*p->sym_ptr_ptr)->name);
390       memcpy (names, (*p->sym_ptr_ptr)->name, len);
391       names += len;
392       if (p->addend != 0)
393         {
394           char buf[30], *a;
395
396           memcpy (names, "+0x", sizeof ("+0x") - 1);
397           names += sizeof ("+0x") - 1;
398           bfd_sprintf_vma (abfd, buf, p->addend);
399           for (a = buf; *a == '0'; ++a)
400             ;
401           len = strlen (a);
402           memcpy (names, a, len);
403           names += len;
404         }
405       memcpy (names, "@plt", sizeof ("@plt"));
406       names += sizeof ("@plt");
407       ++s, ++n;
408     }
409
410   free (plt_sym_val);
411
412   return n;
413 }