[llvm] r336561 - [dsymutil] Add support for outputting assembly

Jonas Devlieghere via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 9 09:58:49 PDT 2018


Author: jdevlieghere
Date: Mon Jul  9 09:58:48 2018
New Revision: 336561

URL: http://llvm.org/viewvc/llvm-project?rev=336561&view=rev
Log:
[dsymutil] Add support for outputting assembly

When implementing the DWARF accelerator tables in dsymutil I ran into an
assertion in the assembler. Debugging these kind of issues is a lot
easier when looking at the assembly instead of debugging the assembler
itself. Since it's only a matter of creating an AsmStreamer instead of a
MCObjectStreamer it made sense to turn this into a (hidden) dsymutil
feature.

Differential revision: https://reviews.llvm.org/D49079

Added:
    llvm/trunk/test/tools/dsymutil/X86/assembly-output.test
Modified:
    llvm/trunk/tools/dsymutil/DwarfLinker.cpp
    llvm/trunk/tools/dsymutil/DwarfStreamer.cpp
    llvm/trunk/tools/dsymutil/DwarfStreamer.h
    llvm/trunk/tools/dsymutil/LinkUtils.h
    llvm/trunk/tools/dsymutil/dsymutil.cpp

Added: llvm/trunk/test/tools/dsymutil/X86/assembly-output.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/dsymutil/X86/assembly-output.test?rev=336561&view=auto
==============================================================================
--- llvm/trunk/test/tools/dsymutil/X86/assembly-output.test (added)
+++ llvm/trunk/test/tools/dsymutil/X86/assembly-output.test Mon Jul  9 09:58:48 2018
@@ -0,0 +1,3 @@
+RUN: dsymutil -S -f -oso-prepend-path=%p/.. %p/../Inputs/basic.macho.x86_64 -o - | FileCheck %s
+
+CHECK:  .section __DWARF

Modified: llvm/trunk/tools/dsymutil/DwarfLinker.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/dsymutil/DwarfLinker.cpp?rev=336561&r1=336560&r2=336561&view=diff
==============================================================================
--- llvm/trunk/tools/dsymutil/DwarfLinker.cpp (original)
+++ llvm/trunk/tools/dsymutil/DwarfLinker.cpp Mon Jul  9 09:58:48 2018
@@ -208,7 +208,7 @@ bool DwarfLinker::createStreamer(const T
   if (Options.NoOutput)
     return true;
 
-  Streamer = llvm::make_unique<DwarfStreamer>(OutFile);
+  Streamer = llvm::make_unique<DwarfStreamer>(OutFile, Options);
   return Streamer->init(TheTriple);
 }
 
