[lld][WebAssembly] Perform data relocations during start function

We already perform memory initialization and apply global relocations
during start.  It makes sense to performs data relocations too.  I think
the reason we were not doing this already is solely historical.

Differential Revision: https://reviews.llvm.org/D117412
This commit is contained in:
Sam Clegg 2022-01-15 15:33:02 -08:00
parent 64de0064f3
commit ec47dba1c8
3 changed files with 60 additions and 16 deletions

View file

@ -115,7 +115,7 @@
; PASSIVE-PIC-NEXT: Functions:
; PASSIVE-PIC-NEXT: - Index: 0
; PASSIVE-PIC-NEXT: Locals: []
; PASSIVE-PIC-NEXT: Body: 10030B
; PASSIVE-PIC-NEXT: Body: 0B
; PASSIVE-PIC-NEXT: - Index: 1
; PASSIVE-PIC-NEXT: Locals: []
; PASSIVE-PIC-NEXT: Body: 0B
@ -195,6 +195,8 @@
; DIS-NEXT: i32.const 10000
; DIS-NEXT: memory.fill 0
; PIC-DIS-NEXT: call 3
; NOPIC-DIS-NEXT: [[PTR]].const 11060
; PIC-DIS-NEXT: local.get 0

View file

@ -1,6 +1,7 @@
; RUN: llc -relocation-model=pic -mattr=+mutable-globals -filetype=obj %s -o %t.o
; RUN: wasm-ld --no-gc-sections --experimental-pic -pie -o %t.wasm %t.o
; RUN: obj2yaml %t.wasm | FileCheck %s
; RUN: llvm-objdump --disassemble-symbols=__wasm_start --no-show-raw-insn --no-leading-addr %t.wasm | FileCheck %s --check-prefixes DISASSEM
target triple = "wasm32-unknown-emscripten"
@ -69,7 +70,7 @@ declare void @external_func()
; CHECK-NEXT: GlobalMutable: false
; CHECK: - Type: START
; CHECK-NEXT: StartFunction: 3
; CHECK-NEXT: StartFunction: 4
; CHECK: - Type: CUSTOM
; CHECK-NEXT: Name: name
@ -82,7 +83,14 @@ declare void @external_func()
; CHECK-NEXT: Name: __wasm_apply_data_relocs
; CHECK-NEXT: - Index: 3
; CHECK-NEXT: Name: __wasm_apply_global_relocs
; CHECK-NEXT: - Index: 4
; CHECK-NEXT: Name: __wasm_start
; DISASSEM: <__wasm_start>:
; DISASSEM-EMPTY:
; DISASSEM-NEXT: call 2
; DISASSEM-NEXT: call 3
; DISASSEM-NEXT: end
; Run the same test with threading support. In this mode
; we expect __wasm_init_memory and __wasm_apply_data_relocs
@ -92,11 +100,16 @@ declare void @external_func()
; RUN: llc -relocation-model=pic -mattr=+mutable-globals,+atomics,+bulk-memory -filetype=obj %s -o %t.shmem.o
; RUN: wasm-ld --no-gc-sections --shared-memory --allow-undefined --experimental-pic -pie -o %t.shmem.wasm %t.shmem.o
; RUN: obj2yaml %t.shmem.wasm | FileCheck %s --check-prefix=SHMEM
; RUN: llvm-objdump --disassemble-symbols=__wasm_start --no-show-raw-insn --no-leading-addr %t.shmem.wasm | FileCheck %s --check-prefixes DISASSEM-SHMEM
; SHMEM: - Type: CODE
; SHMEM: - Index: 6
; SHMEM-NEXT: Locals: []
; SHMEM-NEXT: Body: 100310050B
; SHMEM: - Type: START
; SHMEM-NEXT: StartFunction: 6
; DISASSEM-SHMEM: <__wasm_start>:
; DISASSEM-SHMEM-EMPTY:
; DISASSEM-SHMEM-NEXT: call 3
; DISASSEM-SHMEM-NEXT: call 5
; DISASSEM-SHMEM-NEXT: end
; SHMEM: FunctionNames:
; SHMEM-NEXT: - Index: 0

View file

