; 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"
; CHECK-NEXT: GlobalMutable: false
; CHECK: - Type: START
-; CHECK-NEXT: StartFunction: 3
+; CHECK-NEXT: StartFunction: 4
; CHECK: - Type: CUSTOM
; CHECK-NEXT: Name: name
; 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
; 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
}
}
- 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"));
}
}
+ // 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();
}
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);
WasmSym::startFunction = WasmSym::initMemory;
} else if (WasmSym::applyGlobalRelocs) {
WasmSym::startFunction = WasmSym::applyGlobalRelocs;
+ } else if (WasmSym::applyDataRelocs) {
+ WasmSym::startFunction = WasmSym::applyDataRelocs;
}
}
// 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.
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");