Benjamin Kramer | 43a772e | 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 | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Triple.h" |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/DIContext.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCAsmInfo.h" |
Lang Hames | a1bc0f5 | 2014-04-15 04:40:56 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCContext.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 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 | fd93a59 | 2012-03-05 19:33:20 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCRegisterInfo.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCSubtargetInfo.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 29 | #include "llvm/Object/MachO.h" |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Casting.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 31 | #include "llvm/Support/CommandLine.h" |
| 32 | #include "llvm/Support/Debug.h" |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Endian.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Format.h" |
| 35 | #include "llvm/Support/GraphWriter.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 36 | #include "llvm/Support/MachO.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 37 | #include "llvm/Support/MemoryBuffer.h" |
| 38 | #include "llvm/Support/TargetRegistry.h" |
| 39 | #include "llvm/Support/TargetSelect.h" |
| 40 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 41 | #include <algorithm> |
| 42 | #include <cstring> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 43 | #include <system_error> |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 44 | using namespace llvm; |
| 45 | using namespace object; |
| 46 | |
| 47 | static cl::opt<bool> |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 48 | UseDbg("g", cl::desc("Print line information from debug info if available")); |
| 49 | |
| 50 | static cl::opt<std::string> |
| 51 | DSYMFile("dsym", cl::desc("Use .dSYM file for debug info")); |
| 52 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 53 | static const Target *GetTarget(const MachOObjectFile *MachOObj) { |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 54 | // Figure out the target triple. |
Cameron Zwarich | 88cc16a | 2012-02-03 06:35:22 +0000 | [diff] [blame] | 55 | if (TripleName.empty()) { |
| 56 | llvm::Triple TT("unknown-unknown-unknown"); |
Rafael Espindola | 93f4a62 | 2013-04-11 03:34:37 +0000 | [diff] [blame] | 57 | TT.setArch(Triple::ArchType(MachOObj->getArch())); |
Cameron Zwarich | 88cc16a | 2012-02-03 06:35:22 +0000 | [diff] [blame] | 58 | TripleName = TT.str(); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 61 | // Get the target specific parser. |
| 62 | std::string Error; |
| 63 | const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); |
| 64 | if (TheTarget) |
| 65 | return TheTarget; |
| 66 | |
| 67 | errs() << "llvm-objdump: error: unable to get target for '" << TripleName |
| 68 | << "', see --version and --triple.\n"; |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 69 | return nullptr; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 72 | struct SymbolSorter { |
| 73 | bool operator()(const SymbolRef &A, const SymbolRef &B) { |
| 74 | SymbolRef::Type AType, BType; |
| 75 | A.getType(AType); |
| 76 | B.getType(BType); |
| 77 | |
| 78 | uint64_t AAddr, BAddr; |
| 79 | if (AType != SymbolRef::ST_Function) |
| 80 | AAddr = 0; |
| 81 | else |
| 82 | A.getAddress(AAddr); |
| 83 | if (BType != SymbolRef::ST_Function) |
| 84 | BAddr = 0; |
| 85 | else |
| 86 | B.getAddress(BAddr); |
| 87 | return AAddr < BAddr; |
| 88 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 91 | // Types for the storted data in code table that is built before disassembly |
| 92 | // and the predicate function to sort them. |
| 93 | typedef std::pair<uint64_t, DiceRef> DiceTableEntry; |
| 94 | typedef std::vector<DiceTableEntry> DiceTable; |
| 95 | typedef DiceTable::iterator dice_table_iterator; |
| 96 | |
| 97 | static bool |
| 98 | compareDiceTableEntries(const DiceTableEntry i, |
| 99 | const DiceTableEntry j) { |
| 100 | return i.first == j.first; |
| 101 | } |
| 102 | |
| 103 | static void DumpDataInCode(const char *bytes, uint64_t Size, |
| 104 | unsigned short Kind) { |
| 105 | uint64_t Value; |
| 106 | |
| 107 | switch (Kind) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 108 | case MachO::DICE_KIND_DATA: |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 109 | switch (Size) { |
| 110 | case 4: |
| 111 | Value = bytes[3] << 24 | |
| 112 | bytes[2] << 16 | |
| 113 | bytes[1] << 8 | |
| 114 | bytes[0]; |
| 115 | outs() << "\t.long " << Value; |
| 116 | break; |
| 117 | case 2: |
| 118 | Value = bytes[1] << 8 | |
| 119 | bytes[0]; |
| 120 | outs() << "\t.short " << Value; |
| 121 | break; |
| 122 | case 1: |
| 123 | Value = bytes[0]; |
| 124 | outs() << "\t.byte " << Value; |
| 125 | break; |
| 126 | } |
| 127 | outs() << "\t@ KIND_DATA\n"; |
| 128 | break; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 129 | case MachO::DICE_KIND_JUMP_TABLE8: |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 130 | Value = bytes[0]; |
| 131 | outs() << "\t.byte " << Value << "\t@ KIND_JUMP_TABLE8"; |
| 132 | break; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 133 | case MachO::DICE_KIND_JUMP_TABLE16: |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 134 | Value = bytes[1] << 8 | |
| 135 | bytes[0]; |
| 136 | outs() << "\t.short " << Value << "\t@ KIND_JUMP_TABLE16"; |
| 137 | break; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 138 | case MachO::DICE_KIND_JUMP_TABLE32: |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 139 | Value = bytes[3] << 24 | |
| 140 | bytes[2] << 16 | |
| 141 | bytes[1] << 8 | |
| 142 | bytes[0]; |
| 143 | outs() << "\t.long " << Value << "\t@ KIND_JUMP_TABLE32"; |
| 144 | break; |
| 145 | default: |
| 146 | outs() << "\t@ data in code kind = " << Kind << "\n"; |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 151 | static void getSectionsAndSymbols(const MachO::mach_header Header, |
| 152 | MachOObjectFile *MachOObj, |
| 153 | std::vector<SectionRef> &Sections, |
| 154 | std::vector<SymbolRef> &Symbols, |
| 155 | SmallVectorImpl<uint64_t> &FoundFns, |
| 156 | uint64_t &BaseSegmentAddress) { |
| 157 | for (const SymbolRef &Symbol : MachOObj->symbols()) |
| 158 | Symbols.push_back(Symbol); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 159 | |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 160 | for (const SectionRef &Section : MachOObj->sections()) { |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 161 | StringRef SectName; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 162 | Section.getName(SectName); |
| 163 | Sections.push_back(Section); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 166 | MachOObjectFile::LoadCommandInfo Command = |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 167 | MachOObj->getFirstLoadCommandInfo(); |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 168 | bool BaseSegmentAddressSet = false; |
Rafael Espindola | feef8c2 | 2013-04-19 11:36:47 +0000 | [diff] [blame] | 169 | for (unsigned i = 0; ; ++i) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 170 | if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) { |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 171 | // We found a function starts segment, parse the addresses for later |
| 172 | // consumption. |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 173 | MachO::linkedit_data_command LLC = |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 174 | MachOObj->getLinkeditDataLoadCommand(Command); |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 175 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 176 | MachOObj->ReadULEB128s(LLC.dataoff, FoundFns); |
Benjamin Kramer | 8a529dc | 2011-09-21 22:16:43 +0000 | [diff] [blame] | 177 | } |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 178 | else if (Command.C.cmd == MachO::LC_SEGMENT) { |
| 179 | MachO::segment_command SLC = |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 180 | MachOObj->getSegmentLoadCommand(Command); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 181 | StringRef SegName = SLC.segname; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 182 | if(!BaseSegmentAddressSet && SegName != "__PAGEZERO") { |
| 183 | BaseSegmentAddressSet = true; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 184 | BaseSegmentAddress = SLC.vmaddr; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 185 | } |
| 186 | } |
Rafael Espindola | feef8c2 | 2013-04-19 11:36:47 +0000 | [diff] [blame] | 187 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 188 | if (i == Header.ncmds - 1) |
Rafael Espindola | feef8c2 | 2013-04-19 11:36:47 +0000 | [diff] [blame] | 189 | break; |
| 190 | else |
| 191 | Command = MachOObj->getNextLoadCommandInfo(Command); |
Benjamin Kramer | 8a529dc | 2011-09-21 22:16:43 +0000 | [diff] [blame] | 192 | } |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 195 | static void DisassembleInputMachO2(StringRef Filename, |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 196 | MachOObjectFile *MachOOF); |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 197 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 198 | void llvm::DisassembleInputMachO(StringRef Filename) { |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 199 | ErrorOr<std::unique_ptr<MemoryBuffer>> Buff = |
| 200 | MemoryBuffer::getFileOrSTDIN(Filename); |
| 201 | if (std::error_code EC = Buff.getError()) { |
| 202 | errs() << "llvm-objdump: " << Filename << ": " << EC.message() << "\n"; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | |
Rafael Espindola | 437b0d5 | 2014-07-31 03:12:45 +0000 | [diff] [blame] | 206 | std::unique_ptr<MachOObjectFile> MachOOF = |
| 207 | std::move(ObjectFile::createMachOObjectFile(Buff.get()).get()); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 208 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 209 | DisassembleInputMachO2(Filename, MachOOF.get()); |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 212 | static void DisassembleInputMachO2(StringRef Filename, |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 213 | MachOObjectFile *MachOOF) { |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 214 | const Target *TheTarget = GetTarget(MachOOF); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 215 | if (!TheTarget) { |
| 216 | // GetTarget prints out stuff. |
| 217 | return; |
| 218 | } |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 219 | std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo()); |
| 220 | std::unique_ptr<MCInstrAnalysis> InstrAnalysis( |
| 221 | TheTarget->createMCInstrAnalysis(InstrInfo.get())); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 222 | |
Kevin Enderby | c959562 | 2014-08-06 23:24:41 +0000 | [diff] [blame] | 223 | // Package up features to be passed to target/subtarget |
| 224 | std::string FeaturesStr; |
| 225 | if (MAttrs.size()) { |
| 226 | SubtargetFeatures Features; |
| 227 | for (unsigned i = 0; i != MAttrs.size(); ++i) |
| 228 | Features.AddFeature(MAttrs[i]); |
| 229 | FeaturesStr = Features.getString(); |
| 230 | } |
| 231 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 232 | // Set up disassembler. |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 233 | std::unique_ptr<const MCRegisterInfo> MRI( |
| 234 | TheTarget->createMCRegInfo(TripleName)); |
| 235 | std::unique_ptr<const MCAsmInfo> AsmInfo( |
Rafael Espindola | 227144c | 2013-05-13 01:16:13 +0000 | [diff] [blame] | 236 | TheTarget->createMCAsmInfo(*MRI, TripleName)); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 237 | std::unique_ptr<const MCSubtargetInfo> STI( |
Kevin Enderby | c959562 | 2014-08-06 23:24:41 +0000 | [diff] [blame] | 238 | TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr)); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 239 | MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 240 | std::unique_ptr<const MCDisassembler> DisAsm( |
Lang Hames | a1bc0f5 | 2014-04-15 04:40:56 +0000 | [diff] [blame] | 241 | TheTarget->createMCDisassembler(*STI, Ctx)); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 242 | int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 243 | std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( |
| 244 | AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI, *STI)); |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 245 | |
| 246 | if (!InstrAnalysis || !AsmInfo || !STI || !DisAsm || !IP) { |
Michael J. Spencer | c1363cf | 2011-10-07 19:25:47 +0000 | [diff] [blame] | 247 | errs() << "error: couldn't initialize disassembler for target " |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 248 | << TripleName << '\n'; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 249 | return; |
| 250 | } |
| 251 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 252 | outs() << '\n' << Filename << ":\n\n"; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 253 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 254 | MachO::mach_header Header = MachOOF->getHeader(); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 255 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 256 | // FIXME: FoundFns isn't used anymore. Using symbols/LC_FUNCTION_STARTS to |
| 257 | // determine function locations will eventually go in MCObjectDisassembler. |
| 258 | // FIXME: Using the -cfg command line option, this code used to be able to |
| 259 | // annotate relocations with the referenced symbol's name, and if this was |
| 260 | // inside a __[cf]string section, the data it points to. This is now replaced |
| 261 | // by the upcoming MCSymbolizer, which needs the appropriate setup done above. |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 262 | std::vector<SectionRef> Sections; |
| 263 | std::vector<SymbolRef> Symbols; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 264 | SmallVector<uint64_t, 8> FoundFns; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 265 | uint64_t BaseSegmentAddress; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 266 | |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 267 | getSectionsAndSymbols(Header, MachOOF, Sections, Symbols, FoundFns, |
| 268 | BaseSegmentAddress); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 269 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 270 | // Sort the symbols by address, just in case they didn't come in that way. |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 271 | std::sort(Symbols.begin(), Symbols.end(), SymbolSorter()); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 272 | |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 273 | // Build a data in code table that is sorted on by the address of each entry. |
| 274 | uint64_t BaseAddress = 0; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 275 | if (Header.filetype == MachO::MH_OBJECT) |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 276 | Sections[0].getAddress(BaseAddress); |
| 277 | else |
| 278 | BaseAddress = BaseSegmentAddress; |
| 279 | DiceTable Dices; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 280 | for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices(); |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 281 | DI != DE; ++DI) { |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 282 | uint32_t Offset; |
| 283 | DI->getOffset(Offset); |
| 284 | Dices.push_back(std::make_pair(BaseAddress + Offset, *DI)); |
| 285 | } |
| 286 | array_pod_sort(Dices.begin(), Dices.end()); |
| 287 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 288 | #ifndef NDEBUG |
| 289 | raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls(); |
| 290 | #else |
| 291 | raw_ostream &DebugOut = nulls(); |
| 292 | #endif |
| 293 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 294 | std::unique_ptr<DIContext> diContext; |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 295 | ObjectFile *DbgObj = MachOOF; |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 296 | // Try to find debug info and set up the DIContext for it. |
| 297 | if (UseDbg) { |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 298 | // A separate DSym file path was specified, parse it as a macho file, |
| 299 | // get the sections and supply it to the section name parsing machinery. |
| 300 | if (!DSYMFile.empty()) { |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 301 | ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = |
| 302 | MemoryBuffer::getFileOrSTDIN(DSYMFile); |
| 303 | if (std::error_code EC = Buf.getError()) { |
| 304 | errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n'; |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 305 | return; |
| 306 | } |
Rafael Espindola | 437b0d5 | 2014-07-31 03:12:45 +0000 | [diff] [blame] | 307 | DbgObj = ObjectFile::createMachOObjectFile(Buf.get()).get().release(); |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 310 | // Setup the DIContext |
Rafael Espindola | a04bb5b | 2014-07-31 20:19:36 +0000 | [diff] [blame] | 311 | diContext.reset(DIContext::getDWARFContext(*DbgObj)); |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 314 | for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) { |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 315 | |
| 316 | bool SectIsText = false; |
| 317 | Sections[SectIdx].isText(SectIsText); |
| 318 | if (SectIsText == false) |
| 319 | continue; |
| 320 | |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 321 | StringRef SectName; |
| 322 | if (Sections[SectIdx].getName(SectName) || |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 323 | SectName != "__text") |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 324 | continue; // Skip non-text sections |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 325 | |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 326 | DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl(); |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 327 | |
Rafael Espindola | b0f76a4 | 2013-04-05 15:15:22 +0000 | [diff] [blame] | 328 | StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR); |
| 329 | if (SegmentName != "__TEXT") |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 330 | continue; |
| 331 | |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 332 | StringRef Bytes; |
| 333 | Sections[SectIdx].getContents(Bytes); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 334 | StringRefMemoryObject memoryObject(Bytes); |
| 335 | bool symbolTableWorked = false; |
| 336 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 337 | // Parse relocations. |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 338 | std::vector<std::pair<uint64_t, SymbolRef>> Relocs; |
| 339 | for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) { |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 340 | uint64_t RelocOffset, SectionAddress; |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 341 | Reloc.getOffset(RelocOffset); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 342 | Sections[SectIdx].getAddress(SectionAddress); |
| 343 | RelocOffset -= SectionAddress; |
| 344 | |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 345 | symbol_iterator RelocSym = Reloc.getSymbol(); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 346 | |
Rafael Espindola | 806f006 | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 347 | Relocs.push_back(std::make_pair(RelocOffset, *RelocSym)); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 348 | } |
| 349 | array_pod_sort(Relocs.begin(), Relocs.end()); |
| 350 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 351 | // Disassemble symbol by symbol. |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 352 | for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) { |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 353 | StringRef SymName; |
| 354 | Symbols[SymIdx].getName(SymName); |
| 355 | |
| 356 | SymbolRef::Type ST; |
| 357 | Symbols[SymIdx].getType(ST); |
| 358 | if (ST != SymbolRef::ST_Function) |
| 359 | continue; |
| 360 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 361 | // Make sure the symbol is defined in this section. |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 362 | bool containsSym = false; |
| 363 | Sections[SectIdx].containsSymbol(Symbols[SymIdx], containsSym); |
| 364 | if (!containsSym) |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 365 | continue; |
| 366 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 367 | // Start at the address of the symbol relative to the section's address. |
Cameron Zwarich | 54478a5 | 2012-02-03 05:42:17 +0000 | [diff] [blame] | 368 | uint64_t SectionAddress = 0; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 369 | uint64_t Start = 0; |
Cameron Zwarich | 54478a5 | 2012-02-03 05:42:17 +0000 | [diff] [blame] | 370 | Sections[SectIdx].getAddress(SectionAddress); |
Danil Malyshev | cbe72fc | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 371 | Symbols[SymIdx].getAddress(Start); |
Cameron Zwarich | 54478a5 | 2012-02-03 05:42:17 +0000 | [diff] [blame] | 372 | Start -= SectionAddress; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 373 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 374 | // Stop disassembling either at the beginning of the next symbol or at |
| 375 | // the end of the section. |
Kevin Enderby | edd5872 | 2012-05-15 18:57:14 +0000 | [diff] [blame] | 376 | bool containsNextSym = false; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 377 | uint64_t NextSym = 0; |
| 378 | uint64_t NextSymIdx = SymIdx+1; |
| 379 | while (Symbols.size() > NextSymIdx) { |
| 380 | SymbolRef::Type NextSymType; |
| 381 | Symbols[NextSymIdx].getType(NextSymType); |
| 382 | if (NextSymType == SymbolRef::ST_Function) { |
| 383 | Sections[SectIdx].containsSymbol(Symbols[NextSymIdx], |
| 384 | containsNextSym); |
Danil Malyshev | cbe72fc | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 385 | Symbols[NextSymIdx].getAddress(NextSym); |
Cameron Zwarich | 54478a5 | 2012-02-03 05:42:17 +0000 | [diff] [blame] | 386 | NextSym -= SectionAddress; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 387 | break; |
| 388 | } |
| 389 | ++NextSymIdx; |
| 390 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 391 | |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 392 | uint64_t SectSize; |
| 393 | Sections[SectIdx].getSize(SectSize); |
| 394 | uint64_t End = containsNextSym ? NextSym : SectSize; |
| 395 | uint64_t Size; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 396 | |
| 397 | symbolTableWorked = true; |
| 398 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 399 | outs() << SymName << ":\n"; |
| 400 | DILineInfo lastLine; |
| 401 | for (uint64_t Index = Start; Index < End; Index += Size) { |
| 402 | MCInst Inst; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 403 | |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 404 | uint64_t SectAddress = 0; |
| 405 | Sections[SectIdx].getAddress(SectAddress); |
| 406 | outs() << format("%8" PRIx64 ":\t", SectAddress + Index); |
| 407 | |
| 408 | // Check the data in code table here to see if this is data not an |
| 409 | // instruction to be disassembled. |
| 410 | DiceTable Dice; |
| 411 | Dice.push_back(std::make_pair(SectAddress + Index, DiceRef())); |
| 412 | dice_table_iterator DTI = std::search(Dices.begin(), Dices.end(), |
| 413 | Dice.begin(), Dice.end(), |
| 414 | compareDiceTableEntries); |
| 415 | if (DTI != Dices.end()){ |
| 416 | uint16_t Length; |
| 417 | DTI->second.getLength(Length); |
| 418 | DumpBytes(StringRef(Bytes.data() + Index, Length)); |
| 419 | uint16_t Kind; |
| 420 | DTI->second.getKind(Kind); |
| 421 | DumpDataInCode(Bytes.data() + Index, Length, Kind); |
| 422 | continue; |
| 423 | } |
| 424 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 425 | if (DisAsm->getInstruction(Inst, Size, memoryObject, Index, |
| 426 | DebugOut, nulls())) { |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 427 | DumpBytes(StringRef(Bytes.data() + Index, Size)); |
| 428 | IP->printInst(&Inst, outs(), ""); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 429 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 430 | // Print debug info. |
| 431 | if (diContext) { |
| 432 | DILineInfo dli = |
| 433 | diContext->getLineInfoForAddress(SectAddress + Index); |
| 434 | // Print valid line info if it changed. |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 435 | if (dli != lastLine && dli.Line != 0) |
| 436 | outs() << "\t## " << dli.FileName << ':' << dli.Line << ':' |
| 437 | << dli.Column; |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 438 | lastLine = dli; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 439 | } |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 440 | outs() << "\n"; |
| 441 | } else { |
| 442 | errs() << "llvm-objdump: warning: invalid instruction encoding\n"; |
| 443 | if (Size == 0) |
| 444 | Size = 1; // skip illegible bytes |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 445 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 446 | } |
| 447 | } |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 448 | if (!symbolTableWorked) { |
Kevin Enderby | badd100 | 2012-05-18 00:13:56 +0000 | [diff] [blame] | 449 | // Reading the symbol table didn't work, disassemble the whole section. |
| 450 | uint64_t SectAddress; |
| 451 | Sections[SectIdx].getAddress(SectAddress); |
| 452 | uint64_t SectSize; |
| 453 | Sections[SectIdx].getSize(SectSize); |
| 454 | uint64_t InstSize; |
| 455 | for (uint64_t Index = 0; Index < SectSize; Index += InstSize) { |
Bill Wendling | 4e68e06 | 2012-07-19 00:17:40 +0000 | [diff] [blame] | 456 | MCInst Inst; |
Kevin Enderby | badd100 | 2012-05-18 00:13:56 +0000 | [diff] [blame] | 457 | |
Bill Wendling | 4e68e06 | 2012-07-19 00:17:40 +0000 | [diff] [blame] | 458 | if (DisAsm->getInstruction(Inst, InstSize, memoryObject, Index, |
| 459 | DebugOut, nulls())) { |
| 460 | outs() << format("%8" PRIx64 ":\t", SectAddress + Index); |
| 461 | DumpBytes(StringRef(Bytes.data() + Index, InstSize)); |
| 462 | IP->printInst(&Inst, outs(), ""); |
| 463 | outs() << "\n"; |
| 464 | } else { |
| 465 | errs() << "llvm-objdump: warning: invalid instruction encoding\n"; |
| 466 | if (InstSize == 0) |
| 467 | InstSize = 1; // skip illegible bytes |
| 468 | } |
Kevin Enderby | badd100 | 2012-05-18 00:13:56 +0000 | [diff] [blame] | 469 | } |
| 470 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 473 | |
| 474 | namespace { |
| 475 | struct CompactUnwindEntry { |
| 476 | uint32_t OffsetInSection; |
| 477 | |
| 478 | uint64_t FunctionAddr; |
| 479 | uint32_t Length; |
| 480 | uint32_t CompactEncoding; |
| 481 | uint64_t PersonalityAddr; |
| 482 | uint64_t LSDAAddr; |
| 483 | |
| 484 | RelocationRef FunctionReloc; |
| 485 | RelocationRef PersonalityReloc; |
| 486 | RelocationRef LSDAReloc; |
| 487 | |
| 488 | CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64) |
| 489 | : OffsetInSection(Offset) { |
| 490 | if (Is64) |
| 491 | read<uint64_t>(Contents.data() + Offset); |
| 492 | else |
| 493 | read<uint32_t>(Contents.data() + Offset); |
| 494 | } |
| 495 | |
| 496 | private: |
| 497 | template<typename T> |
| 498 | static uint64_t readNext(const char *&Buf) { |
| 499 | using llvm::support::little; |
| 500 | using llvm::support::unaligned; |
| 501 | |
| 502 | uint64_t Val = support::endian::read<T, little, unaligned>(Buf); |
| 503 | Buf += sizeof(T); |
| 504 | return Val; |
| 505 | } |
| 506 | |
| 507 | template<typename UIntPtr> |
| 508 | void read(const char *Buf) { |
| 509 | FunctionAddr = readNext<UIntPtr>(Buf); |
| 510 | Length = readNext<uint32_t>(Buf); |
| 511 | CompactEncoding = readNext<uint32_t>(Buf); |
| 512 | PersonalityAddr = readNext<UIntPtr>(Buf); |
| 513 | LSDAAddr = readNext<UIntPtr>(Buf); |
| 514 | } |
| 515 | }; |
| 516 | } |
| 517 | |
| 518 | /// Given a relocation from __compact_unwind, consisting of the RelocationRef |
| 519 | /// and data being relocated, determine the best base Name and Addend to use for |
| 520 | /// display purposes. |
| 521 | /// |
| 522 | /// 1. An Extern relocation will directly reference a symbol (and the data is |
| 523 | /// then already an addend), so use that. |
| 524 | /// 2. Otherwise the data is an offset in the object file's layout; try to find |
| 525 | // a symbol before it in the same section, and use the offset from there. |
| 526 | /// 3. Finally, if all that fails, fall back to an offset from the start of the |
| 527 | /// referenced section. |
| 528 | static void findUnwindRelocNameAddend(const MachOObjectFile *Obj, |
| 529 | std::map<uint64_t, SymbolRef> &Symbols, |
| 530 | const RelocationRef &Reloc, |
| 531 | uint64_t Addr, |
| 532 | StringRef &Name, uint64_t &Addend) { |
| 533 | if (Reloc.getSymbol() != Obj->symbol_end()) { |
| 534 | Reloc.getSymbol()->getName(Name); |
| 535 | Addend = Addr; |
| 536 | return; |
| 537 | } |
| 538 | |
| 539 | auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl()); |
| 540 | SectionRef RelocSection = Obj->getRelocationSection(RE); |
| 541 | |
| 542 | uint64_t SectionAddr; |
| 543 | RelocSection.getAddress(SectionAddr); |
| 544 | |
| 545 | auto Sym = Symbols.upper_bound(Addr); |
| 546 | if (Sym == Symbols.begin()) { |
| 547 | // The first symbol in the object is after this reference, the best we can |
| 548 | // do is section-relative notation. |
| 549 | RelocSection.getName(Name); |
| 550 | Addend = Addr - SectionAddr; |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | // Go back one so that SymbolAddress <= Addr. |
| 555 | --Sym; |
| 556 | |
| 557 | section_iterator SymSection = Obj->section_end(); |
| 558 | Sym->second.getSection(SymSection); |
| 559 | if (RelocSection == *SymSection) { |
| 560 | // There's a valid symbol in the same section before this reference. |
| 561 | Sym->second.getName(Name); |
| 562 | Addend = Addr - Sym->first; |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | // There is a symbol before this reference, but it's in a different |
| 567 | // section. Probably not helpful to mention it, so use the section name. |
| 568 | RelocSection.getName(Name); |
| 569 | Addend = Addr - SectionAddr; |
| 570 | } |
| 571 | |
| 572 | static void printUnwindRelocDest(const MachOObjectFile *Obj, |
| 573 | std::map<uint64_t, SymbolRef> &Symbols, |
| 574 | const RelocationRef &Reloc, |
| 575 | uint64_t Addr) { |
| 576 | StringRef Name; |
| 577 | uint64_t Addend; |
| 578 | |
| 579 | findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend); |
| 580 | |
| 581 | outs() << Name; |
| 582 | if (Addend) |
Tim Northover | 63a2562 | 2014-08-11 09:14:06 +0000 | [diff] [blame^] | 583 | outs() << " + " << format("0x%" PRIx64, Addend); |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | static void |
| 587 | printMachOCompactUnwindSection(const MachOObjectFile *Obj, |
| 588 | std::map<uint64_t, SymbolRef> &Symbols, |
| 589 | const SectionRef &CompactUnwind) { |
| 590 | |
| 591 | assert(Obj->isLittleEndian() && |
| 592 | "There should not be a big-endian .o with __compact_unwind"); |
| 593 | |
| 594 | bool Is64 = Obj->is64Bit(); |
| 595 | uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t); |
| 596 | uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t); |
| 597 | |
| 598 | StringRef Contents; |
| 599 | CompactUnwind.getContents(Contents); |
| 600 | |
| 601 | SmallVector<CompactUnwindEntry, 4> CompactUnwinds; |
| 602 | |
| 603 | // First populate the initial raw offsets, encodings and so on from the entry. |
| 604 | for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) { |
| 605 | CompactUnwindEntry Entry(Contents.data(), Offset, Is64); |
| 606 | CompactUnwinds.push_back(Entry); |
| 607 | } |
| 608 | |
| 609 | // Next we need to look at the relocations to find out what objects are |
| 610 | // actually being referred to. |
| 611 | for (const RelocationRef &Reloc : CompactUnwind.relocations()) { |
| 612 | uint64_t RelocAddress; |
| 613 | Reloc.getOffset(RelocAddress); |
| 614 | |
| 615 | uint32_t EntryIdx = RelocAddress / EntrySize; |
| 616 | uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize; |
| 617 | CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx]; |
| 618 | |
| 619 | if (OffsetInEntry == 0) |
| 620 | Entry.FunctionReloc = Reloc; |
| 621 | else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t)) |
| 622 | Entry.PersonalityReloc = Reloc; |
| 623 | else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t)) |
| 624 | Entry.LSDAReloc = Reloc; |
| 625 | else |
| 626 | llvm_unreachable("Unexpected relocation in __compact_unwind section"); |
| 627 | } |
| 628 | |
| 629 | // Finally, we're ready to print the data we've gathered. |
| 630 | outs() << "Contents of __compact_unwind section:\n"; |
| 631 | for (auto &Entry : CompactUnwinds) { |
Tim Northover | 06af260 | 2014-08-08 12:08:51 +0000 | [diff] [blame] | 632 | outs() << " Entry at offset " |
| 633 | << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n"; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 634 | |
| 635 | // 1. Start of the region this entry applies to. |
| 636 | outs() << " start: " |
Tim Northover | b911bf8 | 2014-08-08 12:00:09 +0000 | [diff] [blame] | 637 | << format("0x%" PRIx64, Entry.FunctionAddr) << ' '; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 638 | printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, |
| 639 | Entry.FunctionAddr); |
| 640 | outs() << '\n'; |
| 641 | |
| 642 | // 2. Length of the region this entry applies to. |
| 643 | outs() << " length: " |
Tim Northover | b911bf8 | 2014-08-08 12:00:09 +0000 | [diff] [blame] | 644 | << format("0x%" PRIx32, Entry.Length) << '\n'; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 645 | // 3. The 32-bit compact encoding. |
| 646 | outs() << " compact encoding: " |
Tim Northover | b911bf8 | 2014-08-08 12:00:09 +0000 | [diff] [blame] | 647 | << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n'; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 648 | |
| 649 | // 4. The personality function, if present. |
| 650 | if (Entry.PersonalityReloc.getObjectFile()) { |
| 651 | outs() << " personality function: " |
Tim Northover | b911bf8 | 2014-08-08 12:00:09 +0000 | [diff] [blame] | 652 | << format("0x%" PRIx64, Entry.PersonalityAddr) << ' '; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 653 | printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc, |
| 654 | Entry.PersonalityAddr); |
| 655 | outs() << '\n'; |
| 656 | } |
| 657 | |
| 658 | // 5. This entry's language-specific data area. |
| 659 | if (Entry.LSDAReloc.getObjectFile()) { |
| 660 | outs() << " LSDA: " |
Tim Northover | b911bf8 | 2014-08-08 12:00:09 +0000 | [diff] [blame] | 661 | << format("0x%" PRIx64, Entry.LSDAAddr) << ' '; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 662 | printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr); |
| 663 | outs() << '\n'; |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) { |
| 669 | std::map<uint64_t, SymbolRef> Symbols; |
| 670 | for (const SymbolRef &SymRef : Obj->symbols()) { |
| 671 | // Discard any undefined or absolute symbols. They're not going to take part |
| 672 | // in the convenience lookup for unwind info and just take up resources. |
| 673 | section_iterator Section = Obj->section_end(); |
| 674 | SymRef.getSection(Section); |
| 675 | if (Section == Obj->section_end()) |
| 676 | continue; |
| 677 | |
| 678 | uint64_t Addr; |
| 679 | SymRef.getAddress(Addr); |
| 680 | Symbols.insert(std::make_pair(Addr, SymRef)); |
| 681 | } |
| 682 | |
| 683 | for (const SectionRef &Section : Obj->sections()) { |
| 684 | StringRef SectName; |
| 685 | Section.getName(SectName); |
| 686 | if (SectName == "__compact_unwind") |
| 687 | printMachOCompactUnwindSection(Obj, Symbols, Section); |
| 688 | else if (SectName == "__unwind_info") |
| 689 | outs() << "llvm-objdump: warning: unhandled __unwind_info section\n"; |
| 690 | else if (SectName == "__eh_frame") |
| 691 | outs() << "llvm-objdump: warning: unhandled __eh_frame section\n"; |
| 692 | |
| 693 | } |
| 694 | } |