-
-
Notifications
You must be signed in to change notification settings - Fork 463
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle LLVM-generated TLSDESC code sequence
It had been assumed that the TLSDESC code seqeunce looks like this: lea 0(%rip), %rax R_X86_64_GOTPC32_TLSDESC foo call *(%rax) R_X86_64_TLSDESC_CALL foo However, LLVM seems to also emit something like this: lea 0(%rip), %reg R_X86_64_GOTPC32_TLSDESC foo ... mov %reg, %rax ... call *(%rax) R_X86_64_TLSDESC_CALL foo That means when we rewrite the LEA instruction to relax the code sequence, we need to handle destination registers other than %rax. The wrong assumption caused a program crash as reported as https://bugs.gentoo.org/914849.
- Loading branch information
Showing
5 changed files
with
207 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/bin/bash | ||
. $(dirname $0)/common.inc | ||
|
||
supports_tlsdesc || skip | ||
|
||
cat <<'EOF' | $GCC -c -o $t/a.o -xassembler - | ||
.globl get_foo | ||
.type get_foo, @function | ||
get_foo: | ||
pushl %ebx | ||
call __x86.get_pc_thunk.bx | ||
addl $_GLOBAL_OFFSET_TABLE_, %ebx | ||
subl $8, %esp | ||
leal foo@TLSDESC(%ebx), %ebx | ||
movl %ebx, %eax | ||
call *foo@TLSCALL(%eax) | ||
movl %gs:(%eax), %eax | ||
addl $8, %esp | ||
popl %ebx | ||
ret | ||
EOF | ||
|
||
cat <<EOF | $GCC -fPIC -c -o $t/b.o -xc - $tlsdesc_opt | ||
#include <stdio.h> | ||
_Thread_local int foo; | ||
int get_foo(); | ||
int main() { | ||
foo = 42; | ||
printf("%d\n", get_foo()); | ||
} | ||
EOF | ||
|
||
$CC -B. -o $t/exe1 $t/a.o $t/b.o | ||
$QEMU $t/exe1 | grep -q 42 | ||
|
||
$CC -B. -o $t/exe2 $t/a.o $t/b.o -Wl,-no-relax | ||
$QEMU $t/exe2 | grep -q 42 | ||
|
||
$CC -B. -shared -o $t/c.so $t/a.o | ||
$CC -B. -o $t/exe3 $t/b.o $t/c.so | ||
$QEMU $t/exe3 | grep -q 42 | ||
|
||
$CC -B. -shared -o $t/c.so $t/a.o -Wl,-no-relax | ||
$CC -B. -o $t/exe4 $t/b.o $t/c.so -Wl,-no-relax | ||
$QEMU $t/exe4 | grep -q 42 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/bin/bash | ||
. $(dirname $0)/common.inc | ||
|
||
supports_tlsdesc || skip | ||
|
||
cat <<EOF | $GCC -c -o $t/a.o -xassembler - | ||
.globl get_foo | ||
.type get_foo, @function | ||
get_foo: | ||
pushq %rbp | ||
movq %rsp, %rbp | ||
leaq foo@TLSDESC(%rip), %rbx | ||
movq %rbx, %rax | ||
call *foo@TLSCALL(%rax) | ||
movq %fs:0, %rdx | ||
addq %rdx, %rax | ||
movl (%rax), %eax | ||
popq %rbp | ||
ret | ||
EOF | ||
|
||
cat <<EOF | $GCC -fPIC -c -o $t/b.o -xc - $tlsdesc_opt | ||
#include <stdio.h> | ||
_Thread_local int foo; | ||
int get_foo(); | ||
int main() { | ||
foo = 42; | ||
printf("%d\n", get_foo()); | ||
} | ||
EOF | ||
|
||
$CC -B. -o $t/exe1 $t/a.o $t/b.o | ||
$QEMU $t/exe1 | grep -q 42 | ||
|
||
$CC -B. -o $t/exe2 $t/a.o $t/b.o -Wl,-no-relax | ||
$QEMU $t/exe2 | grep -q 42 | ||
|
||
$CC -B. -shared -o $t/c.so $t/a.o | ||
$CC -B. -o $t/exe3 $t/b.o $t/c.so | ||
$QEMU $t/exe3 | grep -q 42 | ||
|
||
$CC -B. -shared -o $t/c.so $t/a.o -Wl,-no-relax | ||
$CC -B. -o $t/exe4 $t/b.o $t/c.so -Wl,-no-relax | ||
$QEMU $t/exe4 | grep -q 42 |