+ 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[8]
+ = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
+
+ /* GD->LE transition.
+ .byte 0x66; leaq foo@tlsgd(%rip), %rdi
+ .word 0x6666; rex64; call __tls_get_addr@plt
+ Change it into:
+ movq %fs:0, %rax
+ leaq foo@tpoff(%rax), %rax */
+ BFD_ASSERT (rel->r_offset >= 4);
+ for (i = 0; i < 4; i++)
+ BFD_ASSERT (bfd_get_8 (input_bfd,
+ contents + rel->r_offset - 4 + i)
+ == tlsgd[i]);
+ BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
+ for (i = 0; i < 4; i++)
+ BFD_ASSERT (bfd_get_8 (input_bfd,
+ contents + rel->r_offset + 4 + i)
+ == tlsgd[i+4]);
+ BFD_ASSERT (rel + 1 < relend);
+ BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
+ memcpy (contents + rel->r_offset - 4,
+ "\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 + 8);
+ /* 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->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;
+ bfd_byte *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 = htab->srelgot->contents;
+ loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
+ 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 += sizeof (Elf64_External_Rela);
+ 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[8]
+ = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
+
+ /* GD->IE transition.
+ .byte 0x66; leaq foo@tlsgd(%rip), %rdi
+ .word 0x6666; rex64; call __tls_get_addr@plt
+ Change it into:
+ movq %fs:0, %rax
+ addq foo@gottpoff(%rip), %rax */
+ BFD_ASSERT (rel->r_offset >= 4);
+ for (i = 0; i < 4; i++)
+ BFD_ASSERT (bfd_get_8 (input_bfd,
+ contents + rel->r_offset - 4 + i)
+ == tlsgd[i]);
+ BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
+ for (i = 0; i < 4; i++)
+ BFD_ASSERT (bfd_get_8 (input_bfd,
+ contents + rel->r_offset + 4 + i)
+ == tlsgd[i+4]);
+ BFD_ASSERT (rel + 1 < relend);
+ BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
+ memcpy (contents + rel->r_offset - 4,
+ "\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
+ - 12);
+ bfd_put_32 (output_bfd, relocation,
+ contents + rel->r_offset + 8);
+ /* 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->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;
+ bfd_byte *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 = htab->srelgot->contents;
+ loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
+ 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 || (input_section->flags & SEC_CODE) == 0)
+ relocation -= dtpoff_base (info);
+ else
+ relocation = tpoff (info, relocation);
+ break;
+
+ case R_X86_64_TPOFF32:
+ BFD_ASSERT (! info->shared);
+ relocation = tpoff (info, relocation);
+ break;
+