[llvm] [NFC][llvm-objdump] Add quiet disassembly (PR #139931)

Prabhu Rajasekaran via llvm-commits llvm-commits at lists.llvm.org
Wed May 14 10:36:14 PDT 2025


https://github.com/Prabhuk updated https://github.com/llvm/llvm-project/pull/139931

>From 1f3198f90d0d3ffec2c55b63c5da79db149e99d6 Mon Sep 17 00:00:00 2001
From: prabhukr <prabhukr at google.com>
Date: Wed, 14 May 2025 16:43:35 +0000
Subject: [PATCH] [NFC][llvm-objdump] Add quiet disassembly

Add a function param to control whether object disassembly
(disassembleObject()) prints to console. It is always set to `outs()`
which makes this change an NFC.

This is introduced to support the upcoming `--call-graph-info` option
which will utilize the `disassembleObject` function to collect function
and call site addresses without printing to console by passing `nulls()`
instead of `outs()`.
---
 llvm/tools/llvm-objdump/llvm-objdump.cpp | 59 ++++++++++++------------
 1 file changed, 29 insertions(+), 30 deletions(-)

diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp
index 2f839199712eb..7a778da2d3a49 100644
--- a/llvm/tools/llvm-objdump/llvm-objdump.cpp
+++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp
@@ -1375,7 +1375,7 @@ static uint64_t dumpARMELFData(uint64_t SectionAddr, uint64_t Index,
 }
 
 static void dumpELFData(uint64_t SectionAddr, uint64_t Index, uint64_t End,
-                        ArrayRef<uint8_t> Bytes) {
+                        ArrayRef<uint8_t> Bytes, raw_ostream &OS) {
   // print out data up to 8 bytes at a time in hex and ascii
   uint8_t AsciiData[9] = {'\0'};
   uint8_t Byte;
@@ -1383,9 +1383,9 @@ static void dumpELFData(uint64_t SectionAddr, uint64_t Index, uint64_t End,
 
   for (; Index < End; ++Index) {
     if (NumBytes == 0)
-      outs() << format("%8" PRIx64 ":", SectionAddr + Index);
+      OS << format("%8" PRIx64 ":", SectionAddr + Index);
     Byte = Bytes.slice(Index)[0];
-    outs() << format(" %02x", Byte);
+    OS << format(" %02x", Byte);
     AsciiData[NumBytes] = isPrint(Byte) ? Byte : '.';
 
     uint8_t IndentOffset = 0;
@@ -1400,9 +1400,9 @@ static void dumpELFData(uint64_t SectionAddr, uint64_t Index, uint64_t End,
     }
     if (NumBytes == 8) {
       AsciiData[8] = '\0';
-      outs() << std::string(IndentOffset, ' ') << "         ";
-      outs() << reinterpret_cast<char *>(AsciiData);
-      outs() << '\n';
+      OS << std::string(IndentOffset, ' ') << "         ";
+      OS << reinterpret_cast<char *>(AsciiData);
+      OS << '\n';
       NumBytes = 0;
     }
   }
@@ -1666,7 +1666,7 @@ static void
 disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
                   DisassemblerTarget &PrimaryTarget,
                   std::optional<DisassemblerTarget> &SecondaryTarget,
-                  SourcePrinter &SP, bool InlineRelocs) {
+                  SourcePrinter &SP, bool InlineRelocs, raw_ostream &OS) {
   DisassemblerTarget *DT = &PrimaryTarget;
   bool PrimaryIsThumb = false;
   SmallVector<std::pair<uint64_t, uint64_t>, 0> CHPECodeMap;
@@ -2089,10 +2089,10 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
 
       if (!PrintedSection) {
         PrintedSection = true;
-        outs() << "\nDisassembly of section ";
+        OS << "\nDisassembly of section ";
         if (!SegmentName.empty())
-          outs() << SegmentName << ",";
-        outs() << SectionName << ":\n";
+          OS << SegmentName << ",";
+        OS << SectionName << ":\n";
       }
 
       bool PrintedLabel = false;
@@ -2104,22 +2104,22 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
         const StringRef SymbolName = SymNamesHere[i];
 
         if (!PrintedLabel) {
-          outs() << '\n';
+          OS << '\n';
           PrintedLabel = true;
         }
         if (LeadingAddr)
-          outs() << format(Is64Bits ? "%016" PRIx64 " " : "%08" PRIx64 " ",
-                           SectionAddr + Start + VMAAdjustment);
+          OS << format(Is64Bits ? "%016" PRIx64 " " : "%08" PRIx64 " ",
+                       SectionAddr + Start + VMAAdjustment);
         if (Obj.isXCOFF() && SymbolDescription) {
-          outs() << getXCOFFSymbolDescription(Symbol, SymbolName) << ":\n";
+          OS << getXCOFFSymbolDescription(Symbol, SymbolName) << ":\n";
         } else
-          outs() << '<' << SymbolName << ">:\n";
+          OS << '<' << SymbolName << ">:\n";
       }
 
       // Don't print raw contents of a virtual section. A virtual section
       // doesn't have any contents in the file.
       if (Section.isVirtual()) {
-        outs() << "...\n";
+        OS << "...\n";
         continue;
       }
 
@@ -2156,17 +2156,17 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
           do {
             StringRef Line;
             std::tie(Line, ErrMsg) = ErrMsg.split('\n');
-            outs() << DT->Context->getAsmInfo()->getCommentString()
-                   << " error decoding " << SymNamesHere[SHI] << ": " << Line
-                   << '\n';
+            OS << DT->Context->getAsmInfo()->getCommentString()
+               << " error decoding " << SymNamesHere[SHI] << ": " << Line
+               << '\n';
           } while (!ErrMsg.empty());
 
           if (Size) {
-            outs() << DT->Context->getAsmInfo()->getCommentString()
-                   << " decoding failed region as bytes\n";
+            OS << DT->Context->getAsmInfo()->getCommentString()
+               << " decoding failed region as bytes\n";
             for (uint64_t I = 0; I < Size; ++I)
-              outs() << "\t.byte\t " << format_hex(Bytes[I], 1, /*Upper=*/true)
-                     << '\n';
+              OS << "\t.byte\t " << format_hex(Bytes[I], 1, /*Upper=*/true)
+                 << '\n';
           }
         }
 
@@ -2179,13 +2179,13 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
         Start += Size;
         break;
       }
