diff --git a/llvm/tools/dsymutil/BinaryHolder.h b/llvm/tools/dsymutil/BinaryHolder.h --- a/llvm/tools/dsymutil/BinaryHolder.h +++ b/llvm/tools/dsymutil/BinaryHolder.h @@ -23,6 +23,7 @@ #include "llvm/Support/Chrono.h" #include "llvm/Support/Errc.h" #include "llvm/Support/ErrorOr.h" +#include "llvm/Support/VirtualFileSystem.h" #include @@ -37,7 +38,8 @@ public: using TimestampTy = sys::TimePoint; - BinaryHolder(bool Verbose = false) : Verbose(Verbose) {} + BinaryHolder(IntrusiveRefCntPtr VFS, bool Verbose = false) + : VFS(VFS), Verbose(Verbose) {} // Forward declarations for friend declaration. class ObjectEntry; @@ -55,7 +57,8 @@ class ObjectEntry : public EntryBase { public: /// Load the given object binary in memory. - Error load(StringRef Filename, bool Verbose = false); + Error load(IntrusiveRefCntPtr VFS, StringRef Filename, + bool Verbose = false); /// Access all owned ObjectFiles. std::vector getObjects() const; @@ -106,7 +109,8 @@ }; /// Load the given object binary in memory. - Error load(StringRef Filename, TimestampTy Timestamp, bool Verbose = false); + Error load(IntrusiveRefCntPtr VFS, StringRef Filename, + TimestampTy Timestamp, bool Verbose = false); Expected getObjectEntry(StringRef Filename, TimestampTy Timestamp, @@ -133,6 +137,9 @@ StringMap ObjectCache; std::mutex ObjectCacheMutex; + /// Virtual File System instance. + IntrusiveRefCntPtr VFS; + bool Verbose; }; diff --git a/llvm/tools/dsymutil/BinaryHolder.cpp b/llvm/tools/dsymutil/BinaryHolder.cpp --- a/llvm/tools/dsymutil/BinaryHolder.cpp +++ b/llvm/tools/dsymutil/BinaryHolder.cpp @@ -41,12 +41,15 @@ return Buffers; } -Error BinaryHolder::ArchiveEntry::load(StringRef Filename, +Error BinaryHolder::ArchiveEntry::load(IntrusiveRefCntPtr VFS, + StringRef Filename, TimestampTy Timestamp, bool Verbose) { StringRef ArchiveFilename = getArchiveAndObjectName(Filename).first; // Try to load archive and force it to be memory mapped. - auto ErrOrBuff = MemoryBuffer::getFileOrSTDIN(ArchiveFilename, -1, false); + auto ErrOrBuff = (ArchiveFilename == "-") + ? MemoryBuffer::getSTDIN() + : VFS->getBufferForFile(ArchiveFilename, -1, false); if (auto Err = ErrOrBuff.getError()) return errorCodeToError(Err); @@ -83,9 +86,12 @@ return Error::success(); } -Error BinaryHolder::ObjectEntry::load(StringRef Filename, bool Verbose) { +Error BinaryHolder::ObjectEntry::load(IntrusiveRefCntPtr VFS, + StringRef Filename, bool Verbose) { // Try to load regular binary and force it to be memory mapped. - auto ErrOrBuff = MemoryBuffer::getFileOrSTDIN(Filename, -1, false); + auto ErrOrBuff = (Filename == "-") + ? MemoryBuffer::getSTDIN() + : VFS->getBufferForFile(Filename, -1, false); if (auto Err = ErrOrBuff.getError()) return errorCodeToError(Err); @@ -223,7 +229,7 @@ Verbose); } else { ArchiveEntry &AE = ArchiveCache[ArchiveFilename]; - auto Err = AE.load(Filename, Timestamp, Verbose); + auto Err = AE.load(VFS, Filename, Timestamp, Verbose); if (Err) { ArchiveCache.erase(ArchiveFilename); // Don't return the error here: maybe the file wasn't an archive. @@ -240,7 +246,7 @@ std::lock_guard Lock(ObjectCacheMutex); if (!ObjectCache.count(Filename)) { ObjectEntry &OE = ObjectCache[Filename]; - auto Err = OE.load(Filename, Verbose); + auto Err = OE.load(VFS, Filename, Verbose); if (Err) { ObjectCache.erase(Filename); return std::move(Err); diff --git a/llvm/tools/dsymutil/DebugMap.cpp b/llvm/tools/dsymutil/DebugMap.cpp --- a/llvm/tools/dsymutil/DebugMap.cpp +++ b/llvm/tools/dsymutil/DebugMap.cpp @@ -234,7 +234,7 @@ dsymutil::DebugMapObject MappingTraits::YamlDMO::denormalize(IO &IO) { - BinaryHolder BinHolder(/* Verbose =*/false); + BinaryHolder BinHolder(vfs::getRealFileSystem(), /* Verbose =*/false); const auto &Ctxt = *reinterpret_cast(IO.getContext()); SmallString<80> Path(Ctxt.PrependPath); StringMap SymbolAddresses; diff --git a/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp b/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp --- a/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp +++ b/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp @@ -455,8 +455,8 @@ if (Map.getTriple().isOSDarwin() && !Map.getBinaryPath().empty() && Options.FileType == OutputFileType::Object) return MachOUtils::generateDsymCompanion( - Map, Options.Translator, *Streamer->getAsmPrinter().OutStreamer, - OutFile); + Options.VFS, Map, Options.Translator, + *Streamer->getAsmPrinter().OutStreamer, OutFile); Streamer->finish(); return true; diff --git a/llvm/tools/dsymutil/LinkUtils.h b/llvm/tools/dsymutil/LinkUtils.h --- a/llvm/tools/dsymutil/LinkUtils.h +++ b/llvm/tools/dsymutil/LinkUtils.h @@ -13,6 +13,7 @@ #include "llvm/ADT/Twine.h" #include "llvm/Remarks/RemarkFormat.h" +#include "llvm/Support/VirtualFileSystem.h" #include "llvm/Support/WithColor.h" #include "llvm/DWARFLinker/DWARFLinker.h" @@ -62,6 +63,10 @@ /// Symbol map translator. SymbolMapTranslator Translator; + /// Virtual File System. + llvm::IntrusiveRefCntPtr VFS = + vfs::getRealFileSystem(); + /// Fields used for linking and placing remarks into the .dSYM bundle. /// @{ diff --git a/llvm/tools/dsymutil/MachODebugMapParser.cpp b/llvm/tools/dsymutil/MachODebugMapParser.cpp --- a/llvm/tools/dsymutil/MachODebugMapParser.cpp +++ b/llvm/tools/dsymutil/MachODebugMapParser.cpp @@ -23,12 +23,13 @@ class MachODebugMapParser { public: - MachODebugMapParser(StringRef BinaryPath, ArrayRef Archs, + MachODebugMapParser(llvm::IntrusiveRefCntPtr VFS, + StringRef BinaryPath, ArrayRef Archs, StringRef PathPrefix = "", bool PaperTrailWarnings = false, bool Verbose = false) : BinaryPath(std::string(BinaryPath)), Archs(Archs.begin(), Archs.end()), PathPrefix(std::string(PathPrefix)), - PaperTrailWarnings(PaperTrailWarnings), BinHolder(Verbose), + PaperTrailWarnings(PaperTrailWarnings), BinHolder(VFS, Verbose), CurrentDebugMapObject(nullptr) {} /// Parses and returns the DebugMaps of the input binary. The binary contains @@ -190,7 +191,8 @@ StringRef BinaryPath) { loadMainBinarySymbols(MainBinary); ArrayRef UUID = MainBinary.getUuid(); - Result = std::make_unique(MainBinary.getArchTriple(), BinaryPath, UUID); + Result = + std::make_unique(MainBinary.getArchTriple(), BinaryPath, UUID); MainBinaryStrings = MainBinary.getStringTableData(); for (const SymbolRef &Symbol : MainBinary.symbols()) { const DataRefImpl &DRI = Symbol.getRawDataRefImpl(); @@ -583,20 +585,22 @@ namespace llvm { namespace dsymutil { llvm::ErrorOr>> -parseDebugMap(StringRef InputFile, ArrayRef Archs, +parseDebugMap(llvm::IntrusiveRefCntPtr VFS, + StringRef InputFile, ArrayRef Archs, StringRef PrependPath, bool PaperTrailWarnings, bool Verbose, bool InputIsYAML) { if (InputIsYAML) return DebugMap::parseYAMLDebugMap(InputFile, PrependPath, Verbose); - MachODebugMapParser Parser(InputFile, Archs, PrependPath, PaperTrailWarnings, - Verbose); + MachODebugMapParser Parser(VFS, InputFile, Archs, PrependPath, + PaperTrailWarnings, Verbose); return Parser.parse(); } -bool dumpStab(StringRef InputFile, ArrayRef Archs, +bool dumpStab(llvm::IntrusiveRefCntPtr VFS, + StringRef InputFile, ArrayRef Archs, StringRef PrependPath) { - MachODebugMapParser Parser(InputFile, Archs, PrependPath, false); + MachODebugMapParser Parser(VFS, InputFile, Archs, PrependPath, false); return Parser.dumpStab(); } } // namespace dsymutil diff --git a/llvm/tools/dsymutil/MachOUtils.h b/llvm/tools/dsymutil/MachOUtils.h --- a/llvm/tools/dsymutil/MachOUtils.h +++ b/llvm/tools/dsymutil/MachOUtils.h @@ -12,6 +12,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Support/VirtualFileSystem.h" #include @@ -40,7 +41,8 @@ StringRef OutputFileName, const LinkOptions &, StringRef SDKPath); -bool generateDsymCompanion(const DebugMap &DM, SymbolMapTranslator &Translator, +bool generateDsymCompanion(llvm::IntrusiveRefCntPtr VFS, + const DebugMap &DM, SymbolMapTranslator &Translator, MCStreamer &MS, raw_fd_ostream &OutFile); std::string getArchName(StringRef Arch); diff --git a/llvm/tools/dsymutil/MachOUtils.cpp b/llvm/tools/dsymutil/MachOUtils.cpp --- a/llvm/tools/dsymutil/MachOUtils.cpp +++ b/llvm/tools/dsymutil/MachOUtils.cpp @@ -332,7 +332,8 @@ // Stream a dSYM companion binary file corresponding to the binary referenced // by \a DM to \a OutFile. The passed \a MS MCStreamer is setup to write to // \a OutFile and it must be using a MachObjectWriter object to do so. -bool generateDsymCompanion(const DebugMap &DM, SymbolMapTranslator &Translator, +bool generateDsymCompanion(llvm::IntrusiveRefCntPtr VFS, + const DebugMap &DM, SymbolMapTranslator &Translator, MCStreamer &MS, raw_fd_ostream &OutFile) { auto &ObjectStreamer = static_cast(MS); MCAssembler &MCAsm = ObjectStreamer.getAssembler(); @@ -343,7 +344,7 @@ MCAsmLayout Layout(MCAsm); MCAsm.layout(Layout); - BinaryHolder InputBinaryHolder(false); + BinaryHolder InputBinaryHolder(VFS, false); auto ObjectEntry = InputBinaryHolder.getObjectEntry(DM.getBinaryPath()); if (!ObjectEntry) { diff --git a/llvm/tools/dsymutil/dsymutil.h b/llvm/tools/dsymutil/dsymutil.h --- a/llvm/tools/dsymutil/dsymutil.h +++ b/llvm/tools/dsymutil/dsymutil.h @@ -35,12 +35,14 @@ /// The file has to be a MachO object file. Multiple debug maps can be /// returned when the file is universal (aka fat) binary. ErrorOr>> -parseDebugMap(StringRef InputFile, ArrayRef Archs, +parseDebugMap(llvm::IntrusiveRefCntPtr VFS, + StringRef InputFile, ArrayRef Archs, StringRef PrependPath, bool PaperTrailWarnings, bool Verbose, bool InputIsYAML); -/// Dump the symbol table -bool dumpStab(StringRef InputFile, ArrayRef Archs, +/// Dump the symbol table. +bool dumpStab(llvm::IntrusiveRefCntPtr VFS, + StringRef InputFile, ArrayRef Archs, StringRef PrependPath = ""); /// Link the Dwarf debug info as directed by the passed DebugMap \p DM into a diff --git a/llvm/tools/dsymutil/dsymutil.cpp b/llvm/tools/dsymutil/dsymutil.cpp --- a/llvm/tools/dsymutil/dsymutil.cpp +++ b/llvm/tools/dsymutil/dsymutil.cpp @@ -510,15 +510,16 @@ for (auto &InputFile : Options.InputFiles) { // Dump the symbol table for each input file and requested arch if (Options.DumpStab) { - if (!dumpStab(InputFile, Options.Archs, Options.LinkOpts.PrependPath)) + if (!dumpStab(Options.LinkOpts.VFS, InputFile, Options.Archs, + Options.LinkOpts.PrependPath)) return 1; continue; } auto DebugMapPtrsOrErr = - parseDebugMap(InputFile, Options.Archs, Options.LinkOpts.PrependPath, - Options.PaperTrailWarnings, Options.LinkOpts.Verbose, - Options.InputIsYAMLDebugMap); + parseDebugMap(Options.LinkOpts.VFS, InputFile, Options.Archs, + Options.LinkOpts.PrependPath, Options.PaperTrailWarnings, + Options.LinkOpts.Verbose, Options.InputIsYAMLDebugMap); if (auto EC = DebugMapPtrsOrErr.getError()) { WithColor::error() << "cannot parse the debug map for '" << InputFile @@ -545,7 +546,7 @@ } // Shared a single binary holder for all the link steps. - BinaryHolder BinHolder; + BinaryHolder BinHolder(Options.LinkOpts.VFS); ThreadPoolStrategy S = hardware_concurrency(Options.LinkOpts.Threads); if (Options.LinkOpts.Threads == 0) {