diff --git a/llvm/include/llvm/Object/MachO.h b/llvm/include/llvm/Object/MachO.h --- a/llvm/include/llvm/Object/MachO.h +++ b/llvm/include/llvm/Object/MachO.h @@ -652,6 +652,12 @@ return std::string(std::string(Version.str())); } + /// Returns true if the input path is a .dSYM bundle (as created by the + /// dsymutil tool) and populates `BinaryPaths` with paths to the object files + /// that are inside the bundle. + static Expected isDsymBundle(const StringRef Path, + std::vector &BinaryPaths); + private: MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits, Error &Err, uint32_t UniversalCputype = 0, diff --git a/llvm/lib/Object/MachOObjectFile.cpp b/llvm/lib/Object/MachOObjectFile.cpp --- a/llvm/lib/Object/MachOObjectFile.cpp +++ b/llvm/lib/Object/MachOObjectFile.cpp @@ -28,10 +28,12 @@ #include "llvm/Support/Debug.h" #include "llvm/Support/Error.h" #include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/FileSystem.h" #include "llvm/Support/Format.h" #include "llvm/Support/Host.h" #include "llvm/Support/LEB128.h" #include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/Path.h" #include "llvm/Support/SwapByteOrder.h" #include "llvm/Support/raw_ostream.h" #include @@ -4719,3 +4721,37 @@ .Case("debug_str_offs", "debug_str_offsets") .Default(Name); } + +Expected +MachOObjectFile::isDsymBundle(const StringRef Path, + std::vector &BinaryPaths) { + SmallString<256> BundlePath(Path); + // Normalize input path. This is necessary to accept `bundle.dSYM/`. + sys::path::remove_dots(BundlePath); + if (!sys::fs::is_directory(BundlePath) || + sys::path::extension(BundlePath) != ".dSYM") + return false; + sys::path::append(BundlePath, "Contents", "Resources", "DWARF"); + + bool FoundBinary = false; + std::error_code EC; + for (sys::fs::directory_iterator Dir(BundlePath, EC), DirEnd; + Dir != DirEnd && !EC; Dir.increment(EC)) { + const std::string &Path = Dir->path(); + sys::fs::file_status Status; + if (auto EC = sys::fs::status(Path, Status)) + return errorCodeToError(EC); + switch (Status.type()) { + case sys::fs::file_type::regular_file: + case sys::fs::file_type::symlink_file: + case sys::fs::file_type::type_unknown: + FoundBinary = true; + BinaryPaths.push_back(Path); + break; + default: /*ignore*/; + } + } + if (EC) + return errorCodeToError(EC); + return FoundBinary; +} diff --git a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp --- a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp +++ b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp @@ -24,7 +24,6 @@ #include "llvm/Support/Format.h" #include "llvm/Support/InitLLVM.h" #include "llvm/Support/MemoryBuffer.h" -#include "llvm/Support/Path.h" #include "llvm/Support/Regex.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Support/ToolOutputFile.h" @@ -581,41 +580,6 @@ return handleBuffer(Filename, *Buffer, HandleObj, OS); } -/// If the input path is a .dSYM bundle (as created by the dsymutil tool), -/// replace it with individual entries for each of the object files inside the -/// bundle otherwise return the input path. -static std::vector expandBundle(const std::string &InputPath) { - std::vector BundlePaths; - SmallString<256> BundlePath(InputPath); - // Normalize input path. This is necessary to accept `bundle.dSYM/`. - sys::path::remove_dots(BundlePath); - // Manually open up the bundle to avoid introducing additional dependencies. - if (sys::fs::is_directory(BundlePath) && - sys::path::extension(BundlePath) == ".dSYM") { - std::error_code EC; - sys::path::append(BundlePath, "Contents", "Resources", "DWARF"); - for (sys::fs::directory_iterator Dir(BundlePath, EC), DirEnd; - Dir != DirEnd && !EC; Dir.increment(EC)) { - const std::string &Path = Dir->path(); - sys::fs::file_status Status; - EC = sys::fs::status(Path, Status); - error(Path, EC); - switch (Status.type()) { - case sys::fs::file_type::regular_file: - case sys::fs::file_type::symlink_file: - case sys::fs::file_type::type_unknown: - BundlePaths.push_back(Path); - break; - default: /*ignore*/; - } - } - error(BundlePath, EC); - } - if (!BundlePaths.size()) - BundlePaths.push_back(InputPath); - return BundlePaths; -} - int main(int argc, char **argv) { InitLLVM X(argc, argv); @@ -684,8 +648,11 @@ // Expand any .dSYM bundles to the individual object files contained therein. std::vector Objects; for (const auto &F : InputFilenames) { - auto Objs = expandBundle(F); - llvm::append_range(Objects, Objs); + auto IsDsymOrErr = MachOObjectFile::isDsymBundle(F, Objects); + if (auto Err = IsDsymOrErr.takeError()) + error(F, std::move(Err)); + if (!*IsDsymOrErr) + Objects.push_back(F); } bool Success = true; diff --git a/llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp b/llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp --- a/llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp +++ b/llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp @@ -20,7 +20,6 @@ #include "llvm/Support/Format.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/MemoryBuffer.h" -#include "llvm/Support/Path.h" #include "llvm/Support/PrettyStackTrace.h" #include "llvm/Support/Regex.h" #include "llvm/Support/Signals.h" @@ -141,39 +140,6 @@ exit(1); } -/// If the input path is a .dSYM bundle (as created by the dsymutil tool), -/// replace it with individual entries for each of the object files inside the -/// bundle otherwise return the input path. -static std::vector expandBundle(const std::string &InputPath) { - std::vector BundlePaths; - SmallString<256> BundlePath(InputPath); - // Manually open up the bundle to avoid introducing additional dependencies. - if (sys::fs::is_directory(BundlePath) && - sys::path::extension(BundlePath) == ".dSYM") { - std::error_code EC; - sys::path::append(BundlePath, "Contents", "Resources", "DWARF"); - for (sys::fs::directory_iterator Dir(BundlePath, EC), DirEnd; - Dir != DirEnd && !EC; Dir.increment(EC)) { - const std::string &Path = Dir->path(); - sys::fs::file_status Status; - EC = sys::fs::status(Path, Status); - error(Path, EC); - switch (Status.type()) { - case sys::fs::file_type::regular_file: - case sys::fs::file_type::symlink_file: - case sys::fs::file_type::type_unknown: - BundlePaths.push_back(Path); - break; - default: /*ignore*/; - } - } - error(BundlePath, EC); - } - if (!BundlePaths.size()) - BundlePaths.push_back(InputPath); - return BundlePaths; -} - static uint32_t getCPUType(MachOObjectFile &MachO) { if (MachO.is64Bit()) return MachO.getHeader64().cputype; @@ -420,8 +386,11 @@ OS << "Input file: " << ConvertFilename << "\n"; - auto Objs = expandBundle(ConvertFilename); - llvm::append_range(Objects, Objs); + auto IsDsymOrErr = MachOObjectFile::isDsymBundle(ConvertFilename, Objects); + if (auto Err = IsDsymOrErr.takeError()) + error(ConvertFilename, std::move(Err)); + if (!*IsDsymOrErr) + Objects.push_back(ConvertFilename); for (auto Object : Objects) { if (auto Err = handleFileConversionToGSYM(Object, OutFile))