Skip to content

Commit 6949b86

Browse files
rjmansfieldaokblast
authored andcommitted
[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 6789137 commit 6949b86

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"
@@ -7415,18 +7416,28 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
74157416
std::unique_ptr<DIContext> diContext;
74167417
std::unique_ptr<Binary> DSYMBinary;
74177418
std::unique_ptr<MemoryBuffer> DSYMBuf;
7418-
if (UseDbg) {
7419-
// If separate DSym file path was specified, parse it as a macho file,
7420-
// get the sections and supply it to the section name parsing machinery.
7421-
if (const ObjectFile *DbgObj =
7422-
getMachODSymObject(MachOOF, Filename, DSYMBinary, DSYMBuf)) {
7419+
const ObjectFile *DbgObj = MachOOF;
7420+
if (UseDbg || PrintSource || PrintLines) {
7421+
// Look for debug info in external dSYM file or embedded in the object.
7422+
// getMachODSymObject returns MachOOF by default if no external dSYM found.
7423+
const ObjectFile *DSym =
7424+
getMachODSymObject(MachOOF, Filename, DSYMBinary, DSYMBuf);
7425+
if (!DSym)
7426+
return;
7427+
DbgObj = DSym;
7428+
if (UseDbg || PrintLines) {
74237429
// Setup the DIContext
74247430
diContext = DWARFContext::create(*DbgObj);
7425-
} else {
7426-
return;
74277431
}
74287432
}
74297433

7434+
std::optional<SourcePrinter> SP;
7435+
std::optional<LiveElementPrinter> LEP;
7436+
if (PrintSource || PrintLines) {
7437+
SP.emplace(DbgObj, TheTarget->getName());
7438+
LEP.emplace(*MRI, *STI);
7439+
}
7440+
74307441
if (FilterSections.empty())
74317442
outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
74327443

@@ -7605,6 +7616,12 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
76057616
outs() << SymName << ":\n";
76067617

76077618
uint64_t PC = SectAddress + Index;
7619+
7620+
if (PrintSource || PrintLines) {
7621+
formatted_raw_ostream FOS(outs());
7622+
SP->printSourceLine(FOS, {PC, SectIdx}, Filename, *LEP);
7623+
}
7624+
76087625
if (LeadingAddr) {
76097626
if (FullLeadingAddr) {
76107627
if (MachOOF->is64Bit())
@@ -7696,6 +7713,11 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
76967713

76977714
uint64_t PC = SectAddress + Index;
76987715

7716+
if (PrintSource || PrintLines) {
7717+
formatted_raw_ostream FOS(outs());
7718+
SP->printSourceLine(FOS, {PC, SectIdx}, Filename, *LEP);
7719+
}
7720+
76997721
if (DumpAndSkipDataInCode(PC, Bytes.data() + Index, Dices, InstSize))
77007722
continue;
77017723

0 commit comments

Comments
 (0)