1 /* ELF STT_GNU_IFUNC support.
2 Copyright (C) 2009-2014 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
31 /* Create sections needed by STT_GNU_IFUNC symbol. */
34 _bfd_elf_create_ifunc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
36 flagword flags
, pltflags
;
38 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
39 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
41 if (htab
->irelifunc
!= NULL
|| htab
->iplt
!= NULL
)
44 flags
= bed
->dynamic_sec_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
);
52 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
53 if (bed
->plt_readonly
)
54 pltflags
|= SEC_READONLY
;
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");
62 s
= bfd_make_section_with_flags (abfd
, rel_sec
,
63 flags
| SEC_READONLY
);
65 || ! bfd_set_section_alignment (abfd
, s
,
66 bed
->s
->log_file_align
))
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
);
76 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
80 s
= bfd_make_section_with_flags (abfd
,
81 (bed
->rela_plts_and_copies_p
82 ? ".rela.iplt" : ".rel.iplt"),
83 flags
| SEC_READONLY
);
85 || ! bfd_set_section_alignment (abfd
, s
,
86 bed
->s
->log_file_align
))
90 /* We don't need the .igot section if we have the .igot.plt
92 if (bed
->want_got_plt
)
93 s
= bfd_make_section_with_flags (abfd
, ".igot.plt", flags
);
95 s
= bfd_make_section_with_flags (abfd
, ".igot", flags
);
97 || !bfd_set_section_alignment (abfd
, s
,
98 bed
->s
->log_file_align
))
106 /* Allocate space in .plt, .got and associated reloc sections for
107 dynamic relocs against a STT_GNU_IFUNC symbol definition. */
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
)
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
;
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. */
130 || info
->export_dynamic
)
131 && h
->pointer_equality_needed
)
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"),
138 h
->root
.u
.def
.section
->owner
);
139 bfd_set_error (bfd_error_bad_value
);
143 htab
= elf_hash_table (info
);
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. */
148 if (info
->shared
&& !h
->non_got_ref
&& h
->ref_regular
)
149 for (p
= *head
; p
!= NULL
; p
= p
->next
)
156 /* Support garbage collection against STT_GNU_IFUNC symbols. */
157 if (h
->plt
.refcount
<= 0 && h
->got
.refcount
<= 0)
159 h
->got
= htab
->init_got_offset
;
160 h
->plt
= htab
->init_plt_offset
;
165 /* Return and discard space for dynamic relocations against it if
166 it is never referenced in a non-shared object. */
169 if (h
->plt
.refcount
> 0
170 || h
->got
.refcount
> 0)
172 h
->got
= htab
->init_got_offset
;
173 h
->plt
= htab
->init_plt_offset
;
179 bed
= get_elf_backend_data (info
->output_bfd
);
180 if (bed
->rela_plts_and_copies_p
)
181 sizeof_reloc
= bed
->s
->sizeof_rela
;
183 sizeof_reloc
= bed
->s
->sizeof_rel
;
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
)
190 gotplt
= htab
->sgotplt
;
191 relplt
= htab
->srelplt
;
193 /* If this is the first .plt entry, make room for the special
196 plt
->size
+= plt_header_size
;
201 gotplt
= htab
->igotplt
;
202 relplt
= htab
->irelplt
;
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
;
209 /* Make room for this entry in the .plt/.iplt section. */
210 plt
->size
+= plt_entry_size
;
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
;
216 /* We also need to make an entry in the .rel[a].plt/.rel[a].iplt
218 relplt
->size
+= sizeof_reloc
;
219 relplt
->reloc_count
++;
221 /* We need dynamic relocation for STT_GNU_IFUNC symbol only when
222 there is a non-GOT reference in a shared object. */
227 /* Finally, allocate space. */
231 bfd_size_type count
= 0;
238 htab
->irelifunc
->size
+= count
* sizeof_reloc
;
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
247 2. Use .got.plt in a non-shared object if pointer equality isn't
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
253 We only need to relocate .got entry in shared object. */
254 if (h
->got
.refcount
<= 0
259 && !h
->pointer_equality_needed
)
260 || (info
->executable
&& info
->shared
)
261 || htab
->sgot
== NULL
)
264 h
->got
.offset
= (bfd_vma
) -1;
268 h
->got
.offset
= htab
->sgot
->size
;
269 htab
->sgot
->size
+= got_entry_size
;
271 htab
->srelgot
->size
+= sizeof_reloc
;
This page took 0.04232 seconds and 4 git commands to generate.