| 1 |
/* ELF STT_GNU_IFUNC support. |
| 2 |
Copyright 2009 |
| 3 |
Free Software Foundation, Inc. |
| 4 |
|
| 5 |
This file is part of BFD, the Binary File Descriptor library. |
| 6 |
|
| 7 |
This program is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 3 of the License, or |
| 10 |
(at your option) any later version. |
| 11 |
|
| 12 |
This program is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with this program; if not, write to the Free Software |
| 19 |
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
| 20 |
MA 02110-1301, USA. */ |
| 21 |
|
| 22 |
#include "sysdep.h" |
| 23 |
#include "bfd.h" |
| 24 |
#include "bfdlink.h" |
| 25 |
#include "libbfd.h" |
| 26 |
#define ARCH_SIZE 0 |
| 27 |
#include "elf-bfd.h" |
| 28 |
#include "safe-ctype.h" |
| 29 |
#include "libiberty.h" |
| 30 |
#include "objalloc.h" |
| 31 |
|
| 32 |
/* Create sections needed by STT_GNU_IFUNC symbol. */ |
| 33 |
|
| 34 |
bfd_boolean |
| 35 |
_bfd_elf_create_ifunc_sections (bfd *abfd, struct bfd_link_info *info) |
| 36 |
{ |
| 37 |
flagword flags, pltflags; |
| 38 |
asection *s; |
| 39 |
const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
| 40 |
struct elf_link_hash_table *htab = elf_hash_table (info); |
| 41 |
|
| 42 |
if (htab->irelifunc != NULL || htab->iplt != NULL) |
| 43 |
return TRUE; |
| 44 |
|
| 45 |
flags = bed->dynamic_sec_flags; |
| 46 |
pltflags = flags; |
| 47 |
if (bed->plt_not_loaded) |
| 48 |
/* We do not clear SEC_ALLOC here because we still want the OS to |
| 49 |
allocate space for the section; it's just that there's nothing |
| 50 |
to read in from the object file. */ |
| 51 |
pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
| 52 |
else |
| 53 |
pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; |
| 54 |
if (bed->plt_readonly) |
| 55 |
pltflags |= SEC_READONLY; |
| 56 |
|
| 57 |
if (info->shared) |
| 58 |
{ |
| 59 |
/* We need to create .rel[a].ifunc for shared objects. */ |
| 60 |
const char *rel_sec = (bed->rela_plts_and_copies_p |
| 61 |
? ".rela.ifunc" : ".rel.ifunc"); |
| 62 |
|
| 63 |
s = bfd_make_section_with_flags (abfd, rel_sec, |
| 64 |
flags | SEC_READONLY); |
| 65 |
if (s == NULL |
| 66 |
|| ! bfd_set_section_alignment (abfd, s, |
| 67 |
bed->s->log_file_align)) |
| 68 |
return FALSE; |
| 69 |
htab->irelifunc = s; |
| 70 |
} |
| 71 |
else |
| 72 |
{ |
| 73 |
/* We need to create .iplt, .rel[a].iplt, .igot and .igot.plt |
| 74 |
for static executables. */ |
| 75 |
s = bfd_make_section_with_flags (abfd, ".iplt", pltflags); |
| 76 |
if (s == NULL |
| 77 |
|| ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) |
| 78 |
return FALSE; |
| 79 |
htab->iplt = s; |
| 80 |
|
| 81 |
s = bfd_make_section_with_flags (abfd, |
| 82 |
(bed->rela_plts_and_copies_p |
| 83 |
? ".rela.iplt" : ".rel.iplt"), |
| 84 |
flags | SEC_READONLY); |
| 85 |
if (s == NULL |
| 86 |
|| ! bfd_set_section_alignment (abfd, s, |
| 87 |
bed->s->log_file_align)) |
| 88 |
return FALSE; |
| 89 |
htab->irelplt = s; |
| 90 |
|
| 91 |
/* We don't need the .igot section if we have the .igot.plt |
| 92 |
section. */ |
| 93 |
if (bed->want_got_plt) |
| 94 |
s = bfd_make_section_with_flags (abfd, ".igot.plt", flags); |
| 95 |
else |
| 96 |
s = bfd_make_section_with_flags (abfd, ".igot", flags); |
| 97 |
if (s == NULL |
| 98 |
|| !bfd_set_section_alignment (abfd, s, |
| 99 |
bed->s->log_file_align)) |
| 100 |
return FALSE; |
| 101 |
htab->igotplt = s; |
| 102 |
} |
| 103 |
|
| 104 |
return TRUE; |
| 105 |
} |
| 106 |
|
| 107 |
/* For a STT_GNU_IFUNC symbol, create a dynamic reloc section, SRELOC, |
| 108 |
for the input section, SEC, and append this reloc to HEAD. */ |
| 109 |
|
| 110 |
asection * |
| 111 |
_bfd_elf_create_ifunc_dyn_reloc (bfd *abfd, struct bfd_link_info *info, |
| 112 |
asection *sec, asection *sreloc, |
| 113 |
struct elf_dyn_relocs **head) |
| 114 |
{ |
| 115 |
struct elf_dyn_relocs *p; |
| 116 |
struct elf_link_hash_table *htab = elf_hash_table (info); |
| 117 |
|
| 118 |
if (sreloc == NULL) |
| 119 |
{ |
| 120 |
const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
| 121 |
|
| 122 |
if (htab->dynobj == NULL) |
| 123 |
htab->dynobj = abfd; |
| 124 |
|
| 125 |
sreloc = _bfd_elf_make_dynamic_reloc_section (sec, htab->dynobj, |
| 126 |
bed->s->log_file_align, |
| 127 |
abfd, |
| 128 |
bed->rela_plts_and_copies_p); |
| 129 |
if (sreloc == NULL) |
| 130 |
return NULL; |
| 131 |
} |
| 132 |
|
| 133 |
p = *head; |
| 134 |
if (p == NULL || p->sec != sec) |
| 135 |
{ |
| 136 |
bfd_size_type amt = sizeof *p; |
| 137 |
|
| 138 |
p = ((struct elf_dyn_relocs *) bfd_alloc (htab->dynobj, amt)); |
| 139 |
if (p == NULL) |
| 140 |
return NULL; |
| 141 |
p->next = *head; |
| 142 |
*head = p; |
| 143 |
p->sec = sec; |
| 144 |
p->count = 0; |
| 145 |
p->pc_count = 0; |
| 146 |
} |
| 147 |
p->count += 1; |
| 148 |
|
| 149 |
return sreloc; |
| 150 |
} |
| 151 |
|
| 152 |
/* Allocate space in .plt, .got and associated reloc sections for |
| 153 |
dynamic relocs against a STT_GNU_IFUNC symbol definition. */ |
| 154 |
|
| 155 |
bfd_boolean |
| 156 |
_bfd_elf_allocate_ifunc_dyn_relocs (struct bfd_link_info *info, |
| 157 |
struct elf_link_hash_entry *h, |
| 158 |
struct elf_dyn_relocs **head, |
| 159 |
unsigned int plt_entry_size, |
| 160 |
unsigned int got_entry_size) |
| 161 |
{ |
| 162 |
asection *plt, *gotplt, *relplt; |
| 163 |
struct elf_dyn_relocs *p; |
| 164 |
unsigned int sizeof_reloc; |
| 165 |
const struct elf_backend_data *bed; |
| 166 |
struct elf_link_hash_table *htab; |
| 167 |
|
| 168 |
/* When a shared library references a STT_GNU_IFUNC symbol defined |
| 169 |
in executable, the address of the resolved function may be used. |
| 170 |
But in non-shared executable, the address of its .plt slot may |
| 171 |
be used. Pointer equality may not work correctly. PIE should |
| 172 |
be used if pointer equality is required here. */ |
| 173 |
if (!info->shared |
| 174 |
&& (h->dynindx != -1 |
| 175 |
|| info->export_dynamic) |
| 176 |
&& h->pointer_equality_needed) |
| 177 |
{ |
| 178 |
info->callbacks->einfo |
| 179 |
(_("%F%P: dynamic STT_GNU_IFUNC symbol `%s' with pointer " |
| 180 |
"equality in `%B' can not be used when making an " |
| 181 |
"executable; recompile with -fPIE and relink with -pie\n"), |
| 182 |
h->root.root.string, |
| 183 |
h->root.u.def.section->owner); |
| 184 |
bfd_set_error (bfd_error_bad_value); |
| 185 |
return FALSE; |
| 186 |
} |
| 187 |
|
| 188 |
/* Return and discard space for dynamic relocations against it if |
| 189 |
it is never referenced in a non-shared object. */ |
| 190 |
if (!h->ref_regular) |
| 191 |
{ |
| 192 |
if (h->plt.refcount > 0 |
| 193 |
|| h->got.refcount > 0) |
| 194 |
abort (); |
| 195 |
h->got.offset = (bfd_vma) -1; |
| 196 |
*head = NULL; |
| 197 |
return TRUE; |
| 198 |
} |
| 199 |
|
| 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 |
htab = elf_hash_table (info); |
| 207 |
|
| 208 |
/* When building a static executable, use .iplt, .igot.plt and |
| 209 |
.rel[a].iplt sections for STT_GNU_IFUNC symbols. */ |
| 210 |
if (htab->splt != NULL) |
| 211 |
{ |
| 212 |
plt = htab->splt; |
| 213 |
gotplt = htab->sgotplt; |
| 214 |
relplt = htab->srelplt; |
| 215 |
|
| 216 |
/* If this is the first .plt entry, make room for the special |
| 217 |
first entry. */ |
| 218 |
if (plt->size == 0) |
| 219 |
plt->size += plt_entry_size; |
| 220 |
} |
| 221 |
else |
| 222 |
{ |
| 223 |
plt = htab->iplt; |
| 224 |
gotplt = htab->igotplt; |
| 225 |
relplt = htab->irelplt; |
| 226 |
} |
| 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 |
/* We also need to make an entry in the .rel[a].plt/.rel[a].iplt |
| 240 |
section. */ |
| 241 |
relplt->size += sizeof_reloc; |
| 242 |
relplt->reloc_count++; |
| 243 |
|
| 244 |
/* We need dynamic relocation for STT_GNU_IFUNC symbol only when |
| 245 |
there is a non-GOT reference in a shared object. */ |
| 246 |
if (!info->shared |
| 247 |
|| !h->non_got_ref) |
| 248 |
*head = NULL; |
| 249 |
|
| 250 |
/* Finally, allocate space. */ |
| 251 |
for (p = *head; p != NULL; p = p->next) |
| 252 |
htab->irelifunc->size += p->count * sizeof_reloc; |
| 253 |
|
| 254 |
/* For STT_GNU_IFUNC symbol, .got.plt has the real function addres |
| 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 ((info->shared |
| 268 |
&& (h->dynindx == -1 |
| 269 |
|| h->forced_local)) |
| 270 |
|| (!info->shared |
| 271 |
&& !h->pointer_equality_needed) |
| 272 |
|| (info->executable && info->shared) |
| 273 |
|| htab->sgot == NULL) |
| 274 |
{ |
| 275 |
/* Use .got.plt. */ |
| 276 |
h->got.offset = (bfd_vma) -1; |
| 277 |
} |
| 278 |
else |
| 279 |
{ |
| 280 |
h->got.offset = htab->sgot->size; |
| 281 |
htab->sgot->size += got_entry_size; |
| 282 |
if (info->shared) |
| 283 |
htab->srelgot->size += sizeof_reloc; |
| 284 |
} |
| 285 |
|
| 286 |
return TRUE; |
| 287 |
} |