[llvm] b68061a - [dwarf][NFC] Move expandBundle() to MachO.h
Ellis Hoag via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 15 19:09:01 PST 2021
Author: Ellis Hoag
Date: 2021-12-15T19:08:56-08:00
New Revision: b68061a3f7338f007199609d2cf65eb0244c1afe
URL: https://github.com/llvm/llvm-project/commit/b68061a3f7338f007199609d2cf65eb0244c1afe
DIFF: https://github.com/llvm/llvm-project/commit/b68061a3f7338f007199609d2cf65eb0244c1afe.diff
LOG: [dwarf][NFC] Move expandBundle() to MachO.h
The function `expandBundle()` is defined both in `llvm-dwarfdump.cpp` and
`llvm-gsymutil.cpp` in the exact same way. Reduce code duplication by
moving this function to the `MachOObjectFile` class.
Reviewed By: jhenderson, clayborg
Differential Revision: https://reviews.llvm.org/D115418
Added:
Modified:
llvm/include/llvm/Object/MachO.h
llvm/lib/Object/MachOObjectFile.cpp
llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Object/MachO.h b/llvm/include/llvm/Object/MachO.h
index ca5d63e4074f0..ede742c47f971 100644
--- a/llvm/include/llvm/Object/MachO.h
+++ b/llvm/include/llvm/Object/MachO.h
@@ -652,6 +652,13 @@ class MachOObjectFile : public ObjectFile {
return std::string(std::string(Version.str()));
}
+ /// If the input path is a .dSYM bundle (as created by the dsymutil tool),
+ /// return the paths to the object files found in the bundle, otherwise return
+ /// an empty vector. If the path appears to be a .dSYM bundle but no objects
+ /// were found or there was a filesystem error, then return an error.
+ static Expected<std::vector<std::string>>
+ findDsymObjectMembers(StringRef Path);
+
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
index 7501661591f06..42e257516f4e0 100644
--- a/llvm/lib/Object/MachOObjectFile.cpp
+++ b/llvm/lib/Object/MachOObjectFile.cpp
@@ -26,12 +26,15 @@
#include "llvm/Object/SymbolicFile.h"
#include "llvm/Support/DataExtractor.h"
#include "llvm/Support/Debug.h"
+#include "llvm/Support/Errc.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 <algorithm>
@@ -4719,3 +4722,46 @@ StringRef MachOObjectFile::mapDebugSectionName(StringRef Name) const {
.Case("debug_str_offs", "debug_str_offsets")
.Default(Name);
}
+
+Expected<std::vector<std::string>>
+MachOObjectFile::findDsymObjectMembers(StringRef Path) {
+ 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 std::vector<std::string>();
+ sys::path::append(BundlePath, "Contents", "Resources", "DWARF");
+ bool IsDir;
+ auto EC = sys::fs::is_directory(BundlePath, IsDir);
+ if (EC == errc::no_such_file_or_directory || (!EC && !IsDir))
+ return createStringError(
+ EC, "%s: expected directory 'Contents/Resources/DWARF' in dSYM bundle",
+ Path.str().c_str());
+ if (EC)
+ return createFileError(BundlePath, errorCodeToError(EC));
+
+ std::vector<std::string> ObjectPaths;
+ for (sys::fs::directory_iterator Dir(BundlePath, EC), DirEnd;
+ Dir != DirEnd && !EC; Dir.increment(EC)) {
+ StringRef ObjectPath = Dir->path();
+ sys::fs::file_status Status;
+ if (auto EC = sys::fs::status(ObjectPath, Status))
+ return createFileError(ObjectPath, 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:
+ ObjectPaths.push_back(ObjectPath.str());
+ break;
+ default: /*ignore*/;
+ }
+ }
+ if (EC)
+ return createFileError(BundlePath, errorCodeToError(EC));
+ if (ObjectPaths.empty())
+ return createStringError(std::error_code(),
+ "%s: no objects found in dSYM bundle",
+ Path.str().c_str());
+ return ObjectPaths;
+}
diff --git a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
index 957755a0a4a08..9c2ddc3867a58 100644
--- 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"
@@ -265,6 +264,13 @@ static cl::extrahelp
/// @}
//===----------------------------------------------------------------------===//
+static void error(Error Err) {
+ if (!Err)
+ return;
+ WithColor::error() << toString(std::move(Err)) << "\n";
+ exit(1);
+}
+
static void error(StringRef Prefix, Error Err) {
if (!Err)
return;
@@ -583,41 +589,6 @@ static bool handleFile(StringRef Filename, HandlerFn HandleObj,
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<std::string> expandBundle(const std::string &InputPath) {
- std::vector<std::string> 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);
@@ -686,8 +657,14 @@ int main(int argc, char **argv) {
// Expand any .dSYM bundles to the individual object files contained therein.
std::vector<std::string> Objects;
for (const auto &F : InputFilenames) {
- auto Objs = expandBundle(F);
- llvm::append_range(Objects, Objs);
+ if (auto DsymObjectsOrErr = MachOObjectFile::findDsymObjectMembers(F)) {
+ if (DsymObjectsOrErr->empty())
+ Objects.push_back(F);
+ else
+ llvm::append_range(Objects, *DsymObjectsOrErr);
+ } else {
+ error(DsymObjectsOrErr.takeError());
+ }
}
bool Success = true;
diff --git a/llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp b/llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp
index 83c83197118d5..4e3c06e1e5fd7 100644
--- 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"
@@ -126,6 +125,13 @@ static opt<bool> LookupAddressesFromStdin(
/// @}
//===----------------------------------------------------------------------===//
+static void error(Error Err) {
+ if (!Err)
+ return;
+ WithColor::error() << toString(std::move(Err)) << "\n";
+ exit(1);
+}
+
static void error(StringRef Prefix, llvm::Error Err) {
if (!Err)
return;
@@ -141,39 +147,6 @@ static void error(StringRef Prefix, std::error_code EC) {
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<std::string> expandBundle(const std::string &InputPath) {
- std::vector<std::string> 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 +393,15 @@ static llvm::Error convertFileToGSYM(raw_ostream &OS) {
OS << "Input file: " << ConvertFilename << "\n";
- auto Objs = expandBundle(ConvertFilename);
- llvm::append_range(Objects, Objs);
+ if (auto DsymObjectsOrErr =
+ MachOObjectFile::findDsymObjectMembers(ConvertFilename)) {
+ if (DsymObjectsOrErr->empty())
+ Objects.push_back(ConvertFilename);
+ else
+ llvm::append_range(Objects, *DsymObjectsOrErr);
+ } else {
+ error(DsymObjectsOrErr.takeError());
+ }
for (auto Object : Objects) {
if (auto Err = handleFileConversionToGSYM(Object, OutFile))
More information about the llvm-commits
mailing list