| 1 | /* X86-64 specific support for 64-bit ELF |
| 2 | Copyright 2000, 2001, 2002, 2003, 2004, 2005 |
| 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 2 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., 59 Temple Place - Suite 330, Boston, MA 02110-1301, USA. */ |
| 21 | |
| 22 | #include "bfd.h" |
| 23 | #include "sysdep.h" |
| 24 | #include "bfdlink.h" |
| 25 | #include "libbfd.h" |
| 26 | #include "elf-bfd.h" |
| 27 | |
| 28 | #include "elf/x86-64.h" |
| 29 | |
| 30 | /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */ |
| 31 | #define MINUS_ONE (~ (bfd_vma) 0) |
| 32 | |
| 33 | /* The relocation "howto" table. Order of fields: |
| 34 | type, size, bitsize, pc_relative, complain_on_overflow, |
| 35 | special_function, name, partial_inplace, src_mask, dst_pack, pcrel_offset. */ |
| 36 | static reloc_howto_type x86_64_elf_howto_table[] = |
| 37 | { |
| 38 | HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont, |
| 39 | bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000, |
| 40 | FALSE), |
| 41 | HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, |
| 42 | bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE, |
| 43 | FALSE), |
| 44 | HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed, |
| 45 | bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff, |
| 46 | TRUE), |
| 47 | HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed, |
| 48 | bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff, |
| 49 | FALSE), |
| 50 | HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed, |
| 51 | bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff, |
| 52 | TRUE), |
| 53 | HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, |
| 54 | bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff, |
| 55 | FALSE), |
| 56 | HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, |
| 57 | bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE, |
| 58 | MINUS_ONE, FALSE), |
| 59 | HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, |
| 60 | bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE, |
| 61 | MINUS_ONE, FALSE), |
| 62 | HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, |
| 63 | bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE, |
| 64 | MINUS_ONE, FALSE), |
| 65 | HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed, |
| 66 | bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff, |
| 67 | 0xffffffff, TRUE), |
| 68 | HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned, |
| 69 | bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff, |
| 70 | FALSE), |
| 71 | HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed, |
| 72 | bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff, |
| 73 | FALSE), |
| 74 | HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, |
| 75 | bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE), |
| 76 | HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield, |
| 77 | bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE), |
| 78 | HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_signed, |
| 79 | bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE), |
| 80 | HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed, |
| 81 | bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE), |
| 82 | HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, |
| 83 | bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE, |
| 84 | MINUS_ONE, FALSE), |
| 85 | HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, |
| 86 | bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE, |
| 87 | MINUS_ONE, FALSE), |
| 88 | HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, |
| 89 | bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE, |
| 90 | MINUS_ONE, FALSE), |
| 91 | HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed, |
| 92 | bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff, |
| 93 | 0xffffffff, TRUE), |
| 94 | HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed, |
| 95 | bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff, |
| 96 | 0xffffffff, TRUE), |
| 97 | HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, |
| 98 | bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff, |
| 99 | 0xffffffff, FALSE), |
| 100 | HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed, |
| 101 | bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff, |
| 102 | 0xffffffff, TRUE), |
| 103 | HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed, |
| 104 | bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff, |
| 105 | 0xffffffff, FALSE), |
| 106 | |
| 107 | /* GNU extension to record C++ vtable hierarchy. */ |
| 108 | HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont, |
| 109 | NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE), |
| 110 | |
| 111 | /* GNU extension to record C++ vtable member usage. */ |
| 112 | HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont, |
| 113 | _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0, |
| 114 | FALSE) |
| 115 | }; |
| 116 | |
| 117 | /* Map BFD relocs to the x86_64 elf relocs. */ |
| 118 | struct elf_reloc_map |
| 119 | { |
| 120 | bfd_reloc_code_real_type bfd_reloc_val; |
| 121 | unsigned char elf_reloc_val; |
| 122 | }; |
| 123 | |
| 124 | static const struct elf_reloc_map x86_64_reloc_map[] = |
| 125 | { |
| 126 | { BFD_RELOC_NONE, R_X86_64_NONE, }, |
| 127 | { BFD_RELOC_64, R_X86_64_64, }, |
| 128 | { BFD_RELOC_32_PCREL, R_X86_64_PC32, }, |
| 129 | { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,}, |
| 130 | { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,}, |
| 131 | { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, }, |
| 132 | { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, }, |
| 133 | { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, }, |
| 134 | { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, }, |
| 135 | { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, }, |
| 136 | { BFD_RELOC_32, R_X86_64_32, }, |
| 137 | { BFD_RELOC_X86_64_32S, R_X86_64_32S, }, |
| 138 | { BFD_RELOC_16, R_X86_64_16, }, |
| 139 | { BFD_RELOC_16_PCREL, R_X86_64_PC16, }, |
| 140 | { BFD_RELOC_8, R_X86_64_8, }, |
| 141 | { BFD_RELOC_8_PCREL, R_X86_64_PC8, }, |
| 142 | { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, }, |
| 143 | { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, }, |
| 144 | { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, }, |
| 145 | { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, }, |
| 146 | { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, }, |
| 147 | { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, }, |
| 148 | { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, }, |
| 149 | { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, }, |
| 150 | { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, }, |
| 151 | { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, }, |
| 152 | }; |
| 153 | |
| 154 | |
| 155 | /* Given a BFD reloc type, return a HOWTO structure. */ |
| 156 | static reloc_howto_type * |
| 157 | elf64_x86_64_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, |
| 158 | bfd_reloc_code_real_type code) |
| 159 | { |
| 160 | unsigned int i; |
| 161 | |
| 162 | for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map); |
| 163 | i++) |
| 164 | { |
| 165 | if (x86_64_reloc_map[i].bfd_reloc_val == code) |
| 166 | return &x86_64_elf_howto_table[i]; |
| 167 | } |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | /* Given an x86_64 ELF reloc type, fill in an arelent structure. */ |
| 172 | |
| 173 | static void |
| 174 | elf64_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr, |
| 175 | Elf_Internal_Rela *dst) |
| 176 | { |
| 177 | unsigned r_type, i; |
| 178 | |
| 179 | r_type = ELF64_R_TYPE (dst->r_info); |
| 180 | if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT) |
| 181 | { |
| 182 | BFD_ASSERT (r_type <= (unsigned int) R_X86_64_TPOFF32); |
| 183 | i = r_type; |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | BFD_ASSERT (r_type < (unsigned int) R_X86_64_max); |
| 188 | i = r_type - ((unsigned int) R_X86_64_GNU_VTINHERIT - R_X86_64_TPOFF32 - 1); |
| 189 | } |
| 190 | cache_ptr->howto = &x86_64_elf_howto_table[i]; |
| 191 | BFD_ASSERT (r_type == cache_ptr->howto->type); |
| 192 | } |
| 193 | \f |
| 194 | /* Support for core dump NOTE sections. */ |
| 195 | static bfd_boolean |
| 196 | elf64_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
| 197 | { |
| 198 | int offset; |
| 199 | size_t size; |
| 200 | |
| 201 | switch (note->descsz) |
| 202 | { |
| 203 | default: |
| 204 | return FALSE; |
| 205 | |
| 206 | case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */ |
| 207 | /* pr_cursig */ |
| 208 | elf_tdata (abfd)->core_signal |
| 209 | = bfd_get_16 (abfd, note->descdata + 12); |
| 210 | |
| 211 | /* pr_pid */ |
| 212 | elf_tdata (abfd)->core_pid |
| 213 | = bfd_get_32 (abfd, note->descdata + 32); |
| 214 | |
| 215 | /* pr_reg */ |
| 216 | offset = 112; |
| 217 | size = 216; |
| 218 | |
| 219 | break; |
| 220 | } |
| 221 | |
| 222 | /* Make a ".reg/999" section. */ |
| 223 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
| 224 | size, note->descpos + offset); |
| 225 | } |
| 226 | |
| 227 | static bfd_boolean |
| 228 | elf64_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
| 229 | { |
| 230 | switch (note->descsz) |
| 231 | { |
| 232 | default: |
| 233 | return FALSE; |
| 234 | |
| 235 | case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */ |
| 236 | elf_tdata (abfd)->core_program |
| 237 | = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16); |
| 238 | elf_tdata (abfd)->core_command |
| 239 | = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80); |
| 240 | } |
| 241 | |
| 242 | /* Note that for some reason, a spurious space is tacked |
| 243 | onto the end of the args in some (at least one anyway) |
| 244 | implementations, so strip it off if it exists. */ |
| 245 | |
| 246 | { |
| 247 | char *command = elf_tdata (abfd)->core_command; |
| 248 | int n = strlen (command); |
| 249 | |
| 250 | if (0 < n && command[n - 1] == ' ') |
| 251 | command[n - 1] = '\0'; |
| 252 | } |
| 253 | |
| 254 | return TRUE; |
| 255 | } |
| 256 | \f |
| 257 | /* Functions for the x86-64 ELF linker. */ |
| 258 | |
| 259 | /* The name of the dynamic interpreter. This is put in the .interp |
| 260 | section. */ |
| 261 | |
| 262 | #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1" |
| 263 | |
| 264 | /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid |
| 265 | copying dynamic variables from a shared lib into an app's dynbss |
| 266 | section, and instead use a dynamic relocation to point into the |
| 267 | shared lib. */ |
| 268 | #define ELIMINATE_COPY_RELOCS 1 |
| 269 | |
| 270 | /* The size in bytes of an entry in the global offset table. */ |
| 271 | |
| 272 | #define GOT_ENTRY_SIZE 8 |
| 273 | |
| 274 | /* The size in bytes of an entry in the procedure linkage table. */ |
| 275 | |
| 276 | #define PLT_ENTRY_SIZE 16 |
| 277 | |
| 278 | /* The first entry in a procedure linkage table looks like this. See the |
| 279 | SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */ |
| 280 | |
| 281 | static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] = |
| 282 | { |
| 283 | 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */ |
| 284 | 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */ |
| 285 | 0x90, 0x90, 0x90, 0x90 /* pad out to 16 bytes with nops. */ |
| 286 | }; |
| 287 | |
| 288 | /* Subsequent entries in a procedure linkage table look like this. */ |
| 289 | |
| 290 | static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] = |
| 291 | { |
| 292 | 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */ |
| 293 | 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */ |
| 294 | 0x68, /* pushq immediate */ |
| 295 | 0, 0, 0, 0, /* replaced with index into relocation table. */ |
| 296 | 0xe9, /* jmp relative */ |
| 297 | 0, 0, 0, 0 /* replaced with offset to start of .plt0. */ |
| 298 | }; |
| 299 | |
| 300 | /* The x86-64 linker needs to keep track of the number of relocs that |
| 301 | it decides to copy as dynamic relocs in check_relocs for each symbol. |
| 302 | This is so that it can later discard them if they are found to be |
| 303 | unnecessary. We store the information in a field extending the |
| 304 | regular ELF linker hash table. */ |
| 305 | |
| 306 | struct elf64_x86_64_dyn_relocs |
| 307 | { |
| 308 | /* Next section. */ |
| 309 | struct elf64_x86_64_dyn_relocs *next; |
| 310 | |
| 311 | /* The input section of the reloc. */ |
| 312 | asection *sec; |
| 313 | |
| 314 | /* Total number of relocs copied for the input section. */ |
| 315 | bfd_size_type count; |
| 316 | |
| 317 | /* Number of pc-relative relocs copied for the input section. */ |
| 318 | bfd_size_type pc_count; |
| 319 | }; |
| 320 | |
| 321 | /* x86-64 ELF linker hash entry. */ |
| 322 | |
| 323 | struct elf64_x86_64_link_hash_entry |
| 324 | { |
| 325 | struct elf_link_hash_entry elf; |
| 326 | |
| 327 | /* Track dynamic relocs copied for this symbol. */ |
| 328 | struct elf64_x86_64_dyn_relocs *dyn_relocs; |
| 329 | |
| 330 | #define GOT_UNKNOWN 0 |
| 331 | #define GOT_NORMAL 1 |
| 332 | #define GOT_TLS_GD 2 |
| 333 | #define GOT_TLS_IE 3 |
| 334 | unsigned char tls_type; |
| 335 | }; |
| 336 | |
| 337 | #define elf64_x86_64_hash_entry(ent) \ |
| 338 | ((struct elf64_x86_64_link_hash_entry *)(ent)) |
| 339 | |
| 340 | struct elf64_x86_64_obj_tdata |
| 341 | { |
| 342 | struct elf_obj_tdata root; |
| 343 | |
| 344 | /* tls_type for each local got entry. */ |
| 345 | char *local_got_tls_type; |
| 346 | }; |
| 347 | |
| 348 | #define elf64_x86_64_tdata(abfd) \ |
| 349 | ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any) |
| 350 | |
| 351 | #define elf64_x86_64_local_got_tls_type(abfd) \ |
| 352 | (elf64_x86_64_tdata (abfd)->local_got_tls_type) |
| 353 | |
| 354 | |
| 355 | /* x86-64 ELF linker hash table. */ |
| 356 | |
| 357 | struct elf64_x86_64_link_hash_table |
| 358 | { |
| 359 | struct elf_link_hash_table elf; |
| 360 | |
| 361 | /* Short-cuts to get to dynamic linker sections. */ |
| 362 | asection *sgot; |
| 363 | asection *sgotplt; |
| 364 | asection *srelgot; |
| 365 | asection *splt; |
| 366 | asection *srelplt; |
| 367 | asection *sdynbss; |
| 368 | asection *srelbss; |
| 369 | |
| 370 | union { |
| 371 | bfd_signed_vma refcount; |
| 372 | bfd_vma offset; |
| 373 | } tls_ld_got; |
| 374 | |
| 375 | /* Small local sym to section mapping cache. */ |
| 376 | struct sym_sec_cache sym_sec; |
| 377 | }; |
| 378 | |
| 379 | /* Get the x86-64 ELF linker hash table from a link_info structure. */ |
| 380 | |
| 381 | #define elf64_x86_64_hash_table(p) \ |
| 382 | ((struct elf64_x86_64_link_hash_table *) ((p)->hash)) |
| 383 | |
| 384 | /* Create an entry in an x86-64 ELF linker hash table. */ |
| 385 | |
| 386 | static struct bfd_hash_entry * |
| 387 | link_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table, |
| 388 | const char *string) |
| 389 | { |
| 390 | /* Allocate the structure if it has not already been allocated by a |
| 391 | subclass. */ |
| 392 | if (entry == NULL) |
| 393 | { |
| 394 | entry = bfd_hash_allocate (table, |
| 395 | sizeof (struct elf64_x86_64_link_hash_entry)); |
| 396 | if (entry == NULL) |
| 397 | return entry; |
| 398 | } |
| 399 | |
| 400 | /* Call the allocation method of the superclass. */ |
| 401 | entry = _bfd_elf_link_hash_newfunc (entry, table, string); |
| 402 | if (entry != NULL) |
| 403 | { |
| 404 | struct elf64_x86_64_link_hash_entry *eh; |
| 405 | |
| 406 | eh = (struct elf64_x86_64_link_hash_entry *) entry; |
| 407 | eh->dyn_relocs = NULL; |
| 408 | eh->tls_type = GOT_UNKNOWN; |
| 409 | } |
| 410 | |
| 411 | return entry; |
| 412 | } |
| 413 | |
| 414 | /* Create an X86-64 ELF linker hash table. */ |
| 415 | |
| 416 | static struct bfd_link_hash_table * |
| 417 | elf64_x86_64_link_hash_table_create (bfd *abfd) |
| 418 | { |
| 419 | struct elf64_x86_64_link_hash_table *ret; |
| 420 | bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table); |
| 421 | |
| 422 | ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt); |
| 423 | if (ret == NULL) |
| 424 | return NULL; |
| 425 | |
| 426 | if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc)) |
| 427 | { |
| 428 | free (ret); |
| 429 | return NULL; |
| 430 | } |
| 431 | |
| 432 | ret->sgot = NULL; |
| 433 | ret->sgotplt = NULL; |
| 434 | ret->srelgot = NULL; |
| 435 | ret->splt = NULL; |
| 436 | ret->srelplt = NULL; |
| 437 | ret->sdynbss = NULL; |
| 438 | ret->srelbss = NULL; |
| 439 | ret->sym_sec.abfd = NULL; |
| 440 | ret->tls_ld_got.refcount = 0; |
| 441 | |
| 442 | return &ret->elf.root; |
| 443 | } |
| 444 | |
| 445 | /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up |
| 446 | shortcuts to them in our hash table. */ |
| 447 | |
| 448 | static bfd_boolean |
| 449 | create_got_section (bfd *dynobj, struct bfd_link_info *info) |
| 450 | { |
| 451 | struct elf64_x86_64_link_hash_table *htab; |
| 452 | |
| 453 | if (! _bfd_elf_create_got_section (dynobj, info)) |
| 454 | return FALSE; |
| 455 | |
| 456 | htab = elf64_x86_64_hash_table (info); |
| 457 | htab->sgot = bfd_get_section_by_name (dynobj, ".got"); |
| 458 | htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt"); |
| 459 | if (!htab->sgot || !htab->sgotplt) |
| 460 | abort (); |
| 461 | |
| 462 | htab->srelgot = bfd_make_section (dynobj, ".rela.got"); |
| 463 | if (htab->srelgot == NULL |
| 464 | || ! bfd_set_section_flags (dynobj, htab->srelgot, |
| 465 | (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS |
| 466 | | SEC_IN_MEMORY | SEC_LINKER_CREATED |
| 467 | | SEC_READONLY)) |
| 468 | || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3)) |
| 469 | return FALSE; |
| 470 | return TRUE; |
| 471 | } |
| 472 | |
| 473 | /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and |
| 474 | .rela.bss sections in DYNOBJ, and set up shortcuts to them in our |
| 475 | hash table. */ |
| 476 | |
| 477 | static bfd_boolean |
| 478 | elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info) |
| 479 | { |
| 480 | struct elf64_x86_64_link_hash_table *htab; |
| 481 | |
| 482 | htab = elf64_x86_64_hash_table (info); |
| 483 | if (!htab->sgot && !create_got_section (dynobj, info)) |
| 484 | return FALSE; |
| 485 | |
| 486 | if (!_bfd_elf_create_dynamic_sections (dynobj, info)) |
| 487 | return FALSE; |
| 488 | |
| 489 | htab->splt = bfd_get_section_by_name (dynobj, ".plt"); |
| 490 | htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt"); |
| 491 | htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss"); |
| 492 | if (!info->shared) |
| 493 | htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss"); |
| 494 | |
| 495 | if (!htab->splt || !htab->srelplt || !htab->sdynbss |
| 496 | || (!info->shared && !htab->srelbss)) |
| 497 | abort (); |
| 498 | |
| 499 | return TRUE; |
| 500 | } |
| 501 | |
| 502 | /* Copy the extra info we tack onto an elf_link_hash_entry. */ |
| 503 | |
| 504 | static void |
| 505 | elf64_x86_64_copy_indirect_symbol (const struct elf_backend_data *bed, |
| 506 | struct elf_link_hash_entry *dir, |
| 507 | struct elf_link_hash_entry *ind) |
| 508 | { |
| 509 | struct elf64_x86_64_link_hash_entry *edir, *eind; |
| 510 | |
| 511 | edir = (struct elf64_x86_64_link_hash_entry *) dir; |
| 512 | eind = (struct elf64_x86_64_link_hash_entry *) ind; |
| 513 | |
| 514 | if (eind->dyn_relocs != NULL) |
| 515 | { |
| 516 | if (edir->dyn_relocs != NULL) |
| 517 | { |
| 518 | struct elf64_x86_64_dyn_relocs **pp; |
| 519 | struct elf64_x86_64_dyn_relocs *p; |
| 520 | |
| 521 | if (ind->root.type == bfd_link_hash_indirect) |
| 522 | abort (); |
| 523 | |
| 524 | /* Add reloc counts against the weak sym to the strong sym |
| 525 | list. Merge any entries against the same section. */ |
| 526 | for (pp = &eind->dyn_relocs; (p = *pp) != NULL; ) |
| 527 | { |
| 528 | struct elf64_x86_64_dyn_relocs *q; |
| 529 | |
| 530 | for (q = edir->dyn_relocs; q != NULL; q = q->next) |
| 531 | if (q->sec == p->sec) |
| 532 | { |
| 533 | q->pc_count += p->pc_count; |
| 534 | q->count += p->count; |
| 535 | *pp = p->next; |
| 536 | break; |
| 537 | } |
| 538 | if (q == NULL) |
| 539 | pp = &p->next; |
| 540 | } |
| 541 | *pp = edir->dyn_relocs; |
| 542 | } |
| 543 | |
| 544 | edir->dyn_relocs = eind->dyn_relocs; |
| 545 | eind->dyn_relocs = NULL; |
| 546 | } |
| 547 | |
| 548 | if (ind->root.type == bfd_link_hash_indirect |
| 549 | && dir->got.refcount <= 0) |
| 550 | { |
| 551 | edir->tls_type = eind->tls_type; |
| 552 | eind->tls_type = GOT_UNKNOWN; |
| 553 | } |
| 554 | |
| 555 | if (ELIMINATE_COPY_RELOCS |
| 556 | && ind->root.type != bfd_link_hash_indirect |
| 557 | && dir->dynamic_adjusted) |
| 558 | { |
| 559 | /* If called to transfer flags for a weakdef during processing |
| 560 | of elf_adjust_dynamic_symbol, don't copy non_got_ref. |
| 561 | We clear it ourselves for ELIMINATE_COPY_RELOCS. */ |
| 562 | dir->ref_dynamic |= ind->ref_dynamic; |
| 563 | dir->ref_regular |= ind->ref_regular; |
| 564 | dir->ref_regular_nonweak |= ind->ref_regular_nonweak; |
| 565 | dir->needs_plt |= ind->needs_plt; |
| 566 | dir->pointer_equality_needed |= ind->pointer_equality_needed; |
| 567 | } |
| 568 | else |
| 569 | _bfd_elf_link_hash_copy_indirect (bed, dir, ind); |
| 570 | } |
| 571 | |
| 572 | static bfd_boolean |
| 573 | elf64_x86_64_mkobject (bfd *abfd) |
| 574 | { |
| 575 | bfd_size_type amt = sizeof (struct elf64_x86_64_obj_tdata); |
| 576 | abfd->tdata.any = bfd_zalloc (abfd, amt); |
| 577 | if (abfd->tdata.any == NULL) |
| 578 | return FALSE; |
| 579 | return TRUE; |
| 580 | } |
| 581 | |
| 582 | static bfd_boolean |
| 583 | elf64_x86_64_elf_object_p (bfd *abfd) |
| 584 | { |
| 585 | /* Set the right machine number for an x86-64 elf64 file. */ |
| 586 | bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64); |
| 587 | return TRUE; |
| 588 | } |
| 589 | |
| 590 | static int |
| 591 | elf64_x86_64_tls_transition (struct bfd_link_info *info, int r_type, int is_local) |
| 592 | { |
| 593 | if (info->shared) |
| 594 | return r_type; |
| 595 | |
| 596 | switch (r_type) |
| 597 | { |
| 598 | case R_X86_64_TLSGD: |
| 599 | case R_X86_64_GOTTPOFF: |
| 600 | if (is_local) |
| 601 | return R_X86_64_TPOFF32; |
| 602 | return R_X86_64_GOTTPOFF; |
| 603 | case R_X86_64_TLSLD: |
| 604 | return R_X86_64_TPOFF32; |
| 605 | } |
| 606 | |
| 607 | return r_type; |
| 608 | } |
| 609 | |
| 610 | /* Look through the relocs for a section during the first phase, and |
| 611 | calculate needed space in the global offset table, procedure |
| 612 | linkage table, and dynamic reloc sections. */ |
| 613 | |
| 614 | static bfd_boolean |
| 615 | elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec, |
| 616 | const Elf_Internal_Rela *relocs) |
| 617 | { |
| 618 | struct elf64_x86_64_link_hash_table *htab; |
| 619 | Elf_Internal_Shdr *symtab_hdr; |
| 620 | struct elf_link_hash_entry **sym_hashes; |
| 621 | const Elf_Internal_Rela *rel; |
| 622 | const Elf_Internal_Rela *rel_end; |
| 623 | asection *sreloc; |
| 624 | |
| 625 | if (info->relocatable) |
| 626 | return TRUE; |
| 627 | |
| 628 | htab = elf64_x86_64_hash_table (info); |
| 629 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
| 630 | sym_hashes = elf_sym_hashes (abfd); |
| 631 | |
| 632 | sreloc = NULL; |
| 633 | |
| 634 | rel_end = relocs + sec->reloc_count; |
| 635 | for (rel = relocs; rel < rel_end; rel++) |
| 636 | { |
| 637 | unsigned int r_type; |
| 638 | unsigned long r_symndx; |
| 639 | struct elf_link_hash_entry *h; |
| 640 | |
| 641 | r_symndx = ELF64_R_SYM (rel->r_info); |
| 642 | r_type = ELF64_R_TYPE (rel->r_info); |
| 643 | |
| 644 | if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr)) |
| 645 | { |
| 646 | (*_bfd_error_handler) (_("%B: bad symbol index: %d"), |
| 647 | abfd, r_symndx); |
| 648 | return FALSE; |
| 649 | } |
| 650 | |
| 651 | if (r_symndx < symtab_hdr->sh_info) |
| 652 | h = NULL; |
| 653 | else |
| 654 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; |
| 655 | |
| 656 | r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL); |
| 657 | switch (r_type) |
| 658 | { |
| 659 | case R_X86_64_TLSLD: |
| 660 | htab->tls_ld_got.refcount += 1; |
| 661 | goto create_got; |
| 662 | |
| 663 | case R_X86_64_TPOFF32: |
| 664 | if (info->shared) |
| 665 | { |
| 666 | (*_bfd_error_handler) |
| 667 | (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"), |
| 668 | abfd, |
| 669 | x86_64_elf_howto_table[r_type].name, |
| 670 | (h) ? h->root.root.string : "a local symbol"); |
| 671 | bfd_set_error (bfd_error_bad_value); |
| 672 | return FALSE; |
| 673 | } |
| 674 | break; |
| 675 | |
| 676 | case R_X86_64_GOTTPOFF: |
| 677 | if (info->shared) |
| 678 | info->flags |= DF_STATIC_TLS; |
| 679 | /* Fall through */ |
| 680 | |
| 681 | case R_X86_64_GOT32: |
| 682 | case R_X86_64_GOTPCREL: |
| 683 | case R_X86_64_TLSGD: |
| 684 | /* This symbol requires a global offset table entry. */ |
| 685 | { |
| 686 | int tls_type, old_tls_type; |
| 687 | |
| 688 | switch (r_type) |
| 689 | { |
| 690 | default: tls_type = GOT_NORMAL; break; |
| 691 | case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break; |
| 692 | case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break; |
| 693 | } |
| 694 | |
| 695 | if (h != NULL) |
| 696 | { |
| 697 | h->got.refcount += 1; |
| 698 | old_tls_type = elf64_x86_64_hash_entry (h)->tls_type; |
| 699 | } |
| 700 | else |
| 701 | { |
| 702 | bfd_signed_vma *local_got_refcounts; |
| 703 | |
| 704 | /* This is a global offset table entry for a local symbol. */ |
| 705 | local_got_refcounts = elf_local_got_refcounts (abfd); |
| 706 | if (local_got_refcounts == NULL) |
| 707 | { |
| 708 | bfd_size_type size; |
| 709 | |
| 710 | size = symtab_hdr->sh_info; |
| 711 | size *= sizeof (bfd_signed_vma) + sizeof (char); |
| 712 | local_got_refcounts = ((bfd_signed_vma *) |
| 713 | bfd_zalloc (abfd, size)); |
| 714 | if (local_got_refcounts == NULL) |
| 715 | return FALSE; |
| 716 | elf_local_got_refcounts (abfd) = local_got_refcounts; |
| 717 | elf64_x86_64_local_got_tls_type (abfd) |
| 718 | = (char *) (local_got_refcounts + symtab_hdr->sh_info); |
| 719 | } |
| 720 | local_got_refcounts[r_symndx] += 1; |
| 721 | old_tls_type |
| 722 | = elf64_x86_64_local_got_tls_type (abfd) [r_symndx]; |
| 723 | } |
| 724 | |
| 725 | /* If a TLS symbol is accessed using IE at least once, |
| 726 | there is no point to use dynamic model for it. */ |
| 727 | if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN |
| 728 | && (old_tls_type != GOT_TLS_GD || tls_type != GOT_TLS_IE)) |
| 729 | { |
| 730 | if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD) |
| 731 | tls_type = old_tls_type; |
| 732 | else |
| 733 | { |
| 734 | (*_bfd_error_handler) |
| 735 | (_("%B: %s' accessed both as normal and thread local symbol"), |
| 736 | abfd, h ? h->root.root.string : "<local>"); |
| 737 | return FALSE; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | if (old_tls_type != tls_type) |
| 742 | { |
| 743 | if (h != NULL) |
| 744 | elf64_x86_64_hash_entry (h)->tls_type = tls_type; |
| 745 | else |
| 746 | elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type; |
| 747 | } |
| 748 | } |
| 749 | /* Fall through */ |
| 750 | |
| 751 | //case R_X86_64_GOTPCREL: |
| 752 | create_got: |
| 753 | if (htab->sgot == NULL) |
| 754 | { |
| 755 | if (htab->elf.dynobj == NULL) |
| 756 | htab->elf.dynobj = abfd; |
| 757 | if (!create_got_section (htab->elf.dynobj, info)) |
| 758 | return FALSE; |
| 759 | } |
| 760 | break; |
| 761 | |
| 762 | case R_X86_64_PLT32: |
| 763 | /* This symbol requires a procedure linkage table entry. We |
| 764 | actually build the entry in adjust_dynamic_symbol, |
| 765 | because this might be a case of linking PIC code which is |
| 766 | never referenced by a dynamic object, in which case we |
| 767 | don't need to generate a procedure linkage table entry |
| 768 | after all. */ |
| 769 | |
| 770 | /* If this is a local symbol, we resolve it directly without |
| 771 | creating a procedure linkage table entry. */ |
| 772 | if (h == NULL) |
| 773 | continue; |
| 774 | |
| 775 | h->needs_plt = 1; |
| 776 | h->plt.refcount += 1; |
| 777 | break; |
| 778 | |
| 779 | case R_X86_64_8: |
| 780 | case R_X86_64_16: |
| 781 | case R_X86_64_32: |
| 782 | case R_X86_64_32S: |
| 783 | /* Let's help debug shared library creation. These relocs |
| 784 | cannot be used in shared libs. Don't error out for |
| 785 | sections we don't care about, such as debug sections or |
| 786 | non-constant sections. */ |
| 787 | if (info->shared |
| 788 | && (sec->flags & SEC_ALLOC) != 0 |
| 789 | && (sec->flags & SEC_READONLY) != 0) |
| 790 | { |
| 791 | (*_bfd_error_handler) |
| 792 | (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"), |
| 793 | abfd, |
| 794 | x86_64_elf_howto_table[r_type].name, |
| 795 | (h) ? h->root.root.string : "a local symbol"); |
| 796 | bfd_set_error (bfd_error_bad_value); |
| 797 | return FALSE; |
| 798 | } |
| 799 | /* Fall through. */ |
| 800 | |
| 801 | case R_X86_64_PC8: |
| 802 | case R_X86_64_PC16: |
| 803 | case R_X86_64_PC32: |
| 804 | case R_X86_64_64: |
| 805 | if (h != NULL && !info->shared) |
| 806 | { |
| 807 | /* If this reloc is in a read-only section, we might |
| 808 | need a copy reloc. We can't check reliably at this |
| 809 | stage whether the section is read-only, as input |
| 810 | sections have not yet been mapped to output sections. |
| 811 | Tentatively set the flag for now, and correct in |
| 812 | adjust_dynamic_symbol. */ |
| 813 | h->non_got_ref = 1; |
| 814 | |
| 815 | /* We may need a .plt entry if the function this reloc |
| 816 | refers to is in a shared lib. */ |
| 817 | h->plt.refcount += 1; |
| 818 | if (r_type != R_X86_64_PC32) |
| 819 | h->pointer_equality_needed = 1; |
| 820 | } |
| 821 | |
| 822 | /* If we are creating a shared library, and this is a reloc |
| 823 | against a global symbol, or a non PC relative reloc |
| 824 | against a local symbol, then we need to copy the reloc |
| 825 | into the shared library. However, if we are linking with |
| 826 | -Bsymbolic, we do not need to copy a reloc against a |
| 827 | global symbol which is defined in an object we are |
| 828 | including in the link (i.e., DEF_REGULAR is set). At |
| 829 | this point we have not seen all the input files, so it is |
| 830 | possible that DEF_REGULAR is not set now but will be set |
| 831 | later (it is never cleared). In case of a weak definition, |
| 832 | DEF_REGULAR may be cleared later by a strong definition in |
| 833 | a shared library. We account for that possibility below by |
| 834 | storing information in the relocs_copied field of the hash |
| 835 | table entry. A similar situation occurs when creating |
| 836 | shared libraries and symbol visibility changes render the |
| 837 | symbol local. |
| 838 | |
| 839 | If on the other hand, we are creating an executable, we |
| 840 | may need to keep relocations for symbols satisfied by a |
| 841 | dynamic library if we manage to avoid copy relocs for the |
| 842 | symbol. */ |
| 843 | if ((info->shared |
| 844 | && (sec->flags & SEC_ALLOC) != 0 |
| 845 | && (((r_type != R_X86_64_PC8) |
| 846 | && (r_type != R_X86_64_PC16) |
| 847 | && (r_type != R_X86_64_PC32)) |
| 848 | || (h != NULL |
| 849 | && (! info->symbolic |
| 850 | || h->root.type == bfd_link_hash_defweak |
| 851 | || !h->def_regular)))) |
| 852 | || (ELIMINATE_COPY_RELOCS |
| 853 | && !info->shared |
| 854 | && (sec->flags & SEC_ALLOC) != 0 |
| 855 | && h != NULL |
| 856 | && (h->root.type == bfd_link_hash_defweak |
| 857 | || !h->def_regular))) |
| 858 | { |
| 859 | struct elf64_x86_64_dyn_relocs *p; |
| 860 | struct elf64_x86_64_dyn_relocs **head; |
| 861 | |
| 862 | /* We must copy these reloc types into the output file. |
| 863 | Create a reloc section in dynobj and make room for |
| 864 | this reloc. */ |
| 865 | if (sreloc == NULL) |
| 866 | { |
| 867 | const char *name; |
| 868 | bfd *dynobj; |
| 869 | |
| 870 | name = (bfd_elf_string_from_elf_section |
| 871 | (abfd, |
| 872 | elf_elfheader (abfd)->e_shstrndx, |
| 873 | elf_section_data (sec)->rel_hdr.sh_name)); |
| 874 | if (name == NULL) |
| 875 | return FALSE; |
| 876 | |
| 877 | if (strncmp (name, ".rela", 5) != 0 |
| 878 | || strcmp (bfd_get_section_name (abfd, sec), |
| 879 | name + 5) != 0) |
| 880 | { |
| 881 | (*_bfd_error_handler) |
| 882 | (_("%B: bad relocation section name `%s\'"), |
| 883 | abfd, name); |
| 884 | } |
| 885 | |
| 886 | if (htab->elf.dynobj == NULL) |
| 887 | htab->elf.dynobj = abfd; |
| 888 | |
| 889 | dynobj = htab->elf.dynobj; |
| 890 | |
| 891 | sreloc = bfd_get_section_by_name (dynobj, name); |
| 892 | if (sreloc == NULL) |
| 893 | { |
| 894 | flagword flags; |
| 895 | |
| 896 | sreloc = bfd_make_section (dynobj, name); |
| 897 | flags = (SEC_HAS_CONTENTS | SEC_READONLY |
| 898 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); |
| 899 | if ((sec->flags & SEC_ALLOC) != 0) |
| 900 | flags |= SEC_ALLOC | SEC_LOAD; |
| 901 | if (sreloc == NULL |
| 902 | || ! bfd_set_section_flags (dynobj, sreloc, flags) |
| 903 | || ! bfd_set_section_alignment (dynobj, sreloc, 3)) |
| 904 | return FALSE; |
| 905 | } |
| 906 | elf_section_data (sec)->sreloc = sreloc; |
| 907 | } |
| 908 | |
| 909 | /* If this is a global symbol, we count the number of |
| 910 | relocations we need for this symbol. */ |
| 911 | if (h != NULL) |
| 912 | { |
| 913 | head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs; |
| 914 | } |
| 915 | else |
| 916 | { |
| 917 | /* Track dynamic relocs needed for local syms too. |
| 918 | We really need local syms available to do this |
| 919 | easily. Oh well. */ |
| 920 | |
| 921 | asection *s; |
| 922 | s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, |
| 923 | sec, r_symndx); |
| 924 | if (s == NULL) |
| 925 | return FALSE; |
| 926 | |
| 927 | head = ((struct elf64_x86_64_dyn_relocs **) |
| 928 | &elf_section_data (s)->local_dynrel); |
| 929 | } |
| 930 | |
| 931 | p = *head; |
| 932 | if (p == NULL || p->sec != sec) |
| 933 | { |
| 934 | bfd_size_type amt = sizeof *p; |
| 935 | p = ((struct elf64_x86_64_dyn_relocs *) |
| 936 | bfd_alloc (htab->elf.dynobj, amt)); |
| 937 | if (p == NULL) |
| 938 | return FALSE; |
| 939 | p->next = *head; |
| 940 | *head = p; |
| 941 | p->sec = sec; |
| 942 | p->count = 0; |
| 943 | p->pc_count = 0; |
| 944 | } |
| 945 | |
| 946 | p->count += 1; |
| 947 | if (r_type == R_X86_64_PC8 |
| 948 | || r_type == R_X86_64_PC16 |
| 949 | || r_type == R_X86_64_PC32) |
| 950 | p->pc_count += 1; |
| 951 | } |
| 952 | break; |
| 953 | |
| 954 | /* This relocation describes the C++ object vtable hierarchy. |
| 955 | Reconstruct it for later use during GC. */ |
| 956 | case R_X86_64_GNU_VTINHERIT: |
| 957 | if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset)) |
| 958 | return FALSE; |
| 959 | break; |
| 960 | |
| 961 | /* This relocation describes which C++ vtable entries are actually |
| 962 | used. Record for later use during GC. */ |
| 963 | case R_X86_64_GNU_VTENTRY: |
| 964 | if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend)) |
| 965 | return FALSE; |
| 966 | break; |
| 967 | |
| 968 | default: |
| 969 | break; |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | return TRUE; |
| 974 | } |
| 975 | |
| 976 | /* Return the section that should be marked against GC for a given |
| 977 | relocation. */ |
| 978 | |
| 979 | static asection * |
| 980 | elf64_x86_64_gc_mark_hook (asection *sec, |
| 981 | struct bfd_link_info *info ATTRIBUTE_UNUSED, |
| 982 | Elf_Internal_Rela *rel, |
| 983 | struct elf_link_hash_entry *h, |
| 984 | Elf_Internal_Sym *sym) |
| 985 | { |
| 986 | if (h != NULL) |
| 987 | { |
| 988 | switch (ELF64_R_TYPE (rel->r_info)) |
| 989 | { |
| 990 | case R_X86_64_GNU_VTINHERIT: |
| 991 | case R_X86_64_GNU_VTENTRY: |
| 992 | break; |
| 993 | |
| 994 | default: |
| 995 | switch (h->root.type) |
| 996 | { |
| 997 | case bfd_link_hash_defined: |
| 998 | case bfd_link_hash_defweak: |
| 999 | return h->root.u.def.section; |
| 1000 | |
| 1001 | case bfd_link_hash_common: |
| 1002 | return h->root.u.c.p->section; |
| 1003 | |
| 1004 | default: |
| 1005 | break; |
| 1006 | } |
| 1007 | } |
| 1008 | } |
| 1009 | else |
| 1010 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); |
| 1011 | |
| 1012 | return NULL; |
| 1013 | } |
| 1014 | |
| 1015 | /* Update the got entry reference counts for the section being removed. */ |
| 1016 | |
| 1017 | static bfd_boolean |
| 1018 | elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info, |
| 1019 | asection *sec, const Elf_Internal_Rela *relocs) |
| 1020 | { |
| 1021 | Elf_Internal_Shdr *symtab_hdr; |
| 1022 | struct elf_link_hash_entry **sym_hashes; |
| 1023 | bfd_signed_vma *local_got_refcounts; |
| 1024 | const Elf_Internal_Rela *rel, *relend; |
| 1025 | |
| 1026 | elf_section_data (sec)->local_dynrel = NULL; |
| 1027 | |
| 1028 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
| 1029 | sym_hashes = elf_sym_hashes (abfd); |
| 1030 | local_got_refcounts = elf_local_got_refcounts (abfd); |
| 1031 | |
| 1032 | relend = relocs + sec->reloc_count; |
| 1033 | for (rel = relocs; rel < relend; rel++) |
| 1034 | { |
| 1035 | unsigned long r_symndx; |
| 1036 | unsigned int r_type; |
| 1037 | struct elf_link_hash_entry *h = NULL; |
| 1038 | |
| 1039 | r_symndx = ELF64_R_SYM (rel->r_info); |
| 1040 | if (r_symndx >= symtab_hdr->sh_info) |
| 1041 | { |
| 1042 | struct elf64_x86_64_link_hash_entry *eh; |
| 1043 | struct elf64_x86_64_dyn_relocs **pp; |
| 1044 | struct elf64_x86_64_dyn_relocs *p; |
| 1045 | |
| 1046 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; |
| 1047 | while (h->root.type == bfd_link_hash_indirect |
| 1048 | || h->root.type == bfd_link_hash_warning) |
| 1049 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
| 1050 | eh = (struct elf64_x86_64_link_hash_entry *) h; |
| 1051 | |
| 1052 | for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next) |
| 1053 | if (p->sec == sec) |
| 1054 | { |
| 1055 | /* Everything must go for SEC. */ |
| 1056 | *pp = p->next; |
| 1057 | break; |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | r_type = ELF64_R_TYPE (rel->r_info); |
| 1062 | r_type = elf64_x86_64_tls_transition (info, r_type, h != NULL); |
| 1063 | switch (r_type) |
| 1064 | { |
| 1065 | case R_X86_64_TLSLD: |
| 1066 | if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0) |
| 1067 | elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1; |
| 1068 | break; |
| 1069 | |
| 1070 | case R_X86_64_TLSGD: |
| 1071 | case R_X86_64_GOTTPOFF: |
| 1072 | case R_X86_64_GOT32: |
| 1073 | case R_X86_64_GOTPCREL: |
| 1074 | if (h != NULL) |
| 1075 | { |
| 1076 | if (h->got.refcount > 0) |
| 1077 | h->got.refcount -= 1; |
| 1078 | } |
| 1079 | else if (local_got_refcounts != NULL) |
| 1080 | { |
| 1081 | if (local_got_refcounts[r_symndx] > 0) |
| 1082 | local_got_refcounts[r_symndx] -= 1; |
| 1083 | } |
| 1084 | break; |
| 1085 | |
| 1086 | case R_X86_64_8: |
| 1087 | case R_X86_64_16: |
| 1088 | case R_X86_64_32: |
| 1089 | case R_X86_64_64: |
| 1090 | case R_X86_64_32S: |
| 1091 | case R_X86_64_PC8: |
| 1092 | case R_X86_64_PC16: |
| 1093 | case R_X86_64_PC32: |
| 1094 | if (info->shared) |
| 1095 | break; |
| 1096 | /* Fall thru */ |
| 1097 | |
| 1098 | case R_X86_64_PLT32: |
| 1099 | if (h != NULL) |
| 1100 | { |
| 1101 | if (h->plt.refcount > 0) |
| 1102 | h->plt.refcount -= 1; |
| 1103 | } |
| 1104 | break; |
| 1105 | |
| 1106 | default: |
| 1107 | break; |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | return TRUE; |
| 1112 | } |
| 1113 | |
| 1114 | /* Adjust a symbol defined by a dynamic object and referenced by a |
| 1115 | regular object. The current definition is in some section of the |
| 1116 | dynamic object, but we're not including those sections. We have to |
| 1117 | change the definition to something the rest of the link can |
| 1118 | understand. */ |
| 1119 | |
| 1120 | static bfd_boolean |
| 1121 | elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info, |
| 1122 | struct elf_link_hash_entry *h) |
| 1123 | { |
| 1124 | struct elf64_x86_64_link_hash_table *htab; |
| 1125 | asection *s; |
| 1126 | unsigned int power_of_two; |
| 1127 | |
| 1128 | /* If this is a function, put it in the procedure linkage table. We |
| 1129 | will fill in the contents of the procedure linkage table later, |
| 1130 | when we know the address of the .got section. */ |
| 1131 | if (h->type == STT_FUNC |
| 1132 | || h->needs_plt) |
| 1133 | { |
| 1134 | if (h->plt.refcount <= 0 |
| 1135 | || SYMBOL_CALLS_LOCAL (info, h) |
| 1136 | || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
| 1137 | && h->root.type == bfd_link_hash_undefweak)) |
| 1138 | { |
| 1139 | /* This case can occur if we saw a PLT32 reloc in an input |
| 1140 | file, but the symbol was never referred to by a dynamic |
| 1141 | object, or if all references were garbage collected. In |
| 1142 | such a case, we don't actually need to build a procedure |
| 1143 | linkage table, and we can just do a PC32 reloc instead. */ |
| 1144 | h->plt.offset = (bfd_vma) -1; |
| 1145 | h->needs_plt = 0; |
| 1146 | } |
| 1147 | |
| 1148 | return TRUE; |
| 1149 | } |
| 1150 | else |
| 1151 | /* It's possible that we incorrectly decided a .plt reloc was |
| 1152 | needed for an R_X86_64_PC32 reloc to a non-function sym in |
| 1153 | check_relocs. We can't decide accurately between function and |
| 1154 | non-function syms in check-relocs; Objects loaded later in |
| 1155 | the link may change h->type. So fix it now. */ |
| 1156 | h->plt.offset = (bfd_vma) -1; |
| 1157 | |
| 1158 | /* If this is a weak symbol, and there is a real definition, the |
| 1159 | processor independent code will have arranged for us to see the |
| 1160 | real definition first, and we can just use the same value. */ |
| 1161 | if (h->u.weakdef != NULL) |
| 1162 | { |
| 1163 | BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined |
| 1164 | || h->u.weakdef->root.type == bfd_link_hash_defweak); |
| 1165 | h->root.u.def.section = h->u.weakdef->root.u.def.section; |
| 1166 | h->root.u.def.value = h->u.weakdef->root.u.def.value; |
| 1167 | if (ELIMINATE_COPY_RELOCS || info->nocopyreloc) |
| 1168 | h->non_got_ref = h->u.weakdef->non_got_ref; |
| 1169 | return TRUE; |
| 1170 | } |
| 1171 | |
| 1172 | /* This is a reference to a symbol defined by a dynamic object which |
| 1173 | is not a function. */ |
| 1174 | |
| 1175 | /* If we are creating a shared library, we must presume that the |
| 1176 | only references to the symbol are via the global offset table. |
| 1177 | For such cases we need not do anything here; the relocations will |
| 1178 | be handled correctly by relocate_section. */ |
| 1179 | if (info->shared) |
| 1180 | return TRUE; |
| 1181 | |
| 1182 | /* If there are no references to this symbol that do not use the |
| 1183 | GOT, we don't need to generate a copy reloc. */ |
| 1184 | if (!h->non_got_ref) |
| 1185 | return TRUE; |
| 1186 | |
| 1187 | /* If -z nocopyreloc was given, we won't generate them either. */ |
| 1188 | if (info->nocopyreloc) |
| 1189 | { |
| 1190 | h->non_got_ref = 0; |
| 1191 | return TRUE; |
| 1192 | } |
| 1193 | |
| 1194 | if (ELIMINATE_COPY_RELOCS) |
| 1195 | { |
| 1196 | struct elf64_x86_64_link_hash_entry * eh; |
| 1197 | struct elf64_x86_64_dyn_relocs *p; |
| 1198 | |
| 1199 | eh = (struct elf64_x86_64_link_hash_entry *) h; |
| 1200 | for (p = eh->dyn_relocs; p != NULL; p = p->next) |
| 1201 | { |
| 1202 | s = p->sec->output_section; |
| 1203 | if (s != NULL && (s->flags & SEC_READONLY) != 0) |
| 1204 | break; |
| 1205 | } |
| 1206 | |
| 1207 | /* If we didn't find any dynamic relocs in read-only sections, then |
| 1208 | we'll be keeping the dynamic relocs and avoiding the copy reloc. */ |
| 1209 | if (p == NULL) |
| 1210 | { |
| 1211 | h->non_got_ref = 0; |
| 1212 | return TRUE; |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | /* We must allocate the symbol in our .dynbss section, which will |
| 1217 | become part of the .bss section of the executable. There will be |
| 1218 | an entry for this symbol in the .dynsym section. The dynamic |
| 1219 | object will contain position independent code, so all references |
| 1220 | from the dynamic object to this symbol will go through the global |
| 1221 | offset table. The dynamic linker will use the .dynsym entry to |
| 1222 | determine the address it must put in the global offset table, so |
| 1223 | both the dynamic object and the regular object will refer to the |
| 1224 | same memory location for the variable. */ |
| 1225 | |
| 1226 | htab = elf64_x86_64_hash_table (info); |
| 1227 | |
| 1228 | /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker |
| 1229 | to copy the initial value out of the dynamic object and into the |
| 1230 | runtime process image. */ |
| 1231 | if ((h->root.u.def.section->flags & SEC_ALLOC) != 0) |
| 1232 | { |
| 1233 | htab->srelbss->size += sizeof (Elf64_External_Rela); |
| 1234 | h->needs_copy = 1; |
| 1235 | } |
| 1236 | |
| 1237 | /* We need to figure out the alignment required for this symbol. I |
| 1238 | have no idea how ELF linkers handle this. 16-bytes is the size |
| 1239 | of the largest type that requires hard alignment -- long double. */ |
| 1240 | /* FIXME: This is VERY ugly. Should be fixed for all architectures using |
| 1241 | this construct. */ |
| 1242 | power_of_two = bfd_log2 (h->size); |
| 1243 | if (power_of_two > 4) |
| 1244 | power_of_two = 4; |
| 1245 | |
| 1246 | /* Apply the required alignment. */ |
| 1247 | s = htab->sdynbss; |
| 1248 | s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two)); |
| 1249 | if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s)) |
| 1250 | { |
| 1251 | if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two)) |
| 1252 | return FALSE; |
| 1253 | } |
| 1254 | |
| 1255 | /* Define the symbol as being at this point in the section. */ |
| 1256 | h->root.u.def.section = s; |
| 1257 | h->root.u.def.value = s->size; |
| 1258 | |
| 1259 | /* Increment the section size to make room for the symbol. */ |
| 1260 | s->size += h->size; |
| 1261 | |
| 1262 | return TRUE; |
| 1263 | } |
| 1264 | |
| 1265 | /* Allocate space in .plt, .got and associated reloc sections for |
| 1266 | dynamic relocs. */ |
| 1267 | |
| 1268 | static bfd_boolean |
| 1269 | allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf) |
| 1270 | { |
| 1271 | struct bfd_link_info *info; |
| 1272 | struct elf64_x86_64_link_hash_table *htab; |
| 1273 | struct elf64_x86_64_link_hash_entry *eh; |
| 1274 | struct elf64_x86_64_dyn_relocs *p; |
| 1275 | |
| 1276 | if (h->root.type == bfd_link_hash_indirect) |
| 1277 | return TRUE; |
| 1278 | |
| 1279 | if (h->root.type == bfd_link_hash_warning) |
| 1280 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
| 1281 | |
| 1282 | info = (struct bfd_link_info *) inf; |
| 1283 | htab = elf64_x86_64_hash_table (info); |
| 1284 | |
| 1285 | if (htab->elf.dynamic_sections_created |
| 1286 | && h->plt.refcount > 0) |
| 1287 | { |
| 1288 | /* Make sure this symbol is output as a dynamic symbol. |
| 1289 | Undefined weak syms won't yet be marked as dynamic. */ |
| 1290 | if (h->dynindx == -1 |
| 1291 | && !h->forced_local) |
| 1292 | { |
| 1293 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
| 1294 | return FALSE; |
| 1295 | } |
| 1296 | |
| 1297 | if (info->shared |
| 1298 | || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h)) |
| 1299 | { |
| 1300 | asection *s = htab->splt; |
| 1301 | |
| 1302 | /* If this is the first .plt entry, make room for the special |
| 1303 | first entry. */ |
| 1304 | if (s->size == 0) |
| 1305 | s->size += PLT_ENTRY_SIZE; |
| 1306 | |
| 1307 | h->plt.offset = s->size; |
| 1308 | |
| 1309 | /* If this symbol is not defined in a regular file, and we are |
| 1310 | not generating a shared library, then set the symbol to this |
| 1311 | location in the .plt. This is required to make function |
| 1312 | pointers compare as equal between the normal executable and |
| 1313 | the shared library. */ |
| 1314 | if (! info->shared |
| 1315 | && !h->def_regular) |
| 1316 | { |
| 1317 | h->root.u.def.section = s; |
| 1318 | h->root.u.def.value = h->plt.offset; |
| 1319 | } |
| 1320 | |
| 1321 | /* Make room for this entry. */ |
| 1322 | s->size += PLT_ENTRY_SIZE; |
| 1323 | |
| 1324 | /* We also need to make an entry in the .got.plt section, which |
| 1325 | will be placed in the .got section by the linker script. */ |
| 1326 | htab->sgotplt->size += GOT_ENTRY_SIZE; |
| 1327 | |
| 1328 | /* We also need to make an entry in the .rela.plt section. */ |
| 1329 | htab->srelplt->size += sizeof (Elf64_External_Rela); |
| 1330 | } |
| 1331 | else |
| 1332 | { |
| 1333 | h->plt.offset = (bfd_vma) -1; |
| 1334 | h->needs_plt = 0; |
| 1335 | } |
| 1336 | } |
| 1337 | else |
| 1338 | { |
| 1339 | h->plt.offset = (bfd_vma) -1; |
| 1340 | h->needs_plt = 0; |
| 1341 | } |
| 1342 | |
| 1343 | /* If R_X86_64_GOTTPOFF symbol is now local to the binary, |
| 1344 | make it a R_X86_64_TPOFF32 requiring no GOT entry. */ |
| 1345 | if (h->got.refcount > 0 |
| 1346 | && !info->shared |
| 1347 | && h->dynindx == -1 |
| 1348 | && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE) |
| 1349 | h->got.offset = (bfd_vma) -1; |
| 1350 | else if (h->got.refcount > 0) |
| 1351 | { |
| 1352 | asection *s; |
| 1353 | bfd_boolean dyn; |
| 1354 | int tls_type = elf64_x86_64_hash_entry (h)->tls_type; |
| 1355 | |
| 1356 | /* Make sure this symbol is output as a dynamic symbol. |
| 1357 | Undefined weak syms won't yet be marked as dynamic. */ |
| 1358 | if (h->dynindx == -1 |
| 1359 | && !h->forced_local) |
| 1360 | { |
| 1361 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
| 1362 | return FALSE; |
| 1363 | } |
| 1364 | |
| 1365 | s = htab->sgot; |
| 1366 | h->got.offset = s->size; |
| 1367 | s->size += GOT_ENTRY_SIZE; |
| 1368 | /* R_X86_64_TLSGD needs 2 consecutive GOT slots. */ |
| 1369 | if (tls_type == GOT_TLS_GD) |
| 1370 | s->size += GOT_ENTRY_SIZE; |
| 1371 | dyn = htab->elf.dynamic_sections_created; |
| 1372 | /* R_X86_64_TLSGD needs one dynamic relocation if local symbol |
| 1373 | and two if global. |
| 1374 | R_X86_64_GOTTPOFF needs one dynamic relocation. */ |
| 1375 | if ((tls_type == GOT_TLS_GD && h->dynindx == -1) |
| 1376 | || tls_type == GOT_TLS_IE) |
| 1377 | htab->srelgot->size += sizeof (Elf64_External_Rela); |
| 1378 | else if (tls_type == GOT_TLS_GD) |
| 1379 | htab->srelgot->size += 2 * sizeof (Elf64_External_Rela); |
| 1380 | else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT |
| 1381 | || h->root.type != bfd_link_hash_undefweak) |
| 1382 | && (info->shared |
| 1383 | || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))) |
| 1384 | htab->srelgot->size += sizeof (Elf64_External_Rela); |
| 1385 | } |
| 1386 | else |
| 1387 | h->got.offset = (bfd_vma) -1; |
| 1388 | |
| 1389 | eh = (struct elf64_x86_64_link_hash_entry *) h; |
| 1390 | if (eh->dyn_relocs == NULL) |
| 1391 | return TRUE; |
| 1392 | |
| 1393 | /* In the shared -Bsymbolic case, discard space allocated for |
| 1394 | dynamic pc-relative relocs against symbols which turn out to be |
| 1395 | defined in regular objects. For the normal shared case, discard |
| 1396 | space for pc-relative relocs that have become local due to symbol |
| 1397 | visibility changes. */ |
| 1398 | |
| 1399 | if (info->shared) |
| 1400 | { |
| 1401 | /* Relocs that use pc_count are those that appear on a call |
| 1402 | insn, or certain REL relocs that can generated via assembly. |
| 1403 | We want calls to protected symbols to resolve directly to the |
| 1404 | function rather than going via the plt. If people want |
| 1405 | function pointer comparisons to work as expected then they |
| 1406 | should avoid writing weird assembly. */ |
| 1407 | if (SYMBOL_CALLS_LOCAL (info, h)) |
| 1408 | { |
| 1409 | struct elf64_x86_64_dyn_relocs **pp; |
| 1410 | |
| 1411 | for (pp = &eh->dyn_relocs; (p = *pp) != NULL; ) |
| 1412 | { |
| 1413 | p->count -= p->pc_count; |
| 1414 | p->pc_count = 0; |
| 1415 | if (p->count == 0) |
| 1416 | *pp = p->next; |
| 1417 | else |
| 1418 | pp = &p->next; |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | /* Also discard relocs on undefined weak syms with non-default |
| 1423 | visibility. */ |
| 1424 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
| 1425 | && h->root.type == bfd_link_hash_undefweak) |
| 1426 | eh->dyn_relocs = NULL; |
| 1427 | } |
| 1428 | else if (ELIMINATE_COPY_RELOCS) |
| 1429 | { |
| 1430 | /* For the non-shared case, discard space for relocs against |
| 1431 | symbols which turn out to need copy relocs or are not |
| 1432 | dynamic. */ |
| 1433 | |
| 1434 | if (!h->non_got_ref |
| 1435 | && ((h->def_dynamic |
| 1436 | && !h->def_regular) |
| 1437 | || (htab->elf.dynamic_sections_created |
| 1438 | && (h->root.type == bfd_link_hash_undefweak |
| 1439 | || h->root.type == bfd_link_hash_undefined)))) |
| 1440 | { |
| 1441 | /* Make sure this symbol is output as a dynamic symbol. |
| 1442 | Undefined weak syms won't yet be marked as dynamic. */ |
| 1443 | if (h->dynindx == -1 |
| 1444 | && !h->forced_local) |
| 1445 | { |
| 1446 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
| 1447 | return FALSE; |
| 1448 | } |
| 1449 | |
| 1450 | /* If that succeeded, we know we'll be keeping all the |
| 1451 | relocs. */ |
| 1452 | if (h->dynindx != -1) |
| 1453 | goto keep; |
| 1454 | } |
| 1455 | |
| 1456 | eh->dyn_relocs = NULL; |
| 1457 | |
| 1458 | keep: ; |
| 1459 | } |
| 1460 | |
| 1461 | /* Finally, allocate space. */ |
| 1462 | for (p = eh->dyn_relocs; p != NULL; p = p->next) |
| 1463 | { |
| 1464 | asection *sreloc = elf_section_data (p->sec)->sreloc; |
| 1465 | sreloc->size += p->count * sizeof (Elf64_External_Rela); |
| 1466 | } |
| 1467 | |
| 1468 | return TRUE; |
| 1469 | } |
| 1470 | |
| 1471 | /* Find any dynamic relocs that apply to read-only sections. */ |
| 1472 | |
| 1473 | static bfd_boolean |
| 1474 | readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf) |
| 1475 | { |
| 1476 | struct elf64_x86_64_link_hash_entry *eh; |
| 1477 | struct elf64_x86_64_dyn_relocs *p; |
| 1478 | |
| 1479 | if (h->root.type == bfd_link_hash_warning) |
| 1480 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
| 1481 | |
| 1482 | eh = (struct elf64_x86_64_link_hash_entry *) h; |
| 1483 | for (p = eh->dyn_relocs; p != NULL; p = p->next) |
| 1484 | { |
| 1485 | asection *s = p->sec->output_section; |
| 1486 | |
| 1487 | if (s != NULL && (s->flags & SEC_READONLY) != 0) |
| 1488 | { |
| 1489 | struct bfd_link_info *info = (struct bfd_link_info *) inf; |
| 1490 | |
| 1491 | info->flags |= DF_TEXTREL; |
| 1492 | |
| 1493 | /* Not an error, just cut short the traversal. */ |
| 1494 | return FALSE; |
| 1495 | } |
| 1496 | } |
| 1497 | return TRUE; |
| 1498 | } |
| 1499 | |
| 1500 | /* Set the sizes of the dynamic sections. */ |
| 1501 | |
| 1502 | static bfd_boolean |
| 1503 | elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED, |
| 1504 | struct bfd_link_info *info) |
| 1505 | { |
| 1506 | struct elf64_x86_64_link_hash_table *htab; |
| 1507 | bfd *dynobj; |
| 1508 | asection *s; |
| 1509 | bfd_boolean relocs; |
| 1510 | bfd *ibfd; |
| 1511 | |
| 1512 | htab = elf64_x86_64_hash_table (info); |
| 1513 | dynobj = htab->elf.dynobj; |
| 1514 | if (dynobj == NULL) |
| 1515 | abort (); |
| 1516 | |
| 1517 | if (htab->elf.dynamic_sections_created) |
| 1518 | { |
| 1519 | /* Set the contents of the .interp section to the interpreter. */ |
| 1520 | if (info->executable) |
| 1521 | { |
| 1522 | s = bfd_get_section_by_name (dynobj, ".interp"); |
| 1523 | if (s == NULL) |
| 1524 | abort (); |
| 1525 | s->size = sizeof ELF_DYNAMIC_INTERPRETER; |
| 1526 | s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | /* Set up .got offsets for local syms, and space for local dynamic |
| 1531 | relocs. */ |
| 1532 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) |
| 1533 | { |
| 1534 | bfd_signed_vma *local_got; |
| 1535 | bfd_signed_vma *end_local_got; |
| 1536 | char *local_tls_type; |
| 1537 | bfd_size_type locsymcount; |
| 1538 | Elf_Internal_Shdr *symtab_hdr; |
| 1539 | asection *srel; |
| 1540 | |
| 1541 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) |
| 1542 | continue; |
| 1543 | |
| 1544 | for (s = ibfd->sections; s != NULL; s = s->next) |
| 1545 | { |
| 1546 | struct elf64_x86_64_dyn_relocs *p; |
| 1547 | |
| 1548 | for (p = *((struct elf64_x86_64_dyn_relocs **) |
| 1549 | &elf_section_data (s)->local_dynrel); |
| 1550 | p != NULL; |
| 1551 | p = p->next) |
| 1552 | { |
| 1553 | if (!bfd_is_abs_section (p->sec) |
| 1554 | && bfd_is_abs_section (p->sec->output_section)) |
| 1555 | { |
| 1556 | /* Input section has been discarded, either because |
| 1557 | it is a copy of a linkonce section or due to |
| 1558 | linker script /DISCARD/, so we'll be discarding |
| 1559 | the relocs too. */ |
| 1560 | } |
| 1561 | else if (p->count != 0) |
| 1562 | { |
| 1563 | srel = elf_section_data (p->sec)->sreloc; |
| 1564 | srel->size += p->count * sizeof (Elf64_External_Rela); |
| 1565 | if ((p->sec->output_section->flags & SEC_READONLY) != 0) |
| 1566 | info->flags |= DF_TEXTREL; |
| 1567 | |
| 1568 | } |
| 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | local_got = elf_local_got_refcounts (ibfd); |
| 1573 | if (!local_got) |
| 1574 | continue; |
| 1575 | |
| 1576 | symtab_hdr = &elf_tdata (ibfd)->symtab_hdr; |
| 1577 | locsymcount = symtab_hdr->sh_info; |
| 1578 | end_local_got = local_got + locsymcount; |
| 1579 | local_tls_type = elf64_x86_64_local_got_tls_type (ibfd); |
| 1580 | s = htab->sgot; |
| 1581 | srel = htab->srelgot; |
| 1582 | for (; local_got < end_local_got; ++local_got, ++local_tls_type) |
| 1583 | { |
| 1584 | if (*local_got > 0) |
| 1585 | { |
| 1586 | *local_got = s->size; |
| 1587 | s->size += GOT_ENTRY_SIZE; |
| 1588 | if (*local_tls_type == GOT_TLS_GD) |
| 1589 | s->size += GOT_ENTRY_SIZE; |
| 1590 | if (info->shared |
| 1591 | || *local_tls_type == GOT_TLS_GD |
| 1592 | || *local_tls_type == GOT_TLS_IE) |
| 1593 | srel->size += sizeof (Elf64_External_Rela); |
| 1594 | } |
| 1595 | else |
| 1596 | *local_got = (bfd_vma) -1; |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | if (htab->tls_ld_got.refcount > 0) |
| 1601 | { |
| 1602 | /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD |
| 1603 | relocs. */ |
| 1604 | htab->tls_ld_got.offset = htab->sgot->size; |
| 1605 | htab->sgot->size += 2 * GOT_ENTRY_SIZE; |
| 1606 | htab->srelgot->size += sizeof (Elf64_External_Rela); |
| 1607 | } |
| 1608 | else |
| 1609 | htab->tls_ld_got.offset = -1; |
| 1610 | |
| 1611 | /* Allocate global sym .plt and .got entries, and space for global |
| 1612 | sym dynamic relocs. */ |
| 1613 | elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info); |
| 1614 | |
| 1615 | /* We now have determined the sizes of the various dynamic sections. |
| 1616 | Allocate memory for them. */ |
| 1617 | relocs = FALSE; |
| 1618 | for (s = dynobj->sections; s != NULL; s = s->next) |
| 1619 | { |
| 1620 | if ((s->flags & SEC_LINKER_CREATED) == 0) |
| 1621 | continue; |
| 1622 | |
| 1623 | if (s == htab->splt |
| 1624 | || s == htab->sgot |
| 1625 | || s == htab->sgotplt) |
| 1626 | { |
| 1627 | /* Strip this section if we don't need it; see the |
| 1628 | comment below. */ |
| 1629 | } |
| 1630 | else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0) |
| 1631 | { |
| 1632 | if (s->size != 0 && s != htab->srelplt) |
| 1633 | relocs = TRUE; |
| 1634 | |
| 1635 | /* We use the reloc_count field as a counter if we need |
| 1636 | to copy relocs into the output file. */ |
| 1637 | s->reloc_count = 0; |
| 1638 | } |
| 1639 | else |
| 1640 | { |
| 1641 | /* It's not one of our sections, so don't allocate space. */ |
| 1642 | continue; |
| 1643 | } |
| 1644 | |
| 1645 | if (s->size == 0) |
| 1646 | { |
| 1647 | /* If we don't need this section, strip it from the |
| 1648 | output file. This is mostly to handle .rela.bss and |
| 1649 | .rela.plt. We must create both sections in |
| 1650 | create_dynamic_sections, because they must be created |
| 1651 | before the linker maps input sections to output |
| 1652 | sections. The linker does that before |
| 1653 | adjust_dynamic_symbol is called, and it is that |
| 1654 | function which decides whether anything needs to go |
| 1655 | into these sections. */ |
| 1656 | |
| 1657 | s->flags |= SEC_EXCLUDE; |
| 1658 | continue; |
| 1659 | } |
| 1660 | |
| 1661 | /* Allocate memory for the section contents. We use bfd_zalloc |
| 1662 | here in case unused entries are not reclaimed before the |
| 1663 | section's contents are written out. This should not happen, |
| 1664 | but this way if it does, we get a R_X86_64_NONE reloc instead |
| 1665 | of garbage. */ |
| 1666 | s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size); |
| 1667 | if (s->contents == NULL) |
| 1668 | return FALSE; |
| 1669 | } |
| 1670 | |
| 1671 | if (htab->elf.dynamic_sections_created) |
| 1672 | { |
| 1673 | /* Add some entries to the .dynamic section. We fill in the |
| 1674 | values later, in elf64_x86_64_finish_dynamic_sections, but we |
| 1675 | must add the entries now so that we get the correct size for |
| 1676 | the .dynamic section. The DT_DEBUG entry is filled in by the |
| 1677 | dynamic linker and used by the debugger. */ |
| 1678 | #define add_dynamic_entry(TAG, VAL) \ |
| 1679 | _bfd_elf_add_dynamic_entry (info, TAG, VAL) |
| 1680 | |
| 1681 | if (info->executable) |
| 1682 | { |
| 1683 | if (!add_dynamic_entry (DT_DEBUG, 0)) |
| 1684 | return FALSE; |
| 1685 | } |
| 1686 | |
| 1687 | if (htab->splt->size != 0) |
| 1688 | { |
| 1689 | if (!add_dynamic_entry (DT_PLTGOT, 0) |
| 1690 | || !add_dynamic_entry (DT_PLTRELSZ, 0) |
| 1691 | || !add_dynamic_entry (DT_PLTREL, DT_RELA) |
| 1692 | || !add_dynamic_entry (DT_JMPREL, 0)) |
| 1693 | return FALSE; |
| 1694 | } |
| 1695 | |
| 1696 | if (relocs) |
| 1697 | { |
| 1698 | if (!add_dynamic_entry (DT_RELA, 0) |
| 1699 | || !add_dynamic_entry (DT_RELASZ, 0) |
| 1700 | || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela))) |
| 1701 | return FALSE; |
| 1702 | |
| 1703 | /* If any dynamic relocs apply to a read-only section, |
| 1704 | then we need a DT_TEXTREL entry. */ |
| 1705 | if ((info->flags & DF_TEXTREL) == 0) |
| 1706 | elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, |
| 1707 | (PTR) info); |
| 1708 | |
| 1709 | if ((info->flags & DF_TEXTREL) != 0) |
| 1710 | { |
| 1711 | if (!add_dynamic_entry (DT_TEXTREL, 0)) |
| 1712 | return FALSE; |
| 1713 | } |
| 1714 | } |
| 1715 | } |
| 1716 | #undef add_dynamic_entry |
| 1717 | |
| 1718 | return TRUE; |
| 1719 | } |
| 1720 | |
| 1721 | /* Return the base VMA address which should be subtracted from real addresses |
| 1722 | when resolving @dtpoff relocation. |
| 1723 | This is PT_TLS segment p_vaddr. */ |
| 1724 | |
| 1725 | static bfd_vma |
| 1726 | dtpoff_base (struct bfd_link_info *info) |
| 1727 | { |
| 1728 | /* If tls_sec is NULL, we should have signalled an error already. */ |
| 1729 | if (elf_hash_table (info)->tls_sec == NULL) |
| 1730 | return 0; |
| 1731 | return elf_hash_table (info)->tls_sec->vma; |
| 1732 | } |
| 1733 | |
| 1734 | /* Return the relocation value for @tpoff relocation |
| 1735 | if STT_TLS virtual address is ADDRESS. */ |
| 1736 | |
| 1737 | static bfd_vma |
| 1738 | tpoff (struct bfd_link_info *info, bfd_vma address) |
| 1739 | { |
| 1740 | struct elf_link_hash_table *htab = elf_hash_table (info); |
| 1741 | |
| 1742 | /* If tls_segment is NULL, we should have signalled an error already. */ |
| 1743 | if (htab->tls_sec == NULL) |
| 1744 | return 0; |
| 1745 | return address - htab->tls_size - htab->tls_sec->vma; |
| 1746 | } |
| 1747 | |
| 1748 | /* Is the instruction before OFFSET in CONTENTS a 32bit relative |
| 1749 | branch? */ |
| 1750 | |
| 1751 | static bfd_boolean |
| 1752 | is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset) |
| 1753 | { |
| 1754 | /* Opcode Instruction |
| 1755 | 0xe8 call |
| 1756 | 0xe9 jump |
| 1757 | 0x0f 0x8x conditional jump */ |
| 1758 | return ((offset > 0 |
| 1759 | && (contents [offset - 1] == 0xe8 |
| 1760 | || contents [offset - 1] == 0xe9)) |
| 1761 | || (offset > 1 |
| 1762 | && contents [offset - 2] == 0x0f |
| 1763 | && (contents [offset - 1] & 0xf0) == 0x80)); |
| 1764 | } |
| 1765 | |
| 1766 | /* Relocate an x86_64 ELF section. */ |
| 1767 | |
| 1768 | static bfd_boolean |
| 1769 | elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info, |
| 1770 | bfd *input_bfd, asection *input_section, |
| 1771 | bfd_byte *contents, Elf_Internal_Rela *relocs, |
| 1772 | Elf_Internal_Sym *local_syms, |
| 1773 | asection **local_sections) |
| 1774 | { |
| 1775 | struct elf64_x86_64_link_hash_table *htab; |
| 1776 | Elf_Internal_Shdr *symtab_hdr; |
| 1777 | struct elf_link_hash_entry **sym_hashes; |
| 1778 | bfd_vma *local_got_offsets; |
| 1779 | Elf_Internal_Rela *rel; |
| 1780 | Elf_Internal_Rela *relend; |
| 1781 | |
| 1782 | if (info->relocatable) |
| 1783 | return TRUE; |
| 1784 | |
| 1785 | htab = elf64_x86_64_hash_table (info); |
| 1786 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
| 1787 | sym_hashes = elf_sym_hashes (input_bfd); |
| 1788 | local_got_offsets = elf_local_got_offsets (input_bfd); |
| 1789 | |
| 1790 | rel = relocs; |
| 1791 | relend = relocs + input_section->reloc_count; |
| 1792 | for (; rel < relend; rel++) |
| 1793 | { |
| 1794 | unsigned int r_type; |
| 1795 | reloc_howto_type *howto; |
| 1796 | unsigned long r_symndx; |
| 1797 | struct elf_link_hash_entry *h; |
| 1798 | Elf_Internal_Sym *sym; |
| 1799 | asection *sec; |
| 1800 | bfd_vma off; |
| 1801 | bfd_vma relocation; |
| 1802 | bfd_boolean unresolved_reloc; |
| 1803 | bfd_reloc_status_type r; |
| 1804 | int tls_type; |
| 1805 | |
| 1806 | r_type = ELF64_R_TYPE (rel->r_info); |
| 1807 | if (r_type == (int) R_X86_64_GNU_VTINHERIT |
| 1808 | || r_type == (int) R_X86_64_GNU_VTENTRY) |
| 1809 | continue; |
| 1810 | |
| 1811 | if (r_type >= R_X86_64_max) |
| 1812 | { |
| 1813 | bfd_set_error (bfd_error_bad_value); |
| 1814 | return FALSE; |
| 1815 | } |
| 1816 | |
| 1817 | howto = x86_64_elf_howto_table + r_type; |
| 1818 | r_symndx = ELF64_R_SYM (rel->r_info); |
| 1819 | h = NULL; |
| 1820 | sym = NULL; |
| 1821 | sec = NULL; |
| 1822 | unresolved_reloc = FALSE; |
| 1823 | if (r_symndx < symtab_hdr->sh_info) |
| 1824 | { |
| 1825 | sym = local_syms + r_symndx; |
| 1826 | sec = local_sections[r_symndx]; |
| 1827 | |
| 1828 | relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel); |
| 1829 | } |
| 1830 | else |
| 1831 | { |
| 1832 | bfd_boolean warned; |
| 1833 | |
| 1834 | RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel, |
| 1835 | r_symndx, symtab_hdr, sym_hashes, |
| 1836 | h, sec, relocation, |
| 1837 | unresolved_reloc, warned); |
| 1838 | } |
| 1839 | /* When generating a shared object, the relocations handled here are |
| 1840 | copied into the output file to be resolved at run time. */ |
| 1841 | switch (r_type) |
| 1842 | { |
| 1843 | case R_X86_64_GOT32: |
| 1844 | /* Relocation is to the entry for this symbol in the global |
| 1845 | offset table. */ |
| 1846 | case R_X86_64_GOTPCREL: |
| 1847 | /* Use global offset table as symbol value. */ |
| 1848 | if (htab->sgot == NULL) |
| 1849 | abort (); |
| 1850 | |
| 1851 | if (h != NULL) |
| 1852 | { |
| 1853 | bfd_boolean dyn; |
| 1854 | |
| 1855 | off = h->got.offset; |
| 1856 | dyn = htab->elf.dynamic_sections_created; |
| 1857 | |
| 1858 | if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h) |
| 1859 | || (info->shared |
| 1860 | && SYMBOL_REFERENCES_LOCAL (info, h)) |
| 1861 | || (ELF_ST_VISIBILITY (h->other) |
| 1862 | && h->root.type == bfd_link_hash_undefweak)) |
| 1863 | { |
| 1864 | /* This is actually a static link, or it is a -Bsymbolic |
| 1865 | link and the symbol is defined locally, or the symbol |
| 1866 | was forced to be local because of a version file. We |
| 1867 | must initialize this entry in the global offset table. |
| 1868 | Since the offset must always be a multiple of 8, we |
| 1869 | use the least significant bit to record whether we |
| 1870 | have initialized it already. |
| 1871 | |
| 1872 | When doing a dynamic link, we create a .rela.got |
| 1873 | relocation entry to initialize the value. This is |
| 1874 | done in the finish_dynamic_symbol routine. */ |
| 1875 | if ((off & 1) != 0) |
| 1876 | off &= ~1; |
| 1877 | else |
| 1878 | { |
| 1879 | bfd_put_64 (output_bfd, relocation, |
| 1880 | htab->sgot->contents + off); |
| 1881 | h->got.offset |= 1; |
| 1882 | } |
| 1883 | } |
| 1884 | else |
| 1885 | unresolved_reloc = FALSE; |
| 1886 | } |
| 1887 | else |
| 1888 | { |
| 1889 | if (local_got_offsets == NULL) |
| 1890 | abort (); |
| 1891 | |
| 1892 | off = local_got_offsets[r_symndx]; |
| 1893 | |
| 1894 | /* The offset must always be a multiple of 8. We use |
| 1895 | the least significant bit to record whether we have |
| 1896 | already generated the necessary reloc. */ |
| 1897 | if ((off & 1) != 0) |
| 1898 | off &= ~1; |
| 1899 | else |
| 1900 | { |
| 1901 | bfd_put_64 (output_bfd, relocation, |
| 1902 | htab->sgot->contents + off); |
| 1903 | |
| 1904 | if (info->shared) |
| 1905 | { |
| 1906 | asection *s; |
| 1907 | Elf_Internal_Rela outrel; |
| 1908 | bfd_byte *loc; |
| 1909 | |
| 1910 | /* We need to generate a R_X86_64_RELATIVE reloc |
| 1911 | for the dynamic linker. */ |
| 1912 | s = htab->srelgot; |
| 1913 | if (s == NULL) |
| 1914 | abort (); |
| 1915 | |
| 1916 | outrel.r_offset = (htab->sgot->output_section->vma |
| 1917 | + htab->sgot->output_offset |
| 1918 | + off); |
| 1919 | outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE); |
| 1920 | outrel.r_addend = relocation; |
| 1921 | loc = s->contents; |
| 1922 | loc += s->reloc_count++ * sizeof (Elf64_External_Rela); |
| 1923 | bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); |
| 1924 | } |
| 1925 | |
| 1926 | local_got_offsets[r_symndx] |= 1; |
| 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | if (off >= (bfd_vma) -2) |
| 1931 | abort (); |
| 1932 | |
| 1933 | relocation = htab->sgot->output_section->vma |
| 1934 | + htab->sgot->output_offset + off; |
| 1935 | if (r_type != R_X86_64_GOTPCREL) |
| 1936 | relocation -= htab->sgotplt->output_section->vma |
| 1937 | - htab->sgotplt->output_offset; |
| 1938 | |
| 1939 | break; |
| 1940 | |
| 1941 | case R_X86_64_PLT32: |
| 1942 | /* Relocation is to the entry for this symbol in the |
| 1943 | procedure linkage table. */ |
| 1944 | |
| 1945 | /* Resolve a PLT32 reloc against a local symbol directly, |
| 1946 | without using the procedure linkage table. */ |
| 1947 | if (h == NULL) |
| 1948 | break; |
| 1949 | |
| 1950 | if (h->plt.offset == (bfd_vma) -1 |
| 1951 | || htab->splt == NULL) |
| 1952 | { |
| 1953 | /* We didn't make a PLT entry for this symbol. This |
| 1954 | happens when statically linking PIC code, or when |
| 1955 | using -Bsymbolic. */ |
| 1956 | break; |
| 1957 | } |
| 1958 | |
| 1959 | relocation = (htab->splt->output_section->vma |
| 1960 | + htab->splt->output_offset |
| 1961 | + h->plt.offset); |
| 1962 | unresolved_reloc = FALSE; |
| 1963 | break; |
| 1964 | |
| 1965 | case R_X86_64_PC8: |
| 1966 | case R_X86_64_PC16: |
| 1967 | case R_X86_64_PC32: |
| 1968 | if (info->shared |
| 1969 | && !SYMBOL_REFERENCES_LOCAL (info, h) |
| 1970 | && (input_section->flags & SEC_ALLOC) != 0 |
| 1971 | && (input_section->flags & SEC_READONLY) != 0 |
| 1972 | && (!h->def_regular |
| 1973 | || r_type != R_X86_64_PC32 |
| 1974 | || h->type != STT_FUNC |
| 1975 | || ELF_ST_VISIBILITY (h->other) != STV_PROTECTED |
| 1976 | || !is_32bit_relative_branch (contents, |
| 1977 | rel->r_offset))) |
| 1978 | { |
| 1979 | if (h->def_regular |
| 1980 | && r_type == R_X86_64_PC32 |
| 1981 | && h->type == STT_FUNC |
| 1982 | && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) |
| 1983 | (*_bfd_error_handler) |
| 1984 | (_("%B: relocation R_X86_64_PC32 against protected function `%s' can not be used when making a shared object"), |
| 1985 | input_bfd, h->root.root.string); |
| 1986 | else |
| 1987 | (*_bfd_error_handler) |
| 1988 | (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"), |
| 1989 | input_bfd, x86_64_elf_howto_table[r_type].name, |
| 1990 | h->root.root.string); |
| 1991 | bfd_set_error (bfd_error_bad_value); |
| 1992 | return FALSE; |
| 1993 | } |
| 1994 | /* Fall through. */ |
| 1995 | |
| 1996 | case R_X86_64_8: |
| 1997 | case R_X86_64_16: |
| 1998 | case R_X86_64_32: |
| 1999 | case R_X86_64_64: |
| 2000 | /* FIXME: The ABI says the linker should make sure the value is |
| 2001 | the same when it's zeroextended to 64 bit. */ |
| 2002 | |
| 2003 | /* r_symndx will be zero only for relocs against symbols |
| 2004 | from removed linkonce sections, or sections discarded by |
| 2005 | a linker script. */ |
| 2006 | if (r_symndx == 0 |
| 2007 | || (input_section->flags & SEC_ALLOC) == 0) |
| 2008 | break; |
| 2009 | |
| 2010 | if ((info->shared |
| 2011 | && (h == NULL |
| 2012 | || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT |
| 2013 | || h->root.type != bfd_link_hash_undefweak) |
| 2014 | && ((r_type != R_X86_64_PC8 |
| 2015 | && r_type != R_X86_64_PC16 |
| 2016 | && r_type != R_X86_64_PC32) |
| 2017 | || !SYMBOL_CALLS_LOCAL (info, h))) |
| 2018 | || (ELIMINATE_COPY_RELOCS |
| 2019 | && !info->shared |
| 2020 | && h != NULL |
| 2021 | && h->dynindx != -1 |
| 2022 | && !h->non_got_ref |
| 2023 | && ((h->def_dynamic |
| 2024 | && !h->def_regular) |
| 2025 | || h->root.type == bfd_link_hash_undefweak |
| 2026 | || h->root.type == bfd_link_hash_undefined))) |
| 2027 | { |
| 2028 | Elf_Internal_Rela outrel; |
| 2029 | bfd_byte *loc; |
| 2030 | bfd_boolean skip, relocate; |
| 2031 | asection *sreloc; |
| 2032 | |
| 2033 | /* When generating a shared object, these relocations |
| 2034 | are copied into the output file to be resolved at run |
| 2035 | time. */ |
| 2036 | skip = FALSE; |
| 2037 | relocate = FALSE; |
| 2038 | |
| 2039 | outrel.r_offset = |
| 2040 | _bfd_elf_section_offset (output_bfd, info, input_section, |
| 2041 | rel->r_offset); |
| 2042 | if (outrel.r_offset == (bfd_vma) -1) |
| 2043 | skip = TRUE; |
| 2044 | else if (outrel.r_offset == (bfd_vma) -2) |
| 2045 | skip = TRUE, relocate = TRUE; |
| 2046 | |
| 2047 | outrel.r_offset += (input_section->output_section->vma |
| 2048 | + input_section->output_offset); |
| 2049 | |
| 2050 | if (skip) |
| 2051 | memset (&outrel, 0, sizeof outrel); |
| 2052 | |
| 2053 | /* h->dynindx may be -1 if this symbol was marked to |
| 2054 | become local. */ |
| 2055 | else if (h != NULL |
| 2056 | && h->dynindx != -1 |
| 2057 | && (r_type == R_X86_64_PC8 |
| 2058 | || r_type == R_X86_64_PC16 |
| 2059 | || r_type == R_X86_64_PC32 |
| 2060 | || !info->shared |
| 2061 | || !info->symbolic |
| 2062 | || !h->def_regular)) |
| 2063 | { |
| 2064 | outrel.r_info = ELF64_R_INFO (h->dynindx, r_type); |
| 2065 | outrel.r_addend = rel->r_addend; |
| 2066 | } |
| 2067 | else |
| 2068 | { |
| 2069 | /* This symbol is local, or marked to become local. */ |
| 2070 | if (r_type == R_X86_64_64) |
| 2071 | { |
| 2072 | relocate = TRUE; |
| 2073 | outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE); |
| 2074 | outrel.r_addend = relocation + rel->r_addend; |
| 2075 | } |
| 2076 | else |
| 2077 | { |
| 2078 | long sindx; |
| 2079 | |
| 2080 | if (bfd_is_abs_section (sec)) |
| 2081 | sindx = 0; |
| 2082 | else if (sec == NULL || sec->owner == NULL) |
| 2083 | { |
| 2084 | bfd_set_error (bfd_error_bad_value); |
| 2085 | return FALSE; |
| 2086 | } |
| 2087 | else |
| 2088 | { |
| 2089 | asection *osec; |
| 2090 | |
| 2091 | osec = sec->output_section; |
| 2092 | sindx = elf_section_data (osec)->dynindx; |
| 2093 | BFD_ASSERT (sindx > 0); |
| 2094 | } |
| 2095 | |
| 2096 | outrel.r_info = ELF64_R_INFO (sindx, r_type); |
| 2097 | outrel.r_addend = relocation + rel->r_addend; |
| 2098 | } |
| 2099 | } |
| 2100 | |
| 2101 | sreloc = elf_section_data (input_section)->sreloc; |
| 2102 | if (sreloc == NULL) |
| 2103 | abort (); |
| 2104 | |
| 2105 | loc = sreloc->contents; |
| 2106 | loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela); |
| 2107 | bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); |
| 2108 | |
| 2109 | /* If this reloc is against an external symbol, we do |
| 2110 | not want to fiddle with the addend. Otherwise, we |
| 2111 | need to include the symbol value so that it becomes |
| 2112 | an addend for the dynamic reloc. */ |
| 2113 | if (! relocate) |
| 2114 | continue; |
| 2115 | } |
| 2116 | |
| 2117 | break; |
| 2118 | |
| 2119 | case R_X86_64_TLSGD: |
| 2120 | case R_X86_64_GOTTPOFF: |
| 2121 | r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL); |
| 2122 | tls_type = GOT_UNKNOWN; |
| 2123 | if (h == NULL && local_got_offsets) |
| 2124 | tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx]; |
| 2125 | else if (h != NULL) |
| 2126 | { |
| 2127 | tls_type = elf64_x86_64_hash_entry (h)->tls_type; |
| 2128 | if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE) |
| 2129 | r_type = R_X86_64_TPOFF32; |
| 2130 | } |
| 2131 | if (r_type == R_X86_64_TLSGD) |
| 2132 | { |
| 2133 | if (tls_type == GOT_TLS_IE) |
| 2134 | r_type = R_X86_64_GOTTPOFF; |
| 2135 | } |
| 2136 | |
| 2137 | if (r_type == R_X86_64_TPOFF32) |
| 2138 | { |
| 2139 | BFD_ASSERT (! unresolved_reloc); |
| 2140 | if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD) |
| 2141 | { |
| 2142 | unsigned int i; |
| 2143 | static unsigned char tlsgd[8] |
| 2144 | = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 }; |
| 2145 | |
| 2146 | /* GD->LE transition. |
| 2147 | .byte 0x66; leaq foo@tlsgd(%rip), %rdi |
| 2148 | .word 0x6666; rex64; call __tls_get_addr@plt |
| 2149 | Change it into: |
| 2150 | movq %fs:0, %rax |
| 2151 | leaq foo@tpoff(%rax), %rax */ |
| 2152 | BFD_ASSERT (rel->r_offset >= 4); |
| 2153 | for (i = 0; i < 4; i++) |
| 2154 | BFD_ASSERT (bfd_get_8 (input_bfd, |
| 2155 | contents + rel->r_offset - 4 + i) |
| 2156 | == tlsgd[i]); |
| 2157 | BFD_ASSERT (rel->r_offset + 12 <= input_section->size); |
| 2158 | for (i = 0; i < 4; i++) |
| 2159 | BFD_ASSERT (bfd_get_8 (input_bfd, |
| 2160 | contents + rel->r_offset + 4 + i) |
| 2161 | == tlsgd[i+4]); |
| 2162 | BFD_ASSERT (rel + 1 < relend); |
| 2163 | BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32); |
| 2164 | memcpy (contents + rel->r_offset - 4, |
| 2165 | "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0", |
| 2166 | 16); |
| 2167 | bfd_put_32 (output_bfd, tpoff (info, relocation), |
| 2168 | contents + rel->r_offset + 8); |
| 2169 | /* Skip R_X86_64_PLT32. */ |
| 2170 | rel++; |
| 2171 | continue; |
| 2172 | } |
| 2173 | else |
| 2174 | { |
| 2175 | unsigned int val, type, reg; |
| 2176 | |
| 2177 | /* IE->LE transition: |
| 2178 | Originally it can be one of: |
| 2179 | movq foo@gottpoff(%rip), %reg |
| 2180 | addq foo@gottpoff(%rip), %reg |
| 2181 | We change it into: |
| 2182 | movq $foo, %reg |
| 2183 | leaq foo(%reg), %reg |
| 2184 | addq $foo, %reg. */ |
| 2185 | BFD_ASSERT (rel->r_offset >= 3); |
| 2186 | val = bfd_get_8 (input_bfd, contents + rel->r_offset - 3); |
| 2187 | BFD_ASSERT (val == 0x48 || val == 0x4c); |
| 2188 | type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2); |
| 2189 | BFD_ASSERT (type == 0x8b || type == 0x03); |
| 2190 | reg = bfd_get_8 (input_bfd, contents + rel->r_offset - 1); |
| 2191 | BFD_ASSERT ((reg & 0xc7) == 5); |
| 2192 | reg >>= 3; |
| 2193 | BFD_ASSERT (rel->r_offset + 4 <= input_section->size); |
| 2194 | if (type == 0x8b) |
| 2195 | { |
| 2196 | /* movq */ |
| 2197 | if (val == 0x4c) |
| 2198 | bfd_put_8 (output_bfd, 0x49, |
| 2199 | contents + rel->r_offset - 3); |
| 2200 | bfd_put_8 (output_bfd, 0xc7, |
| 2201 | contents + rel->r_offset - 2); |
| 2202 | bfd_put_8 (output_bfd, 0xc0 | reg, |
| 2203 | contents + rel->r_offset - 1); |
| 2204 | } |
| 2205 | else if (reg == 4) |
| 2206 | { |
| 2207 | /* addq -> addq - addressing with %rsp/%r12 is |
| 2208 | special */ |
| 2209 | if (val == 0x4c) |
| 2210 | bfd_put_8 (output_bfd, 0x49, |
| 2211 | contents + rel->r_offset - 3); |
| 2212 | bfd_put_8 (output_bfd, 0x81, |
| 2213 | contents + rel->r_offset - 2); |
| 2214 | bfd_put_8 (output_bfd, 0xc0 | reg, |
| 2215 | contents + rel->r_offset - 1); |
| 2216 | } |
| 2217 | else |
| 2218 | { |
| 2219 | /* addq -> leaq */ |
| 2220 | if (val == 0x4c) |
| 2221 | bfd_put_8 (output_bfd, 0x4d, |
| 2222 | contents + rel->r_offset - 3); |
| 2223 | bfd_put_8 (output_bfd, 0x8d, |
| 2224 | contents + rel->r_offset - 2); |
| 2225 | bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3), |
| 2226 | contents + rel->r_offset - 1); |
| 2227 | } |
| 2228 | bfd_put_32 (output_bfd, tpoff (info, relocation), |
| 2229 | contents + rel->r_offset); |
| 2230 | continue; |
| 2231 | } |
| 2232 | } |
| 2233 | |
| 2234 | if (htab->sgot == NULL) |
| 2235 | abort (); |
| 2236 | |
| 2237 | if (h != NULL) |
| 2238 | off = h->got.offset; |
| 2239 | else |
| 2240 | { |
| 2241 | if (local_got_offsets == NULL) |
| 2242 | abort (); |
| 2243 | |
| 2244 | off = local_got_offsets[r_symndx]; |
| 2245 | } |
| 2246 | |
| 2247 | if ((off & 1) != 0) |
| 2248 | off &= ~1; |
| 2249 | else |
| 2250 | { |
| 2251 | Elf_Internal_Rela outrel; |
| 2252 | bfd_byte *loc; |
| 2253 | int dr_type, indx; |
| 2254 | |
| 2255 | if (htab->srelgot == NULL) |
| 2256 | abort (); |
| 2257 | |
| 2258 | outrel.r_offset = (htab->sgot->output_section->vma |
| 2259 | + htab->sgot->output_offset + off); |
| 2260 | |
| 2261 | indx = h && h->dynindx != -1 ? h->dynindx : 0; |
| 2262 | if (r_type == R_X86_64_TLSGD) |
| 2263 | dr_type = R_X86_64_DTPMOD64; |
| 2264 | else |
| 2265 | dr_type = R_X86_64_TPOFF64; |
| 2266 | |
| 2267 | bfd_put_64 (output_bfd, 0, htab->sgot->contents + off); |
| 2268 | outrel.r_addend = 0; |
| 2269 | if (dr_type == R_X86_64_TPOFF64 && indx == 0) |
| 2270 | outrel.r_addend = relocation - dtpoff_base (info); |
| 2271 | outrel.r_info = ELF64_R_INFO (indx, dr_type); |
| 2272 | |
| 2273 | loc = htab->srelgot->contents; |
| 2274 | loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela); |
| 2275 | bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); |
| 2276 | |
| 2277 | if (r_type == R_X86_64_TLSGD) |
| 2278 | { |
| 2279 | if (indx == 0) |
| 2280 | { |
| 2281 | BFD_ASSERT (! unresolved_reloc); |
| 2282 | bfd_put_64 (output_bfd, |
| 2283 | relocation - dtpoff_base (info), |
| 2284 | htab->sgot->contents + off + GOT_ENTRY_SIZE); |
| 2285 | } |
| 2286 | else |
| 2287 | { |
| 2288 | bfd_put_64 (output_bfd, 0, |
| 2289 | htab->sgot->contents + off + GOT_ENTRY_SIZE); |
| 2290 | outrel.r_info = ELF64_R_INFO (indx, |
| 2291 | R_X86_64_DTPOFF64); |
| 2292 | outrel.r_offset += GOT_ENTRY_SIZE; |
| 2293 | htab->srelgot->reloc_count++; |
| 2294 | loc += sizeof (Elf64_External_Rela); |
| 2295 | bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); |
| 2296 | } |
| 2297 | } |
| 2298 | |
| 2299 | if (h != NULL) |
| 2300 | h->got.offset |= 1; |
| 2301 | else |
| 2302 | local_got_offsets[r_symndx] |= 1; |
| 2303 | } |
| 2304 | |
| 2305 | if (off >= (bfd_vma) -2) |
| 2306 | abort (); |
| 2307 | if (r_type == ELF64_R_TYPE (rel->r_info)) |
| 2308 | { |
| 2309 | relocation = htab->sgot->output_section->vma |
| 2310 | + htab->sgot->output_offset + off; |
| 2311 | unresolved_reloc = FALSE; |
| 2312 | } |
| 2313 | else |
| 2314 | { |
| 2315 | unsigned int i; |
| 2316 | static unsigned char tlsgd[8] |
| 2317 | = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 }; |
| 2318 | |
| 2319 | /* GD->IE transition. |
| 2320 | .byte 0x66; leaq foo@tlsgd(%rip), %rdi |
| 2321 | .word 0x6666; rex64; call __tls_get_addr@plt |
| 2322 | Change it into: |
| 2323 | movq %fs:0, %rax |
| 2324 | addq foo@gottpoff(%rip), %rax */ |
| 2325 | BFD_ASSERT (rel->r_offset >= 4); |
| 2326 | for (i = 0; i < 4; i++) |
| 2327 | BFD_ASSERT (bfd_get_8 (input_bfd, |
| 2328 | contents + rel->r_offset - 4 + i) |
| 2329 | == tlsgd[i]); |
| 2330 | BFD_ASSERT (rel->r_offset + 12 <= input_section->size); |
| 2331 | for (i = 0; i < 4; i++) |
| 2332 | BFD_ASSERT (bfd_get_8 (input_bfd, |
| 2333 | contents + rel->r_offset + 4 + i) |
| 2334 | == tlsgd[i+4]); |
| 2335 | BFD_ASSERT (rel + 1 < relend); |
| 2336 | BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32); |
| 2337 | memcpy (contents + rel->r_offset - 4, |
| 2338 | "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0", |
| 2339 | 16); |
| 2340 | |
| 2341 | relocation = (htab->sgot->output_section->vma |
| 2342 | + htab->sgot->output_offset + off |
| 2343 | - rel->r_offset |
| 2344 | - input_section->output_section->vma |
| 2345 | - input_section->output_offset |
| 2346 | - 12); |
| 2347 | bfd_put_32 (output_bfd, relocation, |
| 2348 | contents + rel->r_offset + 8); |
| 2349 | /* Skip R_X86_64_PLT32. */ |
| 2350 | rel++; |
| 2351 | continue; |
| 2352 | } |
| 2353 | break; |
| 2354 | |
| 2355 | case R_X86_64_TLSLD: |
| 2356 | if (! info->shared) |
| 2357 | { |
| 2358 | /* LD->LE transition: |
| 2359 | Ensure it is: |
| 2360 | leaq foo@tlsld(%rip), %rdi; call __tls_get_addr@plt. |
| 2361 | We change it into: |
| 2362 | .word 0x6666; .byte 0x66; movl %fs:0, %rax. */ |
| 2363 | BFD_ASSERT (rel->r_offset >= 3); |
| 2364 | BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 3) |
| 2365 | == 0x48); |
| 2366 | BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2) |
| 2367 | == 0x8d); |
| 2368 | BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 1) |
| 2369 | == 0x3d); |
| 2370 | BFD_ASSERT (rel->r_offset + 9 <= input_section->size); |
| 2371 | BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4) |
| 2372 | == 0xe8); |
| 2373 | BFD_ASSERT (rel + 1 < relend); |
| 2374 | BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32); |
| 2375 | memcpy (contents + rel->r_offset - 3, |
| 2376 | "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12); |
| 2377 | /* Skip R_X86_64_PLT32. */ |
| 2378 | rel++; |
| 2379 | continue; |
| 2380 | } |
| 2381 | |
| 2382 | if (htab->sgot == NULL) |
| 2383 | abort (); |
| 2384 | |
| 2385 | off = htab->tls_ld_got.offset; |
| 2386 | if (off & 1) |
| 2387 | off &= ~1; |
| 2388 | else |
| 2389 | { |
| 2390 | Elf_Internal_Rela outrel; |
| 2391 | bfd_byte *loc; |
| 2392 | |
| 2393 | if (htab->srelgot == NULL) |
| 2394 | abort (); |
| 2395 | |
| 2396 | outrel.r_offset = (htab->sgot->output_section->vma |
| 2397 | + htab->sgot->output_offset + off); |
| 2398 | |
| 2399 | bfd_put_64 (output_bfd, 0, |
| 2400 | htab->sgot->contents + off); |
| 2401 | bfd_put_64 (output_bfd, 0, |
| 2402 | htab->sgot->contents + off + GOT_ENTRY_SIZE); |
| 2403 | outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64); |
| 2404 | outrel.r_addend = 0; |
| 2405 | loc = htab->srelgot->contents; |
| 2406 | loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela); |
| 2407 | bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); |
| 2408 | htab->tls_ld_got.offset |= 1; |
| 2409 | } |
| 2410 | relocation = htab->sgot->output_section->vma |
| 2411 | + htab->sgot->output_offset + off; |
| 2412 | unresolved_reloc = FALSE; |
| 2413 | break; |
| 2414 | |
| 2415 | case R_X86_64_DTPOFF32: |
| 2416 | if (info->shared || (input_section->flags & SEC_CODE) == 0) |
| 2417 | relocation -= dtpoff_base (info); |
| 2418 | else |
| 2419 | relocation = tpoff (info, relocation); |
| 2420 | break; |
| 2421 | |
| 2422 | case R_X86_64_TPOFF32: |
| 2423 | BFD_ASSERT (! info->shared); |
| 2424 | relocation = tpoff (info, relocation); |
| 2425 | break; |
| 2426 | |
| 2427 | default: |
| 2428 | break; |
| 2429 | } |
| 2430 | |
| 2431 | /* Dynamic relocs are not propagated for SEC_DEBUGGING sections |
| 2432 | because such sections are not SEC_ALLOC and thus ld.so will |
| 2433 | not process them. */ |
| 2434 | if (unresolved_reloc |
| 2435 | && !((input_section->flags & SEC_DEBUGGING) != 0 |
| 2436 | && h->def_dynamic)) |
| 2437 | (*_bfd_error_handler) |
| 2438 | (_("%B(%A+0x%lx): unresolvable relocation against symbol `%s'"), |
| 2439 | input_bfd, |
| 2440 | input_section, |
| 2441 | (long) rel->r_offset, |
| 2442 | h->root.root.string); |
| 2443 | |
| 2444 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, |
| 2445 | contents, rel->r_offset, |
| 2446 | relocation, rel->r_addend); |
| 2447 | |
| 2448 | if (r != bfd_reloc_ok) |
| 2449 | { |
| 2450 | const char *name; |
| 2451 | |
| 2452 | if (h != NULL) |
| 2453 | name = h->root.root.string; |
| 2454 | else |
| 2455 | { |
| 2456 | name = bfd_elf_string_from_elf_section (input_bfd, |
| 2457 | symtab_hdr->sh_link, |
| 2458 | sym->st_name); |
| 2459 | if (name == NULL) |
| 2460 | return FALSE; |
| 2461 | if (*name == '\0') |
| 2462 | name = bfd_section_name (input_bfd, sec); |
| 2463 | } |
| 2464 | |
| 2465 | if (r == bfd_reloc_overflow) |
| 2466 | { |
| 2467 | if (h != NULL |
| 2468 | && h->root.type == bfd_link_hash_undefweak |
| 2469 | && howto->pc_relative) |
| 2470 | /* Ignore reloc overflow on branches to undefweak syms. */ |
| 2471 | continue; |
| 2472 | |
| 2473 | if (! ((*info->callbacks->reloc_overflow) |
| 2474 | (info, (h ? &h->root : NULL), name, howto->name, |
| 2475 | (bfd_vma) 0, input_bfd, input_section, |
| 2476 | rel->r_offset))) |
| 2477 | return FALSE; |
| 2478 | } |
| 2479 | else |
| 2480 | { |
| 2481 | (*_bfd_error_handler) |
| 2482 | (_("%B(%A+0x%lx): reloc against `%s': error %d"), |
| 2483 | input_bfd, input_section, |
| 2484 | (long) rel->r_offset, name, (int) r); |
| 2485 | return FALSE; |
| 2486 | } |
| 2487 | } |
| 2488 | } |
| 2489 | |
| 2490 | return TRUE; |
| 2491 | } |
| 2492 | |
| 2493 | /* Finish up dynamic symbol handling. We set the contents of various |
| 2494 | dynamic sections here. */ |
| 2495 | |
| 2496 | static bfd_boolean |
| 2497 | elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd, |
| 2498 | struct bfd_link_info *info, |
| 2499 | struct elf_link_hash_entry *h, |
| 2500 | Elf_Internal_Sym *sym) |
| 2501 | { |
| 2502 | struct elf64_x86_64_link_hash_table *htab; |
| 2503 | |
| 2504 | htab = elf64_x86_64_hash_table (info); |
| 2505 | |
| 2506 | if (h->plt.offset != (bfd_vma) -1) |
| 2507 | { |
| 2508 | bfd_vma plt_index; |
| 2509 | bfd_vma got_offset; |
| 2510 | Elf_Internal_Rela rela; |
| 2511 | bfd_byte *loc; |
| 2512 | |
| 2513 | /* This symbol has an entry in the procedure linkage table. Set |
| 2514 | it up. */ |
| 2515 | if (h->dynindx == -1 |
| 2516 | || htab->splt == NULL |
| 2517 | || htab->sgotplt == NULL |
| 2518 | || htab->srelplt == NULL) |
| 2519 | abort (); |
| 2520 | |
| 2521 | /* Get the index in the procedure linkage table which |
| 2522 | corresponds to this symbol. This is the index of this symbol |
| 2523 | in all the symbols for which we are making plt entries. The |
| 2524 | first entry in the procedure linkage table is reserved. */ |
| 2525 | plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1; |
| 2526 | |
| 2527 | /* Get the offset into the .got table of the entry that |
| 2528 | corresponds to this function. Each .got entry is GOT_ENTRY_SIZE |
| 2529 | bytes. The first three are reserved for the dynamic linker. */ |
| 2530 | got_offset = (plt_index + 3) * GOT_ENTRY_SIZE; |
| 2531 | |
| 2532 | /* Fill in the entry in the procedure linkage table. */ |
| 2533 | memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry, |
| 2534 | PLT_ENTRY_SIZE); |
| 2535 | |
| 2536 | /* Insert the relocation positions of the plt section. The magic |
| 2537 | numbers at the end of the statements are the positions of the |
| 2538 | relocations in the plt section. */ |
| 2539 | /* Put offset for jmp *name@GOTPCREL(%rip), since the |
| 2540 | instruction uses 6 bytes, subtract this value. */ |
| 2541 | bfd_put_32 (output_bfd, |
| 2542 | (htab->sgotplt->output_section->vma |
| 2543 | + htab->sgotplt->output_offset |
| 2544 | + got_offset |
| 2545 | - htab->splt->output_section->vma |
| 2546 | - htab->splt->output_offset |
| 2547 | - h->plt.offset |
| 2548 | - 6), |
| 2549 | htab->splt->contents + h->plt.offset + 2); |
| 2550 | /* Put relocation index. */ |
| 2551 | bfd_put_32 (output_bfd, plt_index, |
| 2552 | htab->splt->contents + h->plt.offset + 7); |
| 2553 | /* Put offset for jmp .PLT0. */ |
| 2554 | bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE), |
| 2555 | htab->splt->contents + h->plt.offset + 12); |
| 2556 | |
| 2557 | /* Fill in the entry in the global offset table, initially this |
| 2558 | points to the pushq instruction in the PLT which is at offset 6. */ |
| 2559 | bfd_put_64 (output_bfd, (htab->splt->output_section->vma |
| 2560 | + htab->splt->output_offset |
| 2561 | + h->plt.offset + 6), |
| 2562 | htab->sgotplt->contents + got_offset); |
| 2563 | |
| 2564 | /* Fill in the entry in the .rela.plt section. */ |
| 2565 | rela.r_offset = (htab->sgotplt->output_section->vma |
| 2566 | + htab->sgotplt->output_offset |
| 2567 | + got_offset); |
| 2568 | rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT); |
| 2569 | rela.r_addend = 0; |
| 2570 | loc = htab->srelplt->contents + plt_index * sizeof (Elf64_External_Rela); |
| 2571 | bfd_elf64_swap_reloca_out (output_bfd, &rela, loc); |
| 2572 | |
| 2573 | if (!h->def_regular) |
| 2574 | { |
| 2575 | /* Mark the symbol as undefined, rather than as defined in |
| 2576 | the .plt section. Leave the value if there were any |
| 2577 | relocations where pointer equality matters (this is a clue |
| 2578 | for the dynamic linker, to make function pointer |
| 2579 | comparisons work between an application and shared |
| 2580 | library), otherwise set it to zero. If a function is only |
| 2581 | called from a binary, there is no need to slow down |
| 2582 | shared libraries because of that. */ |
| 2583 | sym->st_shndx = SHN_UNDEF; |
| 2584 | if (!h->pointer_equality_needed) |
| 2585 | sym->st_value = 0; |
| 2586 | } |
| 2587 | } |
| 2588 | |
| 2589 | if (h->got.offset != (bfd_vma) -1 |
| 2590 | && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_GD |
| 2591 | && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE) |
| 2592 | { |
| 2593 | Elf_Internal_Rela rela; |
| 2594 | bfd_byte *loc; |
| 2595 | |
| 2596 | /* This symbol has an entry in the global offset table. Set it |
| 2597 | up. */ |
| 2598 | if (htab->sgot == NULL || htab->srelgot == NULL) |
| 2599 | abort (); |
| 2600 | |
| 2601 | rela.r_offset = (htab->sgot->output_section->vma |
| 2602 | + htab->sgot->output_offset |
| 2603 | + (h->got.offset &~ (bfd_vma) 1)); |
| 2604 | |
| 2605 | /* If this is a static link, or it is a -Bsymbolic link and the |
| 2606 | symbol is defined locally or was forced to be local because |
| 2607 | of a version file, we just want to emit a RELATIVE reloc. |
| 2608 | The entry in the global offset table will already have been |
| 2609 | initialized in the relocate_section function. */ |
| 2610 | if (info->shared |
| 2611 | && SYMBOL_REFERENCES_LOCAL (info, h)) |
| 2612 | { |
| 2613 | BFD_ASSERT((h->got.offset & 1) != 0); |
| 2614 | rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE); |
| 2615 | rela.r_addend = (h->root.u.def.value |
| 2616 | + h->root.u.def.section->output_section->vma |
| 2617 | + h->root.u.def.section->output_offset); |
| 2618 | } |
| 2619 | else |
| 2620 | { |
| 2621 | BFD_ASSERT((h->got.offset & 1) == 0); |
| 2622 | bfd_put_64 (output_bfd, (bfd_vma) 0, |
| 2623 | htab->sgot->contents + h->got.offset); |
| 2624 | rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT); |
| 2625 | rela.r_addend = 0; |
| 2626 | } |
| 2627 | |
| 2628 | loc = htab->srelgot->contents; |
| 2629 | loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela); |
| 2630 | bfd_elf64_swap_reloca_out (output_bfd, &rela, loc); |
| 2631 | } |
| 2632 | |
| 2633 | if (h->needs_copy) |
| 2634 | { |
| 2635 | Elf_Internal_Rela rela; |
| 2636 | bfd_byte *loc; |
| 2637 | |
| 2638 | /* This symbol needs a copy reloc. Set it up. */ |
| 2639 | |
| 2640 | if (h->dynindx == -1 |
| 2641 | || (h->root.type != bfd_link_hash_defined |
| 2642 | && h->root.type != bfd_link_hash_defweak) |
| 2643 | || htab->srelbss == NULL) |
| 2644 | abort (); |
| 2645 | |
| 2646 | rela.r_offset = (h->root.u.def.value |
| 2647 | + h->root.u.def.section->output_section->vma |
| 2648 | + h->root.u.def.section->output_offset); |
| 2649 | rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY); |
| 2650 | rela.r_addend = 0; |
| 2651 | loc = htab->srelbss->contents; |
| 2652 | loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela); |
| 2653 | bfd_elf64_swap_reloca_out (output_bfd, &rela, loc); |
| 2654 | } |
| 2655 | |
| 2656 | /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */ |
| 2657 | if (strcmp (h->root.root.string, "_DYNAMIC") == 0 |
| 2658 | || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0) |
| 2659 | sym->st_shndx = SHN_ABS; |
| 2660 | |
| 2661 | return TRUE; |
| 2662 | } |
| 2663 | |
| 2664 | /* Used to decide how to sort relocs in an optimal manner for the |
| 2665 | dynamic linker, before writing them out. */ |
| 2666 | |
| 2667 | static enum elf_reloc_type_class |
| 2668 | elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela) |
| 2669 | { |
| 2670 | switch ((int) ELF64_R_TYPE (rela->r_info)) |
| 2671 | { |
| 2672 | case R_X86_64_RELATIVE: |
| 2673 | return reloc_class_relative; |
| 2674 | case R_X86_64_JUMP_SLOT: |
| 2675 | return reloc_class_plt; |
| 2676 | case R_X86_64_COPY: |
| 2677 | return reloc_class_copy; |
| 2678 | default: |
| 2679 | return reloc_class_normal; |
| 2680 | } |
| 2681 | } |
| 2682 | |
| 2683 | /* Finish up the dynamic sections. */ |
| 2684 | |
| 2685 | static bfd_boolean |
| 2686 | elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info) |
| 2687 | { |
| 2688 | struct elf64_x86_64_link_hash_table *htab; |
| 2689 | bfd *dynobj; |
| 2690 | asection *sdyn; |
| 2691 | |
| 2692 | htab = elf64_x86_64_hash_table (info); |
| 2693 | dynobj = htab->elf.dynobj; |
| 2694 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); |
| 2695 | |
| 2696 | if (htab->elf.dynamic_sections_created) |
| 2697 | { |
| 2698 | Elf64_External_Dyn *dyncon, *dynconend; |
| 2699 | |
| 2700 | if (sdyn == NULL || htab->sgot == NULL) |
| 2701 | abort (); |
| 2702 | |
| 2703 | dyncon = (Elf64_External_Dyn *) sdyn->contents; |
| 2704 | dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size); |
| 2705 | for (; dyncon < dynconend; dyncon++) |
| 2706 | { |
| 2707 | Elf_Internal_Dyn dyn; |
| 2708 | asection *s; |
| 2709 | |
| 2710 | bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn); |
| 2711 | |
| 2712 | switch (dyn.d_tag) |
| 2713 | { |
| 2714 | default: |
| 2715 | continue; |
| 2716 | |
| 2717 | case DT_PLTGOT: |
| 2718 | s = htab->sgotplt; |
| 2719 | dyn.d_un.d_ptr = s->output_section->vma + s->output_offset; |
| 2720 | break; |
| 2721 | |
| 2722 | case DT_JMPREL: |
| 2723 | dyn.d_un.d_ptr = htab->srelplt->output_section->vma; |
| 2724 | break; |
| 2725 | |
| 2726 | case DT_PLTRELSZ: |
| 2727 | s = htab->srelplt->output_section; |
| 2728 | dyn.d_un.d_val = s->size; |
| 2729 | break; |
| 2730 | |
| 2731 | case DT_RELASZ: |
| 2732 | /* The procedure linkage table relocs (DT_JMPREL) should |
| 2733 | not be included in the overall relocs (DT_RELA). |
| 2734 | Therefore, we override the DT_RELASZ entry here to |
| 2735 | make it not include the JMPREL relocs. Since the |
| 2736 | linker script arranges for .rela.plt to follow all |
| 2737 | other relocation sections, we don't have to worry |
| 2738 | about changing the DT_RELA entry. */ |
| 2739 | if (htab->srelplt != NULL) |
| 2740 | { |
| 2741 | s = htab->srelplt->output_section; |
| 2742 | dyn.d_un.d_val -= s->size; |
| 2743 | } |
| 2744 | break; |
| 2745 | } |
| 2746 | |
| 2747 | bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon); |
| 2748 | } |
| 2749 | |
| 2750 | /* Fill in the special first entry in the procedure linkage table. */ |
| 2751 | if (htab->splt && htab->splt->size > 0) |
| 2752 | { |
| 2753 | /* Fill in the first entry in the procedure linkage table. */ |
| 2754 | memcpy (htab->splt->contents, elf64_x86_64_plt0_entry, |
| 2755 | PLT_ENTRY_SIZE); |
| 2756 | /* Add offset for pushq GOT+8(%rip), since the instruction |
| 2757 | uses 6 bytes subtract this value. */ |
| 2758 | bfd_put_32 (output_bfd, |
| 2759 | (htab->sgotplt->output_section->vma |
| 2760 | + htab->sgotplt->output_offset |
| 2761 | + 8 |
| 2762 | - htab->splt->output_section->vma |
| 2763 | - htab->splt->output_offset |
| 2764 | - 6), |
| 2765 | htab->splt->contents + 2); |
| 2766 | /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to |
| 2767 | the end of the instruction. */ |
| 2768 | bfd_put_32 (output_bfd, |
| 2769 | (htab->sgotplt->output_section->vma |
| 2770 | + htab->sgotplt->output_offset |
| 2771 | + 16 |
| 2772 | - htab->splt->output_section->vma |
| 2773 | - htab->splt->output_offset |
| 2774 | - 12), |
| 2775 | htab->splt->contents + 8); |
| 2776 | |
| 2777 | elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize = |
| 2778 | PLT_ENTRY_SIZE; |
| 2779 | } |
| 2780 | } |
| 2781 | |
| 2782 | if (htab->sgotplt) |
| 2783 | { |
| 2784 | /* Fill in the first three entries in the global offset table. */ |
| 2785 | if (htab->sgotplt->size > 0) |
| 2786 | { |
| 2787 | /* Set the first entry in the global offset table to the address of |
| 2788 | the dynamic section. */ |
| 2789 | if (sdyn == NULL) |
| 2790 | bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents); |
| 2791 | else |
| 2792 | bfd_put_64 (output_bfd, |
| 2793 | sdyn->output_section->vma + sdyn->output_offset, |
| 2794 | htab->sgotplt->contents); |
| 2795 | /* Write GOT[1] and GOT[2], needed for the dynamic linker. */ |
| 2796 | bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE); |
| 2797 | bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2); |
| 2798 | } |
| 2799 | |
| 2800 | elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize = |
| 2801 | GOT_ENTRY_SIZE; |
| 2802 | } |
| 2803 | |
| 2804 | if (htab->sgot && htab->sgot->size > 0) |
| 2805 | elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize |
| 2806 | = GOT_ENTRY_SIZE; |
| 2807 | |
| 2808 | return TRUE; |
| 2809 | } |
| 2810 | |
| 2811 | /* Return address for Ith PLT stub in section PLT, for relocation REL |
| 2812 | or (bfd_vma) -1 if it should not be included. */ |
| 2813 | |
| 2814 | static bfd_vma |
| 2815 | elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt, |
| 2816 | const arelent *rel ATTRIBUTE_UNUSED) |
| 2817 | { |
| 2818 | return plt->vma + (i + 1) * PLT_ENTRY_SIZE; |
| 2819 | } |
| 2820 | |
| 2821 | /* Handle an x86-64 specific section when reading an object file. This |
| 2822 | is called when elfcode.h finds a section with an unknown type. */ |
| 2823 | |
| 2824 | static bfd_boolean |
| 2825 | elf64_x86_64_section_from_shdr (bfd *abfd, |
| 2826 | Elf_Internal_Shdr *hdr, |
| 2827 | const char *name, |
| 2828 | int shindex) |
| 2829 | { |
| 2830 | if (hdr->sh_type != SHT_X86_64_UNWIND) |
| 2831 | return FALSE; |
| 2832 | |
| 2833 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
| 2834 | return FALSE; |
| 2835 | |
| 2836 | return TRUE; |
| 2837 | } |
| 2838 | |
| 2839 | #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec |
| 2840 | #define TARGET_LITTLE_NAME "elf64-x86-64" |
| 2841 | #define ELF_ARCH bfd_arch_i386 |
| 2842 | #define ELF_MACHINE_CODE EM_X86_64 |
| 2843 | #define ELF_MAXPAGESIZE 0x100000 |
| 2844 | |
| 2845 | #define elf_backend_can_gc_sections 1 |
| 2846 | #define elf_backend_can_refcount 1 |
| 2847 | #define elf_backend_want_got_plt 1 |
| 2848 | #define elf_backend_plt_readonly 1 |
| 2849 | #define elf_backend_want_plt_sym 0 |
| 2850 | #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3) |
| 2851 | #define elf_backend_rela_normal 1 |
| 2852 | |
| 2853 | #define elf_info_to_howto elf64_x86_64_info_to_howto |
| 2854 | |
| 2855 | #define bfd_elf64_bfd_link_hash_table_create \ |
| 2856 | elf64_x86_64_link_hash_table_create |
| 2857 | #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup |
| 2858 | |
| 2859 | #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol |
| 2860 | #define elf_backend_check_relocs elf64_x86_64_check_relocs |
| 2861 | #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol |
| 2862 | #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections |
| 2863 | #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections |
| 2864 | #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol |
| 2865 | #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook |
| 2866 | #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook |
| 2867 | #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus |
| 2868 | #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo |
| 2869 | #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class |
| 2870 | #define elf_backend_relocate_section elf64_x86_64_relocate_section |
| 2871 | #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections |
| 2872 | #define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val |
| 2873 | #define elf_backend_object_p elf64_x86_64_elf_object_p |
| 2874 | #define bfd_elf64_mkobject elf64_x86_64_mkobject |
| 2875 | |
| 2876 | #define elf_backend_section_from_shdr \ |
| 2877 | elf64_x86_64_section_from_shdr |
| 2878 | |
| 2879 | #include "elf64-target.h" |