878
901
_bfd_elf_link_hash_copy_indirect (info, dir, ind);
882
elf_i386_tls_transition (struct bfd_link_info *info, int r_type, int is_local)
911
/* Return TRUE if the TLS access code sequence support transition
915
elf_i386_check_tls_transition (bfd *abfd, asection *sec,
917
Elf_Internal_Shdr *symtab_hdr,
918
struct elf_link_hash_entry **sym_hashes,
920
const Elf_Internal_Rela *rel,
921
const Elf_Internal_Rela *relend)
923
unsigned int val, type;
924
unsigned long r_symndx;
925
struct elf_link_hash_entry *h;
928
/* Get the section contents. */
929
if (contents == NULL)
931
if (elf_section_data (sec)->this_hdr.contents != NULL)
932
contents = elf_section_data (sec)->this_hdr.contents;
935
/* FIXME: How to better handle error condition? */
936
if (!bfd_malloc_and_get_section (abfd, sec, &contents))
939
/* Cache the section contents for elf_link_input_bfd. */
940
elf_section_data (sec)->this_hdr.contents = contents;
944
offset = rel->r_offset;
889
947
case R_386_TLS_GD:
890
case R_386_TLS_GOTDESC:
891
case R_386_TLS_DESC_CALL:
892
case R_386_TLS_IE_32:
894
return R_386_TLS_LE_32;
895
return R_386_TLS_IE_32;
897
case R_386_TLS_GOTIE:
899
return R_386_TLS_LE_32;
902
return R_386_TLS_LE_32;
949
if (offset < 2 || (rel + 1) >= relend)
952
type = bfd_get_8 (abfd, contents + offset - 2);
953
if (r_type == R_386_TLS_GD)
955
/* Check transition from LD access model. Only
956
leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr
957
leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop
958
can transit to different access model. */
959
if ((offset + 10) > sec->size ||
960
(type != 0x8d && type != 0x04))
963
val = bfd_get_8 (abfd, contents + offset - 1);
966
/* leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr */
970
if (bfd_get_8 (abfd, contents + offset - 3) != 0x8d)
973
if ((val & 0xc7) != 0x05 || val == (4 << 3))
978
/* leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop */
979
if ((val & 0xf8) != 0x80 || (val & 7) == 4)
982
if (bfd_get_8 (abfd, contents + offset + 9) != 0x90)
988
/* Check transition from LD access model. Only
989
leal foo@tlsgd(%reg), %eax; call ___tls_get_addr
990
can transit to different access model. */
991
if (type != 0x8d || (offset + 9) > sec->size)
994
val = bfd_get_8 (abfd, contents + offset - 1);
995
if ((val & 0xf8) != 0x80 || (val & 7) == 4)
999
if (bfd_get_8 (abfd, contents + offset + 4) != 0xe8)
1002
r_symndx = ELF32_R_SYM (rel[1].r_info);
1003
if (r_symndx < symtab_hdr->sh_info)
1006
h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1008
&& h->root.root.string != NULL
1009
&& (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
1010
|| ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32)
1011
&& (strcmp (h->root.root.string, "___tls_get_addr") == 0));
1014
/* Check transition from IE access model:
1015
movl foo@indntpoff(%rip), %eax
1016
movl foo@indntpoff(%rip), %reg
1017
addl foo@indntpoff(%rip), %reg
1020
if (offset < 1 || (offset + 4) > sec->size)
1023
/* Check "movl foo@tpoff(%rip), %eax" first. */
1024
val = bfd_get_8 (abfd, contents + offset - 1);
1031
/* Check movl|addl foo@tpoff(%rip), %reg. */
1032
type = bfd_get_8 (abfd, contents + offset - 2);
1033
return ((type == 0x8b || type == 0x03)
1034
&& (val & 0xc7) == 0x05);
1036
case R_386_TLS_GOTIE:
1037
case R_386_TLS_IE_32:
1038
/* Check transition from {IE_32,GOTIE} access model:
1039
subl foo@{tpoff,gontoff}(%reg1), %reg2
1040
movl foo@{tpoff,gontoff}(%reg1), %reg2
1041
addl foo@{tpoff,gontoff}(%reg1), %reg2
1044
if (offset < 2 || (offset + 4) > sec->size)
1047
val = bfd_get_8 (abfd, contents + offset - 1);
1048
if ((val & 0xc0) != 0x80 || (val & 7) == 4)
1051
type = bfd_get_8 (abfd, contents + offset - 2);
1052
return type == 0x8b || type == 0x2b || type == 0x03;
1054
case R_386_TLS_GOTDESC:
1055
/* Check transition from GDesc access model:
1056
leal x@tlsdesc(%ebx), %eax
1058
Make sure it's a leal adding ebx to a 32-bit offset
1059
into any register, although it's probably almost always
1062
if (offset < 2 || (offset + 4) > sec->size)
1065
if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1068
val = bfd_get_8 (abfd, contents + offset - 1);
1069
return (val & 0xc7) == 0x83;
1071
case R_386_TLS_DESC_CALL:
1072
/* Check transition from GDesc access model:
1073
call *x@tlsdesc(%rax)
1075
if (offset + 2 <= sec->size)
1077
/* Make sure that it's a call *x@tlsdesc(%rax). */
1078
static i386_opcode16 call = { { 0xff, 0x10 } };
1079
return bfd_get_16 (abfd, contents + offset) == call.i;
1089
/* Return TRUE if the TLS access transition is OK or no transition
1090
will be performed. Update R_TYPE if there is a transition. */
1093
elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
1094
asection *sec, bfd_byte *contents,
1095
Elf_Internal_Shdr *symtab_hdr,
1096
struct elf_link_hash_entry **sym_hashes,
1097
unsigned int *r_type, int tls_type,
1098
const Elf_Internal_Rela *rel,
1099
const Elf_Internal_Rela *relend,
1100
struct elf_link_hash_entry *h)
1102
unsigned int from_type = *r_type;
1103
unsigned int to_type = from_type;
1104
bfd_boolean check = TRUE;
1109
case R_386_TLS_GOTDESC:
1110
case R_386_TLS_DESC_CALL:
1111
case R_386_TLS_IE_32:
1113
case R_386_TLS_GOTIE:
1117
to_type = R_386_TLS_LE_32;
1118
else if (from_type != R_386_TLS_IE
1119
&& from_type != R_386_TLS_GOTIE)
1120
to_type = R_386_TLS_IE_32;
1123
/* When we are called from elf_i386_relocate_section, CONTENTS
1124
isn't NULL and there may be additional transitions based on
1126
if (contents != NULL)
1128
unsigned int new_to_type = to_type;
1133
&& (tls_type & GOT_TLS_IE))
1134
new_to_type = R_386_TLS_LE_32;
1136
if (to_type == R_386_TLS_GD
1137
|| to_type == R_386_TLS_GOTDESC
1138
|| to_type == R_386_TLS_DESC_CALL)
1140
if (tls_type == GOT_TLS_IE_POS)
1141
new_to_type = R_386_TLS_GOTIE;
1142
else if (tls_type & GOT_TLS_IE)
1143
new_to_type = R_386_TLS_IE_32;
1146
/* We checked the transition before when we were called from
1147
elf_i386_check_relocs. We only want to check the new
1148
transition which hasn't been checked before. */
1149
check = new_to_type != to_type && from_type == to_type;
1150
to_type = new_to_type;
1157
to_type = R_386_TLS_LE_32;
1164
/* Return TRUE if there is no transition. */
1165
if (from_type == to_type)
1168
/* Check if the transition can be performed. */
1170
&& ! elf_i386_check_tls_transition (abfd, sec, contents,
1171
symtab_hdr, sym_hashes,
1172
from_type, rel, relend))
1174
const reloc_howto_type *from, *to;
1176
from = elf_i386_rtype_to_howto (abfd, from_type);
1177
to = elf_i386_rtype_to_howto (abfd, to_type);
1179
(*_bfd_error_handler)
1180
(_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1181
"in section `%A' failed"),
1182
abfd, sec, from->name, to->name,
1183
h ? h->root.root.string : "a local symbol",
1184
(unsigned long) rel->r_offset);
1185
bfd_set_error (bfd_error_bad_value);
908
1193
/* Look through the relocs for a section during the first phase, and
2658
2902
case R_386_TLS_DESC_CALL:
2659
2903
case R_386_TLS_IE_32:
2660
2904
case R_386_TLS_GOTIE:
2661
r_type = elf_i386_tls_transition (info, r_type, h == NULL);
2662
2905
tls_type = GOT_UNKNOWN;
2663
2906
if (h == NULL && local_got_offsets)
2664
2907
tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
2665
2908
else if (h != NULL)
2667
tls_type = elf_i386_hash_entry(h)->tls_type;
2668
if (!info->shared && h->dynindx == -1 && (tls_type & GOT_TLS_IE))
2669
r_type = R_386_TLS_LE_32;
2909
tls_type = elf_i386_hash_entry(h)->tls_type;
2671
2910
if (tls_type == GOT_TLS_IE)
2672
2911
tls_type = GOT_TLS_IE_NEG;
2673
if (r_type == R_386_TLS_GD
2674
|| r_type == R_386_TLS_GOTDESC
2675
|| r_type == R_386_TLS_DESC_CALL)
2677
if (tls_type == GOT_TLS_IE_POS)
2678
r_type = R_386_TLS_GOTIE;
2679
else if (tls_type & GOT_TLS_IE)
2680
r_type = R_386_TLS_IE_32;
2913
if (! elf_i386_tls_transition (info, input_bfd,
2914
input_section, contents,
2915
symtab_hdr, sym_hashes,
2916
&r_type, tls_type, rel,
2683
2920
if (r_type == R_386_TLS_LE_32)
2685
2922
BFD_ASSERT (! unresolved_reloc);
2686
2923
if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
2688
unsigned int val, type;
2691
2928
/* GD->LE transition. */
2692
BFD_ASSERT (rel->r_offset >= 2);
2693
2929
type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2694
BFD_ASSERT (type == 0x8d || type == 0x04);
2695
BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
2696
BFD_ASSERT (bfd_get_8 (input_bfd,
2697
contents + rel->r_offset + 4)
2699
BFD_ASSERT (rel + 1 < relend);
2700
BFD_ASSERT (ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
2701
roff = rel->r_offset + 5;
2702
val = bfd_get_8 (input_bfd,
2703
contents + rel->r_offset - 1);
2704
2930
if (type == 0x04)
2706
2932
/* leal foo(,%reg,1), %eax; call ___tls_get_addr
2707
2933
Change it into:
2708
2934
movl %gs:0, %eax; subl $foo@tpoff, %eax
2709
2935
(6 byte form of subl). */
2710
BFD_ASSERT (rel->r_offset >= 3);
2711
BFD_ASSERT (bfd_get_8 (input_bfd,
2712
contents + rel->r_offset - 3)
2714
BFD_ASSERT ((val & 0xc7) == 0x05 && val != (4 << 3));
2715
2936
memcpy (contents + rel->r_offset - 3,
2716
2937
"\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2938
roff = rel->r_offset + 5;
2720
BFD_ASSERT ((val & 0xf8) == 0x80 && (val & 7) != 4);
2721
if (rel->r_offset + 10 <= input_section->size
2722
&& bfd_get_8 (input_bfd,
2723
contents + rel->r_offset + 9) == 0x90)
2725
/* leal foo(%reg), %eax; call ___tls_get_addr; nop
2727
movl %gs:0, %eax; subl $foo@tpoff, %eax
2728
(6 byte form of subl). */
2729
memcpy (contents + rel->r_offset - 2,
2730
"\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2731
roff = rel->r_offset + 6;
2735
/* leal foo(%reg), %eax; call ___tls_get_addr
2737
movl %gs:0, %eax; subl $foo@tpoff, %eax
2738
(5 byte form of subl). */
2739
memcpy (contents + rel->r_offset - 2,
2740
"\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2942
/* leal foo(%reg), %eax; call ___tls_get_addr; nop
2944
movl %gs:0, %eax; subl $foo@tpoff, %eax
2945
(6 byte form of subl). */
2946
memcpy (contents + rel->r_offset - 2,
2947
"\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2948
roff = rel->r_offset + 6;
2743
2950
bfd_put_32 (output_bfd, tpoff (info, relocation),
2744
2951
contents + roff);
2745
/* Skip R_386_PLT32. */
2952
/* Skip R_386_PC32/R_386_PLT32. */