@@ -2411,7 +2411,7 @@ bool DwarfLinker::link(const DebugMap &M
       if (LLVM_UNLIKELY(Options.Update)) {
         for (auto &CurrentUnit : LinkContext.CompileUnits)
           CurrentUnit->markEverythingAsKept();
-        Streamer->copyInvariantDebugSection(*LinkContext.ObjectFile, Options);
+        Streamer->copyInvariantDebugSection(*LinkContext.ObjectFile);
       } else {
         for (auto &CurrentUnit : LinkContext.CompileUnits)
           lookForDIEsToKeep(LinkContext.RelocMgr, LinkContext.Ranges,

Modified: llvm/trunk/tools/dsymutil/DwarfStreamer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/dsymutil/DwarfStreamer.cpp?rev=336561&r1=336560&r2=336561&view=diff
==============================================================================
--- llvm/trunk/tools/dsymutil/DwarfStreamer.cpp (original)
+++ llvm/trunk/tools/dsymutil/DwarfStreamer.cpp Mon Jul  9 09:58:48 2018
@@ -69,8 +69,8 @@ bool DwarfStreamer::init(Triple TheTripl
   if (!MSTI)
     return error("no subtarget info for target " + TripleName, Context);
 
-  MCTargetOptions Options;
-  MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, Options);
+  MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
+  MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, MCOptions);
   if (!MAB)
     return error("no asm backend for target " + TripleName, Context);
 
@@ -82,12 +82,26 @@ bool DwarfStreamer::init(Triple TheTripl
   if (!MCE)
     return error("no code emitter for target " + TripleName, Context);
 
-  MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
-  MS = TheTarget->createMCObjectStreamer(
-      TheTriple, *MC, std::unique_ptr<MCAsmBackend>(MAB),
-      MAB->createObjectWriter(OutFile), std::unique_ptr<MCCodeEmitter>(MCE),
-      *MSTI, MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible,
-      /*DWARFMustBeAtTheEnd*/ false);
+  switch (Options.FileType) {
+  case OutputFileType::Assembly: {
+    MIP = TheTarget->createMCInstPrinter(TheTriple, MAI->getAssemblerDialect(),
+                                         *MAI, *MII, *MRI);
+    MS = TheTarget->createAsmStreamer(
+        *MC, llvm::make_unique<formatted_raw_ostream>(OutFile), true, true, MIP,
+        std::unique_ptr<MCCodeEmitter>(MCE), std::unique_ptr<MCAsmBackend>(MAB),
+        true);
+    break;
+  }
+  case OutputFileType::Object: {
+    MS = TheTarget->createMCObjectStreamer(
+        TheTriple, *MC, std::unique_ptr<MCAsmBackend>(MAB),
+        MAB->createObjectWriter(OutFile), std::unique_ptr<MCCodeEmitter>(MCE),
+        *MSTI, MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible,
+        /*DWARFMustBeAtTheEnd*/ false);
+    break;
+  }
+  }
+
   if (!MS)
     return error("no object streamer for target " + TripleName, Context);
 
@@ -111,7 +125,8 @@ bool DwarfStreamer::init(Triple TheTripl
 
 bool DwarfStreamer::finish(const DebugMap &DM) {
   bool Result = true;
-  if (DM.getTriple().isOSDarwin() && !DM.getBinaryPath().empty())
+  if (DM.getTriple().isOSDarwin() && !DM.getBinaryPath().empty() &&
+      Options.FileType == OutputFileType::Object)
     Result = MachOUtils::generateDsymCompanion(DM, *MS, OutFile);
   else
     MS->Finish();
@@ -545,8 +560,7 @@ static void emitSectionContents(const ob
       MS->EmitBytes(Contents);
 }
 
-void DwarfStreamer::copyInvariantDebugSection(const object::ObjectFile &Obj,
-                                              LinkOptions &Options) {
+void DwarfStreamer::copyInvariantDebugSection(const object::ObjectFile &Obj) {
   MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLineSection());
   emitSectionContents(Obj, "debug_line", MS);
 

Modified: llvm/trunk/tools/dsymutil/DwarfStreamer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/dsymutil/DwarfStreamer.h?rev=336561&r1=336560&r2=336561&view=diff
==============================================================================
--- llvm/trunk/tools/dsymutil/DwarfStreamer.h (original)
+++ llvm/trunk/tools/dsymutil/DwarfStreamer.h Mon Jul  9 09:58:48 2018
@@ -9,6 +9,7 @@
 
 #include "CompileUnit.h"
 #include "DebugMap.h"
+#include "LinkUtils.h"
 #include "NonRelocatableStringpool.h"
 #include "llvm/CodeGen/AccelTable.h"
 #include "llvm/CodeGen/AsmPrinter.h"
@@ -36,15 +37,15 @@
 namespace llvm {
 namespace dsymutil {
 
-struct LinkOptions;
-
 /// The Dwarf streaming logic.
 ///
 /// All interactions with the MC layer that is used to build the debug
 /// information binary representation are handled in this class.
 class DwarfStreamer {
 public:
-  DwarfStreamer(raw_fd_ostream &OutFile) : OutFile(OutFile) {}
+  DwarfStreamer(raw_fd_ostream &OutFile, LinkOptions Options)
+      : OutFile(OutFile), Options(std::move(Options)) {}
+
   bool init(Triple TheTriple);
 
   /// Dump the file to the disk.
@@ -103,7 +104,7 @@ public:
                             unsigned AdddressSize);
 
   /// Copy over the debug sections that are not modified when updating.
-  void copyInvariantDebugSection(const object::ObjectFile &Obj, LinkOptions &);
+  void copyInvariantDebugSection(const object::ObjectFile &Obj);
 
   uint32_t getLineSectionSize() const { return LineSectionSize; }
 
@@ -144,6 +145,7 @@ private:
   MCAsmBackend *MAB; // Owned by MCStreamer
   std::unique_ptr<MCInstrInfo> MII;
   std::unique_ptr<MCSubtargetInfo> MSTI;
+  MCInstPrinter *MIP; // Owned by AsmPrinter
   MCCodeEmitter *MCE; // Owned by MCStreamer
   MCStreamer *MS;     // Owned by AsmPrinter
   std::unique_ptr<TargetMachine> TM;
@@ -153,6 +155,8 @@ private:
   /// The file we stream the linked Dwarf to.
   raw_fd_ostream &OutFile;
 
+  LinkOptions Options;
+
   uint32_t RangesSectionSize;
   uint32_t LocSectionSize;
   uint32_t LineSectionSize;

Modified: llvm/trunk/tools/dsymutil/LinkUtils.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/dsymutil/LinkUtils.h?rev=336561&r1=336560&r2=336561&view=diff
==============================================================================
--- llvm/trunk/tools/dsymutil/LinkUtils.h (original)
+++ llvm/trunk/tools/dsymutil/LinkUtils.h Mon Jul  9 09:58:48 2018
@@ -17,6 +17,11 @@
 namespace llvm {
 namespace dsymutil {
 
+enum class OutputFileType {
+  Object,
+  Assembly,
+};
+
 struct LinkOptions {
   /// Verbosity
   bool Verbose = false;
@@ -39,6 +44,9 @@ struct LinkOptions {
   /// Number of threads.
   unsigned Threads = 1;
 
+  // Output file type.
+  OutputFileType FileType = OutputFileType::Object;
+
   /// -oso-prepend-path
   std::string PrependPath;
 

Modified: llvm/trunk/tools/dsymutil/dsymutil.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/dsymutil/dsymutil.cpp?rev=336561&r1=336560&r2=336561&view=diff
==============================================================================
--- llvm/trunk/tools/dsymutil/dsymutil.cpp (original)
+++ llvm/trunk/tools/dsymutil/dsymutil.cpp Mon Jul  9 09:58:48 2018
@@ -66,6 +66,11 @@ static opt<std::string> OsoPrependPath(
     desc("Specify a directory to prepend to the paths of object files."),
     value_desc("path"), cat(DsymCategory));
 
+static opt<bool> Assembly(
+    "S",
+    desc("Output textual assembly instead of a binary dSYM companion file."),
+    init(false), cat(DsymCategory), cl::Hidden);
+
 static opt<bool> DumpStab(
     "symtab",
     desc("Dumps the symbol table found in executable or object file(s) and\n"
@@ -322,6 +327,9 @@ static Expected<LinkOptions> getOptions(
   Options.NoTimestamp = NoTimestamp;
   Options.PrependPath = OsoPrependPath;
 
+  if (Assembly)
+    Options.FileType = OutputFileType::Assembly;
+
   if (Options.Update && std::find(InputFiles.begin(), InputFiles.end(), "-") !=
                             InputFiles.end()) {
     // FIXME: We cannot use stdin for an update because stdin will be




More information about the llvm-commits mailing list