diff options
author | Sam Clegg <sbc@chromium.org> | 2021-09-10 07:21:28 -0400 |
---|---|---|
committer | Sam Clegg <sbc@chromium.org> | 2021-09-12 05:30:38 -0700 |
commit | b78c85a44af300f8e3da582411814385cf70239d (patch) | |
tree | 31129e08f05a93a8abd14f799a71103e233487b5 /llvm/lib/Object/WasmObjectFile.cpp | |
parent | d338e535ec5f1de8b1b6cf7ea74514dfe1ecd0ce (diff) | |
download | llvm-b78c85a44af300f8e3da582411814385cf70239d.zip llvm-b78c85a44af300f8e3da582411814385cf70239d.tar.gz llvm-b78c85a44af300f8e3da582411814385cf70239d.tar.bz2 |
[WebAssembly] Convert to new "dylink.0" section format
This format is based on sub-sections (like the "linking" and "name"
sections) and is therefore easier to extend going forward.
spec change: https://github.com/WebAssembly/tool-conventions/pull/170
binaryen change: https://github.com/WebAssembly/binaryen/pull/4141
wabt change: https://github.com/WebAssembly/wabt/pull/1707
emscripten change: https://github.com/emscripten-core/emscripten/pull/15019
Differential Revision: https://reviews.llvm.org/D109595
Diffstat (limited to 'llvm/lib/Object/WasmObjectFile.cpp')
-rw-r--r-- | llvm/lib/Object/WasmObjectFile.cpp | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp index 9a4e246..3b1392e 100644 --- a/llvm/lib/Object/WasmObjectFile.cpp +++ b/llvm/lib/Object/WasmObjectFile.cpp @@ -339,7 +339,8 @@ Error WasmObjectFile::parseSection(WasmSection &Sec) { } Error WasmObjectFile::parseDylinkSection(ReadContext &Ctx) { - // See https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md + // Legacy "dylink" section support. + // See parseDylink0Section for the current "dylink.0" section parsing. HasDylinkSection = true; DylinkInfo.MemorySize = readVaruint32(Ctx); DylinkInfo.MemoryAlignment = readVaruint32(Ctx); @@ -349,12 +350,58 @@ Error WasmObjectFile::parseDylinkSection(ReadContext &Ctx) { while (Count--) { DylinkInfo.Needed.push_back(readString(Ctx)); } + if (Ctx.Ptr != Ctx.End) return make_error<GenericBinaryError>("dylink section ended prematurely", object_error::parse_failed); return Error::success(); } +Error WasmObjectFile::parseDylink0Section(ReadContext &Ctx) { + // See + // https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md + HasDylinkSection = true; + + const uint8_t *OrigEnd = Ctx.End; + while (Ctx.Ptr < OrigEnd) { + Ctx.End = OrigEnd; + uint8_t Type = readUint8(Ctx); + uint32_t Size = readVaruint32(Ctx); + LLVM_DEBUG(dbgs() << "readSubsection type=" << int(Type) << " size=" << Size + << "\n"); + Ctx.End = Ctx.Ptr + Size; + uint32_t Count; + switch (Type) { + case wasm::WASM_DYLINK_MEM_INFO: + DylinkInfo.MemorySize = readVaruint32(Ctx); + DylinkInfo.MemoryAlignment = readVaruint32(Ctx); + DylinkInfo.TableSize = readVaruint32(Ctx); + DylinkInfo.TableAlignment = readVaruint32(Ctx); + break; + case wasm::WASM_DYLINK_NEEDED: + Count = readVaruint32(Ctx); + while (Count--) { + DylinkInfo.Needed.push_back(readString(Ctx)); + } + break; + default: + return make_error<GenericBinaryError>("unknown dylink.0 sub-section", + object_error::parse_failed); + Ctx.Ptr += Size; + break; + } + if (Ctx.Ptr != Ctx.End) { + return make_error<GenericBinaryError>( + "dylink.0 sub-section ended prematurely", object_error::parse_failed); + } + } + + if (Ctx.Ptr != Ctx.End) + return make_error<GenericBinaryError>("dylink.0 section ended prematurely", + object_error::parse_failed); + return Error::success(); +} + Error WasmObjectFile::parseNameSection(ReadContext &Ctx) { llvm::DenseSet<uint64_t> SeenFunctions; llvm::DenseSet<uint64_t> SeenGlobals; @@ -984,6 +1031,9 @@ Error WasmObjectFile::parseCustomSection(WasmSection &Sec, ReadContext &Ctx) { if (Sec.Name == "dylink") { if (Error Err = parseDylinkSection(Ctx)) return Err; + } else if (Sec.Name == "dylink.0") { + if (Error Err = parseDylink0Section(Ctx)) + return Err; } else if (Sec.Name == "name") { if (Error Err = parseNameSection(Ctx)) return Err; @@ -1793,6 +1843,7 @@ int WasmSectionOrderChecker::getSectionOrder(unsigned ID, case wasm::WASM_SEC_CUSTOM: return StringSwitch<unsigned>(CustomSectionName) .Case("dylink", WASM_SEC_ORDER_DYLINK) + .Case("dylink.0", WASM_SEC_ORDER_DYLINK) .Case("linking", WASM_SEC_ORDER_LINKING) .StartsWith("reloc.", WASM_SEC_ORDER_RELOC) .Case("name", WASM_SEC_ORDER_NAME) |