-
+      formatted_raw_ostream FOS(OS);
       Index = Start;
       if (SectionAddr < StartAddress)
         Index = std::max<uint64_t>(Index, StartAddress - SectionAddr);
 
       if (DisassembleAsELFData) {
-        dumpELFData(SectionAddr, Index, End, Bytes);
+        dumpELFData(SectionAddr, Index, End, Bytes, FOS);
         Index = End;
         continue;
       }
@@ -2203,8 +2203,6 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
           Symbols[SI - 1].XCOFFSymInfo.StorageMappingClass &&
           (*Symbols[SI - 1].XCOFFSymInfo.StorageMappingClass == XCOFF::XMC_PR);
 
-      formatted_raw_ostream FOS(outs());
-
       std::unordered_map<uint64_t, std::string> AllLabels;
       std::unordered_map<uint64_t, std::vector<BBAddrMapLabel>> BBAddrMapLabels;
       if (SymbolizeOperands) {
@@ -2553,7 +2551,8 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
     reportWarning("failed to disassemble missing symbol " + Sym, FileName);
 }
 
-static void disassembleObject(ObjectFile *Obj, bool InlineRelocs) {
+static void disassembleObject(ObjectFile *Obj, bool InlineRelocs,
+                              raw_ostream &OS) {
   // If information useful for showing the disassembly is missing, try to find a
   // more complete binary and disassemble that instead.
   OwningBinary<Binary> FetchedBinary;
@@ -2679,7 +2678,7 @@ static void disassembleObject(ObjectFile *Obj, bool InlineRelocs) {
                   "Unrecognized disassembler option: " + Opt);
 
   disassembleObject(*Obj, *DbgObj, PrimaryTarget, SecondaryTarget, SP,
-                    InlineRelocs);
+                    InlineRelocs, OS);
 }
 
 void Dumper::printRelocations() {
@@ -3340,7 +3339,7 @@ static void dumpObject(ObjectFile *O, const Archive *A = nullptr,
   if (SectionContents)
     printSectionContents(O);
   if (Disassemble)
-    disassembleObject(O, Relocations);
+    disassembleObject(O, Relocations, outs());
   if (UnwindInfo)
     printUnwindInfo(O);
 



More information about the llvm-commits mailing list