[objdump][macho] Emit segment names along with section names

I recently came across a MachO with multiple sections of the same name but
different segments. We should emit the segment name alongside the section name
for MachO's.

Differential Revision: https://reviews.llvm.org/D87119
This commit is contained in:
Daniel Sanders 2020-09-03 17:07:59 -07:00
parent 51932fc6bd
commit 3f1a9b7eca
3 changed files with 25 additions and 16 deletions

View file

@ -4,7 +4,7 @@
; The compact unwind format in ILP32 mode is pretty much the same, except
; references to addresses (function, personality, LSDA) are pointer-sized.
; CHECK: Contents of section __compact_unwind:
; CHECK: Contents of section __LD,__compact_unwind:
; CHECK-NEXT: 0004 00000000 04000000 00000002 00000000
; CHECK-NEXT: 0014 00000000
.globl _test_compact_unwind

View file

@ -1,16 +1,16 @@
RUN: llvm-objdump --macho -s %p/Inputs/hello.obj.macho-x86_64 | FileCheck %s
CHECK: Contents of section __text:
CHECK: Contents of section __TEXT,__text:
CHECK: 0000 554889e5 4883ec20 488d0500 000000c7 UH..H.. H.......
CHECK: 0010 45fc0000 0000897d f8488975 f0488955 E......}.H.u.H.U
CHECK: 0020 e84889c7 b000e800 000000b9 00000000 .H..............
CHECK: 0030 8945e489 c84883c4 205dc3 .E...H.. ].
CHECK: Contents of section __cstring:
CHECK: Contents of section __TEXT,__cstring:
CHECK: 003b 48656c6c 6f20776f 726c640a 00 Hello world..
CHECK: Contents of section __compact_unwind:
CHECK: Contents of section __LD,__compact_unwind:
CHECK: 0048 00000000 00000000 3b000000 00000001 ........;.......
CHECK: 0058 00000000 00000000 00000000 00000000 ................
CHECK: Contents of section __eh_frame:
CHECK: Contents of section __TEXT,__eh_frame:
CHECK: 0068 14000000 00000000 017a5200 01781001 .........zR..x..
CHECK: 0078 100c0708 90010000 24000000 1c000000 ........$.......
CHECK: 0088 78ffffff ffffffff 3b000000 00000000 x.......;.......

View file

@ -1619,6 +1619,16 @@ collectLocalBranchTargets(ArrayRef<uint8_t> Bytes, const MCInstrAnalysis *MIA,
}
}
static StringRef getSegmentName(const MachOObjectFile *MachO,
const SectionRef &Section) {
if (MachO) {
DataRefImpl DR = Section.getRawDataRefImpl();
StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
return SegmentName;
}
return "";
}
static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
MCContext &Ctx, MCDisassembler *PrimaryDisAsm,
MCDisassembler *SecondaryDisAsm,
@ -1783,12 +1793,7 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
}
}
StringRef SegmentName = "";
if (MachO) {
DataRefImpl DR = Section.getRawDataRefImpl();
SegmentName = MachO->getSectionFinalSegmentName(DR);
}
StringRef SegmentName = getSegmentName(MachO, Section);
StringRef SectionName = unwrapOrError(Section.getName(), Obj->getFileName());
// If the section has no symbol at the start, just insert a dummy one.
if (Symbols.empty() || Symbols[0].Addr != 0) {
@ -2388,6 +2393,8 @@ void objdump::printSectionHeaders(const ObjectFile *Obj) {
}
void objdump::printSectionContents(const ObjectFile *Obj) {
const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj);
for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
StringRef Name = unwrapOrError(Section.getName(), Obj->getFileName());
uint64_t BaseAddr = Section.getAddress();
@ -2395,7 +2402,11 @@ void objdump::printSectionContents(const ObjectFile *Obj) {
if (!Size)
continue;
outs() << "Contents of section " << Name << ":\n";
outs() << "Contents of section ";
StringRef SegmentName = getSegmentName(MachO, Section);
if (!SegmentName.empty())
outs() << SegmentName << ",";
outs() << Name << ":\n";
if (Section.isBSS()) {
outs() << format("<skipping contents of bss section at [%04" PRIx64
", %04" PRIx64 ")>\n",
@ -2553,11 +2564,9 @@ void objdump::printSymbol(const ObjectFile *O, const SymbolRef &Symbol,
} else if (Section == O->section_end()) {
outs() << "*UND*";
} else {
if (MachO) {
DataRefImpl DR = Section->getRawDataRefImpl();
StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
StringRef SegmentName = getSegmentName(MachO, *Section);
if (!SegmentName.empty())
outs() << SegmentName << ",";
}
StringRef SectionName = unwrapOrError(Section->getName(), FileName);
outs() << SectionName;
}