Skip to content

Commit

Permalink
[lld][WebAssembly] Fix for TLS + --relocatable
Browse files Browse the repository at this point in the history
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: emscripten-core/emscripten#12934
Fixes: PR48506

Differential Revision: https://reviews.llvm.org/D93554
  • Loading branch information
sbc100 committed Jan 13, 2021
1 parent 4e8e888 commit 07b6aeb
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lld/test/wasm/tls-no-shared.s
Original file line number Diff line number Diff line change
@@ -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

Expand Down
33 changes: 31 additions & 2 deletions lld/test/wasm/tls.s
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion lld/wasm/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions lld/wasm/InputFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lld/wasm/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 07b6aeb

Please sign in to comment.