diff --git a/lld/wasm/Config.h b/lld/wasm/Config.h index cc8a1dcb69afe..dc7ca265e9a2c 100644 --- a/lld/wasm/Config.h +++ b/lld/wasm/Config.h @@ -23,6 +23,13 @@ enum class CodeGenOptLevel; namespace lld::wasm { class InputFile; +class StubFile; +class ObjFile; +class SharedFile; +class BitcodeFile; +class InputTable; +class InputGlobal; +class InputFunction; class Symbol; // For --unresolved-symbols. @@ -108,6 +115,14 @@ extern Configuration *config; // The Ctx object hold all other (non-configuration) global state. struct Ctx { + llvm::SmallVector objectFiles; + llvm::SmallVector stubFiles; + llvm::SmallVector sharedFiles; + llvm::SmallVector bitcodeFiles; + llvm::SmallVector syntheticFunctions; + llvm::SmallVector syntheticGlobals; + llvm::SmallVector syntheticTables; + // True if we are creating position-independent code. bool isPic; diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp index 05a96860966ea..4a4f9a9622794 100644 --- a/lld/wasm/Driver.cpp +++ b/lld/wasm/Driver.cpp @@ -899,7 +899,7 @@ static void createOptionalSymbols() { static void processStubLibrariesPreLTO() { log("-- processStubLibrariesPreLTO"); - for (auto &stub_file : symtab->stubFiles) { + for (auto &stub_file : ctx.stubFiles) { LLVM_DEBUG(llvm::dbgs() << "processing stub file: " << stub_file->getName() << "\n"); for (auto [name, deps]: stub_file->symbolDependencies) { @@ -924,7 +924,7 @@ static void processStubLibraries() { bool depsAdded = false; do { depsAdded = false; - for (auto &stub_file : symtab->stubFiles) { + for (auto &stub_file : ctx.stubFiles) { LLVM_DEBUG(llvm::dbgs() << "processing stub file: " << stub_file->getName() << "\n"); for (auto [name, deps]: stub_file->symbolDependencies) { @@ -1075,7 +1075,7 @@ static void wrapSymbols(ArrayRef wrapped) { } // Update pointers in input files. - parallelForEach(symtab->objectFiles, [&](InputFile *file) { + parallelForEach(ctx.objectFiles, [&](InputFile *file) { MutableArrayRef syms = file->getMutableSymbols(); for (size_t i = 0, e = syms.size(); i != e; ++i) if (Symbol *s = map.lookup(syms[i])) @@ -1091,7 +1091,7 @@ static void splitSections() { // splitIntoPieces needs to be called on each MergeInputChunk // before calling finalizeContents(). LLVM_DEBUG(llvm::dbgs() << "splitSections\n"); - parallelForEach(symtab->objectFiles, [](ObjFile *file) { + parallelForEach(ctx.objectFiles, [](ObjFile *file) { for (InputChunk *seg : file->segments) { if (auto *s = dyn_cast(seg)) s->splitIntoPieces(); @@ -1263,7 +1263,7 @@ void LinkerDriver::linkerMain(ArrayRef argsArr) { // We only need to add libcall symbols to the link before LTO if the symbol's // definition is in bitcode. Any other required libcall symbols will be added // to the link after LTO when we add the LTO object file to the link. - if (!symtab->bitcodeFiles.empty()) + if (!ctx.bitcodeFiles.empty()) for (auto *s : lto::LTO::getRuntimeLibcallSymbols()) handleLibcall(s); if (errorCount()) diff --git a/lld/wasm/MapFile.cpp b/lld/wasm/MapFile.cpp index 288c189854dbd..c96b64cb64838 100644 --- a/lld/wasm/MapFile.cpp +++ b/lld/wasm/MapFile.cpp @@ -51,7 +51,7 @@ static void writeHeader(raw_ostream &os, int64_t vma, uint64_t lma, // Returns a list of all symbols that we want to print out. static std::vector getSymbols() { std::vector v; - for (InputFile *file : symtab->objectFiles) + for (InputFile *file : ctx.objectFiles) for (Symbol *b : file->getSymbols()) if (auto *dr = dyn_cast(b)) if ((!isa(dr)) && dr->isLive() && diff --git a/lld/wasm/MarkLive.cpp b/lld/wasm/MarkLive.cpp index 773af2f7e2360..b8ab7741ff1cb 100644 --- a/lld/wasm/MarkLive.cpp +++ b/lld/wasm/MarkLive.cpp @@ -97,7 +97,7 @@ void MarkLive::run() { enqueue(WasmSym::callDtors); // Enqueue constructors in objects explicitly live from the command-line. - for (const ObjFile *obj : symtab->objectFiles) + for (const ObjFile *obj : ctx.objectFiles) if (obj->isLive()) enqueueInitFunctions(obj); @@ -151,7 +151,7 @@ void markLive() { // Report garbage-collected sections. if (config->printGcSections) { - for (const ObjFile *obj : symtab->objectFiles) { + for (const ObjFile *obj : ctx.objectFiles) { for (InputChunk *c : obj->functions) if (!c->live) message("removing unused section " + toString(c)); @@ -168,13 +168,13 @@ void markLive() { if (!t->live) message("removing unused section " + toString(t)); } - for (InputChunk *c : symtab->syntheticFunctions) + for (InputChunk *c : ctx.syntheticFunctions) if (!c->live) message("removing unused section " + toString(c)); - for (InputGlobal *g : symtab->syntheticGlobals) + for (InputGlobal *g : ctx.syntheticGlobals) if (!g->live) message("removing unused section " + toString(g)); - for (InputTable *t : symtab->syntheticTables) + for (InputTable *t : ctx.syntheticTables) if (!t->live) message("removing unused section " + toString(t)); } @@ -192,7 +192,7 @@ bool MarkLive::isCallCtorsLive() { // If there are any init functions, mark `__wasm_call_ctors` live so that // it can call them. - for (const ObjFile *file : symtab->objectFiles) { + for (const ObjFile *file : ctx.objectFiles) { const WasmLinkingData &l = file->getWasmObj()->linkingData(); for (const WasmInitFunc &f : l.InitFunctions) { auto *sym = file->getFunctionSymbol(f.Symbol); diff --git a/lld/wasm/SymbolTable.cpp b/lld/wasm/SymbolTable.cpp index 9dd599e18975e..9988490e14b0b 100644 --- a/lld/wasm/SymbolTable.cpp +++ b/lld/wasm/SymbolTable.cpp @@ -34,14 +34,14 @@ void SymbolTable::addFile(InputFile *file, StringRef symName) { // .so file if (auto *f = dyn_cast(file)) { - sharedFiles.push_back(f); + ctx.sharedFiles.push_back(f); return; } // stub file if (auto *f = dyn_cast(file)) { f->parse(); - stubFiles.push_back(f); + ctx.stubFiles.push_back(f); return; } @@ -52,7 +52,7 @@ void SymbolTable::addFile(InputFile *file, StringRef symName) { if (auto *f = dyn_cast(file)) { // This order, first adding to `bitcodeFiles` and then parsing is necessary. // See https://github.com/llvm/llvm-project/pull/73095 - bitcodeFiles.push_back(f); + ctx.bitcodeFiles.push_back(f); f->parse(symName); return; } @@ -60,7 +60,7 @@ void SymbolTable::addFile(InputFile *file, StringRef symName) { // Regular object file auto *f = cast(file); f->parse(false); - objectFiles.push_back(f); + ctx.objectFiles.push_back(f); } // This function is where all the optimizations of link-time @@ -74,18 +74,18 @@ void SymbolTable::compileBitcodeFiles() { // Prevent further LTO objects being included BitcodeFile::doneLTO = true; - if (bitcodeFiles.empty()) + if (ctx.bitcodeFiles.empty()) return; // Compile bitcode files and replace bitcode symbols. lto.reset(new BitcodeCompiler); - for (BitcodeFile *f : bitcodeFiles) + for (BitcodeFile *f : ctx.bitcodeFiles) lto->add(*f); for (StringRef filename : lto->compile()) { auto *obj = make(MemoryBufferRef(filename, "lto.tmp"), ""); obj->parse(true); - objectFiles.push_back(obj); + ctx.objectFiles.push_back(obj); } } @@ -218,7 +218,7 @@ DefinedFunction *SymbolTable::addSyntheticFunction(StringRef name, InputFunction *function) { LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << name << "\n"); assert(!find(name)); - syntheticFunctions.emplace_back(function); + ctx.syntheticFunctions.emplace_back(function); return replaceSymbol(insertName(name).first, name, flags, nullptr, function); } @@ -255,7 +255,7 @@ DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef name, uint32_t flags, LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << name << " -> " << global << "\n"); assert(!find(name)); - syntheticGlobals.emplace_back(global); + ctx.syntheticGlobals.emplace_back(global); return replaceSymbol(insertName(name).first, name, flags, nullptr, global); } @@ -267,7 +267,7 @@ DefinedGlobal *SymbolTable::addOptionalGlobalSymbol(StringRef name, return nullptr; LLVM_DEBUG(dbgs() << "addOptionalGlobalSymbol: " << name << " -> " << global << "\n"); - syntheticGlobals.emplace_back(global); + ctx.syntheticGlobals.emplace_back(global); return replaceSymbol(s, name, WASM_SYMBOL_VISIBILITY_HIDDEN, nullptr, global); } @@ -280,7 +280,7 @@ DefinedTable *SymbolTable::addSyntheticTable(StringRef name, uint32_t flags, assert(!s || s->isUndefined()); if (!s) s = insertName(name).first; - syntheticTables.emplace_back(table); + ctx.syntheticTables.emplace_back(table); return replaceSymbol(s, name, flags, nullptr, table); } @@ -855,7 +855,7 @@ InputFunction *SymbolTable::replaceWithUnreachable(Symbol *sym, StringRef debugName) { auto *func = make(sig, sym->getName(), debugName); func->setBody(unreachableFn); - syntheticFunctions.emplace_back(func); + ctx.syntheticFunctions.emplace_back(func); // Mark new symbols as local. For relocatable output we don't want them // to be exported outside the object file. replaceSymbol(sym, debugName, WASM_SYMBOL_BINDING_LOCAL, diff --git a/lld/wasm/SymbolTable.h b/lld/wasm/SymbolTable.h index 59eda1c0b6740..c5518ee23da26 100644 --- a/lld/wasm/SymbolTable.h +++ b/lld/wasm/SymbolTable.h @@ -101,14 +101,6 @@ class SymbolTable { void handleWeakUndefines(); DefinedFunction *createUndefinedStub(const WasmSignature &sig); - std::vector objectFiles; - std::vector stubFiles; - std::vector sharedFiles; - std::vector bitcodeFiles; - std::vector syntheticFunctions; - std::vector syntheticGlobals; - std::vector syntheticTables; - private: std::pair insert(StringRef name, const InputFile *file); std::pair insertName(StringRef name); diff --git a/lld/wasm/SyntheticSections.cpp b/lld/wasm/SyntheticSections.cpp index f95174fe6f269..72e255951608e 100644 --- a/lld/wasm/SyntheticSections.cpp +++ b/lld/wasm/SyntheticSections.cpp @@ -57,7 +57,7 @@ class SubSection { bool DylinkSection::isNeeded() const { return ctx.isPic || config->unresolvedSymbols == UnresolvedPolicy::ImportDynamic || - !symtab->sharedFiles.empty(); + !ctx.sharedFiles.empty(); } void DylinkSection::writeBody() { @@ -72,10 +72,10 @@ void DylinkSection::writeBody() { sub.writeTo(os); } - if (symtab->sharedFiles.size()) { + if (ctx.sharedFiles.size()) { SubSection sub(WASM_DYLINK_NEEDED); - writeUleb128(sub.os, symtab->sharedFiles.size(), "Needed"); - for (auto *so : symtab->sharedFiles) + writeUleb128(sub.os, ctx.sharedFiles.size(), "Needed"); + for (auto *so : ctx.sharedFiles) writeStr(sub.os, llvm::sys::path::filename(so->getName()), "so name"); sub.writeTo(os); } diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp index 7c19a1baf4c04..d1a06c9ac9c2a 100644 --- a/lld/wasm/Writer.cpp +++ b/lld/wasm/Writer.cpp @@ -133,7 +133,7 @@ class Writer { void Writer::calculateCustomSections() { log("calculateCustomSections"); bool stripDebug = config->stripDebug || config->stripAll; - for (ObjFile *file : symtab->objectFiles) { + for (ObjFile *file : ctx.objectFiles) { for (InputChunk *section : file->customSections) { // Exclude COMDAT sections that are not selected for inclusion if (section->discarded) @@ -207,7 +207,7 @@ void Writer::createRelocSections() { } void Writer::populateProducers() { - for (ObjFile *file : symtab->objectFiles) { + for (ObjFile *file : ctx.objectFiles) { const WasmProducerInfo &info = file->getWasmObj()->getProducerInfo(); out.producersSec->addInfo(info); } @@ -591,7 +591,7 @@ void Writer::populateTargetFeatures() { } // Find the sets of used, required, and disallowed features - for (ObjFile *file : symtab->objectFiles) { + for (ObjFile *file : ctx.objectFiles) { StringRef fileName(file->getName()); for (auto &feature : file->getWasmObj()->getTargetFeatures()) { switch (feature.Prefix) { @@ -654,7 +654,7 @@ void Writer::populateTargetFeatures() { } // Validate the required and disallowed constraints for each file - for (ObjFile *file : symtab->objectFiles) { + for (ObjFile *file : ctx.objectFiles) { StringRef fileName(file->getName()); SmallSet objectFeatures; for (const auto &feature : file->getWasmObj()->getTargetFeatures()) { @@ -832,7 +832,7 @@ void Writer::populateSymtab() { if (sym->isUsedInRegularObj && sym->isLive()) out.linkingSec->addToSymtab(sym); - for (ObjFile *file : symtab->objectFiles) { + for (ObjFile *file : ctx.objectFiles) { LLVM_DEBUG(dbgs() << "Local symtab entries: " << file->getName() << "\n"); for (Symbol *sym : file->getSymbols()) if (sym->isLocal() && !isa(sym) && sym->isLive()) @@ -848,7 +848,7 @@ void Writer::calculateTypes() { // 4. The signatures of all imported tags // 5. The signatures of all defined tags - for (ObjFile *file : symtab->objectFiles) { + for (ObjFile *file : ctx.objectFiles) { ArrayRef types = file->getWasmObj()->types(); for (uint32_t i = 0; i < types.size(); i++) if (file->typeIsUsed[i]) @@ -939,7 +939,7 @@ static void finalizeIndirectFunctionTable() { } static void scanRelocations() { - for (ObjFile *file : symtab->objectFiles) { + for (ObjFile *file : ctx.objectFiles) { LLVM_DEBUG(dbgs() << "scanRelocations: " << file->getName() << "\n"); for (InputChunk *chunk : file->functions) scanRelocations(chunk); @@ -955,37 +955,37 @@ void Writer::assignIndexes() { // global are effected by the number of imports. out.importSec->seal(); - for (InputFunction *func : symtab->syntheticFunctions) + for (InputFunction *func : ctx.syntheticFunctions) out.functionSec->addFunction(func); - for (ObjFile *file : symtab->objectFiles) { + for (ObjFile *file : ctx.objectFiles) { LLVM_DEBUG(dbgs() << "Functions: " << file->getName() << "\n"); for (InputFunction *func : file->functions) out.functionSec->addFunction(func); } - for (InputGlobal *global : symtab->syntheticGlobals) + for (InputGlobal *global : ctx.syntheticGlobals) out.globalSec->addGlobal(global); - for (ObjFile *file : symtab->objectFiles) { + for (ObjFile *file : ctx.objectFiles) { LLVM_DEBUG(dbgs() << "Globals: " << file->getName() << "\n"); for (InputGlobal *global : file->globals) out.globalSec->addGlobal(global); } - for (ObjFile *file : symtab->objectFiles) { + for (ObjFile *file : ctx.objectFiles) { LLVM_DEBUG(dbgs() << "Tags: " << file->getName() << "\n"); for (InputTag *tag : file->tags) out.tagSec->addTag(tag); } - for (ObjFile *file : symtab->objectFiles) { + for (ObjFile *file : ctx.objectFiles) { LLVM_DEBUG(dbgs() << "Tables: " << file->getName() << "\n"); for (InputTable *table : file->tables) out.tableSec->addTable(table); } - for (InputTable *table : symtab->syntheticTables) + for (InputTable *table : ctx.syntheticTables) out.tableSec->addTable(table); out.globalSec->assignIndexes(); @@ -1022,7 +1022,7 @@ OutputSegment *Writer::createOutputSegment(StringRef name) { } void Writer::createOutputSegments() { - for (ObjFile *file : symtab->objectFiles) { + for (ObjFile *file : ctx.objectFiles) { for (InputChunk *segment : file->segments) { if (!segment->live) continue; @@ -1639,7 +1639,7 @@ void Writer::calculateInitFunctions() { if (!config->relocatable && !WasmSym::callCtors->isLive()) return; - for (ObjFile *file : symtab->objectFiles) { + for (ObjFile *file : ctx.objectFiles) { const WasmLinkingData &l = file->getWasmObj()->linkingData(); for (const WasmInitFunc &f : l.InitFunctions) { FunctionSymbol *sym = file->getFunctionSymbol(f.Symbol);