Skip to content

Commit 2ccbea1

Browse files
authored
[dsymutil] Share one BinaryHolder between debug map parsing & linking (#113234)
I (re)discovered that dsymutil was instantiating two BinaryHolders: one for parsing the debug map and one for linking. That really defeats the purpose of the BinaryHolder as it serves as a cache. Fix the issue and remove an old FIXME.
1 parent 91fd1b4 commit 2ccbea1

File tree

5 files changed

+34
-42
lines changed

5 files changed

+34
-42
lines changed

llvm/tools/dsymutil/DebugMap.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,23 @@ void DebugMap::dump() const { print(errs()); }
126126
namespace {
127127

128128
struct YAMLContext {
129+
YAMLContext(BinaryHolder &BinHolder, StringRef PrependPath)
130+
: BinHolder(BinHolder), PrependPath(PrependPath) {}
131+
BinaryHolder &BinHolder;
129132
StringRef PrependPath;
130133
Triple BinaryTriple;
131134
};
132135

133136
} // end anonymous namespace
134137

135138
ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
136-
DebugMap::parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath,
137-
bool Verbose) {
139+
DebugMap::parseYAMLDebugMap(BinaryHolder &BinHolder, StringRef InputFile,
140+
StringRef PrependPath, bool Verbose) {
138141
auto ErrOrFile = MemoryBuffer::getFileOrSTDIN(InputFile);
139142
if (auto Err = ErrOrFile.getError())
140143
return Err;
141144

142-
YAMLContext Ctxt;
143-
144-
Ctxt.PrependPath = PrependPath;
145+
YAMLContext Ctxt(BinHolder, PrependPath);
145146

146147
std::unique_ptr<DebugMap> Res;
147148
yaml::Input yin((*ErrOrFile)->getBuffer(), &Ctxt);
@@ -244,14 +245,13 @@ MappingTraits<dsymutil::DebugMapObject>::YamlDMO::YamlDMO(
244245

245246
dsymutil::DebugMapObject
246247
MappingTraits<dsymutil::DebugMapObject>::YamlDMO::denormalize(IO &IO) {
247-
BinaryHolder BinHolder(vfs::getRealFileSystem(), /* Verbose =*/false);
248248
const auto &Ctxt = *reinterpret_cast<YAMLContext *>(IO.getContext());
249249
SmallString<80> Path(Ctxt.PrependPath);
250250
StringMap<uint64_t> SymbolAddresses;
251251

252252
sys::path::append(Path, Filename);
253253

254-
auto ObjectEntry = BinHolder.getObjectEntry(Path);
254+
auto ObjectEntry = Ctxt.BinHolder.getObjectEntry(Path);
255255
if (!ObjectEntry) {
256256
auto Err = ObjectEntry.takeError();
257257
WithColor::warning() << "Unable to open " << Path << " "

llvm/tools/dsymutil/DebugMap.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#ifndef LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H
2222
#define LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H
2323

24+
#include "BinaryHolder.h"
2425
#include "RelocationMap.h"
2526
#include "llvm/ADT/DenseMap.h"
2627
#include "llvm/ADT/StringMap.h"
@@ -127,7 +128,8 @@ class DebugMap {
127128

128129
/// Read a debug map for \a InputFile.
129130
static ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
130-
parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath, bool Verbose);
131+
parseYAMLDebugMap(BinaryHolder &BinHolder, StringRef InputFile,
132+
StringRef PrependPath, bool Verbose);
131133
};
132134

133135
/// The DebugMapObject represents one object file described by the DebugMap. It

llvm/tools/dsymutil/MachODebugMapParser.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ using namespace llvm::object;
2727

2828
class MachODebugMapParser {
2929
public:
30-
MachODebugMapParser(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
31-
StringRef BinaryPath, ArrayRef<std::string> Archs,
30+
MachODebugMapParser(BinaryHolder &BinHolder, StringRef BinaryPath,
31+
ArrayRef<std::string> Archs,
3232
ArrayRef<std::string> DSYMSearchPaths,
3333
StringRef PathPrefix = "", StringRef VariantSuffix = "",
3434
bool Verbose = false)
3535
: BinaryPath(std::string(BinaryPath)), Archs(Archs),
3636
DSYMSearchPaths(DSYMSearchPaths), PathPrefix(std::string(PathPrefix)),
37-
VariantSuffix(std::string(VariantSuffix)), BinHolder(VFS, Verbose),
37+
VariantSuffix(std::string(VariantSuffix)), BinHolder(BinHolder),
3838
CurrentDebugMapObject(nullptr), SkipDebugMapObject(false) {}
3939

4040
/// Parses and returns the DebugMaps of the input binary. The binary contains
@@ -56,7 +56,7 @@ class MachODebugMapParser {
5656
std::string VariantSuffix;
5757

5858
/// Owns the MemoryBuffer for the main binary.
59-
BinaryHolder BinHolder;
59+
BinaryHolder &BinHolder;
6060
/// Map of the binary symbol addresses.
6161
StringMap<uint64_t> MainBinarySymbolAddresses;
6262
StringRef MainBinaryStrings;
@@ -854,24 +854,25 @@ void MachODebugMapParser::loadMainBinarySymbols(
854854
namespace llvm {
855855
namespace dsymutil {
856856
llvm::ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
857-
parseDebugMap(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
858-
StringRef InputFile, ArrayRef<std::string> Archs,
857+
parseDebugMap(BinaryHolder &BinHolder, StringRef InputFile,
858+
ArrayRef<std::string> Archs,
859859
ArrayRef<std::string> DSYMSearchPaths, StringRef PrependPath,
860860
StringRef VariantSuffix, bool Verbose, bool InputIsYAML) {
861861
if (InputIsYAML)
862-
return DebugMap::parseYAMLDebugMap(InputFile, PrependPath, Verbose);
862+
return DebugMap::parseYAMLDebugMap(BinHolder, InputFile, PrependPath,
863+
Verbose);
863864

864-
MachODebugMapParser Parser(VFS, InputFile, Archs, DSYMSearchPaths,
865+
MachODebugMapParser Parser(BinHolder, InputFile, Archs, DSYMSearchPaths,
865866
PrependPath, VariantSuffix, Verbose);
866867

867868
return Parser.parse();
868869
}
869870

870-
bool dumpStab(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
871-
StringRef InputFile, ArrayRef<std::string> Archs,
871+
bool dumpStab(BinaryHolder &BinHolder, StringRef InputFile,
872+
ArrayRef<std::string> Archs,
872873
ArrayRef<std::string> DSYMSearchPaths, StringRef PrependPath,
873874
StringRef VariantSuffix) {
874-
MachODebugMapParser Parser(VFS, InputFile, Archs, DSYMSearchPaths,
875+
MachODebugMapParser Parser(BinHolder, InputFile, Archs, DSYMSearchPaths,
875876
PrependPath, VariantSuffix, false);
876877
return Parser.dumpStab();
877878
}

llvm/tools/dsymutil/dsymutil.cpp

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,6 @@ static Error verifyOptions(const DsymutilOptions &Options) {
180180
errc::invalid_argument);
181181
}
182182

183-
if (Options.LinkOpts.Update && llvm::is_contained(Options.InputFiles, "-")) {
184-
// FIXME: We cannot use stdin for an update because stdin will be
185-
// consumed by the BinaryHolder during the debugmap parsing, and
186-
// then we will want to consume it again in DwarfLinker. If we
187-
// used a unique BinaryHolder object that could cache multiple
188-
// binaries this restriction would go away.
189-
return make_error<StringError>(
190-
"standard input cannot be used as input for a dSYM update.",
191-
errc::invalid_argument);
192-
}
193-
194183
if (!Options.Flat && Options.OutputFile == "-")
195184
return make_error<StringError>(
196185
"cannot emit to standard output without --flat.",
@@ -674,9 +663,12 @@ int dsymutil_main(int argc, char **argv, const llvm::ToolContext &) {
674663
}
675664

676665
for (auto &InputFile : Options.InputFiles) {
666+
// Shared a single binary holder for all the link steps.
667+
BinaryHolder BinHolder(Options.LinkOpts.VFS, Options.LinkOpts.Verbose);
668+
677669
// Dump the symbol table for each input file and requested arch
678670
if (Options.DumpStab) {
679-
if (!dumpStab(Options.LinkOpts.VFS, InputFile, Options.Archs,
671+
if (!dumpStab(BinHolder, InputFile, Options.Archs,
680672
Options.LinkOpts.DSYMSearchPaths,
681673
Options.LinkOpts.PrependPath,
682674
Options.LinkOpts.BuildVariantSuffix))
@@ -685,10 +677,9 @@ int dsymutil_main(int argc, char **argv, const llvm::ToolContext &) {
685677
}
686678

687679
auto DebugMapPtrsOrErr = parseDebugMap(
688-
Options.LinkOpts.VFS, InputFile, Options.Archs,
689-
Options.LinkOpts.DSYMSearchPaths, Options.LinkOpts.PrependPath,
690-
Options.LinkOpts.BuildVariantSuffix, Options.LinkOpts.Verbose,
691-
Options.InputIsYAMLDebugMap);
680+
BinHolder, InputFile, Options.Archs, Options.LinkOpts.DSYMSearchPaths,
681+
Options.LinkOpts.PrependPath, Options.LinkOpts.BuildVariantSuffix,
682+
Options.LinkOpts.Verbose, Options.InputIsYAMLDebugMap);
692683

693684
if (auto EC = DebugMapPtrsOrErr.getError()) {
694685
WithColor::error() << "cannot parse the debug map for '" << InputFile
@@ -714,9 +705,6 @@ int dsymutil_main(int argc, char **argv, const llvm::ToolContext &) {
714705
return EXIT_FAILURE;
715706
}
716707

717-
// Shared a single binary holder for all the link steps.
718-
BinaryHolder BinHolder(Options.LinkOpts.VFS);
719-
720708
// Compute the output location and update the resource directory.
721709
Expected<OutputLocation> OutputLocationOrErr =
722710
getOutputFileName(InputFile, Options);

llvm/tools/dsymutil/dsymutil.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#ifndef LLVM_TOOLS_DSYMUTIL_DSYMUTIL_H
1717
#define LLVM_TOOLS_DSYMUTIL_DSYMUTIL_H
1818

19+
#include "BinaryHolder.h"
1920
#include "DebugMap.h"
2021
#include "LinkUtils.h"
2122
#include "llvm/ADT/ArrayRef.h"
@@ -33,14 +34,14 @@ namespace dsymutil {
3334
/// The file has to be a MachO object file. Multiple debug maps can be
3435
/// returned when the file is universal (aka fat) binary.
3536
ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
36-
parseDebugMap(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
37-
StringRef InputFile, ArrayRef<std::string> Archs,
37+
parseDebugMap(BinaryHolder &BinHolder, StringRef InputFile,
38+
ArrayRef<std::string> Archs,
3839
ArrayRef<std::string> DSYMSearchPaths, StringRef PrependPath,
3940
StringRef VariantSuffix, bool Verbose, bool InputIsYAML);
4041

4142
/// Dump the symbol table.
42-
bool dumpStab(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
43-
StringRef InputFile, ArrayRef<std::string> Archs,
43+
bool dumpStab(BinaryHolder &BinHolder, StringRef InputFile,
44+
ArrayRef<std::string> Archs,
4445
ArrayRef<std::string> DSYMSearchPaths, StringRef PrependPath = "",
4546
StringRef VariantSuffix = "");
4647

0 commit comments

Comments
 (0)