diff --git a/lld/lib/ReaderWriter/PECOFF/CMakeLists.txt b/lld/lib/ReaderWriter/PECOFF/CMakeLists.txt index d0fde5f4e7aa..d6112462d1ad 100644 --- a/lld/lib/ReaderWriter/PECOFF/CMakeLists.txt +++ b/lld/lib/ReaderWriter/PECOFF/CMakeLists.txt @@ -1,6 +1,7 @@ add_lld_library(lldPECOFF EdataPass.cpp IdataPass.cpp + LinkerGeneratedSymbolFile.cpp LoadConfigPass.cpp PECOFFLinkingContext.cpp Pass.cpp diff --git a/lld/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.cpp b/lld/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.cpp new file mode 100644 index 000000000000..9825912a2ae7 --- /dev/null +++ b/lld/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.cpp @@ -0,0 +1,48 @@ +//===- lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.cpp --------------===// +// +// The LLVM Linker +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "LinkerGeneratedSymbolFile.h" + +namespace lld { +namespace pecoff { + +// Find decorated symbol, namely /sym@[0-9]+/ or /\?sym@@.+/. +bool findDecoratedSymbol(PECOFFLinkingContext *ctx, ResolvableSymbols *syms, + std::string sym, std::string &res) { + const std::set &defined = syms->defined(); + // Search for /sym@[0-9]+/ + { + std::string s = sym + '@'; + auto it = defined.lower_bound(s); + for (auto e = defined.end(); it != e; ++it) { + if (!StringRef(*it).startswith(s)) + break; + if (it->size() == s.size()) + continue; + StringRef suffix = StringRef(*it).substr(s.size()); + if (suffix.find_first_not_of("0123456789") != StringRef::npos) + continue; + res = *it; + return true; + } + } + // Search for /\?sym@@.+/ + { + std::string s = "?" + ctx->undecorateSymbol(sym).str() + "@@"; + auto it = defined.lower_bound(s); + if (it != defined.end() && StringRef(*it).startswith(s)) { + res = *it; + return true; + } + } + return false; +} + +} // namespace pecoff +} // namespace lld diff --git a/lld/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h b/lld/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h index 0b02b8ef1fb5..33667ad2b200 100644 --- a/lld/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h +++ b/lld/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h @@ -1,4 +1,4 @@ -//===- lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.cpp --------------===// +//===- lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h ----------------===// // // The LLVM Linker // @@ -21,6 +21,10 @@ using llvm::COFF::WindowsSubsystem; namespace lld { namespace pecoff { +class ResolvableSymbols; +bool findDecoratedSymbol(PECOFFLinkingContext *ctx, ResolvableSymbols *syms, + std::string sym, std::string &res); + namespace impl { /// The defined atom for dllexported symbols with __imp_ prefix. @@ -255,7 +259,7 @@ public: if (it == _exportedSyms.end()) return nullptr; std::string replace; - if (!findDecoratedSymbol(sym.str(), replace)) + if (!findDecoratedSymbol(_ctx, _syms.get(), sym.str(), replace)) return nullptr; it->second->name = replace; if (_ctx->deadStrip()) @@ -264,37 +268,6 @@ public: } private: - // Find decorated symbol, namely /sym@[0-9]+/ or /\?sym@@.+/. - bool findDecoratedSymbol(std::string sym, std::string &res) const { - const std::set &defined = _syms->defined(); - // Search for /sym@[0-9]+/ - { - std::string s = sym + '@'; - auto it = defined.lower_bound(s); - for (auto e = defined.end(); it != e; ++it) { - if (!StringRef(*it).startswith(s)) - break; - if (it->size() == s.size()) - continue; - StringRef suffix = StringRef(*it).substr(s.size()); - if (suffix.find_first_not_of("0123456789") != StringRef::npos) - continue; - res = *it; - return true; - } - } - // Search for /\?sym@@.+/ - { - std::string s = "?" + _ctx->undecorateSymbol(sym).str() + "@@"; - auto it = defined.lower_bound(s); - if (it != defined.end() && StringRef(*it).startswith(s)) { - res = *it; - return true; - } - } - return false; - } - std::map _exportedSyms; std::shared_ptr _syms; mutable llvm::BumpPtrAllocator _alloc;