Skip to content

Commit 368530f

Browse files
committed
[llvm-objdump] Fix --source with --macho flag (llvm#163810)
The --source option was broken when using the --macho flag because DisassembleMachO() only initialized debug info when UseDbg was true, and would return early if no dSYM was found.
1 parent da0f781 commit 368530f

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

llvm/test/tools/llvm-objdump/MachO/disassemble-source-dsym.test

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,35 @@
1313
# RUN: dsymutil -f -oso-prepend-path=%p/../../dsymutil/ %t3 -o %t3.dSYM
1414
# RUN: llvm-objdump --source --prefix=%p/../../dsymutil %t3 | FileCheck --check-prefix=SOURCE %s
1515

16+
## Test that --source works with --macho flag.
17+
18+
## --macho w/ explicit .dSYM
19+
# RUN: llvm-objdump < %p/../../dsymutil/Inputs/basic.macho.x86_64 - --source --macho --dsym=%t1.dSYM --prefix=%p/../../dsymutil | \
20+
# RUN: FileCheck --check-prefix=SOURCE %s
21+
22+
## --macho w/ auto-detected .dSYM (dir)
23+
# RUN: llvm-objdump --source --macho --prefix=%p/../../dsymutil %t2 | FileCheck --check-prefix=SOURCE %s
24+
25+
## --macho w/ auto-detected .dSYM (file)
26+
# RUN: llvm-objdump --source --macho --prefix=%p/../../dsymutil %t3 | FileCheck --check-prefix=SOURCE %s
27+
1628
# SOURCE: ; int bar(int arg) {
29+
30+
## Test that --line-numbers works with --macho flag.
31+
32+
## --macho -l w/ explicit .dSYM
33+
# RUN: llvm-objdump -d -l --macho --dsym=%t1.dSYM %p/../../dsymutil/Inputs/basic.macho.x86_64 | FileCheck --check-prefix=LINE %s
34+
35+
## --macho -l w/ object file (embedded debug info)
36+
# RUN: llvm-objdump -d -l --macho %p/../../dsymutil/Inputs/basic1.macho.x86_64.o | FileCheck --check-prefix=LINE_OBJ %s
37+
38+
# LINE: (__TEXT,__text) section
39+
# LINE: _bar:
40+
# LINE: ; bar():
41+
# LINE: ; {{.*}}basic3.c:
42+
43+
# LINE_OBJ: (__TEXT,__text) section
44+
# LINE_OBJ: _main:
45+
# LINE_OBJ: ; main():
46+
# LINE_OBJ: ; {{.*}}basic1.c:23
47+
# LINE_OBJ: pushq %rbp ## basic1.c:23:0

llvm/tools/llvm-objdump/MachODump.cpp

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "MachODump.h"
1414

1515
#include "ObjdumpOptID.h"
16+
#include "SourcePrinter.h"
1617
#include "llvm-objdump.h"
1718
#include "llvm/ADT/STLExtras.h"
1819
#include "llvm/ADT/StringExtras.h"
@@ -7411,18 +7412,28 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
74117412
std::unique_ptr<DIContext> diContext;
74127413
std::unique_ptr<Binary> DSYMBinary;
74137414
std::unique_ptr<MemoryBuffer> DSYMBuf;
7414-
if (UseDbg) {
7415-
// If separate DSym file path was specified, parse it as a macho file,
7416-
// get the sections and supply it to the section name parsing machinery.
7417-
if (const ObjectFile *DbgObj =
7418-
getMachODSymObject(MachOOF, Filename, DSYMBinary, DSYMBuf)) {
7415+
const ObjectFile *DbgObj = MachOOF;
7416+
if (UseDbg || PrintSource || PrintLines) {
7417+
// Look for debug info in external dSYM file or embedded in the object.
7418+
// getMachODSymObject returns MachOOF by default if no external dSYM found.
7419+
const ObjectFile *DSym =
7420+
getMachODSymObject(MachOOF, Filename, DSYMBinary, DSYMBuf);
7421+
if (!DSym)
7422+
return;
7423+
DbgObj = DSym;
7424+
if (UseDbg || PrintLines) {
74197425
// Setup the DIContext
74207426
diContext = DWARFContext::create(*DbgObj);
7421-
} else {
7422-
return;
74237427
}
74247428
}
74257429

7430+
std::optional<SourcePrinter> SP;
7431+
std::optional<LiveVariablePrinter> LVP;
7432+
if (PrintSource || PrintLines) {
7433+
SP.emplace(DbgObj, TheTarget->getName());
7434+
LVP.emplace(*MRI.get(), *STI.get());
7435+
}
7436+
74267437
if (FilterSections.empty())
74277438
outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
74287439

@@ -7601,6 +7612,12 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
76017612
outs() << SymName << ":\n";
76027613

76037614
uint64_t PC = SectAddress + Index;
7615+
7616+
if (PrintSource || PrintLines) {
7617+
formatted_raw_ostream FOS(outs());
7618+
SP->printSourceLine(FOS, {PC, SectIdx}, Filename, *LVP);
7619+
}
7620+
76047621
if (LeadingAddr) {
76057622
if (FullLeadingAddr) {
76067623
if (MachOOF->is64Bit())
@@ -7692,6 +7709,11 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
76927709

76937710
uint64_t PC = SectAddress + Index;
76947711

7712+
if (PrintSource || PrintLines) {
7713+
formatted_raw_ostream FOS(outs());
7714+
SP->printSourceLine(FOS, {PC, SectIdx}, Filename, *LVP);
7715+
}
7716+
76957717
if (DumpAndSkipDataInCode(PC, Bytes.data() + Index, Dices, InstSize))
76967718
continue;
76977719

0 commit comments

Comments
 (0)