Index: lld/trunk/wasm/InputFiles.h =================================================================== --- lld/trunk/wasm/InputFiles.h +++ lld/trunk/wasm/InputFiles.h @@ -114,17 +114,11 @@ 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; bool isExcludedByComdat(InputChunk *Chunk) const; // 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/trunk/wasm/InputFiles.cpp =================================================================== --- lld/trunk/wasm/InputFiles.cpp +++ lld/trunk/wasm/InputFiles.cpp @@ -43,10 +43,10 @@ } void ObjFile::dumpInfo() const { - log("info for: " + getName() + "\n" + - " Symbols : " + Twine(Symbols.size()) + "\n" + - " Function Imports : " + Twine(NumFunctionImports) + "\n" + - " Global Imports : " + Twine(NumGlobalImports) + "\n"); + log("info for: " + getName() + + "\n Symbols : " + Twine(Symbols.size()) + + "\n Function Imports : " + Twine(WasmObj->getNumImportedFunctions()) + + "\n Global Imports : " + Twine(WasmObj->getNumImportedGlobals())); } // Relocations contain an index into the function, global or table index @@ -117,77 +117,37 @@ TypeMap.resize(getWasmObj()->types().size()); TypeIsUsed.resize(getWasmObj()->types().size(), false); - initializeSymbols(); -} - -// Return the InputSegment in which a given symbol is defined. -InputSegment *ObjFile::getSegment(const WasmSymbol &WasmSym) const { - return Segments[WasmSym.Info.DataRef.Segment]; -} - -InputFunction *ObjFile::getFunction(const WasmSymbol &Sym) const { - assert(Sym.Info.ElementIndex >= NumFunctionImports); - uint32_t FunctionIndex = Sym.Info.ElementIndex - NumFunctionImports; - return Functions[FunctionIndex]; -} - -InputGlobal *ObjFile::getGlobal(const WasmSymbol &Sym) const { - assert(Sym.Info.ElementIndex >= NumGlobalImports); - uint32_t GlobalIndex = Sym.Info.ElementIndex - NumGlobalImports; - return Globals[GlobalIndex]; -} - -bool ObjFile::isExcludedByComdat(InputChunk *Chunk) const { - StringRef Comdat = Chunk->getComdat(); - return !Comdat.empty() && Symtab->findComdat(Comdat) != this; -} - -FunctionSymbol *ObjFile::getFunctionSymbol(uint32_t Index) const { - return cast(Symbols[Index]); -} - -GlobalSymbol *ObjFile::getGlobalSymbol(uint32_t Index) const { - return cast(Symbols[Index]); -} - -DataSymbol *ObjFile::getDataSymbol(uint32_t Index) const { - 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); + // Populate `Segments`. 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)); + // Populate `Functions`. + ArrayRef Funcs = WasmObj->functions(); + ArrayRef FuncTypes = WasmObj->functionTypes(); + ArrayRef Types = WasmObj->types(); + Functions.reserve(Funcs.size()); - 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); + 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 `Symbols` based on the WasmSymbols in the object + // 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 @@ -195,13 +155,31 @@ } } +bool ObjFile::isExcludedByComdat(InputChunk *Chunk) const { + StringRef Comdat = Chunk->getComdat(); + return !Comdat.empty() && Symtab->findComdat(Comdat) != this; +} + +FunctionSymbol *ObjFile::getFunctionSymbol(uint32_t Index) const { + return cast(Symbols[Index]); +} + +GlobalSymbol *ObjFile::getGlobalSymbol(uint32_t Index) const { + return cast(Symbols[Index]); +} + +DataSymbol *ObjFile::getDataSymbol(uint32_t Index) const { + return cast(Symbols[Index]); +} + Symbol *ObjFile::createDefined(const WasmSymbol &Sym) { if (!Sym.isDefined()) return nullptr; switch (Sym.Info.Kind) { case WASM_SYMBOL_TYPE_FUNCTION: { - InputFunction *Func = getFunction(Sym); + InputFunction *Func = + Functions[Sym.Info.ElementIndex - WasmObj->getNumImportedFunctions()]; if (isExcludedByComdat(Func)) { Func->Live = false; return nullptr; @@ -213,7 +191,7 @@ Func); } case WASM_SYMBOL_TYPE_DATA: { - InputSegment *Seg = getSegment(Sym); + InputSegment *Seg = Segments[Sym.Info.DataRef.Segment]; if (isExcludedByComdat(Seg)) { Seg->Live = false; return nullptr; @@ -229,11 +207,12 @@ Offset, Size); } case WASM_SYMBOL_TYPE_GLOBAL: + InputGlobal *Global = + Globals[Sym.Info.ElementIndex - WasmObj->getNumImportedGlobals()]; if (Sym.isBindingLocal()) - return make(Sym.Info.Name, Sym.Info.Flags, this, - getGlobal(Sym)); + return make(Sym.Info.Name, Sym.Info.Flags, this, Global); return Symtab->addDefinedGlobal(Sym.Info.Name, Sym.Info.Flags, this, - getGlobal(Sym)); + Global); } llvm_unreachable("unkown symbol kind"); }