[llvm-branch-commits] [lld] 07b6aeb - [lld][WebAssembly] Fix for TLS + --relocatable

Sam Clegg via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Jan 13 07:55:22 PST 2021


Author: Sam Clegg
Date: 2021-01-13T07:49:39-08:00
New Revision: 07b6aeb5685fa6474cbeae797ebc5ea1f17da0f7

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

LOG: [lld][WebAssembly] Fix for TLS + --relocatable

When running in `-r/--relocatable` we output relocations but the
new TLS relocations type was missing from `ObjFile::calcNewAddend`
causing this combination of inputs/flags to crash the linker.

Also avoid creating tls variables in relocatable mode.  These variables
are only needed when linking final executables.

Fixes: https://github.com/emscripten-core/emscripten/issues/12934
Fixes: PR48506

Differential Revision: https://reviews.llvm.org/D93554

Added: 
    

Modified: 
    lld/test/wasm/tls-no-shared.s
    lld/test/wasm/tls.s
    lld/wasm/Driver.cpp
    lld/wasm/InputFiles.cpp
    lld/wasm/Writer.cpp

Removed: 
    


################################################################################
diff  --git a/lld/test/wasm/tls-no-shared.s b/lld/test/wasm/tls-no-shared.s
index 3fdc7057ad07..6c17489b7d42 100644
--- a/lld/test/wasm/tls-no-shared.s
+++ b/lld/test/wasm/tls-no-shared.s
@@ -1,5 +1,5 @@
 # Test that linking without shared memory causes __tls_base to be
-# interlized
+# internalized
 
 # RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.o %s
 

diff  --git a/lld/test/wasm/tls.s b/lld/test/wasm/tls.s
index f5072dc13afa..878498bc2e48 100644
--- a/lld/test/wasm/tls.s
+++ b/lld/test/wasm/tls.s
@@ -73,8 +73,8 @@ tls3:
 # RUN: wasm-ld -no-gc-sections --shared-memory --max-memory=131072 --no-entry -o %t.wasm %t.o
 # RUN: obj2yaml %t.wasm | FileCheck %s
 
-# RUN: wasm-ld -no-gc-sections --shared-memory --max-memory=131072 --no-merge-data-segments --no-entry -o %t.wasm %t.o
-# RUN: obj2yaml %t.wasm | FileCheck %s
+# RUN: wasm-ld -no-gc-sections --shared-memory --max-memory=131072 --no-merge-data-segments --no-entry -o %t2.wasm %t.o
+# RUN: obj2yaml %t2.wasm | FileCheck %s
 
 # CHECK:      - Type:            GLOBAL
 # CHECK-NEXT:   Globals:
@@ -163,3 +163,32 @@ tls3:
 # Expected body of tls_align:
 #   global.get 3
 #   end
+
+
+# Also verify TLS usage with --relocatable
+# RUN: wasm-ld --relocatable -o %t3.wasm %t.o
+# RUN: obj2yaml %t3.wasm | FileCheck %s --check-prefix=RELOC
+
+# RELOC:       - Type:            IMPORT
+# RELOC-NEXT:    Imports:
+# RELOC-NEXT:      - Module:          env
+# RELOC-NEXT:        Field:           __tls_base
+# RELOC-NEXT:        Kind:            GLOBAL
+# RELOC-NEXT:        GlobalType:      I32
+# RELOC-NEXT:        GlobalMutable:   true
+# RELOC-NEXT:      - Module:          env
+# RELOC-NEXT:        Field:           __tls_align
+# RELOC-NEXT:        Kind:            GLOBAL
+# RELOC-NEXT:        GlobalType:      I32
+# RELOC-NEXT:        GlobalMutable:   false
+
+# RELOC:         GlobalNames:
+# RELOC-NEXT:      - Index:           0
+# RELOC-NEXT:        Name:            __tls_base
+# RELOC-NEXT:      - Index:           1
+# RELOC-NEXT:        Name:            __tls_align
+# RELOC-NEXT:    DataSegmentNames:
+# RELOC-NEXT:      - Index:           0
+# RELOC-NEXT:        Name:            .tdata
+# RELOC-NEXT:      - Index:           1
+# RELOC-NEXT:        Name:            .bss.no_tls

diff  --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp
index fb699c55fc8c..84fdb77aea2c 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -639,7 +639,7 @@ static void createSyntheticSymbols() {
     WasmSym::stackPointer->markLive();
   }
 
-  if (config->sharedMemory) {
+  if (config->sharedMemory && !config->relocatable) {
     WasmSym::tlsBase = createGlobalVariable("__tls_base", true);
     WasmSym::tlsSize = createGlobalVariable("__tls_size", false);
     WasmSym::tlsAlign = createGlobalVariable("__tls_align", false);

diff  --git a/lld/wasm/InputFiles.cpp b/lld/wasm/InputFiles.cpp
index 68a9472819e4..eb37ae548b80 100644
--- a/lld/wasm/InputFiles.cpp
+++ b/lld/wasm/InputFiles.cpp
@@ -123,6 +123,7 @@ uint64_t ObjFile::calcNewAddend(const WasmRelocation &reloc) const {
   case R_WASM_MEMORY_ADDR_REL_SLEB64:
   case R_WASM_MEMORY_ADDR_I32:
   case R_WASM_MEMORY_ADDR_I64:
+  case R_WASM_MEMORY_ADDR_TLS_SLEB:
   case R_WASM_FUNCTION_OFFSET_I32:
   case R_WASM_FUNCTION_OFFSET_I64:
     return reloc.Addend;

diff  --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp
index c95b92504634..710404943df2 100644
--- a/lld/wasm/Writer.cpp
+++ b/lld/wasm/Writer.cpp
@@ -284,7 +284,7 @@ void Writer::layoutMemory() {
     log(formatv("mem: {0,-15} offset={1,-8} size={2,-8} align={3}", seg->name,
                 memoryPtr, seg->size, seg->alignment));
 
-    if (seg->name == ".tdata") {
+    if (!config->relocatable && seg->name == ".tdata") {
       if (config->sharedMemory) {
         auto *tlsSize = cast<DefinedGlobal>(WasmSym::tlsSize);
         setGlobalPtr(tlsSize, seg->size);


        


More information about the llvm-branch-commits mailing list