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 @@ -108,7 +108,7 @@ ; PASSIVE-NEXT: Name: __wasm_init_memory ; PASSIVE-PIC: - Type: START -; PASSIVE-PIC-NEXT: StartFunction: 2 +; PASSIVE-PIC-NEXT: StartFunction: 4 ; PASSIVE-PIC-NEXT: - Type: DATACOUNT ; PASSIVE-PIC-NEXT: Count: 2 ; PASSIVE-PIC-NEXT: - Type: CODE @@ -128,6 +128,9 @@ ; PASSIVE-PIC-NEXT: - Index: 3 ; PASSIVE-PIC-NEXT: Locals: [] ; PASSIVE-PIC-NEXT: Body: 0B +; PASSIVE-PIC-NEXT: - Index: 4 +; PASSIVE-PIC-NEXT: Locals: [] +; PASSIVE-PIC-NEXT: Body: 100210030B ; PASSIVE-PIC-NEXT: - Type: DATA ; PASSIVE-PIC-NEXT: Segments: ; PASSIVE-PIC-NEXT: - SectionOffset: 3 diff --git a/lld/test/wasm/pie.ll b/lld/test/wasm/pie.ll --- a/lld/test/wasm/pie.ll +++ b/lld/test/wasm/pie.ll @@ -93,7 +93,7 @@ ; SHMEM: - Type: CODE ; SHMEM: - Index: 6 ; SHMEM-NEXT: Locals: [] -; SHMEM-NEXT: Body: 100310050B +; SHMEM-NEXT: Body: 1003100510040B ; SHMEM: FunctionNames: ; SHMEM-NEXT: - Index: 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,18 @@ } } - if (WasmSym::applyGlobalRelocs && WasmSym::initMemory) { + int startCount = 0; + if (WasmSym::applyGlobalRelocs) + startCount++; + if (WasmSym::WasmSym::initMemory) + startCount++; + if (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")); @@ -1231,17 +1242,27 @@ } 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::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 +1270,8 @@ WasmSym::startFunction = WasmSym::initMemory; } else if (WasmSym::applyGlobalRelocs) { WasmSym::startFunction = WasmSym::applyGlobalRelocs; + } else if (WasmSym::applyDataRelocs) { + WasmSym::startFunction = WasmSym::applyDataRelocs; } } @@ -1311,8 +1334,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.