Skip to content

Commit

Permalink
Fix TLSDESC relocation
Browse files Browse the repository at this point in the history
Although we do not support TLSDESC on any target on which
ctx.tls_begin != ctx.dtp_addr, my understanding is that the origin
of the TLSDESC base relocation is tls_begin.
  • Loading branch information
rui314 committed Aug 14, 2023
1 parent f5abeda commit 4f5f882
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion elf/output-chunks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,7 @@ static std::vector<GotEntry<E>> get_got_entries(Context<E> &ctx) {
if (sym->is_imported)
add({idx, 0, E::R_TLSDESC, sym});
else
add({idx, sym->get_addr(ctx) - ctx.dtp_addr, E::R_TLSDESC});
add({idx, sym->get_addr(ctx) - ctx.tls_begin, E::R_TLSDESC});
}
}

Expand Down
20 changes: 11 additions & 9 deletions test/elf/tlsdesc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,45 @@ fi

cat <<EOF | $GCC -fPIC -mtls-dialect=$dialect -c -o $t/a.o -xc -
extern _Thread_local int foo;
_Thread_local int bar = 3;
int get_foo() {
return foo;
}
static _Thread_local int bar = 5;
static _Thread_local int baz = 5;
int get_bar() {
return bar;
int get_baz() {
return baz;
}
EOF

cat <<EOF | $GCC -fPIC -mtls-dialect=$dialect -c -o $t/b.o -xc -
#include <stdio.h>
_Thread_local int foo;
extern _Thread_local int bar;
int get_foo();
int get_bar();
int get_baz();
int main() {
foo = 42;
printf("%d %d\n", get_foo(), get_bar());
printf("%d %d %d\n", get_foo(), bar, get_baz());
return 0;
}
EOF

$CC -B. -o $t/exe1 $t/a.o $t/b.o
$QEMU $t/exe1 | grep -q '42 5'
$QEMU $t/exe1 | grep -q '42 3 5'

$CC -B. -o $t/exe2 $t/a.o $t/b.o -Wl,-no-relax
$QEMU $t/exe2 | grep -q '42 5'
$QEMU $t/exe2 | grep -q '42 3 5'

$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 5'
$QEMU $t/exe3 | grep -q '42 3 5'

$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 5'
$QEMU $t/exe4 | grep -q '42 3 5'

0 comments on commit 4f5f882

Please sign in to comment.