[llvm] [llvm-objdump] Fix --source with --macho flag (PR #163810)

Daniel Rodríguez Troitiño via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 17 16:29:45 PDT 2025


================
@@ -7415,18 +7416,24 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
   std::unique_ptr<DIContext> diContext;
   std::unique_ptr<Binary> DSYMBinary;
   std::unique_ptr<MemoryBuffer> DSYMBuf;
-  if (UseDbg) {
-    // If separate DSym file path was specified, parse it as a macho file,
-    // get the sections and supply it to the section name parsing machinery.
-    if (const ObjectFile *DbgObj =
-            getMachODSymObject(MachOOF, Filename, DSYMBinary, DSYMBuf)) {
+  const ObjectFile *DbgObj = MachOOF;
+  if (UseDbg || PrintSource || PrintLines) {
+    // Look for debug info in external dSYM file or embedded in the object.
+    // getMachODSymObject returns MachOOF by default if no external dSYM found.
+    const ObjectFile *DSym =
+        getMachODSymObject(MachOOF, Filename, DSYMBinary, DSYMBuf);
+    if (!DSym)
+      return;
+    DbgObj = DSym;
+    if (UseDbg) {
       // Setup the DIContext
       diContext = DWARFContext::create(*DbgObj);
-    } else {
-      return;
     }
   }
 
+  SourcePrinter SP(DbgObj, TheTarget->getName());
+  LiveElementPrinter LEP(*MRI, *STI);
+
----------------
drodriguez wrote:

Not very important, and I haven't double checked the code compiles/works as I think it should.

This would happen even if `PrintSource` or `PrintLines` is not used. Also `FOS` below is created for each line to be printed, and later destroyed (probably cheap, but…)

With a lambda, it is possible to avoid some of these problems:

```
std::function<void(object::SectionedAddress Address, StringRef ObjectFilename)> printSourceLineIfNeeded;
if (PrintSource || PrintLines) {
  printSourceLineIfNeeded = [DbgObj, TheTarget, &MRI, &STI](object::SectionedAddress Address, StringRef ObjectFilename) {
    static SourcePrinter SP(DbgObj, TheTarget->getName());
    static LiveElementPrinter LEP(*MRI, *STI);
    static formatted_raw_ostream FOS(outs());
    SP.printSourceLine(FOS, Address, ObjectFilename, LEP);
  }
} else {
  printSourceLineIfNeeded = [](object::SectionedAddress Address, StringRef ObjectFilename) {};
}
...
// Below, use the lambda
printSourceLineIfNeeded({PC, SectIdx}, Filename);
```

https://github.com/llvm/llvm-project/pull/163810


More information about the llvm-commits mailing list