xref: /netbsd-src/external/gpl3/gdb/dist/bfd/elfxx-x86.h (revision 4b004442778f1201b2161e87fd65ba87aae6601a)
1 /* x86 specific support for ELF
2    Copyright (C) 2017-2022 Free Software Foundation, Inc.
3 
4    This file is part of BFD, the Binary File Descriptor library.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20 
21 /* Don't generate unused section symbols.  */
22 #define TARGET_KEEP_UNUSED_SECTION_SYMBOLS false
23 
24 #include "sysdep.h"
25 #include "bfd.h"
26 #include "bfdlink.h"
27 #include "libbfd.h"
28 #include "elf-bfd.h"
29 #include "hashtab.h"
30 #include "elf-linker-x86.h"
31 #include "elf/i386.h"
32 #include "elf/x86-64.h"
33 #include "sframe-api.h"
34 
35 #define X86_64_PCREL_TYPE_P(TYPE) \
36   ((TYPE) == R_X86_64_PC8 \
37    || (TYPE) == R_X86_64_PC16 \
38    || (TYPE) == R_X86_64_PC32 \
39    || (TYPE) == R_X86_64_PC64)
40 #define I386_PCREL_TYPE_P(TYPE) ((TYPE) == R_386_PC32)
41 #define X86_PCREL_TYPE_P(IS_X86_64, TYPE) \
42   ((IS_X86_64) ? X86_64_PCREL_TYPE_P (TYPE) : I386_PCREL_TYPE_P (TYPE))
43 
44 #define X86_64_SIZE_TYPE_P(TYPE) \
45   ((TYPE) == R_X86_64_SIZE32 || (TYPE) == R_X86_64_SIZE64)
46 #define I386_SIZE_TYPE_P(TYPE) ((TYPE) == R_386_SIZE32)
47 #define X86_SIZE_TYPE_P(IS_X86_64, TYPE) \
48   ((IS_X86_64) ? X86_64_SIZE_TYPE_P(TYPE) : I386_SIZE_TYPE_P (TYPE))
49 
50 #define X86_64_GOT_TYPE_P(TYPE) \
51   ((TYPE) == R_X86_64_GOTPCREL \
52    || (TYPE) == R_X86_64_GOTPCRELX \
53    || (TYPE) == R_X86_64_REX_GOTPCRELX \
54    || (TYPE) == R_X86_64_GOT32 \
55    || (TYPE) == R_X86_64_GOT64 \
56    || (TYPE) == R_X86_64_GOTPCREL64 \
57    || (TYPE) == R_X86_64_GOTPLT64)
58 #define I386_GOT_TYPE_P(TYPE) \
59   ((TYPE) == R_386_GOT32 || (TYPE) == R_386_GOT32X)
60 #define X86_GOT_TYPE_P(IS_X86_64, TYPE) \
61   ((IS_X86_64) ? X86_64_GOT_TYPE_P (TYPE) : I386_GOT_TYPE_P (TYPE))
62 
63 #define X86_64_RELATIVE_RELOC_TYPE_P(TYPE) \
64   (X86_64_PCREL_TYPE_P (TYPE) \
65    || (TYPE) == R_X86_64_8 \
66    || (TYPE) == R_X86_64_16 \
67    || (TYPE) == R_X86_64_32 \
68    || (TYPE) == R_X86_64_32S \
69    || (TYPE) == R_X86_64_64)
70 #define I386_RELATIVE_RELOC_TYPE_P(TYPE) \
71   ((TYPE) == R_386_32 || (TYPE) == R_386_PC32)
72 #define X86_RELATIVE_RELOC_TYPE_P(IS_X86_64, TYPE) \
73   ((IS_X86_64) \
74    ? X86_64_RELATIVE_RELOC_TYPE_P (TYPE) \
75    : I386_RELATIVE_RELOC_TYPE_P(TYPE))
76 
77 #define X86_64_NEED_DYNAMIC_RELOC_TYPE_P(TYPE) \
78   (X86_64_SIZE_TYPE_P (TYPE) \
79    || X86_64_RELATIVE_RELOC_TYPE_P (TYPE))
80 #define I386_NEED_DYNAMIC_RELOC_TYPE_P(TYPE) \
81   (I386_SIZE_TYPE_P (TYPE) \
82    || I386_RELATIVE_RELOC_TYPE_P (TYPE) \
83    || (TYPE) == R_386_TLS_IE \
84    || (TYPE) == R_386_TLS_LE \
85    || (TYPE) == R_386_TLS_LE_32)
86 #define X86_NEED_DYNAMIC_RELOC_TYPE_P(IS_X86_64, TYPE) \
87   ((IS_X86_64) \
88    ? X86_64_NEED_DYNAMIC_RELOC_TYPE_P (TYPE) \
89    : I386_NEED_DYNAMIC_RELOC_TYPE_P (TYPE))
90 
91 #define X86_LOCAL_GOT_RELATIVE_RELOC_P(IS_X86_64, INFO, SYM) \
92   (bfd_link_pic (INFO) \
93    && (!(IS_X86_64) || ((SYM) != NULL && (SYM)->st_shndx != SHN_ABS)))
94 
95 #define PLT_CIE_LENGTH		20
96 #define PLT_FDE_LENGTH		36
97 #define PLT_FDE_START_OFFSET	4 + PLT_CIE_LENGTH + 8
98 #define PLT_FDE_LEN_OFFSET	4 + PLT_CIE_LENGTH + 12
99 
100 #define I386_PCREL_TYPE_P(TYPE) ((TYPE) == R_386_PC32)
101 #define X86_64_PCREL_TYPE_P(TYPE) \
102   ((TYPE) == R_X86_64_PC8 \
103    || (TYPE) == R_X86_64_PC16 \
104    || (TYPE) == R_X86_64_PC32 \
105    || (TYPE) == R_X86_64_PC64)
106 
107 /* This must be the same as sframe_get_hdr_size (sfh).  For x86-64, this value
108    is the same as sizeof (sframe_header) because there is no SFrame auxilliary
109    header.  */
110 #define PLT_SFRAME_FDE_START_OFFSET	sizeof (sframe_header)
111 
112 #define ABI_64_P(abfd) \
113   (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
114 
115 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
116    copying dynamic variables from a shared lib into an app's dynbss
117    section, and instead use a dynamic relocation to point into the
118    shared lib.  */
119 #define ELIMINATE_COPY_RELOCS 1
120 
121 #define elf_x86_hash_table(p, id) \
122   (is_elf_hash_table ((p)->hash) \
123    && elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) == (id) \
124     ? ((struct elf_x86_link_hash_table *) ((p)->hash)) : NULL)
125 
126 /* Will references to this symbol always be local in this object?  */
127 #define SYMBOL_REFERENCES_LOCAL_P(INFO, H) \
128   _bfd_x86_elf_link_symbol_references_local ((INFO), (H))
129 
130 /* TRUE if an undefined weak symbol should be resolved to 0.  Local
131    undefined weak symbol is always resolved to 0.  Reference to an
132    undefined weak symbol is resolved to 0 in executable if undefined
133    weak symbol should be resolved to 0 (zero_undefweak > 0).  */
134 #define UNDEFINED_WEAK_RESOLVED_TO_ZERO(INFO, EH) \
135   ((EH)->elf.root.type == bfd_link_hash_undefweak		 \
136    && (SYMBOL_REFERENCES_LOCAL_P ((INFO), &(EH)->elf)		 \
137        || (bfd_link_executable (INFO)				 \
138 	   && (EH)->zero_undefweak > 0)))
139 
140 /* Should copy relocation be generated for a symbol.  Don't generate
141    copy relocation against a protected symbol defined in a shared
142    object.  */
143 #define SYMBOL_NO_COPYRELOC(INFO, EH) \
144   ((EH)->def_protected \
145    && ((EH)->elf.root.type == bfd_link_hash_defined \
146        || (EH)->elf.root.type == bfd_link_hash_defweak) \
147    && ((EH)->elf.root.u.def.section->owner->flags & DYNAMIC) != 0 \
148    && ((EH)->elf.root.u.def.section->flags & SEC_CODE) == 0)
149 
150 /* TRUE if dynamic relocation is needed.  If we are creating a shared
151    library, and this is a reloc against a global symbol, or a non PC
152    relative reloc against a local symbol, then we need to copy the reloc
153    into the shared library.  However, if we are linking with -Bsymbolic,
154    we do not need to copy a reloc against a global symbol which is
155    defined in an object we are including in the link (i.e., DEF_REGULAR
156    is set).
157 
158    If PCREL_PLT is true, don't generate dynamic relocation in PIE for
159    PC-relative relocation against a dynamic function definition in data
160    section when PLT address can be used.
161 
162    If on the other hand, we are creating an executable, we may need to
163    keep relocations for symbols satisfied by a dynamic library if we
164    manage to avoid copy relocs for the symbol.
165 
166    We also need to generate dynamic pointer relocation against
167    STT_GNU_IFUNC symbol in the non-code section.  */
168 #define NEED_DYNAMIC_RELOCATION_P(IS_X86_64, INFO, PCREL_PLT, H, SEC, \
169 				  R_TYPE, POINTER_TYPE) \
170   ((bfd_link_pic (INFO) \
171     && (! X86_PCREL_TYPE_P (IS_X86_64, R_TYPE) \
172 	|| ((H) != NULL \
173 	    && (! (bfd_link_pie (INFO) \
174 		   || SYMBOLIC_BIND ((INFO), (H))) \
175 		|| (H)->root.type == bfd_link_hash_defweak \
176 		|| (!(bfd_link_pie (INFO) \
177 		      && (PCREL_PLT) \
178 		      && (H)->plt.refcount > 0 \
179 		      && ((SEC)->flags & SEC_CODE) == 0 \
180 		      && (H)->type == STT_FUNC \
181 		      && (H)->def_dynamic) \
182 		    && !(H)->def_regular))))) \
183    || ((H) != NULL \
184        && (H)->type == STT_GNU_IFUNC \
185        && (R_TYPE) == POINTER_TYPE \
186        && ((SEC)->flags & SEC_CODE) == 0) \
187    || (ELIMINATE_COPY_RELOCS \
188        && !bfd_link_pic (INFO) \
189        && (H) != NULL \
190        && ((H)->root.type == bfd_link_hash_defweak \
191 	   || !(H)->def_regular)))
192 
193 /* TRUE if dynamic relocation should be generated.  Don't copy a
194    pc-relative relocation into the output file if the symbol needs
195    copy reloc or the symbol is undefined when building executable.
196    Copy dynamic function pointer relocations.  Don't generate dynamic
197    relocations against resolved undefined weak symbols in PIE, except
198    when PC32_RELOC is TRUE.  Undefined weak symbol is bound locally
199    when PIC is false.  Don't generate dynamic relocations against
200    non-preemptible absolute symbol.  NB: rel_from_abs is set on symbols
201    defined by linker scripts from "dot" (also SEGMENT_START or ORIGIN)
202    outside of an output section statement, which will be converted from
203    absolute to section-relative in set_sym_sections called from
204    ldexp_finalize_syms after ldemul_finish.   */
205 #define GENERATE_DYNAMIC_RELOCATION_P(IS_X86_64, INFO, EH, R_TYPE, \
206 				      SEC, NEED_COPY_RELOC_IN_PIE, \
207 				      RESOLVED_TO_ZERO, PC32_RELOC) \
208   ((bfd_link_pic (INFO) \
209     && !(bfd_is_abs_section (SEC) \
210 	 && ((EH) == NULL \
211 	     || (EH)->elf.root.rel_from_abs == 0) \
212 	 && ((EH) == NULL \
213 	     || SYMBOL_REFERENCES_LOCAL (INFO, &(EH)->elf))) \
214     && !(NEED_COPY_RELOC_IN_PIE) \
215     && ((EH) == NULL \
216 	|| ((ELF_ST_VISIBILITY ((EH)->elf.other) == STV_DEFAULT \
217 	     && (!(RESOLVED_TO_ZERO) || PC32_RELOC)) \
218 	    || (EH)->elf.root.type != bfd_link_hash_undefweak)) \
219     && ((!X86_PCREL_TYPE_P (IS_X86_64, R_TYPE) \
220 	 && !X86_SIZE_TYPE_P (IS_X86_64, R_TYPE)) \
221 	|| ! SYMBOL_CALLS_LOCAL ((INFO), \
222 				 (struct elf_link_hash_entry *) (EH)))) \
223    || (ELIMINATE_COPY_RELOCS \
224        && !bfd_link_pic (INFO) \
225        && (EH) != NULL \
226        && (EH)->elf.dynindx != -1 \
227        && (!(EH)->elf.non_got_ref \
228 	   || ((EH)->elf.root.type == bfd_link_hash_undefweak \
229 	       && !(RESOLVED_TO_ZERO))) \
230 	       && (((EH)->elf.def_dynamic && !(EH)->elf.def_regular) \
231 		   || (EH)->elf.root.type == bfd_link_hash_undefined)))
232 
233 /* TRUE if this input relocation should be copied to output.  H->dynindx
234    may be -1 if this symbol was marked to become local.  */
235 #define COPY_INPUT_RELOC_P(IS_X86_64, INFO, H, R_TYPE) \
236   ((H) != NULL \
237    && (H)->dynindx != -1 \
238    && (X86_PCREL_TYPE_P (IS_X86_64, R_TYPE) \
239        || !(bfd_link_executable (INFO) || SYMBOLIC_BIND ((INFO), (H))) \
240        || !(H)->def_regular))
241 
242 /* TRUE if this is actually a static link, or it is a -Bsymbolic link
243    and the symbol is defined locally, or the symbol was forced to be
244    local because of a version file.  */
245 #define RESOLVED_LOCALLY_P(INFO, H, HTAB) \
246   (!WILL_CALL_FINISH_DYNAMIC_SYMBOL ((HTAB)->elf.dynamic_sections_created, \
247 				     bfd_link_pic (INFO), (H)) \
248    || (bfd_link_pic (INFO) \
249        && SYMBOL_REFERENCES_LOCAL_P ((INFO), (H))) \
250        || (ELF_ST_VISIBILITY ((H)->other) \
251 	   && (H)->root.type == bfd_link_hash_undefweak))
252 
253 /* TRUE if this symbol isn't defined by a shared object.  */
254 #define SYMBOL_DEFINED_NON_SHARED_P(H) \
255   ((H)->def_regular \
256    || (H)->root.linker_def \
257    || (H)->root.ldscript_def \
258    || ((struct elf_x86_link_hash_entry *) (H))->linker_def \
259    || ELF_COMMON_DEF_P (H))
260 
261 /* Return TRUE if the symbol described by a linker hash entry H is
262    going to be absolute.  Similar to bfd_is_abs_symbol, but excluding
263    all linker-script defined symbols.  */
264 #define ABS_SYMBOL_P(H) \
265   (bfd_is_abs_symbol (&(H)->root) && !(H)->root.ldscript_def)
266 
267 /* TRUE if relative relocation should be generated.  GOT reference to
268    global symbol in PIC will lead to dynamic symbol.  It becomes a
269    problem when "time" or "times" is defined as a variable in an
270    executable, clashing with functions of the same name in libc.  If a
271    symbol isn't undefined weak symbol, don't make it dynamic in PIC and
272    generate relative relocation.   Don't generate relative relocation
273    against non-preemptible absolute symbol.  */
274 #define GENERATE_RELATIVE_RELOC_P(INFO, H) \
275   ((H)->dynindx == -1 \
276    && !(H)->forced_local \
277    && (H)->root.type != bfd_link_hash_undefweak \
278    && bfd_link_pic (INFO) \
279    && !ABS_SYMBOL_P (H))
280 
281 /* TRUE if this is a pointer reference to a local IFUNC.  */
282 #define POINTER_LOCAL_IFUNC_P(INFO, H) \
283   ((H)->dynindx == -1 \
284    || (H)->forced_local \
285    || bfd_link_executable (INFO))
286 
287 /* TRUE if this is a PLT reference to a local IFUNC.  */
288 #define PLT_LOCAL_IFUNC_P(INFO, H) \
289   ((H)->dynindx == -1 \
290    || ((bfd_link_executable (INFO) \
291 	|| ELF_ST_VISIBILITY ((H)->other) != STV_DEFAULT) \
292 	&& (H)->def_regular \
293 	&& (H)->type == STT_GNU_IFUNC))
294 
295 /* TRUE if TLS IE->LE transition is OK.  */
296 #define TLS_TRANSITION_IE_TO_LE_P(INFO, H, TLS_TYPE) \
297   (bfd_link_executable (INFO) \
298    && (H) != NULL \
299    && (H)->dynindx == -1 \
300    && (TLS_TYPE & GOT_TLS_IE))
301 
302 /* Verify that the symbol has an entry in the procedure linkage table.  */
303 #define VERIFY_PLT_ENTRY(INFO, H, PLT, GOTPLT, RELPLT, LOCAL_UNDEFWEAK) \
304   do \
305     { \
306       if (((H)->dynindx == -1 \
307 	   && !LOCAL_UNDEFWEAK \
308 	   && !(((H)->forced_local || bfd_link_executable (INFO)) \
309 		&& (H)->def_regular \
310 		&& (H)->type == STT_GNU_IFUNC)) \
311 	  || (PLT) == NULL \
312 	  || (GOTPLT) == NULL \
313 	  || (RELPLT) == NULL) \
314 	abort (); \
315     } \
316   while (0);
317 
318 /* Verify that the symbol supports copy relocation.  */
319 #define VERIFY_COPY_RELOC(H, HTAB) \
320   do \
321     { \
322       if ((H)->dynindx == -1 \
323 	  || ((H)->root.type != bfd_link_hash_defined \
324 	      && (H)->root.type != bfd_link_hash_defweak) \
325 	  || (HTAB)->elf.srelbss == NULL \
326 	  || (HTAB)->elf.sreldynrelro == NULL) \
327 	abort (); \
328     } \
329   while (0);
330 
331 /* x86 ELF linker hash entry.  */
332 
333 struct elf_x86_link_hash_entry
334 {
335   struct elf_link_hash_entry elf;
336 
337   unsigned char tls_type;
338 
339   /* Bit 0: Symbol has no GOT nor PLT relocations.
340      Bit 1: Symbol has non-GOT/non-PLT relocations in text sections.
341      zero_undefweak is initialized to 1 and undefined weak symbol
342      should be resolved to 0 if zero_undefweak > 0.  */
343   unsigned int zero_undefweak : 2;
344 
345   /* Don't call finish_dynamic_symbol on this symbol.  */
346   unsigned int no_finish_dynamic_symbol : 1;
347 
348   /* R_*_RELATIVE relocation in GOT for this symbol has been
349      processed.  */
350   unsigned int got_relative_reloc_done : 1;
351 
352   /* TRUE if symbol is __tls_get_addr.  */
353   unsigned int tls_get_addr : 1;
354 
355   /* TRUE if symbol is defined as a protected symbol.  */
356   unsigned int def_protected : 1;
357 
358   /* 0: Symbol references are unknown.
359      1: Symbol references aren't local.
360      2: Symbol references are local.
361    */
362   unsigned int local_ref : 2;
363 
364   /* TRUE if symbol is defined by linker.  */
365   unsigned int linker_def : 1;
366 
367   /* TRUE if symbol is referenced by a non-GOT/non-PLT relocation in a
368      relocatable object file without indirect external access marker.  */
369   unsigned int non_got_ref_without_indirect_extern_access : 1;
370 
371   /* TRUE if symbol is referenced by R_386_GOTOFF relocation.  This is
372      only used by i386.  */
373   unsigned int gotoff_ref : 1;
374 
375   /* TRUE if a weak symbol with a real definition needs a copy reloc.
376      When there is a weak symbol with a real definition, the processor
377      independent code will have arranged for us to see the real
378      definition first.  We need to copy the needs_copy bit from the
379      real definition and check it when allowing copy reloc in PIE.  This
380      is only used by x86-64.  */
381   unsigned int needs_copy : 1;
382 
383   /* Information about the GOT PLT entry. Filled when there are both
384      GOT and PLT relocations against the same function.  */
385   union gotplt_union plt_got;
386 
387   /* Information about the second PLT entry.   */
388   union gotplt_union plt_second;
389 
390   /* Offset of the GOTPLT entry reserved for the TLS descriptor,
391      starting at the end of the jump table.  */
392   bfd_vma tlsdesc_got;
393 };
394 
395 #define SFRAME_PLT0_MAX_NUM_FRES 2
396 #define SFRAME_PLTN_MAX_NUM_FRES 2
397 
398 struct elf_x86_sframe_plt
399 {
400   unsigned int plt0_entry_size;
401   unsigned int plt0_num_fres;
402   const sframe_frame_row_entry *plt0_fres[SFRAME_PLT0_MAX_NUM_FRES];
403 
404   unsigned int pltn_entry_size;
405   unsigned int pltn_num_fres;
406   const sframe_frame_row_entry *pltn_fres[SFRAME_PLTN_MAX_NUM_FRES];
407 
408   unsigned int sec_pltn_entry_size;
409   unsigned int sec_pltn_num_fres;
410   const sframe_frame_row_entry *sec_pltn_fres[SFRAME_PLTN_MAX_NUM_FRES];
411 };
412 
413 struct elf_x86_lazy_plt_layout
414 {
415   /* The first entry in a lazy procedure linkage table looks like this.  */
416   const bfd_byte *plt0_entry;
417   unsigned int plt0_entry_size;		 /* Size of PLT0 entry.  */
418 
419   /* Later entries in a lazy procedure linkage table look like this.  */
420   const bfd_byte *plt_entry;
421   unsigned int plt_entry_size;		/* Size of each PLT entry.  */
422 
423   /* The TLSDESC entry in a lazy procedure linkage table looks like
424      this.  This is for x86-64 only.  */
425   const bfd_byte *plt_tlsdesc_entry;
426   unsigned int plt_tlsdesc_entry_size;	 /* Size of TLSDESC entry.  */
427 
428   /* Offsets into the TLSDESC entry that are to be replaced with
429      GOT+8 and GOT+TDG.  These are for x86-64 only.  */
430   unsigned int plt_tlsdesc_got1_offset;
431   unsigned int plt_tlsdesc_got2_offset;
432 
433   /* Offset of the end of the PC-relative instructions containing
434      plt_tlsdesc_got1_offset and plt_tlsdesc_got2_offset.  These
435      are for x86-64 only.  */
436   unsigned int plt_tlsdesc_got1_insn_end;
437   unsigned int plt_tlsdesc_got2_insn_end;
438 
439   /* Offsets into plt0_entry that are to be replaced with GOT[1] and
440      GOT[2].  */
441   unsigned int plt0_got1_offset;
442   unsigned int plt0_got2_offset;
443 
444   /* Offset of the end of the PC-relative instruction containing
445      plt0_got2_offset.  This is for x86-64 only.  */
446   unsigned int plt0_got2_insn_end;
447 
448   /* Offsets into plt_entry that are to be replaced with...  */
449   unsigned int plt_got_offset;    /* ... address of this symbol in .got. */
450   unsigned int plt_reloc_offset;  /* ... offset into relocation table. */
451   unsigned int plt_plt_offset;    /* ... offset to start of .plt. */
452 
453   /* Length of the PC-relative instruction containing plt_got_offset.
454      This is used for x86-64 only.  */
455   unsigned int plt_got_insn_size;
456 
457   /* Offset of the end of the PC-relative jump to plt0_entry.  This is
458      used for x86-64 only.  */
459   unsigned int plt_plt_insn_end;
460 
461   /* Offset into plt_entry where the initial value of the GOT entry
462      points.  */
463   unsigned int plt_lazy_offset;
464 
465   /* The first entry in a PIC lazy procedure linkage table looks like
466      this.  */
467   const bfd_byte *pic_plt0_entry;
468 
469   /* Subsequent entries in a PIC lazy procedure linkage table look
470      like this.  */
471   const bfd_byte *pic_plt_entry;
472 
473   /* .eh_frame covering the lazy .plt section.  */
474   const bfd_byte *eh_frame_plt;
475   unsigned int eh_frame_plt_size;
476 };
477 
478 struct elf_x86_non_lazy_plt_layout
479 {
480   /* Entries in a non-lazy procedure linkage table look like this.  */
481   const bfd_byte *plt_entry;
482   /* Entries in a PIC non-lazy procedure linkage table look like this.
483      This is only used for i386 where absolute PLT and PIC PLT are
484      different.  */
485   const bfd_byte *pic_plt_entry;
486 
487   unsigned int plt_entry_size;		/* Size of each PLT entry.  */
488 
489   /* Offsets into plt_entry that are to be replaced with...  */
490   unsigned int plt_got_offset;    /* ... address of this symbol in .got. */
491 
492   /* Length of the PC-relative instruction containing plt_got_offset.
493      This is used for x86-64 only.  */
494   unsigned int plt_got_insn_size;
495 
496   /* .eh_frame covering the non-lazy .plt section.  */
497   const bfd_byte *eh_frame_plt;
498   unsigned int eh_frame_plt_size;
499 };
500 
501 struct elf_x86_plt_layout
502 {
503   /* The first entry in a lazy procedure linkage table looks like this.  */
504   const bfd_byte *plt0_entry;
505   /* Entries in a procedure linkage table look like this.  */
506   const bfd_byte *plt_entry;
507   unsigned int plt_entry_size;		/* Size of each PLT entry.  */
508 
509   /* 1 has PLT0.  */
510   unsigned int has_plt0;
511 
512   /* Offsets into plt_entry that are to be replaced with...  */
513   unsigned int plt_got_offset;    /* ... address of this symbol in .got. */
514 
515   /* Length of the PC-relative instruction containing plt_got_offset.
516      This is only used for x86-64.  */
517   unsigned int plt_got_insn_size;
518 
519   /* Alignment of the .iplt section.  */
520   unsigned int iplt_alignment;
521 
522   /* .eh_frame covering the .plt section.  */
523   const bfd_byte *eh_frame_plt;
524   unsigned int eh_frame_plt_size;
525 };
526 
527 /* Values in tls_type of x86 ELF linker hash entry.  */
528 #define GOT_UNKNOWN	0
529 #define GOT_NORMAL	1
530 #define GOT_TLS_GD	2
531 #define GOT_TLS_IE	4
532 #define GOT_TLS_IE_POS	5
533 #define GOT_TLS_IE_NEG	6
534 #define GOT_TLS_IE_BOTH 7
535 #define GOT_TLS_GDESC	8
536 #define GOT_ABS		9
537 #define GOT_TLS_GD_BOTH_P(type)	\
538   ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
539 #define GOT_TLS_GD_P(type) \
540   ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
541 #define GOT_TLS_GDESC_P(type) \
542   ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
543 #define GOT_TLS_GD_ANY_P(type) \
544   (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
545 
546 #define elf_x86_hash_entry(ent) \
547   ((struct elf_x86_link_hash_entry *)(ent))
548 
549 /* Information of an input relocation used to compute its contribution
550    to the DT_RELR section size.  */
551 
552 struct elf_x86_relative_reloc_record
553 {
554   /* The original relocation info.  */
555   Elf_Internal_Rela rel;
556   /* The input or the GOT section where the relocation is applied.  */
557   asection *sec;
558   /* Local symbol info.  NULL for global symbol.  */
559   Elf_Internal_Sym *sym;
560   union
561     {
562       /* Section where the local symbol is defined.  */
563       asection *sym_sec;
564       /* Global symbol hash.  */
565       struct elf_link_hash_entry *h;
566     } u;
567   /* The offset into the output section where the relative relocation
568      will be applied at run-time.  */
569   bfd_vma offset;
570   /* The run-time address.  */
571   bfd_vma address;
572 };
573 
574 struct elf_x86_relative_reloc_data
575 {
576   bfd_size_type count;
577   bfd_size_type size;
578   struct elf_x86_relative_reloc_record *data;
579 };
580 
581 /* DT_RELR bitmap.  */
582 struct elf_dt_relr_bitmap
583 {
584   bfd_size_type count;
585   bfd_size_type size;
586   union
587     {
588       /* 32-bit bitmap.  */
589       uint32_t *elf32;
590       /* 64-bit bitmap.  */
591       uint64_t *elf64;
592     } u;
593 };
594 
595 /* x86 ELF linker hash table.  */
596 
597 struct elf_x86_link_hash_table
598 {
599   struct elf_link_hash_table elf;
600 
601   /* Short-cuts to get to dynamic linker sections.  */
602   asection *interp;
603   asection *plt_eh_frame;
604   asection *plt_second;
605   asection *plt_second_eh_frame;
606   asection *plt_got;
607   asection *plt_got_eh_frame;
608 
609   sframe_encoder_ctx *plt_cfe_ctx;
610   asection *plt_sframe;
611   sframe_encoder_ctx *plt_second_cfe_ctx;
612   asection *plt_second_sframe;
613 
614   /* Parameters describing PLT generation, lazy or non-lazy.  */
615   struct elf_x86_plt_layout plt;
616 
617   /* Parameters describing lazy PLT generation.  */
618   const struct elf_x86_lazy_plt_layout *lazy_plt;
619 
620   /* Parameters describing non-lazy PLT generation.  */
621   const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
622 
623   /* The .sframe helper object for .plt section.
624      This is used for x86-64 only.  */
625   const struct elf_x86_sframe_plt *sframe_plt;
626 
627   union
628   {
629     bfd_signed_vma refcount;
630     bfd_vma offset;
631   } tls_ld_or_ldm_got;
632 
633   /* The amount of space used by the jump slots in the GOT.  */
634   bfd_vma sgotplt_jump_table_size;
635 
636   /* _TLS_MODULE_BASE_ symbol.  */
637   struct bfd_link_hash_entry *tls_module_base;
638 
639   /* Used by local STT_GNU_IFUNC symbols.  */
640   htab_t loc_hash_table;
641   void * loc_hash_memory;
642 
643   /* The index of the next R_X86_64_JUMP_SLOT entry in .rela.plt.  */
644   bfd_vma next_jump_slot_index;
645   /* The index of the next R_X86_64_IRELATIVE entry in .rela.plt.  */
646   bfd_vma next_irelative_index;
647 
648   /* The (unloaded but important) .rel.plt.unloaded section on VxWorks.
649      This is used for i386 only.  */
650   asection *srelplt2;
651 
652   /* The index of the next unused R_386_TLS_DESC slot in .rel.plt.  This
653      is only used for i386.  */
654   bfd_vma next_tls_desc_index;
655 
656   /* DT_RELR bitmap.  */
657   struct elf_dt_relr_bitmap dt_relr_bitmap;
658 
659   /* Relative relocation data.  */
660   struct elf_x86_relative_reloc_data relative_reloc;
661 
662   /* Unaligned relative relocation data.  */
663   struct elf_x86_relative_reloc_data unaligned_relative_reloc;
664 
665   /* Number of relative reloc generation pass.  */
666   unsigned int generate_relative_reloc_pass;
667 
668    /* Value used to fill the unused bytes of the first PLT entry.  This
669       is only used for i386.  */
670   bfd_byte plt0_pad_byte;
671 
672   /* TRUE if GOT is referenced.  */
673   unsigned int got_referenced : 1;
674 
675   /* TRUE if PLT is PC-relative.  PLT in PDE and PC-relative PLT in PIE
676      can be used as function address.
677 
678      NB: i386 has non-PIC PLT and PIC PLT.  Only non-PIC PLT in PDE can
679      be used as function address.  PIC PLT in PIE can't be used as
680      function address.  */
681   unsigned int pcrel_plt : 1;
682 
683   bfd_vma (*r_info) (bfd_vma, bfd_vma);
684   bfd_vma (*r_sym) (bfd_vma);
685   bool (*is_reloc_section) (const char *);
686   unsigned int sizeof_reloc;
687   unsigned int got_entry_size;
688   unsigned int pointer_r_type;
689   unsigned int relative_r_type;
690   int dynamic_interpreter_size;
691   const char *dynamic_interpreter;
692   const char *tls_get_addr;
693   const char *relative_r_name;
694   void (*elf_append_reloc) (bfd *, asection *, Elf_Internal_Rela *);
695   void (*elf_write_addend) (bfd *, uint64_t, void *);
696   void (*elf_write_addend_in_got) (bfd *, uint64_t, void *);
697 
698   /* Options passed from the linker.  */
699   struct elf_linker_x86_params *params;
700 };
701 
702 struct elf_x86_init_table
703 {
704   /* The lazy PLT layout.  */
705   const struct elf_x86_lazy_plt_layout *lazy_plt;
706 
707   /* The non-lazy PLT layout.  */
708   const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
709 
710   /* The lazy PLT layout for IBT.  */
711   const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
712 
713   /* The non-lazy PLT layout for IBT.  */
714   const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
715 
716   /* The .sframe helper object for lazy .plt section.
717      This is used for x86-64 only.  */
718   const struct elf_x86_sframe_plt *sframe_lazy_plt;
719 
720   /* The .sframe helper object for non-lazy .plt section.
721      This is used for x86-64 only.  */
722   const struct elf_x86_sframe_plt *sframe_non_lazy_plt;
723 
724   /* The .sframe helper object for lazy IBT .plt section.
725      This is used for x86-64 only.  */
726   const struct elf_x86_sframe_plt *sframe_lazy_ibt_plt;
727 
728   /* The .sframe helper object for non-lazy IBT .plt section.
729      This is used for x86-64 only.  */
730   const struct elf_x86_sframe_plt *sframe_non_lazy_ibt_plt;
731 
732   bfd_byte plt0_pad_byte;
733 
734   bfd_vma (*r_info) (bfd_vma, bfd_vma);
735   bfd_vma (*r_sym) (bfd_vma);
736 };
737 
738 struct elf_x86_obj_tdata
739 {
740   struct elf_obj_tdata root;
741 
742   /* tls_type for each local got entry.  */
743   char *local_got_tls_type;
744 
745   /* GOTPLT entries for TLS descriptors.  */
746   bfd_vma *local_tlsdesc_gotent;
747 
748   /* R_*_RELATIVE relocation in GOT for this local symbol has been
749      processed.  */
750   char *relative_reloc_done;
751 };
752 
753 enum elf_x86_plt_type
754 {
755   plt_non_lazy = 0,
756   plt_lazy = 1 << 0,
757   plt_pic = 1 << 1,
758   plt_second = 1 << 2,
759   plt_unknown = -1
760 };
761 
762 struct elf_x86_plt
763 {
764   const char *name;
765   asection *sec;
766   bfd_byte *contents;
767   enum elf_x86_plt_type type;
768   unsigned int plt_got_offset;
769   unsigned int plt_entry_size;
770   unsigned int plt_got_insn_size;	/* Only used for x86-64.  */
771   long count;
772 };
773 
774 /* Set if a relocation is converted from a GOTPCREL relocation.  */
775 #define R_X86_64_converted_reloc_bit (1 << 7)
776 
777 #define elf_x86_tdata(abfd) \
778   ((struct elf_x86_obj_tdata *) (abfd)->tdata.any)
779 
780 #define elf_x86_local_got_tls_type(abfd) \
781   (elf_x86_tdata (abfd)->local_got_tls_type)
782 
783 #define elf_x86_local_tlsdesc_gotent(abfd) \
784   (elf_x86_tdata (abfd)->local_tlsdesc_gotent)
785 
786 #define elf_x86_relative_reloc_done(abfd) \
787   (elf_x86_tdata (abfd)->relative_reloc_done)
788 
789 #define elf_x86_compute_jump_table_size(htab) \
790   ((htab)->elf.srelplt->reloc_count * (htab)->got_entry_size)
791 
792 #define is_x86_elf(bfd, htab)				\
793   (bfd_get_flavour (bfd) == bfd_target_elf_flavour	\
794    && elf_tdata (bfd) != NULL				\
795    && elf_object_id (bfd) == (htab)->elf.hash_table_id)
796 
797 /* Rename some of the generic section flags to better document how they
798    are used here.  */
799 #define check_relocs_failed	sec_flg0
800 #define relative_reloc_packed	sec_flg1
801 
802 extern bool _bfd_x86_elf_mkobject
803   (bfd *);
804 
805 extern void _bfd_x86_elf_set_tls_module_base
806   (struct bfd_link_info *);
807 
808 extern bfd_vma _bfd_x86_elf_dtpoff_base
809   (struct bfd_link_info *);
810 
811 extern bool _bfd_x86_elf_readonly_dynrelocs
812   (struct elf_link_hash_entry *, void *);
813 
814 extern struct elf_link_hash_entry * _bfd_elf_x86_get_local_sym_hash
815   (struct elf_x86_link_hash_table *, bfd *, const Elf_Internal_Rela *,
816    bool);
817 
818 extern hashval_t _bfd_x86_elf_local_htab_hash
819   (const void *);
820 
821 extern int _bfd_x86_elf_local_htab_eq
822   (const void *, const void *);
823 
824 extern struct bfd_hash_entry * _bfd_x86_elf_link_hash_newfunc
825   (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
826 
827 extern struct bfd_link_hash_table * _bfd_x86_elf_link_hash_table_create
828   (bfd *);
829 
830 extern int _bfd_x86_elf_compare_relocs
831   (const void *, const void *);
832 
833 extern bool _bfd_x86_elf_link_check_relocs
834   (bfd *, struct bfd_link_info *);
835 
836 extern bool _bfd_x86_elf_check_relocs
837   (bfd *, struct bfd_link_info *, asection *,
838    const Elf_Internal_Rela *);
839 
840 extern bool _bfd_x86_elf_link_relax_section
841   (bfd *, asection *, struct bfd_link_info *, bool *);
842 
843 extern bool _bfd_elf_x86_size_relative_relocs
844   (struct bfd_link_info *, bool *);
845 
846 extern bool _bfd_elf_x86_finish_relative_relocs
847   (struct bfd_link_info *);
848 
849 extern void _bfd_elf32_write_addend (bfd *, uint64_t, void *);
850 extern void _bfd_elf64_write_addend (bfd *, uint64_t, void *);
851 
852 extern bool _bfd_elf_x86_valid_reloc_p
853   (asection *, struct bfd_link_info *, struct elf_x86_link_hash_table *,
854    const Elf_Internal_Rela *, struct elf_link_hash_entry *,
855    Elf_Internal_Sym *, Elf_Internal_Shdr *, bool *);
856 
857 extern bool _bfd_x86_elf_size_dynamic_sections
858   (bfd *, struct bfd_link_info *);
859 
860 extern struct elf_x86_link_hash_table *_bfd_x86_elf_finish_dynamic_sections
861   (bfd *, struct bfd_link_info *);
862 
863 extern bool _bfd_x86_elf_always_size_sections
864   (bfd *, struct bfd_link_info *);
865 
866 extern void _bfd_x86_elf_merge_symbol_attribute
867   (struct elf_link_hash_entry *, unsigned int, bool, bool);
868 
869 extern void _bfd_x86_elf_copy_indirect_symbol
870   (struct bfd_link_info *, struct elf_link_hash_entry *,
871    struct elf_link_hash_entry *);
872 
873 extern bool _bfd_x86_elf_fixup_symbol
874   (struct bfd_link_info *, struct elf_link_hash_entry *);
875 
876 extern bool _bfd_x86_elf_hash_symbol
877   (struct elf_link_hash_entry *);
878 
879 extern bool _bfd_x86_elf_adjust_dynamic_symbol
880   (struct bfd_link_info *, struct elf_link_hash_entry *);
881 
882 extern void _bfd_x86_elf_hide_symbol
883   (struct bfd_link_info *, struct elf_link_hash_entry *, bool);
884 
885 extern bool _bfd_x86_elf_link_symbol_references_local
886   (struct bfd_link_info *, struct elf_link_hash_entry *);
887 
888 extern asection * _bfd_x86_elf_gc_mark_hook
889   (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
890    struct elf_link_hash_entry *, Elf_Internal_Sym *);
891 
892 extern long _bfd_x86_elf_get_synthetic_symtab
893   (bfd *, long, long, bfd_vma, struct elf_x86_plt [], asymbol **,
894    asymbol **);
895 
896 extern enum elf_property_kind _bfd_x86_elf_parse_gnu_properties
897   (bfd *, unsigned int, bfd_byte *, unsigned int);
898 
899 extern bool _bfd_x86_elf_merge_gnu_properties
900   (struct bfd_link_info *, bfd *, bfd *, elf_property *, elf_property *);
901 
902 extern void _bfd_x86_elf_link_fixup_gnu_properties
903   (struct bfd_link_info *, elf_property_list **);
904 
905 extern bfd * _bfd_x86_elf_link_setup_gnu_properties
906   (struct bfd_link_info *, struct elf_x86_init_table *);
907 
908 extern void _bfd_x86_elf_link_fixup_ifunc_symbol
909   (struct bfd_link_info *, struct elf_x86_link_hash_table *,
910    struct elf_link_hash_entry *, Elf_Internal_Sym *sym);
911 
912 extern void _bfd_x86_elf_link_report_relative_reloc
913   (struct bfd_link_info *, asection *, struct elf_link_hash_entry *,
914    Elf_Internal_Sym *, const char *, const void *);
915 
916 #define bfd_elf64_mkobject \
917   _bfd_x86_elf_mkobject
918 #define bfd_elf32_mkobject \
919   _bfd_x86_elf_mkobject
920 #define bfd_elf64_bfd_link_hash_table_create \
921   _bfd_x86_elf_link_hash_table_create
922 #define bfd_elf32_bfd_link_hash_table_create \
923   _bfd_x86_elf_link_hash_table_create
924 #define bfd_elf64_bfd_link_check_relocs	\
925   _bfd_x86_elf_link_check_relocs
926 #define bfd_elf32_bfd_link_check_relocs \
927   _bfd_x86_elf_link_check_relocs
928 #define bfd_elf32_bfd_relax_section \
929   _bfd_x86_elf_link_relax_section
930 #define bfd_elf64_bfd_relax_section \
931   _bfd_x86_elf_link_relax_section
932 
933 #define elf_backend_check_relocs \
934   _bfd_x86_elf_check_relocs
935 #define elf_backend_size_dynamic_sections \
936   _bfd_x86_elf_size_dynamic_sections
937 #define elf_backend_merge_symbol_attribute \
938   _bfd_x86_elf_merge_symbol_attribute
939 #define elf_backend_copy_indirect_symbol \
940   _bfd_x86_elf_copy_indirect_symbol
941 #define elf_backend_fixup_symbol \
942   _bfd_x86_elf_fixup_symbol
943 #define elf_backend_hash_symbol \
944   _bfd_x86_elf_hash_symbol
945 #define elf_backend_adjust_dynamic_symbol \
946   _bfd_x86_elf_adjust_dynamic_symbol
947 #define elf_backend_gc_mark_hook \
948   _bfd_x86_elf_gc_mark_hook
949 #define elf_backend_omit_section_dynsym \
950   _bfd_elf_omit_section_dynsym_all
951 #define elf_backend_parse_gnu_properties \
952   _bfd_x86_elf_parse_gnu_properties
953 #define elf_backend_merge_gnu_properties \
954   _bfd_x86_elf_merge_gnu_properties
955 #define elf_backend_fixup_gnu_properties \
956   _bfd_x86_elf_link_fixup_gnu_properties
957 #define elf_backend_size_relative_relocs \
958   _bfd_elf_x86_size_relative_relocs
959 #define elf_backend_finish_relative_relocs \
960   _bfd_elf_x86_finish_relative_relocs
961 
962 #define ELF_P_ALIGN ELF_MINPAGESIZE
963 
964 /* Allocate x86 GOT info for local symbols.  */
965 
966 static inline bool
967 elf_x86_allocate_local_got_info (bfd *abfd, bfd_size_type count)
968 {
969   bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
970   if (local_got_refcounts == NULL)
971     {
972       bfd_size_type size = count * (sizeof (bfd_signed_vma)
973 				    + sizeof (bfd_vma)
974 				    + 2 * sizeof(char));
975       local_got_refcounts = (bfd_signed_vma *) bfd_zalloc (abfd, size);
976       if (local_got_refcounts == NULL)
977 	return false;
978       elf_local_got_refcounts (abfd) = local_got_refcounts;
979       elf_x86_local_tlsdesc_gotent (abfd) =
980 	(bfd_vma *) (local_got_refcounts + count);
981       elf_x86_local_got_tls_type (abfd) =
982 	(char *) (local_got_refcounts + 2 * count);
983       elf_x86_relative_reloc_done (abfd) =
984 	((char *) (local_got_refcounts + 2 * count)) + count;
985     }
986   return true;
987 }
988