From 3f1a9b7eca0a969e18aabefa3ceb9054b94c17c0 Mon Sep 17 00:00:00 2001 From: Daniel Sanders Date: Thu, 3 Sep 2020 17:07:59 -0700 Subject: [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 --- llvm/tools/llvm-objdump/llvm-objdump.cpp | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'llvm/tools/llvm-objdump/llvm-objdump.cpp') diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp index 6b3ecd9..b63d08b 100644 --- a/llvm/tools/llvm-objdump/llvm-objdump.cpp +++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp @@ -1619,6 +1619,16 @@ collectLocalBranchTargets(ArrayRef 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(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("\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; } -- cgit v1.1