/src/binutils-gdb/bfd/elf-ifunc.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* ELF STT_GNU_IFUNC support. |
2 | | Copyright (C) 2009-2025 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 | | bool |
34 | | _bfd_elf_create_ifunc_sections (bfd *abfd, struct bfd_link_info *info) |
35 | 0 | { |
36 | 0 | flagword flags, pltflags; |
37 | 0 | asection *s; |
38 | 0 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
39 | 0 | struct elf_link_hash_table *htab = elf_hash_table (info); |
40 | |
|
41 | 0 | if (htab->irelifunc != NULL || htab->iplt != NULL) |
42 | 0 | return true; |
43 | | |
44 | 0 | flags = bed->dynamic_sec_flags; |
45 | 0 | pltflags = flags; |
46 | 0 | 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 | 0 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
51 | 0 | else |
52 | 0 | pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; |
53 | 0 | if (bed->plt_readonly) |
54 | 0 | pltflags |= SEC_READONLY; |
55 | |
|
56 | 0 | if (bfd_link_pic (info)) |
57 | 0 | { |
58 | | /* We need to create .rel[a].ifunc for PIC objects. */ |
59 | 0 | const char *rel_sec = (bed->rela_plts_and_copies_p |
60 | 0 | ? ".rela.ifunc" : ".rel.ifunc"); |
61 | |
|
62 | 0 | s = bfd_make_section_with_flags (abfd, rel_sec, |
63 | 0 | flags | SEC_READONLY); |
64 | 0 | if (s == NULL |
65 | 0 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
66 | 0 | return false; |
67 | 0 | htab->irelifunc = s; |
68 | 0 | } |
69 | 0 | else |
70 | 0 | { |
71 | | /* We need to create .iplt, .rel[a].iplt, .igot and .igot.plt |
72 | | for static executables. */ |
73 | 0 | s = bfd_make_section_with_flags (abfd, ".iplt", pltflags); |
74 | 0 | if (s == NULL |
75 | 0 | || !bfd_set_section_alignment (s, bed->plt_alignment)) |
76 | 0 | return false; |
77 | 0 | htab->iplt = s; |
78 | |
|
79 | 0 | s = bfd_make_section_with_flags (abfd, |
80 | 0 | (bed->rela_plts_and_copies_p |
81 | 0 | ? ".rela.iplt" : ".rel.iplt"), |
82 | 0 | flags | SEC_READONLY); |
83 | 0 | if (s == NULL |
84 | 0 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
85 | 0 | return false; |
86 | 0 | htab->irelplt = s; |
87 | | |
88 | | /* We don't need the .igot section if we have the .igot.plt |
89 | | section. */ |
90 | 0 | if (bed->want_got_plt) |
91 | 0 | s = bfd_make_section_with_flags (abfd, ".igot.plt", flags); |
92 | 0 | else |
93 | 0 | s = bfd_make_section_with_flags (abfd, ".igot", flags); |
94 | 0 | if (s == NULL |
95 | 0 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
96 | 0 | return false; |
97 | 0 | htab->igotplt = s; |
98 | 0 | } |
99 | | |
100 | 0 | return true; |
101 | 0 | } |
102 | | |
103 | | /* Allocate space in .plt, .got and associated reloc sections for |
104 | | dynamic relocs against a STT_GNU_IFUNC symbol definition. */ |
105 | | |
106 | | bool |
107 | | _bfd_elf_allocate_ifunc_dyn_relocs (struct bfd_link_info *info, |
108 | | struct elf_link_hash_entry *h, |
109 | | struct elf_dyn_relocs **head, |
110 | | unsigned int plt_entry_size, |
111 | | unsigned int plt_header_size, |
112 | | unsigned int got_entry_size, |
113 | | bool avoid_plt) |
114 | 0 | { |
115 | 0 | asection *plt, *gotplt, *relplt; |
116 | 0 | struct elf_dyn_relocs *p; |
117 | 0 | unsigned int sizeof_reloc; |
118 | 0 | const struct elf_backend_data *bed; |
119 | 0 | struct elf_link_hash_table *htab; |
120 | | /* If AVOID_PLT is TRUE, don't use PLT if possible. */ |
121 | 0 | bool use_plt = !avoid_plt || h->plt.refcount > 0; |
122 | 0 | bool need_dynreloc = !use_plt || bfd_link_pic (info); |
123 | | |
124 | | /* When a PIC object references a STT_GNU_IFUNC symbol defined |
125 | | in executable or it isn't referenced via PLT, the address of |
126 | | the resolved function may be used. But in non-PIC executable, |
127 | | the address of its .plt slot may be used. Pointer equality may |
128 | | not work correctly. PIE or non-PLT reference should be used if |
129 | | pointer equality is required here. |
130 | | |
131 | | If STT_GNU_IFUNC symbol is defined in position-dependent executable, |
132 | | backend should change it to the normal function and set its address |
133 | | to its PLT entry which should be resolved by R_*_IRELATIVE at |
134 | | run-time. All external references should be resolved to its PLT in |
135 | | executable. */ |
136 | 0 | if (!need_dynreloc |
137 | 0 | && !(bfd_link_pde (info) && h->def_regular) |
138 | 0 | && (h->dynindx != -1 |
139 | 0 | || info->export_dynamic) |
140 | 0 | && h->pointer_equality_needed) |
141 | 0 | { |
142 | 0 | info->callbacks->fatal |
143 | | /* xgettext:c-format */ |
144 | 0 | (_("%P: dynamic STT_GNU_IFUNC symbol `%s' with pointer " |
145 | 0 | "equality in `%pB' can not be used when making an " |
146 | 0 | "executable; recompile with -fPIE and relink with -pie\n"), |
147 | 0 | h->root.root.string, |
148 | 0 | h->root.u.def.section->owner); |
149 | 0 | bfd_set_error (bfd_error_bad_value); |
150 | 0 | return false; |
151 | 0 | } |
152 | | |
153 | 0 | htab = elf_hash_table (info); |
154 | | |
155 | | /* When the symbol is marked with regular reference, if PLT isn't used |
156 | | or we are building a PIC object, we must keep dynamic relocation |
157 | | if there is non-GOT reference and use PLT if there is PC-relative |
158 | | reference. */ |
159 | 0 | if (need_dynreloc && h->ref_regular) |
160 | 0 | { |
161 | 0 | bool keep = false; |
162 | 0 | for (p = *head; p != NULL; p = p->next) |
163 | 0 | if (p->count) |
164 | 0 | { |
165 | 0 | h->non_got_ref = 1; |
166 | | /* Need dynamic relocations for non-GOT reference. */ |
167 | 0 | keep = true; |
168 | 0 | if (p->pc_count) |
169 | 0 | { |
170 | | /* Must use PLT for PC-relative reference. */ |
171 | 0 | use_plt = true; |
172 | 0 | need_dynreloc = bfd_link_pic (info); |
173 | 0 | break; |
174 | 0 | } |
175 | 0 | } |
176 | 0 | if (keep) |
177 | 0 | goto keep; |
178 | 0 | } |
179 | | |
180 | | /* Support garbage collection against STT_GNU_IFUNC symbols. */ |
181 | 0 | if (h->plt.refcount <= 0 && h->got.refcount <= 0) |
182 | 0 | { |
183 | 0 | h->got = htab->init_got_offset; |
184 | 0 | h->plt = htab->init_plt_offset; |
185 | 0 | *head = NULL; |
186 | 0 | return true; |
187 | 0 | } |
188 | | |
189 | | /* Return and discard space for dynamic relocations against it if |
190 | | it is never referenced. */ |
191 | 0 | if (!h->ref_regular) |
192 | 0 | { |
193 | 0 | if (h->plt.refcount > 0 |
194 | 0 | || h->got.refcount > 0) |
195 | 0 | abort (); |
196 | 0 | h->got = htab->init_got_offset; |
197 | 0 | h->plt = htab->init_plt_offset; |
198 | 0 | *head = NULL; |
199 | 0 | return true; |
200 | 0 | } |
201 | | |
202 | 0 | keep: |
203 | 0 | bed = get_elf_backend_data (info->output_bfd); |
204 | 0 | if (bed->rela_plts_and_copies_p) |
205 | 0 | sizeof_reloc = bed->s->sizeof_rela; |
206 | 0 | else |
207 | 0 | sizeof_reloc = bed->s->sizeof_rel; |
208 | | |
209 | | /* When building a static executable, use .iplt, .igot.plt and |
210 | | .rel[a].iplt sections for STT_GNU_IFUNC symbols. */ |
211 | 0 | if (htab->splt != NULL) |
212 | 0 | { |
213 | 0 | plt = htab->splt; |
214 | 0 | gotplt = htab->sgotplt; |
215 | 0 | relplt = htab->srelplt; |
216 | | |
217 | | /* If this is the first .plt entry and PLT is used, make room for |
218 | | the special first entry. */ |
219 | 0 | if (plt->size == 0 && use_plt) |
220 | 0 | plt->size += plt_header_size; |
221 | 0 | } |
222 | 0 | else |
223 | 0 | { |
224 | 0 | plt = htab->iplt; |
225 | 0 | gotplt = htab->igotplt; |
226 | 0 | relplt = htab->irelplt; |
227 | 0 | } |
228 | |
|
229 | 0 | if (use_plt) |
230 | 0 | { |
231 | | /* Don't update value of STT_GNU_IFUNC symbol to PLT. We need |
232 | | the original value for R_*_IRELATIVE. */ |
233 | 0 | h->plt.offset = plt->size; |
234 | | |
235 | | /* Make room for this entry in the .plt/.iplt section. */ |
236 | 0 | plt->size += plt_entry_size; |
237 | | |
238 | | /* We also need to make an entry in the .got.plt/.got.iplt section, |
239 | | which will be placed in the .got section by the linker script. */ |
240 | 0 | gotplt->size += got_entry_size; |
241 | 0 | } |
242 | | |
243 | | /* We also need to make an entry in the .rel[a].plt/.rel[a].iplt |
244 | | section for GOTPLT relocation if PLT is used. */ |
245 | 0 | if (use_plt) |
246 | 0 | { |
247 | 0 | relplt->size += sizeof_reloc; |
248 | 0 | relplt->reloc_count++; |
249 | 0 | } |
250 | | |
251 | | /* We need dynamic relocation for STT_GNU_IFUNC symbol only when |
252 | | there is a non-GOT reference in a PIC object or PLT isn't used. */ |
253 | 0 | if (!need_dynreloc || !h->non_got_ref) |
254 | 0 | *head = NULL; |
255 | | |
256 | | /* Finally, allocate space. */ |
257 | 0 | p = *head; |
258 | 0 | if (p != NULL) |
259 | 0 | { |
260 | 0 | bfd_size_type count = 0; |
261 | 0 | do |
262 | 0 | { |
263 | 0 | count += p->count; |
264 | 0 | p = p->next; |
265 | 0 | } |
266 | 0 | while (p != NULL); |
267 | |
|
268 | 0 | htab->ifunc_resolvers = count != 0; |
269 | | |
270 | | /* Dynamic relocations are stored in |
271 | | 1. .rel[a].ifunc section in PIC object. |
272 | | 2. .rel[a].got section in dynamic executable. |
273 | | 3. .rel[a].iplt section in static executable. */ |
274 | 0 | if (bfd_link_pic (info)) |
275 | 0 | htab->irelifunc->size += count * sizeof_reloc; |
276 | 0 | else if (htab->splt != NULL) |
277 | 0 | htab->srelgot->size += count * sizeof_reloc; |
278 | 0 | else |
279 | 0 | { |
280 | 0 | relplt->size += count * sizeof_reloc; |
281 | 0 | relplt->reloc_count += count; |
282 | 0 | } |
283 | 0 | } |
284 | | |
285 | | /* For STT_GNU_IFUNC symbol, .got.plt has the real function address |
286 | | and .got has the PLT entry adddress. We will load the GOT entry |
287 | | with the PLT entry in finish_dynamic_symbol if it is used. For |
288 | | branch, it uses .got.plt. For symbol value, if PLT is used, |
289 | | 1. Use .got.plt in a PIC object if it is forced local or not |
290 | | dynamic. |
291 | | 2. Use .got.plt in a non-PIC object if pointer equality isn't |
292 | | needed. |
293 | | 3. Use .got.plt in PIE. |
294 | | 4. Use .got.plt if .got isn't used. |
295 | | 5. Otherwise use .got so that it can be shared among different |
296 | | objects at run-time. |
297 | | If PLT isn't used, always use .got for symbol value. |
298 | | We only need to relocate .got entry in PIC object or in dynamic |
299 | | executable without PLT. */ |
300 | 0 | if (use_plt |
301 | 0 | && (h->got.refcount <= 0 |
302 | 0 | || (bfd_link_pic (info) |
303 | 0 | && (h->dynindx == -1 |
304 | 0 | || h->forced_local)) |
305 | 0 | || (!bfd_link_pic (info) |
306 | 0 | && !h->pointer_equality_needed) |
307 | 0 | || bfd_link_pie (info) |
308 | 0 | || htab->sgot == NULL)) |
309 | 0 | { |
310 | | /* Use .got.plt. */ |
311 | 0 | h->got.offset = (bfd_vma) -1; |
312 | 0 | } |
313 | 0 | else |
314 | 0 | { |
315 | 0 | if (!use_plt) |
316 | 0 | { |
317 | | /* PLT isn't used. */ |
318 | 0 | h->plt.offset = (bfd_vma) -1; |
319 | 0 | } |
320 | 0 | if (h->got.refcount <= 0) |
321 | 0 | { |
322 | | /* GOT isn't need when there are only relocations for static |
323 | | pointers. */ |
324 | 0 | h->got.offset = (bfd_vma) -1; |
325 | 0 | } |
326 | 0 | else |
327 | 0 | { |
328 | 0 | h->got.offset = htab->sgot->size; |
329 | 0 | htab->sgot->size += got_entry_size; |
330 | | /* Need to relocate the GOT entry in a PIC object or PLT isn't |
331 | | used. Otherwise, the GOT entry will be filled with the PLT |
332 | | entry and dynamic GOT relocation isn't needed. */ |
333 | 0 | if (need_dynreloc) |
334 | 0 | { |
335 | | /* For non-static executable, dynamic GOT relocation is in |
336 | | .rel[a].got section, but for static executable, it is |
337 | | in .rel[a].iplt section. */ |
338 | 0 | if (htab->splt != NULL) |
339 | 0 | htab->srelgot->size += sizeof_reloc; |
340 | 0 | else |
341 | 0 | { |
342 | 0 | relplt->size += sizeof_reloc; |
343 | 0 | relplt->reloc_count++; |
344 | 0 | } |
345 | 0 | } |
346 | 0 | } |
347 | 0 | } |
348 | |
|
349 | 0 | return true; |
350 | 0 | } |