llvm-dwarfdump: support dumping UUIDs of Mach-O binaries.

This is a feature supported by Darwin dwarfdump. UUIDs are used to
associate executables with their .dSYM bundles.

llvm-svn: 313165
diff --git a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
index e9b3c0c..02c91d2 100644
--- a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
+++ b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
@@ -80,6 +80,27 @@
   Accel.dump(OS);
 }
 
+/// Dump the UUID load command.
+static void dumpUUID(raw_ostream &OS, const ObjectFile &Obj) {
+  auto *MachO = dyn_cast<MachOObjectFile>(&Obj);
+  if (!MachO)
+    return;
+  for (auto LC : MachO->load_commands()) {
+    raw_ostream::uuid_t UUID;
+    if (LC.C.cmd == MachO::LC_UUID) {
+      if (LC.C.cmdsize < sizeof(UUID) + sizeof(LC.C)) {
+        OS << "error: UUID load command is too short.\n";
+        return;
+      }
+      OS << "UUID: ";
+      memcpy(&UUID, LC.Ptr+sizeof(LC.C), sizeof(UUID));
+      OS.write_uuid(UUID);
+      OS << ' ' << MachO->getFileFormatName();
+      OS << ' ' << MachO->getFileName() << '\n';
+    }
+  }
+}
+
 static void
 dumpDWARFv5StringOffsetsSection(raw_ostream &OS, StringRef SectionName,
                                 const DWARFObject &Obj,
@@ -203,6 +224,13 @@
   uint64_t DumpType = DumpOpts.DumpType;
   bool DumpEH = DumpOpts.DumpEH;
 
+  const auto *ObjFile = DObj->getFile();
+  if (!(DumpType & DIDT_UUID) || DumpType == DIDT_All)
+    outs() << ObjFile->getFileName() << ":\tfile format "
+           << ObjFile->getFileFormatName() << "\n\n";
+  if (DumpType & DIDT_UUID)
+    dumpUUID(OS, *ObjFile);
+
   if (DumpType & DIDT_DebugAbbrev) {
     OS << ".debug_abbrev contents:\n";
     getDebugAbbrev()->dump(OS);