aboutsummaryrefslogtreecommitdiff
path: root/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
diff options
context:
space:
mode:
authorShubham Sandeep Rastogi <srastogi22@apple.com>2022-10-06 12:15:07 -0700
committerShubham Sandeep Rastogi <srastogi22@apple.com>2022-10-06 14:46:01 -0700
commitd96ade00c3c96bd451c60e34a17e613cdd5fdc38 (patch)
tree70fcda3c263953c5f19da4af8cc0699ed4787a57 /llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
parente66ff2a284de9067e63f89f056d8075def139b0b (diff)
downloadllvm-d96ade00c3c96bd451c60e34a17e613cdd5fdc38.zip
llvm-d96ade00c3c96bd451c60e34a17e613cdd5fdc38.tar.gz
llvm-d96ade00c3c96bd451c60e34a17e613cdd5fdc38.tar.bz2
Remove the dependency between lib/DebugInfoDWARF and MC.
This patch had to be reverted because on gcc 7.5.0 we see an error converting from std::unique_ptr<MCRegisterInfo> to Expected<std::unique_ptr<MCRegisterInfo>> as the return type for the function createRegInfo. This has now been fixed.
Diffstat (limited to 'llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp')
-rw-r--r--llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp70
1 files changed, 55 insertions, 15 deletions
diff --git a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
index cc7f353..c024814 100644
--- a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
+++ b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
@@ -18,6 +18,8 @@
#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
+#include "llvm/MC/MCRegisterInfo.h"
+#include "llvm/MC/TargetRegistry.h"
#include "llvm/Object/Archive.h"
#include "llvm/Object/MachOUniversal.h"
#include "llvm/Object/ObjectFile.h"
@@ -341,9 +343,11 @@ using HandlerFn = std::function<bool(ObjectFile &, DWARFContext &DICtx,
const Twine &, raw_ostream &)>;
/// Print only DIEs that have a certain name.
-static bool filterByName(const StringSet<> &Names, DWARFDie Die,
- StringRef NameRef, raw_ostream &OS) {
+static bool filterByName(
+ const StringSet<> &Names, DWARFDie Die, StringRef NameRef, raw_ostream &OS,
+ std::function<StringRef(uint64_t RegNum, bool IsEH)> GetNameForDWARFReg) {
DIDumpOptions DumpOpts = getDumpOpts(Die.getDwarfUnit()->getContext());
+ DumpOpts.GetNameForDWARFReg = GetNameForDWARFReg;
std::string Name =
(IgnoreCase && !UseRegex) ? NameRef.lower() : NameRef.str();
if (UseRegex) {
@@ -369,17 +373,18 @@ static bool filterByName(const StringSet<> &Names, DWARFDie Die,
}
/// Print only DIEs that have a certain name.
-static void filterByName(const StringSet<> &Names,
- DWARFContext::unit_iterator_range CUs,
- raw_ostream &OS) {
+static void filterByName(
+ const StringSet<> &Names, DWARFContext::unit_iterator_range CUs,
+ raw_ostream &OS,
+ std::function<StringRef(uint64_t RegNum, bool IsEH)> GetNameForDWARFReg) {
for (const auto &CU : CUs)
for (const auto &Entry : CU->dies()) {
DWARFDie Die = {CU.get(), &Entry};
if (const char *Name = Die.getName(DINameKind::ShortName))
- if (filterByName(Names, Die, Name, OS))
+ if (filterByName(Names, Die, Name, OS, GetNameForDWARFReg))
continue;
if (const char *Name = Die.getName(DINameKind::LinkageName))
- filterByName(Names, Die, Name, OS);
+ filterByName(Names, Die, Name, OS, GetNameForDWARFReg);
}
}
@@ -423,8 +428,9 @@ static void getDies(DWARFContext &DICtx, const DWARFDebugNames &Accel,
}
/// Print only DIEs that have a certain name.
-static void filterByAccelName(ArrayRef<std::string> Names, DWARFContext &DICtx,
- raw_ostream &OS) {
+static void filterByAccelName(
+ ArrayRef<std::string> Names, DWARFContext &DICtx, raw_ostream &OS,
+ std::function<StringRef(uint64_t RegNum, bool IsEH)> GetNameForDWARFReg) {
SmallVector<DWARFDie, 4> Dies;
for (const auto &Name : Names) {
getDies(DICtx, DICtx.getAppleNames(), Name, Dies);
@@ -436,6 +442,7 @@ static void filterByAccelName(ArrayRef<std::string> Names, DWARFContext &DICtx,
Dies.erase(std::unique(Dies.begin(), Dies.end()), Dies.end());
DIDumpOptions DumpOpts = getDumpOpts(DICtx);
+ DumpOpts.GetNameForDWARFReg = GetNameForDWARFReg;
for (DWARFDie Die : Dies)
Die.dump(OS, 0, DumpOpts);
}
@@ -552,10 +559,41 @@ static bool collectObjectSources(ObjectFile &Obj, DWARFContext &DICtx,
return Result;
}
+static std::unique_ptr<MCRegisterInfo>
+createRegInfo(const object::ObjectFile &Obj) {
+ std::unique_ptr<MCRegisterInfo> MCRegInfo;
+ Triple TT;
+ TT.setArch(Triple::ArchType(Obj.getArch()));
+ TT.setVendor(Triple::UnknownVendor);
+ TT.setOS(Triple::UnknownOS);
+ std::string TargetLookupError;
+ const Target *TheTarget =
+ TargetRegistry::lookupTarget(TT.str(), TargetLookupError);
+ if (!TargetLookupError.empty())
+ return nullptr;
+ MCRegInfo.reset(TheTarget->createMCRegInfo(TT.str()));
+ return MCRegInfo;
+}
+
static bool dumpObjectFile(ObjectFile &Obj, DWARFContext &DICtx,
const Twine &Filename, raw_ostream &OS) {
- logAllUnhandledErrors(DICtx.loadRegisterInfo(Obj), errs(),
- Filename.str() + ": ");
+
+ auto MCRegInfo = createRegInfo(Obj);
+ if (!MCRegInfo)
+ logAllUnhandledErrors(createStringError(inconvertibleErrorCode(),
+ "Error in creating MCRegInfo"),
+ errs(), Filename.str() + ": ");
+
+ auto GetRegName = [&MCRegInfo](uint64_t DwarfRegNum, bool IsEH) -> StringRef {
+ if (!MCRegInfo)
+ return {};
+ if (llvm::Optional<unsigned> LLVMRegNum =
+ MCRegInfo->getLLVMRegNum(DwarfRegNum, IsEH))
+ if (const char *RegName = MCRegInfo->getName(*LLVMRegNum))
+ return StringRef(RegName);
+ return {};
+ };
+
// The UUID dump already contains all the same information.
if (!(DumpType & DIDT_UUID) || DumpType == DIDT_All)
OS << Filename << ":\tfile format " << Obj.getFileFormatName() << '\n';
@@ -570,19 +608,21 @@ static bool dumpObjectFile(ObjectFile &Obj, DWARFContext &DICtx,
for (auto name : Name)
Names.insert((IgnoreCase && !UseRegex) ? StringRef(name).lower() : name);
- filterByName(Names, DICtx.normal_units(), OS);
- filterByName(Names, DICtx.dwo_units(), OS);
+ filterByName(Names, DICtx.normal_units(), OS, GetRegName);
+ filterByName(Names, DICtx.dwo_units(), OS, GetRegName);
return true;
}
// Handle the --find option and lower it to --debug-info=<offset>.
if (!Find.empty()) {
- filterByAccelName(Find, DICtx, OS);
+ filterByAccelName(Find, DICtx, OS, GetRegName);
return true;
}
// Dump the complete DWARF structure.
- DICtx.dump(OS, getDumpOpts(DICtx), DumpOffsets);
+ auto DumpOpts = getDumpOpts(DICtx);
+ DumpOpts.GetNameForDWARFReg = GetRegName;
+ DICtx.dump(OS, DumpOpts, DumpOffsets);
return true;
}