Skip to content

Commit

Permalink
[ELF] Change "no PT_TLS" error to use errorOrWarn
Browse files Browse the repository at this point in the history
so that --noinhibit-exec downgrades the error to a warning, which helps
debugging when `PHDRS` is specified without `PT_TLS`. Also update the
message to make it accurate: STT_TLS may exist in the absence of PT_TLS.

In addition, invoking `exitLld(1)` (through `fatal`) is problematic
(llvm#66974): When a thread is `exitLld(1)`, triggering `llvm_shutdown`,
another thread may be at `relocateAlloc`, accessing `sec.relocs()` which
got destroyed(tampered?), leading to
incorrect `llvm_unreachable("invalid expression")`.
  • Loading branch information
MaskRay committed Aug 12, 2024
1 parent e26b42c commit b6448a0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lld/ELF/InputSection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,8 @@ static int64_t getTlsTpOffset(const Symbol &s) {
// before TP. The alignment padding is added so that (TP - padding -
// p_memsz) is congruent to p_vaddr modulo p_align.
PhdrEntry *tls = ctx.tlsPhdr;
if (!tls) // Reported an error in getSymVA
return 0;
switch (config->emachine) {
// Variant 1.
case EM_ARM:
Expand Down
8 changes: 5 additions & 3 deletions lld/ELF/Symbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,11 @@ static uint64_t getSymVA(const Symbol &sym, int64_t addend) {
// after sections are finalized. (e.g. Measuring the size of .rela.dyn
// for Android relocation packing requires knowing TLS symbol addresses
// during section finalization.)
if (!ctx.tlsPhdr || !ctx.tlsPhdr->firstSec)
fatal(toString(d.file) +
" has an STT_TLS symbol but doesn't have an SHF_TLS section");
if (!ctx.tlsPhdr || !ctx.tlsPhdr->firstSec) {
errorOrWarn(toString(d.file) +
" has an STT_TLS symbol but doesn't have a PT_TLS segment");
return 0;
}
return va - ctx.tlsPhdr->firstSec->addr;
}
return va;
Expand Down
2 changes: 1 addition & 1 deletion lld/test/ELF/invalid/tls-symbol.s
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# RUN: yaml2obj %s -o %t.o
# RUN: not ld.lld %t.o -o /dev/null 2>&1 | FileCheck %s

# CHECK: has an STT_TLS symbol but doesn't have an SHF_TLS section
# CHECK: has an STT_TLS symbol but doesn't have a PT_TLS segment

--- !ELF
FileHeader:
Expand Down
6 changes: 4 additions & 2 deletions lld/test/ELF/linkerscript/phdrs-no-tls.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
# REQUIRES: x86
# RUN: rm -rf %t && split-file %s %t && cd %t
# RUN: llvm-mc -filetype=obj -triple=x86_64 a.s -o a.o
# RUN: not ld.lld -T a.lds a.o --noinhibit-exec 2>&1 | FileCheck %s --implicit-check-not=warning:
# RUN: not ld.lld -T a.lds a.o
# RUN: ld.lld -T a.lds a.o --noinhibit-exec 2>&1 | FileCheck %s --implicit-check-not=warning:

# CHECK: error: a.o has an STT_TLS symbol but doesn't have an SHF_TLS section
# CHECK: warning: a.o has an STT_TLS symbol but doesn't have a PT_TLS segment
# CHECK-NEXT: warning: a.o has an STT_TLS symbol but doesn't have a PT_TLS segment

#--- a.lds
PHDRS {
Expand Down

0 comments on commit b6448a0

Please sign in to comment.