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 | |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 53 | static std::string ThumbTripleName; |
| 54 | |
| 55 | static const Target *GetTarget(const MachOObjectFile *MachOObj, |
| 56 | const char **McpuDefault, |
| 57 | const Target **ThumbTarget) { |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 58 | // Figure out the target triple. |
Cameron Zwarich | 88cc16a | 2012-02-03 06:35:22 +0000 | [diff] [blame] | 59 | if (TripleName.empty()) { |
| 60 | llvm::Triple TT("unknown-unknown-unknown"); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 61 | llvm::Triple ThumbTriple = Triple(); |
| 62 | TT = MachOObj->getArch(McpuDefault, &ThumbTriple); |
Cameron Zwarich | 88cc16a | 2012-02-03 06:35:22 +0000 | [diff] [blame] | 63 | TripleName = TT.str(); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 64 | ThumbTripleName = ThumbTriple.str(); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 67 | // Get the target specific parser. |
| 68 | std::string Error; |
| 69 | const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 70 | if (TheTarget && ThumbTripleName.empty()) |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 71 | return TheTarget; |
| 72 | |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 73 | *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error); |
| 74 | if (*ThumbTarget) |
| 75 | return TheTarget; |
| 76 | |
| 77 | errs() << "llvm-objdump: error: unable to get target for '"; |
| 78 | if (!TheTarget) |
| 79 | errs() << TripleName; |
| 80 | else |
| 81 | errs() << ThumbTripleName; |
| 82 | errs() << "', see --version and --triple.\n"; |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 83 | return nullptr; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 86 | struct SymbolSorter { |
| 87 | bool operator()(const SymbolRef &A, const SymbolRef &B) { |
| 88 | SymbolRef::Type AType, BType; |
| 89 | A.getType(AType); |
| 90 | B.getType(BType); |
| 91 | |
| 92 | uint64_t AAddr, BAddr; |
| 93 | if (AType != SymbolRef::ST_Function) |
| 94 | AAddr = 0; |
| 95 | else |
| 96 | A.getAddress(AAddr); |
| 97 | if (BType != SymbolRef::ST_Function) |
| 98 | BAddr = 0; |
| 99 | else |
| 100 | B.getAddress(BAddr); |
| 101 | return AAddr < BAddr; |
| 102 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 105 | // Types for the storted data in code table that is built before disassembly |
| 106 | // and the predicate function to sort them. |
| 107 | typedef std::pair<uint64_t, DiceRef> DiceTableEntry; |
| 108 | typedef std::vector<DiceTableEntry> DiceTable; |
| 109 | typedef DiceTable::iterator dice_table_iterator; |
| 110 | |
| 111 | static bool |
| 112 | compareDiceTableEntries(const DiceTableEntry i, |
| 113 | const DiceTableEntry j) { |
| 114 | return i.first == j.first; |
| 115 | } |
| 116 | |
| 117 | static void DumpDataInCode(const char *bytes, uint64_t Size, |
| 118 | unsigned short Kind) { |
| 119 | uint64_t Value; |
| 120 | |
| 121 | switch (Kind) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 122 | case MachO::DICE_KIND_DATA: |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 123 | switch (Size) { |
| 124 | case 4: |
| 125 | Value = bytes[3] << 24 | |
| 126 | bytes[2] << 16 | |
| 127 | bytes[1] << 8 | |
| 128 | bytes[0]; |
| 129 | outs() << "\t.long " << Value; |
| 130 | break; |
| 131 | case 2: |
| 132 | Value = bytes[1] << 8 | |
| 133 | bytes[0]; |
| 134 | outs() << "\t.short " << Value; |
| 135 | break; |
| 136 | case 1: |
| 137 | Value = bytes[0]; |
| 138 | outs() << "\t.byte " << Value; |
| 139 | break; |
| 140 | } |
| 141 | outs() << "\t@ KIND_DATA\n"; |
| 142 | break; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 143 | case MachO::DICE_KIND_JUMP_TABLE8: |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 144 | Value = bytes[0]; |
| 145 | outs() << "\t.byte " << Value << "\t@ KIND_JUMP_TABLE8"; |
| 146 | break; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 147 | case MachO::DICE_KIND_JUMP_TABLE16: |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 148 | Value = bytes[1] << 8 | |
| 149 | bytes[0]; |
| 150 | outs() << "\t.short " << Value << "\t@ KIND_JUMP_TABLE16"; |
| 151 | break; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 152 | case MachO::DICE_KIND_JUMP_TABLE32: |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 153 | Value = bytes[3] << 24 | |
| 154 | bytes[2] << 16 | |
| 155 | bytes[1] << 8 | |
| 156 | bytes[0]; |
| 157 | outs() << "\t.long " << Value << "\t@ KIND_JUMP_TABLE32"; |
| 158 | break; |
| 159 | default: |
| 160 | outs() << "\t@ data in code kind = " << Kind << "\n"; |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 165 | static void getSectionsAndSymbols(const MachO::mach_header Header, |
| 166 | MachOObjectFile *MachOObj, |
| 167 | std::vector<SectionRef> &Sections, |
| 168 | std::vector<SymbolRef> &Symbols, |
| 169 | SmallVectorImpl<uint64_t> &FoundFns, |
| 170 | uint64_t &BaseSegmentAddress) { |
| 171 | for (const SymbolRef &Symbol : MachOObj->symbols()) |
| 172 | Symbols.push_back(Symbol); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 173 | |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 174 | for (const SectionRef &Section : MachOObj->sections()) { |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 175 | StringRef SectName; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 176 | Section.getName(SectName); |
| 177 | Sections.push_back(Section); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 180 | MachOObjectFile::LoadCommandInfo Command = |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 181 | MachOObj->getFirstLoadCommandInfo(); |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 182 | bool BaseSegmentAddressSet = false; |
Rafael Espindola | feef8c2 | 2013-04-19 11:36:47 +0000 | [diff] [blame] | 183 | for (unsigned i = 0; ; ++i) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 184 | if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) { |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 185 | // We found a function starts segment, parse the addresses for later |
| 186 | // consumption. |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 187 | MachO::linkedit_data_command LLC = |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 188 | MachOObj->getLinkeditDataLoadCommand(Command); |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 189 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 190 | MachOObj->ReadULEB128s(LLC.dataoff, FoundFns); |
Benjamin Kramer | 8a529dc | 2011-09-21 22:16:43 +0000 | [diff] [blame] | 191 | } |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 192 | else if (Command.C.cmd == MachO::LC_SEGMENT) { |
| 193 | MachO::segment_command SLC = |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 194 | MachOObj->getSegmentLoadCommand(Command); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 195 | StringRef SegName = SLC.segname; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 196 | if(!BaseSegmentAddressSet && SegName != "__PAGEZERO") { |
| 197 | BaseSegmentAddressSet = true; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 198 | BaseSegmentAddress = SLC.vmaddr; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
Rafael Espindola | feef8c2 | 2013-04-19 11:36:47 +0000 | [diff] [blame] | 201 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 202 | if (i == Header.ncmds - 1) |
Rafael Espindola | feef8c2 | 2013-04-19 11:36:47 +0000 | [diff] [blame] | 203 | break; |
| 204 | else |
| 205 | Command = MachOObj->getNextLoadCommandInfo(Command); |
Benjamin Kramer | 8a529dc | 2011-09-21 22:16:43 +0000 | [diff] [blame] | 206 | } |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 209 | static void DisassembleInputMachO2(StringRef Filename, |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 210 | MachOObjectFile *MachOOF); |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 211 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 212 | void llvm::DisassembleInputMachO(StringRef Filename) { |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 213 | ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr = |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 214 | MemoryBuffer::getFileOrSTDIN(Filename); |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 215 | if (std::error_code EC = BuffOrErr.getError()) { |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 216 | errs() << "llvm-objdump: " << Filename << ": " << EC.message() << "\n"; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 217 | return; |
| 218 | } |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 219 | std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get()); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 220 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 221 | std::unique_ptr<MachOObjectFile> MachOOF = std::move( |
| 222 | ObjectFile::createMachOObjectFile(Buff.get()->getMemBufferRef()).get()); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 223 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 224 | DisassembleInputMachO2(Filename, MachOOF.get()); |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 227 | static void DisassembleInputMachO2(StringRef Filename, |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 228 | MachOObjectFile *MachOOF) { |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 229 | const char *McpuDefault = nullptr; |
| 230 | const Target *ThumbTarget = nullptr; |
| 231 | const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 232 | if (!TheTarget) { |
| 233 | // GetTarget prints out stuff. |
| 234 | return; |
| 235 | } |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 236 | if (MCPU.empty() && McpuDefault) |
| 237 | MCPU = McpuDefault; |
| 238 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 239 | std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo()); |
| 240 | std::unique_ptr<MCInstrAnalysis> InstrAnalysis( |
| 241 | TheTarget->createMCInstrAnalysis(InstrInfo.get())); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 242 | std::unique_ptr<const MCInstrInfo> ThumbInstrInfo; |
| 243 | std::unique_ptr<MCInstrAnalysis> ThumbInstrAnalysis; |
| 244 | if (ThumbTarget) { |
| 245 | ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo()); |
| 246 | ThumbInstrAnalysis.reset( |
| 247 | ThumbTarget->createMCInstrAnalysis(ThumbInstrInfo.get())); |
| 248 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 249 | |
Kevin Enderby | c959562 | 2014-08-06 23:24:41 +0000 | [diff] [blame] | 250 | // Package up features to be passed to target/subtarget |
| 251 | std::string FeaturesStr; |
| 252 | if (MAttrs.size()) { |
| 253 | SubtargetFeatures Features; |
| 254 | for (unsigned i = 0; i != MAttrs.size(); ++i) |
| 255 | Features.AddFeature(MAttrs[i]); |
| 256 | FeaturesStr = Features.getString(); |
| 257 | } |
| 258 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 259 | // Set up disassembler. |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 260 | std::unique_ptr<const MCRegisterInfo> MRI( |
| 261 | TheTarget->createMCRegInfo(TripleName)); |
| 262 | std::unique_ptr<const MCAsmInfo> AsmInfo( |
Rafael Espindola | 227144c | 2013-05-13 01:16:13 +0000 | [diff] [blame] | 263 | TheTarget->createMCAsmInfo(*MRI, TripleName)); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 264 | std::unique_ptr<const MCSubtargetInfo> STI( |
Kevin Enderby | c959562 | 2014-08-06 23:24:41 +0000 | [diff] [blame] | 265 | TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr)); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 266 | MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 267 | std::unique_ptr<const MCDisassembler> DisAsm( |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 268 | TheTarget->createMCDisassembler(*STI, Ctx)); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 269 | int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 270 | std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( |
| 271 | AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI, *STI)); |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 272 | |
| 273 | if (!InstrAnalysis || !AsmInfo || !STI || !DisAsm || !IP) { |
Michael J. Spencer | c1363cf | 2011-10-07 19:25:47 +0000 | [diff] [blame] | 274 | errs() << "error: couldn't initialize disassembler for target " |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 275 | << TripleName << '\n'; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 276 | return; |
| 277 | } |
| 278 | |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 279 | // Set up thumb disassembler. |
| 280 | std::unique_ptr<const MCRegisterInfo> ThumbMRI; |
| 281 | std::unique_ptr<const MCAsmInfo> ThumbAsmInfo; |
| 282 | std::unique_ptr<const MCSubtargetInfo> ThumbSTI; |
| 283 | std::unique_ptr<const MCDisassembler> ThumbDisAsm; |
| 284 | std::unique_ptr<MCInstPrinter> ThumbIP; |
| 285 | std::unique_ptr<MCContext> ThumbCtx; |
| 286 | if (ThumbTarget) { |
| 287 | ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName)); |
| 288 | ThumbAsmInfo.reset( |
| 289 | ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName)); |
| 290 | ThumbSTI.reset( |
| 291 | ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr)); |
| 292 | ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr)); |
| 293 | ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx)); |
| 294 | int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect(); |
| 295 | ThumbIP.reset(ThumbTarget->createMCInstPrinter( |
| 296 | ThumbAsmPrinterVariant, *ThumbAsmInfo, *ThumbInstrInfo, *ThumbMRI, |
| 297 | *ThumbSTI)); |
| 298 | } |
| 299 | |
| 300 | if (ThumbTarget && (!ThumbInstrAnalysis || !ThumbAsmInfo || !ThumbSTI || |
| 301 | !ThumbDisAsm || !ThumbIP)) { |
| 302 | errs() << "error: couldn't initialize disassembler for target " |
| 303 | << ThumbTripleName << '\n'; |
| 304 | return; |
| 305 | } |
| 306 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 307 | outs() << '\n' << Filename << ":\n\n"; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 308 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 309 | MachO::mach_header Header = MachOOF->getHeader(); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 310 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 311 | // FIXME: FoundFns isn't used anymore. Using symbols/LC_FUNCTION_STARTS to |
| 312 | // determine function locations will eventually go in MCObjectDisassembler. |
| 313 | // FIXME: Using the -cfg command line option, this code used to be able to |
| 314 | // annotate relocations with the referenced symbol's name, and if this was |
| 315 | // inside a __[cf]string section, the data it points to. This is now replaced |
| 316 | // by the upcoming MCSymbolizer, which needs the appropriate setup done above. |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 317 | std::vector<SectionRef> Sections; |
| 318 | std::vector<SymbolRef> Symbols; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 319 | SmallVector<uint64_t, 8> FoundFns; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 320 | uint64_t BaseSegmentAddress; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 321 | |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 322 | getSectionsAndSymbols(Header, MachOOF, Sections, Symbols, FoundFns, |
| 323 | BaseSegmentAddress); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 324 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 325 | // 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] | 326 | std::sort(Symbols.begin(), Symbols.end(), SymbolSorter()); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 327 | |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 328 | // Build a data in code table that is sorted on by the address of each entry. |
| 329 | uint64_t BaseAddress = 0; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 330 | if (Header.filetype == MachO::MH_OBJECT) |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 331 | Sections[0].getAddress(BaseAddress); |
| 332 | else |
| 333 | BaseAddress = BaseSegmentAddress; |
| 334 | DiceTable Dices; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 335 | for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices(); |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 336 | DI != DE; ++DI) { |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 337 | uint32_t Offset; |
| 338 | DI->getOffset(Offset); |
| 339 | Dices.push_back(std::make_pair(BaseAddress + Offset, *DI)); |
| 340 | } |
| 341 | array_pod_sort(Dices.begin(), Dices.end()); |
| 342 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 343 | #ifndef NDEBUG |
| 344 | raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls(); |
| 345 | #else |
| 346 | raw_ostream &DebugOut = nulls(); |
| 347 | #endif |
| 348 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 349 | std::unique_ptr<DIContext> diContext; |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 350 | ObjectFile *DbgObj = MachOOF; |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 351 | // Try to find debug info and set up the DIContext for it. |
| 352 | if (UseDbg) { |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 353 | // A separate DSym file path was specified, parse it as a macho file, |
| 354 | // get the sections and supply it to the section name parsing machinery. |
| 355 | if (!DSYMFile.empty()) { |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 356 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr = |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 357 | MemoryBuffer::getFileOrSTDIN(DSYMFile); |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 358 | if (std::error_code EC = BufOrErr.getError()) { |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 359 | errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n'; |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 360 | return; |
| 361 | } |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 362 | DbgObj = |
| 363 | ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef()) |
| 364 | .get() |
| 365 | .release(); |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 368 | // Setup the DIContext |
Rafael Espindola | a04bb5b | 2014-07-31 20:19:36 +0000 | [diff] [blame] | 369 | diContext.reset(DIContext::getDWARFContext(*DbgObj)); |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 372 | for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) { |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 373 | |
| 374 | bool SectIsText = false; |
| 375 | Sections[SectIdx].isText(SectIsText); |
| 376 | if (SectIsText == false) |
| 377 | continue; |
| 378 | |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 379 | StringRef SectName; |
| 380 | if (Sections[SectIdx].getName(SectName) || |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 381 | SectName != "__text") |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 382 | continue; // Skip non-text sections |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 383 | |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 384 | DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl(); |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 385 | |
Rafael Espindola | b0f76a4 | 2013-04-05 15:15:22 +0000 | [diff] [blame] | 386 | StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR); |
| 387 | if (SegmentName != "__TEXT") |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 388 | continue; |
| 389 | |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 390 | StringRef Bytes; |
| 391 | Sections[SectIdx].getContents(Bytes); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 392 | StringRefMemoryObject memoryObject(Bytes); |
| 393 | bool symbolTableWorked = false; |
| 394 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 395 | // Parse relocations. |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 396 | std::vector<std::pair<uint64_t, SymbolRef>> Relocs; |
| 397 | for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) { |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 398 | uint64_t RelocOffset, SectionAddress; |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 399 | Reloc.getOffset(RelocOffset); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 400 | Sections[SectIdx].getAddress(SectionAddress); |
| 401 | RelocOffset -= SectionAddress; |
| 402 | |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 403 | symbol_iterator RelocSym = Reloc.getSymbol(); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 404 | |
Rafael Espindola | 806f006 | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 405 | Relocs.push_back(std::make_pair(RelocOffset, *RelocSym)); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 406 | } |
| 407 | array_pod_sort(Relocs.begin(), Relocs.end()); |
| 408 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 409 | // Disassemble symbol by symbol. |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 410 | for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) { |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 411 | StringRef SymName; |
| 412 | Symbols[SymIdx].getName(SymName); |
| 413 | |
| 414 | SymbolRef::Type ST; |
| 415 | Symbols[SymIdx].getType(ST); |
| 416 | if (ST != SymbolRef::ST_Function) |
| 417 | continue; |
| 418 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 419 | // Make sure the symbol is defined in this section. |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 420 | bool containsSym = false; |
| 421 | Sections[SectIdx].containsSymbol(Symbols[SymIdx], containsSym); |
| 422 | if (!containsSym) |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 423 | continue; |
| 424 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 425 | // 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] | 426 | uint64_t SectionAddress = 0; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 427 | uint64_t Start = 0; |
Cameron Zwarich | 54478a5 | 2012-02-03 05:42:17 +0000 | [diff] [blame] | 428 | Sections[SectIdx].getAddress(SectionAddress); |
Danil Malyshev | cbe72fc | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 429 | Symbols[SymIdx].getAddress(Start); |
Cameron Zwarich | 54478a5 | 2012-02-03 05:42:17 +0000 | [diff] [blame] | 430 | Start -= SectionAddress; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 431 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 432 | // Stop disassembling either at the beginning of the next symbol or at |
| 433 | // the end of the section. |
Kevin Enderby | edd5872 | 2012-05-15 18:57:14 +0000 | [diff] [blame] | 434 | bool containsNextSym = false; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 435 | uint64_t NextSym = 0; |
| 436 | uint64_t NextSymIdx = SymIdx+1; |
| 437 | while (Symbols.size() > NextSymIdx) { |
| 438 | SymbolRef::Type NextSymType; |
| 439 | Symbols[NextSymIdx].getType(NextSymType); |
| 440 | if (NextSymType == SymbolRef::ST_Function) { |
| 441 | Sections[SectIdx].containsSymbol(Symbols[NextSymIdx], |
| 442 | containsNextSym); |
Danil Malyshev | cbe72fc | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 443 | Symbols[NextSymIdx].getAddress(NextSym); |
Cameron Zwarich | 54478a5 | 2012-02-03 05:42:17 +0000 | [diff] [blame] | 444 | NextSym -= SectionAddress; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 445 | break; |
| 446 | } |
| 447 | ++NextSymIdx; |
| 448 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 449 | |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 450 | uint64_t SectSize; |
| 451 | Sections[SectIdx].getSize(SectSize); |
| 452 | uint64_t End = containsNextSym ? NextSym : SectSize; |
| 453 | uint64_t Size; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 454 | |
| 455 | symbolTableWorked = true; |
| 456 | |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 457 | DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl(); |
| 458 | bool isThumb = |
| 459 | (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget; |
| 460 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 461 | outs() << SymName << ":\n"; |
| 462 | DILineInfo lastLine; |
| 463 | for (uint64_t Index = Start; Index < End; Index += Size) { |
| 464 | MCInst Inst; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 465 | |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 466 | uint64_t SectAddress = 0; |
| 467 | Sections[SectIdx].getAddress(SectAddress); |
| 468 | outs() << format("%8" PRIx64 ":\t", SectAddress + Index); |
| 469 | |
| 470 | // Check the data in code table here to see if this is data not an |
| 471 | // instruction to be disassembled. |
| 472 | DiceTable Dice; |
| 473 | Dice.push_back(std::make_pair(SectAddress + Index, DiceRef())); |
| 474 | dice_table_iterator DTI = std::search(Dices.begin(), Dices.end(), |
| 475 | Dice.begin(), Dice.end(), |
| 476 | compareDiceTableEntries); |
| 477 | if (DTI != Dices.end()){ |
| 478 | uint16_t Length; |
| 479 | DTI->second.getLength(Length); |
| 480 | DumpBytes(StringRef(Bytes.data() + Index, Length)); |
| 481 | uint16_t Kind; |
| 482 | DTI->second.getKind(Kind); |
| 483 | DumpDataInCode(Bytes.data() + Index, Length, Kind); |
| 484 | continue; |
| 485 | } |
| 486 | |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 487 | bool gotInst; |
| 488 | if (isThumb) |
| 489 | gotInst = ThumbDisAsm->getInstruction(Inst, Size, memoryObject, Index, |
| 490 | DebugOut, nulls()); |
| 491 | else |
| 492 | gotInst = DisAsm->getInstruction(Inst, Size, memoryObject, Index, |
| 493 | DebugOut, nulls()); |
| 494 | if (gotInst) { |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 495 | DumpBytes(StringRef(Bytes.data() + Index, Size)); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 496 | if (isThumb) |
| 497 | ThumbIP->printInst(&Inst, outs(), ""); |
| 498 | else |
| 499 | IP->printInst(&Inst, outs(), ""); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 500 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 501 | // Print debug info. |
| 502 | if (diContext) { |
| 503 | DILineInfo dli = |
| 504 | diContext->getLineInfoForAddress(SectAddress + Index); |
| 505 | // Print valid line info if it changed. |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 506 | if (dli != lastLine && dli.Line != 0) |
| 507 | outs() << "\t## " << dli.FileName << ':' << dli.Line << ':' |
| 508 | << dli.Column; |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 509 | lastLine = dli; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 510 | } |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 511 | outs() << "\n"; |
| 512 | } else { |
| 513 | errs() << "llvm-objdump: warning: invalid instruction encoding\n"; |
| 514 | if (Size == 0) |
| 515 | Size = 1; // skip illegible bytes |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 516 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 517 | } |
| 518 | } |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 519 | if (!symbolTableWorked) { |
Kevin Enderby | badd100 | 2012-05-18 00:13:56 +0000 | [diff] [blame] | 520 | // Reading the symbol table didn't work, disassemble the whole section. |
| 521 | uint64_t SectAddress; |
| 522 | Sections[SectIdx].getAddress(SectAddress); |
| 523 | uint64_t SectSize; |
| 524 | Sections[SectIdx].getSize(SectSize); |
| 525 | uint64_t InstSize; |
| 526 | for (uint64_t Index = 0; Index < SectSize; Index += InstSize) { |
Bill Wendling | 4e68e06 | 2012-07-19 00:17:40 +0000 | [diff] [blame] | 527 | MCInst Inst; |
Kevin Enderby | badd100 | 2012-05-18 00:13:56 +0000 | [diff] [blame] | 528 | |
Bill Wendling | 4e68e06 | 2012-07-19 00:17:40 +0000 | [diff] [blame] | 529 | if (DisAsm->getInstruction(Inst, InstSize, memoryObject, Index, |
| 530 | DebugOut, nulls())) { |
| 531 | outs() << format("%8" PRIx64 ":\t", SectAddress + Index); |
| 532 | DumpBytes(StringRef(Bytes.data() + Index, InstSize)); |
| 533 | IP->printInst(&Inst, outs(), ""); |
| 534 | outs() << "\n"; |
| 535 | } else { |
| 536 | errs() << "llvm-objdump: warning: invalid instruction encoding\n"; |
| 537 | if (InstSize == 0) |
| 538 | InstSize = 1; // skip illegible bytes |
| 539 | } |
Kevin Enderby | badd100 | 2012-05-18 00:13:56 +0000 | [diff] [blame] | 540 | } |
| 541 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 542 | } |
| 543 | } |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 544 | |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 545 | |
| 546 | //===----------------------------------------------------------------------===// |
| 547 | // __compact_unwind section dumping |
| 548 | //===----------------------------------------------------------------------===// |
| 549 | |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 550 | namespace { |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 551 | |
| 552 | template <typename T> static uint64_t readNext(const char *&Buf) { |
| 553 | using llvm::support::little; |
| 554 | using llvm::support::unaligned; |
| 555 | |
| 556 | uint64_t Val = support::endian::read<T, little, unaligned>(Buf); |
| 557 | Buf += sizeof(T); |
| 558 | return Val; |
| 559 | } |
| 560 | |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 561 | struct CompactUnwindEntry { |
| 562 | uint32_t OffsetInSection; |
| 563 | |
| 564 | uint64_t FunctionAddr; |
| 565 | uint32_t Length; |
| 566 | uint32_t CompactEncoding; |
| 567 | uint64_t PersonalityAddr; |
| 568 | uint64_t LSDAAddr; |
| 569 | |
| 570 | RelocationRef FunctionReloc; |
| 571 | RelocationRef PersonalityReloc; |
| 572 | RelocationRef LSDAReloc; |
| 573 | |
| 574 | CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64) |
| 575 | : OffsetInSection(Offset) { |
| 576 | if (Is64) |
| 577 | read<uint64_t>(Contents.data() + Offset); |
| 578 | else |
| 579 | read<uint32_t>(Contents.data() + Offset); |
| 580 | } |
| 581 | |
| 582 | private: |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 583 | template<typename UIntPtr> |
| 584 | void read(const char *Buf) { |
| 585 | FunctionAddr = readNext<UIntPtr>(Buf); |
| 586 | Length = readNext<uint32_t>(Buf); |
| 587 | CompactEncoding = readNext<uint32_t>(Buf); |
| 588 | PersonalityAddr = readNext<UIntPtr>(Buf); |
| 589 | LSDAAddr = readNext<UIntPtr>(Buf); |
| 590 | } |
| 591 | }; |
| 592 | } |
| 593 | |
| 594 | /// Given a relocation from __compact_unwind, consisting of the RelocationRef |
| 595 | /// and data being relocated, determine the best base Name and Addend to use for |
| 596 | /// display purposes. |
| 597 | /// |
| 598 | /// 1. An Extern relocation will directly reference a symbol (and the data is |
| 599 | /// then already an addend), so use that. |
| 600 | /// 2. Otherwise the data is an offset in the object file's layout; try to find |
| 601 | // a symbol before it in the same section, and use the offset from there. |
| 602 | /// 3. Finally, if all that fails, fall back to an offset from the start of the |
| 603 | /// referenced section. |
| 604 | static void findUnwindRelocNameAddend(const MachOObjectFile *Obj, |
| 605 | std::map<uint64_t, SymbolRef> &Symbols, |
| 606 | const RelocationRef &Reloc, |
| 607 | uint64_t Addr, |
| 608 | StringRef &Name, uint64_t &Addend) { |
| 609 | if (Reloc.getSymbol() != Obj->symbol_end()) { |
| 610 | Reloc.getSymbol()->getName(Name); |
| 611 | Addend = Addr; |
| 612 | return; |
| 613 | } |
| 614 | |
| 615 | auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl()); |
| 616 | SectionRef RelocSection = Obj->getRelocationSection(RE); |
| 617 | |
| 618 | uint64_t SectionAddr; |
| 619 | RelocSection.getAddress(SectionAddr); |
| 620 | |
| 621 | auto Sym = Symbols.upper_bound(Addr); |
| 622 | if (Sym == Symbols.begin()) { |
| 623 | // The first symbol in the object is after this reference, the best we can |
| 624 | // do is section-relative notation. |
| 625 | RelocSection.getName(Name); |
| 626 | Addend = Addr - SectionAddr; |
| 627 | return; |
| 628 | } |
| 629 | |
| 630 | // Go back one so that SymbolAddress <= Addr. |
| 631 | --Sym; |
| 632 | |
| 633 | section_iterator SymSection = Obj->section_end(); |
| 634 | Sym->second.getSection(SymSection); |
| 635 | if (RelocSection == *SymSection) { |
| 636 | // There's a valid symbol in the same section before this reference. |
| 637 | Sym->second.getName(Name); |
| 638 | Addend = Addr - Sym->first; |
| 639 | return; |
| 640 | } |
| 641 | |
| 642 | // There is a symbol before this reference, but it's in a different |
| 643 | // section. Probably not helpful to mention it, so use the section name. |
| 644 | RelocSection.getName(Name); |
| 645 | Addend = Addr - SectionAddr; |
| 646 | } |
| 647 | |
| 648 | static void printUnwindRelocDest(const MachOObjectFile *Obj, |
| 649 | std::map<uint64_t, SymbolRef> &Symbols, |
| 650 | const RelocationRef &Reloc, |
| 651 | uint64_t Addr) { |
| 652 | StringRef Name; |
| 653 | uint64_t Addend; |
| 654 | |
| 655 | findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend); |
| 656 | |
| 657 | outs() << Name; |
| 658 | if (Addend) |
Tim Northover | 63a2562 | 2014-08-11 09:14:06 +0000 | [diff] [blame] | 659 | outs() << " + " << format("0x%" PRIx64, Addend); |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | static void |
| 663 | printMachOCompactUnwindSection(const MachOObjectFile *Obj, |
| 664 | std::map<uint64_t, SymbolRef> &Symbols, |
| 665 | const SectionRef &CompactUnwind) { |
| 666 | |
| 667 | assert(Obj->isLittleEndian() && |
| 668 | "There should not be a big-endian .o with __compact_unwind"); |
| 669 | |
| 670 | bool Is64 = Obj->is64Bit(); |
| 671 | uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t); |
| 672 | uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t); |
| 673 | |
| 674 | StringRef Contents; |
| 675 | CompactUnwind.getContents(Contents); |
| 676 | |
| 677 | SmallVector<CompactUnwindEntry, 4> CompactUnwinds; |
| 678 | |
| 679 | // First populate the initial raw offsets, encodings and so on from the entry. |
| 680 | for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) { |
| 681 | CompactUnwindEntry Entry(Contents.data(), Offset, Is64); |
| 682 | CompactUnwinds.push_back(Entry); |
| 683 | } |
| 684 | |
| 685 | // Next we need to look at the relocations to find out what objects are |
| 686 | // actually being referred to. |
| 687 | for (const RelocationRef &Reloc : CompactUnwind.relocations()) { |
| 688 | uint64_t RelocAddress; |
| 689 | Reloc.getOffset(RelocAddress); |
| 690 | |
| 691 | uint32_t EntryIdx = RelocAddress / EntrySize; |
| 692 | uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize; |
| 693 | CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx]; |
| 694 | |
| 695 | if (OffsetInEntry == 0) |
| 696 | Entry.FunctionReloc = Reloc; |
| 697 | else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t)) |
| 698 | Entry.PersonalityReloc = Reloc; |
| 699 | else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t)) |
| 700 | Entry.LSDAReloc = Reloc; |
| 701 | else |
| 702 | llvm_unreachable("Unexpected relocation in __compact_unwind section"); |
| 703 | } |
| 704 | |
| 705 | // Finally, we're ready to print the data we've gathered. |
| 706 | outs() << "Contents of __compact_unwind section:\n"; |
| 707 | for (auto &Entry : CompactUnwinds) { |
Tim Northover | 06af260 | 2014-08-08 12:08:51 +0000 | [diff] [blame] | 708 | outs() << " Entry at offset " |
| 709 | << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n"; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 710 | |
| 711 | // 1. Start of the region this entry applies to. |
| 712 | outs() << " start: " |
Tim Northover | b911bf8 | 2014-08-08 12:00:09 +0000 | [diff] [blame] | 713 | << format("0x%" PRIx64, Entry.FunctionAddr) << ' '; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 714 | printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, |
| 715 | Entry.FunctionAddr); |
| 716 | outs() << '\n'; |
| 717 | |
| 718 | // 2. Length of the region this entry applies to. |
| 719 | outs() << " length: " |
Tim Northover | b911bf8 | 2014-08-08 12:00:09 +0000 | [diff] [blame] | 720 | << format("0x%" PRIx32, Entry.Length) << '\n'; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 721 | // 3. The 32-bit compact encoding. |
| 722 | outs() << " compact encoding: " |
Tim Northover | b911bf8 | 2014-08-08 12:00:09 +0000 | [diff] [blame] | 723 | << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n'; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 724 | |
| 725 | // 4. The personality function, if present. |
| 726 | if (Entry.PersonalityReloc.getObjectFile()) { |
| 727 | outs() << " personality function: " |
Tim Northover | b911bf8 | 2014-08-08 12:00:09 +0000 | [diff] [blame] | 728 | << format("0x%" PRIx64, Entry.PersonalityAddr) << ' '; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 729 | printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc, |
| 730 | Entry.PersonalityAddr); |
| 731 | outs() << '\n'; |
| 732 | } |
| 733 | |
| 734 | // 5. This entry's language-specific data area. |
| 735 | if (Entry.LSDAReloc.getObjectFile()) { |
| 736 | outs() << " LSDA: " |
Tim Northover | b911bf8 | 2014-08-08 12:00:09 +0000 | [diff] [blame] | 737 | << format("0x%" PRIx64, Entry.LSDAAddr) << ' '; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 738 | printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr); |
| 739 | outs() << '\n'; |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 744 | //===----------------------------------------------------------------------===// |
| 745 | // __unwind_info section dumping |
| 746 | //===----------------------------------------------------------------------===// |
| 747 | |
| 748 | static void printRegularSecondLevelUnwindPage(const char *PageStart) { |
| 749 | const char *Pos = PageStart; |
| 750 | uint32_t Kind = readNext<uint32_t>(Pos); |
| 751 | (void)Kind; |
| 752 | assert(Kind == 2 && "kind for a regular 2nd level index should be 2"); |
| 753 | |
| 754 | uint16_t EntriesStart = readNext<uint16_t>(Pos); |
| 755 | uint16_t NumEntries = readNext<uint16_t>(Pos); |
| 756 | |
| 757 | Pos = PageStart + EntriesStart; |
| 758 | for (unsigned i = 0; i < NumEntries; ++i) { |
| 759 | uint32_t FunctionOffset = readNext<uint32_t>(Pos); |
| 760 | uint32_t Encoding = readNext<uint32_t>(Pos); |
| 761 | |
| 762 | outs() << " [" << i << "]: " |
| 763 | << "function offset=" |
| 764 | << format("0x%08" PRIx32, FunctionOffset) << ", " |
| 765 | << "encoding=" |
| 766 | << format("0x%08" PRIx32, Encoding) |
| 767 | << '\n'; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | static void printCompressedSecondLevelUnwindPage( |
| 772 | const char *PageStart, uint32_t FunctionBase, |
| 773 | const SmallVectorImpl<uint32_t> &CommonEncodings) { |
| 774 | const char *Pos = PageStart; |
| 775 | uint32_t Kind = readNext<uint32_t>(Pos); |
| 776 | (void)Kind; |
| 777 | assert(Kind == 3 && "kind for a compressed 2nd level index should be 3"); |
| 778 | |
| 779 | uint16_t EntriesStart = readNext<uint16_t>(Pos); |
| 780 | uint16_t NumEntries = readNext<uint16_t>(Pos); |
| 781 | |
| 782 | uint16_t EncodingsStart = readNext<uint16_t>(Pos); |
| 783 | readNext<uint16_t>(Pos); |
Aaron Ballman | 80930af | 2014-08-14 13:53:19 +0000 | [diff] [blame] | 784 | const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>( |
| 785 | PageStart + EncodingsStart); |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 786 | |
| 787 | Pos = PageStart + EntriesStart; |
| 788 | for (unsigned i = 0; i < NumEntries; ++i) { |
| 789 | uint32_t Entry = readNext<uint32_t>(Pos); |
| 790 | uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff); |
| 791 | uint32_t EncodingIdx = Entry >> 24; |
| 792 | |
| 793 | uint32_t Encoding; |
| 794 | if (EncodingIdx < CommonEncodings.size()) |
| 795 | Encoding = CommonEncodings[EncodingIdx]; |
| 796 | else |
| 797 | Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()]; |
| 798 | |
| 799 | outs() << " [" << i << "]: " |
| 800 | << "function offset=" |
| 801 | << format("0x%08" PRIx32, FunctionOffset) << ", " |
| 802 | << "encoding[" << EncodingIdx << "]=" |
| 803 | << format("0x%08" PRIx32, Encoding) |
| 804 | << '\n'; |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | static void |
| 809 | printMachOUnwindInfoSection(const MachOObjectFile *Obj, |
| 810 | std::map<uint64_t, SymbolRef> &Symbols, |
| 811 | const SectionRef &UnwindInfo) { |
| 812 | |
| 813 | assert(Obj->isLittleEndian() && |
| 814 | "There should not be a big-endian .o with __unwind_info"); |
| 815 | |
| 816 | outs() << "Contents of __unwind_info section:\n"; |
| 817 | |
| 818 | StringRef Contents; |
| 819 | UnwindInfo.getContents(Contents); |
| 820 | const char *Pos = Contents.data(); |
| 821 | |
| 822 | //===---------------------------------- |
| 823 | // Section header |
| 824 | //===---------------------------------- |
| 825 | |
| 826 | uint32_t Version = readNext<uint32_t>(Pos); |
| 827 | outs() << " Version: " |
| 828 | << format("0x%" PRIx32, Version) << '\n'; |
| 829 | assert(Version == 1 && "only understand version 1"); |
| 830 | |
| 831 | uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos); |
| 832 | outs() << " Common encodings array section offset: " |
| 833 | << format("0x%" PRIx32, CommonEncodingsStart) << '\n'; |
| 834 | uint32_t NumCommonEncodings = readNext<uint32_t>(Pos); |
| 835 | outs() << " Number of common encodings in array: " |
| 836 | << format("0x%" PRIx32, NumCommonEncodings) << '\n'; |
| 837 | |
| 838 | uint32_t PersonalitiesStart = readNext<uint32_t>(Pos); |
| 839 | outs() << " Personality function array section offset: " |
| 840 | << format("0x%" PRIx32, PersonalitiesStart) << '\n'; |
| 841 | uint32_t NumPersonalities = readNext<uint32_t>(Pos); |
| 842 | outs() << " Number of personality functions in array: " |
| 843 | << format("0x%" PRIx32, NumPersonalities) << '\n'; |
| 844 | |
| 845 | uint32_t IndicesStart = readNext<uint32_t>(Pos); |
| 846 | outs() << " Index array section offset: " |
| 847 | << format("0x%" PRIx32, IndicesStart) << '\n'; |
| 848 | uint32_t NumIndices = readNext<uint32_t>(Pos); |
| 849 | outs() << " Number of indices in array: " |
| 850 | << format("0x%" PRIx32, NumIndices) << '\n'; |
| 851 | |
| 852 | //===---------------------------------- |
| 853 | // A shared list of common encodings |
| 854 | //===---------------------------------- |
| 855 | |
| 856 | // These occupy indices in the range [0, N] whenever an encoding is referenced |
| 857 | // from a compressed 2nd level index table. In practice the linker only |
| 858 | // creates ~128 of these, so that indices are available to embed encodings in |
| 859 | // the 2nd level index. |
| 860 | |
| 861 | SmallVector<uint32_t, 64> CommonEncodings; |
| 862 | outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n"; |
| 863 | Pos = Contents.data() + CommonEncodingsStart; |
| 864 | for (unsigned i = 0; i < NumCommonEncodings; ++i) { |
| 865 | uint32_t Encoding = readNext<uint32_t>(Pos); |
| 866 | CommonEncodings.push_back(Encoding); |
| 867 | |
| 868 | outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding) |
| 869 | << '\n'; |
| 870 | } |
| 871 | |
| 872 | |
| 873 | //===---------------------------------- |
| 874 | // Personality functions used in this executable |
| 875 | //===---------------------------------- |
| 876 | |
| 877 | // There should be only a handful of these (one per source language, |
| 878 | // roughly). Particularly since they only get 2 bits in the compact encoding. |
| 879 | |
| 880 | outs() << " Personality functions: (count = " << NumPersonalities << ")\n"; |
| 881 | Pos = Contents.data() + PersonalitiesStart; |
| 882 | for (unsigned i = 0; i < NumPersonalities; ++i) { |
| 883 | uint32_t PersonalityFn = readNext<uint32_t>(Pos); |
| 884 | outs() << " personality[" << i + 1 |
| 885 | << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n'; |
| 886 | } |
| 887 | |
| 888 | //===---------------------------------- |
| 889 | // The level 1 index entries |
| 890 | //===---------------------------------- |
| 891 | |
| 892 | // These specify an approximate place to start searching for the more detailed |
| 893 | // information, sorted by PC. |
| 894 | |
| 895 | struct IndexEntry { |
| 896 | uint32_t FunctionOffset; |
| 897 | uint32_t SecondLevelPageStart; |
| 898 | uint32_t LSDAStart; |
| 899 | }; |
| 900 | |
| 901 | SmallVector<IndexEntry, 4> IndexEntries; |
| 902 | |
| 903 | outs() << " Top level indices: (count = " << NumIndices << ")\n"; |
| 904 | Pos = Contents.data() + IndicesStart; |
| 905 | for (unsigned i = 0; i < NumIndices; ++i) { |
| 906 | IndexEntry Entry; |
| 907 | |
| 908 | Entry.FunctionOffset = readNext<uint32_t>(Pos); |
| 909 | Entry.SecondLevelPageStart = readNext<uint32_t>(Pos); |
| 910 | Entry.LSDAStart = readNext<uint32_t>(Pos); |
| 911 | IndexEntries.push_back(Entry); |
| 912 | |
| 913 | outs() << " [" << i << "]: " |
| 914 | << "function offset=" |
| 915 | << format("0x%08" PRIx32, Entry.FunctionOffset) << ", " |
| 916 | << "2nd level page offset=" |
| 917 | << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", " |
| 918 | << "LSDA offset=" |
| 919 | << format("0x%08" PRIx32, Entry.LSDAStart) << '\n'; |
| 920 | } |
| 921 | |
| 922 | |
| 923 | //===---------------------------------- |
| 924 | // Next come the LSDA tables |
| 925 | //===---------------------------------- |
| 926 | |
| 927 | // The LSDA layout is rather implicit: it's a contiguous array of entries from |
| 928 | // the first top-level index's LSDAOffset to the last (sentinel). |
| 929 | |
| 930 | outs() << " LSDA descriptors:\n"; |
| 931 | Pos = Contents.data() + IndexEntries[0].LSDAStart; |
| 932 | int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) / |
| 933 | (2 * sizeof(uint32_t)); |
| 934 | for (int i = 0; i < NumLSDAs; ++i) { |
| 935 | uint32_t FunctionOffset = readNext<uint32_t>(Pos); |
| 936 | uint32_t LSDAOffset = readNext<uint32_t>(Pos); |
| 937 | outs() << " [" << i << "]: " |
| 938 | << "function offset=" |
| 939 | << format("0x%08" PRIx32, FunctionOffset) << ", " |
| 940 | << "LSDA offset=" |
| 941 | << format("0x%08" PRIx32, LSDAOffset) << '\n'; |
| 942 | } |
| 943 | |
| 944 | //===---------------------------------- |
| 945 | // Finally, the 2nd level indices |
| 946 | //===---------------------------------- |
| 947 | |
| 948 | // Generally these are 4K in size, and have 2 possible forms: |
| 949 | // + Regular stores up to 511 entries with disparate encodings |
| 950 | // + Compressed stores up to 1021 entries if few enough compact encoding |
| 951 | // values are used. |
| 952 | outs() << " Second level indices:\n"; |
| 953 | for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) { |
| 954 | // The final sentinel top-level index has no associated 2nd level page |
| 955 | if (IndexEntries[i].SecondLevelPageStart == 0) |
| 956 | break; |
| 957 | |
| 958 | outs() << " Second level index[" << i << "]: " |
| 959 | << "offset in section=" |
| 960 | << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart) |
| 961 | << ", " |
| 962 | << "base function offset=" |
| 963 | << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n'; |
| 964 | |
| 965 | Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart; |
Aaron Ballman | 80930af | 2014-08-14 13:53:19 +0000 | [diff] [blame] | 966 | uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos); |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 967 | if (Kind == 2) |
| 968 | printRegularSecondLevelUnwindPage(Pos); |
| 969 | else if (Kind == 3) |
| 970 | printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset, |
| 971 | CommonEncodings); |
| 972 | else |
| 973 | llvm_unreachable("Do not know how to print this kind of 2nd level page"); |
| 974 | |
| 975 | } |
| 976 | } |
| 977 | |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 978 | void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) { |
| 979 | std::map<uint64_t, SymbolRef> Symbols; |
| 980 | for (const SymbolRef &SymRef : Obj->symbols()) { |
| 981 | // Discard any undefined or absolute symbols. They're not going to take part |
| 982 | // in the convenience lookup for unwind info and just take up resources. |
| 983 | section_iterator Section = Obj->section_end(); |
| 984 | SymRef.getSection(Section); |
| 985 | if (Section == Obj->section_end()) |
| 986 | continue; |
| 987 | |
| 988 | uint64_t Addr; |
| 989 | SymRef.getAddress(Addr); |
| 990 | Symbols.insert(std::make_pair(Addr, SymRef)); |
| 991 | } |
| 992 | |
| 993 | for (const SectionRef &Section : Obj->sections()) { |
| 994 | StringRef SectName; |
| 995 | Section.getName(SectName); |
| 996 | if (SectName == "__compact_unwind") |
| 997 | printMachOCompactUnwindSection(Obj, Symbols, Section); |
| 998 | else if (SectName == "__unwind_info") |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 999 | printMachOUnwindInfoSection(Obj, Symbols, Section); |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 1000 | else if (SectName == "__eh_frame") |
| 1001 | outs() << "llvm-objdump: warning: unhandled __eh_frame section\n"; |
| 1002 | |
| 1003 | } |
| 1004 | } |
Kevin Enderby | b76d386 | 2014-08-22 20:35:18 +0000 | [diff] [blame^] | 1005 | |
| 1006 | static void PrintMachHeader(uint32_t magic, uint32_t cputype, |
| 1007 | uint32_t cpusubtype, uint32_t filetype, |
| 1008 | uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags, |
| 1009 | bool verbose) { |
| 1010 | outs() << "Mach header\n"; |
| 1011 | outs() << " magic cputype cpusubtype caps filetype ncmds " |
| 1012 | "sizeofcmds flags\n"; |
| 1013 | if (verbose) { |
| 1014 | if (magic == MachO::MH_MAGIC) |
| 1015 | outs() << " MH_MAGIC"; |
| 1016 | else if (magic == MachO::MH_MAGIC_64) |
| 1017 | outs() << "MH_MAGIC_64"; |
| 1018 | else |
| 1019 | outs() << format(" 0x%08" PRIx32, magic); |
| 1020 | switch (cputype) { |
| 1021 | case MachO::CPU_TYPE_I386: |
| 1022 | outs() << " I386"; |
| 1023 | switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { |
| 1024 | case MachO::CPU_SUBTYPE_I386_ALL: |
| 1025 | outs() << " ALL"; |
| 1026 | break; |
| 1027 | default: |
| 1028 | outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); |
| 1029 | break; |
| 1030 | } |
| 1031 | break; |
| 1032 | case MachO::CPU_TYPE_X86_64: |
| 1033 | outs() << " X86_64"; |
| 1034 | case MachO::CPU_SUBTYPE_X86_64_ALL: |
| 1035 | outs() << " ALL"; |
| 1036 | break; |
| 1037 | case MachO::CPU_SUBTYPE_X86_64_H: |
| 1038 | outs() << " Haswell"; |
| 1039 | break; |
| 1040 | switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { |
| 1041 | default: |
| 1042 | outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); |
| 1043 | break; |
| 1044 | } |
| 1045 | break; |
| 1046 | case MachO::CPU_TYPE_ARM: |
| 1047 | outs() << " ARM"; |
| 1048 | switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { |
| 1049 | case MachO::CPU_SUBTYPE_ARM_ALL: |
| 1050 | outs() << " ALL"; |
| 1051 | break; |
| 1052 | case MachO::CPU_SUBTYPE_ARM_V4T: |
| 1053 | outs() << " V4T"; |
| 1054 | break; |
| 1055 | case MachO::CPU_SUBTYPE_ARM_V5TEJ: |
| 1056 | outs() << " V5TEJ"; |
| 1057 | break; |
| 1058 | case MachO::CPU_SUBTYPE_ARM_XSCALE: |
| 1059 | outs() << " XSCALE"; |
| 1060 | break; |
| 1061 | case MachO::CPU_SUBTYPE_ARM_V6: |
| 1062 | outs() << " V6"; |
| 1063 | break; |
| 1064 | case MachO::CPU_SUBTYPE_ARM_V6M: |
| 1065 | outs() << " V6M"; |
| 1066 | break; |
| 1067 | case MachO::CPU_SUBTYPE_ARM_V7: |
| 1068 | outs() << " V7"; |
| 1069 | break; |
| 1070 | case MachO::CPU_SUBTYPE_ARM_V7EM: |
| 1071 | outs() << " V7EM"; |
| 1072 | break; |
| 1073 | case MachO::CPU_SUBTYPE_ARM_V7K: |
| 1074 | outs() << " V7K"; |
| 1075 | break; |
| 1076 | case MachO::CPU_SUBTYPE_ARM_V7M: |
| 1077 | outs() << " V7M"; |
| 1078 | break; |
| 1079 | case MachO::CPU_SUBTYPE_ARM_V7S: |
| 1080 | outs() << " V7S"; |
| 1081 | break; |
| 1082 | default: |
| 1083 | outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); |
| 1084 | break; |
| 1085 | } |
| 1086 | break; |
| 1087 | case MachO::CPU_TYPE_ARM64: |
| 1088 | outs() << " ARM64"; |
| 1089 | switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { |
| 1090 | case MachO::CPU_SUBTYPE_ARM64_ALL: |
| 1091 | outs() << " ALL"; |
| 1092 | break; |
| 1093 | default: |
| 1094 | outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); |
| 1095 | break; |
| 1096 | } |
| 1097 | break; |
| 1098 | case MachO::CPU_TYPE_POWERPC: |
| 1099 | outs() << " PPC"; |
| 1100 | switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { |
| 1101 | case MachO::CPU_SUBTYPE_POWERPC_ALL: |
| 1102 | outs() << " ALL"; |
| 1103 | break; |
| 1104 | default: |
| 1105 | outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); |
| 1106 | break; |
| 1107 | } |
| 1108 | break; |
| 1109 | case MachO::CPU_TYPE_POWERPC64: |
| 1110 | outs() << " PPC64"; |
| 1111 | switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { |
| 1112 | case MachO::CPU_SUBTYPE_POWERPC_ALL: |
| 1113 | outs() << " ALL"; |
| 1114 | break; |
| 1115 | default: |
| 1116 | outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); |
| 1117 | break; |
| 1118 | } |
| 1119 | break; |
| 1120 | } |
| 1121 | if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) { |
| 1122 | outs() << " LIB64 "; |
| 1123 | } else { |
| 1124 | outs() << format(" 0x%02" PRIx32, |
| 1125 | (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24); |
| 1126 | } |
| 1127 | switch (filetype) { |
| 1128 | case MachO::MH_OBJECT: |
| 1129 | outs() << " OBJECT"; |
| 1130 | break; |
| 1131 | case MachO::MH_EXECUTE: |
| 1132 | outs() << " EXECUTE"; |
| 1133 | break; |
| 1134 | case MachO::MH_FVMLIB: |
| 1135 | outs() << " FVMLIB"; |
| 1136 | break; |
| 1137 | case MachO::MH_CORE: |
| 1138 | outs() << " CORE"; |
| 1139 | break; |
| 1140 | case MachO::MH_PRELOAD: |
| 1141 | outs() << " PRELOAD"; |
| 1142 | break; |
| 1143 | case MachO::MH_DYLIB: |
| 1144 | outs() << " DYLIB"; |
| 1145 | break; |
| 1146 | case MachO::MH_DYLIB_STUB: |
| 1147 | outs() << " DYLIB_STUB"; |
| 1148 | break; |
| 1149 | case MachO::MH_DYLINKER: |
| 1150 | outs() << " DYLINKER"; |
| 1151 | break; |
| 1152 | case MachO::MH_BUNDLE: |
| 1153 | outs() << " BUNDLE"; |
| 1154 | break; |
| 1155 | case MachO::MH_DSYM: |
| 1156 | outs() << " DSYM"; |
| 1157 | break; |
| 1158 | case MachO::MH_KEXT_BUNDLE: |
| 1159 | outs() << " KEXTBUNDLE"; |
| 1160 | break; |
| 1161 | default: |
| 1162 | outs() << format(" %10u", filetype); |
| 1163 | break; |
| 1164 | } |
| 1165 | outs() << format(" %5u", ncmds); |
| 1166 | outs() << format(" %10u", sizeofcmds); |
| 1167 | uint32_t f = flags; |
| 1168 | if (f & MachO::MH_NOUNDEFS) { |
| 1169 | outs() << " NOUNDEFS"; |
| 1170 | f &= ~MachO::MH_NOUNDEFS; |
| 1171 | } |
| 1172 | if (f & MachO::MH_INCRLINK) { |
| 1173 | outs() << " INCRLINK"; |
| 1174 | f &= ~MachO::MH_INCRLINK; |
| 1175 | } |
| 1176 | if (f & MachO::MH_DYLDLINK) { |
| 1177 | outs() << " DYLDLINK"; |
| 1178 | f &= ~MachO::MH_DYLDLINK; |
| 1179 | } |
| 1180 | if (f & MachO::MH_BINDATLOAD) { |
| 1181 | outs() << " BINDATLOAD"; |
| 1182 | f &= ~MachO::MH_BINDATLOAD; |
| 1183 | } |
| 1184 | if (f & MachO::MH_PREBOUND) { |
| 1185 | outs() << " PREBOUND"; |
| 1186 | f &= ~MachO::MH_PREBOUND; |
| 1187 | } |
| 1188 | if (f & MachO::MH_SPLIT_SEGS) { |
| 1189 | outs() << " SPLIT_SEGS"; |
| 1190 | f &= ~MachO::MH_SPLIT_SEGS; |
| 1191 | } |
| 1192 | if (f & MachO::MH_LAZY_INIT) { |
| 1193 | outs() << " LAZY_INIT"; |
| 1194 | f &= ~MachO::MH_LAZY_INIT; |
| 1195 | } |
| 1196 | if (f & MachO::MH_TWOLEVEL) { |
| 1197 | outs() << " TWOLEVEL"; |
| 1198 | f &= ~MachO::MH_TWOLEVEL; |
| 1199 | } |
| 1200 | if (f & MachO::MH_FORCE_FLAT) { |
| 1201 | outs() << " FORCE_FLAT"; |
| 1202 | f &= ~MachO::MH_FORCE_FLAT; |
| 1203 | } |
| 1204 | if (f & MachO::MH_NOMULTIDEFS) { |
| 1205 | outs() << " NOMULTIDEFS"; |
| 1206 | f &= ~MachO::MH_NOMULTIDEFS; |
| 1207 | } |
| 1208 | if (f & MachO::MH_NOFIXPREBINDING) { |
| 1209 | outs() << " NOFIXPREBINDING"; |
| 1210 | f &= ~MachO::MH_NOFIXPREBINDING; |
| 1211 | } |
| 1212 | if (f & MachO::MH_PREBINDABLE) { |
| 1213 | outs() << " PREBINDABLE"; |
| 1214 | f &= ~MachO::MH_PREBINDABLE; |
| 1215 | } |
| 1216 | if (f & MachO::MH_ALLMODSBOUND) { |
| 1217 | outs() << " ALLMODSBOUND"; |
| 1218 | f &= ~MachO::MH_ALLMODSBOUND; |
| 1219 | } |
| 1220 | if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) { |
| 1221 | outs() << " SUBSECTIONS_VIA_SYMBOLS"; |
| 1222 | f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS; |
| 1223 | } |
| 1224 | if (f & MachO::MH_CANONICAL) { |
| 1225 | outs() << " CANONICAL"; |
| 1226 | f &= ~MachO::MH_CANONICAL; |
| 1227 | } |
| 1228 | if (f & MachO::MH_WEAK_DEFINES) { |
| 1229 | outs() << " WEAK_DEFINES"; |
| 1230 | f &= ~MachO::MH_WEAK_DEFINES; |
| 1231 | } |
| 1232 | if (f & MachO::MH_BINDS_TO_WEAK) { |
| 1233 | outs() << " BINDS_TO_WEAK"; |
| 1234 | f &= ~MachO::MH_BINDS_TO_WEAK; |
| 1235 | } |
| 1236 | if (f & MachO::MH_ALLOW_STACK_EXECUTION) { |
| 1237 | outs() << " ALLOW_STACK_EXECUTION"; |
| 1238 | f &= ~MachO::MH_ALLOW_STACK_EXECUTION; |
| 1239 | } |
| 1240 | if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) { |
| 1241 | outs() << " DEAD_STRIPPABLE_DYLIB"; |
| 1242 | f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB; |
| 1243 | } |
| 1244 | if (f & MachO::MH_PIE) { |
| 1245 | outs() << " PIE"; |
| 1246 | f &= ~MachO::MH_PIE; |
| 1247 | } |
| 1248 | if (f & MachO::MH_NO_REEXPORTED_DYLIBS) { |
| 1249 | outs() << " NO_REEXPORTED_DYLIBS"; |
| 1250 | f &= ~MachO::MH_NO_REEXPORTED_DYLIBS; |
| 1251 | } |
| 1252 | if (f & MachO::MH_HAS_TLV_DESCRIPTORS) { |
| 1253 | outs() << " MH_HAS_TLV_DESCRIPTORS"; |
| 1254 | f &= ~MachO::MH_HAS_TLV_DESCRIPTORS; |
| 1255 | } |
| 1256 | if (f & MachO::MH_NO_HEAP_EXECUTION) { |
| 1257 | outs() << " MH_NO_HEAP_EXECUTION"; |
| 1258 | f &= ~MachO::MH_NO_HEAP_EXECUTION; |
| 1259 | } |
| 1260 | if (f & MachO::MH_APP_EXTENSION_SAFE) { |
| 1261 | outs() << " APP_EXTENSION_SAFE"; |
| 1262 | f &= ~MachO::MH_APP_EXTENSION_SAFE; |
| 1263 | } |
| 1264 | if (f != 0 || flags == 0) |
| 1265 | outs() << format(" 0x%08" PRIx32, f); |
| 1266 | } else { |
| 1267 | outs() << format(" 0x%08" PRIx32, magic); |
| 1268 | outs() << format(" %7d", cputype); |
| 1269 | outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); |
| 1270 | outs() << format(" 0x%02" PRIx32, |
| 1271 | (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24); |
| 1272 | outs() << format(" %10u", filetype); |
| 1273 | outs() << format(" %5u", ncmds); |
| 1274 | outs() << format(" %10u", sizeofcmds); |
| 1275 | outs() << format(" 0x%08" PRIx32, flags); |
| 1276 | } |
| 1277 | outs() << "\n"; |
| 1278 | } |
| 1279 | |
| 1280 | static void getAndPrintMachHeader(const MachOObjectFile *Obj, bool verbose) { |
| 1281 | if (Obj->is64Bit()) { |
| 1282 | MachO::mach_header_64 H_64; |
| 1283 | H_64 = Obj->getHeader64(); |
| 1284 | PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype, |
| 1285 | H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose); |
| 1286 | } else { |
| 1287 | MachO::mach_header H; |
| 1288 | H = Obj->getHeader(); |
| 1289 | PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds, |
| 1290 | H.sizeofcmds, H.flags, verbose); |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | void llvm::printMachOFileHeader(const object::ObjectFile *Obj) { |
| 1295 | const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj); |
| 1296 | getAndPrintMachHeader(file, true); |
| 1297 | // TODO: next get and print the load commands. |
| 1298 | } |