elfxx-x86.c revision 1.1.1.3 1 1.1 christos /* x86 specific support for ELF
2 1.1.1.3 christos Copyright (C) 2017-2020 Free Software Foundation, Inc.
3 1.1 christos
4 1.1 christos This file is part of BFD, the Binary File Descriptor library.
5 1.1 christos
6 1.1 christos This program is free software; you can redistribute it and/or modify
7 1.1 christos it under the terms of the GNU General Public License as published by
8 1.1 christos the Free Software Foundation; either version 3 of the License, or
9 1.1 christos (at your option) any later version.
10 1.1 christos
11 1.1 christos This program is distributed in the hope that it will be useful,
12 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
13 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 1.1 christos GNU General Public License for more details.
15 1.1 christos
16 1.1 christos You should have received a copy of the GNU General Public License
17 1.1 christos along with this program; if not, write to the Free Software
18 1.1 christos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 1.1 christos MA 02110-1301, USA. */
20 1.1 christos
21 1.1 christos #include "elfxx-x86.h"
22 1.1 christos #include "elf-vxworks.h"
23 1.1 christos #include "objalloc.h"
24 1.1 christos #include "elf/i386.h"
25 1.1 christos #include "elf/x86-64.h"
26 1.1 christos
27 1.1 christos /* The name of the dynamic interpreter. This is put in the .interp
28 1.1 christos section. */
29 1.1 christos
30 1.1 christos #define ELF32_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
31 1.1 christos #define ELF64_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
32 1.1 christos #define ELFX32_DYNAMIC_INTERPRETER "/lib/ldx32.so.1"
33 1.1 christos
34 1.1 christos bfd_boolean
35 1.1 christos _bfd_x86_elf_mkobject (bfd *abfd)
36 1.1 christos {
37 1.1 christos return bfd_elf_allocate_object (abfd,
38 1.1 christos sizeof (struct elf_x86_obj_tdata),
39 1.1 christos get_elf_backend_data (abfd)->target_id);
40 1.1 christos }
41 1.1 christos
42 1.1 christos /* _TLS_MODULE_BASE_ needs to be treated especially when linking
43 1.1 christos executables. Rather than setting it to the beginning of the TLS
44 1.1 christos section, we have to set it to the end. This function may be called
45 1.1 christos multiple times, it is idempotent. */
46 1.1 christos
47 1.1 christos void
48 1.1 christos _bfd_x86_elf_set_tls_module_base (struct bfd_link_info *info)
49 1.1 christos {
50 1.1 christos struct elf_x86_link_hash_table *htab;
51 1.1 christos struct bfd_link_hash_entry *base;
52 1.1 christos const struct elf_backend_data *bed;
53 1.1 christos
54 1.1 christos if (!bfd_link_executable (info))
55 1.1 christos return;
56 1.1 christos
57 1.1 christos bed = get_elf_backend_data (info->output_bfd);
58 1.1 christos htab = elf_x86_hash_table (info, bed->target_id);
59 1.1 christos if (htab == NULL)
60 1.1 christos return;
61 1.1 christos
62 1.1 christos base = htab->tls_module_base;
63 1.1 christos if (base == NULL)
64 1.1 christos return;
65 1.1 christos
66 1.1 christos base->u.def.value = htab->elf.tls_size;
67 1.1 christos }
68 1.1 christos
69 1.1 christos /* Return the base VMA address which should be subtracted from real addresses
70 1.1 christos when resolving @dtpoff relocation.
71 1.1 christos This is PT_TLS segment p_vaddr. */
72 1.1 christos
73 1.1 christos bfd_vma
74 1.1 christos _bfd_x86_elf_dtpoff_base (struct bfd_link_info *info)
75 1.1 christos {
76 1.1 christos /* If tls_sec is NULL, we should have signalled an error already. */
77 1.1 christos if (elf_hash_table (info)->tls_sec == NULL)
78 1.1 christos return 0;
79 1.1 christos return elf_hash_table (info)->tls_sec->vma;
80 1.1 christos }
81 1.1 christos
82 1.1 christos /* Allocate space in .plt, .got and associated reloc sections for
83 1.1 christos dynamic relocs. */
84 1.1 christos
85 1.1 christos static bfd_boolean
86 1.1 christos elf_x86_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
87 1.1 christos {
88 1.1 christos struct bfd_link_info *info;
89 1.1 christos struct elf_x86_link_hash_table *htab;
90 1.1 christos struct elf_x86_link_hash_entry *eh;
91 1.1 christos struct elf_dyn_relocs *p;
92 1.1 christos unsigned int plt_entry_size;
93 1.1 christos bfd_boolean resolved_to_zero;
94 1.1 christos const struct elf_backend_data *bed;
95 1.1 christos
96 1.1 christos if (h->root.type == bfd_link_hash_indirect)
97 1.1 christos return TRUE;
98 1.1 christos
99 1.1 christos eh = (struct elf_x86_link_hash_entry *) h;
100 1.1 christos
101 1.1 christos info = (struct bfd_link_info *) inf;
102 1.1 christos bed = get_elf_backend_data (info->output_bfd);
103 1.1 christos htab = elf_x86_hash_table (info, bed->target_id);
104 1.1 christos if (htab == NULL)
105 1.1 christos return FALSE;
106 1.1 christos
107 1.1 christos plt_entry_size = htab->plt.plt_entry_size;
108 1.1 christos
109 1.1 christos resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
110 1.1 christos
111 1.1 christos /* We can't use the GOT PLT if pointer equality is needed since
112 1.1 christos finish_dynamic_symbol won't clear symbol value and the dynamic
113 1.1 christos linker won't update the GOT slot. We will get into an infinite
114 1.1 christos loop at run-time. */
115 1.1 christos if (htab->plt_got != NULL
116 1.1 christos && h->type != STT_GNU_IFUNC
117 1.1 christos && !h->pointer_equality_needed
118 1.1 christos && h->plt.refcount > 0
119 1.1 christos && h->got.refcount > 0)
120 1.1 christos {
121 1.1 christos /* Don't use the regular PLT if there are both GOT and GOTPLT
122 1.1 christos reloctions. */
123 1.1 christos h->plt.offset = (bfd_vma) -1;
124 1.1 christos
125 1.1 christos /* Use the GOT PLT. */
126 1.1 christos eh->plt_got.refcount = 1;
127 1.1 christos }
128 1.1 christos
129 1.1 christos /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
130 1.1 christos here if it is defined and referenced in a non-shared object. */
131 1.1 christos if (h->type == STT_GNU_IFUNC
132 1.1 christos && h->def_regular)
133 1.1 christos {
134 1.1 christos if (_bfd_elf_allocate_ifunc_dyn_relocs (info, h, &eh->dyn_relocs,
135 1.1 christos &htab->readonly_dynrelocs_against_ifunc,
136 1.1 christos plt_entry_size,
137 1.1 christos (htab->plt.has_plt0
138 1.1 christos * plt_entry_size),
139 1.1 christos htab->got_entry_size,
140 1.1 christos TRUE))
141 1.1 christos {
142 1.1 christos asection *s = htab->plt_second;
143 1.1 christos if (h->plt.offset != (bfd_vma) -1 && s != NULL)
144 1.1 christos {
145 1.1 christos /* Use the second PLT section if it is created. */
146 1.1 christos eh->plt_second.offset = s->size;
147 1.1 christos
148 1.1 christos /* Make room for this entry in the second PLT section. */
149 1.1 christos s->size += htab->non_lazy_plt->plt_entry_size;
150 1.1 christos }
151 1.1 christos
152 1.1 christos return TRUE;
153 1.1 christos }
154 1.1 christos else
155 1.1 christos return FALSE;
156 1.1 christos }
157 1.1 christos /* Don't create the PLT entry if there are only function pointer
158 1.1 christos relocations which can be resolved at run-time. */
159 1.1 christos else if (htab->elf.dynamic_sections_created
160 1.1 christos && (h->plt.refcount > 0
161 1.1 christos || eh->plt_got.refcount > 0))
162 1.1 christos {
163 1.1 christos bfd_boolean use_plt_got = eh->plt_got.refcount > 0;
164 1.1 christos
165 1.1 christos /* Make sure this symbol is output as a dynamic symbol.
166 1.1 christos Undefined weak syms won't yet be marked as dynamic. */
167 1.1 christos if (h->dynindx == -1
168 1.1 christos && !h->forced_local
169 1.1 christos && !resolved_to_zero
170 1.1 christos && h->root.type == bfd_link_hash_undefweak)
171 1.1 christos {
172 1.1 christos if (! bfd_elf_link_record_dynamic_symbol (info, h))
173 1.1 christos return FALSE;
174 1.1 christos }
175 1.1 christos
176 1.1 christos if (bfd_link_pic (info)
177 1.1 christos || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
178 1.1 christos {
179 1.1 christos asection *s = htab->elf.splt;
180 1.1 christos asection *second_s = htab->plt_second;
181 1.1 christos asection *got_s = htab->plt_got;
182 1.1.1.2 christos bfd_boolean use_plt;
183 1.1 christos
184 1.1 christos /* If this is the first .plt entry, make room for the special
185 1.1 christos first entry. The .plt section is used by prelink to undo
186 1.1 christos prelinking for dynamic relocations. */
187 1.1 christos if (s->size == 0)
188 1.1 christos s->size = htab->plt.has_plt0 * plt_entry_size;
189 1.1 christos
190 1.1 christos if (use_plt_got)
191 1.1 christos eh->plt_got.offset = got_s->size;
192 1.1 christos else
193 1.1 christos {
194 1.1 christos h->plt.offset = s->size;
195 1.1 christos if (second_s)
196 1.1 christos eh->plt_second.offset = second_s->size;
197 1.1 christos }
198 1.1 christos
199 1.1 christos /* If this symbol is not defined in a regular file, and we are
200 1.1.1.2 christos generating PDE, then set the symbol to this location in the
201 1.1.1.2 christos .plt. This is required to make function pointers compare
202 1.1.1.2 christos as equal between PDE and the shared library.
203 1.1.1.2 christos
204 1.1.1.2 christos NB: If PLT is PC-relative, we can use the .plt in PIE for
205 1.1.1.2 christos function address. */
206 1.1.1.2 christos if (h->def_regular)
207 1.1.1.2 christos use_plt = FALSE;
208 1.1.1.2 christos else if (htab->pcrel_plt)
209 1.1.1.2 christos use_plt = ! bfd_link_dll (info);
210 1.1.1.2 christos else
211 1.1.1.2 christos use_plt = bfd_link_pde (info);
212 1.1.1.2 christos if (use_plt)
213 1.1 christos {
214 1.1 christos if (use_plt_got)
215 1.1 christos {
216 1.1 christos /* We need to make a call to the entry of the GOT PLT
217 1.1 christos instead of regular PLT entry. */
218 1.1 christos h->root.u.def.section = got_s;
219 1.1 christos h->root.u.def.value = eh->plt_got.offset;
220 1.1 christos }
221 1.1 christos else
222 1.1 christos {
223 1.1 christos if (second_s)
224 1.1 christos {
225 1.1 christos /* We need to make a call to the entry of the
226 1.1 christos second PLT instead of regular PLT entry. */
227 1.1 christos h->root.u.def.section = second_s;
228 1.1 christos h->root.u.def.value = eh->plt_second.offset;
229 1.1 christos }
230 1.1 christos else
231 1.1 christos {
232 1.1 christos h->root.u.def.section = s;
233 1.1 christos h->root.u.def.value = h->plt.offset;
234 1.1 christos }
235 1.1 christos }
236 1.1 christos }
237 1.1 christos
238 1.1 christos /* Make room for this entry. */
239 1.1 christos if (use_plt_got)
240 1.1 christos got_s->size += htab->non_lazy_plt->plt_entry_size;
241 1.1 christos else
242 1.1 christos {
243 1.1 christos s->size += plt_entry_size;
244 1.1 christos if (second_s)
245 1.1 christos second_s->size += htab->non_lazy_plt->plt_entry_size;
246 1.1 christos
247 1.1 christos /* We also need to make an entry in the .got.plt section,
248 1.1 christos which will be placed in the .got section by the linker
249 1.1 christos script. */
250 1.1 christos htab->elf.sgotplt->size += htab->got_entry_size;
251 1.1 christos
252 1.1 christos /* There should be no PLT relocation against resolved
253 1.1 christos undefined weak symbol in executable. */
254 1.1 christos if (!resolved_to_zero)
255 1.1 christos {
256 1.1 christos /* We also need to make an entry in the .rel.plt
257 1.1 christos section. */
258 1.1 christos htab->elf.srelplt->size += htab->sizeof_reloc;
259 1.1 christos htab->elf.srelplt->reloc_count++;
260 1.1 christos }
261 1.1 christos }
262 1.1 christos
263 1.1 christos if (htab->target_os == is_vxworks && !bfd_link_pic (info))
264 1.1 christos {
265 1.1 christos /* VxWorks has a second set of relocations for each PLT entry
266 1.1 christos in executables. They go in a separate relocation section,
267 1.1 christos which is processed by the kernel loader. */
268 1.1 christos
269 1.1 christos /* There are two relocations for the initial PLT entry: an
270 1.1 christos R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
271 1.1 christos R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
272 1.1 christos
273 1.1 christos asection *srelplt2 = htab->srelplt2;
274 1.1 christos if (h->plt.offset == plt_entry_size)
275 1.1 christos srelplt2->size += (htab->sizeof_reloc * 2);
276 1.1 christos
277 1.1 christos /* There are two extra relocations for each subsequent PLT entry:
278 1.1 christos an R_386_32 relocation for the GOT entry, and an R_386_32
279 1.1 christos relocation for the PLT entry. */
280 1.1 christos
281 1.1 christos srelplt2->size += (htab->sizeof_reloc * 2);
282 1.1 christos }
283 1.1 christos }
284 1.1 christos else
285 1.1 christos {
286 1.1 christos eh->plt_got.offset = (bfd_vma) -1;
287 1.1 christos h->plt.offset = (bfd_vma) -1;
288 1.1 christos h->needs_plt = 0;
289 1.1 christos }
290 1.1 christos }
291 1.1 christos else
292 1.1 christos {
293 1.1 christos eh->plt_got.offset = (bfd_vma) -1;
294 1.1 christos h->plt.offset = (bfd_vma) -1;
295 1.1 christos h->needs_plt = 0;
296 1.1 christos }
297 1.1 christos
298 1.1 christos eh->tlsdesc_got = (bfd_vma) -1;
299 1.1 christos
300 1.1 christos /* For i386, if R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the
301 1.1 christos binary, make it a R_386_TLS_LE_32 requiring no TLS entry. For
302 1.1 christos x86-64, if R_X86_64_GOTTPOFF symbol is now local to the binary,
303 1.1 christos make it a R_X86_64_TPOFF32 requiring no GOT entry. */
304 1.1 christos if (h->got.refcount > 0
305 1.1 christos && bfd_link_executable (info)
306 1.1 christos && h->dynindx == -1
307 1.1 christos && (elf_x86_hash_entry (h)->tls_type & GOT_TLS_IE))
308 1.1 christos h->got.offset = (bfd_vma) -1;
309 1.1 christos else if (h->got.refcount > 0)
310 1.1 christos {
311 1.1 christos asection *s;
312 1.1 christos bfd_boolean dyn;
313 1.1 christos int tls_type = elf_x86_hash_entry (h)->tls_type;
314 1.1 christos
315 1.1 christos /* Make sure this symbol is output as a dynamic symbol.
316 1.1 christos Undefined weak syms won't yet be marked as dynamic. */
317 1.1 christos if (h->dynindx == -1
318 1.1 christos && !h->forced_local
319 1.1 christos && !resolved_to_zero
320 1.1 christos && h->root.type == bfd_link_hash_undefweak)
321 1.1 christos {
322 1.1 christos if (! bfd_elf_link_record_dynamic_symbol (info, h))
323 1.1 christos return FALSE;
324 1.1 christos }
325 1.1 christos
326 1.1 christos s = htab->elf.sgot;
327 1.1 christos if (GOT_TLS_GDESC_P (tls_type))
328 1.1 christos {
329 1.1 christos eh->tlsdesc_got = htab->elf.sgotplt->size
330 1.1 christos - elf_x86_compute_jump_table_size (htab);
331 1.1 christos htab->elf.sgotplt->size += 2 * htab->got_entry_size;
332 1.1 christos h->got.offset = (bfd_vma) -2;
333 1.1 christos }
334 1.1 christos if (! GOT_TLS_GDESC_P (tls_type)
335 1.1 christos || GOT_TLS_GD_P (tls_type))
336 1.1 christos {
337 1.1 christos h->got.offset = s->size;
338 1.1 christos s->size += htab->got_entry_size;
339 1.1 christos /* R_386_TLS_GD and R_X86_64_TLSGD need 2 consecutive GOT
340 1.1 christos slots. */
341 1.1 christos if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
342 1.1 christos s->size += htab->got_entry_size;
343 1.1 christos }
344 1.1 christos dyn = htab->elf.dynamic_sections_created;
345 1.1 christos /* R_386_TLS_IE_32 needs one dynamic relocation,
346 1.1 christos R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
347 1.1 christos (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
348 1.1 christos need two), R_386_TLS_GD and R_X86_64_TLSGD need one if local
349 1.1 christos symbol and two if global. No dynamic relocation against
350 1.1 christos resolved undefined weak symbol in executable. */
351 1.1 christos if (tls_type == GOT_TLS_IE_BOTH)
352 1.1 christos htab->elf.srelgot->size += 2 * htab->sizeof_reloc;
353 1.1 christos else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
354 1.1 christos || (tls_type & GOT_TLS_IE))
355 1.1 christos htab->elf.srelgot->size += htab->sizeof_reloc;
356 1.1 christos else if (GOT_TLS_GD_P (tls_type))
357 1.1 christos htab->elf.srelgot->size += 2 * htab->sizeof_reloc;
358 1.1 christos else if (! GOT_TLS_GDESC_P (tls_type)
359 1.1 christos && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
360 1.1 christos && !resolved_to_zero)
361 1.1 christos || h->root.type != bfd_link_hash_undefweak)
362 1.1 christos && (bfd_link_pic (info)
363 1.1 christos || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
364 1.1 christos htab->elf.srelgot->size += htab->sizeof_reloc;
365 1.1 christos if (GOT_TLS_GDESC_P (tls_type))
366 1.1 christos {
367 1.1 christos htab->elf.srelplt->size += htab->sizeof_reloc;
368 1.1 christos if (bed->target_id == X86_64_ELF_DATA)
369 1.1 christos htab->tlsdesc_plt = (bfd_vma) -1;
370 1.1 christos }
371 1.1 christos }
372 1.1 christos else
373 1.1 christos h->got.offset = (bfd_vma) -1;
374 1.1 christos
375 1.1 christos if (eh->dyn_relocs == NULL)
376 1.1 christos return TRUE;
377 1.1 christos
378 1.1 christos /* In the shared -Bsymbolic case, discard space allocated for
379 1.1 christos dynamic pc-relative relocs against symbols which turn out to be
380 1.1 christos defined in regular objects. For the normal shared case, discard
381 1.1 christos space for pc-relative relocs that have become local due to symbol
382 1.1 christos visibility changes. */
383 1.1 christos
384 1.1 christos if (bfd_link_pic (info))
385 1.1 christos {
386 1.1 christos /* Relocs that use pc_count are those that appear on a call
387 1.1 christos insn, or certain REL relocs that can generated via assembly.
388 1.1 christos We want calls to protected symbols to resolve directly to the
389 1.1 christos function rather than going via the plt. If people want
390 1.1 christos function pointer comparisons to work as expected then they
391 1.1 christos should avoid writing weird assembly. */
392 1.1 christos if (SYMBOL_CALLS_LOCAL (info, h))
393 1.1 christos {
394 1.1 christos struct elf_dyn_relocs **pp;
395 1.1 christos
396 1.1 christos for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
397 1.1 christos {
398 1.1 christos p->count -= p->pc_count;
399 1.1 christos p->pc_count = 0;
400 1.1 christos if (p->count == 0)
401 1.1 christos *pp = p->next;
402 1.1 christos else
403 1.1 christos pp = &p->next;
404 1.1 christos }
405 1.1 christos }
406 1.1 christos
407 1.1 christos if (htab->target_os == is_vxworks)
408 1.1 christos {
409 1.1 christos struct elf_dyn_relocs **pp;
410 1.1 christos for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
411 1.1 christos {
412 1.1 christos if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
413 1.1 christos *pp = p->next;
414 1.1 christos else
415 1.1 christos pp = &p->next;
416 1.1 christos }
417 1.1 christos }
418 1.1 christos
419 1.1 christos /* Also discard relocs on undefined weak syms with non-default
420 1.1 christos visibility or in PIE. */
421 1.1 christos if (eh->dyn_relocs != NULL)
422 1.1 christos {
423 1.1 christos if (h->root.type == bfd_link_hash_undefweak)
424 1.1 christos {
425 1.1 christos /* Undefined weak symbol is never bound locally in shared
426 1.1 christos library. */
427 1.1 christos if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
428 1.1 christos || resolved_to_zero)
429 1.1 christos {
430 1.1 christos if (bed->target_id == I386_ELF_DATA
431 1.1 christos && h->non_got_ref)
432 1.1 christos {
433 1.1 christos /* Keep dynamic non-GOT/non-PLT relocation so
434 1.1 christos that we can branch to 0 without PLT. */
435 1.1 christos struct elf_dyn_relocs **pp;
436 1.1 christos
437 1.1 christos for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
438 1.1 christos if (p->pc_count == 0)
439 1.1 christos *pp = p->next;
440 1.1 christos else
441 1.1 christos {
442 1.1 christos /* Remove non-R_386_PC32 relocation. */
443 1.1 christos p->count = p->pc_count;
444 1.1 christos pp = &p->next;
445 1.1 christos }
446 1.1 christos
447 1.1 christos /* Make sure undefined weak symbols are output
448 1.1 christos as dynamic symbols in PIEs for dynamic non-GOT
449 1.1 christos non-PLT reloations. */
450 1.1 christos if (eh->dyn_relocs != NULL
451 1.1 christos && !bfd_elf_link_record_dynamic_symbol (info, h))
452 1.1 christos return FALSE;
453 1.1 christos }
454 1.1 christos else
455 1.1 christos eh->dyn_relocs = NULL;
456 1.1 christos }
457 1.1 christos else if (h->dynindx == -1
458 1.1 christos && !h->forced_local
459 1.1 christos && !bfd_elf_link_record_dynamic_symbol (info, h))
460 1.1 christos return FALSE;
461 1.1 christos }
462 1.1 christos else if (bfd_link_executable (info)
463 1.1 christos && (h->needs_copy || eh->needs_copy)
464 1.1 christos && h->def_dynamic
465 1.1 christos && !h->def_regular)
466 1.1 christos {
467 1.1 christos /* NB: needs_copy is set only for x86-64. For PIE,
468 1.1 christos discard space for pc-relative relocs against symbols
469 1.1 christos which turn out to need copy relocs. */
470 1.1 christos struct elf_dyn_relocs **pp;
471 1.1 christos
472 1.1 christos for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
473 1.1 christos {
474 1.1 christos if (p->pc_count != 0)
475 1.1 christos *pp = p->next;
476 1.1 christos else
477 1.1 christos pp = &p->next;
478 1.1 christos }
479 1.1 christos }
480 1.1 christos }
481 1.1 christos }
482 1.1 christos else if (ELIMINATE_COPY_RELOCS)
483 1.1 christos {
484 1.1 christos /* For the non-shared case, discard space for relocs against
485 1.1 christos symbols which turn out to need copy relocs or are not
486 1.1 christos dynamic. Keep dynamic relocations for run-time function
487 1.1 christos pointer initialization. */
488 1.1 christos
489 1.1 christos if ((!h->non_got_ref
490 1.1 christos || (h->root.type == bfd_link_hash_undefweak
491 1.1 christos && !resolved_to_zero))
492 1.1 christos && ((h->def_dynamic
493 1.1 christos && !h->def_regular)
494 1.1 christos || (htab->elf.dynamic_sections_created
495 1.1 christos && (h->root.type == bfd_link_hash_undefweak
496 1.1 christos || h->root.type == bfd_link_hash_undefined))))
497 1.1 christos {
498 1.1 christos /* Make sure this symbol is output as a dynamic symbol.
499 1.1 christos Undefined weak syms won't yet be marked as dynamic. */
500 1.1 christos if (h->dynindx == -1
501 1.1 christos && !h->forced_local
502 1.1 christos && !resolved_to_zero
503 1.1 christos && h->root.type == bfd_link_hash_undefweak
504 1.1 christos && ! bfd_elf_link_record_dynamic_symbol (info, h))
505 1.1 christos return FALSE;
506 1.1 christos
507 1.1 christos /* If that succeeded, we know we'll be keeping all the
508 1.1 christos relocs. */
509 1.1 christos if (h->dynindx != -1)
510 1.1 christos goto keep;
511 1.1 christos }
512 1.1 christos
513 1.1 christos eh->dyn_relocs = NULL;
514 1.1 christos
515 1.1 christos keep: ;
516 1.1 christos }
517 1.1 christos
518 1.1 christos /* Finally, allocate space. */
519 1.1 christos for (p = eh->dyn_relocs; p != NULL; p = p->next)
520 1.1 christos {
521 1.1 christos asection *sreloc;
522 1.1 christos
523 1.1 christos sreloc = elf_section_data (p->sec)->sreloc;
524 1.1 christos
525 1.1 christos BFD_ASSERT (sreloc != NULL);
526 1.1 christos sreloc->size += p->count * htab->sizeof_reloc;
527 1.1 christos }
528 1.1 christos
529 1.1 christos return TRUE;
530 1.1 christos }
531 1.1 christos
532 1.1 christos /* Find dynamic relocs for H that apply to read-only sections. */
533 1.1 christos
534 1.1 christos static asection *
535 1.1 christos readonly_dynrelocs (struct elf_link_hash_entry *h)
536 1.1 christos {
537 1.1 christos struct elf_dyn_relocs *p;
538 1.1 christos
539 1.1 christos for (p = elf_x86_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
540 1.1 christos {
541 1.1 christos asection *s = p->sec->output_section;
542 1.1 christos
543 1.1 christos if (s != NULL && (s->flags & SEC_READONLY) != 0)
544 1.1 christos return p->sec;
545 1.1 christos }
546 1.1 christos return NULL;
547 1.1 christos }
548 1.1 christos
549 1.1 christos /* Set DF_TEXTREL if we find any dynamic relocs that apply to
550 1.1 christos read-only sections. */
551 1.1 christos
552 1.1 christos static bfd_boolean
553 1.1 christos maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
554 1.1 christos {
555 1.1 christos asection *sec;
556 1.1 christos
557 1.1 christos if (h->root.type == bfd_link_hash_indirect)
558 1.1 christos return TRUE;
559 1.1 christos
560 1.1 christos /* Skip local IFUNC symbols. */
561 1.1 christos if (h->forced_local && h->type == STT_GNU_IFUNC)
562 1.1 christos return TRUE;
563 1.1 christos
564 1.1 christos sec = readonly_dynrelocs (h);
565 1.1 christos if (sec != NULL)
566 1.1 christos {
567 1.1 christos struct bfd_link_info *info = (struct bfd_link_info *) inf;
568 1.1 christos
569 1.1 christos info->flags |= DF_TEXTREL;
570 1.1 christos /* xgettext:c-format */
571 1.1.1.2 christos info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
572 1.1.1.2 christos "in read-only section `%pA'\n"),
573 1.1 christos sec->owner, h->root.root.string, sec);
574 1.1 christos
575 1.1 christos if ((info->warn_shared_textrel && bfd_link_pic (info))
576 1.1 christos || info->error_textrel)
577 1.1 christos /* xgettext:c-format */
578 1.1.1.2 christos info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
579 1.1.1.2 christos "in read-only section `%pA'\n"),
580 1.1 christos sec->owner, h->root.root.string, sec);
581 1.1 christos
582 1.1 christos /* Not an error, just cut short the traversal. */
583 1.1 christos return FALSE;
584 1.1 christos }
585 1.1 christos return TRUE;
586 1.1 christos }
587 1.1 christos
588 1.1 christos /* Allocate space in .plt, .got and associated reloc sections for
589 1.1 christos local dynamic relocs. */
590 1.1 christos
591 1.1 christos static bfd_boolean
592 1.1 christos elf_x86_allocate_local_dynreloc (void **slot, void *inf)
593 1.1 christos {
594 1.1 christos struct elf_link_hash_entry *h
595 1.1 christos = (struct elf_link_hash_entry *) *slot;
596 1.1 christos
597 1.1 christos if (h->type != STT_GNU_IFUNC
598 1.1 christos || !h->def_regular
599 1.1 christos || !h->ref_regular
600 1.1 christos || !h->forced_local
601 1.1 christos || h->root.type != bfd_link_hash_defined)
602 1.1 christos abort ();
603 1.1 christos
604 1.1 christos return elf_x86_allocate_dynrelocs (h, inf);
605 1.1 christos }
606 1.1 christos
607 1.1 christos /* Find and/or create a hash entry for local symbol. */
608 1.1 christos
609 1.1 christos struct elf_link_hash_entry *
610 1.1 christos _bfd_elf_x86_get_local_sym_hash (struct elf_x86_link_hash_table *htab,
611 1.1 christos bfd *abfd, const Elf_Internal_Rela *rel,
612 1.1 christos bfd_boolean create)
613 1.1 christos {
614 1.1 christos struct elf_x86_link_hash_entry e, *ret;
615 1.1 christos asection *sec = abfd->sections;
616 1.1 christos hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
617 1.1 christos htab->r_sym (rel->r_info));
618 1.1 christos void **slot;
619 1.1 christos
620 1.1 christos e.elf.indx = sec->id;
621 1.1 christos e.elf.dynstr_index = htab->r_sym (rel->r_info);
622 1.1 christos slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
623 1.1 christos create ? INSERT : NO_INSERT);
624 1.1 christos
625 1.1 christos if (!slot)
626 1.1 christos return NULL;
627 1.1 christos
628 1.1 christos if (*slot)
629 1.1 christos {
630 1.1 christos ret = (struct elf_x86_link_hash_entry *) *slot;
631 1.1 christos return &ret->elf;
632 1.1 christos }
633 1.1 christos
634 1.1 christos ret = (struct elf_x86_link_hash_entry *)
635 1.1 christos objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
636 1.1 christos sizeof (struct elf_x86_link_hash_entry));
637 1.1 christos if (ret)
638 1.1 christos {
639 1.1 christos memset (ret, 0, sizeof (*ret));
640 1.1 christos ret->elf.indx = sec->id;
641 1.1 christos ret->elf.dynstr_index = htab->r_sym (rel->r_info);
642 1.1 christos ret->elf.dynindx = -1;
643 1.1 christos ret->plt_got.offset = (bfd_vma) -1;
644 1.1 christos *slot = ret;
645 1.1 christos }
646 1.1 christos return &ret->elf;
647 1.1 christos }
648 1.1 christos
649 1.1 christos /* Create an entry in a x86 ELF linker hash table. NB: THIS MUST BE IN
650 1.1 christos SYNC WITH _bfd_elf_link_hash_newfunc. */
651 1.1 christos
652 1.1 christos struct bfd_hash_entry *
653 1.1 christos _bfd_x86_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
654 1.1 christos struct bfd_hash_table *table,
655 1.1 christos const char *string)
656 1.1 christos {
657 1.1 christos /* Allocate the structure if it has not already been allocated by a
658 1.1 christos subclass. */
659 1.1 christos if (entry == NULL)
660 1.1 christos {
661 1.1 christos entry = (struct bfd_hash_entry *)
662 1.1 christos bfd_hash_allocate (table,
663 1.1 christos sizeof (struct elf_x86_link_hash_entry));
664 1.1 christos if (entry == NULL)
665 1.1 christos return entry;
666 1.1 christos }
667 1.1 christos
668 1.1 christos /* Call the allocation method of the superclass. */
669 1.1 christos entry = _bfd_link_hash_newfunc (entry, table, string);
670 1.1 christos if (entry != NULL)
671 1.1 christos {
672 1.1 christos struct elf_x86_link_hash_entry *eh
673 1.1 christos = (struct elf_x86_link_hash_entry *) entry;
674 1.1 christos struct elf_link_hash_table *htab
675 1.1 christos = (struct elf_link_hash_table *) table;
676 1.1 christos
677 1.1 christos memset (&eh->elf.size, 0,
678 1.1 christos (sizeof (struct elf_x86_link_hash_entry)
679 1.1 christos - offsetof (struct elf_link_hash_entry, size)));
680 1.1 christos /* Set local fields. */
681 1.1 christos eh->elf.indx = -1;
682 1.1 christos eh->elf.dynindx = -1;
683 1.1 christos eh->elf.got = htab->init_got_refcount;
684 1.1 christos eh->elf.plt = htab->init_plt_refcount;
685 1.1 christos /* Assume that we have been called by a non-ELF symbol reader.
686 1.1 christos This flag is then reset by the code which reads an ELF input
687 1.1 christos file. This ensures that a symbol created by a non-ELF symbol
688 1.1 christos reader will have the flag set correctly. */
689 1.1 christos eh->elf.non_elf = 1;
690 1.1 christos eh->plt_second.offset = (bfd_vma) -1;
691 1.1 christos eh->plt_got.offset = (bfd_vma) -1;
692 1.1 christos eh->tlsdesc_got = (bfd_vma) -1;
693 1.1 christos eh->zero_undefweak = 1;
694 1.1 christos }
695 1.1 christos
696 1.1 christos return entry;
697 1.1 christos }
698 1.1 christos
699 1.1 christos /* Compute a hash of a local hash entry. We use elf_link_hash_entry
700 1.1 christos for local symbol so that we can handle local STT_GNU_IFUNC symbols
701 1.1 christos as global symbol. We reuse indx and dynstr_index for local symbol
702 1.1 christos hash since they aren't used by global symbols in this backend. */
703 1.1 christos
704 1.1 christos hashval_t
705 1.1 christos _bfd_x86_elf_local_htab_hash (const void *ptr)
706 1.1 christos {
707 1.1 christos struct elf_link_hash_entry *h
708 1.1 christos = (struct elf_link_hash_entry *) ptr;
709 1.1 christos return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
710 1.1 christos }
711 1.1 christos
712 1.1 christos /* Compare local hash entries. */
713 1.1 christos
714 1.1 christos int
715 1.1 christos _bfd_x86_elf_local_htab_eq (const void *ptr1, const void *ptr2)
716 1.1 christos {
717 1.1 christos struct elf_link_hash_entry *h1
718 1.1 christos = (struct elf_link_hash_entry *) ptr1;
719 1.1 christos struct elf_link_hash_entry *h2
720 1.1 christos = (struct elf_link_hash_entry *) ptr2;
721 1.1 christos
722 1.1 christos return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
723 1.1 christos }
724 1.1 christos
725 1.1 christos /* Destroy an x86 ELF linker hash table. */
726 1.1 christos
727 1.1 christos static void
728 1.1 christos elf_x86_link_hash_table_free (bfd *obfd)
729 1.1 christos {
730 1.1 christos struct elf_x86_link_hash_table *htab
731 1.1 christos = (struct elf_x86_link_hash_table *) obfd->link.hash;
732 1.1 christos
733 1.1 christos if (htab->loc_hash_table)
734 1.1 christos htab_delete (htab->loc_hash_table);
735 1.1 christos if (htab->loc_hash_memory)
736 1.1 christos objalloc_free ((struct objalloc *) htab->loc_hash_memory);
737 1.1 christos _bfd_elf_link_hash_table_free (obfd);
738 1.1 christos }
739 1.1 christos
740 1.1 christos static bfd_boolean
741 1.1 christos elf_i386_is_reloc_section (const char *secname)
742 1.1 christos {
743 1.1 christos return CONST_STRNEQ (secname, ".rel");
744 1.1 christos }
745 1.1 christos
746 1.1 christos static bfd_boolean
747 1.1 christos elf_x86_64_is_reloc_section (const char *secname)
748 1.1 christos {
749 1.1 christos return CONST_STRNEQ (secname, ".rela");
750 1.1 christos }
751 1.1 christos
752 1.1 christos /* Create an x86 ELF linker hash table. */
753 1.1 christos
754 1.1 christos struct bfd_link_hash_table *
755 1.1 christos _bfd_x86_elf_link_hash_table_create (bfd *abfd)
756 1.1 christos {
757 1.1 christos struct elf_x86_link_hash_table *ret;
758 1.1 christos const struct elf_backend_data *bed;
759 1.1 christos bfd_size_type amt = sizeof (struct elf_x86_link_hash_table);
760 1.1 christos
761 1.1 christos ret = (struct elf_x86_link_hash_table *) bfd_zmalloc (amt);
762 1.1 christos if (ret == NULL)
763 1.1 christos return NULL;
764 1.1 christos
765 1.1 christos bed = get_elf_backend_data (abfd);
766 1.1 christos if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
767 1.1 christos _bfd_x86_elf_link_hash_newfunc,
768 1.1 christos sizeof (struct elf_x86_link_hash_entry),
769 1.1 christos bed->target_id))
770 1.1 christos {
771 1.1 christos free (ret);
772 1.1 christos return NULL;
773 1.1 christos }
774 1.1 christos
775 1.1 christos if (bed->target_id == X86_64_ELF_DATA)
776 1.1 christos {
777 1.1 christos ret->is_reloc_section = elf_x86_64_is_reloc_section;
778 1.1 christos ret->dt_reloc = DT_RELA;
779 1.1 christos ret->dt_reloc_sz = DT_RELASZ;
780 1.1 christos ret->dt_reloc_ent = DT_RELAENT;
781 1.1 christos ret->got_entry_size = 8;
782 1.1.1.2 christos ret->pcrel_plt = TRUE;
783 1.1 christos ret->tls_get_addr = "__tls_get_addr";
784 1.1 christos }
785 1.1 christos if (ABI_64_P (abfd))
786 1.1 christos {
787 1.1 christos ret->sizeof_reloc = sizeof (Elf64_External_Rela);
788 1.1 christos ret->pointer_r_type = R_X86_64_64;
789 1.1 christos ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
790 1.1 christos ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
791 1.1 christos }
792 1.1 christos else
793 1.1 christos {
794 1.1 christos if (bed->target_id == X86_64_ELF_DATA)
795 1.1 christos {
796 1.1 christos ret->sizeof_reloc = sizeof (Elf32_External_Rela);
797 1.1 christos ret->pointer_r_type = R_X86_64_32;
798 1.1 christos ret->dynamic_interpreter = ELFX32_DYNAMIC_INTERPRETER;
799 1.1 christos ret->dynamic_interpreter_size
800 1.1 christos = sizeof ELFX32_DYNAMIC_INTERPRETER;
801 1.1 christos }
802 1.1 christos else
803 1.1 christos {
804 1.1 christos ret->is_reloc_section = elf_i386_is_reloc_section;
805 1.1 christos ret->dt_reloc = DT_REL;
806 1.1 christos ret->dt_reloc_sz = DT_RELSZ;
807 1.1 christos ret->dt_reloc_ent = DT_RELENT;
808 1.1 christos ret->sizeof_reloc = sizeof (Elf32_External_Rel);
809 1.1 christos ret->got_entry_size = 4;
810 1.1.1.2 christos ret->pcrel_plt = FALSE;
811 1.1 christos ret->pointer_r_type = R_386_32;
812 1.1 christos ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
813 1.1 christos ret->dynamic_interpreter_size
814 1.1 christos = sizeof ELF32_DYNAMIC_INTERPRETER;
815 1.1 christos ret->tls_get_addr = "___tls_get_addr";
816 1.1 christos }
817 1.1 christos }
818 1.1 christos ret->target_id = bed->target_id;
819 1.1 christos ret->target_os = get_elf_x86_backend_data (abfd)->target_os;
820 1.1 christos
821 1.1 christos ret->loc_hash_table = htab_try_create (1024,
822 1.1 christos _bfd_x86_elf_local_htab_hash,
823 1.1 christos _bfd_x86_elf_local_htab_eq,
824 1.1 christos NULL);
825 1.1 christos ret->loc_hash_memory = objalloc_create ();
826 1.1 christos if (!ret->loc_hash_table || !ret->loc_hash_memory)
827 1.1 christos {
828 1.1 christos elf_x86_link_hash_table_free (abfd);
829 1.1 christos return NULL;
830 1.1 christos }
831 1.1 christos ret->elf.root.hash_table_free = elf_x86_link_hash_table_free;
832 1.1 christos
833 1.1 christos return &ret->elf.root;
834 1.1 christos }
835 1.1 christos
836 1.1 christos /* Sort relocs into address order. */
837 1.1 christos
838 1.1 christos int
839 1.1 christos _bfd_x86_elf_compare_relocs (const void *ap, const void *bp)
840 1.1 christos {
841 1.1 christos const arelent *a = * (const arelent **) ap;
842 1.1 christos const arelent *b = * (const arelent **) bp;
843 1.1 christos
844 1.1 christos if (a->address > b->address)
845 1.1 christos return 1;
846 1.1 christos else if (a->address < b->address)
847 1.1 christos return -1;
848 1.1 christos else
849 1.1 christos return 0;
850 1.1 christos }
851 1.1 christos
852 1.1.1.2 christos /* Mark symbol, NAME, as locally defined by linker if it is referenced
853 1.1.1.2 christos and not defined in a relocatable object file. */
854 1.1.1.2 christos
855 1.1.1.2 christos static void
856 1.1.1.2 christos elf_x86_linker_defined (struct bfd_link_info *info, const char *name)
857 1.1.1.2 christos {
858 1.1.1.2 christos struct elf_link_hash_entry *h;
859 1.1.1.2 christos
860 1.1.1.2 christos h = elf_link_hash_lookup (elf_hash_table (info), name,
861 1.1.1.2 christos FALSE, FALSE, FALSE);
862 1.1.1.2 christos if (h == NULL)
863 1.1.1.2 christos return;
864 1.1.1.2 christos
865 1.1.1.2 christos while (h->root.type == bfd_link_hash_indirect)
866 1.1.1.2 christos h = (struct elf_link_hash_entry *) h->root.u.i.link;
867 1.1.1.2 christos
868 1.1.1.2 christos if (h->root.type == bfd_link_hash_new
869 1.1.1.2 christos || h->root.type == bfd_link_hash_undefined
870 1.1.1.2 christos || h->root.type == bfd_link_hash_undefweak
871 1.1.1.2 christos || h->root.type == bfd_link_hash_common
872 1.1.1.2 christos || (!h->def_regular && h->def_dynamic))
873 1.1.1.2 christos {
874 1.1.1.2 christos elf_x86_hash_entry (h)->local_ref = 2;
875 1.1.1.2 christos elf_x86_hash_entry (h)->linker_def = 1;
876 1.1.1.2 christos }
877 1.1.1.2 christos }
878 1.1.1.2 christos
879 1.1.1.2 christos /* Hide a linker-defined symbol, NAME, with hidden visibility. */
880 1.1.1.2 christos
881 1.1.1.2 christos static void
882 1.1.1.2 christos elf_x86_hide_linker_defined (struct bfd_link_info *info,
883 1.1.1.2 christos const char *name)
884 1.1.1.2 christos {
885 1.1.1.2 christos struct elf_link_hash_entry *h;
886 1.1.1.2 christos
887 1.1.1.2 christos h = elf_link_hash_lookup (elf_hash_table (info), name,
888 1.1.1.2 christos FALSE, FALSE, FALSE);
889 1.1.1.2 christos if (h == NULL)
890 1.1.1.2 christos return;
891 1.1.1.2 christos
892 1.1.1.2 christos while (h->root.type == bfd_link_hash_indirect)
893 1.1.1.2 christos h = (struct elf_link_hash_entry *) h->root.u.i.link;
894 1.1.1.2 christos
895 1.1.1.2 christos if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
896 1.1.1.2 christos || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
897 1.1.1.2 christos _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
898 1.1.1.2 christos }
899 1.1.1.2 christos
900 1.1 christos bfd_boolean
901 1.1 christos _bfd_x86_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
902 1.1 christos {
903 1.1 christos if (!bfd_link_relocatable (info))
904 1.1 christos {
905 1.1 christos /* Check for __tls_get_addr reference. */
906 1.1 christos struct elf_x86_link_hash_table *htab;
907 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
908 1.1 christos htab = elf_x86_hash_table (info, bed->target_id);
909 1.1 christos if (htab)
910 1.1 christos {
911 1.1 christos struct elf_link_hash_entry *h;
912 1.1 christos
913 1.1 christos h = elf_link_hash_lookup (elf_hash_table (info),
914 1.1 christos htab->tls_get_addr,
915 1.1 christos FALSE, FALSE, FALSE);
916 1.1 christos if (h != NULL)
917 1.1 christos {
918 1.1 christos elf_x86_hash_entry (h)->tls_get_addr = 1;
919 1.1 christos
920 1.1 christos /* Check the versioned __tls_get_addr symbol. */
921 1.1 christos while (h->root.type == bfd_link_hash_indirect)
922 1.1 christos {
923 1.1 christos h = (struct elf_link_hash_entry *) h->root.u.i.link;
924 1.1 christos elf_x86_hash_entry (h)->tls_get_addr = 1;
925 1.1 christos }
926 1.1 christos }
927 1.1 christos
928 1.1 christos /* "__ehdr_start" will be defined by linker as a hidden symbol
929 1.1 christos later if it is referenced and not defined. */
930 1.1.1.2 christos elf_x86_linker_defined (info, "__ehdr_start");
931 1.1.1.2 christos
932 1.1.1.2 christos if (bfd_link_executable (info))
933 1.1 christos {
934 1.1.1.2 christos /* References to __bss_start, _end and _edata should be
935 1.1.1.2 christos locally resolved within executables. */
936 1.1.1.2 christos elf_x86_linker_defined (info, "__bss_start");
937 1.1.1.2 christos elf_x86_linker_defined (info, "_end");
938 1.1.1.2 christos elf_x86_linker_defined (info, "_edata");
939 1.1.1.2 christos }
940 1.1.1.2 christos else
941 1.1.1.2 christos {
942 1.1.1.2 christos /* Hide hidden __bss_start, _end and _edata in shared
943 1.1.1.2 christos libraries. */
944 1.1.1.2 christos elf_x86_hide_linker_defined (info, "__bss_start");
945 1.1.1.2 christos elf_x86_hide_linker_defined (info, "_end");
946 1.1.1.2 christos elf_x86_hide_linker_defined (info, "_edata");
947 1.1 christos }
948 1.1 christos }
949 1.1 christos }
950 1.1 christos
951 1.1 christos /* Invoke the regular ELF backend linker to do all the work. */
952 1.1 christos return _bfd_elf_link_check_relocs (abfd, info);
953 1.1 christos }
954 1.1 christos
955 1.1 christos /* Set the sizes of the dynamic sections. */
956 1.1 christos
957 1.1 christos bfd_boolean
958 1.1 christos _bfd_x86_elf_size_dynamic_sections (bfd *output_bfd,
959 1.1 christos struct bfd_link_info *info)
960 1.1 christos {
961 1.1 christos struct elf_x86_link_hash_table *htab;
962 1.1 christos bfd *dynobj;
963 1.1 christos asection *s;
964 1.1 christos bfd_boolean relocs;
965 1.1 christos bfd *ibfd;
966 1.1 christos const struct elf_backend_data *bed
967 1.1 christos = get_elf_backend_data (output_bfd);
968 1.1 christos
969 1.1 christos htab = elf_x86_hash_table (info, bed->target_id);
970 1.1 christos if (htab == NULL)
971 1.1 christos return FALSE;
972 1.1 christos dynobj = htab->elf.dynobj;
973 1.1 christos if (dynobj == NULL)
974 1.1 christos abort ();
975 1.1 christos
976 1.1 christos /* Set up .got offsets for local syms, and space for local dynamic
977 1.1 christos relocs. */
978 1.1 christos for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
979 1.1 christos {
980 1.1 christos bfd_signed_vma *local_got;
981 1.1 christos bfd_signed_vma *end_local_got;
982 1.1 christos char *local_tls_type;
983 1.1 christos bfd_vma *local_tlsdesc_gotent;
984 1.1 christos bfd_size_type locsymcount;
985 1.1 christos Elf_Internal_Shdr *symtab_hdr;
986 1.1 christos asection *srel;
987 1.1 christos
988 1.1 christos if (! is_x86_elf (ibfd, htab))
989 1.1 christos continue;
990 1.1 christos
991 1.1 christos for (s = ibfd->sections; s != NULL; s = s->next)
992 1.1 christos {
993 1.1 christos struct elf_dyn_relocs *p;
994 1.1 christos
995 1.1 christos for (p = ((struct elf_dyn_relocs *)
996 1.1 christos elf_section_data (s)->local_dynrel);
997 1.1 christos p != NULL;
998 1.1 christos p = p->next)
999 1.1 christos {
1000 1.1 christos if (!bfd_is_abs_section (p->sec)
1001 1.1 christos && bfd_is_abs_section (p->sec->output_section))
1002 1.1 christos {
1003 1.1 christos /* Input section has been discarded, either because
1004 1.1 christos it is a copy of a linkonce section or due to
1005 1.1 christos linker script /DISCARD/, so we'll be discarding
1006 1.1 christos the relocs too. */
1007 1.1 christos }
1008 1.1 christos else if (htab->target_os == is_vxworks
1009 1.1 christos && strcmp (p->sec->output_section->name,
1010 1.1 christos ".tls_vars") == 0)
1011 1.1 christos {
1012 1.1 christos /* Relocations in vxworks .tls_vars sections are
1013 1.1 christos handled specially by the loader. */
1014 1.1 christos }
1015 1.1 christos else if (p->count != 0)
1016 1.1 christos {
1017 1.1 christos srel = elf_section_data (p->sec)->sreloc;
1018 1.1 christos srel->size += p->count * htab->sizeof_reloc;
1019 1.1 christos if ((p->sec->output_section->flags & SEC_READONLY) != 0
1020 1.1 christos && (info->flags & DF_TEXTREL) == 0)
1021 1.1 christos {
1022 1.1 christos info->flags |= DF_TEXTREL;
1023 1.1 christos if ((info->warn_shared_textrel && bfd_link_pic (info))
1024 1.1 christos || info->error_textrel)
1025 1.1 christos /* xgettext:c-format */
1026 1.1 christos info->callbacks->einfo
1027 1.1.1.2 christos (_("%P: %pB: warning: relocation "
1028 1.1.1.2 christos "in read-only section `%pA'\n"),
1029 1.1 christos p->sec->owner, p->sec);
1030 1.1 christos }
1031 1.1 christos }
1032 1.1 christos }
1033 1.1 christos }
1034 1.1 christos
1035 1.1 christos local_got = elf_local_got_refcounts (ibfd);
1036 1.1 christos if (!local_got)
1037 1.1 christos continue;
1038 1.1 christos
1039 1.1 christos symtab_hdr = &elf_symtab_hdr (ibfd);
1040 1.1 christos locsymcount = symtab_hdr->sh_info;
1041 1.1 christos end_local_got = local_got + locsymcount;
1042 1.1 christos local_tls_type = elf_x86_local_got_tls_type (ibfd);
1043 1.1 christos local_tlsdesc_gotent = elf_x86_local_tlsdesc_gotent (ibfd);
1044 1.1 christos s = htab->elf.sgot;
1045 1.1 christos srel = htab->elf.srelgot;
1046 1.1 christos for (; local_got < end_local_got;
1047 1.1 christos ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
1048 1.1 christos {
1049 1.1 christos *local_tlsdesc_gotent = (bfd_vma) -1;
1050 1.1 christos if (*local_got > 0)
1051 1.1 christos {
1052 1.1 christos if (GOT_TLS_GDESC_P (*local_tls_type))
1053 1.1 christos {
1054 1.1 christos *local_tlsdesc_gotent = htab->elf.sgotplt->size
1055 1.1 christos - elf_x86_compute_jump_table_size (htab);
1056 1.1 christos htab->elf.sgotplt->size += 2 * htab->got_entry_size;
1057 1.1 christos *local_got = (bfd_vma) -2;
1058 1.1 christos }
1059 1.1 christos if (! GOT_TLS_GDESC_P (*local_tls_type)
1060 1.1 christos || GOT_TLS_GD_P (*local_tls_type))
1061 1.1 christos {
1062 1.1 christos *local_got = s->size;
1063 1.1 christos s->size += htab->got_entry_size;
1064 1.1 christos if (GOT_TLS_GD_P (*local_tls_type)
1065 1.1 christos || *local_tls_type == GOT_TLS_IE_BOTH)
1066 1.1 christos s->size += htab->got_entry_size;
1067 1.1 christos }
1068 1.1 christos if (bfd_link_pic (info)
1069 1.1 christos || GOT_TLS_GD_ANY_P (*local_tls_type)
1070 1.1 christos || (*local_tls_type & GOT_TLS_IE))
1071 1.1 christos {
1072 1.1 christos if (*local_tls_type == GOT_TLS_IE_BOTH)
1073 1.1 christos srel->size += 2 * htab->sizeof_reloc;
1074 1.1 christos else if (GOT_TLS_GD_P (*local_tls_type)
1075 1.1 christos || ! GOT_TLS_GDESC_P (*local_tls_type))
1076 1.1 christos srel->size += htab->sizeof_reloc;
1077 1.1 christos if (GOT_TLS_GDESC_P (*local_tls_type))
1078 1.1 christos {
1079 1.1 christos htab->elf.srelplt->size += htab->sizeof_reloc;
1080 1.1 christos if (bed->target_id == X86_64_ELF_DATA)
1081 1.1 christos htab->tlsdesc_plt = (bfd_vma) -1;
1082 1.1 christos }
1083 1.1 christos }
1084 1.1 christos }
1085 1.1 christos else
1086 1.1 christos *local_got = (bfd_vma) -1;
1087 1.1 christos }
1088 1.1 christos }
1089 1.1 christos
1090 1.1 christos if (htab->tls_ld_or_ldm_got.refcount > 0)
1091 1.1 christos {
1092 1.1 christos /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
1093 1.1 christos or R_X86_64_TLSLD relocs. */
1094 1.1 christos htab->tls_ld_or_ldm_got.offset = htab->elf.sgot->size;
1095 1.1 christos htab->elf.sgot->size += 2 * htab->got_entry_size;
1096 1.1 christos htab->elf.srelgot->size += htab->sizeof_reloc;
1097 1.1 christos }
1098 1.1 christos else
1099 1.1 christos htab->tls_ld_or_ldm_got.offset = -1;
1100 1.1 christos
1101 1.1 christos /* Allocate global sym .plt and .got entries, and space for global
1102 1.1 christos sym dynamic relocs. */
1103 1.1 christos elf_link_hash_traverse (&htab->elf, elf_x86_allocate_dynrelocs,
1104 1.1 christos info);
1105 1.1 christos
1106 1.1 christos /* Allocate .plt and .got entries, and space for local symbols. */
1107 1.1 christos htab_traverse (htab->loc_hash_table, elf_x86_allocate_local_dynreloc,
1108 1.1 christos info);
1109 1.1 christos
1110 1.1 christos /* For every jump slot reserved in the sgotplt, reloc_count is
1111 1.1 christos incremented. However, when we reserve space for TLS descriptors,
1112 1.1 christos it's not incremented, so in order to compute the space reserved
1113 1.1 christos for them, it suffices to multiply the reloc count by the jump
1114 1.1 christos slot size.
1115 1.1 christos
1116 1.1 christos PR ld/13302: We start next_irelative_index at the end of .rela.plt
1117 1.1 christos so that R_{386,X86_64}_IRELATIVE entries come last. */
1118 1.1 christos if (htab->elf.srelplt)
1119 1.1 christos {
1120 1.1 christos htab->next_tls_desc_index = htab->elf.srelplt->reloc_count;
1121 1.1 christos htab->sgotplt_jump_table_size
1122 1.1 christos = elf_x86_compute_jump_table_size (htab);
1123 1.1 christos htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
1124 1.1 christos }
1125 1.1 christos else if (htab->elf.irelplt)
1126 1.1 christos htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
1127 1.1 christos
1128 1.1 christos if (htab->tlsdesc_plt)
1129 1.1 christos {
1130 1.1 christos /* NB: tlsdesc_plt is set only for x86-64. If we're not using
1131 1.1 christos lazy TLS relocations, don't generate the PLT and GOT entries
1132 1.1 christos they require. */
1133 1.1 christos if ((info->flags & DF_BIND_NOW))
1134 1.1 christos htab->tlsdesc_plt = 0;
1135 1.1 christos else
1136 1.1 christos {
1137 1.1 christos htab->tlsdesc_got = htab->elf.sgot->size;
1138 1.1 christos htab->elf.sgot->size += htab->got_entry_size;
1139 1.1 christos /* Reserve room for the initial entry.
1140 1.1 christos FIXME: we could probably do away with it in this case. */
1141 1.1 christos if (htab->elf.splt->size == 0)
1142 1.1 christos htab->elf.splt->size = htab->plt.plt_entry_size;
1143 1.1 christos htab->tlsdesc_plt = htab->elf.splt->size;
1144 1.1 christos htab->elf.splt->size += htab->plt.plt_entry_size;
1145 1.1 christos }
1146 1.1 christos }
1147 1.1 christos
1148 1.1 christos if (htab->elf.sgotplt)
1149 1.1 christos {
1150 1.1 christos /* Don't allocate .got.plt section if there are no GOT nor PLT
1151 1.1 christos entries and there is no reference to _GLOBAL_OFFSET_TABLE_. */
1152 1.1 christos if ((htab->elf.hgot == NULL
1153 1.1.1.2 christos || !htab->got_referenced)
1154 1.1 christos && (htab->elf.sgotplt->size == bed->got_header_size)
1155 1.1 christos && (htab->elf.splt == NULL
1156 1.1 christos || htab->elf.splt->size == 0)
1157 1.1 christos && (htab->elf.sgot == NULL
1158 1.1 christos || htab->elf.sgot->size == 0)
1159 1.1 christos && (htab->elf.iplt == NULL
1160 1.1 christos || htab->elf.iplt->size == 0)
1161 1.1 christos && (htab->elf.igotplt == NULL
1162 1.1 christos || htab->elf.igotplt->size == 0))
1163 1.1.1.2 christos {
1164 1.1.1.2 christos htab->elf.sgotplt->size = 0;
1165 1.1.1.2 christos /* Solaris requires to keep _GLOBAL_OFFSET_TABLE_ even if it
1166 1.1.1.2 christos isn't used. */
1167 1.1.1.2 christos if (htab->elf.hgot != NULL && htab->target_os != is_solaris)
1168 1.1.1.2 christos {
1169 1.1.1.2 christos /* Remove the unused _GLOBAL_OFFSET_TABLE_ from symbol
1170 1.1.1.2 christos table. */
1171 1.1.1.2 christos htab->elf.hgot->root.type = bfd_link_hash_undefined;
1172 1.1.1.2 christos htab->elf.hgot->root.u.undef.abfd
1173 1.1.1.2 christos = htab->elf.hgot->root.u.def.section->owner;
1174 1.1.1.2 christos htab->elf.hgot->root.linker_def = 0;
1175 1.1.1.2 christos htab->elf.hgot->ref_regular = 0;
1176 1.1.1.2 christos htab->elf.hgot->def_regular = 0;
1177 1.1.1.2 christos }
1178 1.1.1.2 christos }
1179 1.1 christos }
1180 1.1 christos
1181 1.1 christos if (_bfd_elf_eh_frame_present (info))
1182 1.1 christos {
1183 1.1 christos if (htab->plt_eh_frame != NULL
1184 1.1 christos && htab->elf.splt != NULL
1185 1.1 christos && htab->elf.splt->size != 0
1186 1.1 christos && !bfd_is_abs_section (htab->elf.splt->output_section))
1187 1.1 christos htab->plt_eh_frame->size = htab->plt.eh_frame_plt_size;
1188 1.1 christos
1189 1.1 christos if (htab->plt_got_eh_frame != NULL
1190 1.1 christos && htab->plt_got != NULL
1191 1.1 christos && htab->plt_got->size != 0
1192 1.1 christos && !bfd_is_abs_section (htab->plt_got->output_section))
1193 1.1 christos htab->plt_got_eh_frame->size
1194 1.1 christos = htab->non_lazy_plt->eh_frame_plt_size;
1195 1.1 christos
1196 1.1 christos /* Unwind info for the second PLT and .plt.got sections are
1197 1.1 christos identical. */
1198 1.1 christos if (htab->plt_second_eh_frame != NULL
1199 1.1 christos && htab->plt_second != NULL
1200 1.1 christos && htab->plt_second->size != 0
1201 1.1 christos && !bfd_is_abs_section (htab->plt_second->output_section))
1202 1.1 christos htab->plt_second_eh_frame->size
1203 1.1 christos = htab->non_lazy_plt->eh_frame_plt_size;
1204 1.1 christos }
1205 1.1 christos
1206 1.1 christos /* We now have determined the sizes of the various dynamic sections.
1207 1.1 christos Allocate memory for them. */
1208 1.1 christos relocs = FALSE;
1209 1.1 christos for (s = dynobj->sections; s != NULL; s = s->next)
1210 1.1 christos {
1211 1.1 christos bfd_boolean strip_section = TRUE;
1212 1.1 christos
1213 1.1 christos if ((s->flags & SEC_LINKER_CREATED) == 0)
1214 1.1 christos continue;
1215 1.1 christos
1216 1.1 christos if (s == htab->elf.splt
1217 1.1 christos || s == htab->elf.sgot)
1218 1.1 christos {
1219 1.1 christos /* Strip this section if we don't need it; see the
1220 1.1 christos comment below. */
1221 1.1 christos /* We'd like to strip these sections if they aren't needed, but if
1222 1.1 christos we've exported dynamic symbols from them we must leave them.
1223 1.1 christos It's too late to tell BFD to get rid of the symbols. */
1224 1.1 christos
1225 1.1 christos if (htab->elf.hplt != NULL)
1226 1.1 christos strip_section = FALSE;
1227 1.1 christos }
1228 1.1 christos else if (s == htab->elf.sgotplt
1229 1.1 christos || s == htab->elf.iplt
1230 1.1 christos || s == htab->elf.igotplt
1231 1.1 christos || s == htab->plt_second
1232 1.1 christos || s == htab->plt_got
1233 1.1 christos || s == htab->plt_eh_frame
1234 1.1 christos || s == htab->plt_got_eh_frame
1235 1.1 christos || s == htab->plt_second_eh_frame
1236 1.1 christos || s == htab->elf.sdynbss
1237 1.1 christos || s == htab->elf.sdynrelro)
1238 1.1 christos {
1239 1.1 christos /* Strip these too. */
1240 1.1 christos }
1241 1.1.1.3 christos else if (htab->is_reloc_section (bfd_section_name (s)))
1242 1.1 christos {
1243 1.1 christos if (s->size != 0
1244 1.1 christos && s != htab->elf.srelplt
1245 1.1 christos && s != htab->srelplt2)
1246 1.1 christos relocs = TRUE;
1247 1.1 christos
1248 1.1 christos /* We use the reloc_count field as a counter if we need
1249 1.1 christos to copy relocs into the output file. */
1250 1.1 christos if (s != htab->elf.srelplt)
1251 1.1 christos s->reloc_count = 0;
1252 1.1 christos }
1253 1.1 christos else
1254 1.1 christos {
1255 1.1 christos /* It's not one of our sections, so don't allocate space. */
1256 1.1 christos continue;
1257 1.1 christos }
1258 1.1 christos
1259 1.1 christos if (s->size == 0)
1260 1.1 christos {
1261 1.1 christos /* If we don't need this section, strip it from the
1262 1.1 christos output file. This is mostly to handle .rel.bss and
1263 1.1 christos .rel.plt. We must create both sections in
1264 1.1 christos create_dynamic_sections, because they must be created
1265 1.1 christos before the linker maps input sections to output
1266 1.1 christos sections. The linker does that before
1267 1.1 christos adjust_dynamic_symbol is called, and it is that
1268 1.1 christos function which decides whether anything needs to go
1269 1.1 christos into these sections. */
1270 1.1 christos if (strip_section)
1271 1.1 christos s->flags |= SEC_EXCLUDE;
1272 1.1 christos continue;
1273 1.1 christos }
1274 1.1 christos
1275 1.1 christos if ((s->flags & SEC_HAS_CONTENTS) == 0)
1276 1.1 christos continue;
1277 1.1 christos
1278 1.1.1.3 christos /* NB: Initially, the iplt section has minimal alignment to
1279 1.1.1.3 christos avoid moving dot of the following section backwards when
1280 1.1.1.3 christos it is empty. Update its section alignment now since it
1281 1.1.1.3 christos is non-empty. */
1282 1.1.1.3 christos if (s == htab->elf.iplt)
1283 1.1.1.3 christos bfd_set_section_alignment (s, htab->plt.iplt_alignment);
1284 1.1.1.3 christos
1285 1.1 christos /* Allocate memory for the section contents. We use bfd_zalloc
1286 1.1 christos here in case unused entries are not reclaimed before the
1287 1.1 christos section's contents are written out. This should not happen,
1288 1.1 christos but this way if it does, we get a R_386_NONE or R_X86_64_NONE
1289 1.1 christos reloc instead of garbage. */
1290 1.1 christos s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
1291 1.1 christos if (s->contents == NULL)
1292 1.1 christos return FALSE;
1293 1.1 christos }
1294 1.1 christos
1295 1.1 christos if (htab->plt_eh_frame != NULL
1296 1.1 christos && htab->plt_eh_frame->contents != NULL)
1297 1.1 christos {
1298 1.1 christos memcpy (htab->plt_eh_frame->contents,
1299 1.1 christos htab->plt.eh_frame_plt,
1300 1.1 christos htab->plt_eh_frame->size);
1301 1.1 christos bfd_put_32 (dynobj, htab->elf.splt->size,
1302 1.1 christos htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
1303 1.1 christos }
1304 1.1 christos
1305 1.1 christos if (htab->plt_got_eh_frame != NULL
1306 1.1 christos && htab->plt_got_eh_frame->contents != NULL)
1307 1.1 christos {
1308 1.1 christos memcpy (htab->plt_got_eh_frame->contents,
1309 1.1 christos htab->non_lazy_plt->eh_frame_plt,
1310 1.1 christos htab->plt_got_eh_frame->size);
1311 1.1 christos bfd_put_32 (dynobj, htab->plt_got->size,
1312 1.1 christos (htab->plt_got_eh_frame->contents
1313 1.1 christos + PLT_FDE_LEN_OFFSET));
1314 1.1 christos }
1315 1.1 christos
1316 1.1 christos if (htab->plt_second_eh_frame != NULL
1317 1.1 christos && htab->plt_second_eh_frame->contents != NULL)
1318 1.1 christos {
1319 1.1 christos memcpy (htab->plt_second_eh_frame->contents,
1320 1.1 christos htab->non_lazy_plt->eh_frame_plt,
1321 1.1 christos htab->plt_second_eh_frame->size);
1322 1.1 christos bfd_put_32 (dynobj, htab->plt_second->size,
1323 1.1 christos (htab->plt_second_eh_frame->contents
1324 1.1 christos + PLT_FDE_LEN_OFFSET));
1325 1.1 christos }
1326 1.1 christos
1327 1.1 christos if (htab->elf.dynamic_sections_created)
1328 1.1 christos {
1329 1.1 christos /* Add some entries to the .dynamic section. We fill in the
1330 1.1 christos values later, in elf_{i386,x86_64}_finish_dynamic_sections,
1331 1.1 christos but we must add the entries now so that we get the correct
1332 1.1 christos size for the .dynamic section. The DT_DEBUG entry is filled
1333 1.1 christos in by the dynamic linker and used by the debugger. */
1334 1.1 christos #define add_dynamic_entry(TAG, VAL) \
1335 1.1 christos _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1336 1.1 christos
1337 1.1 christos if (bfd_link_executable (info))
1338 1.1 christos {
1339 1.1 christos if (!add_dynamic_entry (DT_DEBUG, 0))
1340 1.1 christos return FALSE;
1341 1.1 christos }
1342 1.1 christos
1343 1.1 christos if (htab->elf.splt->size != 0)
1344 1.1 christos {
1345 1.1 christos /* DT_PLTGOT is used by prelink even if there is no PLT
1346 1.1 christos relocation. */
1347 1.1 christos if (!add_dynamic_entry (DT_PLTGOT, 0))
1348 1.1 christos return FALSE;
1349 1.1 christos }
1350 1.1 christos
1351 1.1 christos if (htab->elf.srelplt->size != 0)
1352 1.1 christos {
1353 1.1 christos if (!add_dynamic_entry (DT_PLTRELSZ, 0)
1354 1.1 christos || !add_dynamic_entry (DT_PLTREL, htab->dt_reloc)
1355 1.1 christos || !add_dynamic_entry (DT_JMPREL, 0))
1356 1.1 christos return FALSE;
1357 1.1 christos }
1358 1.1 christos
1359 1.1 christos if (htab->tlsdesc_plt
1360 1.1 christos && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
1361 1.1 christos || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
1362 1.1 christos return FALSE;
1363 1.1 christos
1364 1.1 christos if (relocs)
1365 1.1 christos {
1366 1.1 christos if (!add_dynamic_entry (htab->dt_reloc, 0)
1367 1.1 christos || !add_dynamic_entry (htab->dt_reloc_sz, 0)
1368 1.1 christos || !add_dynamic_entry (htab->dt_reloc_ent,
1369 1.1 christos htab->sizeof_reloc))
1370 1.1 christos return FALSE;
1371 1.1 christos
1372 1.1 christos /* If any dynamic relocs apply to a read-only section,
1373 1.1 christos then we need a DT_TEXTREL entry. */
1374 1.1 christos if ((info->flags & DF_TEXTREL) == 0)
1375 1.1 christos elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
1376 1.1 christos
1377 1.1 christos if ((info->flags & DF_TEXTREL) != 0)
1378 1.1 christos {
1379 1.1 christos if (htab->readonly_dynrelocs_against_ifunc)
1380 1.1 christos {
1381 1.1 christos info->callbacks->einfo
1382 1.1 christos (_("%P%X: read-only segment has dynamic IFUNC relocations;"
1383 1.1.1.3 christos " recompile with %s\n"),
1384 1.1.1.3 christos bfd_link_dll (info) ? "-fPIC" : "-fPIE");
1385 1.1 christos bfd_set_error (bfd_error_bad_value);
1386 1.1 christos return FALSE;
1387 1.1 christos }
1388 1.1 christos
1389 1.1 christos if (!add_dynamic_entry (DT_TEXTREL, 0))
1390 1.1 christos return FALSE;
1391 1.1 christos }
1392 1.1 christos }
1393 1.1 christos if (htab->target_os == is_vxworks
1394 1.1 christos && !elf_vxworks_add_dynamic_entries (output_bfd, info))
1395 1.1 christos return FALSE;
1396 1.1 christos }
1397 1.1 christos #undef add_dynamic_entry
1398 1.1 christos
1399 1.1 christos return TRUE;
1400 1.1 christos }
1401 1.1 christos
1402 1.1 christos /* Finish up the x86 dynamic sections. */
1403 1.1 christos
1404 1.1 christos struct elf_x86_link_hash_table *
1405 1.1 christos _bfd_x86_elf_finish_dynamic_sections (bfd *output_bfd,
1406 1.1 christos struct bfd_link_info *info)
1407 1.1 christos {
1408 1.1 christos struct elf_x86_link_hash_table *htab;
1409 1.1 christos const struct elf_backend_data *bed;
1410 1.1 christos bfd *dynobj;
1411 1.1 christos asection *sdyn;
1412 1.1 christos bfd_byte *dyncon, *dynconend;
1413 1.1 christos bfd_size_type sizeof_dyn;
1414 1.1 christos
1415 1.1 christos bed = get_elf_backend_data (output_bfd);
1416 1.1 christos htab = elf_x86_hash_table (info, bed->target_id);
1417 1.1 christos if (htab == NULL)
1418 1.1 christos return htab;
1419 1.1 christos
1420 1.1 christos dynobj = htab->elf.dynobj;
1421 1.1 christos sdyn = bfd_get_linker_section (dynobj, ".dynamic");
1422 1.1 christos
1423 1.1 christos /* GOT is always created in setup_gnu_properties. But it may not be
1424 1.1 christos needed. .got.plt section may be needed for static IFUNC. */
1425 1.1 christos if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
1426 1.1 christos {
1427 1.1 christos bfd_vma dynamic_addr;
1428 1.1 christos
1429 1.1 christos if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
1430 1.1 christos {
1431 1.1 christos _bfd_error_handler
1432 1.1.1.2 christos (_("discarded output section: `%pA'"), htab->elf.sgotplt);
1433 1.1 christos return NULL;
1434 1.1 christos }
1435 1.1 christos
1436 1.1 christos elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize
1437 1.1 christos = htab->got_entry_size;
1438 1.1 christos
1439 1.1 christos dynamic_addr = (sdyn == NULL
1440 1.1 christos ? (bfd_vma) 0
1441 1.1 christos : sdyn->output_section->vma + sdyn->output_offset);
1442 1.1 christos
1443 1.1 christos /* Set the first entry in the global offset table to the address
1444 1.1 christos of the dynamic section. Write GOT[1] and GOT[2], needed for
1445 1.1 christos the dynamic linker. */
1446 1.1 christos if (htab->got_entry_size == 8)
1447 1.1 christos {
1448 1.1 christos bfd_put_64 (output_bfd, dynamic_addr,
1449 1.1 christos htab->elf.sgotplt->contents);
1450 1.1 christos bfd_put_64 (output_bfd, (bfd_vma) 0,
1451 1.1 christos htab->elf.sgotplt->contents + 8);
1452 1.1 christos bfd_put_64 (output_bfd, (bfd_vma) 0,
1453 1.1 christos htab->elf.sgotplt->contents + 8*2);
1454 1.1 christos }
1455 1.1 christos else
1456 1.1 christos {
1457 1.1 christos bfd_put_32 (output_bfd, dynamic_addr,
1458 1.1 christos htab->elf.sgotplt->contents);
1459 1.1 christos bfd_put_32 (output_bfd, 0,
1460 1.1 christos htab->elf.sgotplt->contents + 4);
1461 1.1 christos bfd_put_32 (output_bfd, 0,
1462 1.1 christos htab->elf.sgotplt->contents + 4*2);
1463 1.1 christos }
1464 1.1 christos }
1465 1.1 christos
1466 1.1 christos if (!htab->elf.dynamic_sections_created)
1467 1.1 christos return htab;
1468 1.1 christos
1469 1.1 christos if (sdyn == NULL || htab->elf.sgot == NULL)
1470 1.1 christos abort ();
1471 1.1 christos
1472 1.1 christos sizeof_dyn = bed->s->sizeof_dyn;
1473 1.1 christos dyncon = sdyn->contents;
1474 1.1 christos dynconend = sdyn->contents + sdyn->size;
1475 1.1 christos for (; dyncon < dynconend; dyncon += sizeof_dyn)
1476 1.1 christos {
1477 1.1 christos Elf_Internal_Dyn dyn;
1478 1.1 christos asection *s;
1479 1.1 christos
1480 1.1 christos (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
1481 1.1 christos
1482 1.1 christos switch (dyn.d_tag)
1483 1.1 christos {
1484 1.1 christos default:
1485 1.1 christos if (htab->target_os == is_vxworks
1486 1.1 christos && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
1487 1.1 christos break;
1488 1.1 christos continue;
1489 1.1 christos
1490 1.1 christos case DT_PLTGOT:
1491 1.1 christos s = htab->elf.sgotplt;
1492 1.1 christos dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
1493 1.1 christos break;
1494 1.1 christos
1495 1.1 christos case DT_JMPREL:
1496 1.1 christos dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
1497 1.1 christos break;
1498 1.1 christos
1499 1.1 christos case DT_PLTRELSZ:
1500 1.1 christos s = htab->elf.srelplt->output_section;
1501 1.1 christos dyn.d_un.d_val = s->size;
1502 1.1 christos break;
1503 1.1 christos
1504 1.1 christos case DT_TLSDESC_PLT:
1505 1.1 christos s = htab->elf.splt;
1506 1.1 christos dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
1507 1.1 christos + htab->tlsdesc_plt;
1508 1.1 christos break;
1509 1.1 christos
1510 1.1 christos case DT_TLSDESC_GOT:
1511 1.1 christos s = htab->elf.sgot;
1512 1.1 christos dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
1513 1.1 christos + htab->tlsdesc_got;
1514 1.1 christos break;
1515 1.1 christos }
1516 1.1 christos
1517 1.1 christos (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
1518 1.1 christos }
1519 1.1 christos
1520 1.1 christos if (htab->plt_got != NULL && htab->plt_got->size > 0)
1521 1.1 christos elf_section_data (htab->plt_got->output_section)
1522 1.1 christos ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
1523 1.1 christos
1524 1.1 christos if (htab->plt_second != NULL && htab->plt_second->size > 0)
1525 1.1 christos elf_section_data (htab->plt_second->output_section)
1526 1.1 christos ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
1527 1.1 christos
1528 1.1 christos /* Adjust .eh_frame for .plt section. */
1529 1.1 christos if (htab->plt_eh_frame != NULL
1530 1.1 christos && htab->plt_eh_frame->contents != NULL)
1531 1.1 christos {
1532 1.1 christos if (htab->elf.splt != NULL
1533 1.1 christos && htab->elf.splt->size != 0
1534 1.1 christos && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
1535 1.1 christos && htab->elf.splt->output_section != NULL
1536 1.1 christos && htab->plt_eh_frame->output_section != NULL)
1537 1.1 christos {
1538 1.1 christos bfd_vma plt_start = htab->elf.splt->output_section->vma;
1539 1.1 christos bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
1540 1.1 christos + htab->plt_eh_frame->output_offset
1541 1.1 christos + PLT_FDE_START_OFFSET;
1542 1.1 christos bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
1543 1.1 christos htab->plt_eh_frame->contents
1544 1.1 christos + PLT_FDE_START_OFFSET);
1545 1.1 christos }
1546 1.1 christos
1547 1.1 christos if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
1548 1.1 christos {
1549 1.1 christos if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
1550 1.1 christos htab->plt_eh_frame,
1551 1.1 christos htab->plt_eh_frame->contents))
1552 1.1 christos return NULL;
1553 1.1 christos }
1554 1.1 christos }
1555 1.1 christos
1556 1.1 christos /* Adjust .eh_frame for .plt.got section. */
1557 1.1 christos if (htab->plt_got_eh_frame != NULL
1558 1.1 christos && htab->plt_got_eh_frame->contents != NULL)
1559 1.1 christos {
1560 1.1 christos if (htab->plt_got != NULL
1561 1.1 christos && htab->plt_got->size != 0
1562 1.1 christos && (htab->plt_got->flags & SEC_EXCLUDE) == 0
1563 1.1 christos && htab->plt_got->output_section != NULL
1564 1.1 christos && htab->plt_got_eh_frame->output_section != NULL)
1565 1.1 christos {
1566 1.1 christos bfd_vma plt_start = htab->plt_got->output_section->vma;
1567 1.1 christos bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
1568 1.1 christos + htab->plt_got_eh_frame->output_offset
1569 1.1 christos + PLT_FDE_START_OFFSET;
1570 1.1 christos bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
1571 1.1 christos htab->plt_got_eh_frame->contents
1572 1.1 christos + PLT_FDE_START_OFFSET);
1573 1.1 christos }
1574 1.1 christos if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
1575 1.1 christos {
1576 1.1 christos if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
1577 1.1 christos htab->plt_got_eh_frame,
1578 1.1 christos htab->plt_got_eh_frame->contents))
1579 1.1 christos return NULL;
1580 1.1 christos }
1581 1.1 christos }
1582 1.1 christos
1583 1.1 christos /* Adjust .eh_frame for the second PLT section. */
1584 1.1 christos if (htab->plt_second_eh_frame != NULL
1585 1.1 christos && htab->plt_second_eh_frame->contents != NULL)
1586 1.1 christos {
1587 1.1 christos if (htab->plt_second != NULL
1588 1.1 christos && htab->plt_second->size != 0
1589 1.1 christos && (htab->plt_second->flags & SEC_EXCLUDE) == 0
1590 1.1 christos && htab->plt_second->output_section != NULL
1591 1.1 christos && htab->plt_second_eh_frame->output_section != NULL)
1592 1.1 christos {
1593 1.1 christos bfd_vma plt_start = htab->plt_second->output_section->vma;
1594 1.1 christos bfd_vma eh_frame_start
1595 1.1 christos = (htab->plt_second_eh_frame->output_section->vma
1596 1.1 christos + htab->plt_second_eh_frame->output_offset
1597 1.1 christos + PLT_FDE_START_OFFSET);
1598 1.1 christos bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
1599 1.1 christos htab->plt_second_eh_frame->contents
1600 1.1 christos + PLT_FDE_START_OFFSET);
1601 1.1 christos }
1602 1.1 christos if (htab->plt_second_eh_frame->sec_info_type
1603 1.1 christos == SEC_INFO_TYPE_EH_FRAME)
1604 1.1 christos {
1605 1.1 christos if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
1606 1.1 christos htab->plt_second_eh_frame,
1607 1.1 christos htab->plt_second_eh_frame->contents))
1608 1.1 christos return NULL;
1609 1.1 christos }
1610 1.1 christos }
1611 1.1 christos
1612 1.1 christos if (htab->elf.sgot && htab->elf.sgot->size > 0)
1613 1.1 christos elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
1614 1.1 christos = htab->got_entry_size;
1615 1.1 christos
1616 1.1 christos return htab;
1617 1.1 christos }
1618 1.1 christos
1619 1.1 christos
1620 1.1 christos bfd_boolean
1621 1.1 christos _bfd_x86_elf_always_size_sections (bfd *output_bfd,
1622 1.1 christos struct bfd_link_info *info)
1623 1.1 christos {
1624 1.1 christos asection *tls_sec = elf_hash_table (info)->tls_sec;
1625 1.1 christos
1626 1.1 christos if (tls_sec)
1627 1.1 christos {
1628 1.1 christos struct elf_link_hash_entry *tlsbase;
1629 1.1 christos
1630 1.1 christos tlsbase = elf_link_hash_lookup (elf_hash_table (info),
1631 1.1 christos "_TLS_MODULE_BASE_",
1632 1.1 christos FALSE, FALSE, FALSE);
1633 1.1 christos
1634 1.1 christos if (tlsbase && tlsbase->type == STT_TLS)
1635 1.1 christos {
1636 1.1 christos struct elf_x86_link_hash_table *htab;
1637 1.1 christos struct bfd_link_hash_entry *bh = NULL;
1638 1.1 christos const struct elf_backend_data *bed
1639 1.1 christos = get_elf_backend_data (output_bfd);
1640 1.1 christos
1641 1.1 christos htab = elf_x86_hash_table (info, bed->target_id);
1642 1.1 christos if (htab == NULL)
1643 1.1 christos return FALSE;
1644 1.1 christos
1645 1.1 christos if (!(_bfd_generic_link_add_one_symbol
1646 1.1 christos (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
1647 1.1 christos tls_sec, 0, NULL, FALSE,
1648 1.1 christos bed->collect, &bh)))
1649 1.1 christos return FALSE;
1650 1.1 christos
1651 1.1 christos htab->tls_module_base = bh;
1652 1.1 christos
1653 1.1 christos tlsbase = (struct elf_link_hash_entry *)bh;
1654 1.1 christos tlsbase->def_regular = 1;
1655 1.1 christos tlsbase->other = STV_HIDDEN;
1656 1.1 christos tlsbase->root.linker_def = 1;
1657 1.1 christos (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
1658 1.1 christos }
1659 1.1 christos }
1660 1.1 christos
1661 1.1 christos return TRUE;
1662 1.1 christos }
1663 1.1 christos
1664 1.1 christos void
1665 1.1 christos _bfd_x86_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
1666 1.1 christos const Elf_Internal_Sym *isym,
1667 1.1 christos bfd_boolean definition,
1668 1.1 christos bfd_boolean dynamic ATTRIBUTE_UNUSED)
1669 1.1 christos {
1670 1.1 christos if (definition)
1671 1.1 christos {
1672 1.1 christos struct elf_x86_link_hash_entry *eh
1673 1.1 christos = (struct elf_x86_link_hash_entry *) h;
1674 1.1 christos eh->def_protected = (ELF_ST_VISIBILITY (isym->st_other)
1675 1.1 christos == STV_PROTECTED);
1676 1.1 christos }
1677 1.1 christos }
1678 1.1 christos
1679 1.1 christos /* Copy the extra info we tack onto an elf_link_hash_entry. */
1680 1.1 christos
1681 1.1 christos void
1682 1.1 christos _bfd_x86_elf_copy_indirect_symbol (struct bfd_link_info *info,
1683 1.1 christos struct elf_link_hash_entry *dir,
1684 1.1 christos struct elf_link_hash_entry *ind)
1685 1.1 christos {
1686 1.1 christos struct elf_x86_link_hash_entry *edir, *eind;
1687 1.1 christos
1688 1.1 christos edir = (struct elf_x86_link_hash_entry *) dir;
1689 1.1 christos eind = (struct elf_x86_link_hash_entry *) ind;
1690 1.1 christos
1691 1.1 christos if (eind->dyn_relocs != NULL)
1692 1.1 christos {
1693 1.1 christos if (edir->dyn_relocs != NULL)
1694 1.1 christos {
1695 1.1 christos struct elf_dyn_relocs **pp;
1696 1.1 christos struct elf_dyn_relocs *p;
1697 1.1 christos
1698 1.1 christos /* Add reloc counts against the indirect sym to the direct sym
1699 1.1 christos list. Merge any entries against the same section. */
1700 1.1 christos for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1701 1.1 christos {
1702 1.1 christos struct elf_dyn_relocs *q;
1703 1.1 christos
1704 1.1 christos for (q = edir->dyn_relocs; q != NULL; q = q->next)
1705 1.1 christos if (q->sec == p->sec)
1706 1.1 christos {
1707 1.1 christos q->pc_count += p->pc_count;
1708 1.1 christos q->count += p->count;
1709 1.1 christos *pp = p->next;
1710 1.1 christos break;
1711 1.1 christos }
1712 1.1 christos if (q == NULL)
1713 1.1 christos pp = &p->next;
1714 1.1 christos }
1715 1.1 christos *pp = edir->dyn_relocs;
1716 1.1 christos }
1717 1.1 christos
1718 1.1 christos edir->dyn_relocs = eind->dyn_relocs;
1719 1.1 christos eind->dyn_relocs = NULL;
1720 1.1 christos }
1721 1.1 christos
1722 1.1 christos if (ind->root.type == bfd_link_hash_indirect
1723 1.1 christos && dir->got.refcount <= 0)
1724 1.1 christos {
1725 1.1 christos edir->tls_type = eind->tls_type;
1726 1.1 christos eind->tls_type = GOT_UNKNOWN;
1727 1.1 christos }
1728 1.1 christos
1729 1.1 christos /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will
1730 1.1 christos generate a R_386_COPY reloc. */
1731 1.1 christos edir->gotoff_ref |= eind->gotoff_ref;
1732 1.1 christos
1733 1.1 christos edir->zero_undefweak |= eind->zero_undefweak;
1734 1.1 christos
1735 1.1 christos if (ELIMINATE_COPY_RELOCS
1736 1.1 christos && ind->root.type != bfd_link_hash_indirect
1737 1.1 christos && dir->dynamic_adjusted)
1738 1.1 christos {
1739 1.1 christos /* If called to transfer flags for a weakdef during processing
1740 1.1 christos of elf_adjust_dynamic_symbol, don't copy non_got_ref.
1741 1.1 christos We clear it ourselves for ELIMINATE_COPY_RELOCS. */
1742 1.1 christos if (dir->versioned != versioned_hidden)
1743 1.1 christos dir->ref_dynamic |= ind->ref_dynamic;
1744 1.1 christos dir->ref_regular |= ind->ref_regular;
1745 1.1 christos dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
1746 1.1 christos dir->needs_plt |= ind->needs_plt;
1747 1.1 christos dir->pointer_equality_needed |= ind->pointer_equality_needed;
1748 1.1 christos }
1749 1.1 christos else
1750 1.1 christos _bfd_elf_link_hash_copy_indirect (info, dir, ind);
1751 1.1 christos }
1752 1.1 christos
1753 1.1 christos /* Remove undefined weak symbol from the dynamic symbol table if it
1754 1.1 christos is resolved to 0. */
1755 1.1 christos
1756 1.1 christos bfd_boolean
1757 1.1 christos _bfd_x86_elf_fixup_symbol (struct bfd_link_info *info,
1758 1.1 christos struct elf_link_hash_entry *h)
1759 1.1 christos {
1760 1.1 christos if (h->dynindx != -1
1761 1.1 christos && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, elf_x86_hash_entry (h)))
1762 1.1 christos {
1763 1.1 christos h->dynindx = -1;
1764 1.1 christos _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1765 1.1 christos h->dynstr_index);
1766 1.1 christos }
1767 1.1 christos return TRUE;
1768 1.1 christos }
1769 1.1 christos
1770 1.1.1.2 christos /* Change the STT_GNU_IFUNC symbol defined in position-dependent
1771 1.1.1.2 christos executable into the normal function symbol and set its address
1772 1.1.1.2 christos to its PLT entry, which should be resolved by R_*_IRELATIVE at
1773 1.1.1.2 christos run-time. */
1774 1.1.1.2 christos
1775 1.1.1.2 christos void
1776 1.1.1.2 christos _bfd_x86_elf_link_fixup_ifunc_symbol (struct bfd_link_info *info,
1777 1.1.1.2 christos struct elf_x86_link_hash_table *htab,
1778 1.1.1.2 christos struct elf_link_hash_entry *h,
1779 1.1.1.2 christos Elf_Internal_Sym *sym)
1780 1.1.1.2 christos {
1781 1.1.1.2 christos if (bfd_link_pde (info)
1782 1.1.1.2 christos && h->def_regular
1783 1.1.1.2 christos && h->dynindx != -1
1784 1.1.1.2 christos && h->plt.offset != (bfd_vma) -1
1785 1.1.1.2 christos && h->type == STT_GNU_IFUNC
1786 1.1.1.2 christos && h->pointer_equality_needed)
1787 1.1.1.2 christos {
1788 1.1.1.2 christos asection *plt_s;
1789 1.1.1.2 christos bfd_vma plt_offset;
1790 1.1.1.2 christos bfd *output_bfd = info->output_bfd;
1791 1.1.1.2 christos
1792 1.1.1.2 christos if (htab->plt_second)
1793 1.1.1.2 christos {
1794 1.1.1.2 christos struct elf_x86_link_hash_entry *eh
1795 1.1.1.2 christos = (struct elf_x86_link_hash_entry *) h;
1796 1.1.1.2 christos
1797 1.1.1.2 christos plt_s = htab->plt_second;
1798 1.1.1.2 christos plt_offset = eh->plt_second.offset;
1799 1.1.1.2 christos }
1800 1.1.1.2 christos else
1801 1.1.1.2 christos {
1802 1.1.1.2 christos plt_s = htab->elf.splt;
1803 1.1.1.2 christos plt_offset = h->plt.offset;
1804 1.1.1.2 christos }
1805 1.1.1.2 christos
1806 1.1.1.2 christos sym->st_size = 0;
1807 1.1.1.2 christos sym->st_info = ELF_ST_INFO (ELF_ST_BIND (sym->st_info), STT_FUNC);
1808 1.1.1.2 christos sym->st_shndx
1809 1.1.1.2 christos = _bfd_elf_section_from_bfd_section (output_bfd,
1810 1.1.1.2 christos plt_s->output_section);
1811 1.1.1.2 christos sym->st_value = (plt_s->output_section->vma
1812 1.1.1.2 christos + plt_s->output_offset + plt_offset);
1813 1.1.1.2 christos }
1814 1.1.1.2 christos }
1815 1.1.1.2 christos
1816 1.1 christos /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
1817 1.1 christos
1818 1.1 christos bfd_boolean
1819 1.1 christos _bfd_x86_elf_hash_symbol (struct elf_link_hash_entry *h)
1820 1.1 christos {
1821 1.1 christos if (h->plt.offset != (bfd_vma) -1
1822 1.1 christos && !h->def_regular
1823 1.1 christos && !h->pointer_equality_needed)
1824 1.1 christos return FALSE;
1825 1.1 christos
1826 1.1 christos return _bfd_elf_hash_symbol (h);
1827 1.1 christos }
1828 1.1 christos
1829 1.1 christos /* Adjust a symbol defined by a dynamic object and referenced by a
1830 1.1 christos regular object. The current definition is in some section of the
1831 1.1 christos dynamic object, but we're not including those sections. We have to
1832 1.1 christos change the definition to something the rest of the link can
1833 1.1 christos understand. */
1834 1.1 christos
1835 1.1 christos bfd_boolean
1836 1.1 christos _bfd_x86_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
1837 1.1 christos struct elf_link_hash_entry *h)
1838 1.1 christos {
1839 1.1 christos struct elf_x86_link_hash_table *htab;
1840 1.1 christos asection *s, *srel;
1841 1.1 christos struct elf_x86_link_hash_entry *eh;
1842 1.1 christos struct elf_dyn_relocs *p;
1843 1.1 christos const struct elf_backend_data *bed
1844 1.1 christos = get_elf_backend_data (info->output_bfd);
1845 1.1 christos
1846 1.1 christos /* STT_GNU_IFUNC symbol must go through PLT. */
1847 1.1 christos if (h->type == STT_GNU_IFUNC)
1848 1.1 christos {
1849 1.1 christos /* All local STT_GNU_IFUNC references must be treate as local
1850 1.1 christos calls via local PLT. */
1851 1.1 christos if (h->ref_regular
1852 1.1 christos && SYMBOL_CALLS_LOCAL (info, h))
1853 1.1 christos {
1854 1.1 christos bfd_size_type pc_count = 0, count = 0;
1855 1.1 christos struct elf_dyn_relocs **pp;
1856 1.1 christos
1857 1.1 christos eh = (struct elf_x86_link_hash_entry *) h;
1858 1.1 christos for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1859 1.1 christos {
1860 1.1 christos pc_count += p->pc_count;
1861 1.1 christos p->count -= p->pc_count;
1862 1.1 christos p->pc_count = 0;
1863 1.1 christos count += p->count;
1864 1.1 christos if (p->count == 0)
1865 1.1 christos *pp = p->next;
1866 1.1 christos else
1867 1.1 christos pp = &p->next;
1868 1.1 christos }
1869 1.1 christos
1870 1.1 christos if (pc_count || count)
1871 1.1 christos {
1872 1.1 christos h->non_got_ref = 1;
1873 1.1 christos if (pc_count)
1874 1.1 christos {
1875 1.1 christos /* Increment PLT reference count only for PC-relative
1876 1.1 christos references. */
1877 1.1 christos h->needs_plt = 1;
1878 1.1 christos if (h->plt.refcount <= 0)
1879 1.1 christos h->plt.refcount = 1;
1880 1.1 christos else
1881 1.1 christos h->plt.refcount += 1;
1882 1.1 christos }
1883 1.1 christos }
1884 1.1 christos }
1885 1.1 christos
1886 1.1 christos if (h->plt.refcount <= 0)
1887 1.1 christos {
1888 1.1 christos h->plt.offset = (bfd_vma) -1;
1889 1.1 christos h->needs_plt = 0;
1890 1.1 christos }
1891 1.1 christos return TRUE;
1892 1.1 christos }
1893 1.1 christos
1894 1.1 christos /* If this is a function, put it in the procedure linkage table. We
1895 1.1 christos will fill in the contents of the procedure linkage table later,
1896 1.1 christos when we know the address of the .got section. */
1897 1.1 christos if (h->type == STT_FUNC
1898 1.1 christos || h->needs_plt)
1899 1.1 christos {
1900 1.1 christos if (h->plt.refcount <= 0
1901 1.1 christos || SYMBOL_CALLS_LOCAL (info, h)
1902 1.1 christos || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1903 1.1 christos && h->root.type == bfd_link_hash_undefweak))
1904 1.1 christos {
1905 1.1 christos /* This case can occur if we saw a PLT32 reloc in an input
1906 1.1 christos file, but the symbol was never referred to by a dynamic
1907 1.1 christos object, or if all references were garbage collected. In
1908 1.1 christos such a case, we don't actually need to build a procedure
1909 1.1 christos linkage table, and we can just do a PC32 reloc instead. */
1910 1.1 christos h->plt.offset = (bfd_vma) -1;
1911 1.1 christos h->needs_plt = 0;
1912 1.1 christos }
1913 1.1 christos
1914 1.1 christos return TRUE;
1915 1.1 christos }
1916 1.1 christos else
1917 1.1 christos /* It's possible that we incorrectly decided a .plt reloc was needed
1918 1.1 christos * for an R_386_PC32/R_X86_64_PC32 reloc to a non-function sym in
1919 1.1 christos check_relocs. We can't decide accurately between function and
1920 1.1 christos non-function syms in check-relocs; Objects loaded later in
1921 1.1 christos the link may change h->type. So fix it now. */
1922 1.1 christos h->plt.offset = (bfd_vma) -1;
1923 1.1 christos
1924 1.1 christos eh = (struct elf_x86_link_hash_entry *) h;
1925 1.1 christos
1926 1.1 christos /* If this is a weak symbol, and there is a real definition, the
1927 1.1 christos processor independent code will have arranged for us to see the
1928 1.1 christos real definition first, and we can just use the same value. */
1929 1.1 christos if (h->is_weakalias)
1930 1.1 christos {
1931 1.1 christos struct elf_link_hash_entry *def = weakdef (h);
1932 1.1 christos BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1933 1.1 christos h->root.u.def.section = def->root.u.def.section;
1934 1.1 christos h->root.u.def.value = def->root.u.def.value;
1935 1.1 christos if (ELIMINATE_COPY_RELOCS
1936 1.1 christos || info->nocopyreloc
1937 1.1 christos || SYMBOL_NO_COPYRELOC (info, eh))
1938 1.1 christos {
1939 1.1 christos /* NB: needs_copy is always 0 for i386. */
1940 1.1 christos h->non_got_ref = def->non_got_ref;
1941 1.1 christos eh->needs_copy = def->needs_copy;
1942 1.1 christos }
1943 1.1 christos return TRUE;
1944 1.1 christos }
1945 1.1 christos
1946 1.1 christos /* This is a reference to a symbol defined by a dynamic object which
1947 1.1 christos is not a function. */
1948 1.1 christos
1949 1.1 christos /* If we are creating a shared library, we must presume that the
1950 1.1 christos only references to the symbol are via the global offset table.
1951 1.1 christos For such cases we need not do anything here; the relocations will
1952 1.1 christos be handled correctly by relocate_section. */
1953 1.1 christos if (!bfd_link_executable (info))
1954 1.1 christos return TRUE;
1955 1.1 christos
1956 1.1 christos /* If there are no references to this symbol that do not use the
1957 1.1 christos GOT nor R_386_GOTOFF relocation, we don't need to generate a copy
1958 1.1 christos reloc. NB: gotoff_ref is always 0 for x86-64. */
1959 1.1 christos if (!h->non_got_ref && !eh->gotoff_ref)
1960 1.1 christos return TRUE;
1961 1.1 christos
1962 1.1 christos /* If -z nocopyreloc was given, we won't generate them either. */
1963 1.1 christos if (info->nocopyreloc || SYMBOL_NO_COPYRELOC (info, eh))
1964 1.1 christos {
1965 1.1 christos h->non_got_ref = 0;
1966 1.1 christos return TRUE;
1967 1.1 christos }
1968 1.1 christos
1969 1.1 christos htab = elf_x86_hash_table (info, bed->target_id);
1970 1.1 christos if (htab == NULL)
1971 1.1 christos return FALSE;
1972 1.1 christos
1973 1.1 christos /* If there aren't any dynamic relocs in read-only sections nor
1974 1.1 christos R_386_GOTOFF relocation, then we can keep the dynamic relocs and
1975 1.1 christos avoid the copy reloc. This doesn't work on VxWorks, where we can
1976 1.1 christos not have dynamic relocations (other than copy and jump slot
1977 1.1 christos relocations) in an executable. */
1978 1.1 christos if (ELIMINATE_COPY_RELOCS
1979 1.1 christos && (bed->target_id == X86_64_ELF_DATA
1980 1.1 christos || (!eh->gotoff_ref
1981 1.1 christos && htab->target_os != is_vxworks)))
1982 1.1 christos {
1983 1.1 christos /* If we don't find any dynamic relocs in read-only sections,
1984 1.1 christos then we'll be keeping the dynamic relocs and avoiding the copy
1985 1.1 christos reloc. */
1986 1.1 christos if (!readonly_dynrelocs (h))
1987 1.1 christos {
1988 1.1 christos h->non_got_ref = 0;
1989 1.1 christos return TRUE;
1990 1.1 christos }
1991 1.1 christos }
1992 1.1 christos
1993 1.1 christos /* We must allocate the symbol in our .dynbss section, which will
1994 1.1 christos become part of the .bss section of the executable. There will be
1995 1.1 christos an entry for this symbol in the .dynsym section. The dynamic
1996 1.1 christos object will contain position independent code, so all references
1997 1.1 christos from the dynamic object to this symbol will go through the global
1998 1.1 christos offset table. The dynamic linker will use the .dynsym entry to
1999 1.1 christos determine the address it must put in the global offset table, so
2000 1.1 christos both the dynamic object and the regular object will refer to the
2001 1.1 christos same memory location for the variable. */
2002 1.1 christos
2003 1.1 christos /* We must generate a R_386_COPY/R_X86_64_COPY reloc to tell the
2004 1.1 christos dynamic linker to copy the initial value out of the dynamic object
2005 1.1 christos and into the runtime process image. */
2006 1.1 christos if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
2007 1.1 christos {
2008 1.1 christos s = htab->elf.sdynrelro;
2009 1.1 christos srel = htab->elf.sreldynrelro;
2010 1.1 christos }
2011 1.1 christos else
2012 1.1 christos {
2013 1.1 christos s = htab->elf.sdynbss;
2014 1.1 christos srel = htab->elf.srelbss;
2015 1.1 christos }
2016 1.1 christos if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2017 1.1 christos {
2018 1.1 christos srel->size += htab->sizeof_reloc;
2019 1.1 christos h->needs_copy = 1;
2020 1.1 christos }
2021 1.1 christos
2022 1.1 christos return _bfd_elf_adjust_dynamic_copy (info, h, s);
2023 1.1 christos }
2024 1.1 christos
2025 1.1 christos void
2026 1.1 christos _bfd_x86_elf_hide_symbol (struct bfd_link_info *info,
2027 1.1 christos struct elf_link_hash_entry *h,
2028 1.1 christos bfd_boolean force_local)
2029 1.1 christos {
2030 1.1 christos if (h->root.type == bfd_link_hash_undefweak
2031 1.1 christos && info->nointerp
2032 1.1 christos && bfd_link_pie (info))
2033 1.1 christos {
2034 1.1 christos /* When there is no dynamic interpreter in PIE, make the undefined
2035 1.1 christos weak symbol dynamic so that PC relative branch to the undefined
2036 1.1 christos weak symbol will land to address 0. */
2037 1.1 christos struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
2038 1.1 christos if (h->plt.refcount > 0
2039 1.1 christos || eh->plt_got.refcount > 0)
2040 1.1 christos return;
2041 1.1 christos }
2042 1.1 christos
2043 1.1 christos _bfd_elf_link_hash_hide_symbol (info, h, force_local);
2044 1.1 christos }
2045 1.1 christos
2046 1.1 christos /* Return TRUE if a symbol is referenced locally. It is similar to
2047 1.1 christos SYMBOL_REFERENCES_LOCAL, but it also checks version script. It
2048 1.1 christos works in check_relocs. */
2049 1.1 christos
2050 1.1 christos bfd_boolean
2051 1.1 christos _bfd_x86_elf_link_symbol_references_local (struct bfd_link_info *info,
2052 1.1 christos struct elf_link_hash_entry *h)
2053 1.1 christos {
2054 1.1 christos struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
2055 1.1 christos struct elf_x86_link_hash_table *htab
2056 1.1 christos = (struct elf_x86_link_hash_table *) info->hash;
2057 1.1 christos
2058 1.1 christos if (eh->local_ref > 1)
2059 1.1 christos return TRUE;
2060 1.1 christos
2061 1.1 christos if (eh->local_ref == 1)
2062 1.1 christos return FALSE;
2063 1.1 christos
2064 1.1 christos /* Unversioned symbols defined in regular objects can be forced local
2065 1.1 christos by linker version script. A weak undefined symbol is forced local
2066 1.1 christos if
2067 1.1 christos 1. It has non-default visibility. Or
2068 1.1 christos 2. When building executable, there is no dynamic linker. Or
2069 1.1 christos 3. or "-z nodynamic-undefined-weak" is used.
2070 1.1 christos */
2071 1.1 christos if (SYMBOL_REFERENCES_LOCAL (info, h)
2072 1.1 christos || (h->root.type == bfd_link_hash_undefweak
2073 1.1 christos && (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2074 1.1 christos || (bfd_link_executable (info)
2075 1.1 christos && htab->interp == NULL)
2076 1.1 christos || info->dynamic_undefined_weak == 0))
2077 1.1 christos || ((h->def_regular || ELF_COMMON_DEF_P (h))
2078 1.1 christos && info->version_info != NULL
2079 1.1.1.2 christos && _bfd_elf_link_hide_sym_by_version (info, h)))
2080 1.1 christos {
2081 1.1 christos eh->local_ref = 2;
2082 1.1 christos return TRUE;
2083 1.1 christos }
2084 1.1 christos
2085 1.1 christos eh->local_ref = 1;
2086 1.1 christos return FALSE;
2087 1.1 christos }
2088 1.1 christos
2089 1.1 christos /* Return the section that should be marked against GC for a given
2090 1.1 christos relocation. */
2091 1.1 christos
2092 1.1 christos asection *
2093 1.1 christos _bfd_x86_elf_gc_mark_hook (asection *sec,
2094 1.1 christos struct bfd_link_info *info,
2095 1.1 christos Elf_Internal_Rela *rel,
2096 1.1 christos struct elf_link_hash_entry *h,
2097 1.1 christos Elf_Internal_Sym *sym)
2098 1.1 christos {
2099 1.1 christos /* Compiler should optimize this out. */
2100 1.1 christos if (((unsigned int) R_X86_64_GNU_VTINHERIT
2101 1.1 christos != (unsigned int) R_386_GNU_VTINHERIT)
2102 1.1 christos || ((unsigned int) R_X86_64_GNU_VTENTRY
2103 1.1 christos != (unsigned int) R_386_GNU_VTENTRY))
2104 1.1 christos abort ();
2105 1.1 christos
2106 1.1 christos if (h != NULL)
2107 1.1 christos switch (ELF32_R_TYPE (rel->r_info))
2108 1.1 christos {
2109 1.1 christos case R_X86_64_GNU_VTINHERIT:
2110 1.1 christos case R_X86_64_GNU_VTENTRY:
2111 1.1 christos return NULL;
2112 1.1 christos }
2113 1.1 christos
2114 1.1 christos return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2115 1.1 christos }
2116 1.1 christos
2117 1.1 christos static bfd_vma
2118 1.1 christos elf_i386_get_plt_got_vma (struct elf_x86_plt *plt_p ATTRIBUTE_UNUSED,
2119 1.1 christos bfd_vma off,
2120 1.1 christos bfd_vma offset ATTRIBUTE_UNUSED,
2121 1.1 christos bfd_vma got_addr)
2122 1.1 christos {
2123 1.1 christos return got_addr + off;
2124 1.1 christos }
2125 1.1 christos
2126 1.1 christos static bfd_vma
2127 1.1 christos elf_x86_64_get_plt_got_vma (struct elf_x86_plt *plt_p,
2128 1.1 christos bfd_vma off,
2129 1.1 christos bfd_vma offset,
2130 1.1 christos bfd_vma got_addr ATTRIBUTE_UNUSED)
2131 1.1 christos {
2132 1.1 christos return plt_p->sec->vma + offset + off + plt_p->plt_got_insn_size;
2133 1.1 christos }
2134 1.1 christos
2135 1.1 christos static bfd_boolean
2136 1.1 christos elf_i386_valid_plt_reloc_p (unsigned int type)
2137 1.1 christos {
2138 1.1 christos return (type == R_386_JUMP_SLOT
2139 1.1 christos || type == R_386_GLOB_DAT
2140 1.1 christos || type == R_386_IRELATIVE);
2141 1.1 christos }
2142 1.1 christos
2143 1.1 christos static bfd_boolean
2144 1.1 christos elf_x86_64_valid_plt_reloc_p (unsigned int type)
2145 1.1 christos {
2146 1.1 christos return (type == R_X86_64_JUMP_SLOT
2147 1.1 christos || type == R_X86_64_GLOB_DAT
2148 1.1 christos || type == R_X86_64_IRELATIVE);
2149 1.1 christos }
2150 1.1 christos
2151 1.1 christos long
2152 1.1 christos _bfd_x86_elf_get_synthetic_symtab (bfd *abfd,
2153 1.1 christos long count,
2154 1.1 christos long relsize,
2155 1.1 christos bfd_vma got_addr,
2156 1.1 christos struct elf_x86_plt plts[],
2157 1.1 christos asymbol **dynsyms,
2158 1.1 christos asymbol **ret)
2159 1.1 christos {
2160 1.1 christos long size, i, n, len;
2161 1.1 christos int j;
2162 1.1 christos unsigned int plt_got_offset, plt_entry_size;
2163 1.1 christos asymbol *s;
2164 1.1 christos bfd_byte *plt_contents;
2165 1.1 christos long dynrelcount;
2166 1.1 christos arelent **dynrelbuf, *p;
2167 1.1 christos char *names;
2168 1.1 christos const struct elf_backend_data *bed;
2169 1.1 christos bfd_vma (*get_plt_got_vma) (struct elf_x86_plt *, bfd_vma, bfd_vma,
2170 1.1 christos bfd_vma);
2171 1.1 christos bfd_boolean (*valid_plt_reloc_p) (unsigned int);
2172 1.1 christos
2173 1.1.1.3 christos dynrelbuf = NULL;
2174 1.1 christos if (count == 0)
2175 1.1.1.3 christos goto bad_return;
2176 1.1 christos
2177 1.1 christos dynrelbuf = (arelent **) bfd_malloc (relsize);
2178 1.1 christos if (dynrelbuf == NULL)
2179 1.1.1.3 christos goto bad_return;
2180 1.1 christos
2181 1.1 christos dynrelcount = bfd_canonicalize_dynamic_reloc (abfd, dynrelbuf,
2182 1.1 christos dynsyms);
2183 1.1 christos if (dynrelcount <= 0)
2184 1.1.1.3 christos goto bad_return;
2185 1.1 christos
2186 1.1 christos /* Sort the relocs by address. */
2187 1.1 christos qsort (dynrelbuf, dynrelcount, sizeof (arelent *),
2188 1.1 christos _bfd_x86_elf_compare_relocs);
2189 1.1 christos
2190 1.1 christos size = count * sizeof (asymbol);
2191 1.1 christos
2192 1.1 christos /* Allocate space for @plt suffixes. */
2193 1.1 christos n = 0;
2194 1.1 christos for (i = 0; i < dynrelcount; i++)
2195 1.1 christos {
2196 1.1 christos p = dynrelbuf[i];
2197 1.1 christos size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2198 1.1 christos if (p->addend != 0)
2199 1.1 christos size += sizeof ("+0x") - 1 + 8 + 8 * ABI_64_P (abfd);
2200 1.1 christos }
2201 1.1 christos
2202 1.1 christos s = *ret = (asymbol *) bfd_zmalloc (size);
2203 1.1 christos if (s == NULL)
2204 1.1 christos goto bad_return;
2205 1.1 christos
2206 1.1 christos bed = get_elf_backend_data (abfd);
2207 1.1 christos
2208 1.1 christos if (bed->target_id == X86_64_ELF_DATA)
2209 1.1 christos {
2210 1.1 christos get_plt_got_vma = elf_x86_64_get_plt_got_vma;
2211 1.1 christos valid_plt_reloc_p = elf_x86_64_valid_plt_reloc_p;
2212 1.1 christos }
2213 1.1 christos else
2214 1.1 christos {
2215 1.1 christos get_plt_got_vma = elf_i386_get_plt_got_vma;
2216 1.1 christos valid_plt_reloc_p = elf_i386_valid_plt_reloc_p;
2217 1.1 christos if (got_addr)
2218 1.1 christos {
2219 1.1 christos /* Check .got.plt and then .got to get the _GLOBAL_OFFSET_TABLE_
2220 1.1 christos address. */
2221 1.1 christos asection *sec = bfd_get_section_by_name (abfd, ".got.plt");
2222 1.1 christos if (sec != NULL)
2223 1.1 christos got_addr = sec->vma;
2224 1.1 christos else
2225 1.1 christos {
2226 1.1 christos sec = bfd_get_section_by_name (abfd, ".got");
2227 1.1 christos if (sec != NULL)
2228 1.1 christos got_addr = sec->vma;
2229 1.1 christos }
2230 1.1 christos
2231 1.1 christos if (got_addr == (bfd_vma) -1)
2232 1.1 christos goto bad_return;
2233 1.1 christos }
2234 1.1 christos }
2235 1.1 christos
2236 1.1 christos /* Check for each PLT section. */
2237 1.1 christos names = (char *) (s + count);
2238 1.1 christos size = 0;
2239 1.1 christos n = 0;
2240 1.1 christos for (j = 0; plts[j].name != NULL; j++)
2241 1.1 christos if ((plt_contents = plts[j].contents) != NULL)
2242 1.1 christos {
2243 1.1 christos long k;
2244 1.1 christos bfd_vma offset;
2245 1.1 christos asection *plt;
2246 1.1 christos struct elf_x86_plt *plt_p = &plts[j];
2247 1.1 christos
2248 1.1 christos plt_got_offset = plt_p->plt_got_offset;
2249 1.1 christos plt_entry_size = plt_p->plt_entry_size;
2250 1.1 christos
2251 1.1 christos plt = plt_p->sec;
2252 1.1 christos
2253 1.1 christos if ((plt_p->type & plt_lazy))
2254 1.1 christos {
2255 1.1 christos /* Skip PLT0 in lazy PLT. */
2256 1.1 christos k = 1;
2257 1.1 christos offset = plt_entry_size;
2258 1.1 christos }
2259 1.1 christos else
2260 1.1 christos {
2261 1.1 christos k = 0;
2262 1.1 christos offset = 0;
2263 1.1 christos }
2264 1.1 christos
2265 1.1 christos /* Check each PLT entry against dynamic relocations. */
2266 1.1 christos for (; k < plt_p->count; k++)
2267 1.1 christos {
2268 1.1 christos int off;
2269 1.1 christos bfd_vma got_vma;
2270 1.1 christos long min, max, mid;
2271 1.1 christos
2272 1.1 christos /* Get the GOT offset for i386 or the PC-relative offset
2273 1.1 christos for x86-64, a signed 32-bit integer. */
2274 1.1 christos off = H_GET_32 (abfd, (plt_contents + offset
2275 1.1 christos + plt_got_offset));
2276 1.1 christos got_vma = get_plt_got_vma (plt_p, off, offset, got_addr);
2277 1.1 christos
2278 1.1 christos /* Binary search. */
2279 1.1 christos p = dynrelbuf[0];
2280 1.1 christos min = 0;
2281 1.1 christos max = dynrelcount;
2282 1.1 christos while ((min + 1) < max)
2283 1.1 christos {
2284 1.1 christos arelent *r;
2285 1.1 christos
2286 1.1 christos mid = (min + max) / 2;
2287 1.1 christos r = dynrelbuf[mid];
2288 1.1 christos if (got_vma > r->address)
2289 1.1 christos min = mid;
2290 1.1 christos else if (got_vma < r->address)
2291 1.1 christos max = mid;
2292 1.1 christos else
2293 1.1 christos {
2294 1.1 christos p = r;
2295 1.1 christos break;
2296 1.1 christos }
2297 1.1 christos }
2298 1.1 christos
2299 1.1 christos /* Skip unknown relocation. PR 17512: file: bc9d6cf5. */
2300 1.1 christos if (got_vma == p->address
2301 1.1 christos && p->howto != NULL
2302 1.1 christos && valid_plt_reloc_p (p->howto->type))
2303 1.1 christos {
2304 1.1 christos *s = **p->sym_ptr_ptr;
2305 1.1 christos /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL
2306 1.1 christos set. Since we are defining a symbol, ensure one
2307 1.1 christos of them is set. */
2308 1.1 christos if ((s->flags & BSF_LOCAL) == 0)
2309 1.1 christos s->flags |= BSF_GLOBAL;
2310 1.1 christos s->flags |= BSF_SYNTHETIC;
2311 1.1 christos /* This is no longer a section symbol. */
2312 1.1 christos s->flags &= ~BSF_SECTION_SYM;
2313 1.1 christos s->section = plt;
2314 1.1 christos s->the_bfd = plt->owner;
2315 1.1 christos s->value = offset;
2316 1.1 christos s->udata.p = NULL;
2317 1.1 christos s->name = names;
2318 1.1 christos len = strlen ((*p->sym_ptr_ptr)->name);
2319 1.1 christos memcpy (names, (*p->sym_ptr_ptr)->name, len);
2320 1.1 christos names += len;
2321 1.1 christos if (p->addend != 0)
2322 1.1 christos {
2323 1.1 christos char buf[30], *a;
2324 1.1 christos
2325 1.1 christos memcpy (names, "+0x", sizeof ("+0x") - 1);
2326 1.1 christos names += sizeof ("+0x") - 1;
2327 1.1 christos bfd_sprintf_vma (abfd, buf, p->addend);
2328 1.1 christos for (a = buf; *a == '0'; ++a)
2329 1.1 christos ;
2330 1.1 christos size = strlen (a);
2331 1.1 christos memcpy (names, a, size);
2332 1.1 christos names += size;
2333 1.1 christos }
2334 1.1 christos memcpy (names, "@plt", sizeof ("@plt"));
2335 1.1 christos names += sizeof ("@plt");
2336 1.1 christos n++;
2337 1.1 christos s++;
2338 1.1 christos /* There should be only one entry in PLT for a given
2339 1.1 christos symbol. Set howto to NULL after processing a PLT
2340 1.1 christos entry to guard against corrupted PLT. */
2341 1.1 christos p->howto = NULL;
2342 1.1 christos }
2343 1.1 christos offset += plt_entry_size;
2344 1.1 christos }
2345 1.1 christos }
2346 1.1 christos
2347 1.1 christos /* PLT entries with R_386_TLS_DESC relocations are skipped. */
2348 1.1 christos if (n == 0)
2349 1.1 christos {
2350 1.1 christos bad_return:
2351 1.1 christos count = -1;
2352 1.1 christos }
2353 1.1 christos else
2354 1.1 christos count = n;
2355 1.1 christos
2356 1.1 christos for (j = 0; plts[j].name != NULL; j++)
2357 1.1 christos if (plts[j].contents != NULL)
2358 1.1 christos free (plts[j].contents);
2359 1.1 christos
2360 1.1 christos free (dynrelbuf);
2361 1.1 christos
2362 1.1 christos return count;
2363 1.1 christos }
2364 1.1 christos
2365 1.1 christos /* Parse x86 GNU properties. */
2366 1.1 christos
2367 1.1 christos enum elf_property_kind
2368 1.1 christos _bfd_x86_elf_parse_gnu_properties (bfd *abfd, unsigned int type,
2369 1.1 christos bfd_byte *ptr, unsigned int datasz)
2370 1.1 christos {
2371 1.1 christos elf_property *prop;
2372 1.1 christos
2373 1.1.1.3 christos if (type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
2374 1.1.1.3 christos || type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
2375 1.1.1.3 christos || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
2376 1.1.1.3 christos && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
2377 1.1.1.3 christos || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
2378 1.1.1.3 christos && type <= GNU_PROPERTY_X86_UINT32_OR_HI)
2379 1.1.1.3 christos || (type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
2380 1.1.1.3 christos && type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
2381 1.1 christos {
2382 1.1 christos if (datasz != 4)
2383 1.1 christos {
2384 1.1 christos _bfd_error_handler
2385 1.1.1.3 christos (_("error: %pB: <corrupt x86 property (0x%x) size: 0x%x>"),
2386 1.1.1.3 christos abfd, type, datasz);
2387 1.1 christos return property_corrupt;
2388 1.1 christos }
2389 1.1 christos prop = _bfd_elf_get_property (abfd, type, datasz);
2390 1.1 christos prop->u.number |= bfd_h_get_32 (abfd, ptr);
2391 1.1 christos prop->pr_kind = property_number;
2392 1.1.1.3 christos return property_number;
2393 1.1 christos }
2394 1.1 christos
2395 1.1.1.3 christos return property_ignored;
2396 1.1 christos }
2397 1.1 christos
2398 1.1 christos /* Merge x86 GNU property BPROP with APROP. If APROP isn't NULL,
2399 1.1 christos return TRUE if APROP is updated. Otherwise, return TRUE if BPROP
2400 1.1 christos should be merged with ABFD. */
2401 1.1 christos
2402 1.1 christos bfd_boolean
2403 1.1 christos _bfd_x86_elf_merge_gnu_properties (struct bfd_link_info *info,
2404 1.1 christos bfd *abfd ATTRIBUTE_UNUSED,
2405 1.1.1.3 christos bfd *bbfd ATTRIBUTE_UNUSED,
2406 1.1 christos elf_property *aprop,
2407 1.1 christos elf_property *bprop)
2408 1.1 christos {
2409 1.1 christos unsigned int number, features;
2410 1.1 christos bfd_boolean updated = FALSE;
2411 1.1 christos unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
2412 1.1 christos
2413 1.1.1.3 christos if (pr_type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
2414 1.1.1.3 christos || (pr_type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
2415 1.1.1.3 christos && pr_type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
2416 1.1.1.3 christos {
2417 1.1.1.3 christos if (aprop == NULL || bprop == NULL)
2418 1.1.1.3 christos {
2419 1.1.1.3 christos /* Only one of APROP and BPROP can be NULL. */
2420 1.1.1.3 christos if (aprop != NULL)
2421 1.1.1.3 christos {
2422 1.1.1.3 christos /* Remove this property since the other input file doesn't
2423 1.1.1.3 christos have it. */
2424 1.1.1.3 christos aprop->pr_kind = property_remove;
2425 1.1.1.3 christos updated = TRUE;
2426 1.1.1.3 christos }
2427 1.1.1.3 christos }
2428 1.1.1.3 christos else
2429 1.1.1.3 christos {
2430 1.1.1.3 christos number = aprop->u.number;
2431 1.1.1.3 christos aprop->u.number = number | bprop->u.number;
2432 1.1.1.3 christos updated = number != (unsigned int) aprop->u.number;
2433 1.1.1.3 christos }
2434 1.1.1.3 christos return updated;
2435 1.1.1.3 christos }
2436 1.1.1.3 christos else if (pr_type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
2437 1.1.1.3 christos || (pr_type >= GNU_PROPERTY_X86_UINT32_OR_LO
2438 1.1.1.3 christos && pr_type <= GNU_PROPERTY_X86_UINT32_OR_HI))
2439 1.1 christos {
2440 1.1 christos if (aprop != NULL && bprop != NULL)
2441 1.1 christos {
2442 1.1 christos number = aprop->u.number;
2443 1.1 christos aprop->u.number = number | bprop->u.number;
2444 1.1.1.3 christos /* Remove the property if all bits are empty. */
2445 1.1.1.2 christos if (aprop->u.number == 0)
2446 1.1.1.2 christos {
2447 1.1.1.2 christos aprop->pr_kind = property_remove;
2448 1.1.1.2 christos updated = TRUE;
2449 1.1.1.2 christos }
2450 1.1.1.2 christos else
2451 1.1.1.2 christos updated = number != (unsigned int) aprop->u.number;
2452 1.1 christos }
2453 1.1 christos else
2454 1.1 christos {
2455 1.1.1.2 christos /* Only one of APROP and BPROP can be NULL. */
2456 1.1.1.2 christos if (aprop != NULL)
2457 1.1.1.2 christos {
2458 1.1.1.2 christos if (aprop->u.number == 0)
2459 1.1.1.2 christos {
2460 1.1.1.3 christos /* Remove APROP if all bits are empty. */
2461 1.1.1.2 christos aprop->pr_kind = property_remove;
2462 1.1.1.2 christos updated = TRUE;
2463 1.1.1.2 christos }
2464 1.1.1.2 christos }
2465 1.1.1.2 christos else
2466 1.1.1.2 christos {
2467 1.1.1.3 christos /* Return TRUE if APROP is NULL and all bits of BPROP
2468 1.1.1.2 christos aren't empty to indicate that BPROP should be added
2469 1.1.1.2 christos to ABFD. */
2470 1.1.1.2 christos updated = bprop->u.number != 0;
2471 1.1.1.2 christos }
2472 1.1 christos }
2473 1.1.1.3 christos return updated;
2474 1.1.1.3 christos }
2475 1.1.1.3 christos else if (pr_type >= GNU_PROPERTY_X86_UINT32_AND_LO
2476 1.1.1.3 christos && pr_type <= GNU_PROPERTY_X86_UINT32_AND_HI)
2477 1.1.1.3 christos {
2478 1.1 christos /* Only one of APROP and BPROP can be NULL:
2479 1.1 christos 1. APROP & BPROP when both APROP and BPROP aren't NULL.
2480 1.1 christos 2. If APROP is NULL, remove x86 feature.
2481 1.1 christos 3. Otherwise, do nothing.
2482 1.1 christos */
2483 1.1.1.3 christos const struct elf_backend_data *bed
2484 1.1.1.3 christos = get_elf_backend_data (info->output_bfd);
2485 1.1.1.3 christos struct elf_x86_link_hash_table *htab
2486 1.1.1.3 christos = elf_x86_hash_table (info, bed->target_id);
2487 1.1.1.3 christos if (!htab)
2488 1.1.1.3 christos abort ();
2489 1.1 christos if (aprop != NULL && bprop != NULL)
2490 1.1 christos {
2491 1.1 christos features = 0;
2492 1.1.1.3 christos if (htab->params->ibt)
2493 1.1 christos features = GNU_PROPERTY_X86_FEATURE_1_IBT;
2494 1.1.1.3 christos if (htab->params->shstk)
2495 1.1 christos features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
2496 1.1 christos number = aprop->u.number;
2497 1.1 christos /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and
2498 1.1 christos GNU_PROPERTY_X86_FEATURE_1_SHSTK. */
2499 1.1 christos aprop->u.number = (number & bprop->u.number) | features;
2500 1.1 christos updated = number != (unsigned int) aprop->u.number;
2501 1.1 christos /* Remove the property if all feature bits are cleared. */
2502 1.1 christos if (aprop->u.number == 0)
2503 1.1 christos aprop->pr_kind = property_remove;
2504 1.1 christos }
2505 1.1 christos else
2506 1.1 christos {
2507 1.1.1.3 christos /* There should be no AND properties since some input doesn't
2508 1.1.1.3 christos have them. Set IBT and SHSTK properties for -z ibt and -z
2509 1.1.1.3 christos shstk if needed. */
2510 1.1 christos features = 0;
2511 1.1.1.3 christos if (htab->params->ibt)
2512 1.1 christos features = GNU_PROPERTY_X86_FEATURE_1_IBT;
2513 1.1.1.3 christos if (htab->params->shstk)
2514 1.1 christos features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
2515 1.1 christos if (features)
2516 1.1 christos {
2517 1.1 christos if (aprop != NULL)
2518 1.1 christos {
2519 1.1.1.3 christos updated = features != (unsigned int) aprop->u.number;
2520 1.1.1.3 christos aprop->u.number = features;
2521 1.1 christos }
2522 1.1 christos else
2523 1.1 christos {
2524 1.1 christos updated = TRUE;
2525 1.1.1.3 christos bprop->u.number = features;
2526 1.1 christos }
2527 1.1 christos }
2528 1.1 christos else if (aprop != NULL)
2529 1.1 christos {
2530 1.1 christos aprop->pr_kind = property_remove;
2531 1.1 christos updated = TRUE;
2532 1.1 christos }
2533 1.1 christos }
2534 1.1.1.3 christos return updated;
2535 1.1.1.3 christos }
2536 1.1.1.3 christos else
2537 1.1.1.3 christos {
2538 1.1 christos /* Never should happen. */
2539 1.1 christos abort ();
2540 1.1 christos }
2541 1.1 christos
2542 1.1 christos return updated;
2543 1.1 christos }
2544 1.1 christos
2545 1.1 christos /* Set up x86 GNU properties. Return the first relocatable ELF input
2546 1.1 christos with GNU properties if found. Otherwise, return NULL. */
2547 1.1 christos
2548 1.1 christos bfd *
2549 1.1 christos _bfd_x86_elf_link_setup_gnu_properties
2550 1.1 christos (struct bfd_link_info *info, struct elf_x86_init_table *init_table)
2551 1.1 christos {
2552 1.1 christos bfd_boolean normal_target;
2553 1.1 christos bfd_boolean lazy_plt;
2554 1.1 christos asection *sec, *pltsec;
2555 1.1 christos bfd *dynobj;
2556 1.1 christos bfd_boolean use_ibt_plt;
2557 1.1 christos unsigned int plt_alignment, features;
2558 1.1 christos struct elf_x86_link_hash_table *htab;
2559 1.1 christos bfd *pbfd;
2560 1.1 christos bfd *ebfd = NULL;
2561 1.1 christos elf_property *prop;
2562 1.1 christos const struct elf_backend_data *bed;
2563 1.1 christos unsigned int class_align = ABI_64_P (info->output_bfd) ? 3 : 2;
2564 1.1 christos unsigned int got_align;
2565 1.1 christos
2566 1.1 christos /* Find a normal input file with GNU property note. */
2567 1.1 christos for (pbfd = info->input_bfds;
2568 1.1 christos pbfd != NULL;
2569 1.1 christos pbfd = pbfd->link.next)
2570 1.1 christos if (bfd_get_flavour (pbfd) == bfd_target_elf_flavour
2571 1.1 christos && bfd_count_sections (pbfd) != 0)
2572 1.1 christos {
2573 1.1 christos ebfd = pbfd;
2574 1.1 christos
2575 1.1 christos if (elf_properties (pbfd) != NULL)
2576 1.1 christos break;
2577 1.1 christos }
2578 1.1 christos
2579 1.1.1.3 christos bed = get_elf_backend_data (info->output_bfd);
2580 1.1.1.3 christos
2581 1.1.1.3 christos htab = elf_x86_hash_table (info, bed->target_id);
2582 1.1.1.3 christos if (htab == NULL)
2583 1.1.1.3 christos return pbfd;
2584 1.1.1.3 christos
2585 1.1.1.3 christos features = 0;
2586 1.1.1.3 christos if (htab->params->ibt)
2587 1.1 christos {
2588 1.1.1.3 christos features = GNU_PROPERTY_X86_FEATURE_1_IBT;
2589 1.1.1.3 christos htab->params->cet_report &= ~cet_report_ibt;
2590 1.1.1.3 christos }
2591 1.1.1.3 christos if (htab->params->shstk)
2592 1.1.1.3 christos {
2593 1.1.1.3 christos features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
2594 1.1.1.3 christos htab->params->cet_report &= ~cet_report_shstk;
2595 1.1.1.3 christos }
2596 1.1.1.3 christos if (!(htab->params->cet_report & (cet_report_ibt | cet_report_shstk)))
2597 1.1.1.3 christos htab->params->cet_report = cet_report_none;
2598 1.1.1.3 christos
2599 1.1.1.3 christos if (ebfd != NULL)
2600 1.1.1.3 christos {
2601 1.1.1.3 christos prop = NULL;
2602 1.1.1.3 christos if (features)
2603 1.1.1.3 christos {
2604 1.1.1.3 christos /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT and
2605 1.1.1.3 christos GNU_PROPERTY_X86_FEATURE_1_SHSTK. */
2606 1.1.1.3 christos prop = _bfd_elf_get_property (ebfd,
2607 1.1.1.3 christos GNU_PROPERTY_X86_FEATURE_1_AND,
2608 1.1.1.3 christos 4);
2609 1.1.1.3 christos prop->u.number |= features;
2610 1.1.1.3 christos prop->pr_kind = property_number;
2611 1.1.1.3 christos }
2612 1.1 christos
2613 1.1 christos /* Create the GNU property note section if needed. */
2614 1.1.1.3 christos if (prop != NULL && pbfd == NULL)
2615 1.1 christos {
2616 1.1 christos sec = bfd_make_section_with_flags (ebfd,
2617 1.1 christos NOTE_GNU_PROPERTY_SECTION_NAME,
2618 1.1 christos (SEC_ALLOC
2619 1.1 christos | SEC_LOAD
2620 1.1 christos | SEC_IN_MEMORY
2621 1.1 christos | SEC_READONLY
2622 1.1 christos | SEC_HAS_CONTENTS
2623 1.1 christos | SEC_DATA));
2624 1.1 christos if (sec == NULL)
2625 1.1 christos info->callbacks->einfo (_("%F%P: failed to create GNU property section\n"));
2626 1.1 christos
2627 1.1.1.3 christos if (!bfd_set_section_alignment (sec, class_align))
2628 1.1 christos {
2629 1.1 christos error_alignment:
2630 1.1.1.2 christos info->callbacks->einfo (_("%F%pA: failed to align section\n"),
2631 1.1 christos sec);
2632 1.1 christos }
2633 1.1 christos
2634 1.1 christos elf_section_type (sec) = SHT_NOTE;
2635 1.1 christos }
2636 1.1 christos }
2637 1.1 christos
2638 1.1.1.3 christos if (htab->params->cet_report)
2639 1.1.1.3 christos {
2640 1.1.1.3 christos /* Report missing IBT and SHSTK properties. */
2641 1.1.1.3 christos bfd *abfd;
2642 1.1.1.3 christos const char *msg;
2643 1.1.1.3 christos elf_property_list *p;
2644 1.1.1.3 christos bfd_boolean missing_ibt, missing_shstk;
2645 1.1.1.3 christos bfd_boolean check_ibt
2646 1.1.1.3 christos = !!(htab->params->cet_report & cet_report_ibt);
2647 1.1.1.3 christos bfd_boolean check_shstk
2648 1.1.1.3 christos = !!(htab->params->cet_report & cet_report_shstk);
2649 1.1 christos
2650 1.1.1.3 christos if ((htab->params->cet_report & cet_report_warning))
2651 1.1.1.3 christos msg = _("%P: %pB: warning: missing %s\n");
2652 1.1.1.3 christos else
2653 1.1.1.3 christos msg = _("%X%P: %pB: error: missing %s\n");
2654 1.1 christos
2655 1.1.1.3 christos for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
2656 1.1.1.3 christos if (!(abfd->flags & (DYNAMIC | BFD_PLUGIN | BFD_LINKER_CREATED))
2657 1.1.1.3 christos && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
2658 1.1.1.3 christos {
2659 1.1.1.3 christos for (p = elf_properties (abfd); p; p = p->next)
2660 1.1.1.3 christos if (p->property.pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
2661 1.1.1.3 christos break;
2662 1.1.1.3 christos
2663 1.1.1.3 christos missing_ibt = check_ibt;
2664 1.1.1.3 christos missing_shstk = check_shstk;
2665 1.1.1.3 christos if (p)
2666 1.1.1.3 christos {
2667 1.1.1.3 christos missing_ibt &= !(p->property.u.number
2668 1.1.1.3 christos & GNU_PROPERTY_X86_FEATURE_1_IBT);
2669 1.1.1.3 christos missing_shstk &= !(p->property.u.number
2670 1.1.1.3 christos & GNU_PROPERTY_X86_FEATURE_1_SHSTK);
2671 1.1.1.3 christos }
2672 1.1.1.3 christos if (missing_ibt || missing_shstk)
2673 1.1.1.3 christos {
2674 1.1.1.3 christos const char *missing;
2675 1.1.1.3 christos if (missing_ibt && missing_shstk)
2676 1.1.1.3 christos missing = _("IBT and SHSTK properties");
2677 1.1.1.3 christos else if (missing_ibt)
2678 1.1.1.3 christos missing = _("IBT property");
2679 1.1.1.3 christos else
2680 1.1.1.3 christos missing = _("SHSTK property");
2681 1.1.1.3 christos info->callbacks->einfo (msg, abfd, missing);
2682 1.1.1.3 christos }
2683 1.1.1.3 christos }
2684 1.1.1.3 christos }
2685 1.1.1.3 christos
2686 1.1.1.3 christos pbfd = _bfd_elf_link_setup_gnu_properties (info);
2687 1.1 christos
2688 1.1 christos htab->r_info = init_table->r_info;
2689 1.1 christos htab->r_sym = init_table->r_sym;
2690 1.1 christos
2691 1.1 christos if (bfd_link_relocatable (info))
2692 1.1 christos return pbfd;
2693 1.1 christos
2694 1.1 christos htab->plt0_pad_byte = init_table->plt0_pad_byte;
2695 1.1 christos
2696 1.1.1.3 christos use_ibt_plt = htab->params->ibtplt || htab->params->ibt;
2697 1.1 christos if (!use_ibt_plt && pbfd != NULL)
2698 1.1 christos {
2699 1.1 christos /* Check if GNU_PROPERTY_X86_FEATURE_1_IBT is on. */
2700 1.1 christos elf_property_list *p;
2701 1.1 christos
2702 1.1 christos /* The property list is sorted in order of type. */
2703 1.1 christos for (p = elf_properties (pbfd); p; p = p->next)
2704 1.1 christos {
2705 1.1 christos if (GNU_PROPERTY_X86_FEATURE_1_AND == p->property.pr_type)
2706 1.1 christos {
2707 1.1 christos use_ibt_plt = !!(p->property.u.number
2708 1.1 christos & GNU_PROPERTY_X86_FEATURE_1_IBT);
2709 1.1 christos break;
2710 1.1 christos }
2711 1.1 christos else if (GNU_PROPERTY_X86_FEATURE_1_AND < p->property.pr_type)
2712 1.1 christos break;
2713 1.1 christos }
2714 1.1 christos }
2715 1.1 christos
2716 1.1 christos dynobj = htab->elf.dynobj;
2717 1.1 christos
2718 1.1 christos /* Set htab->elf.dynobj here so that there is no need to check and
2719 1.1 christos set it in check_relocs. */
2720 1.1 christos if (dynobj == NULL)
2721 1.1 christos {
2722 1.1 christos if (pbfd != NULL)
2723 1.1 christos {
2724 1.1 christos htab->elf.dynobj = pbfd;
2725 1.1 christos dynobj = pbfd;
2726 1.1 christos }
2727 1.1 christos else
2728 1.1 christos {
2729 1.1 christos bfd *abfd;
2730 1.1 christos
2731 1.1 christos /* Find a normal input file to hold linker created
2732 1.1 christos sections. */
2733 1.1 christos for (abfd = info->input_bfds;
2734 1.1 christos abfd != NULL;
2735 1.1 christos abfd = abfd->link.next)
2736 1.1 christos if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
2737 1.1 christos && (abfd->flags
2738 1.1.1.2 christos & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
2739 1.1.1.2 christos && bed->relocs_compatible (abfd->xvec,
2740 1.1.1.2 christos info->output_bfd->xvec))
2741 1.1 christos {
2742 1.1 christos htab->elf.dynobj = abfd;
2743 1.1 christos dynobj = abfd;
2744 1.1 christos break;
2745 1.1 christos }
2746 1.1 christos }
2747 1.1 christos }
2748 1.1 christos
2749 1.1 christos /* Return if there are no normal input files. */
2750 1.1 christos if (dynobj == NULL)
2751 1.1 christos return pbfd;
2752 1.1 christos
2753 1.1 christos /* Even when lazy binding is disabled by "-z now", the PLT0 entry may
2754 1.1 christos still be used with LD_AUDIT or LD_PROFILE if PLT entry is used for
2755 1.1 christos canonical function address. */
2756 1.1 christos htab->plt.has_plt0 = 1;
2757 1.1 christos normal_target = htab->target_os == is_normal;
2758 1.1 christos
2759 1.1 christos if (normal_target)
2760 1.1 christos {
2761 1.1 christos if (use_ibt_plt)
2762 1.1 christos {
2763 1.1 christos htab->lazy_plt = init_table->lazy_ibt_plt;
2764 1.1 christos htab->non_lazy_plt = init_table->non_lazy_ibt_plt;
2765 1.1 christos }
2766 1.1 christos else
2767 1.1 christos {
2768 1.1 christos htab->lazy_plt = init_table->lazy_plt;
2769 1.1 christos htab->non_lazy_plt = init_table->non_lazy_plt;
2770 1.1 christos }
2771 1.1 christos }
2772 1.1 christos else
2773 1.1 christos {
2774 1.1 christos htab->lazy_plt = init_table->lazy_plt;
2775 1.1 christos htab->non_lazy_plt = NULL;
2776 1.1 christos }
2777 1.1 christos
2778 1.1 christos pltsec = htab->elf.splt;
2779 1.1 christos
2780 1.1 christos /* If the non-lazy PLT is available, use it for all PLT entries if
2781 1.1 christos there are no PLT0 or no .plt section. */
2782 1.1 christos if (htab->non_lazy_plt != NULL
2783 1.1 christos && (!htab->plt.has_plt0 || pltsec == NULL))
2784 1.1 christos {
2785 1.1 christos lazy_plt = FALSE;
2786 1.1 christos if (bfd_link_pic (info))
2787 1.1 christos htab->plt.plt_entry = htab->non_lazy_plt->pic_plt_entry;
2788 1.1 christos else
2789 1.1 christos htab->plt.plt_entry = htab->non_lazy_plt->plt_entry;
2790 1.1 christos htab->plt.plt_entry_size = htab->non_lazy_plt->plt_entry_size;
2791 1.1 christos htab->plt.plt_got_offset = htab->non_lazy_plt->plt_got_offset;
2792 1.1 christos htab->plt.plt_got_insn_size
2793 1.1 christos = htab->non_lazy_plt->plt_got_insn_size;
2794 1.1 christos htab->plt.eh_frame_plt_size
2795 1.1 christos = htab->non_lazy_plt->eh_frame_plt_size;
2796 1.1 christos htab->plt.eh_frame_plt = htab->non_lazy_plt->eh_frame_plt;
2797 1.1 christos }
2798 1.1 christos else
2799 1.1 christos {
2800 1.1 christos lazy_plt = TRUE;
2801 1.1 christos if (bfd_link_pic (info))
2802 1.1 christos {
2803 1.1 christos htab->plt.plt0_entry = htab->lazy_plt->pic_plt0_entry;
2804 1.1 christos htab->plt.plt_entry = htab->lazy_plt->pic_plt_entry;
2805 1.1 christos }
2806 1.1 christos else
2807 1.1 christos {
2808 1.1 christos htab->plt.plt0_entry = htab->lazy_plt->plt0_entry;
2809 1.1 christos htab->plt.plt_entry = htab->lazy_plt->plt_entry;
2810 1.1 christos }
2811 1.1 christos htab->plt.plt_entry_size = htab->lazy_plt->plt_entry_size;
2812 1.1 christos htab->plt.plt_got_offset = htab->lazy_plt->plt_got_offset;
2813 1.1 christos htab->plt.plt_got_insn_size
2814 1.1 christos = htab->lazy_plt->plt_got_insn_size;
2815 1.1 christos htab->plt.eh_frame_plt_size
2816 1.1 christos = htab->lazy_plt->eh_frame_plt_size;
2817 1.1 christos htab->plt.eh_frame_plt = htab->lazy_plt->eh_frame_plt;
2818 1.1 christos }
2819 1.1 christos
2820 1.1 christos if (htab->target_os == is_vxworks
2821 1.1 christos && !elf_vxworks_create_dynamic_sections (dynobj, info,
2822 1.1 christos &htab->srelplt2))
2823 1.1 christos {
2824 1.1 christos info->callbacks->einfo (_("%F%P: failed to create VxWorks dynamic sections\n"));
2825 1.1 christos return pbfd;
2826 1.1 christos }
2827 1.1 christos
2828 1.1 christos /* Since create_dynamic_sections isn't always called, but GOT
2829 1.1 christos relocations need GOT relocations, create them here so that we
2830 1.1 christos don't need to do it in check_relocs. */
2831 1.1 christos if (htab->elf.sgot == NULL
2832 1.1 christos && !_bfd_elf_create_got_section (dynobj, info))
2833 1.1 christos info->callbacks->einfo (_("%F%P: failed to create GOT sections\n"));
2834 1.1 christos
2835 1.1 christos got_align = (bed->target_id == X86_64_ELF_DATA) ? 3 : 2;
2836 1.1 christos
2837 1.1 christos /* Align .got and .got.plt sections to their entry size. Do it here
2838 1.1 christos instead of in create_dynamic_sections so that they are always
2839 1.1 christos properly aligned even if create_dynamic_sections isn't called. */
2840 1.1 christos sec = htab->elf.sgot;
2841 1.1.1.3 christos if (!bfd_set_section_alignment (sec, got_align))
2842 1.1 christos goto error_alignment;
2843 1.1 christos
2844 1.1 christos sec = htab->elf.sgotplt;
2845 1.1.1.3 christos if (!bfd_set_section_alignment (sec, got_align))
2846 1.1 christos goto error_alignment;
2847 1.1 christos
2848 1.1 christos /* Create the ifunc sections here so that check_relocs can be
2849 1.1 christos simplified. */
2850 1.1 christos if (!_bfd_elf_create_ifunc_sections (dynobj, info))
2851 1.1 christos info->callbacks->einfo (_("%F%P: failed to create ifunc sections\n"));
2852 1.1 christos
2853 1.1 christos plt_alignment = bfd_log2 (htab->plt.plt_entry_size);
2854 1.1 christos
2855 1.1 christos if (pltsec != NULL)
2856 1.1 christos {
2857 1.1 christos /* Whe creating executable, set the contents of the .interp
2858 1.1 christos section to the interpreter. */
2859 1.1 christos if (bfd_link_executable (info) && !info->nointerp)
2860 1.1 christos {
2861 1.1 christos asection *s = bfd_get_linker_section (dynobj, ".interp");
2862 1.1 christos if (s == NULL)
2863 1.1 christos abort ();
2864 1.1 christos s->size = htab->dynamic_interpreter_size;
2865 1.1 christos s->contents = (unsigned char *) htab->dynamic_interpreter;
2866 1.1 christos htab->interp = s;
2867 1.1 christos }
2868 1.1 christos
2869 1.1 christos /* Don't change PLT section alignment for NaCl since it uses
2870 1.1 christos 64-byte PLT entry and sets PLT section alignment to 32
2871 1.1 christos bytes. Don't create additional PLT sections for NaCl. */
2872 1.1 christos if (normal_target)
2873 1.1 christos {
2874 1.1 christos flagword pltflags = (bed->dynamic_sec_flags
2875 1.1 christos | SEC_ALLOC
2876 1.1 christos | SEC_CODE
2877 1.1 christos | SEC_LOAD
2878 1.1 christos | SEC_READONLY);
2879 1.1 christos unsigned int non_lazy_plt_alignment
2880 1.1 christos = bfd_log2 (htab->non_lazy_plt->plt_entry_size);
2881 1.1 christos
2882 1.1 christos sec = pltsec;
2883 1.1.1.3 christos if (!bfd_set_section_alignment (sec, plt_alignment))
2884 1.1 christos goto error_alignment;
2885 1.1 christos
2886 1.1 christos /* Create the GOT procedure linkage table. */
2887 1.1 christos sec = bfd_make_section_anyway_with_flags (dynobj,
2888 1.1 christos ".plt.got",
2889 1.1 christos pltflags);
2890 1.1 christos if (sec == NULL)
2891 1.1 christos info->callbacks->einfo (_("%F%P: failed to create GOT PLT section\n"));
2892 1.1 christos
2893 1.1.1.3 christos if (!bfd_set_section_alignment (sec, non_lazy_plt_alignment))
2894 1.1 christos goto error_alignment;
2895 1.1 christos
2896 1.1 christos htab->plt_got = sec;
2897 1.1 christos
2898 1.1 christos if (lazy_plt)
2899 1.1 christos {
2900 1.1 christos sec = NULL;
2901 1.1 christos
2902 1.1 christos if (use_ibt_plt)
2903 1.1 christos {
2904 1.1 christos /* Create the second PLT for Intel IBT support. IBT
2905 1.1 christos PLT is supported only for non-NaCl target and is
2906 1.1 christos is needed only for lazy binding. */
2907 1.1 christos sec = bfd_make_section_anyway_with_flags (dynobj,
2908 1.1 christos ".plt.sec",
2909 1.1 christos pltflags);
2910 1.1 christos if (sec == NULL)
2911 1.1 christos info->callbacks->einfo (_("%F%P: failed to create IBT-enabled PLT section\n"));
2912 1.1 christos
2913 1.1.1.3 christos if (!bfd_set_section_alignment (sec, plt_alignment))
2914 1.1 christos goto error_alignment;
2915 1.1 christos }
2916 1.1.1.3 christos else if (htab->params->bndplt && ABI_64_P (dynobj))
2917 1.1 christos {
2918 1.1 christos /* Create the second PLT for Intel MPX support. MPX
2919 1.1 christos PLT is supported only for non-NaCl target in 64-bit
2920 1.1 christos mode and is needed only for lazy binding. */
2921 1.1 christos sec = bfd_make_section_anyway_with_flags (dynobj,
2922 1.1 christos ".plt.sec",
2923 1.1 christos pltflags);
2924 1.1 christos if (sec == NULL)
2925 1.1 christos info->callbacks->einfo (_("%F%P: failed to create BND PLT section\n"));
2926 1.1 christos
2927 1.1.1.3 christos if (!bfd_set_section_alignment (sec, non_lazy_plt_alignment))
2928 1.1 christos goto error_alignment;
2929 1.1 christos }
2930 1.1 christos
2931 1.1 christos htab->plt_second = sec;
2932 1.1 christos }
2933 1.1 christos }
2934 1.1 christos
2935 1.1 christos if (!info->no_ld_generated_unwind_info)
2936 1.1 christos {
2937 1.1 christos flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
2938 1.1 christos | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2939 1.1 christos | SEC_LINKER_CREATED);
2940 1.1 christos
2941 1.1 christos sec = bfd_make_section_anyway_with_flags (dynobj,
2942 1.1 christos ".eh_frame",
2943 1.1 christos flags);
2944 1.1 christos if (sec == NULL)
2945 1.1 christos info->callbacks->einfo (_("%F%P: failed to create PLT .eh_frame section\n"));
2946 1.1 christos
2947 1.1.1.3 christos if (!bfd_set_section_alignment (sec, class_align))
2948 1.1 christos goto error_alignment;
2949 1.1 christos
2950 1.1 christos htab->plt_eh_frame = sec;
2951 1.1 christos
2952 1.1 christos if (htab->plt_got != NULL)
2953 1.1 christos {
2954 1.1 christos sec = bfd_make_section_anyway_with_flags (dynobj,
2955 1.1 christos ".eh_frame",
2956 1.1 christos flags);
2957 1.1 christos if (sec == NULL)
2958 1.1 christos info->callbacks->einfo (_("%F%P: failed to create GOT PLT .eh_frame section\n"));
2959 1.1 christos
2960 1.1.1.3 christos if (!bfd_set_section_alignment (sec, class_align))
2961 1.1 christos goto error_alignment;
2962 1.1 christos
2963 1.1 christos htab->plt_got_eh_frame = sec;
2964 1.1 christos }
2965 1.1 christos
2966 1.1 christos if (htab->plt_second != NULL)
2967 1.1 christos {
2968 1.1 christos sec = bfd_make_section_anyway_with_flags (dynobj,
2969 1.1 christos ".eh_frame",
2970 1.1 christos flags);
2971 1.1 christos if (sec == NULL)
2972 1.1 christos info->callbacks->einfo (_("%F%P: failed to create the second PLT .eh_frame section\n"));
2973 1.1 christos
2974 1.1.1.3 christos if (!bfd_set_section_alignment (sec, class_align))
2975 1.1 christos goto error_alignment;
2976 1.1 christos
2977 1.1 christos htab->plt_second_eh_frame = sec;
2978 1.1 christos }
2979 1.1 christos }
2980 1.1 christos }
2981 1.1 christos
2982 1.1.1.3 christos /* The .iplt section is used for IFUNC symbols in static
2983 1.1.1.3 christos executables. */
2984 1.1.1.3 christos sec = htab->elf.iplt;
2985 1.1.1.3 christos if (sec != NULL)
2986 1.1 christos {
2987 1.1.1.3 christos /* NB: Delay setting its alignment until we know it is non-empty.
2988 1.1.1.3 christos Otherwise an empty iplt section may change vma and lma of the
2989 1.1.1.3 christos following sections, which triggers moving dot of the following
2990 1.1.1.3 christos section backwards, resulting in a warning and section lma not
2991 1.1.1.3 christos being set properly. It later leads to a "File truncated"
2992 1.1.1.3 christos error. */
2993 1.1.1.3 christos if (!bfd_set_section_alignment (sec, 0))
2994 1.1 christos goto error_alignment;
2995 1.1.1.3 christos
2996 1.1.1.3 christos htab->plt.iplt_alignment = (normal_target
2997 1.1.1.3 christos ? plt_alignment
2998 1.1.1.3 christos : bed->plt_alignment);
2999 1.1 christos }
3000 1.1 christos
3001 1.1 christos return pbfd;
3002 1.1 christos }
3003 1.1.1.3 christos
3004 1.1.1.3 christos /* Fix up x86 GNU properties. */
3005 1.1.1.3 christos
3006 1.1.1.3 christos void
3007 1.1.1.3 christos _bfd_x86_elf_link_fixup_gnu_properties
3008 1.1.1.3 christos (struct bfd_link_info *info ATTRIBUTE_UNUSED,
3009 1.1.1.3 christos elf_property_list **listp)
3010 1.1.1.3 christos {
3011 1.1.1.3 christos elf_property_list *p;
3012 1.1.1.3 christos
3013 1.1.1.3 christos for (p = *listp; p; p = p->next)
3014 1.1.1.3 christos {
3015 1.1.1.3 christos unsigned int type = p->property.pr_type;
3016 1.1.1.3 christos if (type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
3017 1.1.1.3 christos || type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
3018 1.1.1.3 christos || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
3019 1.1.1.3 christos && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
3020 1.1.1.3 christos || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
3021 1.1.1.3 christos && type <= GNU_PROPERTY_X86_UINT32_OR_HI)
3022 1.1.1.3 christos || (type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
3023 1.1.1.3 christos && type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
3024 1.1.1.3 christos {
3025 1.1.1.3 christos if (p->property.u.number == 0
3026 1.1.1.3 christos && (type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
3027 1.1.1.3 christos || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
3028 1.1.1.3 christos && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
3029 1.1.1.3 christos || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
3030 1.1.1.3 christos && type <= GNU_PROPERTY_X86_UINT32_OR_HI)))
3031 1.1.1.3 christos {
3032 1.1.1.3 christos /* Remove empty property. */
3033 1.1.1.3 christos *listp = p->next;
3034 1.1.1.3 christos continue;
3035 1.1.1.3 christos }
3036 1.1.1.3 christos
3037 1.1.1.3 christos listp = &p->next;
3038 1.1.1.3 christos }
3039 1.1.1.3 christos else if (type > GNU_PROPERTY_HIPROC)
3040 1.1.1.3 christos {
3041 1.1.1.3 christos /* The property list is sorted in order of type. */
3042 1.1.1.3 christos break;
3043 1.1.1.3 christos }
3044 1.1.1.3 christos }
3045 1.1.1.3 christos }
3046 1.1.1.3 christos
3047 1.1.1.3 christos void
3048 1.1.1.3 christos _bfd_elf_linker_x86_set_options (struct bfd_link_info * info,
3049 1.1.1.3 christos struct elf_linker_x86_params *params)
3050 1.1.1.3 christos {
3051 1.1.1.3 christos const struct elf_backend_data *bed
3052 1.1.1.3 christos = get_elf_backend_data (info->output_bfd);
3053 1.1.1.3 christos struct elf_x86_link_hash_table *htab
3054 1.1.1.3 christos = elf_x86_hash_table (info, bed->target_id);
3055 1.1.1.3 christos if (htab != NULL)
3056 1.1.1.3 christos htab->params = params;
3057 1.1.1.3 christos }
3058