[lld] b6448a0 - [ELF] Change "no PT_TLS" error to use errorOrWarn

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 12 11:56:36 PDT 2024


Author: Fangrui Song
Date: 2024-08-12T11:56:29-07:00
New Revision: b6448a03d887a1615fdffd1016109f9f24bea275

URL: https://github.com/llvm/llvm-project/commit/b6448a03d887a1615fdffd1016109f9f24bea275
DIFF: https://github.com/llvm/llvm-project/commit/b6448a03d887a1615fdffd1016109f9f24bea275.diff

LOG: [ELF] Change "no PT_TLS" error to use errorOrWarn

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
(#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")`.

Added: 
    

Modified: 
    lld/ELF/InputSection.cpp
    lld/ELF/Symbols.cpp
    lld/test/ELF/invalid/tls-symbol.s
    lld/test/ELF/linkerscript/phdrs-no-tls.test

Removed: 
    


################################################################################
diff  --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index cf34a814fed415..a7bb9bd47299e2 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -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:

diff  --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp
index 4e1ae819b86fad..13fc6dc0dd572a 100644
--- a/lld/ELF/Symbols.cpp
+++ b/lld/ELF/Symbols.cpp
@@ -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;

diff  --git a/lld/test/ELF/invalid/tls-symbol.s b/lld/test/ELF/invalid/tls-symbol.s
index 3f371d406e1f2f..1af1e783d1f5b4 100644
--- a/lld/test/ELF/invalid/tls-symbol.s
+++ b/lld/test/ELF/invalid/tls-symbol.s
@@ -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:      

diff  --git a/lld/test/ELF/linkerscript/phdrs-no-tls.test b/lld/test/ELF/linkerscript/phdrs-no-tls.test
index 1d5d726b6c9e10..0d374beaf8eee8 100644
--- a/lld/test/ELF/linkerscript/phdrs-no-tls.test
+++ b/lld/test/ELF/linkerscript/phdrs-no-tls.test
@@ -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 {


        


More information about the llvm-commits mailing list