forked from OSchip/llvm-project
[PECOFF] Remove ResolvableSymbols to simplify.
We had such class there because of InputGraph abstraction. Previously, no one except InputGraph itself has complete picture of input file list. In order to create a set of all defined symbols, we had to use some indirections there to workaround InputGraph. It can now be rewritten as simple code. No change in functionality. llvm-svn: 226319
This commit is contained in:
parent
3b047e0ee5
commit
6d176f88ea
|
@ -30,28 +30,6 @@ static const uint8_t DEFAULT_DOS_STUB[128] = {'M', 'Z'};
|
|||
|
||||
namespace lld {
|
||||
|
||||
namespace pecoff {
|
||||
class ResolvableSymbols {
|
||||
public:
|
||||
void add(File *file);
|
||||
|
||||
const std::set<std::string> &defined() {
|
||||
readAllSymbols();
|
||||
return _defined;
|
||||
}
|
||||
|
||||
private:
|
||||
// Files are read lazily, so that it has no runtime overhead if
|
||||
// no one accesses this class.
|
||||
void readAllSymbols();
|
||||
|
||||
std::set<std::string> _defined;
|
||||
std::set<File *> _seen;
|
||||
std::set<File *> _queue;
|
||||
std::mutex _mutex;
|
||||
};
|
||||
} // end namespace pecoff
|
||||
|
||||
class PECOFFLinkingContext : public LinkingContext {
|
||||
public:
|
||||
PECOFFLinkingContext()
|
||||
|
@ -119,6 +97,9 @@ public:
|
|||
return _machineType == llvm::COFF::IMAGE_FILE_MACHINE_AMD64;
|
||||
}
|
||||
|
||||
// Returns a set of all defined symbols in input files.
|
||||
const std::set<std::string> &definedSymbols();
|
||||
|
||||
/// Page size of x86 processor. Some data needs to be aligned at page boundary
|
||||
/// when loaded into memory.
|
||||
uint64_t getPageSize() const {
|
||||
|
@ -345,10 +326,6 @@ public:
|
|||
|
||||
std::recursive_mutex &getMutex() { return _mutex; }
|
||||
|
||||
pecoff::ResolvableSymbols *getResolvableSymsFile() {
|
||||
return &_resolvableSyms;
|
||||
}
|
||||
|
||||
protected:
|
||||
/// Method to create a internal file for the entry symbol
|
||||
std::unique_ptr<File> createEntrySymbolFile() const override;
|
||||
|
@ -461,7 +438,8 @@ private:
|
|||
// only.
|
||||
std::string _moduleDefinitionFile;
|
||||
|
||||
pecoff::ResolvableSymbols _resolvableSyms;
|
||||
std::set<std::string> _definedSyms;
|
||||
std::set<Node *> _seen;
|
||||
};
|
||||
|
||||
} // end namespace lld
|
||||
|
|
|
@ -1414,7 +1414,6 @@ bool WinLinkDriver::parse(int argc, const char *argv[],
|
|||
File *f = file.get();
|
||||
ctx.getTaskGroup().spawn([f] { f->parse(); });
|
||||
}
|
||||
ctx.getResolvableSymsFile()->add(file.get());
|
||||
ctx.getNodes().push_back(llvm::make_unique<FileNode>(std::move(file)));
|
||||
}
|
||||
|
||||
|
@ -1431,7 +1430,6 @@ bool WinLinkDriver::parse(int argc, const char *argv[],
|
|||
File *f = file.get();
|
||||
ctx.getTaskGroup().spawn([f] { f->parse(); });
|
||||
}
|
||||
ctx.getResolvableSymsFile()->add(file.get());
|
||||
ctx.addLibraryFile(llvm::make_unique<FileNode>(std::move(file)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,9 +13,9 @@ namespace lld {
|
|||
namespace pecoff {
|
||||
|
||||
// Find decorated symbol, namely /sym@[0-9]+/ or /\?sym@@.+/.
|
||||
bool findDecoratedSymbol(PECOFFLinkingContext *ctx, ResolvableSymbols *syms,
|
||||
bool findDecoratedSymbol(PECOFFLinkingContext *ctx,
|
||||
std::string sym, std::string &res) {
|
||||
const std::set<std::string> &defined = syms->defined();
|
||||
const std::set<std::string> &defined = ctx->definedSymbols();
|
||||
// Search for /sym@[0-9]+/
|
||||
{
|
||||
std::string s = sym + '@';
|
||||
|
|
|
@ -20,8 +20,7 @@ using llvm::COFF::WindowsSubsystem;
|
|||
namespace lld {
|
||||
namespace pecoff {
|
||||
|
||||
class ResolvableSymbols;
|
||||
bool findDecoratedSymbol(PECOFFLinkingContext *ctx, ResolvableSymbols *syms,
|
||||
bool findDecoratedSymbol(PECOFFLinkingContext *ctx,
|
||||
std::string sym, std::string &res);
|
||||
|
||||
namespace impl {
|
||||
|
@ -206,9 +205,8 @@ private:
|
|||
// next visit.
|
||||
class ExportedSymbolRenameFile : public impl::VirtualArchiveLibraryFile {
|
||||
public:
|
||||
ExportedSymbolRenameFile(const PECOFFLinkingContext &ctx,
|
||||
ResolvableSymbols *syms)
|
||||
: VirtualArchiveLibraryFile("<export>"), _syms(syms),
|
||||
ExportedSymbolRenameFile(const PECOFFLinkingContext &ctx)
|
||||
: VirtualArchiveLibraryFile("<export>"),
|
||||
_ctx(const_cast<PECOFFLinkingContext *>(&ctx)) {
|
||||
for (PECOFFLinkingContext::ExportDesc &desc : _ctx->getDllExports())
|
||||
_exportedSyms.insert(desc.name);
|
||||
|
@ -219,7 +217,7 @@ public:
|
|||
if (_exportedSyms.count(sym) == 0)
|
||||
return nullptr;
|
||||
std::string replace;
|
||||
if (!findDecoratedSymbol(_ctx, _syms, sym.str(), replace))
|
||||
if (!findDecoratedSymbol(_ctx, sym.str(), replace))
|
||||
return nullptr;
|
||||
|
||||
for (ExportDesc &exp : _ctx->getDllExports())
|
||||
|
@ -232,7 +230,6 @@ public:
|
|||
|
||||
private:
|
||||
std::set<std::string> _exportedSyms;
|
||||
ResolvableSymbols *_syms;
|
||||
mutable llvm::BumpPtrAllocator _alloc;
|
||||
mutable PECOFFLinkingContext *_ctx;
|
||||
};
|
||||
|
@ -244,10 +241,9 @@ private:
|
|||
// http://msdn.microsoft.com/en-us/library/f9t8842e.aspx
|
||||
class EntryPointFile : public SimpleFile {
|
||||
public:
|
||||
EntryPointFile(const PECOFFLinkingContext &ctx,
|
||||
ResolvableSymbols *syms)
|
||||
EntryPointFile(const PECOFFLinkingContext &ctx)
|
||||
: SimpleFile("<entry>"), _ctx(const_cast<PECOFFLinkingContext *>(&ctx)),
|
||||
_syms(syms), _firstTime(true) {}
|
||||
_firstTime(true) {}
|
||||
|
||||
const atom_collection<UndefinedAtom> &undefined() const override {
|
||||
return const_cast<EntryPointFile *>(this)->getUndefinedAtoms();
|
||||
|
@ -277,7 +273,7 @@ private:
|
|||
StringRef opt = _ctx->getEntrySymbolName();
|
||||
if (!opt.empty()) {
|
||||
std::string mangled;
|
||||
if (findDecoratedSymbol(_ctx, _syms, opt, mangled))
|
||||
if (findDecoratedSymbol(_ctx, opt, mangled))
|
||||
return mangled;
|
||||
return _ctx->decorateSymbol(opt);
|
||||
}
|
||||
|
@ -299,10 +295,10 @@ private:
|
|||
// Returns true if a given name exists in an input object file.
|
||||
auto defined = [&](StringRef name) -> bool {
|
||||
StringRef sym = _ctx->decorateSymbol(name);
|
||||
if (_syms->defined().count(sym))
|
||||
if (_ctx->definedSymbols().count(sym))
|
||||
return true;
|
||||
std::string ignore;
|
||||
return findDecoratedSymbol(_ctx, _syms, sym, ignore);
|
||||
return findDecoratedSymbol(_ctx, sym, ignore);
|
||||
};
|
||||
|
||||
switch (_ctx->getSubsystem()) {
|
||||
|
@ -333,7 +329,6 @@ private:
|
|||
PECOFFLinkingContext *_ctx;
|
||||
atom_collection_vector<UndefinedAtom> _undefinedAtoms;
|
||||
std::mutex _mutex;
|
||||
ResolvableSymbols *_syms;
|
||||
llvm::BumpPtrAllocator _alloc;
|
||||
bool _firstTime;
|
||||
};
|
||||
|
|
|
@ -78,6 +78,29 @@ bool PECOFFLinkingContext::validateImpl(raw_ostream &diagnostics) {
|
|||
return true;
|
||||
}
|
||||
|
||||
const std::set<std::string> &PECOFFLinkingContext::definedSymbols() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
for (std::unique_ptr<Node> &node : getNodes()) {
|
||||
if (_seen.count(node.get()) > 0)
|
||||
continue;
|
||||
FileNode *fnode = dyn_cast<FileNode>(node.get());
|
||||
if (!fnode)
|
||||
continue;
|
||||
File *file = fnode->getFile();
|
||||
if (file->parse())
|
||||
continue;
|
||||
if (auto *archive = dyn_cast<ArchiveLibraryFile>(file)) {
|
||||
for (const std::string &sym : archive->getDefinedSymbols())
|
||||
_definedSyms.insert(sym);
|
||||
continue;
|
||||
}
|
||||
for (const DefinedAtom *atom : file->defined())
|
||||
if (!atom->name().empty())
|
||||
_definedSyms.insert(atom->name());
|
||||
}
|
||||
return _definedSyms;
|
||||
}
|
||||
|
||||
std::unique_ptr<File> PECOFFLinkingContext::createEntrySymbolFile() const {
|
||||
return LinkingContext::createEntrySymbolFile("<command line option /entry>");
|
||||
}
|
||||
|
@ -112,12 +135,11 @@ void PECOFFLinkingContext::addLibraryFile(std::unique_ptr<FileNode> file) {
|
|||
|
||||
bool PECOFFLinkingContext::createImplicitFiles(
|
||||
std::vector<std::unique_ptr<File>> &) {
|
||||
pecoff::ResolvableSymbols* syms = getResolvableSymsFile();
|
||||
std::vector<std::unique_ptr<Node>> &members = getNodes();
|
||||
|
||||
// Create a file for the entry point function.
|
||||
std::unique_ptr<FileNode> entry(new FileNode(
|
||||
llvm::make_unique<pecoff::EntryPointFile>(*this, syms)));
|
||||
llvm::make_unique<pecoff::EntryPointFile>(*this)));
|
||||
members.insert(members.begin() + getGroupStartPos(members), std::move(entry));
|
||||
|
||||
// Create a file for __ImageBase.
|
||||
|
@ -132,7 +154,7 @@ bool PECOFFLinkingContext::createImplicitFiles(
|
|||
|
||||
// Create a file for dllexported symbols.
|
||||
std::unique_ptr<FileNode> exportNode(new FileNode(
|
||||
llvm::make_unique<pecoff::ExportedSymbolRenameFile>(*this, syms)));
|
||||
llvm::make_unique<pecoff::ExportedSymbolRenameFile>(*this)));
|
||||
addLibraryFile(std::move(exportNode));
|
||||
|
||||
return true;
|
||||
|
@ -335,29 +357,4 @@ void PECOFFLinkingContext::addPasses(PassManager &pm) {
|
|||
pm.add(std::unique_ptr<Pass>(new pecoff::InferSubsystemPass(*this)));
|
||||
}
|
||||
|
||||
void pecoff::ResolvableSymbols::add(File *file) {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
if (_seen.count(file) > 0)
|
||||
return;
|
||||
_seen.insert(file);
|
||||
_queue.insert(file);
|
||||
}
|
||||
|
||||
void pecoff::ResolvableSymbols::readAllSymbols() {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
for (File *file : _queue) {
|
||||
if (file->parse())
|
||||
return;
|
||||
if (auto *archive = dyn_cast<ArchiveLibraryFile>(file)) {
|
||||
for (const std::string &sym : archive->getDefinedSymbols())
|
||||
_defined.insert(sym);
|
||||
continue;
|
||||
}
|
||||
for (const DefinedAtom *atom : file->defined())
|
||||
if (!atom->name().empty())
|
||||
_defined.insert(atom->name());
|
||||
}
|
||||
_queue.clear();
|
||||
}
|
||||
|
||||
} // end namespace lld
|
||||
|
|
Loading…
Reference in New Issue