Skip to content

Conversation

@Prabhuk
Copy link
Contributor

@Prabhuk Prabhuk commented May 14, 2025

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().

@llvmbot
Copy link
Member

llvmbot commented May 14, 2025

@llvm/pr-subscribers-llvm-binary-utilities

Author: Prabhu Rajasekaran (Prabhuk)

Changes

Add a boolean to control whether object disassembly
(disassembleObject()) prints to console. It is always set to false
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.


Full diff: https://github.com/llvm/llvm-project/pull/139931.diff

1 Files Affected:

  • (modified) llvm/tools/llvm-objdump/llvm-objdump.cpp (+28-22)
diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp
index 2f839199712eb..0e1be02f52e60 100644
--- a/llvm/tools/llvm-objdump/llvm-objdump.cpp
+++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp
@@ -349,6 +349,7 @@ bool objdump::UnwindInfo;
 static bool Wide;
 std::string objdump::Prefix;
 uint32_t objdump::PrefixStrip;
+static bool QuietDisasm = false;
 
 DebugVarsFormat objdump::DbgVariables = DVDisabled;
 
@@ -1374,6 +1375,8 @@ static uint64_t dumpARMELFData(uint64_t SectionAddr, uint64_t Index,
   return 1;
 }
 
+static raw_ostream &disasmOuts() { return QuietDisasm ? nulls() : outs(); }
+
 static void dumpELFData(uint64_t SectionAddr, uint64_t Index, uint64_t End,
                         ArrayRef<uint8_t> Bytes) {
   // print out data up to 8 bytes at a time in hex and ascii
@@ -1383,9 +1386,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);
+      disasmOuts() << format("%8" PRIx64 ":", SectionAddr + Index);
     Byte = Bytes.slice(Index)[0];
-    outs() << format(" %02x", Byte);
+    disasmOuts() << format(" %02x", Byte);
     AsciiData[NumBytes] = isPrint(Byte) ? Byte : '.';
 
     uint8_t IndentOffset = 0;
@@ -1400,9 +1403,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';
+      disasmOuts() << std::string(IndentOffset, ' ') << "         ";
+      disasmOuts() << reinterpret_cast<char *>(AsciiData);
+      disasmOuts() << '\n';
       NumBytes = 0;
     }
   }
@@ -2089,10 +2092,10 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
 
       if (!PrintedSection) {
         PrintedSection = true;
-        outs() << "\nDisassembly of section ";
+        disasmOuts() << "\nDisassembly of section ";
         if (!SegmentName.empty())
-          outs() << SegmentName << ",";
-        outs() << SectionName << ":\n";
+          disasmOuts() << SegmentName << ",";
+        disasmOuts() << SectionName << ":\n";
       }
 
       bool PrintedLabel = false;
@@ -2104,22 +2107,24 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
         const StringRef SymbolName = SymNamesHere[i];
 
         if (!PrintedLabel) {
-          outs() << '\n';
+          disasmOuts() << '\n';
           PrintedLabel = true;
         }
         if (LeadingAddr)
-          outs() << format(Is64Bits ? "%016" PRIx64 " " : "%08" PRIx64 " ",
-                           SectionAddr + Start + VMAAdjustment);
+          disasmOuts() << format(Is64Bits ? "%016" PRIx64 " "
+                                          : "%08" PRIx64 " ",
+                                 SectionAddr + Start + VMAAdjustment);
         if (Obj.isXCOFF() && SymbolDescription) {
-          outs() << getXCOFFSymbolDescription(Symbol, SymbolName) << ":\n";
+          disasmOuts() << getXCOFFSymbolDescription(Symbol, SymbolName)
+                       << ":\n";
         } else
-          outs() << '<' << SymbolName << ">:\n";
+          disasmOuts() << '<' << 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";
+        disasmOuts() << "...\n";
         continue;
       }
 
@@ -2156,17 +2161,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';
+            disasmOuts() << 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";
+            disasmOuts() << 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';
+              disasmOuts() << "\t.byte\t "
+                           << format_hex(Bytes[I], 1, /*Upper=*/true) << '\n';
           }
         }
 
@@ -2203,7 +2208,7 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
           Symbols[SI - 1].XCOFFSymInfo.StorageMappingClass &&
           (*Symbols[SI - 1].XCOFFSymInfo.StorageMappingClass == XCOFF::XMC_PR);
 
-      formatted_raw_ostream FOS(outs());
+      formatted_raw_ostream FOS(disasmOuts());
 
       std::unordered_map<uint64_t, std::string> AllLabels;
       std::unordered_map<uint64_t, std::vector<BBAddrMapLabel>> BBAddrMapLabels;
@@ -2551,6 +2556,7 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
       set_difference(DisasmSymbolSet, FoundDisasmSymbolSet);
   for (StringRef Sym : MissingDisasmSymbolSet.keys())
     reportWarning("failed to disassemble missing symbol " + Sym, FileName);
+  QuietDisasm = false;
 }
 
 static void disassembleObject(ObjectFile *Obj, bool InlineRelocs) {

@Prabhuk Prabhuk requested review from jh7370 and petrhosek May 14, 2025 16:53
Copy link
Member

@petrhosek petrhosek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer instead adding an extra argument to disassembleObject (and other functions like dumpELFData) representing the output stream.

--call-graph-info implementation could then pass null() as the argument when invoking disassembleObject.

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()`.
Copy link
Member

@petrhosek petrhosek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The title and description need to be updated. Please also wait for @jh7370 to take a look before merging this change.

@Prabhuk Prabhuk changed the title [NFC][llvm-objdump] Add quiet disassembly [NFC][llvm-objdump] Add ostream param to control console prints May 15, 2025
Copy link
Collaborator

@jh7370 jh7370 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks.

@Prabhuk Prabhuk merged commit 15b20a1 into llvm:main May 15, 2025
11 checks passed
@Prabhuk Prabhuk deleted the objdump_nfc branch May 15, 2025 16:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants