1 /* X86-64 specific support for 64-bit ELF 2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 3 Free Software Foundation, Inc. 4 Contributed by Jan Hubicka <jh@suse.cz>. 5 6 This file is part of BFD, the Binary File Descriptor library. 7 8 This program is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 3 of the License, or 11 (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the Free Software 20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 21 MA 02110-1301, USA. */ 22 23 #include "sysdep.h" 24 #include "bfd.h" 25 #include "bfdlink.h" 26 #include "libbfd.h" 27 #include "elf-bfd.h" 28 #include "bfd_stdint.h" 29 30 #include "elf/x86-64.h" 31 32 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */ 33 #define MINUS_ONE (~ (bfd_vma) 0) 34 35 /* The relocation "howto" table. Order of fields: 36 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow, 37 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */ 38 static reloc_howto_type x86_64_elf_howto_table[] = 39 { 40 HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont, 41 bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000, 42 FALSE), 43 HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, 44 bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE, 45 FALSE), 46 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed, 47 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff, 48 TRUE), 49 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed, 50 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff, 51 FALSE), 52 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed, 53 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff, 54 TRUE), 55 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 56 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff, 57 FALSE), 58 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, 59 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE, 60 MINUS_ONE, FALSE), 61 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, 62 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE, 63 MINUS_ONE, FALSE), 64 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, 65 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE, 66 MINUS_ONE, FALSE), 67 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed, 68 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff, 69 0xffffffff, TRUE), 70 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned, 71 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff, 72 FALSE), 73 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed, 74 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff, 75 FALSE), 76 HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, 77 bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE), 78 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield, 79 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE), 80 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 81 bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE), 82 HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed, 83 bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE), 84 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, 85 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE, 86 MINUS_ONE, FALSE), 87 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, 88 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE, 89 MINUS_ONE, FALSE), 90 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, 91 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE, 92 MINUS_ONE, FALSE), 93 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed, 94 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff, 95 0xffffffff, TRUE), 96 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed, 97 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff, 98 0xffffffff, TRUE), 99 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed, 100 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff, 101 0xffffffff, FALSE), 102 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed, 103 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff, 104 0xffffffff, TRUE), 105 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed, 106 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff, 107 0xffffffff, FALSE), 108 HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield, 109 bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE, 110 TRUE), 111 HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, 112 bfd_elf_generic_reloc, "R_X86_64_GOTOFF64", 113 FALSE, MINUS_ONE, MINUS_ONE, FALSE), 114 HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed, 115 bfd_elf_generic_reloc, "R_X86_64_GOTPC32", 116 FALSE, 0xffffffff, 0xffffffff, TRUE), 117 HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed, 118 bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE, 119 FALSE), 120 HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed, 121 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE, 122 MINUS_ONE, TRUE), 123 HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed, 124 bfd_elf_generic_reloc, "R_X86_64_GOTPC64", 125 FALSE, MINUS_ONE, MINUS_ONE, TRUE), 126 HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed, 127 bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE, 128 MINUS_ONE, FALSE), 129 HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed, 130 bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE, 131 MINUS_ONE, FALSE), 132 EMPTY_HOWTO (32), 133 EMPTY_HOWTO (33), 134 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0, 135 complain_overflow_bitfield, bfd_elf_generic_reloc, 136 "R_X86_64_GOTPC32_TLSDESC", 137 FALSE, 0xffffffff, 0xffffffff, TRUE), 138 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0, 139 complain_overflow_dont, bfd_elf_generic_reloc, 140 "R_X86_64_TLSDESC_CALL", 141 FALSE, 0, 0, FALSE), 142 HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0, 143 complain_overflow_bitfield, bfd_elf_generic_reloc, 144 "R_X86_64_TLSDESC", 145 FALSE, MINUS_ONE, MINUS_ONE, FALSE), 146 147 /* We have a gap in the reloc numbers here. 148 R_X86_64_standard counts the number up to this point, and 149 R_X86_64_vt_offset is the value to subtract from a reloc type of 150 R_X86_64_GNU_VT* to form an index into this table. */ 151 #define R_X86_64_standard (R_X86_64_TLSDESC + 1) 152 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard) 153 154 /* GNU extension to record C++ vtable hierarchy. */ 155 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont, 156 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE), 157 158 /* GNU extension to record C++ vtable member usage. */ 159 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont, 160 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0, 161 FALSE) 162 }; 163 164 /* Map BFD relocs to the x86_64 elf relocs. */ 165 struct elf_reloc_map 166 { 167 bfd_reloc_code_real_type bfd_reloc_val; 168 unsigned char elf_reloc_val; 169 }; 170 171 static const struct elf_reloc_map x86_64_reloc_map[] = 172 { 173 { BFD_RELOC_NONE, R_X86_64_NONE, }, 174 { BFD_RELOC_64, R_X86_64_64, }, 175 { BFD_RELOC_32_PCREL, R_X86_64_PC32, }, 176 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,}, 177 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,}, 178 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, }, 179 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, }, 180 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, }, 181 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, }, 182 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, }, 183 { BFD_RELOC_32, R_X86_64_32, }, 184 { BFD_RELOC_X86_64_32S, R_X86_64_32S, }, 185 { BFD_RELOC_16, R_X86_64_16, }, 186 { BFD_RELOC_16_PCREL, R_X86_64_PC16, }, 187 { BFD_RELOC_8, R_X86_64_8, }, 188 { BFD_RELOC_8_PCREL, R_X86_64_PC8, }, 189 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, }, 190 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, }, 191 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, }, 192 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, }, 193 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, }, 194 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, }, 195 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, }, 196 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, }, 197 { BFD_RELOC_64_PCREL, R_X86_64_PC64, }, 198 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, }, 199 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, }, 200 { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, }, 201 { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, }, 202 { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, }, 203 { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, }, 204 { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, }, 205 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, }, 206 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, }, 207 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, }, 208 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, }, 209 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, }, 210 }; 211 212 static reloc_howto_type * 213 elf64_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type) 214 { 215 unsigned i; 216 217 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT 218 || r_type >= (unsigned int) R_X86_64_max) 219 { 220 if (r_type >= (unsigned int) R_X86_64_standard) 221 { 222 (*_bfd_error_handler) (_("%B: invalid relocation type %d"), 223 abfd, (int) r_type); 224 r_type = R_X86_64_NONE; 225 } 226 i = r_type; 227 } 228 else 229 i = r_type - (unsigned int) R_X86_64_vt_offset; 230 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type); 231 return &x86_64_elf_howto_table[i]; 232 } 233 234 /* Given a BFD reloc type, return a HOWTO structure. */ 235 static reloc_howto_type * 236 elf64_x86_64_reloc_type_lookup (bfd *abfd, 237 bfd_reloc_code_real_type code) 238 { 239 unsigned int i; 240 241 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map); 242 i++) 243 { 244 if (x86_64_reloc_map[i].bfd_reloc_val == code) 245 return elf64_x86_64_rtype_to_howto (abfd, 246 x86_64_reloc_map[i].elf_reloc_val); 247 } 248 return 0; 249 } 250 251 static reloc_howto_type * 252 elf64_x86_64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, 253 const char *r_name) 254 { 255 unsigned int i; 256 257 for (i = 0; 258 i < (sizeof (x86_64_elf_howto_table) 259 / sizeof (x86_64_elf_howto_table[0])); 260 i++) 261 if (x86_64_elf_howto_table[i].name != NULL 262 && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0) 263 return &x86_64_elf_howto_table[i]; 264 265 return NULL; 266 } 267 268 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */ 269 270 static void 271 elf64_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr, 272 Elf_Internal_Rela *dst) 273 { 274 unsigned r_type; 275 276 r_type = ELF64_R_TYPE (dst->r_info); 277 cache_ptr->howto = elf64_x86_64_rtype_to_howto (abfd, r_type); 278 BFD_ASSERT (r_type == cache_ptr->howto->type); 279 } 280 281 /* Support for core dump NOTE sections. */ 282 static bfd_boolean 283 elf64_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) 284 { 285 int offset; 286 size_t size; 287 288 switch (note->descsz) 289 { 290 default: 291 return FALSE; 292 293 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */ 294 /* pr_cursig */ 295 elf_tdata (abfd)->core_signal 296 = bfd_get_16 (abfd, note->descdata + 12); 297 298 /* pr_pid */ 299 elf_tdata (abfd)->core_pid 300 = bfd_get_32 (abfd, note->descdata + 32); 301 302 /* pr_reg */ 303 offset = 112; 304 size = 216; 305 306 break; 307 } 308 309 /* Make a ".reg/999" section. */ 310 return _bfd_elfcore_make_pseudosection (abfd, ".reg", 311 size, note->descpos + offset); 312 } 313 314 static bfd_boolean 315 elf64_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) 316 { 317 switch (note->descsz) 318 { 319 default: 320 return FALSE; 321 322 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */ 323 elf_tdata (abfd)->core_program 324 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16); 325 elf_tdata (abfd)->core_command 326 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80); 327 } 328 329 /* Note that for some reason, a spurious space is tacked 330 onto the end of the args in some (at least one anyway) 331 implementations, so strip it off if it exists. */ 332 333 { 334 char *command = elf_tdata (abfd)->core_command; 335 int n = strlen (command); 336 337 if (0 < n && command[n - 1] == ' ') 338 command[n - 1] = '\0'; 339 } 340 341 return TRUE; 342 } 343 344 /* Functions for the x86-64 ELF linker. */ 345 346 /* The name of the dynamic interpreter. This is put in the .interp 347 section. */ 348 349 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1" 350 351 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid 352 copying dynamic variables from a shared lib into an app's dynbss 353 section, and instead use a dynamic relocation to point into the 354 shared lib. */ 355 #define ELIMINATE_COPY_RELOCS 1 356 357 /* The size in bytes of an entry in the global offset table. */ 358 359 #define GOT_ENTRY_SIZE 8 360 361 /* The size in bytes of an entry in the procedure linkage table. */ 362 363 #define PLT_ENTRY_SIZE 16 364 365 /* The first entry in a procedure linkage table looks like this. See the 366 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */ 367 368 static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] = 369 { 370 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */ 371 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */ 372 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */ 373 }; 374 375 /* Subsequent entries in a procedure linkage table look like this. */ 376 377 static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] = 378 { 379 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */ 380 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */ 381 0x68, /* pushq immediate */ 382 0, 0, 0, 0, /* replaced with index into relocation table. */ 383 0xe9, /* jmp relative */ 384 0, 0, 0, 0 /* replaced with offset to start of .plt0. */ 385 }; 386 387 /* The x86-64 linker needs to keep track of the number of relocs that 388 it decides to copy as dynamic relocs in check_relocs for each symbol. 389 This is so that it can later discard them if they are found to be 390 unnecessary. We store the information in a field extending the 391 regular ELF linker hash table. */ 392 393 struct elf64_x86_64_dyn_relocs 394 { 395 /* Next section. */ 396 struct elf64_x86_64_dyn_relocs *next; 397 398 /* The input section of the reloc. */ 399 asection *sec; 400 401 /* Total number of relocs copied for the input section. */ 402 bfd_size_type count; 403 404 /* Number of pc-relative relocs copied for the input section. */ 405 bfd_size_type pc_count; 406 }; 407 408 /* x86-64 ELF linker hash entry. */ 409 410 struct elf64_x86_64_link_hash_entry 411 { 412 struct elf_link_hash_entry elf; 413 414 /* Track dynamic relocs copied for this symbol. */ 415 struct elf64_x86_64_dyn_relocs *dyn_relocs; 416 417 #define GOT_UNKNOWN 0 418 #define GOT_NORMAL 1 419 #define GOT_TLS_GD 2 420 #define GOT_TLS_IE 3 421 #define GOT_TLS_GDESC 4 422 #define GOT_TLS_GD_BOTH_P(type) \ 423 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC)) 424 #define GOT_TLS_GD_P(type) \ 425 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type)) 426 #define GOT_TLS_GDESC_P(type) \ 427 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type)) 428 #define GOT_TLS_GD_ANY_P(type) \ 429 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type)) 430 unsigned char tls_type; 431 432 /* Offset of the GOTPLT entry reserved for the TLS descriptor, 433 starting at the end of the jump table. */ 434 bfd_vma tlsdesc_got; 435 }; 436 437 #define elf64_x86_64_hash_entry(ent) \ 438 ((struct elf64_x86_64_link_hash_entry *)(ent)) 439 440 struct elf64_x86_64_obj_tdata 441 { 442 struct elf_obj_tdata root; 443 444 /* tls_type for each local got entry. */ 445 char *local_got_tls_type; 446 447 /* GOTPLT entries for TLS descriptors. */ 448 bfd_vma *local_tlsdesc_gotent; 449 }; 450 451 #define elf64_x86_64_tdata(abfd) \ 452 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any) 453 454 #define elf64_x86_64_local_got_tls_type(abfd) \ 455 (elf64_x86_64_tdata (abfd)->local_got_tls_type) 456 457 #define elf64_x86_64_local_tlsdesc_gotent(abfd) \ 458 (elf64_x86_64_tdata (abfd)->local_tlsdesc_gotent) 459 460 #define is_x86_64_elf(bfd) \ 461 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \ 462 && elf_tdata (bfd) != NULL \ 463 && elf_object_id (bfd) == X86_64_ELF_TDATA) 464 465 static bfd_boolean 466 elf64_x86_64_mkobject (bfd *abfd) 467 { 468 return bfd_elf_allocate_object (abfd, sizeof (struct elf64_x86_64_obj_tdata), 469 X86_64_ELF_TDATA); 470 } 471 472 /* x86-64 ELF linker hash table. */ 473 474 struct elf64_x86_64_link_hash_table 475 { 476 struct elf_link_hash_table elf; 477 478 /* Short-cuts to get to dynamic linker sections. */ 479 asection *sgot; 480 asection *sgotplt; 481 asection *srelgot; 482 asection *splt; 483 asection *srelplt; 484 asection *sdynbss; 485 asection *srelbss; 486 487 /* The offset into splt of the PLT entry for the TLS descriptor 488 resolver. Special values are 0, if not necessary (or not found 489 to be necessary yet), and -1 if needed but not determined 490 yet. */ 491 bfd_vma tlsdesc_plt; 492 /* The offset into sgot of the GOT entry used by the PLT entry 493 above. */ 494 bfd_vma tlsdesc_got; 495 496 union { 497 bfd_signed_vma refcount; 498 bfd_vma offset; 499 } tls_ld_got; 500 501 /* The amount of space used by the jump slots in the GOT. */ 502 bfd_vma sgotplt_jump_table_size; 503 504 /* Small local sym to section mapping cache. */ 505 struct sym_sec_cache sym_sec; 506 507 /* _TLS_MODULE_BASE_ symbol. */ 508 struct bfd_link_hash_entry *tls_module_base; 509 }; 510 511 /* Get the x86-64 ELF linker hash table from a link_info structure. */ 512 513 #define elf64_x86_64_hash_table(p) \ 514 ((struct elf64_x86_64_link_hash_table *) ((p)->hash)) 515 516 #define elf64_x86_64_compute_jump_table_size(htab) \ 517 ((htab)->srelplt->reloc_count * GOT_ENTRY_SIZE) 518 519 /* Create an entry in an x86-64 ELF linker hash table. */ 520 521 static struct bfd_hash_entry * 522 link_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table, 523 const char *string) 524 { 525 /* Allocate the structure if it has not already been allocated by a 526 subclass. */ 527 if (entry == NULL) 528 { 529 entry = bfd_hash_allocate (table, 530 sizeof (struct elf64_x86_64_link_hash_entry)); 531 if (entry == NULL) 532 return entry; 533 } 534 535 /* Call the allocation method of the superclass. */ 536 entry = _bfd_elf_link_hash_newfunc (entry, table, string); 537 if (entry != NULL) 538 { 539 struct elf64_x86_64_link_hash_entry *eh; 540 541 eh = (struct elf64_x86_64_link_hash_entry *) entry; 542 eh->dyn_relocs = NULL; 543 eh->tls_type = GOT_UNKNOWN; 544 eh->tlsdesc_got = (bfd_vma) -1; 545 } 546 547 return entry; 548 } 549 550 /* Create an X86-64 ELF linker hash table. */ 551 552 static struct bfd_link_hash_table * 553 elf64_x86_64_link_hash_table_create (bfd *abfd) 554 { 555 struct elf64_x86_64_link_hash_table *ret; 556 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table); 557 558 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt); 559 if (ret == NULL) 560 return NULL; 561 562 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc, 563 sizeof (struct elf64_x86_64_link_hash_entry))) 564 { 565 free (ret); 566 return NULL; 567 } 568 569 ret->sgot = NULL; 570 ret->sgotplt = NULL; 571 ret->srelgot = NULL; 572 ret->splt = NULL; 573 ret->srelplt = NULL; 574 ret->sdynbss = NULL; 575 ret->srelbss = NULL; 576 ret->sym_sec.abfd = NULL; 577 ret->tlsdesc_plt = 0; 578 ret->tlsdesc_got = 0; 579 ret->tls_ld_got.refcount = 0; 580 ret->sgotplt_jump_table_size = 0; 581 ret->tls_module_base = NULL; 582 583 return &ret->elf.root; 584 } 585 586 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up 587 shortcuts to them in our hash table. */ 588 589 static bfd_boolean 590 create_got_section (bfd *dynobj, struct bfd_link_info *info) 591 { 592 struct elf64_x86_64_link_hash_table *htab; 593 594 if (! _bfd_elf_create_got_section (dynobj, info)) 595 return FALSE; 596 597 htab = elf64_x86_64_hash_table (info); 598 htab->sgot = bfd_get_section_by_name (dynobj, ".got"); 599 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt"); 600 if (!htab->sgot || !htab->sgotplt) 601 abort (); 602 603 htab->srelgot = bfd_make_section_with_flags (dynobj, ".rela.got", 604 (SEC_ALLOC | SEC_LOAD 605 | SEC_HAS_CONTENTS 606 | SEC_IN_MEMORY 607 | SEC_LINKER_CREATED 608 | SEC_READONLY)); 609 if (htab->srelgot == NULL 610 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3)) 611 return FALSE; 612 return TRUE; 613 } 614 615 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and 616 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our 617 hash table. */ 618 619 static bfd_boolean 620 elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info) 621 { 622 struct elf64_x86_64_link_hash_table *htab; 623 624 htab = elf64_x86_64_hash_table (info); 625 if (!htab->sgot && !create_got_section (dynobj, info)) 626 return FALSE; 627 628 if (!_bfd_elf_create_dynamic_sections (dynobj, info)) 629 return FALSE; 630 631 htab->splt = bfd_get_section_by_name (dynobj, ".plt"); 632 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt"); 633 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss"); 634 if (!info->shared) 635 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss"); 636 637 if (!htab->splt || !htab->srelplt || !htab->sdynbss 638 || (!info->shared && !htab->srelbss)) 639 abort (); 640 641 return TRUE; 642 } 643 644 /* Copy the extra info we tack onto an elf_link_hash_entry. */ 645 646 static void 647 elf64_x86_64_copy_indirect_symbol (struct bfd_link_info *info, 648 struct elf_link_hash_entry *dir, 649 struct elf_link_hash_entry *ind) 650 { 651 struct elf64_x86_64_link_hash_entry *edir, *eind; 652 653 edir = (struct elf64_x86_64_link_hash_entry *) dir; 654 eind = (struct elf64_x86_64_link_hash_entry *) ind; 655 656 if (eind->dyn_relocs != NULL) 657 { 658 if (edir->dyn_relocs != NULL) 659 { 660 struct elf64_x86_64_dyn_relocs **pp; 661 struct elf64_x86_64_dyn_relocs *p; 662 663 /* Add reloc counts against the indirect sym to the direct sym 664 list. Merge any entries against the same section. */ 665 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; ) 666 { 667 struct elf64_x86_64_dyn_relocs *q; 668 669 for (q = edir->dyn_relocs; q != NULL; q = q->next) 670 if (q->sec == p->sec) 671 { 672 q->pc_count += p->pc_count; 673 q->count += p->count; 674 *pp = p->next; 675 break; 676 } 677 if (q == NULL) 678 pp = &p->next; 679 } 680 *pp = edir->dyn_relocs; 681 } 682 683 edir->dyn_relocs = eind->dyn_relocs; 684 eind->dyn_relocs = NULL; 685 } 686 687 if (ind->root.type == bfd_link_hash_indirect 688 && dir->got.refcount <= 0) 689 { 690 edir->tls_type = eind->tls_type; 691 eind->tls_type = GOT_UNKNOWN; 692 } 693 694 if (ELIMINATE_COPY_RELOCS 695 && ind->root.type != bfd_link_hash_indirect 696 && dir->dynamic_adjusted) 697 { 698 /* If called to transfer flags for a weakdef during processing 699 of elf_adjust_dynamic_symbol, don't copy non_got_ref. 700 We clear it ourselves for ELIMINATE_COPY_RELOCS. */ 701 dir->ref_dynamic |= ind->ref_dynamic; 702 dir->ref_regular |= ind->ref_regular; 703 dir->ref_regular_nonweak |= ind->ref_regular_nonweak; 704 dir->needs_plt |= ind->needs_plt; 705 dir->pointer_equality_needed |= ind->pointer_equality_needed; 706 } 707 else 708 _bfd_elf_link_hash_copy_indirect (info, dir, ind); 709 } 710 711 static bfd_boolean 712 elf64_x86_64_elf_object_p (bfd *abfd) 713 { 714 /* Set the right machine number for an x86-64 elf64 file. */ 715 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64); 716 return TRUE; 717 } 718 719 typedef union 720 { 721 unsigned char c[2]; 722 uint16_t i; 723 } 724 x86_64_opcode16; 725 726 typedef union 727 { 728 unsigned char c[4]; 729 uint32_t i; 730 } 731 x86_64_opcode32; 732 733 /* Return TRUE if the TLS access code sequence support transition 734 from R_TYPE. */ 735 736 static bfd_boolean 737 elf64_x86_64_check_tls_transition (bfd *abfd, asection *sec, 738 bfd_byte *contents, 739 Elf_Internal_Shdr *symtab_hdr, 740 struct elf_link_hash_entry **sym_hashes, 741 unsigned int r_type, 742 const Elf_Internal_Rela *rel, 743 const Elf_Internal_Rela *relend) 744 { 745 unsigned int val; 746 unsigned long r_symndx; 747 struct elf_link_hash_entry *h; 748 bfd_vma offset; 749 750 /* Get the section contents. */ 751 if (contents == NULL) 752 { 753 if (elf_section_data (sec)->this_hdr.contents != NULL) 754 contents = elf_section_data (sec)->this_hdr.contents; 755 else 756 { 757 /* FIXME: How to better handle error condition? */ 758 if (!bfd_malloc_and_get_section (abfd, sec, &contents)) 759 return FALSE; 760 761 /* Cache the section contents for elf_link_input_bfd. */ 762 elf_section_data (sec)->this_hdr.contents = contents; 763 } 764 } 765 766 offset = rel->r_offset; 767 switch (r_type) 768 { 769 case R_X86_64_TLSGD: 770 case R_X86_64_TLSLD: 771 if ((rel + 1) >= relend) 772 return FALSE; 773 774 if (r_type == R_X86_64_TLSGD) 775 { 776 /* Check transition from GD access model. Only 777 .byte 0x66; leaq foo@tlsgd(%rip), %rdi 778 .word 0x6666; rex64; call __tls_get_addr 779 can transit to different access model. */ 780 781 static x86_64_opcode32 leaq = { { 0x66, 0x48, 0x8d, 0x3d } }, 782 call = { { 0x66, 0x66, 0x48, 0xe8 } }; 783 if (offset < 4 784 || (offset + 12) > sec->size 785 || bfd_get_32 (abfd, contents + offset - 4) != leaq.i 786 || bfd_get_32 (abfd, contents + offset + 4) != call.i) 787 return FALSE; 788 } 789 else 790 { 791 /* Check transition from LD access model. Only 792 leaq foo@tlsld(%rip), %rdi; 793 call __tls_get_addr 794 can transit to different access model. */ 795 796 static x86_64_opcode32 ld = { { 0x48, 0x8d, 0x3d, 0xe8 } }; 797 x86_64_opcode32 op; 798 799 if (offset < 3 || (offset + 9) > sec->size) 800 return FALSE; 801 802 op.i = bfd_get_32 (abfd, contents + offset - 3); 803 op.c[3] = bfd_get_8 (abfd, contents + offset + 4); 804 if (op.i != ld.i) 805 return FALSE; 806 } 807 808 r_symndx = ELF64_R_SYM (rel[1].r_info); 809 if (r_symndx < symtab_hdr->sh_info) 810 return FALSE; 811 812 h = sym_hashes[r_symndx - symtab_hdr->sh_info]; 813 return (h != NULL 814 && h->root.root.string != NULL 815 && (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PC32 816 || ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32) 817 && (strcmp (h->root.root.string, "__tls_get_addr") == 0)); 818 819 case R_X86_64_GOTTPOFF: 820 /* Check transition from IE access model: 821 movq foo@gottpoff(%rip), %reg 822 addq foo@gottpoff(%rip), %reg 823 */ 824 825 if (offset < 3 || (offset + 4) > sec->size) 826 return FALSE; 827 828 val = bfd_get_8 (abfd, contents + offset - 3); 829 if (val != 0x48 && val != 0x4c) 830 return FALSE; 831 832 val = bfd_get_8 (abfd, contents + offset - 2); 833 if (val != 0x8b && val != 0x03) 834 return FALSE; 835 836 val = bfd_get_8 (abfd, contents + offset - 1); 837 return (val & 0xc7) == 5; 838 839 case R_X86_64_GOTPC32_TLSDESC: 840 /* Check transition from GDesc access model: 841 leaq x@tlsdesc(%rip), %rax 842 843 Make sure it's a leaq adding rip to a 32-bit offset 844 into any register, although it's probably almost always 845 going to be rax. */ 846 847 if (offset < 3 || (offset + 4) > sec->size) 848 return FALSE; 849 850 val = bfd_get_8 (abfd, contents + offset - 3); 851 if ((val & 0xfb) != 0x48) 852 return FALSE; 853 854 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d) 855 return FALSE; 856 857 val = bfd_get_8 (abfd, contents + offset - 1); 858 return (val & 0xc7) == 0x05; 859 860 case R_X86_64_TLSDESC_CALL: 861 /* Check transition from GDesc access model: 862 call *x@tlsdesc(%rax) 863 */ 864 if (offset + 2 <= sec->size) 865 { 866 /* Make sure that it's a call *x@tlsdesc(%rax). */ 867 static x86_64_opcode16 call = { { 0xff, 0x10 } }; 868 return bfd_get_16 (abfd, contents + offset) == call.i; 869 } 870 871 return FALSE; 872 873 default: 874 abort (); 875 } 876 } 877 878 /* Return TRUE if the TLS access transition is OK or no transition 879 will be performed. Update R_TYPE if there is a transition. */ 880 881 static bfd_boolean 882 elf64_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd, 883 asection *sec, bfd_byte *contents, 884 Elf_Internal_Shdr *symtab_hdr, 885 struct elf_link_hash_entry **sym_hashes, 886 unsigned int *r_type, int tls_type, 887 const Elf_Internal_Rela *rel, 888 const Elf_Internal_Rela *relend, 889 struct elf_link_hash_entry *h) 890 { 891 unsigned int from_type = *r_type; 892 unsigned int to_type = from_type; 893 bfd_boolean check = TRUE; 894 895 switch (from_type) 896 { 897 case R_X86_64_TLSGD: 898 case R_X86_64_GOTPC32_TLSDESC: 899 case R_X86_64_TLSDESC_CALL: 900 case R_X86_64_GOTTPOFF: 901 if (!info->shared) 902 { 903 if (h == NULL) 904 to_type = R_X86_64_TPOFF32; 905 else 906 to_type = R_X86_64_GOTTPOFF; 907 } 908 909 /* When we are called from elf64_x86_64_relocate_section, 910 CONTENTS isn't NULL and there may be additional transitions 911 based on TLS_TYPE. */ 912 if (contents != NULL) 913 { 914 unsigned int new_to_type = to_type; 915 916 if (!info->shared 917 && h != NULL 918 && h->dynindx == -1 919 && tls_type == GOT_TLS_IE) 920 new_to_type = R_X86_64_TPOFF32; 921 922 if (to_type == R_X86_64_TLSGD 923 || to_type == R_X86_64_GOTPC32_TLSDESC 924 || to_type == R_X86_64_TLSDESC_CALL) 925 { 926 if (tls_type == GOT_TLS_IE) 927 new_to_type = R_X86_64_GOTTPOFF; 928 } 929 930 /* We checked the transition before when we were called from 931 elf64_x86_64_check_relocs. We only want to check the new 932 transition which hasn't been checked before. */ 933 check = new_to_type != to_type && from_type == to_type; 934 to_type = new_to_type; 935 } 936 937 break; 938 939 case R_X86_64_TLSLD: 940 if (!info->shared) 941 to_type = R_X86_64_TPOFF32; 942 break; 943 944 default: 945 return TRUE; 946 } 947 948 /* Return TRUE if there is no transition. */ 949 if (from_type == to_type) 950 return TRUE; 951 952 /* Check if the transition can be performed. */ 953 if (check 954 && ! elf64_x86_64_check_tls_transition (abfd, sec, contents, 955 symtab_hdr, sym_hashes, 956 from_type, rel, relend)) 957 { 958 reloc_howto_type *from, *to; 959 960 from = elf64_x86_64_rtype_to_howto (abfd, from_type); 961 to = elf64_x86_64_rtype_to_howto (abfd, to_type); 962 963 (*_bfd_error_handler) 964 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx " 965 "in section `%A' failed"), 966 abfd, sec, from->name, to->name, 967 h ? h->root.root.string : "a local symbol", 968 (unsigned long) rel->r_offset); 969 bfd_set_error (bfd_error_bad_value); 970 return FALSE; 971 } 972 973 *r_type = to_type; 974 return TRUE; 975 } 976 977 /* Look through the relocs for a section during the first phase, and 978 calculate needed space in the global offset table, procedure 979 linkage table, and dynamic reloc sections. */ 980 981 static bfd_boolean 982 elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info, 983 asection *sec, 984 const Elf_Internal_Rela *relocs) 985 { 986 struct elf64_x86_64_link_hash_table *htab; 987 Elf_Internal_Shdr *symtab_hdr; 988 struct elf_link_hash_entry **sym_hashes; 989 const Elf_Internal_Rela *rel; 990 const Elf_Internal_Rela *rel_end; 991 asection *sreloc; 992 993 if (info->relocatable) 994 return TRUE; 995 996 BFD_ASSERT (is_x86_64_elf (abfd)); 997 998 htab = elf64_x86_64_hash_table (info); 999 symtab_hdr = &elf_symtab_hdr (abfd); 1000 sym_hashes = elf_sym_hashes (abfd); 1001 1002 sreloc = NULL; 1003 1004 rel_end = relocs + sec->reloc_count; 1005 for (rel = relocs; rel < rel_end; rel++) 1006 { 1007 unsigned int r_type; 1008 unsigned long r_symndx; 1009 struct elf_link_hash_entry *h; 1010 1011 r_symndx = ELF64_R_SYM (rel->r_info); 1012 r_type = ELF64_R_TYPE (rel->r_info); 1013 1014 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr)) 1015 { 1016 (*_bfd_error_handler) (_("%B: bad symbol index: %d"), 1017 abfd, r_symndx); 1018 return FALSE; 1019 } 1020 1021 if (r_symndx < symtab_hdr->sh_info) 1022 h = NULL; 1023 else 1024 { 1025 h = sym_hashes[r_symndx - symtab_hdr->sh_info]; 1026 while (h->root.type == bfd_link_hash_indirect 1027 || h->root.type == bfd_link_hash_warning) 1028 h = (struct elf_link_hash_entry *) h->root.u.i.link; 1029 } 1030 1031 if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL, 1032 symtab_hdr, sym_hashes, 1033 &r_type, GOT_UNKNOWN, 1034 rel, rel_end, h)) 1035 return FALSE; 1036 1037 switch (r_type) 1038 { 1039 case R_X86_64_TLSLD: 1040 htab->tls_ld_got.refcount += 1; 1041 goto create_got; 1042 1043 case R_X86_64_TPOFF32: 1044 if (info->shared) 1045 { 1046 (*_bfd_error_handler) 1047 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"), 1048 abfd, 1049 x86_64_elf_howto_table[r_type].name, 1050 (h) ? h->root.root.string : "a local symbol"); 1051 bfd_set_error (bfd_error_bad_value); 1052 return FALSE; 1053 } 1054 break; 1055 1056 case R_X86_64_GOTTPOFF: 1057 if (info->shared) 1058 info->flags |= DF_STATIC_TLS; 1059 /* Fall through */ 1060 1061 case R_X86_64_GOT32: 1062 case R_X86_64_GOTPCREL: 1063 case R_X86_64_TLSGD: 1064 case R_X86_64_GOT64: 1065 case R_X86_64_GOTPCREL64: 1066 case R_X86_64_GOTPLT64: 1067 case R_X86_64_GOTPC32_TLSDESC: 1068 case R_X86_64_TLSDESC_CALL: 1069 /* This symbol requires a global offset table entry. */ 1070 { 1071 int tls_type, old_tls_type; 1072 1073 switch (r_type) 1074 { 1075 default: tls_type = GOT_NORMAL; break; 1076 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break; 1077 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break; 1078 case R_X86_64_GOTPC32_TLSDESC: 1079 case R_X86_64_TLSDESC_CALL: 1080 tls_type = GOT_TLS_GDESC; break; 1081 } 1082 1083 if (h != NULL) 1084 { 1085 if (r_type == R_X86_64_GOTPLT64) 1086 { 1087 /* This relocation indicates that we also need 1088 a PLT entry, as this is a function. We don't need 1089 a PLT entry for local symbols. */ 1090 h->needs_plt = 1; 1091 h->plt.refcount += 1; 1092 } 1093 h->got.refcount += 1; 1094 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type; 1095 } 1096 else 1097 { 1098 bfd_signed_vma *local_got_refcounts; 1099 1100 /* This is a global offset table entry for a local symbol. */ 1101 local_got_refcounts = elf_local_got_refcounts (abfd); 1102 if (local_got_refcounts == NULL) 1103 { 1104 bfd_size_type size; 1105 1106 size = symtab_hdr->sh_info; 1107 size *= sizeof (bfd_signed_vma) 1108 + sizeof (bfd_vma) + sizeof (char); 1109 local_got_refcounts = ((bfd_signed_vma *) 1110 bfd_zalloc (abfd, size)); 1111 if (local_got_refcounts == NULL) 1112 return FALSE; 1113 elf_local_got_refcounts (abfd) = local_got_refcounts; 1114 elf64_x86_64_local_tlsdesc_gotent (abfd) 1115 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info); 1116 elf64_x86_64_local_got_tls_type (abfd) 1117 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info); 1118 } 1119 local_got_refcounts[r_symndx] += 1; 1120 old_tls_type 1121 = elf64_x86_64_local_got_tls_type (abfd) [r_symndx]; 1122 } 1123 1124 /* If a TLS symbol is accessed using IE at least once, 1125 there is no point to use dynamic model for it. */ 1126 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN 1127 && (! GOT_TLS_GD_ANY_P (old_tls_type) 1128 || tls_type != GOT_TLS_IE)) 1129 { 1130 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type)) 1131 tls_type = old_tls_type; 1132 else if (GOT_TLS_GD_ANY_P (old_tls_type) 1133 && GOT_TLS_GD_ANY_P (tls_type)) 1134 tls_type |= old_tls_type; 1135 else 1136 { 1137 (*_bfd_error_handler) 1138 (_("%B: '%s' accessed both as normal and thread local symbol"), 1139 abfd, h ? h->root.root.string : "<local>"); 1140 return FALSE; 1141 } 1142 } 1143 1144 if (old_tls_type != tls_type) 1145 { 1146 if (h != NULL) 1147 elf64_x86_64_hash_entry (h)->tls_type = tls_type; 1148 else 1149 elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type; 1150 } 1151 } 1152 /* Fall through */ 1153 1154 case R_X86_64_GOTOFF64: 1155 case R_X86_64_GOTPC32: 1156 case R_X86_64_GOTPC64: 1157 create_got: 1158 if (htab->sgot == NULL) 1159 { 1160 if (htab->elf.dynobj == NULL) 1161 htab->elf.dynobj = abfd; 1162 if (!create_got_section (htab->elf.dynobj, info)) 1163 return FALSE; 1164 } 1165 break; 1166 1167 case R_X86_64_PLT32: 1168 /* This symbol requires a procedure linkage table entry. We 1169 actually build the entry in adjust_dynamic_symbol, 1170 because this might be a case of linking PIC code which is 1171 never referenced by a dynamic object, in which case we 1172 don't need to generate a procedure linkage table entry 1173 after all. */ 1174 1175 /* If this is a local symbol, we resolve it directly without 1176 creating a procedure linkage table entry. */ 1177 if (h == NULL) 1178 continue; 1179 1180 h->needs_plt = 1; 1181 h->plt.refcount += 1; 1182 break; 1183 1184 case R_X86_64_PLTOFF64: 1185 /* This tries to form the 'address' of a function relative 1186 to GOT. For global symbols we need a PLT entry. */ 1187 if (h != NULL) 1188 { 1189 h->needs_plt = 1; 1190 h->plt.refcount += 1; 1191 } 1192 goto create_got; 1193 1194 case R_X86_64_8: 1195 case R_X86_64_16: 1196 case R_X86_64_32: 1197 case R_X86_64_32S: 1198 /* Let's help debug shared library creation. These relocs 1199 cannot be used in shared libs. Don't error out for 1200 sections we don't care about, such as debug sections or 1201 non-constant sections. */ 1202 if (info->shared 1203 && (sec->flags & SEC_ALLOC) != 0 1204 && (sec->flags & SEC_READONLY) != 0) 1205 { 1206 (*_bfd_error_handler) 1207 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"), 1208 abfd, 1209 x86_64_elf_howto_table[r_type].name, 1210 (h) ? h->root.root.string : "a local symbol"); 1211 bfd_set_error (bfd_error_bad_value); 1212 return FALSE; 1213 } 1214 /* Fall through. */ 1215 1216 case R_X86_64_PC8: 1217 case R_X86_64_PC16: 1218 case R_X86_64_PC32: 1219 case R_X86_64_PC64: 1220 case R_X86_64_64: 1221 if (h != NULL && !info->shared) 1222 { 1223 /* If this reloc is in a read-only section, we might 1224 need a copy reloc. We can't check reliably at this 1225 stage whether the section is read-only, as input 1226 sections have not yet been mapped to output sections. 1227 Tentatively set the flag for now, and correct in 1228 adjust_dynamic_symbol. */ 1229 h->non_got_ref = 1; 1230 1231 /* We may need a .plt entry if the function this reloc 1232 refers to is in a shared lib. */ 1233 h->plt.refcount += 1; 1234 if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64) 1235 h->pointer_equality_needed = 1; 1236 } 1237 1238 /* If we are creating a shared library, and this is a reloc 1239 against a global symbol, or a non PC relative reloc 1240 against a local symbol, then we need to copy the reloc 1241 into the shared library. However, if we are linking with 1242 -Bsymbolic, we do not need to copy a reloc against a 1243 global symbol which is defined in an object we are 1244 including in the link (i.e., DEF_REGULAR is set). At 1245 this point we have not seen all the input files, so it is 1246 possible that DEF_REGULAR is not set now but will be set 1247 later (it is never cleared). In case of a weak definition, 1248 DEF_REGULAR may be cleared later by a strong definition in 1249 a shared library. We account for that possibility below by 1250 storing information in the relocs_copied field of the hash 1251 table entry. A similar situation occurs when creating 1252 shared libraries and symbol visibility changes render the 1253 symbol local. 1254 1255 If on the other hand, we are creating an executable, we 1256 may need to keep relocations for symbols satisfied by a 1257 dynamic library if we manage to avoid copy relocs for the 1258 symbol. */ 1259 if ((info->shared 1260 && (sec->flags & SEC_ALLOC) != 0 1261 && (((r_type != R_X86_64_PC8) 1262 && (r_type != R_X86_64_PC16) 1263 && (r_type != R_X86_64_PC32) 1264 && (r_type != R_X86_64_PC64)) 1265 || (h != NULL 1266 && (! SYMBOLIC_BIND (info, h) 1267 || h->root.type == bfd_link_hash_defweak 1268 || !h->def_regular)))) 1269 || (ELIMINATE_COPY_RELOCS 1270 && !info->shared 1271 && (sec->flags & SEC_ALLOC) != 0 1272 && h != NULL 1273 && (h->root.type == bfd_link_hash_defweak 1274 || !h->def_regular))) 1275 { 1276 struct elf64_x86_64_dyn_relocs *p; 1277 struct elf64_x86_64_dyn_relocs **head; 1278 1279 /* We must copy these reloc types into the output file. 1280 Create a reloc section in dynobj and make room for 1281 this reloc. */ 1282 if (sreloc == NULL) 1283 { 1284 const char *name; 1285 bfd *dynobj; 1286 1287 name = (bfd_elf_string_from_elf_section 1288 (abfd, 1289 elf_elfheader (abfd)->e_shstrndx, 1290 elf_section_data (sec)->rel_hdr.sh_name)); 1291 if (name == NULL) 1292 return FALSE; 1293 1294 if (! CONST_STRNEQ (name, ".rela") 1295 || strcmp (bfd_get_section_name (abfd, sec), 1296 name + 5) != 0) 1297 { 1298 (*_bfd_error_handler) 1299 (_("%B: bad relocation section name `%s\'"), 1300 abfd, name); 1301 } 1302 1303 if (htab->elf.dynobj == NULL) 1304 htab->elf.dynobj = abfd; 1305 1306 dynobj = htab->elf.dynobj; 1307 1308 sreloc = bfd_get_section_by_name (dynobj, name); 1309 if (sreloc == NULL) 1310 { 1311 flagword flags; 1312 1313 flags = (SEC_HAS_CONTENTS | SEC_READONLY 1314 | SEC_IN_MEMORY | SEC_LINKER_CREATED); 1315 if ((sec->flags & SEC_ALLOC) != 0) 1316 flags |= SEC_ALLOC | SEC_LOAD; 1317 sreloc = bfd_make_section_with_flags (dynobj, 1318 name, 1319 flags); 1320 if (sreloc == NULL 1321 || ! bfd_set_section_alignment (dynobj, sreloc, 3)) 1322 return FALSE; 1323 } 1324 elf_section_data (sec)->sreloc = sreloc; 1325 } 1326 1327 /* If this is a global symbol, we count the number of 1328 relocations we need for this symbol. */ 1329 if (h != NULL) 1330 { 1331 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs; 1332 } 1333 else 1334 { 1335 void **vpp; 1336 /* Track dynamic relocs needed for local syms too. 1337 We really need local syms available to do this 1338 easily. Oh well. */ 1339 1340 asection *s; 1341 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, 1342 sec, r_symndx); 1343 if (s == NULL) 1344 return FALSE; 1345 1346 /* Beware of type punned pointers vs strict aliasing 1347 rules. */ 1348 vpp = &(elf_section_data (s)->local_dynrel); 1349 head = (struct elf64_x86_64_dyn_relocs **)vpp; 1350 } 1351 1352 p = *head; 1353 if (p == NULL || p->sec != sec) 1354 { 1355 bfd_size_type amt = sizeof *p; 1356 p = ((struct elf64_x86_64_dyn_relocs *) 1357 bfd_alloc (htab->elf.dynobj, amt)); 1358 if (p == NULL) 1359 return FALSE; 1360 p->next = *head; 1361 *head = p; 1362 p->sec = sec; 1363 p->count = 0; 1364 p->pc_count = 0; 1365 } 1366 1367 p->count += 1; 1368 if (r_type == R_X86_64_PC8 1369 || r_type == R_X86_64_PC16 1370 || r_type == R_X86_64_PC32 1371 || r_type == R_X86_64_PC64) 1372 p->pc_count += 1; 1373 } 1374 break; 1375 1376 /* This relocation describes the C++ object vtable hierarchy. 1377 Reconstruct it for later use during GC. */ 1378 case R_X86_64_GNU_VTINHERIT: 1379 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset)) 1380 return FALSE; 1381 break; 1382 1383 /* This relocation describes which C++ vtable entries are actually 1384 used. Record for later use during GC. */ 1385 case R_X86_64_GNU_VTENTRY: 1386 BFD_ASSERT (h != NULL); 1387 if (h != NULL 1388 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend)) 1389 return FALSE; 1390 break; 1391 1392 default: 1393 break; 1394 } 1395 } 1396 1397 return TRUE; 1398 } 1399 1400 /* Return the section that should be marked against GC for a given 1401 relocation. */ 1402 1403 static asection * 1404 elf64_x86_64_gc_mark_hook (asection *sec, 1405 struct bfd_link_info *info, 1406 Elf_Internal_Rela *rel, 1407 struct elf_link_hash_entry *h, 1408 Elf_Internal_Sym *sym) 1409 { 1410 if (h != NULL) 1411 switch (ELF64_R_TYPE (rel->r_info)) 1412 { 1413 case R_X86_64_GNU_VTINHERIT: 1414 case R_X86_64_GNU_VTENTRY: 1415 return NULL; 1416 } 1417 1418 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym); 1419 } 1420 1421 /* Update the got entry reference counts for the section being removed. */ 1422 1423 static bfd_boolean 1424 elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info, 1425 asection *sec, 1426 const Elf_Internal_Rela *relocs) 1427 { 1428 Elf_Internal_Shdr *symtab_hdr; 1429 struct elf_link_hash_entry **sym_hashes; 1430 bfd_signed_vma *local_got_refcounts; 1431 const Elf_Internal_Rela *rel, *relend; 1432 1433 if (info->relocatable) 1434 return TRUE; 1435 1436 elf_section_data (sec)->local_dynrel = NULL; 1437 1438 symtab_hdr = &elf_symtab_hdr (abfd); 1439 sym_hashes = elf_sym_hashes (abfd); 1440 local_got_refcounts = elf_local_got_refcounts (abfd); 1441 1442 relend = relocs + sec->reloc_count; 1443 for (rel = relocs; rel < relend; rel++) 1444 { 1445 unsigned long r_symndx; 1446 unsigned int r_type; 1447 struct elf_link_hash_entry *h = NULL; 1448 1449 r_symndx = ELF64_R_SYM (rel->r_info); 1450 if (r_symndx >= symtab_hdr->sh_info) 1451 { 1452 struct elf64_x86_64_link_hash_entry *eh; 1453 struct elf64_x86_64_dyn_relocs **pp; 1454 struct elf64_x86_64_dyn_relocs *p; 1455 1456 h = sym_hashes[r_symndx - symtab_hdr->sh_info]; 1457 while (h->root.type == bfd_link_hash_indirect 1458 || h->root.type == bfd_link_hash_warning) 1459 h = (struct elf_link_hash_entry *) h->root.u.i.link; 1460 eh = (struct elf64_x86_64_link_hash_entry *) h; 1461 1462 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next) 1463 if (p->sec == sec) 1464 { 1465 /* Everything must go for SEC. */ 1466 *pp = p->next; 1467 break; 1468 } 1469 } 1470 1471 r_type = ELF64_R_TYPE (rel->r_info); 1472 if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL, 1473 symtab_hdr, sym_hashes, 1474 &r_type, GOT_UNKNOWN, 1475 rel, relend, h)) 1476 return FALSE; 1477 1478 switch (r_type) 1479 { 1480 case R_X86_64_TLSLD: 1481 if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0) 1482 elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1; 1483 break; 1484 1485 case R_X86_64_TLSGD: 1486 case R_X86_64_GOTPC32_TLSDESC: 1487 case R_X86_64_TLSDESC_CALL: 1488 case R_X86_64_GOTTPOFF: 1489 case R_X86_64_GOT32: 1490 case R_X86_64_GOTPCREL: 1491 case R_X86_64_GOT64: 1492 case R_X86_64_GOTPCREL64: 1493 case R_X86_64_GOTPLT64: 1494 if (h != NULL) 1495 { 1496 if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0) 1497 h->plt.refcount -= 1; 1498 if (h->got.refcount > 0) 1499 h->got.refcount -= 1; 1500 } 1501 else if (local_got_refcounts != NULL) 1502 { 1503 if (local_got_refcounts[r_symndx] > 0) 1504 local_got_refcounts[r_symndx] -= 1; 1505 } 1506 break; 1507 1508 case R_X86_64_8: 1509 case R_X86_64_16: 1510 case R_X86_64_32: 1511 case R_X86_64_64: 1512 case R_X86_64_32S: 1513 case R_X86_64_PC8: 1514 case R_X86_64_PC16: 1515 case R_X86_64_PC32: 1516 case R_X86_64_PC64: 1517 if (info->shared) 1518 break; 1519 /* Fall thru */ 1520 1521 case R_X86_64_PLT32: 1522 case R_X86_64_PLTOFF64: 1523 if (h != NULL) 1524 { 1525 if (h->plt.refcount > 0) 1526 h->plt.refcount -= 1; 1527 } 1528 break; 1529 1530 default: 1531 break; 1532 } 1533 } 1534 1535 return TRUE; 1536 } 1537 1538 /* Adjust a symbol defined by a dynamic object and referenced by a 1539 regular object. The current definition is in some section of the 1540 dynamic object, but we're not including those sections. We have to 1541 change the definition to something the rest of the link can 1542 understand. */ 1543 1544 static bfd_boolean 1545 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info, 1546 struct elf_link_hash_entry *h) 1547 { 1548 struct elf64_x86_64_link_hash_table *htab; 1549 asection *s; 1550 1551 /* If this is a function, put it in the procedure linkage table. We 1552 will fill in the contents of the procedure linkage table later, 1553 when we know the address of the .got section. */ 1554 if (h->type == STT_FUNC 1555 || h->needs_plt) 1556 { 1557 if (h->plt.refcount <= 0 1558 || SYMBOL_CALLS_LOCAL (info, h) 1559 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT 1560 && h->root.type == bfd_link_hash_undefweak)) 1561 { 1562 /* This case can occur if we saw a PLT32 reloc in an input 1563 file, but the symbol was never referred to by a dynamic 1564 object, or if all references were garbage collected. In 1565 such a case, we don't actually need to build a procedure 1566 linkage table, and we can just do a PC32 reloc instead. */ 1567 h->plt.offset = (bfd_vma) -1; 1568 h->needs_plt = 0; 1569 } 1570 1571 return TRUE; 1572 } 1573 else 1574 /* It's possible that we incorrectly decided a .plt reloc was 1575 needed for an R_X86_64_PC32 reloc to a non-function sym in 1576 check_relocs. We can't decide accurately between function and 1577 non-function syms in check-relocs; Objects loaded later in 1578 the link may change h->type. So fix it now. */ 1579 h->plt.offset = (bfd_vma) -1; 1580 1581 /* If this is a weak symbol, and there is a real definition, the 1582 processor independent code will have arranged for us to see the 1583 real definition first, and we can just use the same value. */ 1584 if (h->u.weakdef != NULL) 1585 { 1586 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined 1587 || h->u.weakdef->root.type == bfd_link_hash_defweak); 1588 h->root.u.def.section = h->u.weakdef->root.u.def.section; 1589 h->root.u.def.value = h->u.weakdef->root.u.def.value; 1590 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc) 1591 h->non_got_ref = h->u.weakdef->non_got_ref; 1592 return TRUE; 1593 } 1594 1595 /* This is a reference to a symbol defined by a dynamic object which 1596 is not a function. */ 1597 1598 /* If we are creating a shared library, we must presume that the 1599 only references to the symbol are via the global offset table. 1600 For such cases we need not do anything here; the relocations will 1601 be handled correctly by relocate_section. */ 1602 if (info->shared) 1603 return TRUE; 1604 1605 /* If there are no references to this symbol that do not use the 1606 GOT, we don't need to generate a copy reloc. */ 1607 if (!h->non_got_ref) 1608 return TRUE; 1609 1610 /* If -z nocopyreloc was given, we won't generate them either. */ 1611 if (info->nocopyreloc) 1612 { 1613 h->non_got_ref = 0; 1614 return TRUE; 1615 } 1616 1617 if (ELIMINATE_COPY_RELOCS) 1618 { 1619 struct elf64_x86_64_link_hash_entry * eh; 1620 struct elf64_x86_64_dyn_relocs *p; 1621 1622 eh = (struct elf64_x86_64_link_hash_entry *) h; 1623 for (p = eh->dyn_relocs; p != NULL; p = p->next) 1624 { 1625 s = p->sec->output_section; 1626 if (s != NULL && (s->flags & SEC_READONLY) != 0) 1627 break; 1628 } 1629 1630 /* If we didn't find any dynamic relocs in read-only sections, then 1631 we'll be keeping the dynamic relocs and avoiding the copy reloc. */ 1632 if (p == NULL) 1633 { 1634 h->non_got_ref = 0; 1635 return TRUE; 1636 } 1637 } 1638 1639 if (h->size == 0) 1640 { 1641 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"), 1642 h->root.root.string); 1643 return TRUE; 1644 } 1645 1646 /* We must allocate the symbol in our .dynbss section, which will 1647 become part of the .bss section of the executable. There will be 1648 an entry for this symbol in the .dynsym section. The dynamic 1649 object will contain position independent code, so all references 1650 from the dynamic object to this symbol will go through the global 1651 offset table. The dynamic linker will use the .dynsym entry to 1652 determine the address it must put in the global offset table, so 1653 both the dynamic object and the regular object will refer to the 1654 same memory location for the variable. */ 1655 1656 htab = elf64_x86_64_hash_table (info); 1657 1658 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker 1659 to copy the initial value out of the dynamic object and into the 1660 runtime process image. */ 1661 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0) 1662 { 1663 htab->srelbss->size += sizeof (Elf64_External_Rela); 1664 h->needs_copy = 1; 1665 } 1666 1667 s = htab->sdynbss; 1668 1669 return _bfd_elf_adjust_dynamic_copy (h, s); 1670 } 1671 1672 /* Allocate space in .plt, .got and associated reloc sections for 1673 dynamic relocs. */ 1674 1675 static bfd_boolean 1676 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf) 1677 { 1678 struct bfd_link_info *info; 1679 struct elf64_x86_64_link_hash_table *htab; 1680 struct elf64_x86_64_link_hash_entry *eh; 1681 struct elf64_x86_64_dyn_relocs *p; 1682 1683 if (h->root.type == bfd_link_hash_indirect) 1684 return TRUE; 1685 1686 if (h->root.type == bfd_link_hash_warning) 1687 h = (struct elf_link_hash_entry *) h->root.u.i.link; 1688 1689 info = (struct bfd_link_info *) inf; 1690 htab = elf64_x86_64_hash_table (info); 1691 1692 if (htab->elf.dynamic_sections_created 1693 && h->plt.refcount > 0) 1694 { 1695 /* Make sure this symbol is output as a dynamic symbol. 1696 Undefined weak syms won't yet be marked as dynamic. */ 1697 if (h->dynindx == -1 1698 && !h->forced_local) 1699 { 1700 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 1701 return FALSE; 1702 } 1703 1704 if (info->shared 1705 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h)) 1706 { 1707 asection *s = htab->splt; 1708 1709 /* If this is the first .plt entry, make room for the special 1710 first entry. */ 1711 if (s->size == 0) 1712 s->size += PLT_ENTRY_SIZE; 1713 1714 h->plt.offset = s->size; 1715 1716 /* If this symbol is not defined in a regular file, and we are 1717 not generating a shared library, then set the symbol to this 1718 location in the .plt. This is required to make function 1719 pointers compare as equal between the normal executable and 1720 the shared library. */ 1721 if (! info->shared 1722 && !h->def_regular) 1723 { 1724 h->root.u.def.section = s; 1725 h->root.u.def.value = h->plt.offset; 1726 } 1727 1728 /* Make room for this entry. */ 1729 s->size += PLT_ENTRY_SIZE; 1730 1731 /* We also need to make an entry in the .got.plt section, which 1732 will be placed in the .got section by the linker script. */ 1733 htab->sgotplt->size += GOT_ENTRY_SIZE; 1734 1735 /* We also need to make an entry in the .rela.plt section. */ 1736 htab->srelplt->size += sizeof (Elf64_External_Rela); 1737 htab->srelplt->reloc_count++; 1738 } 1739 else 1740 { 1741 h->plt.offset = (bfd_vma) -1; 1742 h->needs_plt = 0; 1743 } 1744 } 1745 else 1746 { 1747 h->plt.offset = (bfd_vma) -1; 1748 h->needs_plt = 0; 1749 } 1750 1751 eh = (struct elf64_x86_64_link_hash_entry *) h; 1752 eh->tlsdesc_got = (bfd_vma) -1; 1753 1754 /* If R_X86_64_GOTTPOFF symbol is now local to the binary, 1755 make it a R_X86_64_TPOFF32 requiring no GOT entry. */ 1756 if (h->got.refcount > 0 1757 && !info->shared 1758 && h->dynindx == -1 1759 && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE) 1760 h->got.offset = (bfd_vma) -1; 1761 else if (h->got.refcount > 0) 1762 { 1763 asection *s; 1764 bfd_boolean dyn; 1765 int tls_type = elf64_x86_64_hash_entry (h)->tls_type; 1766 1767 /* Make sure this symbol is output as a dynamic symbol. 1768 Undefined weak syms won't yet be marked as dynamic. */ 1769 if (h->dynindx == -1 1770 && !h->forced_local) 1771 { 1772 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 1773 return FALSE; 1774 } 1775 1776 if (GOT_TLS_GDESC_P (tls_type)) 1777 { 1778 eh->tlsdesc_got = htab->sgotplt->size 1779 - elf64_x86_64_compute_jump_table_size (htab); 1780 htab->sgotplt->size += 2 * GOT_ENTRY_SIZE; 1781 h->got.offset = (bfd_vma) -2; 1782 } 1783 if (! GOT_TLS_GDESC_P (tls_type) 1784 || GOT_TLS_GD_P (tls_type)) 1785 { 1786 s = htab->sgot; 1787 h->got.offset = s->size; 1788 s->size += GOT_ENTRY_SIZE; 1789 if (GOT_TLS_GD_P (tls_type)) 1790 s->size += GOT_ENTRY_SIZE; 1791 } 1792 dyn = htab->elf.dynamic_sections_created; 1793 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol 1794 and two if global. 1795 R_X86_64_GOTTPOFF needs one dynamic relocation. */ 1796 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1) 1797 || tls_type == GOT_TLS_IE) 1798 htab->srelgot->size += sizeof (Elf64_External_Rela); 1799 else if (GOT_TLS_GD_P (tls_type)) 1800 htab->srelgot->size += 2 * sizeof (Elf64_External_Rela); 1801 else if (! GOT_TLS_GDESC_P (tls_type) 1802 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT 1803 || h->root.type != bfd_link_hash_undefweak) 1804 && (info->shared 1805 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))) 1806 htab->srelgot->size += sizeof (Elf64_External_Rela); 1807 if (GOT_TLS_GDESC_P (tls_type)) 1808 { 1809 htab->srelplt->size += sizeof (Elf64_External_Rela); 1810 htab->tlsdesc_plt = (bfd_vma) -1; 1811 } 1812 } 1813 else 1814 h->got.offset = (bfd_vma) -1; 1815 1816 if (eh->dyn_relocs == NULL) 1817 return TRUE; 1818 1819 /* In the shared -Bsymbolic case, discard space allocated for 1820 dynamic pc-relative relocs against symbols which turn out to be 1821 defined in regular objects. For the normal shared case, discard 1822 space for pc-relative relocs that have become local due to symbol 1823 visibility changes. */ 1824 1825 if (info->shared) 1826 { 1827 /* Relocs that use pc_count are those that appear on a call 1828 insn, or certain REL relocs that can generated via assembly. 1829 We want calls to protected symbols to resolve directly to the 1830 function rather than going via the plt. If people want 1831 function pointer comparisons to work as expected then they 1832 should avoid writing weird assembly. */ 1833 if (SYMBOL_CALLS_LOCAL (info, h)) 1834 { 1835 struct elf64_x86_64_dyn_relocs **pp; 1836 1837 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; ) 1838 { 1839 p->count -= p->pc_count; 1840 p->pc_count = 0; 1841 if (p->count == 0) 1842 *pp = p->next; 1843 else 1844 pp = &p->next; 1845 } 1846 } 1847 1848 /* Also discard relocs on undefined weak syms with non-default 1849 visibility. */ 1850 if (eh->dyn_relocs != NULL 1851 && h->root.type == bfd_link_hash_undefweak) 1852 { 1853 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) 1854 eh->dyn_relocs = NULL; 1855 1856 /* Make sure undefined weak symbols are output as a dynamic 1857 symbol in PIEs. */ 1858 else if (h->dynindx == -1 1859 && !h->forced_local) 1860 { 1861 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 1862 return FALSE; 1863 } 1864 } 1865 } 1866 else if (ELIMINATE_COPY_RELOCS) 1867 { 1868 /* For the non-shared case, discard space for relocs against 1869 symbols which turn out to need copy relocs or are not 1870 dynamic. */ 1871 1872 if (!h->non_got_ref 1873 && ((h->def_dynamic 1874 && !h->def_regular) 1875 || (htab->elf.dynamic_sections_created 1876 && (h->root.type == bfd_link_hash_undefweak 1877 || h->root.type == bfd_link_hash_undefined)))) 1878 { 1879 /* Make sure this symbol is output as a dynamic symbol. 1880 Undefined weak syms won't yet be marked as dynamic. */ 1881 if (h->dynindx == -1 1882 && !h->forced_local) 1883 { 1884 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 1885 return FALSE; 1886 } 1887 1888 /* If that succeeded, we know we'll be keeping all the 1889 relocs. */ 1890 if (h->dynindx != -1) 1891 goto keep; 1892 } 1893 1894 eh->dyn_relocs = NULL; 1895 1896 keep: ; 1897 } 1898 1899 /* Finally, allocate space. */ 1900 for (p = eh->dyn_relocs; p != NULL; p = p->next) 1901 { 1902 asection *sreloc = elf_section_data (p->sec)->sreloc; 1903 sreloc->size += p->count * sizeof (Elf64_External_Rela); 1904 } 1905 1906 return TRUE; 1907 } 1908 1909 /* Find any dynamic relocs that apply to read-only sections. */ 1910 1911 static bfd_boolean 1912 readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf) 1913 { 1914 struct elf64_x86_64_link_hash_entry *eh; 1915 struct elf64_x86_64_dyn_relocs *p; 1916 1917 if (h->root.type == bfd_link_hash_warning) 1918 h = (struct elf_link_hash_entry *) h->root.u.i.link; 1919 1920 eh = (struct elf64_x86_64_link_hash_entry *) h; 1921 for (p = eh->dyn_relocs; p != NULL; p = p->next) 1922 { 1923 asection *s = p->sec->output_section; 1924 1925 if (s != NULL && (s->flags & SEC_READONLY) != 0) 1926 { 1927 struct bfd_link_info *info = (struct bfd_link_info *) inf; 1928 1929 if (info->warn_shared_textrel) 1930 (*_bfd_error_handler) 1931 (_("warning: dynamic relocation in readonly section `%s'"), 1932 h->root.root.string); 1933 info->flags |= DF_TEXTREL; 1934 1935 /* Not an error, just cut short the traversal. */ 1936 return FALSE; 1937 } 1938 } 1939 return TRUE; 1940 } 1941 1942 /* Set the sizes of the dynamic sections. */ 1943 1944 static bfd_boolean 1945 elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED, 1946 struct bfd_link_info *info) 1947 { 1948 struct elf64_x86_64_link_hash_table *htab; 1949 bfd *dynobj; 1950 asection *s; 1951 bfd_boolean relocs; 1952 bfd *ibfd; 1953 1954 htab = elf64_x86_64_hash_table (info); 1955 dynobj = htab->elf.dynobj; 1956 if (dynobj == NULL) 1957 abort (); 1958 1959 if (htab->elf.dynamic_sections_created) 1960 { 1961 /* Set the contents of the .interp section to the interpreter. */ 1962 if (info->executable) 1963 { 1964 s = bfd_get_section_by_name (dynobj, ".interp"); 1965 if (s == NULL) 1966 abort (); 1967 s->size = sizeof ELF_DYNAMIC_INTERPRETER; 1968 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; 1969 } 1970 } 1971 1972 /* Set up .got offsets for local syms, and space for local dynamic 1973 relocs. */ 1974 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) 1975 { 1976 bfd_signed_vma *local_got; 1977 bfd_signed_vma *end_local_got; 1978 char *local_tls_type; 1979 bfd_vma *local_tlsdesc_gotent; 1980 bfd_size_type locsymcount; 1981 Elf_Internal_Shdr *symtab_hdr; 1982 asection *srel; 1983 1984 if (! is_x86_64_elf (ibfd)) 1985 continue; 1986 1987 for (s = ibfd->sections; s != NULL; s = s->next) 1988 { 1989 struct elf64_x86_64_dyn_relocs *p; 1990 1991 for (p = (struct elf64_x86_64_dyn_relocs *) 1992 (elf_section_data (s)->local_dynrel); 1993 p != NULL; 1994 p = p->next) 1995 { 1996 if (!bfd_is_abs_section (p->sec) 1997 && bfd_is_abs_section (p->sec->output_section)) 1998 { 1999 /* Input section has been discarded, either because 2000 it is a copy of a linkonce section or due to 2001 linker script /DISCARD/, so we'll be discarding 2002 the relocs too. */ 2003 } 2004 else if (p->count != 0) 2005 { 2006 srel = elf_section_data (p->sec)->sreloc; 2007 srel->size += p->count * sizeof (Elf64_External_Rela); 2008 if ((p->sec->output_section->flags & SEC_READONLY) != 0) 2009 info->flags |= DF_TEXTREL; 2010 2011 } 2012 } 2013 } 2014 2015 local_got = elf_local_got_refcounts (ibfd); 2016 if (!local_got) 2017 continue; 2018 2019 symtab_hdr = &elf_symtab_hdr (ibfd); 2020 locsymcount = symtab_hdr->sh_info; 2021 end_local_got = local_got + locsymcount; 2022 local_tls_type = elf64_x86_64_local_got_tls_type (ibfd); 2023 local_tlsdesc_gotent = elf64_x86_64_local_tlsdesc_gotent (ibfd); 2024 s = htab->sgot; 2025 srel = htab->srelgot; 2026 for (; local_got < end_local_got; 2027 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent) 2028 { 2029 *local_tlsdesc_gotent = (bfd_vma) -1; 2030 if (*local_got > 0) 2031 { 2032 if (GOT_TLS_GDESC_P (*local_tls_type)) 2033 { 2034 *local_tlsdesc_gotent = htab->sgotplt->size 2035 - elf64_x86_64_compute_jump_table_size (htab); 2036 htab->sgotplt->size += 2 * GOT_ENTRY_SIZE; 2037 *local_got = (bfd_vma) -2; 2038 } 2039 if (! GOT_TLS_GDESC_P (*local_tls_type) 2040 || GOT_TLS_GD_P (*local_tls_type)) 2041 { 2042 *local_got = s->size; 2043 s->size += GOT_ENTRY_SIZE; 2044 if (GOT_TLS_GD_P (*local_tls_type)) 2045 s->size += GOT_ENTRY_SIZE; 2046 } 2047 if (info->shared 2048 || GOT_TLS_GD_ANY_P (*local_tls_type) 2049 || *local_tls_type == GOT_TLS_IE) 2050 { 2051 if (GOT_TLS_GDESC_P (*local_tls_type)) 2052 { 2053 htab->srelplt->size += sizeof (Elf64_External_Rela); 2054 htab->tlsdesc_plt = (bfd_vma) -1; 2055 } 2056 if (! GOT_TLS_GDESC_P (*local_tls_type) 2057 || GOT_TLS_GD_P (*local_tls_type)) 2058 srel->size += sizeof (Elf64_External_Rela); 2059 } 2060 } 2061 else 2062 *local_got = (bfd_vma) -1; 2063 } 2064 } 2065 2066 if (htab->tls_ld_got.refcount > 0) 2067 { 2068 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD 2069 relocs. */ 2070 htab->tls_ld_got.offset = htab->sgot->size; 2071 htab->sgot->size += 2 * GOT_ENTRY_SIZE; 2072 htab->srelgot->size += sizeof (Elf64_External_Rela); 2073 } 2074 else 2075 htab->tls_ld_got.offset = -1; 2076 2077 /* Allocate global sym .plt and .got entries, and space for global 2078 sym dynamic relocs. */ 2079 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info); 2080 2081 /* For every jump slot reserved in the sgotplt, reloc_count is 2082 incremented. However, when we reserve space for TLS descriptors, 2083 it's not incremented, so in order to compute the space reserved 2084 for them, it suffices to multiply the reloc count by the jump 2085 slot size. */ 2086 if (htab->srelplt) 2087 htab->sgotplt_jump_table_size 2088 = elf64_x86_64_compute_jump_table_size (htab); 2089 2090 if (htab->tlsdesc_plt) 2091 { 2092 /* If we're not using lazy TLS relocations, don't generate the 2093 PLT and GOT entries they require. */ 2094 if ((info->flags & DF_BIND_NOW)) 2095 htab->tlsdesc_plt = 0; 2096 else 2097 { 2098 htab->tlsdesc_got = htab->sgot->size; 2099 htab->sgot->size += GOT_ENTRY_SIZE; 2100 /* Reserve room for the initial entry. 2101 FIXME: we could probably do away with it in this case. */ 2102 if (htab->splt->size == 0) 2103 htab->splt->size += PLT_ENTRY_SIZE; 2104 htab->tlsdesc_plt = htab->splt->size; 2105 htab->splt->size += PLT_ENTRY_SIZE; 2106 } 2107 } 2108 2109 /* We now have determined the sizes of the various dynamic sections. 2110 Allocate memory for them. */ 2111 relocs = FALSE; 2112 for (s = dynobj->sections; s != NULL; s = s->next) 2113 { 2114 if ((s->flags & SEC_LINKER_CREATED) == 0) 2115 continue; 2116 2117 if (s == htab->splt 2118 || s == htab->sgot 2119 || s == htab->sgotplt 2120 || s == htab->sdynbss) 2121 { 2122 /* Strip this section if we don't need it; see the 2123 comment below. */ 2124 } 2125 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela")) 2126 { 2127 if (s->size != 0 && s != htab->srelplt) 2128 relocs = TRUE; 2129 2130 /* We use the reloc_count field as a counter if we need 2131 to copy relocs into the output file. */ 2132 if (s != htab->srelplt) 2133 s->reloc_count = 0; 2134 } 2135 else 2136 { 2137 /* It's not one of our sections, so don't allocate space. */ 2138 continue; 2139 } 2140 2141 if (s->size == 0) 2142 { 2143 /* If we don't need this section, strip it from the 2144 output file. This is mostly to handle .rela.bss and 2145 .rela.plt. We must create both sections in 2146 create_dynamic_sections, because they must be created 2147 before the linker maps input sections to output 2148 sections. The linker does that before 2149 adjust_dynamic_symbol is called, and it is that 2150 function which decides whether anything needs to go 2151 into these sections. */ 2152 2153 s->flags |= SEC_EXCLUDE; 2154 continue; 2155 } 2156 2157 if ((s->flags & SEC_HAS_CONTENTS) == 0) 2158 continue; 2159 2160 /* Allocate memory for the section contents. We use bfd_zalloc 2161 here in case unused entries are not reclaimed before the 2162 section's contents are written out. This should not happen, 2163 but this way if it does, we get a R_X86_64_NONE reloc instead 2164 of garbage. */ 2165 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size); 2166 if (s->contents == NULL) 2167 return FALSE; 2168 } 2169 2170 if (htab->elf.dynamic_sections_created) 2171 { 2172 /* Add some entries to the .dynamic section. We fill in the 2173 values later, in elf64_x86_64_finish_dynamic_sections, but we 2174 must add the entries now so that we get the correct size for 2175 the .dynamic section. The DT_DEBUG entry is filled in by the 2176 dynamic linker and used by the debugger. */ 2177 #define add_dynamic_entry(TAG, VAL) \ 2178 _bfd_elf_add_dynamic_entry (info, TAG, VAL) 2179 2180 if (info->executable) 2181 { 2182 if (!add_dynamic_entry (DT_DEBUG, 0)) 2183 return FALSE; 2184 } 2185 2186 if (htab->splt->size != 0) 2187 { 2188 if (!add_dynamic_entry (DT_PLTGOT, 0) 2189 || !add_dynamic_entry (DT_PLTRELSZ, 0) 2190 || !add_dynamic_entry (DT_PLTREL, DT_RELA) 2191 || !add_dynamic_entry (DT_JMPREL, 0)) 2192 return FALSE; 2193 2194 if (htab->tlsdesc_plt 2195 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0) 2196 || !add_dynamic_entry (DT_TLSDESC_GOT, 0))) 2197 return FALSE; 2198 } 2199 2200 if (relocs) 2201 { 2202 if (!add_dynamic_entry (DT_RELA, 0) 2203 || !add_dynamic_entry (DT_RELASZ, 0) 2204 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela))) 2205 return FALSE; 2206 2207 /* If any dynamic relocs apply to a read-only section, 2208 then we need a DT_TEXTREL entry. */ 2209 if ((info->flags & DF_TEXTREL) == 0) 2210 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, 2211 (PTR) info); 2212 2213 if ((info->flags & DF_TEXTREL) != 0) 2214 { 2215 if (!add_dynamic_entry (DT_TEXTREL, 0)) 2216 return FALSE; 2217 } 2218 } 2219 } 2220 #undef add_dynamic_entry 2221 2222 return TRUE; 2223 } 2224 2225 static bfd_boolean 2226 elf64_x86_64_always_size_sections (bfd *output_bfd, 2227 struct bfd_link_info *info) 2228 { 2229 asection *tls_sec = elf_hash_table (info)->tls_sec; 2230 2231 if (tls_sec) 2232 { 2233 struct elf_link_hash_entry *tlsbase; 2234 2235 tlsbase = elf_link_hash_lookup (elf_hash_table (info), 2236 "_TLS_MODULE_BASE_", 2237 FALSE, FALSE, FALSE); 2238 2239 if (tlsbase && tlsbase->type == STT_TLS) 2240 { 2241 struct bfd_link_hash_entry *bh = NULL; 2242 const struct elf_backend_data *bed 2243 = get_elf_backend_data (output_bfd); 2244 2245 if (!(_bfd_generic_link_add_one_symbol 2246 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL, 2247 tls_sec, 0, NULL, FALSE, 2248 bed->collect, &bh))) 2249 return FALSE; 2250 2251 elf64_x86_64_hash_table (info)->tls_module_base = bh; 2252 2253 tlsbase = (struct elf_link_hash_entry *)bh; 2254 tlsbase->def_regular = 1; 2255 tlsbase->other = STV_HIDDEN; 2256 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE); 2257 } 2258 } 2259 2260 return TRUE; 2261 } 2262 2263 /* _TLS_MODULE_BASE_ needs to be treated especially when linking 2264 executables. Rather than setting it to the beginning of the TLS 2265 section, we have to set it to the end. This function may be called 2266 multiple times, it is idempotent. */ 2267 2268 static void 2269 set_tls_module_base (struct bfd_link_info *info) 2270 { 2271 struct bfd_link_hash_entry *base; 2272 2273 if (!info->executable) 2274 return; 2275 2276 base = elf64_x86_64_hash_table (info)->tls_module_base; 2277 2278 if (!base) 2279 return; 2280 2281 base->u.def.value = elf_hash_table (info)->tls_size; 2282 } 2283 2284 /* Return the base VMA address which should be subtracted from real addresses 2285 when resolving @dtpoff relocation. 2286 This is PT_TLS segment p_vaddr. */ 2287 2288 static bfd_vma 2289 dtpoff_base (struct bfd_link_info *info) 2290 { 2291 /* If tls_sec is NULL, we should have signalled an error already. */ 2292 if (elf_hash_table (info)->tls_sec == NULL) 2293 return 0; 2294 return elf_hash_table (info)->tls_sec->vma; 2295 } 2296 2297 /* Return the relocation value for @tpoff relocation 2298 if STT_TLS virtual address is ADDRESS. */ 2299 2300 static bfd_vma 2301 tpoff (struct bfd_link_info *info, bfd_vma address) 2302 { 2303 struct elf_link_hash_table *htab = elf_hash_table (info); 2304 2305 /* If tls_segment is NULL, we should have signalled an error already. */ 2306 if (htab->tls_sec == NULL) 2307 return 0; 2308 return address - htab->tls_size - htab->tls_sec->vma; 2309 } 2310 2311 /* Is the instruction before OFFSET in CONTENTS a 32bit relative 2312 branch? */ 2313 2314 static bfd_boolean 2315 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset) 2316 { 2317 /* Opcode Instruction 2318 0xe8 call 2319 0xe9 jump 2320 0x0f 0x8x conditional jump */ 2321 return ((offset > 0 2322 && (contents [offset - 1] == 0xe8 2323 || contents [offset - 1] == 0xe9)) 2324 || (offset > 1 2325 && contents [offset - 2] == 0x0f 2326 && (contents [offset - 1] & 0xf0) == 0x80)); 2327 } 2328 2329 /* Relocate an x86_64 ELF section. */ 2330 2331 static bfd_boolean 2332 elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info, 2333 bfd *input_bfd, asection *input_section, 2334 bfd_byte *contents, Elf_Internal_Rela *relocs, 2335 Elf_Internal_Sym *local_syms, 2336 asection **local_sections) 2337 { 2338 struct elf64_x86_64_link_hash_table *htab; 2339 Elf_Internal_Shdr *symtab_hdr; 2340 struct elf_link_hash_entry **sym_hashes; 2341 bfd_vma *local_got_offsets; 2342 bfd_vma *local_tlsdesc_gotents; 2343 Elf_Internal_Rela *rel; 2344 Elf_Internal_Rela *relend; 2345 2346 BFD_ASSERT (is_x86_64_elf (input_bfd)); 2347 2348 htab = elf64_x86_64_hash_table (info); 2349 symtab_hdr = &elf_symtab_hdr (input_bfd); 2350 sym_hashes = elf_sym_hashes (input_bfd); 2351 local_got_offsets = elf_local_got_offsets (input_bfd); 2352 local_tlsdesc_gotents = elf64_x86_64_local_tlsdesc_gotent (input_bfd); 2353 2354 set_tls_module_base (info); 2355 2356 rel = relocs; 2357 relend = relocs + input_section->reloc_count; 2358 for (; rel < relend; rel++) 2359 { 2360 unsigned int r_type; 2361 reloc_howto_type *howto; 2362 unsigned long r_symndx; 2363 struct elf_link_hash_entry *h; 2364 Elf_Internal_Sym *sym; 2365 asection *sec; 2366 bfd_vma off, offplt; 2367 bfd_vma relocation; 2368 bfd_boolean unresolved_reloc; 2369 bfd_reloc_status_type r; 2370 int tls_type; 2371 2372 r_type = ELF64_R_TYPE (rel->r_info); 2373 if (r_type == (int) R_X86_64_GNU_VTINHERIT 2374 || r_type == (int) R_X86_64_GNU_VTENTRY) 2375 continue; 2376 2377 if (r_type >= R_X86_64_max) 2378 { 2379 bfd_set_error (bfd_error_bad_value); 2380 return FALSE; 2381 } 2382 2383 howto = x86_64_elf_howto_table + r_type; 2384 r_symndx = ELF64_R_SYM (rel->r_info); 2385 h = NULL; 2386 sym = NULL; 2387 sec = NULL; 2388 unresolved_reloc = FALSE; 2389 if (r_symndx < symtab_hdr->sh_info) 2390 { 2391 sym = local_syms + r_symndx; 2392 sec = local_sections[r_symndx]; 2393 2394 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel); 2395 } 2396 else 2397 { 2398 bfd_boolean warned; 2399 2400 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel, 2401 r_symndx, symtab_hdr, sym_hashes, 2402 h, sec, relocation, 2403 unresolved_reloc, warned); 2404 } 2405 2406 if (sec != NULL && elf_discarded_section (sec)) 2407 { 2408 /* For relocs against symbols from removed linkonce sections, 2409 or sections discarded by a linker script, we just want the 2410 section contents zeroed. Avoid any special processing. */ 2411 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset); 2412 rel->r_info = 0; 2413 rel->r_addend = 0; 2414 continue; 2415 } 2416 2417 if (info->relocatable) 2418 continue; 2419 2420 /* When generating a shared object, the relocations handled here are 2421 copied into the output file to be resolved at run time. */ 2422 switch (r_type) 2423 { 2424 asection *base_got; 2425 case R_X86_64_GOT32: 2426 case R_X86_64_GOT64: 2427 /* Relocation is to the entry for this symbol in the global 2428 offset table. */ 2429 case R_X86_64_GOTPCREL: 2430 case R_X86_64_GOTPCREL64: 2431 /* Use global offset table entry as symbol value. */ 2432 case R_X86_64_GOTPLT64: 2433 /* This is the same as GOT64 for relocation purposes, but 2434 indicates the existence of a PLT entry. The difficulty is, 2435 that we must calculate the GOT slot offset from the PLT 2436 offset, if this symbol got a PLT entry (it was global). 2437 Additionally if it's computed from the PLT entry, then that 2438 GOT offset is relative to .got.plt, not to .got. */ 2439 base_got = htab->sgot; 2440 2441 if (htab->sgot == NULL) 2442 abort (); 2443 2444 if (h != NULL) 2445 { 2446 bfd_boolean dyn; 2447 2448 off = h->got.offset; 2449 if (h->needs_plt 2450 && h->plt.offset != (bfd_vma)-1 2451 && off == (bfd_vma)-1) 2452 { 2453 /* We can't use h->got.offset here to save 2454 state, or even just remember the offset, as 2455 finish_dynamic_symbol would use that as offset into 2456 .got. */ 2457 bfd_vma plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1; 2458 off = (plt_index + 3) * GOT_ENTRY_SIZE; 2459 base_got = htab->sgotplt; 2460 } 2461 2462 dyn = htab->elf.dynamic_sections_created; 2463 2464 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h) 2465 || (info->shared 2466 && SYMBOL_REFERENCES_LOCAL (info, h)) 2467 || (ELF_ST_VISIBILITY (h->other) 2468 && h->root.type == bfd_link_hash_undefweak)) 2469 { 2470 /* This is actually a static link, or it is a -Bsymbolic 2471 link and the symbol is defined locally, or the symbol 2472 was forced to be local because of a version file. We 2473 must initialize this entry in the global offset table. 2474 Since the offset must always be a multiple of 8, we 2475 use the least significant bit to record whether we 2476 have initialized it already. 2477 2478 When doing a dynamic link, we create a .rela.got 2479 relocation entry to initialize the value. This is 2480 done in the finish_dynamic_symbol routine. */ 2481 if ((off & 1) != 0) 2482 off &= ~1; 2483 else 2484 { 2485 bfd_put_64 (output_bfd, relocation, 2486 base_got->contents + off); 2487 /* Note that this is harmless for the GOTPLT64 case, 2488 as -1 | 1 still is -1. */ 2489 h->got.offset |= 1; 2490 } 2491 } 2492 else 2493 unresolved_reloc = FALSE; 2494 } 2495 else 2496 { 2497 if (local_got_offsets == NULL) 2498 abort (); 2499 2500 off = local_got_offsets[r_symndx]; 2501 2502 /* The offset must always be a multiple of 8. We use 2503 the least significant bit to record whether we have 2504 already generated the necessary reloc. */ 2505 if ((off & 1) != 0) 2506 off &= ~1; 2507 else 2508 { 2509 bfd_put_64 (output_bfd, relocation, 2510 base_got->contents + off); 2511 2512 if (info->shared) 2513 { 2514 asection *s; 2515 Elf_Internal_Rela outrel; 2516 bfd_byte *loc; 2517 2518 /* We need to generate a R_X86_64_RELATIVE reloc 2519 for the dynamic linker. */ 2520 s = htab->srelgot; 2521 if (s == NULL) 2522 abort (); 2523 2524 outrel.r_offset = (base_got->output_section->vma 2525 + base_got->output_offset 2526 + off); 2527 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE); 2528 outrel.r_addend = relocation; 2529 loc = s->contents; 2530 loc += s->reloc_count++ * sizeof (Elf64_External_Rela); 2531 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); 2532 } 2533 2534 local_got_offsets[r_symndx] |= 1; 2535 } 2536 } 2537 2538 if (off >= (bfd_vma) -2) 2539 abort (); 2540 2541 relocation = base_got->output_section->vma 2542 + base_got->output_offset + off; 2543 if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64) 2544 relocation -= htab->sgotplt->output_section->vma 2545 - htab->sgotplt->output_offset; 2546 2547 break; 2548 2549 case R_X86_64_GOTOFF64: 2550 /* Relocation is relative to the start of the global offset 2551 table. */ 2552 2553 /* Check to make sure it isn't a protected function symbol 2554 for shared library since it may not be local when used 2555 as function address. */ 2556 if (info->shared 2557 && h 2558 && h->def_regular 2559 && h->type == STT_FUNC 2560 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) 2561 { 2562 (*_bfd_error_handler) 2563 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"), 2564 input_bfd, h->root.root.string); 2565 bfd_set_error (bfd_error_bad_value); 2566 return FALSE; 2567 } 2568 2569 /* Note that sgot is not involved in this 2570 calculation. We always want the start of .got.plt. If we 2571 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is 2572 permitted by the ABI, we might have to change this 2573 calculation. */ 2574 relocation -= htab->sgotplt->output_section->vma 2575 + htab->sgotplt->output_offset; 2576 break; 2577 2578 case R_X86_64_GOTPC32: 2579 case R_X86_64_GOTPC64: 2580 /* Use global offset table as symbol value. */ 2581 relocation = htab->sgotplt->output_section->vma 2582 + htab->sgotplt->output_offset; 2583 unresolved_reloc = FALSE; 2584 break; 2585 2586 case R_X86_64_PLTOFF64: 2587 /* Relocation is PLT entry relative to GOT. For local 2588 symbols it's the symbol itself relative to GOT. */ 2589 if (h != NULL 2590 /* See PLT32 handling. */ 2591 && h->plt.offset != (bfd_vma) -1 2592 && htab->splt != NULL) 2593 { 2594 relocation = (htab->splt->output_section->vma 2595 + htab->splt->output_offset 2596 + h->plt.offset); 2597 unresolved_reloc = FALSE; 2598 } 2599 2600 relocation -= htab->sgotplt->output_section->vma 2601 + htab->sgotplt->output_offset; 2602 break; 2603 2604 case R_X86_64_PLT32: 2605 /* Relocation is to the entry for this symbol in the 2606 procedure linkage table. */ 2607 2608 /* Resolve a PLT32 reloc against a local symbol directly, 2609 without using the procedure linkage table. */ 2610 if (h == NULL) 2611 break; 2612 2613 if (h->plt.offset == (bfd_vma) -1 2614 || htab->splt == NULL) 2615 { 2616 /* We didn't make a PLT entry for this symbol. This 2617 happens when statically linking PIC code, or when 2618 using -Bsymbolic. */ 2619 break; 2620 } 2621 2622 relocation = (htab->splt->output_section->vma 2623 + htab->splt->output_offset 2624 + h->plt.offset); 2625 unresolved_reloc = FALSE; 2626 break; 2627 2628 case R_X86_64_PC8: 2629 case R_X86_64_PC16: 2630 case R_X86_64_PC32: 2631 if (info->shared 2632 && (input_section->flags & SEC_ALLOC) != 0 2633 && (input_section->flags & SEC_READONLY) != 0 2634 && h != NULL) 2635 { 2636 bfd_boolean fail = FALSE; 2637 bfd_boolean branch 2638 = (r_type == R_X86_64_PC32 2639 && is_32bit_relative_branch (contents, rel->r_offset)); 2640 2641 if (SYMBOL_REFERENCES_LOCAL (info, h)) 2642 { 2643 /* Symbol is referenced locally. Make sure it is 2644 defined locally or for a branch. */ 2645 fail = !h->def_regular && !branch; 2646 } 2647 else 2648 { 2649 /* Symbol isn't referenced locally. We only allow 2650 branch to symbol with non-default visibility. */ 2651 fail = (!branch 2652 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT); 2653 } 2654 2655 if (fail) 2656 { 2657 const char *fmt; 2658 const char *v; 2659 const char *pic = ""; 2660 2661 switch (ELF_ST_VISIBILITY (h->other)) 2662 { 2663 case STV_HIDDEN: 2664 v = _("hidden symbol"); 2665 break; 2666 case STV_INTERNAL: 2667 v = _("internal symbol"); 2668 break; 2669 case STV_PROTECTED: 2670 v = _("protected symbol"); 2671 break; 2672 default: 2673 v = _("symbol"); 2674 pic = _("; recompile with -fPIC"); 2675 break; 2676 } 2677 2678 if (h->def_regular) 2679 fmt = _("%B: relocation %s against %s `%s' can not be used when making a shared object%s"); 2680 else 2681 fmt = _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s"); 2682 2683 (*_bfd_error_handler) (fmt, input_bfd, 2684 x86_64_elf_howto_table[r_type].name, 2685 v, h->root.root.string, pic); 2686 bfd_set_error (bfd_error_bad_value); 2687 return FALSE; 2688 } 2689 } 2690 /* Fall through. */ 2691 2692 case R_X86_64_8: 2693 case R_X86_64_16: 2694 case R_X86_64_32: 2695 case R_X86_64_PC64: 2696 case R_X86_64_64: 2697 /* FIXME: The ABI says the linker should make sure the value is 2698 the same when it's zeroextended to 64 bit. */ 2699 2700 if ((input_section->flags & SEC_ALLOC) == 0) 2701 break; 2702 2703 if ((info->shared 2704 && (h == NULL 2705 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT 2706 || h->root.type != bfd_link_hash_undefweak) 2707 && ((r_type != R_X86_64_PC8 2708 && r_type != R_X86_64_PC16 2709 && r_type != R_X86_64_PC32 2710 && r_type != R_X86_64_PC64) 2711 || !SYMBOL_CALLS_LOCAL (info, h))) 2712 || (ELIMINATE_COPY_RELOCS 2713 && !info->shared 2714 && h != NULL 2715 && h->dynindx != -1 2716 && !h->non_got_ref 2717 && ((h->def_dynamic 2718 && !h->def_regular) 2719 || h->root.type == bfd_link_hash_undefweak 2720 || h->root.type == bfd_link_hash_undefined))) 2721 { 2722 Elf_Internal_Rela outrel; 2723 bfd_byte *loc; 2724 bfd_boolean skip, relocate; 2725 asection *sreloc; 2726 2727 /* When generating a shared object, these relocations 2728 are copied into the output file to be resolved at run 2729 time. */ 2730 skip = FALSE; 2731 relocate = FALSE; 2732 2733 outrel.r_offset = 2734 _bfd_elf_section_offset (output_bfd, info, input_section, 2735 rel->r_offset); 2736 if (outrel.r_offset == (bfd_vma) -1) 2737 skip = TRUE; 2738 else if (outrel.r_offset == (bfd_vma) -2) 2739 skip = TRUE, relocate = TRUE; 2740 2741 outrel.r_offset += (input_section->output_section->vma 2742 + input_section->output_offset); 2743 2744 if (skip) 2745 memset (&outrel, 0, sizeof outrel); 2746 2747 /* h->dynindx may be -1 if this symbol was marked to 2748 become local. */ 2749 else if (h != NULL 2750 && h->dynindx != -1 2751 && (r_type == R_X86_64_PC8 2752 || r_type == R_X86_64_PC16 2753 || r_type == R_X86_64_PC32 2754 || r_type == R_X86_64_PC64 2755 || !info->shared 2756 || !SYMBOLIC_BIND (info, h) 2757 || !h->def_regular)) 2758 { 2759 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type); 2760 outrel.r_addend = rel->r_addend; 2761 } 2762 else 2763 { 2764 /* This symbol is local, or marked to become local. */ 2765 if (r_type == R_X86_64_64) 2766 { 2767 relocate = TRUE; 2768 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE); 2769 outrel.r_addend = relocation + rel->r_addend; 2770 } 2771 else 2772 { 2773 long sindx; 2774 2775 if (bfd_is_abs_section (sec)) 2776 sindx = 0; 2777 else if (sec == NULL || sec->owner == NULL) 2778 { 2779 bfd_set_error (bfd_error_bad_value); 2780 return FALSE; 2781 } 2782 else 2783 { 2784 asection *osec; 2785 2786 /* We are turning this relocation into one 2787 against a section symbol. It would be 2788 proper to subtract the symbol's value, 2789 osec->vma, from the emitted reloc addend, 2790 but ld.so expects buggy relocs. */ 2791 osec = sec->output_section; 2792 sindx = elf_section_data (osec)->dynindx; 2793 if (sindx == 0) 2794 { 2795 asection *oi = htab->elf.text_index_section; 2796 sindx = elf_section_data (oi)->dynindx; 2797 } 2798 BFD_ASSERT (sindx != 0); 2799 } 2800 2801 outrel.r_info = ELF64_R_INFO (sindx, r_type); 2802 outrel.r_addend = relocation + rel->r_addend; 2803 } 2804 } 2805 2806 sreloc = elf_section_data (input_section)->sreloc; 2807 if (sreloc == NULL) 2808 abort (); 2809 2810 loc = sreloc->contents; 2811 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela); 2812 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); 2813 2814 /* If this reloc is against an external symbol, we do 2815 not want to fiddle with the addend. Otherwise, we 2816 need to include the symbol value so that it becomes 2817 an addend for the dynamic reloc. */ 2818 if (! relocate) 2819 continue; 2820 } 2821 2822 break; 2823 2824 case R_X86_64_TLSGD: 2825 case R_X86_64_GOTPC32_TLSDESC: 2826 case R_X86_64_TLSDESC_CALL: 2827 case R_X86_64_GOTTPOFF: 2828 tls_type = GOT_UNKNOWN; 2829 if (h == NULL && local_got_offsets) 2830 tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx]; 2831 else if (h != NULL) 2832 tls_type = elf64_x86_64_hash_entry (h)->tls_type; 2833 2834 if (! elf64_x86_64_tls_transition (info, input_bfd, 2835 input_section, contents, 2836 symtab_hdr, sym_hashes, 2837 &r_type, tls_type, rel, 2838 relend, h)) 2839 return FALSE; 2840 2841 if (r_type == R_X86_64_TPOFF32) 2842 { 2843 bfd_vma roff = rel->r_offset; 2844 2845 BFD_ASSERT (! unresolved_reloc); 2846 2847 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD) 2848 { 2849 /* GD->LE transition. 2850 .byte 0x66; leaq foo@tlsgd(%rip), %rdi 2851 .word 0x6666; rex64; call __tls_get_addr 2852 Change it into: 2853 movq %fs:0, %rax 2854 leaq foo@tpoff(%rax), %rax */ 2855 memcpy (contents + roff - 4, 2856 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0", 2857 16); 2858 bfd_put_32 (output_bfd, tpoff (info, relocation), 2859 contents + roff + 8); 2860 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */ 2861 rel++; 2862 continue; 2863 } 2864 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC) 2865 { 2866 /* GDesc -> LE transition. 2867 It's originally something like: 2868 leaq x@tlsdesc(%rip), %rax 2869 2870 Change it to: 2871 movl $x@tpoff, %rax 2872 */ 2873 2874 unsigned int val, type, type2; 2875 2876 type = bfd_get_8 (input_bfd, contents + roff - 3); 2877 type2 = bfd_get_8 (input_bfd, contents + roff - 2); 2878 val = bfd_get_8 (input_bfd, contents + roff - 1); 2879 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1), 2880 contents + roff - 3); 2881 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2); 2882 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7), 2883 contents + roff - 1); 2884 bfd_put_32 (output_bfd, tpoff (info, relocation), 2885 contents + roff); 2886 continue; 2887 } 2888 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL) 2889 { 2890 /* GDesc -> LE transition. 2891 It's originally: 2892 call *(%rax) 2893 Turn it into: 2894 xchg %ax,%ax. */ 2895 bfd_put_8 (output_bfd, 0x66, contents + roff); 2896 bfd_put_8 (output_bfd, 0x90, contents + roff + 1); 2897 continue; 2898 } 2899 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF) 2900 { 2901 /* IE->LE transition: 2902 Originally it can be one of: 2903 movq foo@gottpoff(%rip), %reg 2904 addq foo@gottpoff(%rip), %reg 2905 We change it into: 2906 movq $foo, %reg 2907 leaq foo(%reg), %reg 2908 addq $foo, %reg. */ 2909 2910 unsigned int val, type, reg; 2911 2912 val = bfd_get_8 (input_bfd, contents + roff - 3); 2913 type = bfd_get_8 (input_bfd, contents + roff - 2); 2914 reg = bfd_get_8 (input_bfd, contents + roff - 1); 2915 reg >>= 3; 2916 if (type == 0x8b) 2917 { 2918 /* movq */ 2919 if (val == 0x4c) 2920 bfd_put_8 (output_bfd, 0x49, 2921 contents + roff - 3); 2922 bfd_put_8 (output_bfd, 0xc7, 2923 contents + roff - 2); 2924 bfd_put_8 (output_bfd, 0xc0 | reg, 2925 contents + roff - 1); 2926 } 2927 else if (reg == 4) 2928 { 2929 /* addq -> addq - addressing with %rsp/%r12 is 2930 special */ 2931 if (val == 0x4c) 2932 bfd_put_8 (output_bfd, 0x49, 2933 contents + roff - 3); 2934 bfd_put_8 (output_bfd, 0x81, 2935 contents + roff - 2); 2936 bfd_put_8 (output_bfd, 0xc0 | reg, 2937 contents + roff - 1); 2938 } 2939 else 2940 { 2941 /* addq -> leaq */ 2942 if (val == 0x4c) 2943 bfd_put_8 (output_bfd, 0x4d, 2944 contents + roff - 3); 2945 bfd_put_8 (output_bfd, 0x8d, 2946 contents + roff - 2); 2947 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3), 2948 contents + roff - 1); 2949 } 2950 bfd_put_32 (output_bfd, tpoff (info, relocation), 2951 contents + roff); 2952 continue; 2953 } 2954 else 2955 BFD_ASSERT (FALSE); 2956 } 2957 2958 if (htab->sgot == NULL) 2959 abort (); 2960 2961 if (h != NULL) 2962 { 2963 off = h->got.offset; 2964 offplt = elf64_x86_64_hash_entry (h)->tlsdesc_got; 2965 } 2966 else 2967 { 2968 if (local_got_offsets == NULL) 2969 abort (); 2970 2971 off = local_got_offsets[r_symndx]; 2972 offplt = local_tlsdesc_gotents[r_symndx]; 2973 } 2974 2975 if ((off & 1) != 0) 2976 off &= ~1; 2977 else 2978 { 2979 Elf_Internal_Rela outrel; 2980 bfd_byte *loc; 2981 int dr_type, indx; 2982 asection *sreloc; 2983 2984 if (htab->srelgot == NULL) 2985 abort (); 2986 2987 indx = h && h->dynindx != -1 ? h->dynindx : 0; 2988 2989 if (GOT_TLS_GDESC_P (tls_type)) 2990 { 2991 outrel.r_info = ELF64_R_INFO (indx, R_X86_64_TLSDESC); 2992 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt 2993 + 2 * GOT_ENTRY_SIZE <= htab->sgotplt->size); 2994 outrel.r_offset = (htab->sgotplt->output_section->vma 2995 + htab->sgotplt->output_offset 2996 + offplt 2997 + htab->sgotplt_jump_table_size); 2998 sreloc = htab->srelplt; 2999 loc = sreloc->contents; 3000 loc += sreloc->reloc_count++ 3001 * sizeof (Elf64_External_Rela); 3002 BFD_ASSERT (loc + sizeof (Elf64_External_Rela) 3003 <= sreloc->contents + sreloc->size); 3004 if (indx == 0) 3005 outrel.r_addend = relocation - dtpoff_base (info); 3006 else 3007 outrel.r_addend = 0; 3008 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); 3009 } 3010 3011 sreloc = htab->srelgot; 3012 3013 outrel.r_offset = (htab->sgot->output_section->vma 3014 + htab->sgot->output_offset + off); 3015 3016 if (GOT_TLS_GD_P (tls_type)) 3017 dr_type = R_X86_64_DTPMOD64; 3018 else if (GOT_TLS_GDESC_P (tls_type)) 3019 goto dr_done; 3020 else 3021 dr_type = R_X86_64_TPOFF64; 3022 3023 bfd_put_64 (output_bfd, 0, htab->sgot->contents + off); 3024 outrel.r_addend = 0; 3025 if ((dr_type == R_X86_64_TPOFF64 3026 || dr_type == R_X86_64_TLSDESC) && indx == 0) 3027 outrel.r_addend = relocation - dtpoff_base (info); 3028 outrel.r_info = ELF64_R_INFO (indx, dr_type); 3029 3030 loc = sreloc->contents; 3031 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela); 3032 BFD_ASSERT (loc + sizeof (Elf64_External_Rela) 3033 <= sreloc->contents + sreloc->size); 3034 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); 3035 3036 if (GOT_TLS_GD_P (tls_type)) 3037 { 3038 if (indx == 0) 3039 { 3040 BFD_ASSERT (! unresolved_reloc); 3041 bfd_put_64 (output_bfd, 3042 relocation - dtpoff_base (info), 3043 htab->sgot->contents + off + GOT_ENTRY_SIZE); 3044 } 3045 else 3046 { 3047 bfd_put_64 (output_bfd, 0, 3048 htab->sgot->contents + off + GOT_ENTRY_SIZE); 3049 outrel.r_info = ELF64_R_INFO (indx, 3050 R_X86_64_DTPOFF64); 3051 outrel.r_offset += GOT_ENTRY_SIZE; 3052 sreloc->reloc_count++; 3053 loc += sizeof (Elf64_External_Rela); 3054 BFD_ASSERT (loc + sizeof (Elf64_External_Rela) 3055 <= sreloc->contents + sreloc->size); 3056 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); 3057 } 3058 } 3059 3060 dr_done: 3061 if (h != NULL) 3062 h->got.offset |= 1; 3063 else 3064 local_got_offsets[r_symndx] |= 1; 3065 } 3066 3067 if (off >= (bfd_vma) -2 3068 && ! GOT_TLS_GDESC_P (tls_type)) 3069 abort (); 3070 if (r_type == ELF64_R_TYPE (rel->r_info)) 3071 { 3072 if (r_type == R_X86_64_GOTPC32_TLSDESC 3073 || r_type == R_X86_64_TLSDESC_CALL) 3074 relocation = htab->sgotplt->output_section->vma 3075 + htab->sgotplt->output_offset 3076 + offplt + htab->sgotplt_jump_table_size; 3077 else 3078 relocation = htab->sgot->output_section->vma 3079 + htab->sgot->output_offset + off; 3080 unresolved_reloc = FALSE; 3081 } 3082 else 3083 { 3084 bfd_vma roff = rel->r_offset; 3085 3086 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD) 3087 { 3088 /* GD->IE transition. 3089 .byte 0x66; leaq foo@tlsgd(%rip), %rdi 3090 .word 0x6666; rex64; call __tls_get_addr@plt 3091 Change it into: 3092 movq %fs:0, %rax 3093 addq foo@gottpoff(%rip), %rax */ 3094 memcpy (contents + roff - 4, 3095 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0", 3096 16); 3097 3098 relocation = (htab->sgot->output_section->vma 3099 + htab->sgot->output_offset + off 3100 - roff 3101 - input_section->output_section->vma 3102 - input_section->output_offset 3103 - 12); 3104 bfd_put_32 (output_bfd, relocation, 3105 contents + roff + 8); 3106 /* Skip R_X86_64_PLT32. */ 3107 rel++; 3108 continue; 3109 } 3110 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC) 3111 { 3112 /* GDesc -> IE transition. 3113 It's originally something like: 3114 leaq x@tlsdesc(%rip), %rax 3115 3116 Change it to: 3117 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax 3118 */ 3119 3120 unsigned int val, type, type2; 3121 3122 type = bfd_get_8 (input_bfd, contents + roff - 3); 3123 type2 = bfd_get_8 (input_bfd, contents + roff - 2); 3124 val = bfd_get_8 (input_bfd, contents + roff - 1); 3125 3126 /* Now modify the instruction as appropriate. To 3127 turn a leaq into a movq in the form we use it, it 3128 suffices to change the second byte from 0x8d to 3129 0x8b. */ 3130 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2); 3131 3132 bfd_put_32 (output_bfd, 3133 htab->sgot->output_section->vma 3134 + htab->sgot->output_offset + off 3135 - rel->r_offset 3136 - input_section->output_section->vma 3137 - input_section->output_offset 3138 - 4, 3139 contents + roff); 3140 continue; 3141 } 3142 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL) 3143 { 3144 /* GDesc -> IE transition. 3145 It's originally: 3146 call *(%rax) 3147 3148 Change it to: 3149 xchg %ax,%ax. */ 3150 3151 unsigned int val, type; 3152 3153 type = bfd_get_8 (input_bfd, contents + roff); 3154 val = bfd_get_8 (input_bfd, contents + roff + 1); 3155 bfd_put_8 (output_bfd, 0x66, contents + roff); 3156 bfd_put_8 (output_bfd, 0x90, contents + roff + 1); 3157 continue; 3158 } 3159 else 3160 BFD_ASSERT (FALSE); 3161 } 3162 break; 3163 3164 case R_X86_64_TLSLD: 3165 if (! elf64_x86_64_tls_transition (info, input_bfd, 3166 input_section, contents, 3167 symtab_hdr, sym_hashes, 3168 &r_type, GOT_UNKNOWN, 3169 rel, relend, h)) 3170 return FALSE; 3171 3172 if (r_type != R_X86_64_TLSLD) 3173 { 3174 /* LD->LE transition: 3175 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr. 3176 We change it into: 3177 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */ 3178 3179 BFD_ASSERT (r_type == R_X86_64_TPOFF32); 3180 memcpy (contents + rel->r_offset - 3, 3181 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12); 3182 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */ 3183 rel++; 3184 continue; 3185 } 3186 3187 if (htab->sgot == NULL) 3188 abort (); 3189 3190 off = htab->tls_ld_got.offset; 3191 if (off & 1) 3192 off &= ~1; 3193 else 3194 { 3195 Elf_Internal_Rela outrel; 3196 bfd_byte *loc; 3197 3198 if (htab->srelgot == NULL) 3199 abort (); 3200 3201 outrel.r_offset = (htab->sgot->output_section->vma 3202 + htab->sgot->output_offset + off); 3203 3204 bfd_put_64 (output_bfd, 0, 3205 htab->sgot->contents + off); 3206 bfd_put_64 (output_bfd, 0, 3207 htab->sgot->contents + off + GOT_ENTRY_SIZE); 3208 outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64); 3209 outrel.r_addend = 0; 3210 loc = htab->srelgot->contents; 3211 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela); 3212 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); 3213 htab->tls_ld_got.offset |= 1; 3214 } 3215 relocation = htab->sgot->output_section->vma 3216 + htab->sgot->output_offset + off; 3217 unresolved_reloc = FALSE; 3218 break; 3219 3220 case R_X86_64_DTPOFF32: 3221 if (info->shared || (input_section->flags & SEC_CODE) == 0) 3222 relocation -= dtpoff_base (info); 3223 else 3224 relocation = tpoff (info, relocation); 3225 break; 3226 3227 case R_X86_64_TPOFF32: 3228 BFD_ASSERT (! info->shared); 3229 relocation = tpoff (info, relocation); 3230 break; 3231 3232 default: 3233 break; 3234 } 3235 3236 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections 3237 because such sections are not SEC_ALLOC and thus ld.so will 3238 not process them. */ 3239 if (unresolved_reloc 3240 && !((input_section->flags & SEC_DEBUGGING) != 0 3241 && h->def_dynamic)) 3242 (*_bfd_error_handler) 3243 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"), 3244 input_bfd, 3245 input_section, 3246 (long) rel->r_offset, 3247 howto->name, 3248 h->root.root.string); 3249 3250 r = _bfd_final_link_relocate (howto, input_bfd, input_section, 3251 contents, rel->r_offset, 3252 relocation, rel->r_addend); 3253 3254 if (r != bfd_reloc_ok) 3255 { 3256 const char *name; 3257 3258 if (h != NULL) 3259 name = h->root.root.string; 3260 else 3261 { 3262 name = bfd_elf_string_from_elf_section (input_bfd, 3263 symtab_hdr->sh_link, 3264 sym->st_name); 3265 if (name == NULL) 3266 return FALSE; 3267 if (*name == '\0') 3268 name = bfd_section_name (input_bfd, sec); 3269 } 3270 3271 if (r == bfd_reloc_overflow) 3272 { 3273 if (! ((*info->callbacks->reloc_overflow) 3274 (info, (h ? &h->root : NULL), name, howto->name, 3275 (bfd_vma) 0, input_bfd, input_section, 3276 rel->r_offset))) 3277 return FALSE; 3278 } 3279 else 3280 { 3281 (*_bfd_error_handler) 3282 (_("%B(%A+0x%lx): reloc against `%s': error %d"), 3283 input_bfd, input_section, 3284 (long) rel->r_offset, name, (int) r); 3285 return FALSE; 3286 } 3287 } 3288 } 3289 3290 return TRUE; 3291 } 3292 3293 /* Finish up dynamic symbol handling. We set the contents of various 3294 dynamic sections here. */ 3295 3296 static bfd_boolean 3297 elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd, 3298 struct bfd_link_info *info, 3299 struct elf_link_hash_entry *h, 3300 Elf_Internal_Sym *sym) 3301 { 3302 struct elf64_x86_64_link_hash_table *htab; 3303 3304 htab = elf64_x86_64_hash_table (info); 3305 3306 if (h->plt.offset != (bfd_vma) -1) 3307 { 3308 bfd_vma plt_index; 3309 bfd_vma got_offset; 3310 Elf_Internal_Rela rela; 3311 bfd_byte *loc; 3312 3313 /* This symbol has an entry in the procedure linkage table. Set 3314 it up. */ 3315 if (h->dynindx == -1 3316 || htab->splt == NULL 3317 || htab->sgotplt == NULL 3318 || htab->srelplt == NULL) 3319 abort (); 3320 3321 /* Get the index in the procedure linkage table which 3322 corresponds to this symbol. This is the index of this symbol 3323 in all the symbols for which we are making plt entries. The 3324 first entry in the procedure linkage table is reserved. */ 3325 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1; 3326 3327 /* Get the offset into the .got table of the entry that 3328 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE 3329 bytes. The first three are reserved for the dynamic linker. */ 3330 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE; 3331 3332 /* Fill in the entry in the procedure linkage table. */ 3333 memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry, 3334 PLT_ENTRY_SIZE); 3335 3336 /* Insert the relocation positions of the plt section. The magic 3337 numbers at the end of the statements are the positions of the 3338 relocations in the plt section. */ 3339 /* Put offset for jmp *name@GOTPCREL(%rip), since the 3340 instruction uses 6 bytes, subtract this value. */ 3341 bfd_put_32 (output_bfd, 3342 (htab->sgotplt->output_section->vma 3343 + htab->sgotplt->output_offset 3344 + got_offset 3345 - htab->splt->output_section->vma 3346 - htab->splt->output_offset 3347 - h->plt.offset 3348 - 6), 3349 htab->splt->contents + h->plt.offset + 2); 3350 /* Put relocation index. */ 3351 bfd_put_32 (output_bfd, plt_index, 3352 htab->splt->contents + h->plt.offset + 7); 3353 /* Put offset for jmp .PLT0. */ 3354 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE), 3355 htab->splt->contents + h->plt.offset + 12); 3356 3357 /* Fill in the entry in the global offset table, initially this 3358 points to the pushq instruction in the PLT which is at offset 6. */ 3359 bfd_put_64 (output_bfd, (htab->splt->output_section->vma 3360 + htab->splt->output_offset 3361 + h->plt.offset + 6), 3362 htab->sgotplt->contents + got_offset); 3363 3364 /* Fill in the entry in the .rela.plt section. */ 3365 rela.r_offset = (htab->sgotplt->output_section->vma 3366 + htab->sgotplt->output_offset 3367 + got_offset); 3368 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT); 3369 rela.r_addend = 0; 3370 loc = htab->srelplt->contents + plt_index * sizeof (Elf64_External_Rela); 3371 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc); 3372 3373 if (!h->def_regular) 3374 { 3375 /* Mark the symbol as undefined, rather than as defined in 3376 the .plt section. Leave the value if there were any 3377 relocations where pointer equality matters (this is a clue 3378 for the dynamic linker, to make function pointer 3379 comparisons work between an application and shared 3380 library), otherwise set it to zero. If a function is only 3381 called from a binary, there is no need to slow down 3382 shared libraries because of that. */ 3383 sym->st_shndx = SHN_UNDEF; 3384 if (!h->pointer_equality_needed) 3385 sym->st_value = 0; 3386 } 3387 } 3388 3389 if (h->got.offset != (bfd_vma) -1 3390 && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h)->tls_type) 3391 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE) 3392 { 3393 Elf_Internal_Rela rela; 3394 bfd_byte *loc; 3395 3396 /* This symbol has an entry in the global offset table. Set it 3397 up. */ 3398 if (htab->sgot == NULL || htab->srelgot == NULL) 3399 abort (); 3400 3401 rela.r_offset = (htab->sgot->output_section->vma 3402 + htab->sgot->output_offset 3403 + (h->got.offset &~ (bfd_vma) 1)); 3404 3405 /* If this is a static link, or it is a -Bsymbolic link and the 3406 symbol is defined locally or was forced to be local because 3407 of a version file, we just want to emit a RELATIVE reloc. 3408 The entry in the global offset table will already have been 3409 initialized in the relocate_section function. */ 3410 if (info->shared 3411 && SYMBOL_REFERENCES_LOCAL (info, h)) 3412 { 3413 if (!h->def_regular) 3414 return FALSE; 3415 BFD_ASSERT((h->got.offset & 1) != 0); 3416 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE); 3417 rela.r_addend = (h->root.u.def.value 3418 + h->root.u.def.section->output_section->vma 3419 + h->root.u.def.section->output_offset); 3420 } 3421 else 3422 { 3423 BFD_ASSERT((h->got.offset & 1) == 0); 3424 bfd_put_64 (output_bfd, (bfd_vma) 0, 3425 htab->sgot->contents + h->got.offset); 3426 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT); 3427 rela.r_addend = 0; 3428 } 3429 3430 loc = htab->srelgot->contents; 3431 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela); 3432 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc); 3433 } 3434 3435 if (h->needs_copy) 3436 { 3437 Elf_Internal_Rela rela; 3438 bfd_byte *loc; 3439 3440 /* This symbol needs a copy reloc. Set it up. */ 3441 3442 if (h->dynindx == -1 3443 || (h->root.type != bfd_link_hash_defined 3444 && h->root.type != bfd_link_hash_defweak) 3445 || htab->srelbss == NULL) 3446 abort (); 3447 3448 rela.r_offset = (h->root.u.def.value 3449 + h->root.u.def.section->output_section->vma 3450 + h->root.u.def.section->output_offset); 3451 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY); 3452 rela.r_addend = 0; 3453 loc = htab->srelbss->contents; 3454 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela); 3455 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc); 3456 } 3457 3458 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */ 3459 if (strcmp (h->root.root.string, "_DYNAMIC") == 0 3460 || h == htab->elf.hgot) 3461 sym->st_shndx = SHN_ABS; 3462 3463 return TRUE; 3464 } 3465 3466 /* Used to decide how to sort relocs in an optimal manner for the 3467 dynamic linker, before writing them out. */ 3468 3469 static enum elf_reloc_type_class 3470 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela) 3471 { 3472 switch ((int) ELF64_R_TYPE (rela->r_info)) 3473 { 3474 case R_X86_64_RELATIVE: 3475 return reloc_class_relative; 3476 case R_X86_64_JUMP_SLOT: 3477 return reloc_class_plt; 3478 case R_X86_64_COPY: 3479 return reloc_class_copy; 3480 default: 3481 return reloc_class_normal; 3482 } 3483 } 3484 3485 /* Finish up the dynamic sections. */ 3486 3487 static bfd_boolean 3488 elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info) 3489 { 3490 struct elf64_x86_64_link_hash_table *htab; 3491 bfd *dynobj; 3492 asection *sdyn; 3493 3494 htab = elf64_x86_64_hash_table (info); 3495 dynobj = htab->elf.dynobj; 3496 sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); 3497 3498 if (htab->elf.dynamic_sections_created) 3499 { 3500 Elf64_External_Dyn *dyncon, *dynconend; 3501 3502 if (sdyn == NULL || htab->sgot == NULL) 3503 abort (); 3504 3505 dyncon = (Elf64_External_Dyn *) sdyn->contents; 3506 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size); 3507 for (; dyncon < dynconend; dyncon++) 3508 { 3509 Elf_Internal_Dyn dyn; 3510 asection *s; 3511 3512 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn); 3513 3514 switch (dyn.d_tag) 3515 { 3516 default: 3517 continue; 3518 3519 case DT_PLTGOT: 3520 s = htab->sgotplt; 3521 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset; 3522 break; 3523 3524 case DT_JMPREL: 3525 dyn.d_un.d_ptr = htab->srelplt->output_section->vma; 3526 break; 3527 3528 case DT_PLTRELSZ: 3529 s = htab->srelplt->output_section; 3530 dyn.d_un.d_val = s->size; 3531 break; 3532 3533 case DT_RELASZ: 3534 /* The procedure linkage table relocs (DT_JMPREL) should 3535 not be included in the overall relocs (DT_RELA). 3536 Therefore, we override the DT_RELASZ entry here to 3537 make it not include the JMPREL relocs. Since the 3538 linker script arranges for .rela.plt to follow all 3539 other relocation sections, we don't have to worry 3540 about changing the DT_RELA entry. */ 3541 if (htab->srelplt != NULL) 3542 { 3543 s = htab->srelplt->output_section; 3544 dyn.d_un.d_val -= s->size; 3545 } 3546 break; 3547 3548 case DT_TLSDESC_PLT: 3549 s = htab->splt; 3550 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset 3551 + htab->tlsdesc_plt; 3552 break; 3553 3554 case DT_TLSDESC_GOT: 3555 s = htab->sgot; 3556 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset 3557 + htab->tlsdesc_got; 3558 break; 3559 } 3560 3561 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon); 3562 } 3563 3564 /* Fill in the special first entry in the procedure linkage table. */ 3565 if (htab->splt && htab->splt->size > 0) 3566 { 3567 /* Fill in the first entry in the procedure linkage table. */ 3568 memcpy (htab->splt->contents, elf64_x86_64_plt0_entry, 3569 PLT_ENTRY_SIZE); 3570 /* Add offset for pushq GOT+8(%rip), since the instruction 3571 uses 6 bytes subtract this value. */ 3572 bfd_put_32 (output_bfd, 3573 (htab->sgotplt->output_section->vma 3574 + htab->sgotplt->output_offset 3575 + 8 3576 - htab->splt->output_section->vma 3577 - htab->splt->output_offset 3578 - 6), 3579 htab->splt->contents + 2); 3580 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to 3581 the end of the instruction. */ 3582 bfd_put_32 (output_bfd, 3583 (htab->sgotplt->output_section->vma 3584 + htab->sgotplt->output_offset 3585 + 16 3586 - htab->splt->output_section->vma 3587 - htab->splt->output_offset 3588 - 12), 3589 htab->splt->contents + 8); 3590 3591 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize = 3592 PLT_ENTRY_SIZE; 3593 3594 if (htab->tlsdesc_plt) 3595 { 3596 bfd_put_64 (output_bfd, (bfd_vma) 0, 3597 htab->sgot->contents + htab->tlsdesc_got); 3598 3599 memcpy (htab->splt->contents + htab->tlsdesc_plt, 3600 elf64_x86_64_plt0_entry, 3601 PLT_ENTRY_SIZE); 3602 3603 /* Add offset for pushq GOT+8(%rip), since the 3604 instruction uses 6 bytes subtract this value. */ 3605 bfd_put_32 (output_bfd, 3606 (htab->sgotplt->output_section->vma 3607 + htab->sgotplt->output_offset 3608 + 8 3609 - htab->splt->output_section->vma 3610 - htab->splt->output_offset 3611 - htab->tlsdesc_plt 3612 - 6), 3613 htab->splt->contents + htab->tlsdesc_plt + 2); 3614 /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for 3615 htab->tlsdesc_got. The 12 is the offset to the end of 3616 the instruction. */ 3617 bfd_put_32 (output_bfd, 3618 (htab->sgot->output_section->vma 3619 + htab->sgot->output_offset 3620 + htab->tlsdesc_got 3621 - htab->splt->output_section->vma 3622 - htab->splt->output_offset 3623 - htab->tlsdesc_plt 3624 - 12), 3625 htab->splt->contents + htab->tlsdesc_plt + 8); 3626 } 3627 } 3628 } 3629 3630 if (htab->sgotplt) 3631 { 3632 /* Fill in the first three entries in the global offset table. */ 3633 if (htab->sgotplt->size > 0) 3634 { 3635 /* Set the first entry in the global offset table to the address of 3636 the dynamic section. */ 3637 if (sdyn == NULL) 3638 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents); 3639 else 3640 bfd_put_64 (output_bfd, 3641 sdyn->output_section->vma + sdyn->output_offset, 3642 htab->sgotplt->contents); 3643 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */ 3644 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE); 3645 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2); 3646 } 3647 3648 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize = 3649 GOT_ENTRY_SIZE; 3650 } 3651 3652 if (htab->sgot && htab->sgot->size > 0) 3653 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize 3654 = GOT_ENTRY_SIZE; 3655 3656 return TRUE; 3657 } 3658 3659 /* Return address for Ith PLT stub in section PLT, for relocation REL 3660 or (bfd_vma) -1 if it should not be included. */ 3661 3662 static bfd_vma 3663 elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt, 3664 const arelent *rel ATTRIBUTE_UNUSED) 3665 { 3666 return plt->vma + (i + 1) * PLT_ENTRY_SIZE; 3667 } 3668 3669 /* Handle an x86-64 specific section when reading an object file. This 3670 is called when elfcode.h finds a section with an unknown type. */ 3671 3672 static bfd_boolean 3673 elf64_x86_64_section_from_shdr (bfd *abfd, 3674 Elf_Internal_Shdr *hdr, 3675 const char *name, 3676 int shindex) 3677 { 3678 if (hdr->sh_type != SHT_X86_64_UNWIND) 3679 return FALSE; 3680 3681 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) 3682 return FALSE; 3683 3684 return TRUE; 3685 } 3686 3687 /* Hook called by the linker routine which adds symbols from an object 3688 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead 3689 of .bss. */ 3690 3691 static bfd_boolean 3692 elf64_x86_64_add_symbol_hook (bfd *abfd, 3693 struct bfd_link_info *info ATTRIBUTE_UNUSED, 3694 Elf_Internal_Sym *sym, 3695 const char **namep ATTRIBUTE_UNUSED, 3696 flagword *flagsp ATTRIBUTE_UNUSED, 3697 asection **secp, bfd_vma *valp) 3698 { 3699 asection *lcomm; 3700 3701 switch (sym->st_shndx) 3702 { 3703 case SHN_X86_64_LCOMMON: 3704 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON"); 3705 if (lcomm == NULL) 3706 { 3707 lcomm = bfd_make_section_with_flags (abfd, 3708 "LARGE_COMMON", 3709 (SEC_ALLOC 3710 | SEC_IS_COMMON 3711 | SEC_LINKER_CREATED)); 3712 if (lcomm == NULL) 3713 return FALSE; 3714 elf_section_flags (lcomm) |= SHF_X86_64_LARGE; 3715 } 3716 *secp = lcomm; 3717 *valp = sym->st_size; 3718 break; 3719 } 3720 return TRUE; 3721 } 3722 3723 3724 /* Given a BFD section, try to locate the corresponding ELF section 3725 index. */ 3726 3727 static bfd_boolean 3728 elf64_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED, 3729 asection *sec, int *index) 3730 { 3731 if (sec == &_bfd_elf_large_com_section) 3732 { 3733 *index = SHN_X86_64_LCOMMON; 3734 return TRUE; 3735 } 3736 return FALSE; 3737 } 3738 3739 /* Process a symbol. */ 3740 3741 static void 3742 elf64_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED, 3743 asymbol *asym) 3744 { 3745 elf_symbol_type *elfsym = (elf_symbol_type *) asym; 3746 3747 switch (elfsym->internal_elf_sym.st_shndx) 3748 { 3749 case SHN_X86_64_LCOMMON: 3750 asym->section = &_bfd_elf_large_com_section; 3751 asym->value = elfsym->internal_elf_sym.st_size; 3752 /* Common symbol doesn't set BSF_GLOBAL. */ 3753 asym->flags &= ~BSF_GLOBAL; 3754 break; 3755 } 3756 } 3757 3758 static bfd_boolean 3759 elf64_x86_64_common_definition (Elf_Internal_Sym *sym) 3760 { 3761 return (sym->st_shndx == SHN_COMMON 3762 || sym->st_shndx == SHN_X86_64_LCOMMON); 3763 } 3764 3765 static unsigned int 3766 elf64_x86_64_common_section_index (asection *sec) 3767 { 3768 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0) 3769 return SHN_COMMON; 3770 else 3771 return SHN_X86_64_LCOMMON; 3772 } 3773 3774 static asection * 3775 elf64_x86_64_common_section (asection *sec) 3776 { 3777 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0) 3778 return bfd_com_section_ptr; 3779 else 3780 return &_bfd_elf_large_com_section; 3781 } 3782 3783 static bfd_boolean 3784 elf64_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED, 3785 struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED, 3786 struct elf_link_hash_entry *h, 3787 Elf_Internal_Sym *sym, 3788 asection **psec, 3789 bfd_vma *pvalue ATTRIBUTE_UNUSED, 3790 unsigned int *pold_alignment ATTRIBUTE_UNUSED, 3791 bfd_boolean *skip ATTRIBUTE_UNUSED, 3792 bfd_boolean *override ATTRIBUTE_UNUSED, 3793 bfd_boolean *type_change_ok ATTRIBUTE_UNUSED, 3794 bfd_boolean *size_change_ok ATTRIBUTE_UNUSED, 3795 bfd_boolean *newdef ATTRIBUTE_UNUSED, 3796 bfd_boolean *newdyn, 3797 bfd_boolean *newdyncommon ATTRIBUTE_UNUSED, 3798 bfd_boolean *newweak ATTRIBUTE_UNUSED, 3799 bfd *abfd ATTRIBUTE_UNUSED, 3800 asection **sec, 3801 bfd_boolean *olddef ATTRIBUTE_UNUSED, 3802 bfd_boolean *olddyn, 3803 bfd_boolean *olddyncommon ATTRIBUTE_UNUSED, 3804 bfd_boolean *oldweak ATTRIBUTE_UNUSED, 3805 bfd *oldbfd, 3806 asection **oldsec) 3807 { 3808 /* A normal common symbol and a large common symbol result in a 3809 normal common symbol. We turn the large common symbol into a 3810 normal one. */ 3811 if (!*olddyn 3812 && h->root.type == bfd_link_hash_common 3813 && !*newdyn 3814 && bfd_is_com_section (*sec) 3815 && *oldsec != *sec) 3816 { 3817 if (sym->st_shndx == SHN_COMMON 3818 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0) 3819 { 3820 h->root.u.c.p->section 3821 = bfd_make_section_old_way (oldbfd, "COMMON"); 3822 h->root.u.c.p->section->flags = SEC_ALLOC; 3823 } 3824 else if (sym->st_shndx == SHN_X86_64_LCOMMON 3825 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0) 3826 *psec = *sec = bfd_com_section_ptr; 3827 } 3828 3829 return TRUE; 3830 } 3831 3832 static int 3833 elf64_x86_64_additional_program_headers (bfd *abfd, 3834 struct bfd_link_info *info ATTRIBUTE_UNUSED) 3835 { 3836 asection *s; 3837 int count = 0; 3838 3839 /* Check to see if we need a large readonly segment. */ 3840 s = bfd_get_section_by_name (abfd, ".lrodata"); 3841 if (s && (s->flags & SEC_LOAD)) 3842 count++; 3843 3844 /* Check to see if we need a large data segment. Since .lbss sections 3845 is placed right after the .bss section, there should be no need for 3846 a large data segment just because of .lbss. */ 3847 s = bfd_get_section_by_name (abfd, ".ldata"); 3848 if (s && (s->flags & SEC_LOAD)) 3849 count++; 3850 3851 return count; 3852 } 3853 3854 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ 3855 3856 static bfd_boolean 3857 elf64_x86_64_hash_symbol (struct elf_link_hash_entry *h) 3858 { 3859 if (h->plt.offset != (bfd_vma) -1 3860 && !h->def_regular 3861 && !h->pointer_equality_needed) 3862 return FALSE; 3863 3864 return _bfd_elf_hash_symbol (h); 3865 } 3866 3867 static const struct bfd_elf_special_section 3868 elf64_x86_64_special_sections[]= 3869 { 3870 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE}, 3871 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE}, 3872 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE}, 3873 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE}, 3874 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE}, 3875 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE}, 3876 { NULL, 0, 0, 0, 0 } 3877 }; 3878 3879 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec 3880 #define TARGET_LITTLE_NAME "elf64-x86-64" 3881 #define ELF_ARCH bfd_arch_i386 3882 #define ELF_MACHINE_CODE EM_X86_64 3883 #define ELF_MAXPAGESIZE 0x200000 3884 #define ELF_MINPAGESIZE 0x1000 3885 #define ELF_COMMONPAGESIZE 0x1000 3886 3887 #define elf_backend_can_gc_sections 1 3888 #define elf_backend_can_refcount 1 3889 #define elf_backend_want_got_plt 1 3890 #define elf_backend_plt_readonly 1 3891 #define elf_backend_want_plt_sym 0 3892 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3) 3893 #define elf_backend_rela_normal 1 3894 3895 #define elf_info_to_howto elf64_x86_64_info_to_howto 3896 3897 #define bfd_elf64_bfd_link_hash_table_create \ 3898 elf64_x86_64_link_hash_table_create 3899 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup 3900 #define bfd_elf64_bfd_reloc_name_lookup \ 3901 elf64_x86_64_reloc_name_lookup 3902 3903 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol 3904 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible 3905 #define elf_backend_check_relocs elf64_x86_64_check_relocs 3906 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol 3907 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections 3908 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections 3909 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol 3910 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook 3911 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook 3912 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus 3913 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo 3914 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class 3915 #define elf_backend_relocate_section elf64_x86_64_relocate_section 3916 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections 3917 #define elf_backend_always_size_sections elf64_x86_64_always_size_sections 3918 #define elf_backend_init_index_section _bfd_elf_init_1_index_section 3919 #define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val 3920 #define elf_backend_object_p elf64_x86_64_elf_object_p 3921 #define bfd_elf64_mkobject elf64_x86_64_mkobject 3922 3923 #define elf_backend_section_from_shdr \ 3924 elf64_x86_64_section_from_shdr 3925 3926 #define elf_backend_section_from_bfd_section \ 3927 elf64_x86_64_elf_section_from_bfd_section 3928 #define elf_backend_add_symbol_hook \ 3929 elf64_x86_64_add_symbol_hook 3930 #define elf_backend_symbol_processing \ 3931 elf64_x86_64_symbol_processing 3932 #define elf_backend_common_section_index \ 3933 elf64_x86_64_common_section_index 3934 #define elf_backend_common_section \ 3935 elf64_x86_64_common_section 3936 #define elf_backend_common_definition \ 3937 elf64_x86_64_common_definition 3938 #define elf_backend_merge_symbol \ 3939 elf64_x86_64_merge_symbol 3940 #define elf_backend_special_sections \ 3941 elf64_x86_64_special_sections 3942 #define elf_backend_additional_program_headers \ 3943 elf64_x86_64_additional_program_headers 3944 #define elf_backend_hash_symbol \ 3945 elf64_x86_64_hash_symbol 3946 3947 #include "elf64-target.h" 3948 3949 /* FreeBSD support. */ 3950 3951 #undef TARGET_LITTLE_SYM 3952 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec 3953 #undef TARGET_LITTLE_NAME 3954 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd" 3955 3956 #undef ELF_OSABI 3957 #define ELF_OSABI ELFOSABI_FREEBSD 3958 3959 #undef elf_backend_post_process_headers 3960 #define elf_backend_post_process_headers _bfd_elf_set_osabi 3961 3962 #undef elf64_bed 3963 #define elf64_bed elf64_x86_64_fbsd_bed 3964 3965 #include "elf64-target.h" 3966