From 4273998cf9013ce92c7bf1effafef76a65e31d0c Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Thu, 14 Dec 2017 21:10:03 +0000 Subject: [WebAssembly] Add support for init functions linking metadata Summary: This change lays the groundwork lowering of @llvm.global_ctors and @llvm.global_dtors for the wasm object format. Some parts of this patch are subset of: https://reviews.llvm.org/D40759 See https://github.com/WebAssembly/tool-conventions/issues/25 Subscribers: jfb, dschuff, jgravelle-google, aheejin, sunfish Differential Revision: https://reviews.llvm.org/D41208 llvm-svn: 320742 --- llvm/lib/Object/WasmObjectFile.cpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'llvm/lib/Object/WasmObjectFile.cpp') diff --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp index 8d4c15a..677fccc 100644 --- a/llvm/lib/Object/WasmObjectFile.cpp +++ b/llvm/lib/Object/WasmObjectFile.cpp @@ -398,6 +398,21 @@ Error WasmObjectFile::parseLinkingSection(const uint8_t *Ptr, } break; } + case wasm::WASM_INIT_FUNCS: { + uint32_t Count = readVaruint32(Ptr); + LinkingData.InitFunctions.reserve(Count); + for (uint32_t i = 0; i < Count; i++) { + wasm::WasmInitFunc Init; + Init.Priority = readVaruint32(Ptr); + Init.FunctionIndex = readVaruint32(Ptr); + if (!isValidFunctionIndex(Init.FunctionIndex)) + return make_error("Invalid function index: " + + Twine(Init.FunctionIndex), + object_error::parse_failed); + LinkingData.InitFunctions.emplace_back(Init); + } + break; + } default: Ptr += Size; break; @@ -656,9 +671,13 @@ Error WasmObjectFile::parseExportSection(const uint8_t *Ptr, const uint8_t *End) return Error::success(); } +bool WasmObjectFile::isValidFunctionIndex(uint32_t Index) const { + return Index < FunctionTypes.size() + NumImportedFunctions; +} + Error WasmObjectFile::parseStartSection(const uint8_t *Ptr, const uint8_t *End) { StartFunction = readVaruint32(Ptr); - if (StartFunction >= FunctionTypes.size()) + if (!isValidFunctionIndex(StartFunction)) return make_error("Invalid start function", object_error::parse_failed); return Error::success(); -- cgit v1.1