+ case R_386_TLS_LDM:
+ if (offset < 2 || (rel + 1) >= relend)
+ return FALSE;
+
+ type = bfd_get_8 (abfd, contents + offset - 2);
+ if (r_type == R_386_TLS_GD)
+ {
+ /* Check transition from LD access model. Only
+ leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr
+ leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop
+ can transit to different access model. */
+ if ((offset + 10) > sec->size ||
+ (type != 0x8d && type != 0x04))
+ return FALSE;
+
+ val = bfd_get_8 (abfd, contents + offset - 1);
+ if (type == 0x04)
+ {
+ /* leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr */
+ if (offset < 3)
+ return FALSE;
+
+ if (bfd_get_8 (abfd, contents + offset - 3) != 0x8d)
+ return FALSE;
+
+ if ((val & 0xc7) != 0x05 || val == (4 << 3))
+ return FALSE;
+ }
+ else
+ {
+ /* leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop */
+ if ((val & 0xf8) != 0x80 || (val & 7) == 4)
+ return FALSE;
+
+ if (bfd_get_8 (abfd, contents + offset + 9) != 0x90)
+ return FALSE;
+ }
+ }
+ else
+ {
+ /* Check transition from LD access model. Only
+ leal foo@tlsgd(%reg), %eax; call ___tls_get_addr
+ can transit to different access model. */
+ if (type != 0x8d || (offset + 9) > sec->size)
+ return FALSE;
+
+ val = bfd_get_8 (abfd, contents + offset - 1);
+ if ((val & 0xf8) != 0x80 || (val & 7) == 4)
+ return FALSE;
+ }
+
+ if (bfd_get_8 (abfd, contents + offset + 4) != 0xe8)
+ return FALSE;
+
+ r_symndx = ELF32_R_SYM (rel[1].r_info);
+ if (r_symndx < symtab_hdr->sh_info)
+ return FALSE;
+
+ h = sym_hashes[r_symndx - symtab_hdr->sh_info];
+ return (h != NULL
+ && h->root.root.string != NULL
+ && (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
+ || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32)
+ && (strcmp (h->root.root.string, "___tls_get_addr") == 0));
+
+ case R_386_TLS_IE:
+ /* Check transition from IE access model:
+ movl foo@indntpoff(%rip), %eax
+ movl foo@indntpoff(%rip), %reg
+ addl foo@indntpoff(%rip), %reg
+ */
+
+ if (offset < 1 || (offset + 4) > sec->size)
+ return FALSE;
+
+ /* Check "movl foo@tpoff(%rip), %eax" first. */
+ val = bfd_get_8 (abfd, contents + offset - 1);
+ if (val == 0xa1)
+ return TRUE;
+
+ if (offset < 2)
+ return FALSE;
+
+ /* Check movl|addl foo@tpoff(%rip), %reg. */
+ type = bfd_get_8 (abfd, contents + offset - 2);
+ return ((type == 0x8b || type == 0x03)
+ && (val & 0xc7) == 0x05);
+
+ case R_386_TLS_GOTIE:
+ case R_386_TLS_IE_32:
+ /* Check transition from {IE_32,GOTIE} access model:
+ subl foo@{tpoff,gontoff}(%reg1), %reg2
+ movl foo@{tpoff,gontoff}(%reg1), %reg2
+ addl foo@{tpoff,gontoff}(%reg1), %reg2
+ */
+
+ if (offset < 2 || (offset + 4) > sec->size)
+ return FALSE;
+
+ val = bfd_get_8 (abfd, contents + offset - 1);
+ if ((val & 0xc0) != 0x80 || (val & 7) == 4)
+ return FALSE;
+
+ type = bfd_get_8 (abfd, contents + offset - 2);
+ return type == 0x8b || type == 0x2b || type == 0x03;
+
+ case R_386_TLS_GOTDESC:
+ /* Check transition from GDesc access model:
+ leal x@tlsdesc(%ebx), %eax
+
+ Make sure it's a leal adding ebx to a 32-bit offset
+ into any register, although it's probably almost always
+ going to be eax. */
+
+ if (offset < 2 || (offset + 4) > sec->size)
+ return FALSE;
+
+ if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
+ return FALSE;
+
+ val = bfd_get_8 (abfd, contents + offset - 1);
+ return (val & 0xc7) == 0x83;
+
+ case R_386_TLS_DESC_CALL:
+ /* Check transition from GDesc access model:
+ call *x@tlsdesc(%rax)
+ */
+ if (offset + 2 <= sec->size)
+ {
+ /* Make sure that it's a call *x@tlsdesc(%rax). */
+ static i386_opcode16 call = { { 0xff, 0x10 } };
+ return bfd_get_16 (abfd, contents + offset) == call.i;
+ }
+
+ return FALSE;
+
+ default:
+ abort ();
+ }
+}
+
+/* Return TRUE if the TLS access transition is OK or no transition
+ will be performed. Update R_TYPE if there is a transition. */
+
+static bfd_boolean
+elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
+ asection *sec, bfd_byte *contents,
+ Elf_Internal_Shdr *symtab_hdr,
+ struct elf_link_hash_entry **sym_hashes,
+ unsigned int *r_type, int tls_type,
+ const Elf_Internal_Rela *rel,
+ const Elf_Internal_Rela *relend,
+ struct elf_link_hash_entry *h)
+{
+ unsigned int from_type = *r_type;
+ unsigned int to_type = from_type;
+ bfd_boolean check = TRUE;
+
+ switch (from_type)
+ {
+ case R_386_TLS_GD:
+ case R_386_TLS_GOTDESC:
+ case R_386_TLS_DESC_CALL: