1 /* ELF STT_GNU_IFUNC support.
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
28 #include "safe-ctype.h"
29 #include "libiberty.h"
32 /* Create sections needed by STT_GNU_IFUNC symbol. */
35 _bfd_elf_create_ifunc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
37 flagword flags
, pltflags
;
39 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
40 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
42 if (htab
->irelifunc
!= NULL
|| htab
->iplt
!= NULL
)
45 flags
= bed
->dynamic_sec_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
);
53 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
54 if (bed
->plt_readonly
)
55 pltflags
|= SEC_READONLY
;
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");
63 s
= bfd_make_section_with_flags (abfd
, rel_sec
,
64 flags
| SEC_READONLY
);
66 || ! bfd_set_section_alignment (abfd
, s
,
67 bed
->s
->log_file_align
))
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
);
77 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
81 s
= bfd_make_section_with_flags (abfd
,
82 (bed
->rela_plts_and_copies_p
83 ? ".rela.iplt" : ".rel.iplt"),
84 flags
| SEC_READONLY
);
86 || ! bfd_set_section_alignment (abfd
, s
,
87 bed
->s
->log_file_align
))
91 /* We don't need the .igot section if we have the .igot.plt
93 if (bed
->want_got_plt
)
94 s
= bfd_make_section_with_flags (abfd
, ".igot.plt", flags
);
96 s
= bfd_make_section_with_flags (abfd
, ".igot", flags
);
98 || !bfd_set_section_alignment (abfd
, s
,
99 bed
->s
->log_file_align
))
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. */
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
)
115 struct elf_dyn_relocs
*p
;
116 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
120 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
122 if (htab
->dynobj
== NULL
)
125 sreloc
= _bfd_elf_make_dynamic_reloc_section (sec
, htab
->dynobj
,
126 bed
->s
->log_file_align
,
128 bed
->rela_plts_and_copies_p
);
134 if (p
== NULL
|| p
->sec
!= sec
)
136 bfd_size_type amt
= sizeof *p
;
138 p
= ((struct elf_dyn_relocs
*) bfd_alloc (htab
->dynobj
, amt
));
152 /* Allocate space in .plt, .got and associated reloc sections for
153 dynamic relocs against a STT_GNU_IFUNC symbol definition. */
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
)
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
;
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. */
175 || info
->export_dynamic
)
176 && h
->pointer_equality_needed
)
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"),
183 h
->root
.u
.def
.section
->owner
);
184 bfd_set_error (bfd_error_bad_value
);
188 htab
= elf_hash_table (info
);
190 /* Support garbage collection against STT_GNU_IFUNC symbols. */
191 if (h
->plt
.refcount
<= 0 && h
->got
.refcount
<= 0)
193 /* When building shared library, we need to handle the case
194 where it is marked with regular reference, but not non-GOT
195 reference. It may happen if we didn't see STT_GNU_IFUNC
196 symbol at the time when checking relocations. */
200 for (p
= *head
; p
!= NULL
; p
= p
->next
)
207 h
->got
= htab
->init_got_offset
;
208 h
->plt
= htab
->init_plt_offset
;
213 /* Return and discard space for dynamic relocations against it if
214 it is never referenced in a non-shared object. */
217 if (h
->plt
.refcount
> 0
218 || h
->got
.refcount
> 0)
220 h
->got
= htab
->init_got_offset
;
221 h
->plt
= htab
->init_plt_offset
;
227 bed
= get_elf_backend_data (info
->output_bfd
);
228 if (bed
->rela_plts_and_copies_p
)
229 sizeof_reloc
= bed
->s
->sizeof_rela
;
231 sizeof_reloc
= bed
->s
->sizeof_rel
;
233 /* When building a static executable, use .iplt, .igot.plt and
234 .rel[a].iplt sections for STT_GNU_IFUNC symbols. */
235 if (htab
->splt
!= NULL
)
238 gotplt
= htab
->sgotplt
;
239 relplt
= htab
->srelplt
;
241 /* If this is the first .plt entry, make room for the special
244 plt
->size
+= plt_entry_size
;
249 gotplt
= htab
->igotplt
;
250 relplt
= htab
->irelplt
;
253 /* Don't update value of STT_GNU_IFUNC symbol to PLT. We need
254 the original value for R_*_IRELATIVE. */
255 h
->plt
.offset
= plt
->size
;
257 /* Make room for this entry in the .plt/.iplt section. */
258 plt
->size
+= plt_entry_size
;
260 /* We also need to make an entry in the .got.plt/.got.iplt section,
261 which will be placed in the .got section by the linker script. */
262 gotplt
->size
+= got_entry_size
;
264 /* We also need to make an entry in the .rel[a].plt/.rel[a].iplt
266 relplt
->size
+= sizeof_reloc
;
267 relplt
->reloc_count
++;
269 /* We need dynamic relocation for STT_GNU_IFUNC symbol only when
270 there is a non-GOT reference in a shared object. */
275 /* Finally, allocate space. */
279 bfd_size_type count
= 0;
286 htab
->irelifunc
->size
+= count
* sizeof_reloc
;
289 /* For STT_GNU_IFUNC symbol, .got.plt has the real function address
290 and .got has the PLT entry adddress. We will load the GOT entry
291 with the PLT entry in finish_dynamic_symbol if it is used. For
292 branch, it uses .got.plt. For symbol value,
293 1. Use .got.plt in a shared object if it is forced local or not
295 2. Use .got.plt in a non-shared object if pointer equality isn't
297 3. Use .got.plt in PIE.
298 4. Use .got.plt if .got isn't used.
299 5. Otherwise use .got so that it can be shared among different
301 We only need to relocate .got entry in shared object. */
306 && !h
->pointer_equality_needed
)
307 || (info
->executable
&& info
->shared
)
308 || htab
->sgot
== NULL
)
311 h
->got
.offset
= (bfd_vma
) -1;
315 h
->got
.offset
= htab
->sgot
->size
;
316 htab
->sgot
->size
+= got_entry_size
;
318 htab
->srelgot
->size
+= sizeof_reloc
;