diff --git a/lld/wasm/InputFiles.h b/lld/wasm/InputFiles.h --- a/lld/wasm/InputFiles.h +++ b/lld/wasm/InputFiles.h @@ -154,7 +154,7 @@ Symbol *createDefined(const WasmSymbol &sym); Symbol *createUndefined(const WasmSymbol &sym, bool isCalledDirectly); - bool isExcludedByComdat(InputChunk *chunk) const; + bool isExcludedByComdat(const InputChunk *chunk) const; void addLegacyIndirectFunctionTableIfNeeded(uint32_t tableSymbolCount); std::unique_ptr wasmObj; diff --git a/lld/wasm/InputFiles.cpp b/lld/wasm/InputFiles.cpp --- a/lld/wasm/InputFiles.cpp +++ b/lld/wasm/InputFiles.cpp @@ -496,12 +496,11 @@ // Populate `Functions`. ArrayRef funcs = wasmObj->functions(); - ArrayRef funcTypes = wasmObj->functionTypes(); ArrayRef types = wasmObj->types(); functions.reserve(funcs.size()); - for (size_t i = 0, e = funcs.size(); i != e; ++i) { - auto* func = make(types[funcTypes[i]], &funcs[i], this); + for (auto &f : funcs) { + auto *func = make(types[f.SigIndex], &f, this); func->discarded = isExcludedByComdat(func); functions.emplace_back(func); } @@ -541,7 +540,7 @@ addLegacyIndirectFunctionTableIfNeeded(tableSymbolCount); } -bool ObjFile::isExcludedByComdat(InputChunk *chunk) const { +bool ObjFile::isExcludedByComdat(const InputChunk *chunk) const { uint32_t c = chunk->getComdat(); if (c == UINT32_MAX) return false; diff --git a/llvm/include/llvm/BinaryFormat/Wasm.h b/llvm/include/llvm/BinaryFormat/Wasm.h --- a/llvm/include/llvm/BinaryFormat/Wasm.h +++ b/llvm/include/llvm/BinaryFormat/Wasm.h @@ -139,6 +139,7 @@ struct WasmFunction { uint32_t Index; + uint32_t SigIndex; std::vector Locals; ArrayRef Body; uint32_t CodeSectionOffset; diff --git a/llvm/include/llvm/Object/Wasm.h b/llvm/include/llvm/Object/Wasm.h --- a/llvm/include/llvm/Object/Wasm.h +++ b/llvm/include/llvm/Object/Wasm.h @@ -138,7 +138,6 @@ return TargetFeatures; } ArrayRef types() const { return Signatures; } - ArrayRef functionTypes() const { return FunctionTypes; } ArrayRef imports() const { return Imports; } ArrayRef tables() const { return Tables; } ArrayRef memories() const { return Memories; } diff --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp --- a/llvm/lib/Object/WasmObjectFile.cpp +++ b/llvm/lib/Object/WasmObjectFile.cpp @@ -412,7 +412,7 @@ llvm::DenseSet SeenFunctions; llvm::DenseSet SeenGlobals; llvm::DenseSet SeenSegments; - if (FunctionTypes.size() && !SeenCodeSection) { + if (Functions.size() && !SeenCodeSection) { return make_error("names must come after code section", object_error::parse_failed); } @@ -480,7 +480,7 @@ Error WasmObjectFile::parseLinkingSection(ReadContext &Ctx) { HasLinkingSection = true; - if (FunctionTypes.size() && !SeenCodeSection) { + if (Functions.size() && !SeenCodeSection) { return make_error( "linking data must come after code section", object_error::parse_failed); @@ -598,8 +598,8 @@ if (IsDefined) { Info.Name = readString(Ctx); unsigned FuncIndex = Info.ElementIndex - NumImportedFunctions; - Signature = &Signatures[FunctionTypes[FuncIndex]]; wasm::WasmFunction &Function = Functions[FuncIndex]; + Signature = &Signatures[Function.SigIndex]; if (Function.SymbolName.empty()) Function.SymbolName = Info.Name; } else { @@ -1140,15 +1140,16 @@ Error WasmObjectFile::parseFunctionSection(ReadContext &Ctx) { uint32_t Count = readVaruint32(Ctx); - FunctionTypes.reserve(Count); - Functions.resize(Count); + Functions.reserve(Count); uint32_t NumTypes = Signatures.size(); while (Count--) { uint32_t Type = readVaruint32(Ctx); if (Type >= NumTypes) return make_error("invalid function type", object_error::parse_failed); - FunctionTypes.push_back(Type); + wasm::WasmFunction F; + F.SigIndex = Type; + Functions.push_back(F); } if (Ctx.Ptr != Ctx.End) return make_error("function section ended prematurely", @@ -1272,7 +1273,7 @@ } bool WasmObjectFile::isValidFunctionIndex(uint32_t Index) const { - return Index < NumImportedFunctions + FunctionTypes.size(); + return Index < NumImportedFunctions + Functions.size(); } bool WasmObjectFile::isDefinedFunctionIndex(uint32_t Index) const { @@ -1360,7 +1361,7 @@ SeenCodeSection = true; CodeSection = Sections.size(); uint32_t FunctionCount = readVaruint32(Ctx); - if (FunctionCount != FunctionTypes.size()) { + if (FunctionCount != Functions.size()) { return make_error("invalid function count", object_error::parse_failed); } diff --git a/llvm/tools/obj2yaml/wasm2yaml.cpp b/llvm/tools/obj2yaml/wasm2yaml.cpp --- a/llvm/tools/obj2yaml/wasm2yaml.cpp +++ b/llvm/tools/obj2yaml/wasm2yaml.cpp @@ -260,8 +260,8 @@ } case wasm::WASM_SEC_FUNCTION: { auto FuncSec = std::make_unique(); - for (const auto &Func : Obj.functionTypes()) { - FuncSec->FunctionTypes.push_back(Func); + for (const auto &Func : Obj.functions()) { + FuncSec->FunctionTypes.push_back(Func.SigIndex); } S = std::move(FuncSec); break;