Subversion Repositories Kolibri OS

Rev

Rev 5197 | Details | Compare with Previous | Last modification | View Log | RSS feed

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