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