diff --git a/lld/test/wasm/data-segments.ll b/lld/test/wasm/data-segments.ll --- a/lld/test/wasm/data-segments.ll +++ b/lld/test/wasm/data-segments.ll @@ -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 diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp --- a/lld/wasm/Writer.cpp +++ b/lld/wasm/Writer.cpp @@ -1022,7 +1022,16 @@ } } - 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(nullSignature, "__wasm_start")); @@ -1179,6 +1188,14 @@ } } + // 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,30 @@ } 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"); + } + if (WasmSym::applyGlobalRelocs) { + writeU8(os, WASM_OPCODE_CALL, "CALL"); + writeUleb128(os, WasmSym::applyGlobalRelocs->getFunctionIndex(), + "function index"); + } + if (!WasmSym::initMemory) { + // When initMemory is present it calls applyDataRelocs + if (WasmSym::applyDataRelocs) { + writeU8(os, WASM_OPCODE_CALL, "CALL"); + writeUleb128(os, WasmSym::applyDataRelocs->getFunctionIndex(), + "function index"); + } + } writeU8(os, WASM_OPCODE_END, "END"); } createFunction(WasmSym::startFunction, bodyContent); @@ -1249,6 +1279,8 @@ WasmSym::startFunction = WasmSym::initMemory; } else if (WasmSym::applyGlobalRelocs) { WasmSym::startFunction = WasmSym::applyGlobalRelocs; + } else if (WasmSym::applyDataRelocs) { + WasmSym::startFunction = WasmSym::applyDataRelocs; } } @@ -1311,8 +1343,7 @@ // 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 +1352,7 @@ 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");