Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 1 | //===-- MachODump.cpp - Object file dumping utility for llvm --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the MachO-specific dumper for llvm-objdump. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm-objdump.h" |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/OwningPtr.h" |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Triple.h" |
Benjamin Kramer | 8c93097 | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/DIContext.h" |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCAsmInfo.h" |
| 21 | #include "llvm/MC/MCDisassembler.h" |
| 22 | #include "llvm/MC/MCInst.h" |
| 23 | #include "llvm/MC/MCInstPrinter.h" |
| 24 | #include "llvm/MC/MCInstrAnalysis.h" |
| 25 | #include "llvm/MC/MCInstrDesc.h" |
| 26 | #include "llvm/MC/MCInstrInfo.h" |
Jim Grosbach | c6449b6 | 2012-03-05 19:33:20 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCRegisterInfo.h" |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCSubtargetInfo.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 29 | #include "llvm/Object/MachO.h" |
Rafael Espindola | da2a237 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Casting.h" |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 31 | #include "llvm/Support/CommandLine.h" |
| 32 | #include "llvm/Support/Debug.h" |
| 33 | #include "llvm/Support/Format.h" |
| 34 | #include "llvm/Support/GraphWriter.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 35 | #include "llvm/Support/MachO.h" |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 36 | #include "llvm/Support/MemoryBuffer.h" |
| 37 | #include "llvm/Support/TargetRegistry.h" |
| 38 | #include "llvm/Support/TargetSelect.h" |
| 39 | #include "llvm/Support/raw_ostream.h" |
| 40 | #include "llvm/Support/system_error.h" |
| 41 | #include <algorithm> |
| 42 | #include <cstring> |
| 43 | using namespace llvm; |
| 44 | using namespace object; |
| 45 | |
| 46 | static cl::opt<bool> |
Benjamin Kramer | 8c93097 | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 47 | UseDbg("g", cl::desc("Print line information from debug info if available")); |
| 48 | |
| 49 | static cl::opt<std::string> |
| 50 | DSYMFile("dsym", cl::desc("Use .dSYM file for debug info")); |
| 51 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 52 | static const Target *GetTarget(const MachOObjectFile *MachOObj) { |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 53 | // Figure out the target triple. |
Cameron Zwarich | a993505 | 2012-02-03 06:35:22 +0000 | [diff] [blame] | 54 | if (TripleName.empty()) { |
| 55 | llvm::Triple TT("unknown-unknown-unknown"); |
Rafael Espindola | 317d3f4 | 2013-04-11 03:34:37 +0000 | [diff] [blame] | 56 | TT.setArch(Triple::ArchType(MachOObj->getArch())); |
Cameron Zwarich | a993505 | 2012-02-03 06:35:22 +0000 | [diff] [blame] | 57 | TripleName = TT.str(); |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 60 | // Get the target specific parser. |
| 61 | std::string Error; |
| 62 | const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); |
| 63 | if (TheTarget) |
| 64 | return TheTarget; |
| 65 | |
| 66 | errs() << "llvm-objdump: error: unable to get target for '" << TripleName |
| 67 | << "', see --version and --triple.\n"; |
| 68 | return 0; |
| 69 | } |
| 70 | |
Owen Anderson | 481837a | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 71 | struct SymbolSorter { |
| 72 | bool operator()(const SymbolRef &A, const SymbolRef &B) { |
| 73 | SymbolRef::Type AType, BType; |
| 74 | A.getType(AType); |
| 75 | B.getType(BType); |
| 76 | |
| 77 | uint64_t AAddr, BAddr; |
| 78 | if (AType != SymbolRef::ST_Function) |
| 79 | AAddr = 0; |
| 80 | else |
| 81 | A.getAddress(AAddr); |
| 82 | if (BType != SymbolRef::ST_Function) |
| 83 | BAddr = 0; |
| 84 | else |
| 85 | B.getAddress(BAddr); |
| 86 | return AAddr < BAddr; |
| 87 | } |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
Rafael Espindola | da2a237 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 90 | static void |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 91 | getSectionsAndSymbols(const macho::Header Header, |
| 92 | MachOObjectFile *MachOObj, |
Rafael Espindola | da2a237 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 93 | std::vector<SectionRef> &Sections, |
| 94 | std::vector<SymbolRef> &Symbols, |
| 95 | SmallVectorImpl<uint64_t> &FoundFns) { |
Owen Anderson | 481837a | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 96 | error_code ec; |
| 97 | for (symbol_iterator SI = MachOObj->begin_symbols(), |
| 98 | SE = MachOObj->end_symbols(); SI != SE; SI.increment(ec)) |
| 99 | Symbols.push_back(*SI); |
| 100 | |
| 101 | for (section_iterator SI = MachOObj->begin_sections(), |
| 102 | SE = MachOObj->end_sections(); SI != SE; SI.increment(ec)) { |
| 103 | SectionRef SR = *SI; |
| 104 | StringRef SectName; |
| 105 | SR.getName(SectName); |
| 106 | Sections.push_back(*SI); |
| 107 | } |
| 108 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 109 | MachOObjectFile::LoadCommandInfo Command = |
| 110 | MachOObj->getFirstLoadCommandInfo(); |
Rafael Espindola | db5f927 | 2013-04-19 11:36:47 +0000 | [diff] [blame] | 111 | for (unsigned i = 0; ; ++i) { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 112 | if (Command.C.Type == macho::LCT_FunctionStarts) { |
Benjamin Kramer | 8c93097 | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 113 | // We found a function starts segment, parse the addresses for later |
| 114 | // consumption. |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 115 | macho::LinkeditDataLoadCommand LLC = |
| 116 | MachOObj->getLinkeditDataLoadCommand(Command); |
Benjamin Kramer | 8c93097 | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 117 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 118 | MachOObj->ReadULEB128s(LLC.DataOffset, FoundFns); |
Benjamin Kramer | afbaf48 | 2011-09-21 22:16:43 +0000 | [diff] [blame] | 119 | } |
Rafael Espindola | db5f927 | 2013-04-19 11:36:47 +0000 | [diff] [blame] | 120 | |
| 121 | if (i == Header.NumLoadCommands - 1) |
| 122 | break; |
| 123 | else |
| 124 | Command = MachOObj->getNextLoadCommandInfo(Command); |
Benjamin Kramer | afbaf48 | 2011-09-21 22:16:43 +0000 | [diff] [blame] | 125 | } |
Benjamin Kramer | 8c93097 | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Rafael Espindola | da2a237 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 128 | static void DisassembleInputMachO2(StringRef Filename, |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 129 | MachOObjectFile *MachOOF); |
Rafael Espindola | da2a237 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 130 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 131 | void llvm::DisassembleInputMachO(StringRef Filename) { |
| 132 | OwningPtr<MemoryBuffer> Buff; |
| 133 | |
| 134 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) { |
| 135 | errs() << "llvm-objdump: " << Filename << ": " << ec.message() << "\n"; |
| 136 | return; |
| 137 | } |
| 138 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 139 | OwningPtr<MachOObjectFile> MachOOF(static_cast<MachOObjectFile*>( |
Owen Anderson | 481837a | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 140 | ObjectFile::createMachOObjectFile(Buff.take()))); |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 141 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 142 | DisassembleInputMachO2(Filename, MachOOF.get()); |
Rafael Espindola | da2a237 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Rafael Espindola | da2a237 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 145 | static void DisassembleInputMachO2(StringRef Filename, |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 146 | MachOObjectFile *MachOOF) { |
Rafael Espindola | da2a237 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 147 | const Target *TheTarget = GetTarget(MachOOF); |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 148 | if (!TheTarget) { |
| 149 | // GetTarget prints out stuff. |
| 150 | return; |
| 151 | } |
Benjamin Kramer | d226ed71 | 2011-10-10 13:10:09 +0000 | [diff] [blame] | 152 | OwningPtr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo()); |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 153 | OwningPtr<MCInstrAnalysis> |
Benjamin Kramer | d226ed71 | 2011-10-10 13:10:09 +0000 | [diff] [blame] | 154 | InstrAnalysis(TheTarget->createMCInstrAnalysis(InstrInfo.get())); |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 155 | |
| 156 | // Set up disassembler. |
Rafael Espindola | 4a97170 | 2013-05-13 01:16:13 +0000 | [diff] [blame] | 157 | OwningPtr<const MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); |
| 158 | OwningPtr<const MCAsmInfo> AsmInfo( |
| 159 | TheTarget->createMCAsmInfo(*MRI, TripleName)); |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 160 | OwningPtr<const MCSubtargetInfo> |
| 161 | STI(TheTarget->createMCSubtargetInfo(TripleName, "", "")); |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 162 | OwningPtr<const MCDisassembler> DisAsm(TheTarget->createMCDisassembler(*STI)); |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 163 | int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); |
Craig Topper | 17463b3 | 2012-04-02 06:09:36 +0000 | [diff] [blame] | 164 | OwningPtr<MCInstPrinter> |
| 165 | IP(TheTarget->createMCInstPrinter(AsmPrinterVariant, *AsmInfo, *InstrInfo, |
| 166 | *MRI, *STI)); |
Benjamin Kramer | a894c8e | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 167 | |
| 168 | if (!InstrAnalysis || !AsmInfo || !STI || !DisAsm || !IP) { |
Michael J. Spencer | 3773fb4 | 2011-10-07 19:25:47 +0000 | [diff] [blame] | 169 | errs() << "error: couldn't initialize disassembler for target " |
Benjamin Kramer | a894c8e | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 170 | << TripleName << '\n'; |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 171 | return; |
| 172 | } |
| 173 | |
Benjamin Kramer | a894c8e | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 174 | outs() << '\n' << Filename << ":\n\n"; |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 175 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 176 | macho::Header Header = MachOOF->getHeader(); |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 177 | |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 178 | // FIXME: FoundFns isn't used anymore. Using symbols/LC_FUNCTION_STARTS to |
| 179 | // determine function locations will eventually go in MCObjectDisassembler. |
| 180 | // FIXME: Using the -cfg command line option, this code used to be able to |
| 181 | // annotate relocations with the referenced symbol's name, and if this was |
| 182 | // inside a __[cf]string section, the data it points to. This is now replaced |
| 183 | // by the upcoming MCSymbolizer, which needs the appropriate setup done above. |
Owen Anderson | 481837a | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 184 | std::vector<SectionRef> Sections; |
| 185 | std::vector<SymbolRef> Symbols; |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 186 | SmallVector<uint64_t, 8> FoundFns; |
| 187 | |
Rafael Espindola | da2a237 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 188 | getSectionsAndSymbols(Header, MachOOF, Sections, Symbols, FoundFns); |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 189 | |
Benjamin Kramer | 8c93097 | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 190 | // Make a copy of the unsorted symbol list. FIXME: duplication |
Owen Anderson | 481837a | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 191 | std::vector<SymbolRef> UnsortedSymbols(Symbols); |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 192 | // Sort the symbols by address, just in case they didn't come in that way. |
Owen Anderson | 481837a | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 193 | std::sort(Symbols.begin(), Symbols.end(), SymbolSorter()); |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 194 | |
| 195 | #ifndef NDEBUG |
| 196 | raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls(); |
| 197 | #else |
| 198 | raw_ostream &DebugOut = nulls(); |
| 199 | #endif |
| 200 | |
Benjamin Kramer | 8c93097 | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 201 | OwningPtr<DIContext> diContext; |
Rafael Espindola | da2a237 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 202 | ObjectFile *DbgObj = MachOOF; |
Benjamin Kramer | 8c93097 | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 203 | // Try to find debug info and set up the DIContext for it. |
| 204 | if (UseDbg) { |
Benjamin Kramer | 8c93097 | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 205 | // A separate DSym file path was specified, parse it as a macho file, |
| 206 | // get the sections and supply it to the section name parsing machinery. |
| 207 | if (!DSYMFile.empty()) { |
| 208 | OwningPtr<MemoryBuffer> Buf; |
| 209 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(DSYMFile.c_str(), Buf)) { |
| 210 | errs() << "llvm-objdump: " << Filename << ": " << ec.message() << '\n'; |
| 211 | return; |
| 212 | } |
Eric Christopher | d1726a4 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 213 | DbgObj = ObjectFile::createMachOObjectFile(Buf.take()); |
Benjamin Kramer | 8c93097 | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Eric Christopher | d1726a4 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 216 | // Setup the DIContext |
| 217 | diContext.reset(DIContext::getDWARFContext(DbgObj)); |
Benjamin Kramer | 8c93097 | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 220 | for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) { |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 221 | |
| 222 | bool SectIsText = false; |
| 223 | Sections[SectIdx].isText(SectIsText); |
| 224 | if (SectIsText == false) |
| 225 | continue; |
| 226 | |
Owen Anderson | 481837a | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 227 | StringRef SectName; |
| 228 | if (Sections[SectIdx].getName(SectName) || |
Rafael Espindola | cef81b3 | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 229 | SectName != "__text") |
Benjamin Kramer | a894c8e | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 230 | continue; // Skip non-text sections |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 231 | |
Rafael Espindola | cef81b3 | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 232 | DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl(); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 233 | |
Rafael Espindola | f16c2bb | 2013-04-05 15:15:22 +0000 | [diff] [blame] | 234 | StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR); |
| 235 | if (SegmentName != "__TEXT") |
Rafael Espindola | cef81b3 | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 236 | continue; |
| 237 | |
Owen Anderson | 481837a | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 238 | StringRef Bytes; |
| 239 | Sections[SectIdx].getContents(Bytes); |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 240 | StringRefMemoryObject memoryObject(Bytes); |
| 241 | bool symbolTableWorked = false; |
| 242 | |
Benjamin Kramer | a894c8e | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 243 | // Parse relocations. |
Owen Anderson | 7d3f8b8 | 2011-11-07 17:21:36 +0000 | [diff] [blame] | 244 | std::vector<std::pair<uint64_t, SymbolRef> > Relocs; |
Owen Anderson | 481837a | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 245 | error_code ec; |
| 246 | for (relocation_iterator RI = Sections[SectIdx].begin_relocations(), |
| 247 | RE = Sections[SectIdx].end_relocations(); RI != RE; RI.increment(ec)) { |
| 248 | uint64_t RelocOffset, SectionAddress; |
Rafael Espindola | 956ca72 | 2013-04-25 12:28:45 +0000 | [diff] [blame] | 249 | RI->getOffset(RelocOffset); |
Owen Anderson | 481837a | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 250 | Sections[SectIdx].getAddress(SectionAddress); |
| 251 | RelocOffset -= SectionAddress; |
| 252 | |
Rafael Espindola | 6c1202c | 2013-06-05 01:33:53 +0000 | [diff] [blame^] | 253 | symbol_iterator RelocSym = RI->getSymbol(); |
Owen Anderson | 481837a | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 254 | |
Rafael Espindola | 6c1202c | 2013-06-05 01:33:53 +0000 | [diff] [blame^] | 255 | Relocs.push_back(std::make_pair(RelocOffset, *RelocSym)); |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 256 | } |
| 257 | array_pod_sort(Relocs.begin(), Relocs.end()); |
| 258 | |
Benjamin Kramer | a894c8e | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 259 | // Disassemble symbol by symbol. |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 260 | for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) { |
Owen Anderson | 481837a | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 261 | StringRef SymName; |
| 262 | Symbols[SymIdx].getName(SymName); |
| 263 | |
| 264 | SymbolRef::Type ST; |
| 265 | Symbols[SymIdx].getType(ST); |
| 266 | if (ST != SymbolRef::ST_Function) |
| 267 | continue; |
| 268 | |
Benjamin Kramer | a894c8e | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 269 | // Make sure the symbol is defined in this section. |
Owen Anderson | 481837a | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 270 | bool containsSym = false; |
| 271 | Sections[SectIdx].containsSymbol(Symbols[SymIdx], containsSym); |
| 272 | if (!containsSym) |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 273 | continue; |
| 274 | |
Benjamin Kramer | a894c8e | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 275 | // Start at the address of the symbol relative to the section's address. |
Cameron Zwarich | ec8eac6 | 2012-02-03 05:42:17 +0000 | [diff] [blame] | 276 | uint64_t SectionAddress = 0; |
Owen Anderson | 481837a | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 277 | uint64_t Start = 0; |
Cameron Zwarich | ec8eac6 | 2012-02-03 05:42:17 +0000 | [diff] [blame] | 278 | Sections[SectIdx].getAddress(SectionAddress); |
Danil Malyshev | b0436a7 | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 279 | Symbols[SymIdx].getAddress(Start); |
Cameron Zwarich | ec8eac6 | 2012-02-03 05:42:17 +0000 | [diff] [blame] | 280 | Start -= SectionAddress; |
Owen Anderson | 481837a | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 281 | |
Benjamin Kramer | a894c8e | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 282 | // Stop disassembling either at the beginning of the next symbol or at |
| 283 | // the end of the section. |
Kevin Enderby | 41854ae | 2012-05-15 18:57:14 +0000 | [diff] [blame] | 284 | bool containsNextSym = false; |
Owen Anderson | 481837a | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 285 | uint64_t NextSym = 0; |
| 286 | uint64_t NextSymIdx = SymIdx+1; |
| 287 | while (Symbols.size() > NextSymIdx) { |
| 288 | SymbolRef::Type NextSymType; |
| 289 | Symbols[NextSymIdx].getType(NextSymType); |
| 290 | if (NextSymType == SymbolRef::ST_Function) { |
| 291 | Sections[SectIdx].containsSymbol(Symbols[NextSymIdx], |
| 292 | containsNextSym); |
Danil Malyshev | b0436a7 | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 293 | Symbols[NextSymIdx].getAddress(NextSym); |
Cameron Zwarich | ec8eac6 | 2012-02-03 05:42:17 +0000 | [diff] [blame] | 294 | NextSym -= SectionAddress; |
Owen Anderson | 481837a | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 295 | break; |
| 296 | } |
| 297 | ++NextSymIdx; |
| 298 | } |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 299 | |
Owen Anderson | 481837a | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 300 | uint64_t SectSize; |
| 301 | Sections[SectIdx].getSize(SectSize); |
| 302 | uint64_t End = containsNextSym ? NextSym : SectSize; |
| 303 | uint64_t Size; |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 304 | |
| 305 | symbolTableWorked = true; |
| 306 | |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 307 | outs() << SymName << ":\n"; |
| 308 | DILineInfo lastLine; |
| 309 | for (uint64_t Index = Start; Index < End; Index += Size) { |
| 310 | MCInst Inst; |
Owen Anderson | 481837a | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 311 | |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 312 | if (DisAsm->getInstruction(Inst, Size, memoryObject, Index, |
| 313 | DebugOut, nulls())) { |
| 314 | uint64_t SectAddress = 0; |
| 315 | Sections[SectIdx].getAddress(SectAddress); |
| 316 | outs() << format("%8" PRIx64 ":\t", SectAddress + Index); |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 317 | |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 318 | DumpBytes(StringRef(Bytes.data() + Index, Size)); |
| 319 | IP->printInst(&Inst, outs(), ""); |
Owen Anderson | 481837a | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 320 | |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 321 | // Print debug info. |
| 322 | if (diContext) { |
| 323 | DILineInfo dli = |
| 324 | diContext->getLineInfoForAddress(SectAddress + Index); |
| 325 | // Print valid line info if it changed. |
| 326 | if (dli != lastLine && dli.getLine() != 0) |
| 327 | outs() << "\t## " << dli.getFileName() << ':' |
| 328 | << dli.getLine() << ':' << dli.getColumn(); |
| 329 | lastLine = dli; |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 330 | } |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 331 | outs() << "\n"; |
| 332 | } else { |
| 333 | errs() << "llvm-objdump: warning: invalid instruction encoding\n"; |
| 334 | if (Size == 0) |
| 335 | Size = 1; // skip illegible bytes |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 336 | } |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 337 | } |
| 338 | } |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 339 | if (!symbolTableWorked) { |
Kevin Enderby | 59c15e9 | 2012-05-18 00:13:56 +0000 | [diff] [blame] | 340 | // Reading the symbol table didn't work, disassemble the whole section. |
| 341 | uint64_t SectAddress; |
| 342 | Sections[SectIdx].getAddress(SectAddress); |
| 343 | uint64_t SectSize; |
| 344 | Sections[SectIdx].getSize(SectSize); |
| 345 | uint64_t InstSize; |
| 346 | for (uint64_t Index = 0; Index < SectSize; Index += InstSize) { |
Bill Wendling | f59083c | 2012-07-19 00:17:40 +0000 | [diff] [blame] | 347 | MCInst Inst; |
Kevin Enderby | 59c15e9 | 2012-05-18 00:13:56 +0000 | [diff] [blame] | 348 | |
Bill Wendling | f59083c | 2012-07-19 00:17:40 +0000 | [diff] [blame] | 349 | if (DisAsm->getInstruction(Inst, InstSize, memoryObject, Index, |
| 350 | DebugOut, nulls())) { |
| 351 | outs() << format("%8" PRIx64 ":\t", SectAddress + Index); |
| 352 | DumpBytes(StringRef(Bytes.data() + Index, InstSize)); |
| 353 | IP->printInst(&Inst, outs(), ""); |
| 354 | outs() << "\n"; |
| 355 | } else { |
| 356 | errs() << "llvm-objdump: warning: invalid instruction encoding\n"; |
| 357 | if (InstSize == 0) |
| 358 | InstSize = 1; // skip illegible bytes |
| 359 | } |
Kevin Enderby | 59c15e9 | 2012-05-18 00:13:56 +0000 | [diff] [blame] | 360 | } |
| 361 | } |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 362 | } |
| 363 | } |