Index: lld/wasm/InputFiles.h =================================================================== --- lld/wasm/InputFiles.h +++ lld/wasm/InputFiles.h @@ -114,7 +114,6 @@ Symbol *createDefined(const WasmSymbol &Sym); Symbol *createUndefined(const WasmSymbol &Sym); - void initializeSymbols(); InputSegment *getSegment(const WasmSymbol &WasmSym) const; InputFunction *getFunction(const WasmSymbol &Sym) const; InputGlobal *getGlobal(const WasmSymbol &Sym) const; @@ -123,8 +122,6 @@ // List of all symbols referenced or defined by this file. std::vector Symbols; - uint32_t NumGlobalImports = 0; - uint32_t NumFunctionImports = 0; std::unique_ptr WasmObj; }; Index: lld/wasm/InputFiles.cpp =================================================================== --- lld/wasm/InputFiles.cpp +++ lld/wasm/InputFiles.cpp @@ -43,10 +43,13 @@ } void ObjFile::dumpInfo() const { + uint32_t NumFuncs = WasmObj->getNumImportedFunctions(); + uint32_t NumGlobals = WasmObj->getNumImportedGlobals(); + log("info for: " + getName() + "\n" + " Symbols : " + Twine(Symbols.size()) + "\n" + - " Function Imports : " + Twine(NumFunctionImports) + "\n" + - " Global Imports : " + Twine(NumGlobalImports) + "\n"); + " Function Imports : " + Twine(NumFuncs) + "\n" + + " Global Imports : " + Twine(NumGlobals) + "\n"); } // Relocations contain an index into the function, global or table index @@ -117,7 +120,42 @@ TypeMap.resize(getWasmObj()->types().size()); TypeIsUsed.resize(getWasmObj()->types().size(), false); - initializeSymbols(); + for (StringRef Name : WasmObj->comdats()) + Symtab->addComdat(Name, this); + + // Populate `Segments`. + for (const WasmSegment &S : WasmObj->dataSegments()) { + InputSegment *Seg = make(S, this); + Seg->copyRelocations(*DataSection); + Segments.emplace_back(Seg); + } + + // 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) { + InputFunction *F = + make(Types[FuncTypes[I]], &Funcs[I], this); + F->copyRelocations(*CodeSection); + Functions.emplace_back(F); + } + + // Populate `Globals`. + for (const WasmGlobal &G : WasmObj->globals()) + Globals.emplace_back(make(G)); + + // Populate `Symbols` based on the WasmSymbols in the object. + Symbols.reserve(WasmObj->getNumberOfSymbols()); + for (const SymbolRef &Sym : WasmObj->symbols()) { + const WasmSymbol &WasmSym = WasmObj->getWasmSymbol(Sym.getRawDataRefImpl()); + if (Symbol *Sym = createDefined(WasmSym)) + Symbols.push_back(Sym); + else + Symbols.push_back(createUndefined(WasmSym)); + } } // Return the InputSegment in which a given symbol is defined. @@ -126,15 +164,15 @@ } InputFunction *ObjFile::getFunction(const WasmSymbol &Sym) const { - assert(Sym.Info.ElementIndex >= NumFunctionImports); - uint32_t FunctionIndex = Sym.Info.ElementIndex - NumFunctionImports; - return Functions[FunctionIndex]; + uint32_t N = WasmObj->getNumImportedFunctions(); + assert(N <= Sym.Info.ElementIndex); + return Functions[Sym.Info.ElementIndex - N]; } InputGlobal *ObjFile::getGlobal(const WasmSymbol &Sym) const { - assert(Sym.Info.ElementIndex >= NumGlobalImports); - uint32_t GlobalIndex = Sym.Info.ElementIndex - NumGlobalImports; - return Globals[GlobalIndex]; + uint32_t N = WasmObj->getNumImportedGlobals(); + assert(N <= Sym.Info.ElementIndex); + return Globals[Sym.Info.ElementIndex - N]; } bool ObjFile::isExcludedByComdat(InputChunk *Chunk) const { @@ -154,47 +192,6 @@ return cast(Symbols[Index]); } -void ObjFile::initializeSymbols() { - Symbols.reserve(WasmObj->getNumberOfSymbols()); - - NumFunctionImports = WasmObj->getNumImportedFunctions(); - NumGlobalImports = WasmObj->getNumImportedGlobals(); - - ArrayRef Funcs = WasmObj->functions(); - ArrayRef FuncTypes = WasmObj->functionTypes(); - ArrayRef Types = WasmObj->types(); - - for (StringRef Name : WasmObj->comdats()) - Symtab->addComdat(Name, this); - - for (const WasmSegment &S : WasmObj->dataSegments()) { - InputSegment *Seg = make(S, this); - Seg->copyRelocations(*DataSection); - Segments.emplace_back(Seg); - } - - for (const WasmGlobal &G : WasmObj->globals()) - Globals.emplace_back(make(G)); - - for (size_t I = 0; I < Funcs.size(); ++I) { - const WasmFunction &Func = Funcs[I]; - const WasmSignature &Sig = Types[FuncTypes[I]]; - InputFunction *F = make(Sig, &Func, this); - F->copyRelocations(*CodeSection); - Functions.emplace_back(F); - } - - // Populate `Symbols` based on the WasmSymbols in the object - for (const SymbolRef &Sym : WasmObj->symbols()) { - const WasmSymbol &WasmSym = WasmObj->getWasmSymbol(Sym.getRawDataRefImpl()); - - if (Symbol *Sym = createDefined(WasmSym)) - Symbols.push_back(Sym); - else - Symbols.push_back(createUndefined(WasmSym)); - } -} - Symbol *ObjFile::createDefined(const WasmSymbol &Sym) { if (!Sym.isDefined()) return nullptr;