Skip to content

Commit 01d2a01

Browse files
committed
[ELF] Fix a null pointer dereference when relocating a Local-Exec TLS relocation for a lazy symbol
If there is no SHF_TLS section, there will be no PT_TLS and Out::tlsPhdr may be a nullptr. If the symbol referenced by an R_TLS is lazy, we should treat the symbol as undefined. Also reorganize tls-in-archive.s and tls-weak-undef.s . They do not test what they intended to test.
1 parent c860262 commit 01d2a01

File tree

6 files changed

+20
-37
lines changed

6 files changed

+20
-37
lines changed

lld/ELF/InputSection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ uint64_t InputSectionBase::getRelocTargetVA(const InputFile *file, RelType type,
807807
// --noinhibit-exec, even a non-weak undefined reference may reach here.
808808
// Just return A, which matches R_ABS, and the behavior of some dynamic
809809
// loaders.
810-
if (sym.isUndefined())
810+
if (sym.isUndefined() || sym.isLazy())
811811
return a;
812812
return getTlsTpOffset(sym) + a;
813813
case R_RELAX_TLS_GD_TO_LE_NEG:

lld/test/ELF/Inputs/tls-in-archive.s

Lines changed: 0 additions & 3 deletions
This file was deleted.

lld/test/ELF/tls-in-archive.s

Lines changed: 0 additions & 12 deletions
This file was deleted.

lld/test/ELF/tls-le-weak-undef.s

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# REQUIRES: x86
2+
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
3+
# RUN: echo '.tbss; .globl tls; tls:' | llvm-mc -filetype=obj -triple=x86_64 - -o %tdef.o
4+
# RUN: ld.lld %t.o -o - | llvm-objdump -d - | FileCheck %s
5+
6+
## A weak symbol does not fetch a lazy definition.
7+
# RUN: ld.lld %t.o --start-lib %tdef.o --end-lib -o - | llvm-objdump -d - | FileCheck %s
8+
9+
## Undefined TLS symbols arbitrarily resolve to 0.
10+
# CHECK: leaq 16(%rax), %rdx
11+
12+
# RUN: ld.lld -shared %tdef.o -o %tdef.so
13+
# RUN: not ld.lld %t.o %tdef.so -o /dev/null 2>&1 | FileCheck --check-prefix=COPYRELOC %s
14+
15+
# COPYRELOC: symbol 'tls' has no type
16+
17+
.weak tls
18+
leaq tls@tpoff+16(%rax), %rdx

lld/test/ELF/tls-weak-undef.s

Lines changed: 0 additions & 17 deletions
This file was deleted.

lld/test/ELF/x86-64-tls-le-undef.s

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
## Undefined TLS symbols resolve to 0.
77
## In --noinhibit-exec mode, a non-weak undefined symbol is not an error.
88

9-
# CHECK: leaq 16(%rax), %rdx
10-
# CHECK-NEXT: leaq 32(%rax), %rdx
9+
# CHECK: leaq 32(%rax), %rdx
1110

12-
.weak weak
1311
movq %fs:0, %rax
14-
leaq weak@tpoff+16(%rax), %rdx
1512
leaq global@tpoff+32(%rax), %rdx

0 commit comments

Comments
 (0)