@ -1022,7 +1022,16 @@ void Writer::createSyntheticInitFunctions() {
}
}
if (WasmSym::applyGlobalRelocs && WasmSym::initMemory) {
int startCount = 0;
if (WasmSym::applyGlobalRelocs)
startCount++;
if (WasmSym::WasmSym::initMemory || WasmSym::applyDataRelocs)
startCount++;
// If there is only one start function we can just use that function
// itself as the Wasm start function, otherwise we need to synthesize
// a new function to call them in sequence.
if (startCount > 1) {
WasmSym::startFunction = symtab->addSyntheticFunction(
"__wasm_start", WASM_SYMBOL_VISIBILITY_HIDDEN,
make<SyntheticFunction>(nullSignature, "__wasm_start"));
@ -1179,6 +1188,14 @@ void Writer::createInitMemoryFunction() {
}
}
// Memory init is now complete. Apply data relocation if there
// are any.
if (WasmSym::applyDataRelocs) {
writeU8(os, WASM_OPCODE_CALL, "CALL");
writeUleb128(os, WasmSym::applyDataRelocs->getFunctionIndex(),
"function index");
}
if (config->sharedMemory) {
// Set flag to 2 to mark end of initialization
writeGetFlagAddress();
@ -1231,17 +1248,28 @@ void Writer::createInitMemoryFunction() {
}
void Writer::createStartFunction() {
// If the start function exists when we have more than one function to call.
if (WasmSym::startFunction) {
std::string bodyContent;
{
raw_string_ostream os(bodyContent);
writeUleb128(os, 0, "num locals");
writeU8(os, WASM_OPCODE_CALL, "CALL");
writeUleb128(os, WasmSym::initMemory->getFunctionIndex(),
"function index");
writeU8(os, WASM_OPCODE_CALL, "CALL");
writeUleb128(os, WasmSym::applyGlobalRelocs->getFunctionIndex(),
"function index");
if (WasmSym::initMemory) {
writeU8(os, WASM_OPCODE_CALL, "CALL");
writeUleb128(os, WasmSym::initMemory->getFunctionIndex(),
"function index");
} else if (WasmSym::applyDataRelocs) {
// When initMemory is present it calls applyDataRelocs. If not,
// we must call it directly.
writeU8(os, WASM_OPCODE_CALL, "CALL");
writeUleb128(os, WasmSym::applyDataRelocs->getFunctionIndex(),
"function index");
}
if (WasmSym::applyGlobalRelocs) {
writeU8(os, WASM_OPCODE_CALL, "CALL");
writeUleb128(os, WasmSym::applyGlobalRelocs->getFunctionIndex(),
"function index");
}
writeU8(os, WASM_OPCODE_END, "END");
}
createFunction(WasmSym::startFunction, bodyContent);
@ -1249,6 +1277,8 @@ void Writer::createStartFunction() {
WasmSym::startFunction = WasmSym::initMemory;
} else if (WasmSym::applyGlobalRelocs) {
WasmSym::startFunction = WasmSym::applyGlobalRelocs;
} else if (WasmSym::applyDataRelocs) {
WasmSym::startFunction = WasmSym::applyDataRelocs;
}
}
@ -1311,8 +1341,7 @@ void Writer::createCallCtorsFunction() {
// If __wasm_call_ctors isn't referenced, there aren't any ctors, and we
// aren't calling `__wasm_apply_data_relocs` for Emscripten-style PIC, don't
// define the `__wasm_call_ctors` function.
if (!WasmSym::callCtors->isLive() && !WasmSym::applyDataRelocs &&
initFunctions.empty())
if (!WasmSym::callCtors->isLive() && initFunctions.empty())
return;
// First write the body's contents to a string.
@ -1321,7 +1350,7 @@ void Writer::createCallCtorsFunction() {
raw_string_ostream os(bodyContent);
writeUleb128(os, 0, "num locals");
if (WasmSym::applyDataRelocs) {
if (WasmSym::applyDataRelocs && !WasmSym::initMemory) {
writeU8(os, WASM_OPCODE_CALL, "CALL");
writeUleb128(os, WasmSym::applyDataRelocs->getFunctionIndex(),
"function index");