[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
This commit is contained in:
Sam Clegg 2020-12-18 10:02:29 -08:00
parent 4e8e888905
commit 07b6aeb568
5 changed files with 35 additions and 5 deletions

View File

@ -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

View File

@ -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

View File

@ -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);

View File

@ -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;

View File

@ -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);