diff options
author | Jakub Jelinek <jakub@redhat.com> | 2002-09-27 19:29:18 +0000 |
---|---|---|
committer | Jakub Jelinek <jakub@redhat.com> | 2002-09-27 19:29:18 +0000 |
commit | bffbf9406ff7c4c8545d3f154e54926e5f37a9bb (patch) | |
tree | b72fb7174541fc1ce2227be80bbc88bf22db2c9e /bfd/elf64-x86-64.c | |
parent | 43b2fdc74cb309f762060ac012d1a2319bc9a1cf (diff) | |
download | gdb-bffbf9406ff7c4c8545d3f154e54926e5f37a9bb.zip gdb-bffbf9406ff7c4c8545d3f154e54926e5f37a9bb.tar.gz gdb-bffbf9406ff7c4c8545d3f154e54926e5f37a9bb.tar.bz2 |
bfd/
* reloc.c: Add x86-64 TLS relocs.
* bfd-in2.h, libbfd.h: Rebuilt.
* elf64-x86-64.c (x86_64_elf_howto): Fix size fields for 32-bit
relocs. Add TLS relocs.
(x86_64_reloc_map): Add TLS relocs.
(elf64_x86_64_info_to_howto): Adjust for added TLS relocs.
(struct elf64_x86_64_link_hash_entry): Add tls_type field.
(GOT_UNKNOWN, GOT_NORMAL, GOT_TLS_GD, GOT_TLS_IE): Define.
(elf64_x86_64_hash_entry): Define.
(struct elf64_x86_64_obj_tdata): New.
(elf64_x86_64_tdata, elf64_x86_64_local_got_tls_type): Define.
(struct elf64_x86_64_link_hash_table): Add tls_ld_got.
(link_hash_newfunc): Initialize tls_type.
(elf64_x86_64_link_hash_table_create): Initialize tls_ld_got.
(elf64_x86_64_copy_indirect_symbol): Swap tls_type if necessary.
(elf64_x86_64_mkobject): New.
(elf64_x86_64_elf_object_p): Allocate struct elf64_x86_64_obj_tdata.
(elf64_x86_64_tls_transition): New.
(elf64_x86_64_check_relocs): Add r_type variable and use it.
Handle TLS relocs.
(elf64_x86_64_gc_sweep_hook): Handle TLS relocs.
(allocate_dynrelocs): Allocate GOT space for TLS relocs.
(elf64_x86_64_size_dynamic_sections): Likewise.
(dtpoff_base, tpoff): New.
(elf64_x86_64_relocate_section): Handle TLS relocs.
(elf64_x86_64_finish_dynamic_symbol): Only handle non-TLS GOT
entries.
(bfd_elf64_mkobject): Define.
* elf32-i386.c (elf_i386_check_relocs) [R_386_TLS_LE]: Set
DF_STATIC_TLS if shared.
gas/
* config/tc-i386.c (tc_i386_fix_adjustable): Add x86-64 TLS relocs.
Define them if not BFD_ASSEMBLER.
(lex_got): Handle @tlsgd, @dtpoff and @tpoff in 64-bit mode, add
@tlsld.
(md_apply_fix3): No addend for BFD_RELOC_X86_64_TLSGD,
BFD_RELOC_X86_64_TLSLD and BFD_RELOC_X86_64_GOTTPOFF.
(tc_gen_reloc): Handle x86-64 TLS relocs.
include/
* elf/x86-64.h: Add TLS relocs.
ld/testsuite/
* lib/ld-lib.exp (run_ld_link_tests): Add.
* ld-sh/sh64/sh64.exp (run_ld_link_tests, regexp_diff,
file_contents): Remove.
(sh64tests): Add 6th field to the tests array.
* ld-i386/i386.exp (run_ld_link_tests): Remove.
* ld-x86-64/x86-64.exp: New.
* ld-x86-64/tlsbin.dd: New test.
* ld-x86-64/tlsbinpic.s: New test.
* ld-x86-64/tlsbin.rd: New test.
* ld-x86-64/tlsbin.s: New test.
* ld-x86-64/tlsbin.sd: New test.
* ld-x86-64/tlsbin.td: New test.
* ld-x86-64/tlslib.s: New test.
* ld-x86-64/tlspic1.s: New test.
* ld-x86-64/tlspic2.s: New test.
* ld-x86-64/tlspic.dd: New test.
* ld-x86-64/tlspic.rd: New test.
* ld-x86-64/tlspic.sd: New test.
* ld-x86-64/tlspic.td: New test.
Diffstat (limited to 'bfd/elf64-x86-64.c')
-rw-r--r-- | bfd/elf64-x86-64.c | 675 |
1 files changed, 626 insertions, 49 deletions
diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c index 9c4a9d4..fa2f2ee 100644 --- a/bfd/elf64-x86-64.c +++ b/bfd/elf64-x86-64.c @@ -40,16 +40,16 @@ static reloc_howto_type x86_64_elf_howto_table[] = HOWTO(R_X86_64_64, 0, 4, 64, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_X86_64_64", false, MINUS_ONE, MINUS_ONE, false), - HOWTO(R_X86_64_PC32, 0, 4, 32, true, 0, complain_overflow_signed, + HOWTO(R_X86_64_PC32, 0, 2, 32, true, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_X86_64_PC32", false, 0xffffffff, 0xffffffff, true), - HOWTO(R_X86_64_GOT32, 0, 4, 32, false, 0, complain_overflow_signed, + HOWTO(R_X86_64_GOT32, 0, 2, 32, false, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_X86_64_GOT32", false, 0xffffffff, 0xffffffff, false), - HOWTO(R_X86_64_PLT32, 0, 4, 32, true, 0, complain_overflow_signed, + HOWTO(R_X86_64_PLT32, 0, 2, 32, true, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_X86_64_PLT32", false, 0xffffffff, 0xffffffff, true), - HOWTO(R_X86_64_COPY, 0, 4, 32, false, 0, complain_overflow_bitfield, + HOWTO(R_X86_64_COPY, 0, 2, 32, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_X86_64_COPY", false, 0xffffffff, 0xffffffff, false), HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, false, 0, complain_overflow_bitfield, @@ -61,13 +61,13 @@ static reloc_howto_type x86_64_elf_howto_table[] = HOWTO(R_X86_64_RELATIVE, 0, 4, 64, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_X86_64_RELATIVE", false, MINUS_ONE, MINUS_ONE, false), - HOWTO(R_X86_64_GOTPCREL, 0, 4, 32, true,0 , complain_overflow_signed, + HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, true, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", false, 0xffffffff, 0xffffffff, true), - HOWTO(R_X86_64_32, 0, 4, 32, false, 0, complain_overflow_unsigned, + HOWTO(R_X86_64_32, 0, 2, 32, false, 0, complain_overflow_unsigned, bfd_elf_generic_reloc, "R_X86_64_32", false, 0xffffffff, 0xffffffff, false), - HOWTO(R_X86_64_32S, 0, 4, 32, false, 0, complain_overflow_signed, + HOWTO(R_X86_64_32S, 0, 2, 32, false, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_X86_64_32S", false, 0xffffffff, 0xffffffff, false), HOWTO(R_X86_64_16, 0, 1, 16, false, 0, complain_overflow_bitfield, @@ -78,6 +78,30 @@ static reloc_howto_type x86_64_elf_howto_table[] = bfd_elf_generic_reloc, "R_X86_64_8", false, 0xff, 0xff, false), HOWTO(R_X86_64_PC8, 0, 0, 8, true, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_X86_64_PC8", false, 0xff, 0xff, true), + HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, false, 0, complain_overflow_bitfield, + bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", false, MINUS_ONE, + MINUS_ONE, false), + HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, false, 0, complain_overflow_bitfield, + bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", false, MINUS_ONE, + MINUS_ONE, false), + HOWTO(R_X86_64_TPOFF64, 0, 4, 64, false, 0, complain_overflow_bitfield, + bfd_elf_generic_reloc, "R_X86_64_TPOFF64", false, MINUS_ONE, + MINUS_ONE, false), + HOWTO(R_X86_64_TLSGD, 0, 2, 32, true, 0, complain_overflow_signed, + bfd_elf_generic_reloc, "R_X86_64_TLSGD", false, 0xffffffff, + 0xffffffff, true), + HOWTO(R_X86_64_TLSLD, 0, 2, 32, true, 0, complain_overflow_signed, + bfd_elf_generic_reloc, "R_X86_64_TLSLD", false, 0xffffffff, + 0xffffffff, true), + HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, false, 0, complain_overflow_bitfield, + bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", false, 0xffffffff, + 0xffffffff, false), + HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, true, 0, complain_overflow_signed, + bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", false, 0xffffffff, + 0xffffffff, true), + HOWTO(R_X86_64_TPOFF32, 0, 2, 32, false, 0, complain_overflow_signed, + bfd_elf_generic_reloc, "R_X86_64_TPOFF32", false, 0xffffffff, + 0xffffffff, false), /* GNU extension to record C++ vtable hierarchy. */ HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, false, 0, complain_overflow_dont, @@ -114,6 +138,14 @@ static const struct elf_reloc_map x86_64_reloc_map[] = { BFD_RELOC_16_PCREL, R_X86_64_PC16, }, { BFD_RELOC_8, R_X86_64_8, }, { BFD_RELOC_8_PCREL, R_X86_64_PC8, }, + { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, }, + { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, }, + { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, }, + { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, }, + { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, }, + { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, }, + { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, }, + { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, }, { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, }, { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, }, }; @@ -128,6 +160,10 @@ static boolean elf64_x86_64_grok_psinfo PARAMS ((bfd *, Elf_Internal_Note *)); static struct bfd_link_hash_table *elf64_x86_64_link_hash_table_create PARAMS ((bfd *)); +static int elf64_x86_64_tls_transition + PARAMS ((struct bfd_link_info *, int, int)); +static boolean elf64_x86_64_mkobject + PARAMS((bfd *)); static boolean elf64_x86_64_elf_object_p PARAMS ((bfd *abfd)); static boolean create_got_section PARAMS((bfd *, struct bfd_link_info *)); @@ -158,6 +194,10 @@ static boolean readonly_dynrelocs PARAMS ((struct elf_link_hash_entry *, PTR)); static boolean elf64_x86_64_size_dynamic_sections PARAMS ((bfd *, struct bfd_link_info *)); +static bfd_vma dtpoff_base + PARAMS ((struct bfd_link_info *)); +static bfd_vma tpoff + PARAMS ((struct bfd_link_info *, bfd_vma)); static boolean elf64_x86_64_relocate_section PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **)); @@ -198,13 +238,13 @@ elf64_x86_64_info_to_howto (abfd, cache_ptr, dst) r_type = ELF64_R_TYPE (dst->r_info); if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT) { - BFD_ASSERT (r_type <= (unsigned int) R_X86_64_PC8); + BFD_ASSERT (r_type <= (unsigned int) R_X86_64_TPOFF32); i = r_type; } else { BFD_ASSERT (r_type < (unsigned int) R_X86_64_max); - i = r_type - ((unsigned int) R_X86_64_GNU_VTINHERIT - R_X86_64_PC8 - 1); + i = r_type - ((unsigned int) R_X86_64_GNU_VTINHERIT - R_X86_64_TPOFF32 - 1); } cache_ptr->howto = &x86_64_elf_howto_table[i]; BFD_ASSERT (r_type == cache_ptr->howto->type); @@ -343,8 +383,32 @@ struct elf64_x86_64_link_hash_entry /* Track dynamic relocs copied for this symbol. */ struct elf64_x86_64_dyn_relocs *dyn_relocs; + +#define GOT_UNKNOWN 0 +#define GOT_NORMAL 1 +#define GOT_TLS_GD 2 +#define GOT_TLS_IE 3 + unsigned char tls_type; +}; + +#define elf64_x86_64_hash_entry(ent) \ + ((struct elf64_x86_64_link_hash_entry *)(ent)) + +struct elf64_x86_64_obj_tdata +{ + struct elf_obj_tdata root; + + /* tls_type for each local got entry. */ + char *local_got_tls_type; }; +#define elf64_x86_64_tdata(abfd) \ + ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any) + +#define elf64_x86_64_local_got_tls_type(abfd) \ + (elf64_x86_64_tdata (abfd)->local_got_tls_type) + + /* x86-64 ELF linker hash table. */ struct elf64_x86_64_link_hash_table @@ -360,6 +424,11 @@ struct elf64_x86_64_link_hash_table asection *sdynbss; asection *srelbss; + union { + bfd_signed_vma refcount; + bfd_vma offset; + } tls_ld_got; + /* Small local sym to section mapping cache. */ struct sym_sec_cache sym_sec; }; @@ -395,6 +464,7 @@ link_hash_newfunc (entry, table, string) eh = (struct elf64_x86_64_link_hash_entry *) entry; eh->dyn_relocs = NULL; + eh->tls_type = GOT_UNKNOWN; } return entry; @@ -427,6 +497,7 @@ elf64_x86_64_link_hash_table_create (abfd) ret->sdynbss = NULL; ret->srelbss = NULL; ret->sym_sec.abfd = NULL; + ret->tls_ld_got.refcount = 0; return &ret->elf.root; } @@ -538,18 +609,67 @@ elf64_x86_64_copy_indirect_symbol (bed, dir, ind) eind->dyn_relocs = NULL; } + if (ind->root.type == bfd_link_hash_indirect + && dir->got.refcount <= 0) + { + edir->tls_type = eind->tls_type; + eind->tls_type = GOT_UNKNOWN; + } + _bfd_elf_link_hash_copy_indirect (bed, dir, ind); } static boolean -elf64_x86_64_elf_object_p (abfd) +elf64_x86_64_mkobject (abfd) bfd *abfd; { + bfd_size_type amt = sizeof (struct elf64_x86_64_obj_tdata); + abfd->tdata.any = bfd_zalloc (abfd, amt); + if (abfd->tdata.any == NULL) + return false; + return true; +} + +static boolean +elf64_x86_64_elf_object_p (abfd) + bfd *abfd; +{ + /* Allocate our special target data. */ + struct elf64_x86_64_obj_tdata *new_tdata; + bfd_size_type amt = sizeof (struct elf64_x86_64_obj_tdata); + new_tdata = bfd_zalloc (abfd, amt); + if (new_tdata == NULL) + return false; + new_tdata->root = *abfd->tdata.elf_obj_data; + abfd->tdata.any = new_tdata; /* Set the right machine number for an x86-64 elf64 file. */ bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64); return true; } +static int +elf64_x86_64_tls_transition (info, r_type, is_local) + struct bfd_link_info *info; + int r_type; + int is_local; +{ + if (info->shared) + return r_type; + + switch (r_type) + { + case R_X86_64_TLSGD: + case R_X86_64_GOTTPOFF: + if (is_local) + return R_X86_64_TPOFF32; + return R_X86_64_GOTTPOFF; + case R_X86_64_TLSLD: + return R_X86_64_TPOFF32; + } + + return r_type; +} + /* Look through the relocs for a section during the first phase, and calculate needed space in the global offset table, procedure linkage table, and dynamic reloc sections. */ @@ -580,10 +700,12 @@ elf64_x86_64_check_relocs (abfd, info, sec, relocs) rel_end = relocs + sec->reloc_count; for (rel = relocs; rel < rel_end; rel++) { + unsigned int r_type; unsigned long r_symndx; struct elf_link_hash_entry *h; r_symndx = ELF64_R_SYM (rel->r_info); + r_type = ELF64_R_TYPE (rel->r_info); if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr)) { @@ -598,38 +720,103 @@ elf64_x86_64_check_relocs (abfd, info, sec, relocs) else h = sym_hashes[r_symndx - symtab_hdr->sh_info]; - switch (ELF64_R_TYPE (rel->r_info)) + r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL); + switch (r_type) { - case R_X86_64_GOT32: - case R_X86_64_GOTPCREL: - /* This symbol requires a global offset table entry. */ - if (h != NULL) + case R_X86_64_TLSLD: + htab->tls_ld_got.refcount += 1; + goto create_got; + + case R_X86_64_TPOFF32: + if (info->shared) { - h->got.refcount += 1; + (*_bfd_error_handler) + (_("%s: relocation %s can not be used when making a shared object; recompile with -fPIC"), + bfd_archive_filename (abfd), + x86_64_elf_howto_table[r_type].name); + bfd_set_error (bfd_error_bad_value); + return false; } - else - { - bfd_signed_vma *local_got_refcounts; + break; - /* This is a global offset table entry for a local symbol. */ - local_got_refcounts = elf_local_got_refcounts (abfd); - if (local_got_refcounts == NULL) - { - bfd_size_type size; + case R_X86_64_GOTTPOFF: + if (info->shared) + info->flags |= DF_STATIC_TLS; + /* Fall through */ - size = symtab_hdr->sh_info; - size *= sizeof (bfd_signed_vma); - local_got_refcounts = ((bfd_signed_vma *) - bfd_zalloc (abfd, size)); - if (local_got_refcounts == NULL) + case R_X86_64_GOT32: + case R_X86_64_GOTPCREL: + case R_X86_64_TLSGD: + /* This symbol requires a global offset table entry. */ + { + int tls_type, old_tls_type; + + switch (r_type) + { + default: tls_type = GOT_NORMAL; break; + case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break; + case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break; + } + + if (h != NULL) + { + h->got.refcount += 1; + old_tls_type = elf64_x86_64_hash_entry (h)->tls_type; + } + else + { + bfd_signed_vma *local_got_refcounts; + + /* This is a global offset table entry for a local symbol. */ + local_got_refcounts = elf_local_got_refcounts (abfd); + if (local_got_refcounts == NULL) + { + bfd_size_type size; + + size = symtab_hdr->sh_info; + size *= sizeof (bfd_signed_vma) + sizeof (char); + local_got_refcounts = ((bfd_signed_vma *) + bfd_zalloc (abfd, size)); + if (local_got_refcounts == NULL) + return false; + elf_local_got_refcounts (abfd) = local_got_refcounts; + elf64_x86_64_local_got_tls_type (abfd) + = (char *) (local_got_refcounts + symtab_hdr->sh_info); + } + local_got_refcounts[r_symndx] += 1; + old_tls_type + = elf64_x86_64_local_got_tls_type (abfd) [r_symndx]; + } + + /* If a TLS symbol is accessed using IE at least once, + there is no point to use dynamic model for it. */ + if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN + && (old_tls_type != GOT_TLS_GD || tls_type != GOT_TLS_IE)) + { + if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD) + tls_type = old_tls_type; + else + { + (*_bfd_error_handler) + (_("%s: %s' accessed both as normal and thread local symbol"), + bfd_archive_filename (abfd), + h ? h->root.root.string : "<local>"); return false; - elf_local_got_refcounts (abfd) = local_got_refcounts; - } - local_got_refcounts[r_symndx] += 1; - } + } + } + + if (old_tls_type != tls_type) + { + if (h != NULL) + elf64_x86_64_hash_entry (h)->tls_type = tls_type; + else + elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type; + } + } /* Fall through */ //case R_X86_64_GOTPCREL: + create_got: if (htab->sgot == NULL) { if (htab->elf.dynobj == NULL) @@ -671,7 +858,7 @@ elf64_x86_64_check_relocs (abfd, info, sec, relocs) (*_bfd_error_handler) (_("%s: relocation %s can not be used when making a shared object; recompile with -fPIC"), bfd_archive_filename (abfd), - x86_64_elf_howto_table[ELF64_R_TYPE (rel->r_info)].name); + x86_64_elf_howto_table[r_type].name); bfd_set_error (bfd_error_bad_value); return false; } @@ -719,9 +906,9 @@ elf64_x86_64_check_relocs (abfd, info, sec, relocs) symbol. */ if ((info->shared && (sec->flags & SEC_ALLOC) != 0 - && (((ELF64_R_TYPE (rel->r_info) != R_X86_64_PC8) - && (ELF64_R_TYPE (rel->r_info) != R_X86_64_PC16) - && (ELF64_R_TYPE (rel->r_info) != R_X86_64_PC32)) + && (((r_type != R_X86_64_PC8) + && (r_type != R_X86_64_PC16) + && (r_type != R_X86_64_PC32)) || (h != NULL && (! info->symbolic || h->root.type == bfd_link_hash_defweak @@ -822,9 +1009,9 @@ elf64_x86_64_check_relocs (abfd, info, sec, relocs) } p->count += 1; - if (ELF64_R_TYPE (rel->r_info) == R_X86_64_PC8 - || ELF64_R_TYPE (rel->r_info) == R_X86_64_PC16 - || ELF64_R_TYPE (rel->r_info) == R_X86_64_PC32) + if (r_type == R_X86_64_PC8 + || r_type == R_X86_64_PC16 + || r_type == R_X86_64_PC32) p->pc_count += 1; } break; @@ -905,6 +1092,7 @@ elf64_x86_64_gc_sweep_hook (abfd, info, sec, relocs) bfd_signed_vma *local_got_refcounts; const Elf_Internal_Rela *rel, *relend; unsigned long r_symndx; + int r_type; struct elf_link_hash_entry *h; elf_section_data (sec)->local_dynrel = NULL; @@ -915,8 +1103,18 @@ elf64_x86_64_gc_sweep_hook (abfd, info, sec, relocs) relend = relocs + sec->reloc_count; for (rel = relocs; rel < relend; rel++) - switch (ELF64_R_TYPE (rel->r_info)) + switch ((r_type = elf64_x86_64_tls_transition (info, + ELF64_R_TYPE (rel->r_info), + ELF64_R_SYM (rel->r_info) + >= symtab_hdr->sh_info))) { + case R_X86_64_TLSLD: + if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0) + elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1; + break; + + case R_X86_64_TLSGD: + case R_X86_64_GOTTPOFF: case R_X86_64_GOT32: case R_X86_64_GOTPCREL: r_symndx = ELF64_R_SYM (rel->r_info); @@ -1226,10 +1424,18 @@ allocate_dynrelocs (h, inf) h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT; } - if (h->got.refcount > 0) + /* If R_X86_64_GOTTPOFF symbol is now local to the binary, + make it a R_X86_64_TPOFF32 requiring no GOT entry. */ + if (h->got.refcount > 0 + && !info->shared + && h->dynindx == -1 + && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE) + h->got.offset = (bfd_vma) -1; + else if (h->got.refcount > 0) { asection *s; boolean dyn; + int tls_type = elf64_x86_64_hash_entry (h)->tls_type; /* Make sure this symbol is output as a dynamic symbol. Undefined weak syms won't yet be marked as dynamic. */ @@ -1243,8 +1449,19 @@ allocate_dynrelocs (h, inf) s = htab->sgot; h->got.offset = s->_raw_size; s->_raw_size += GOT_ENTRY_SIZE; + /* R_X86_64_TLSGD needs 2 consecutive GOT slots. */ + if (tls_type == GOT_TLS_GD) + s->_raw_size += GOT_ENTRY_SIZE; dyn = htab->elf.dynamic_sections_created; - if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)) + /* R_X86_64_TLSGD needs one dynamic relocation if local symbol + and two if global. + R_X86_64_GOTTPOFF needs one dynamic relocation. */ + if ((tls_type == GOT_TLS_GD && h->dynindx == -1) + || tls_type == GOT_TLS_IE) + htab->srelgot->_raw_size += sizeof (Elf64_External_Rela); + else if (tls_type == GOT_TLS_GD) + htab->srelgot->_raw_size += 2 * sizeof (Elf64_External_Rela); + else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)) htab->srelgot->_raw_size += sizeof (Elf64_External_Rela); } else @@ -1390,6 +1607,7 @@ elf64_x86_64_size_dynamic_sections (output_bfd, info) { bfd_signed_vma *local_got; bfd_signed_vma *end_local_got; + char *local_tls_type; bfd_size_type locsymcount; Elf_Internal_Shdr *symtab_hdr; asection *srel; @@ -1432,15 +1650,20 @@ elf64_x86_64_size_dynamic_sections (output_bfd, info) symtab_hdr = &elf_tdata (ibfd)->symtab_hdr; locsymcount = symtab_hdr->sh_info; end_local_got = local_got + locsymcount; + local_tls_type = elf64_x86_64_local_got_tls_type (ibfd); s = htab->sgot; srel = htab->srelgot; - for (; local_got < end_local_got; ++local_got) + for (; local_got < end_local_got; ++local_got, ++local_tls_type) { if (*local_got > 0) { *local_got = s->_raw_size; s->_raw_size += GOT_ENTRY_SIZE; - if (info->shared) + if (*local_tls_type == GOT_TLS_GD) + s->_raw_size += GOT_ENTRY_SIZE; + if (info->shared + || *local_tls_type == GOT_TLS_GD + || *local_tls_type == GOT_TLS_IE) srel->_raw_size += sizeof (Elf64_External_Rela); } else @@ -1448,6 +1671,17 @@ elf64_x86_64_size_dynamic_sections (output_bfd, info) } } + if (htab->tls_ld_got.refcount > 0) + { + /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD + relocs. */ + htab->tls_ld_got.offset = htab->sgot->_raw_size; + htab->sgot->_raw_size += 2 * GOT_ENTRY_SIZE; + htab->srelgot->_raw_size += sizeof (Elf64_External_Rela); + } + else + htab->tls_ld_got.offset = -1; + /* Allocate global sym .plt and .got entries, and space for global sym dynamic relocs. */ elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info); @@ -1558,6 +1792,38 @@ elf64_x86_64_size_dynamic_sections (output_bfd, info) return true; } +/* Return the base VMA address which should be subtracted from real addresses + when resolving @dtpoff relocation. + This is PT_TLS segment p_vaddr. */ + +static bfd_vma +dtpoff_base (info) + struct bfd_link_info *info; +{ + /* If tls_segment is NULL, we should have signalled an error already. */ + if (elf_hash_table (info)->tls_segment == NULL) + return 0; + return elf_hash_table (info)->tls_segment->start; +} + +/* Return the relocation value for @tpoff relocation + if STT_TLS virtual address is ADDRESS. */ + +static bfd_vma +tpoff (info, address) + struct bfd_link_info *info; + bfd_vma address; +{ + struct elf_link_tls_segment *tls_segment + = elf_hash_table (info)->tls_segment; + + /* If tls_segment is NULL, we should have signalled an error already. */ + if (tls_segment == NULL) + return 0; + return address - align_power (tls_segment->size, tls_segment->align) + - tls_segment->start; +} + /* Relocate an x86_64 ELF section. */ static boolean @@ -1591,7 +1857,7 @@ elf64_x86_64_relocate_section (output_bfd, info, input_bfd, input_section, relend = relocs + input_section->reloc_count; for (; rel < relend; rel++) { - int r_type; + unsigned int r_type; reloc_howto_type *howto; unsigned long r_symndx; struct elf_link_hash_entry *h; @@ -1601,13 +1867,14 @@ elf64_x86_64_relocate_section (output_bfd, info, input_bfd, input_section, bfd_vma relocation; boolean unresolved_reloc; bfd_reloc_status_type r; + int tls_type; r_type = ELF64_R_TYPE (rel->r_info); if (r_type == (int) R_X86_64_GNU_VTINHERIT || r_type == (int) R_X86_64_GNU_VTENTRY) continue; - if (r_type < 0 || r_type >= R_X86_64_max) + if (r_type >= R_X86_64_max) { bfd_set_error (bfd_error_bad_value); return false; @@ -1933,6 +2200,313 @@ elf64_x86_64_relocate_section (output_bfd, info, input_bfd, input_section, break; + case R_X86_64_TLSGD: + case R_X86_64_GOTTPOFF: + r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL); + tls_type = GOT_UNKNOWN; + if (h == NULL && local_got_offsets) + tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx]; + else if (h != NULL) + { + tls_type = elf64_x86_64_hash_entry (h)->tls_type; + if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE) + r_type = R_X86_64_TPOFF32; + } + if (r_type == R_X86_64_TLSGD) + { + if (tls_type == GOT_TLS_IE) + r_type = R_X86_64_GOTTPOFF; + } + + if (r_type == R_X86_64_TPOFF32) + { + BFD_ASSERT (! unresolved_reloc); + if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD) + { + unsigned int i; + static unsigned char tlsgd[7] + = { 0x66, 0x66, 0x66, 0x66, 0x48, 0x8d, 0x3d }; + + /* GD->LE transition. + .long 0x66666666; leaq foo@tlsgd(%rip), %rdi + callq __tls_get_addr@plt + Change it into: + movq %fs:0, %rax + leaq foo@tpoff(%rax), %rax */ + BFD_ASSERT (rel->r_offset >= 7); + for (i = 0; i < 7; i++) + BFD_ASSERT (bfd_get_8 (input_bfd, + contents + rel->r_offset - 7 + i) + == tlsgd[i]); + BFD_ASSERT (rel->r_offset + 9 <= input_section->_raw_size); + BFD_ASSERT (bfd_get_8 (input_bfd, + contents + rel->r_offset + 4) + == 0xe8); + BFD_ASSERT (rel + 1 < relend); + BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32); + memcpy (contents + rel->r_offset - 7, + "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0", + 16); + bfd_put_32 (output_bfd, tpoff (info, relocation), + contents + rel->r_offset + 5); + /* Skip R_X86_64_PLT32. */ + rel++; + continue; + } + else + { + unsigned int val, type, reg; + + /* IE->LE transition: + Originally it can be one of: + movq foo@gottpoff(%rip), %reg + addq foo@gottpoff(%rip), %reg + We change it into: + movq $foo, %reg + leaq foo(%reg), %reg + addq $foo, %reg. */ + BFD_ASSERT (rel->r_offset >= 3); + val = bfd_get_8 (input_bfd, contents + rel->r_offset - 3); + BFD_ASSERT (val == 0x48 || val == 0x4c); + type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2); + BFD_ASSERT (type == 0x8b || type == 0x03); + reg = bfd_get_8 (input_bfd, contents + rel->r_offset - 1); + BFD_ASSERT ((reg & 0xc7) == 5); + reg >>= 3; + BFD_ASSERT (rel->r_offset + 4 <= input_section->_raw_size); + if (type == 0x8b) + { + /* movq */ + if (val == 0x4c) + bfd_put_8 (output_bfd, 0x49, + contents + rel->r_offset - 3); + bfd_put_8 (output_bfd, 0xc7, + contents + rel->r_offset - 2); + bfd_put_8 (output_bfd, 0xc0 | reg, + contents + rel->r_offset - 1); + } + else if (reg == 4) + { + /* addq -> addq - addressing with %rsp/%r12 is + special */ + if (val == 0x4c) + bfd_put_8 (output_bfd, 0x49, + contents + rel->r_offset - 3); + bfd_put_8 (output_bfd, 0x81, + contents + rel->r_offset - 2); + bfd_put_8 (output_bfd, 0xc0 | reg, + contents + rel->r_offset - 1); + } + else + { + /* addq -> leaq */ + if (val == 0x4c) + bfd_put_8 (output_bfd, 0x4d, + contents + rel->r_offset - 3); + bfd_put_8 (output_bfd, 0x8d, + contents + rel->r_offset - 2); + bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3), + contents + rel->r_offset - 1); + } + bfd_put_32 (output_bfd, tpoff (info, relocation), + contents + rel->r_offset); + continue; + } + } + + if (htab->sgot == NULL) + abort (); + + if (h != NULL) + off = h->got.offset; + else + { + if (local_got_offsets == NULL) + abort (); + + off = local_got_offsets[r_symndx]; + } + + if ((off & 1) != 0) + off &= ~1; + else + { + Elf_Internal_Rela outrel; + Elf64_External_Rela *loc; + int dr_type, indx; + + if (htab->srelgot == NULL) + abort (); + + outrel.r_offset = (htab->sgot->output_section->vma + + htab->sgot->output_offset + off); + + indx = h && h->dynindx != -1 ? h->dynindx : 0; + if (r_type == R_X86_64_TLSGD) + dr_type = R_X86_64_DTPMOD64; + else + dr_type = R_X86_64_TPOFF64; + + bfd_put_64 (output_bfd, 0, htab->sgot->contents + off); + outrel.r_addend = 0; + if (dr_type == R_X86_64_TPOFF64 && indx == 0) + outrel.r_addend = relocation - dtpoff_base (info); + outrel.r_info = ELF64_R_INFO (indx, dr_type); + + loc = (Elf64_External_Rela *) htab->srelgot->contents; + loc += htab->srelgot->reloc_count++; + bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); + + if (r_type == R_X86_64_TLSGD) + { + if (indx == 0) + { + BFD_ASSERT (! unresolved_reloc); + bfd_put_64 (output_bfd, + relocation - dtpoff_base (info), + htab->sgot->contents + off + GOT_ENTRY_SIZE); + } + else + { + bfd_put_64 (output_bfd, 0, + htab->sgot->contents + off + GOT_ENTRY_SIZE); + outrel.r_info = ELF64_R_INFO (indx, + R_X86_64_DTPOFF64); + outrel.r_offset += GOT_ENTRY_SIZE; + htab->srelgot->reloc_count++; + loc++; + bfd_elf64_swap_reloca_out (output_bfd, &outrel, + loc); + } + } + + if (h != NULL) + h->got.offset |= 1; + else + local_got_offsets[r_symndx] |= 1; + } + + if (off >= (bfd_vma) -2) + abort (); + if (r_type == ELF64_R_TYPE (rel->r_info)) + { + relocation = htab->sgot->output_section->vma + + htab->sgot->output_offset + off; + unresolved_reloc = false; + } + else + { + unsigned int i; + static unsigned char tlsgd[7] + = { 0x66, 0x66, 0x66, 0x66, 0x48, 0x8d, 0x3d }; + + /* GD->IE transition. + .long 0x66666666; leaq foo@tlsgd(%rip), %rdi + callq __tls_get_addr@plt + Change it into: + movq %fs:0, %rax + addq foo@gottpoff(%rip), %rax */ + BFD_ASSERT (rel->r_offset >= 7); + for (i = 0; i < 7; i++) + BFD_ASSERT (bfd_get_8 (input_bfd, + contents + rel->r_offset - 7 + i) + == tlsgd[i]); + BFD_ASSERT (rel->r_offset + 9 <= input_section->_raw_size); + BFD_ASSERT (bfd_get_8 (input_bfd, + contents + rel->r_offset + 4) + == 0xe8); + BFD_ASSERT (rel + 1 < relend); + BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32); + memcpy (contents + rel->r_offset - 7, + "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0", + 16); + + relocation = (htab->sgot->output_section->vma + + htab->sgot->output_offset + off + - rel->r_offset + - input_section->output_section->vma + - input_section->output_offset + - 9); + bfd_put_32 (output_bfd, relocation, + contents + rel->r_offset + 5); + /* Skip R_X86_64_PLT32. */ + rel++; + continue; + } + break; + + case R_X86_64_TLSLD: + if (! info->shared) + { + /* LD->LE transition: + Ensure it is: + leaq foo@tlsld(%rip), %rdi; call __tls_get_addr@plt. + We change it into: + .word 0x6666; .byte 0x66; movl %fs:0, %rax. */ + BFD_ASSERT (rel->r_offset >= 3); + BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 3) + == 0x48); + BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2) + == 0x8d); + BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 1) + == 0x3d); + BFD_ASSERT (rel->r_offset + 9 <= input_section->_raw_size); + BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4) + == 0xe8); + BFD_ASSERT (rel + 1 < relend); + BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32); + memcpy (contents + rel->r_offset - 3, + "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12); + /* Skip R_X86_64_PLT32. */ + rel++; + continue; + } + + if (htab->sgot == NULL) + abort (); + + off = htab->tls_ld_got.offset; + if (off & 1) + off &= ~1; + else + { + Elf_Internal_Rela outrel; + Elf64_External_Rela *loc; + + if (htab->srelgot == NULL) + abort (); + + outrel.r_offset = (htab->sgot->output_section->vma + + htab->sgot->output_offset + off); + + bfd_put_64 (output_bfd, 0, + htab->sgot->contents + off); + bfd_put_64 (output_bfd, 0, + htab->sgot->contents + off + GOT_ENTRY_SIZE); + outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64); + outrel.r_addend = 0; + loc = (Elf64_External_Rela *) htab->srelgot->contents; + loc += htab->srelgot->reloc_count++; + bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); + htab->tls_ld_got.offset |= 1; + } + relocation = htab->sgot->output_section->vma + + htab->sgot->output_offset + off; + unresolved_reloc = false; + break; + + case R_X86_64_DTPOFF32: + if (info->shared) + relocation -= dtpoff_base (info); + else + relocation = tpoff (info, relocation); + break; + + case R_X86_64_TPOFF32: + BFD_ASSERT (! info->shared); + relocation = tpoff (info, relocation); + break; + default: break; } @@ -2087,13 +2661,15 @@ elf64_x86_64_finish_dynamic_symbol (output_bfd, info, h, sym) } } - if (h->got.offset != (bfd_vma) -1) + if (h->got.offset != (bfd_vma) -1 + && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_GD + && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE) { Elf_Internal_Rela rela; Elf64_External_Rela *loc; /* This symbol has an entry in the global offset table. Set it - up. */ + up. */ if (htab->sgot == NULL || htab->srelgot == NULL) abort (); @@ -2351,5 +2927,6 @@ elf64_x86_64_finish_dynamic_sections (output_bfd, info) #define elf_backend_relocate_section elf64_x86_64_relocate_section #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections #define elf_backend_object_p elf64_x86_64_elf_object_p +#define bfd_elf64_mkobject elf64_x86_64_mkobject #include "elf64-target.h" |