Index: wasm/InputChunks.h =================================================================== --- wasm/InputChunks.h +++ wasm/InputChunks.h @@ -24,6 +24,7 @@ #include "Config.h" #include "InputFiles.h" #include "lld/Common/ErrorHandler.h" +#include "lld/Common/Strings.h" #include "llvm/Object/Wasm.h" using llvm::object::WasmSegment; @@ -151,17 +152,16 @@ class SyntheticFunction : public InputFunction { public: - SyntheticFunction(const WasmSignature &S, ArrayRef Body, - StringRef Name) - : InputFunction(S, nullptr, nullptr), Name(Name), Body(Body) {} + SyntheticFunction(const WasmSignature &S, std::string Body, StringRef Name) + : InputFunction(S, nullptr, nullptr), Name(Name), Body(std::move(Body)) {} StringRef getName() const override { return Name; } protected: - ArrayRef data() const override { return Body; } + ArrayRef data() const override { return toArrayRef(Body); } StringRef Name; - ArrayRef Body; + std::string Body; }; } // namespace wasm Index: wasm/Writer.cpp =================================================================== --- wasm/Writer.cpp +++ wasm/Writer.cpp @@ -17,7 +17,6 @@ #include "WriterUtils.h" #include "lld/Common/ErrorHandler.h" #include "lld/Common/Memory.h" -#include "lld/Common/Strings.h" #include "lld/Common/Threads.h" #include "llvm/ADT/DenseSet.h" #include "llvm/BinaryFormat/Wasm.h" @@ -137,7 +136,6 @@ std::vector OutputSections; std::unique_ptr Buffer; - std::string CtorFunctionBody; std::vector Segments; llvm::SmallDenseMap SegmentMap; @@ -859,27 +857,28 @@ uint32_t FunctionIndex = NumImportedFunctions + InputFunctions.size(); WasmSym::CallCtors->setOutputIndex(FunctionIndex); - // First write the body bytes to a string. std::string FunctionBody; { - raw_string_ostream OS(FunctionBody); + // First write the body's contents to a string. + SmallString<256> BodyContent; + raw_svector_ostream OS(BodyContent); writeUleb128(OS, 0, "num locals"); - for (const WasmInitEntry &F : InitFunctions) { + for (const WasmInitEntry& F : InitFunctions) { writeU8(OS, OPCODE_CALL, "CALL"); writeUleb128(OS, F.Sym->getOutputIndex(), "function index"); } writeU8(OS, OPCODE_END, "END"); - } - // Once we know the size of the body we can create the final function body - raw_string_ostream OS(CtorFunctionBody); - writeUleb128(OS, FunctionBody.size(), "function size"); - OS.flush(); - CtorFunctionBody += FunctionBody; + // Once we know the size of the body we can create the final function body + raw_string_ostream OS2(FunctionBody); + writeUleb128(OS2, BodyContent.size(), "function size"); + OS2 << BodyContent; + OS2.flush(); + } const WasmSignature *Sig = WasmSym::CallCtors->getFunctionType(); SyntheticFunction *F = make( - *Sig, toArrayRef(CtorFunctionBody), WasmSym::CallCtors->getName()); + *Sig, std::move(FunctionBody), WasmSym::CallCtors->getName()); F->setOutputIndex(FunctionIndex); F->Live = true; @@ -891,11 +890,15 @@ // This is then used either when creating the output linking section or to // synthesize the "__wasm_call_ctors" function. void Writer::calculateInitFunctions() { + WasmSignature VoidSignature = {{}, WASM_TYPE_NORESULT}; + for (ObjFile *File : Symtab->ObjectFiles) { const WasmLinkingData &L = File->getWasmObj()->linkingData(); - for (const WasmInitFunc &F : L.InitFunctions) - InitFunctions.emplace_back( - WasmInitEntry{File->getFunctionSymbol(F.Symbol), F.Priority}); + for (const WasmInitFunc &F : L.InitFunctions) { + FunctionSymbol *Sym = File->getFunctionSymbol(F.Symbol); + assert(*Sym->getFunctionType() == VoidSignature); + InitFunctions.emplace_back(WasmInitEntry{Sym, F.Priority}); + } } // Sort in order of priority (lowest first) so that they are called