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" |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 15 | #include "llvm-c/Disassembler.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Triple.h" |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/DIContext.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCAsmInfo.h" |
Lang Hames | a1bc0f5 | 2014-04-15 04:40:56 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCContext.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCDisassembler.h" |
| 23 | #include "llvm/MC/MCInst.h" |
| 24 | #include "llvm/MC/MCInstPrinter.h" |
| 25 | #include "llvm/MC/MCInstrAnalysis.h" |
| 26 | #include "llvm/MC/MCInstrDesc.h" |
| 27 | #include "llvm/MC/MCInstrInfo.h" |
Jim Grosbach | fd93a59 | 2012-03-05 19:33:20 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCRegisterInfo.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCSubtargetInfo.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 30 | #include "llvm/Object/MachO.h" |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Casting.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CommandLine.h" |
| 33 | #include "llvm/Support/Debug.h" |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Endian.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Format.h" |
| 36 | #include "llvm/Support/GraphWriter.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 37 | #include "llvm/Support/MachO.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 38 | #include "llvm/Support/MemoryBuffer.h" |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 39 | #include "llvm/Support/FormattedStream.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 40 | #include "llvm/Support/TargetRegistry.h" |
| 41 | #include "llvm/Support/TargetSelect.h" |
| 42 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 43 | #include <algorithm> |
| 44 | #include <cstring> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 45 | #include <system_error> |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 46 | using namespace llvm; |
| 47 | using namespace object; |
| 48 | |
| 49 | static cl::opt<bool> |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 50 | UseDbg("g", cl::desc("Print line information from debug info if available")); |
| 51 | |
| 52 | static cl::opt<std::string> |
| 53 | DSYMFile("dsym", cl::desc("Use .dSYM file for debug info")); |
| 54 | |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 55 | static cl::opt<bool> |
| 56 | FullLeadingAddr("full-leading-addr", |
| 57 | cl::desc("Print full leading address")); |
| 58 | |
| 59 | static cl::opt<bool> |
| 60 | PrintImmHex("print-imm-hex", |
| 61 | cl::desc("Use hex format for immediate values")); |
| 62 | |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 63 | static std::string ThumbTripleName; |
| 64 | |
| 65 | static const Target *GetTarget(const MachOObjectFile *MachOObj, |
| 66 | const char **McpuDefault, |
| 67 | const Target **ThumbTarget) { |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 68 | // Figure out the target triple. |
Cameron Zwarich | 88cc16a | 2012-02-03 06:35:22 +0000 | [diff] [blame] | 69 | if (TripleName.empty()) { |
| 70 | llvm::Triple TT("unknown-unknown-unknown"); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 71 | llvm::Triple ThumbTriple = Triple(); |
| 72 | TT = MachOObj->getArch(McpuDefault, &ThumbTriple); |
Cameron Zwarich | 88cc16a | 2012-02-03 06:35:22 +0000 | [diff] [blame] | 73 | TripleName = TT.str(); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 74 | ThumbTripleName = ThumbTriple.str(); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 77 | // Get the target specific parser. |
| 78 | std::string Error; |
| 79 | const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 80 | if (TheTarget && ThumbTripleName.empty()) |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 81 | return TheTarget; |
| 82 | |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 83 | *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error); |
| 84 | if (*ThumbTarget) |
| 85 | return TheTarget; |
| 86 | |
| 87 | errs() << "llvm-objdump: error: unable to get target for '"; |
| 88 | if (!TheTarget) |
| 89 | errs() << TripleName; |
| 90 | else |
| 91 | errs() << ThumbTripleName; |
| 92 | errs() << "', see --version and --triple.\n"; |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 93 | return nullptr; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 96 | struct SymbolSorter { |
| 97 | bool operator()(const SymbolRef &A, const SymbolRef &B) { |
| 98 | SymbolRef::Type AType, BType; |
| 99 | A.getType(AType); |
| 100 | B.getType(BType); |
| 101 | |
| 102 | uint64_t AAddr, BAddr; |
| 103 | if (AType != SymbolRef::ST_Function) |
| 104 | AAddr = 0; |
| 105 | else |
| 106 | A.getAddress(AAddr); |
| 107 | if (BType != SymbolRef::ST_Function) |
| 108 | BAddr = 0; |
| 109 | else |
| 110 | B.getAddress(BAddr); |
| 111 | return AAddr < BAddr; |
| 112 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 115 | // Types for the storted data in code table that is built before disassembly |
| 116 | // and the predicate function to sort them. |
| 117 | typedef std::pair<uint64_t, DiceRef> DiceTableEntry; |
| 118 | typedef std::vector<DiceTableEntry> DiceTable; |
| 119 | typedef DiceTable::iterator dice_table_iterator; |
| 120 | |
| 121 | static bool |
| 122 | compareDiceTableEntries(const DiceTableEntry i, |
| 123 | const DiceTableEntry j) { |
| 124 | return i.first == j.first; |
| 125 | } |
| 126 | |
| 127 | static void DumpDataInCode(const char *bytes, uint64_t Size, |
| 128 | unsigned short Kind) { |
| 129 | uint64_t Value; |
| 130 | |
| 131 | switch (Kind) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 132 | case MachO::DICE_KIND_DATA: |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 133 | switch (Size) { |
| 134 | case 4: |
| 135 | Value = bytes[3] << 24 | |
| 136 | bytes[2] << 16 | |
| 137 | bytes[1] << 8 | |
| 138 | bytes[0]; |
| 139 | outs() << "\t.long " << Value; |
| 140 | break; |
| 141 | case 2: |
| 142 | Value = bytes[1] << 8 | |
| 143 | bytes[0]; |
| 144 | outs() << "\t.short " << Value; |
| 145 | break; |
| 146 | case 1: |
| 147 | Value = bytes[0]; |
| 148 | outs() << "\t.byte " << Value; |
| 149 | break; |
| 150 | } |
| 151 | outs() << "\t@ KIND_DATA\n"; |
| 152 | break; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 153 | case MachO::DICE_KIND_JUMP_TABLE8: |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 154 | Value = bytes[0]; |
| 155 | outs() << "\t.byte " << Value << "\t@ KIND_JUMP_TABLE8"; |
| 156 | break; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 157 | case MachO::DICE_KIND_JUMP_TABLE16: |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 158 | Value = bytes[1] << 8 | |
| 159 | bytes[0]; |
| 160 | outs() << "\t.short " << Value << "\t@ KIND_JUMP_TABLE16"; |
| 161 | break; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 162 | case MachO::DICE_KIND_JUMP_TABLE32: |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 163 | Value = bytes[3] << 24 | |
| 164 | bytes[2] << 16 | |
| 165 | bytes[1] << 8 | |
| 166 | bytes[0]; |
| 167 | outs() << "\t.long " << Value << "\t@ KIND_JUMP_TABLE32"; |
| 168 | break; |
| 169 | default: |
| 170 | outs() << "\t@ data in code kind = " << Kind << "\n"; |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 175 | static void getSectionsAndSymbols(const MachO::mach_header Header, |
| 176 | MachOObjectFile *MachOObj, |
| 177 | std::vector<SectionRef> &Sections, |
| 178 | std::vector<SymbolRef> &Symbols, |
| 179 | SmallVectorImpl<uint64_t> &FoundFns, |
| 180 | uint64_t &BaseSegmentAddress) { |
| 181 | for (const SymbolRef &Symbol : MachOObj->symbols()) |
| 182 | Symbols.push_back(Symbol); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 183 | |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 184 | for (const SectionRef &Section : MachOObj->sections()) { |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 185 | StringRef SectName; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 186 | Section.getName(SectName); |
| 187 | Sections.push_back(Section); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 190 | MachOObjectFile::LoadCommandInfo Command = |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 191 | MachOObj->getFirstLoadCommandInfo(); |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 192 | bool BaseSegmentAddressSet = false; |
Rafael Espindola | feef8c2 | 2013-04-19 11:36:47 +0000 | [diff] [blame] | 193 | for (unsigned i = 0; ; ++i) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 194 | if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) { |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 195 | // We found a function starts segment, parse the addresses for later |
| 196 | // consumption. |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 197 | MachO::linkedit_data_command LLC = |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 198 | MachOObj->getLinkeditDataLoadCommand(Command); |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 199 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 200 | MachOObj->ReadULEB128s(LLC.dataoff, FoundFns); |
Benjamin Kramer | 8a529dc | 2011-09-21 22:16:43 +0000 | [diff] [blame] | 201 | } |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 202 | else if (Command.C.cmd == MachO::LC_SEGMENT) { |
| 203 | MachO::segment_command SLC = |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 204 | MachOObj->getSegmentLoadCommand(Command); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 205 | StringRef SegName = SLC.segname; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 206 | if(!BaseSegmentAddressSet && SegName != "__PAGEZERO") { |
| 207 | BaseSegmentAddressSet = true; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 208 | BaseSegmentAddress = SLC.vmaddr; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
Rafael Espindola | feef8c2 | 2013-04-19 11:36:47 +0000 | [diff] [blame] | 211 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 212 | if (i == Header.ncmds - 1) |
Rafael Espindola | feef8c2 | 2013-04-19 11:36:47 +0000 | [diff] [blame] | 213 | break; |
| 214 | else |
| 215 | Command = MachOObj->getNextLoadCommandInfo(Command); |
Benjamin Kramer | 8a529dc | 2011-09-21 22:16:43 +0000 | [diff] [blame] | 216 | } |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 219 | static void DisassembleInputMachO2(StringRef Filename, |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 220 | MachOObjectFile *MachOOF); |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 221 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 222 | void llvm::DisassembleInputMachO(StringRef Filename) { |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 223 | ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr = |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 224 | MemoryBuffer::getFileOrSTDIN(Filename); |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 225 | if (std::error_code EC = BuffOrErr.getError()) { |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 226 | errs() << "llvm-objdump: " << Filename << ": " << EC.message() << "\n"; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 227 | return; |
| 228 | } |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 229 | std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get()); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 230 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 231 | std::unique_ptr<MachOObjectFile> MachOOF = std::move( |
| 232 | ObjectFile::createMachOObjectFile(Buff.get()->getMemBufferRef()).get()); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 233 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 234 | DisassembleInputMachO2(Filename, MachOOF.get()); |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 237 | typedef DenseMap<uint64_t, StringRef> SymbolAddressMap; |
| 238 | |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 239 | // The block of info used by the Symbolizer call backs. |
| 240 | struct DisassembleInfo { |
| 241 | bool verbose; |
| 242 | MachOObjectFile *O; |
| 243 | SectionRef S; |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 244 | SymbolAddressMap *AddrMap; |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | // SymbolizerGetOpInfo() is the operand information call back function. |
| 248 | // This is called to get the symbolic information for operand(s) of an |
| 249 | // instruction when it is being done. This routine does this from |
| 250 | // the relocation information, symbol table, etc. That block of information |
| 251 | // is a pointer to the struct DisassembleInfo that was passed when the |
| 252 | // disassembler context was created and passed to back to here when |
| 253 | // called back by the disassembler for instruction operands that could have |
| 254 | // relocation information. The address of the instruction containing operand is |
| 255 | // at the Pc parameter. The immediate value the operand has is passed in |
| 256 | // op_info->Value and is at Offset past the start of the instruction and has a |
| 257 | // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the |
| 258 | // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol |
| 259 | // names and addends of the symbolic expression to add for the operand. The |
| 260 | // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic |
| 261 | // information is returned then this function returns 1 else it returns 0. |
| 262 | int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset, |
| 263 | uint64_t Size, int TagType, void *TagBuf) { |
| 264 | struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo; |
| 265 | struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf; |
| 266 | unsigned int value = op_info->Value; |
| 267 | |
| 268 | // Make sure all fields returned are zero if we don't set them. |
| 269 | memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1)); |
| 270 | op_info->Value = value; |
| 271 | |
| 272 | // If the TagType is not the value 1 which it code knows about or if no |
| 273 | // verbose symbolic information is wanted then just return 0, indicating no |
| 274 | // information is being returned. |
| 275 | if (TagType != 1 || info->verbose == false) |
| 276 | return 0; |
| 277 | |
| 278 | unsigned int Arch = info->O->getArch(); |
| 279 | if (Arch == Triple::x86) { |
| 280 | return 0; |
| 281 | } else if (Arch == Triple::x86_64) { |
| 282 | if (Size != 1 && Size != 2 && Size != 4 && Size != 0) |
| 283 | return 0; |
| 284 | // First search the section's relocation entries (if any) for an entry |
| 285 | // for this section offset. |
| 286 | uint64_t sect_addr; |
| 287 | info->S.getAddress(sect_addr); |
| 288 | uint64_t sect_offset = (Pc + Offset) - sect_addr; |
| 289 | bool reloc_found = false; |
| 290 | DataRefImpl Rel; |
| 291 | MachO::any_relocation_info RE; |
| 292 | bool isExtern = false; |
| 293 | SymbolRef Symbol; |
| 294 | for (const RelocationRef &Reloc : info->S.relocations()) { |
| 295 | uint64_t RelocOffset; |
| 296 | Reloc.getOffset(RelocOffset); |
| 297 | if (RelocOffset == sect_offset) { |
| 298 | Rel = Reloc.getRawDataRefImpl(); |
| 299 | RE = info->O->getRelocation(Rel); |
| 300 | // NOTE: Scattered relocations don't exist on x86_64. |
| 301 | isExtern = info->O->getPlainRelocationExternal(RE); |
| 302 | if (isExtern) { |
| 303 | symbol_iterator RelocSym = Reloc.getSymbol(); |
| 304 | Symbol = *RelocSym; |
| 305 | } |
| 306 | reloc_found = true; |
| 307 | break; |
| 308 | } |
| 309 | } |
| 310 | if (reloc_found && isExtern) { |
| 311 | // The Value passed in will be adjusted by the Pc if the instruction |
| 312 | // adds the Pc. But for x86_64 external relocation entries the Value |
| 313 | // is the offset from the external symbol. |
| 314 | if (info->O->getAnyRelocationPCRel(RE)) |
| 315 | op_info->Value -= Pc + Offset + Size; |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 316 | StringRef SymName; |
| 317 | Symbol.getName(SymName); |
| 318 | const char *name = SymName.data(); |
| 319 | unsigned Type = info->O->getAnyRelocationType(RE); |
| 320 | if (Type == MachO::X86_64_RELOC_SUBTRACTOR) { |
| 321 | DataRefImpl RelNext = Rel; |
| 322 | info->O->moveRelocationNext(RelNext); |
| 323 | MachO::any_relocation_info RENext = info->O->getRelocation(RelNext); |
| 324 | unsigned TypeNext = info->O->getAnyRelocationType(RENext); |
| 325 | bool isExternNext = info->O->getPlainRelocationExternal(RENext); |
| 326 | unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext); |
| 327 | if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) { |
| 328 | op_info->SubtractSymbol.Present = 1; |
| 329 | op_info->SubtractSymbol.Name = name; |
| 330 | symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum); |
| 331 | Symbol = *RelocSymNext; |
| 332 | StringRef SymNameNext; |
| 333 | Symbol.getName(SymNameNext); |
| 334 | name = SymNameNext.data(); |
| 335 | } |
| 336 | } |
| 337 | // TODO: add the VariantKinds to op_info->VariantKind for relocation types |
| 338 | // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT. |
| 339 | op_info->AddSymbol.Present = 1; |
| 340 | op_info->AddSymbol.Name = name; |
| 341 | return 1; |
| 342 | } |
| 343 | // TODO: |
| 344 | // Second search the external relocation entries of a fully linked image |
| 345 | // (if any) for an entry that matches this segment offset. |
| 346 | //uint64_t seg_offset = (Pc + Offset); |
| 347 | return 0; |
| 348 | } else if (Arch == Triple::arm) { |
| 349 | return 0; |
| 350 | } else if (Arch == Triple::aarch64) { |
| 351 | return 0; |
| 352 | } else { |
| 353 | return 0; |
| 354 | } |
| 355 | } |
| 356 | |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 357 | // GuessCstringPointer is passed the address of what might be a pointer to a |
| 358 | // literal string in a cstring section. If that address is in a cstring section |
| 359 | // it returns a pointer to that string. Else it returns nullptr. |
| 360 | const char *GuessCstringPointer(uint64_t ReferenceValue, |
| 361 | struct DisassembleInfo *info) { |
| 362 | uint32_t LoadCommandCount = info->O->getHeader().ncmds; |
| 363 | MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo(); |
| 364 | for (unsigned I = 0;; ++I) { |
| 365 | if (Load.C.cmd == MachO::LC_SEGMENT_64) { |
| 366 | MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load); |
| 367 | for (unsigned J = 0; J < Seg.nsects; ++J) { |
| 368 | MachO::section_64 Sec = info->O->getSection64(Load, J); |
| 369 | uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; |
| 370 | if (section_type == MachO::S_CSTRING_LITERALS && |
| 371 | ReferenceValue >= Sec.addr && |
| 372 | ReferenceValue < Sec.addr + Sec.size) { |
| 373 | uint64_t sect_offset = ReferenceValue - Sec.addr; |
| 374 | uint64_t object_offset = Sec.offset + sect_offset; |
| 375 | StringRef MachOContents = info->O->getData(); |
| 376 | uint64_t object_size = MachOContents.size(); |
| 377 | const char *object_addr = (const char *)MachOContents.data(); |
| 378 | if (object_offset < object_size) { |
| 379 | const char *name = object_addr + object_offset; |
| 380 | return name; |
| 381 | } else { |
| 382 | return nullptr; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | } else if (Load.C.cmd == MachO::LC_SEGMENT) { |
| 387 | MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load); |
| 388 | for (unsigned J = 0; J < Seg.nsects; ++J) { |
| 389 | MachO::section Sec = info->O->getSection(Load, J); |
| 390 | uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; |
| 391 | if (section_type == MachO::S_CSTRING_LITERALS && |
| 392 | ReferenceValue >= Sec.addr && |
| 393 | ReferenceValue < Sec.addr + Sec.size) { |
| 394 | uint64_t sect_offset = ReferenceValue - Sec.addr; |
| 395 | uint64_t object_offset = Sec.offset + sect_offset; |
| 396 | StringRef MachOContents = info->O->getData(); |
| 397 | uint64_t object_size = MachOContents.size(); |
| 398 | const char *object_addr = (const char *)MachOContents.data(); |
| 399 | if (object_offset < object_size) { |
| 400 | const char *name = object_addr + object_offset; |
| 401 | return name; |
| 402 | } else { |
| 403 | return nullptr; |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | if (I == LoadCommandCount - 1) |
| 409 | break; |
| 410 | else |
| 411 | Load = info->O->getNextLoadCommandInfo(Load); |
| 412 | } |
| 413 | return nullptr; |
| 414 | } |
| 415 | |
| 416 | // GuessLiteralPointer returns a string which for the item in the Mach-O file |
| 417 | // for the address passed in as ReferenceValue for printing as a comment with |
| 418 | // the instruction and also returns the corresponding type of that item |
| 419 | // indirectly through ReferenceType. |
| 420 | // |
| 421 | // If ReferenceValue is an address of literal cstring then a pointer to the |
| 422 | // cstring is returned and ReferenceType is set to |
| 423 | // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr . |
| 424 | // |
| 425 | // TODO: other literals such as Objective-C CFStrings refs, Selector refs, |
| 426 | // Message refs, Class refs and a Symbol address in a literal pool are yet |
| 427 | // to be done here. |
| 428 | const char *GuessLiteralPointer(uint64_t ReferenceValue, uint64_t ReferencePC, |
| 429 | uint64_t *ReferenceType, |
| 430 | struct DisassembleInfo *info) { |
| 431 | // TODO: This rouine's code is only for an x86_64 Mach-O file for now. |
| 432 | unsigned int Arch = info->O->getArch(); |
| 433 | if (Arch != Triple::x86_64) |
| 434 | return nullptr; |
| 435 | |
| 436 | // First see if there is an external relocation entry at the ReferencePC. |
| 437 | uint64_t sect_addr; |
| 438 | info->S.getAddress(sect_addr); |
| 439 | uint64_t sect_offset = ReferencePC - sect_addr; |
| 440 | bool reloc_found = false; |
| 441 | DataRefImpl Rel; |
| 442 | MachO::any_relocation_info RE; |
| 443 | bool isExtern = false; |
| 444 | SymbolRef Symbol; |
| 445 | for (const RelocationRef &Reloc : info->S.relocations()) { |
| 446 | uint64_t RelocOffset; |
| 447 | Reloc.getOffset(RelocOffset); |
| 448 | if (RelocOffset == sect_offset) { |
| 449 | Rel = Reloc.getRawDataRefImpl(); |
| 450 | RE = info->O->getRelocation(Rel); |
| 451 | if (info->O->isRelocationScattered(RE)) |
| 452 | continue; |
| 453 | isExtern = info->O->getPlainRelocationExternal(RE); |
| 454 | if (isExtern) { |
| 455 | symbol_iterator RelocSym = Reloc.getSymbol(); |
| 456 | Symbol = *RelocSym; |
| 457 | } |
| 458 | reloc_found = true; |
| 459 | break; |
| 460 | } |
| 461 | } |
| 462 | // If there is an external relocation entry for a symbol in a section |
| 463 | // then used that symbol's value for the value of the reference. |
| 464 | if (reloc_found && isExtern) { |
| 465 | if (info->O->getAnyRelocationPCRel(RE)) { |
| 466 | unsigned Type = info->O->getAnyRelocationType(RE); |
| 467 | if (Type == MachO::X86_64_RELOC_SIGNED) { |
| 468 | Symbol.getAddress(ReferenceValue); |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | // TODO: the code to look for other literals such as Objective-C CFStrings |
| 474 | // refs, Selector refs, Message refs, Class refs will be added here. |
| 475 | |
| 476 | const char *name = GuessCstringPointer(ReferenceValue, info); |
| 477 | if (name) { |
| 478 | // TODO: note when the code is added above for Selector refs and Message |
| 479 | // refs we will need check for that here and set the ReferenceType |
| 480 | // accordingly. |
| 481 | *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr; |
| 482 | return name; |
| 483 | } |
| 484 | |
| 485 | // TODO: look for an indirect symbol with this ReferenceValue which is in |
| 486 | // a literal pool. |
| 487 | |
| 488 | return nullptr; |
| 489 | } |
| 490 | |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 491 | // SymbolizerSymbolLookUp is the symbol lookup function passed when creating |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 492 | // the Symbolizer. It looks up the ReferenceValue using the info passed via the |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 493 | // pointer to the struct DisassembleInfo that was passed when MCSymbolizer |
| 494 | // is created and returns the symbol name that matches the ReferenceValue or |
| 495 | // nullptr if none. The ReferenceType is passed in for the IN type of |
| 496 | // reference the instruction is making from the values in defined in the header |
| 497 | // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific |
| 498 | // Out type and the ReferenceName will also be set which is added as a comment |
| 499 | // to the disassembled instruction. |
| 500 | // |
| 501 | // If the symbol name is a C++ mangled name then the demangled name is |
| 502 | // returned through ReferenceName and ReferenceType is set to |
| 503 | // LLVMDisassembler_ReferenceType_DeMangled_Name . |
| 504 | // |
| 505 | // When this is called to get a symbol name for a branch target then the |
| 506 | // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then |
| 507 | // SymbolValue will be looked for in the indirect symbol table to determine if |
| 508 | // it is an address for a symbol stub. If so then the symbol name for that |
| 509 | // stub is returned indirectly through ReferenceName and then ReferenceType is |
| 510 | // set to LLVMDisassembler_ReferenceType_Out_SymbolStub. |
| 511 | // |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 512 | // When this is called with an value loaded via a PC relative load then |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 513 | // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the |
| 514 | // SymbolValue is checked to be an address of literal pointer, symbol pointer, |
| 515 | // or an Objective-C meta data reference. If so the output ReferenceType is |
| 516 | // set to correspond to that as well as ReferenceName. |
| 517 | const char *SymbolizerSymbolLookUp(void *DisInfo, uint64_t ReferenceValue, |
| 518 | uint64_t *ReferenceType, |
| 519 | uint64_t ReferencePC, |
| 520 | const char **ReferenceName) { |
| 521 | struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo; |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 522 | // If no verbose symbolic information is wanted then just return nullptr. |
| 523 | if (info->verbose == false) { |
| 524 | *ReferenceName = nullptr; |
| 525 | *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 526 | return nullptr; |
| 527 | } |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 528 | |
| 529 | const char *SymbolName = nullptr; |
| 530 | StringRef name = info->AddrMap->lookup(ReferenceValue); |
| 531 | if (!name.empty()) |
| 532 | SymbolName = name.data(); |
| 533 | |
| 534 | if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) { |
| 535 | *ReferenceName = GuessLiteralPointer(ReferenceValue, ReferencePC, |
| 536 | ReferenceType, info); |
| 537 | if (*ReferenceName == nullptr) |
| 538 | *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; |
| 539 | // TODO: other types of references to be added. |
| 540 | } else { |
| 541 | *ReferenceName = nullptr; |
| 542 | *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; |
| 543 | } |
| 544 | |
| 545 | return SymbolName; |
| 546 | } |
| 547 | |
| 548 | // |
| 549 | // This is the memory object used by DisAsm->getInstruction() which has its |
| 550 | // BasePC. This then allows the 'address' parameter to getInstruction() to |
| 551 | // be the actual PC of the instruction. Then when a branch dispacement is |
| 552 | // added to the PC of an instruction, the 'ReferenceValue' passed to the |
| 553 | // SymbolizerSymbolLookUp() routine is the correct target addresses. As in |
| 554 | // the case of a fully linked Mach-O file where a section being disassembled |
| 555 | // generally not linked at address zero. |
| 556 | // |
| 557 | class DisasmMemoryObject : public MemoryObject { |
| 558 | uint8_t *Bytes; |
| 559 | uint64_t Size; |
| 560 | uint64_t BasePC; |
| 561 | public: |
| 562 | DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) : |
| 563 | Bytes(bytes), Size(size), BasePC(basePC) {} |
| 564 | |
| 565 | uint64_t getBase() const override { return BasePC; } |
| 566 | uint64_t getExtent() const override { return Size; } |
| 567 | |
| 568 | int readByte(uint64_t Addr, uint8_t *Byte) const override { |
| 569 | if (Addr - BasePC >= Size) |
| 570 | return -1; |
| 571 | *Byte = Bytes[Addr - BasePC]; |
| 572 | return 0; |
| 573 | } |
| 574 | }; |
| 575 | |
| 576 | /// \brief Emits the comments that are stored in the CommentStream. |
| 577 | /// Each comment in the CommentStream must end with a newline. |
| 578 | static void emitComments(raw_svector_ostream &CommentStream, |
| 579 | SmallString<128> &CommentsToEmit, |
| 580 | formatted_raw_ostream &FormattedOS, |
| 581 | const MCAsmInfo &MAI) { |
| 582 | // Flush the stream before taking its content. |
| 583 | CommentStream.flush(); |
| 584 | StringRef Comments = CommentsToEmit.str(); |
| 585 | // Get the default information for printing a comment. |
| 586 | const char *CommentBegin = MAI.getCommentString(); |
| 587 | unsigned CommentColumn = MAI.getCommentColumn(); |
| 588 | bool IsFirst = true; |
| 589 | while (!Comments.empty()) { |
| 590 | if (!IsFirst) |
| 591 | FormattedOS << '\n'; |
| 592 | // Emit a line of comments. |
| 593 | FormattedOS.PadToColumn(CommentColumn); |
| 594 | size_t Position = Comments.find('\n'); |
| 595 | FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position); |
| 596 | // Move after the newline character. |
| 597 | Comments = Comments.substr(Position + 1); |
| 598 | IsFirst = false; |
| 599 | } |
| 600 | FormattedOS.flush(); |
| 601 | |
| 602 | // Tell the comment stream that the vector changed underneath it. |
| 603 | CommentsToEmit.clear(); |
| 604 | CommentStream.resync(); |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 607 | static void DisassembleInputMachO2(StringRef Filename, |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 608 | MachOObjectFile *MachOOF) { |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 609 | const char *McpuDefault = nullptr; |
| 610 | const Target *ThumbTarget = nullptr; |
| 611 | const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 612 | if (!TheTarget) { |
| 613 | // GetTarget prints out stuff. |
| 614 | return; |
| 615 | } |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 616 | if (MCPU.empty() && McpuDefault) |
| 617 | MCPU = McpuDefault; |
| 618 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 619 | std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo()); |
| 620 | std::unique_ptr<MCInstrAnalysis> InstrAnalysis( |
| 621 | TheTarget->createMCInstrAnalysis(InstrInfo.get())); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 622 | std::unique_ptr<const MCInstrInfo> ThumbInstrInfo; |
| 623 | std::unique_ptr<MCInstrAnalysis> ThumbInstrAnalysis; |
| 624 | if (ThumbTarget) { |
| 625 | ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo()); |
| 626 | ThumbInstrAnalysis.reset( |
| 627 | ThumbTarget->createMCInstrAnalysis(ThumbInstrInfo.get())); |
| 628 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 629 | |
Kevin Enderby | c959562 | 2014-08-06 23:24:41 +0000 | [diff] [blame] | 630 | // Package up features to be passed to target/subtarget |
| 631 | std::string FeaturesStr; |
| 632 | if (MAttrs.size()) { |
| 633 | SubtargetFeatures Features; |
| 634 | for (unsigned i = 0; i != MAttrs.size(); ++i) |
| 635 | Features.AddFeature(MAttrs[i]); |
| 636 | FeaturesStr = Features.getString(); |
| 637 | } |
| 638 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 639 | // Set up disassembler. |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 640 | std::unique_ptr<const MCRegisterInfo> MRI( |
| 641 | TheTarget->createMCRegInfo(TripleName)); |
| 642 | std::unique_ptr<const MCAsmInfo> AsmInfo( |
Rafael Espindola | 227144c | 2013-05-13 01:16:13 +0000 | [diff] [blame] | 643 | TheTarget->createMCAsmInfo(*MRI, TripleName)); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 644 | std::unique_ptr<const MCSubtargetInfo> STI( |
Kevin Enderby | c959562 | 2014-08-06 23:24:41 +0000 | [diff] [blame] | 645 | TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr)); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 646 | MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr); |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 647 | std::unique_ptr<MCDisassembler> DisAsm( |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 648 | TheTarget->createMCDisassembler(*STI, Ctx)); |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 649 | std::unique_ptr<MCSymbolizer> Symbolizer; |
| 650 | struct DisassembleInfo SymbolizerInfo; |
| 651 | std::unique_ptr<MCRelocationInfo> RelInfo( |
| 652 | TheTarget->createMCRelocationInfo(TripleName, Ctx)); |
| 653 | if (RelInfo) { |
| 654 | Symbolizer.reset(TheTarget->createMCSymbolizer( |
| 655 | TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp, |
| 656 | &SymbolizerInfo, &Ctx, RelInfo.release())); |
| 657 | DisAsm->setSymbolizer(std::move(Symbolizer)); |
| 658 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 659 | int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 660 | std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( |
| 661 | AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI, *STI)); |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 662 | // Set the display preference for hex vs. decimal immediates. |
| 663 | IP->setPrintImmHex(PrintImmHex); |
| 664 | // Comment stream and backing vector. |
| 665 | SmallString<128> CommentsToEmit; |
| 666 | raw_svector_ostream CommentStream(CommentsToEmit); |
| 667 | IP->setCommentStream(CommentStream); |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 668 | |
| 669 | if (!InstrAnalysis || !AsmInfo || !STI || !DisAsm || !IP) { |
Michael J. Spencer | c1363cf | 2011-10-07 19:25:47 +0000 | [diff] [blame] | 670 | errs() << "error: couldn't initialize disassembler for target " |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 671 | << TripleName << '\n'; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 672 | return; |
| 673 | } |
| 674 | |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 675 | // Set up thumb disassembler. |
| 676 | std::unique_ptr<const MCRegisterInfo> ThumbMRI; |
| 677 | std::unique_ptr<const MCAsmInfo> ThumbAsmInfo; |
| 678 | std::unique_ptr<const MCSubtargetInfo> ThumbSTI; |
| 679 | std::unique_ptr<const MCDisassembler> ThumbDisAsm; |
| 680 | std::unique_ptr<MCInstPrinter> ThumbIP; |
| 681 | std::unique_ptr<MCContext> ThumbCtx; |
| 682 | if (ThumbTarget) { |
| 683 | ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName)); |
| 684 | ThumbAsmInfo.reset( |
| 685 | ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName)); |
| 686 | ThumbSTI.reset( |
| 687 | ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr)); |
| 688 | ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr)); |
| 689 | ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx)); |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 690 | // TODO: add MCSymbolizer here for the ThumbTarget like above for TheTarget. |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 691 | int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect(); |
| 692 | ThumbIP.reset(ThumbTarget->createMCInstPrinter( |
| 693 | ThumbAsmPrinterVariant, *ThumbAsmInfo, *ThumbInstrInfo, *ThumbMRI, |
| 694 | *ThumbSTI)); |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 695 | // Set the display preference for hex vs. decimal immediates. |
| 696 | ThumbIP->setPrintImmHex(PrintImmHex); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | if (ThumbTarget && (!ThumbInstrAnalysis || !ThumbAsmInfo || !ThumbSTI || |
| 700 | !ThumbDisAsm || !ThumbIP)) { |
| 701 | errs() << "error: couldn't initialize disassembler for target " |
| 702 | << ThumbTripleName << '\n'; |
| 703 | return; |
| 704 | } |
| 705 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 706 | outs() << '\n' << Filename << ":\n\n"; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 707 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 708 | MachO::mach_header Header = MachOOF->getHeader(); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 709 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 710 | // FIXME: Using the -cfg command line option, this code used to be able to |
| 711 | // annotate relocations with the referenced symbol's name, and if this was |
| 712 | // inside a __[cf]string section, the data it points to. This is now replaced |
| 713 | // by the upcoming MCSymbolizer, which needs the appropriate setup done above. |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 714 | std::vector<SectionRef> Sections; |
| 715 | std::vector<SymbolRef> Symbols; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 716 | SmallVector<uint64_t, 8> FoundFns; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 717 | uint64_t BaseSegmentAddress; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 718 | |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 719 | getSectionsAndSymbols(Header, MachOOF, Sections, Symbols, FoundFns, |
| 720 | BaseSegmentAddress); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 721 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 722 | // 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] | 723 | std::sort(Symbols.begin(), Symbols.end(), SymbolSorter()); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 724 | |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 725 | // Build a data in code table that is sorted on by the address of each entry. |
| 726 | uint64_t BaseAddress = 0; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 727 | if (Header.filetype == MachO::MH_OBJECT) |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 728 | Sections[0].getAddress(BaseAddress); |
| 729 | else |
| 730 | BaseAddress = BaseSegmentAddress; |
| 731 | DiceTable Dices; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 732 | for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices(); |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 733 | DI != DE; ++DI) { |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 734 | uint32_t Offset; |
| 735 | DI->getOffset(Offset); |
| 736 | Dices.push_back(std::make_pair(BaseAddress + Offset, *DI)); |
| 737 | } |
| 738 | array_pod_sort(Dices.begin(), Dices.end()); |
| 739 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 740 | #ifndef NDEBUG |
| 741 | raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls(); |
| 742 | #else |
| 743 | raw_ostream &DebugOut = nulls(); |
| 744 | #endif |
| 745 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 746 | std::unique_ptr<DIContext> diContext; |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 747 | ObjectFile *DbgObj = MachOOF; |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 748 | // Try to find debug info and set up the DIContext for it. |
| 749 | if (UseDbg) { |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 750 | // A separate DSym file path was specified, parse it as a macho file, |
| 751 | // get the sections and supply it to the section name parsing machinery. |
| 752 | if (!DSYMFile.empty()) { |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 753 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr = |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 754 | MemoryBuffer::getFileOrSTDIN(DSYMFile); |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 755 | if (std::error_code EC = BufOrErr.getError()) { |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 756 | errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n'; |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 757 | return; |
| 758 | } |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 759 | DbgObj = |
| 760 | ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef()) |
| 761 | .get() |
| 762 | .release(); |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 765 | // Setup the DIContext |
Rafael Espindola | a04bb5b | 2014-07-31 20:19:36 +0000 | [diff] [blame] | 766 | diContext.reset(DIContext::getDWARFContext(*DbgObj)); |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 769 | for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) { |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 770 | |
| 771 | bool SectIsText = false; |
| 772 | Sections[SectIdx].isText(SectIsText); |
| 773 | if (SectIsText == false) |
| 774 | continue; |
| 775 | |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 776 | StringRef SectName; |
| 777 | if (Sections[SectIdx].getName(SectName) || |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 778 | SectName != "__text") |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 779 | continue; // Skip non-text sections |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 780 | |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 781 | DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl(); |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 782 | |
Rafael Espindola | b0f76a4 | 2013-04-05 15:15:22 +0000 | [diff] [blame] | 783 | StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR); |
| 784 | if (SegmentName != "__TEXT") |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 785 | continue; |
| 786 | |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 787 | StringRef Bytes; |
| 788 | Sections[SectIdx].getContents(Bytes); |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 789 | uint64_t SectAddress = 0; |
| 790 | Sections[SectIdx].getAddress(SectAddress); |
| 791 | DisasmMemoryObject MemoryObject((uint8_t *)Bytes.data(), Bytes.size(), |
| 792 | SectAddress); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 793 | bool symbolTableWorked = false; |
| 794 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 795 | // Parse relocations. |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 796 | std::vector<std::pair<uint64_t, SymbolRef>> Relocs; |
| 797 | for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) { |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 798 | uint64_t RelocOffset, SectionAddress; |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 799 | Reloc.getOffset(RelocOffset); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 800 | Sections[SectIdx].getAddress(SectionAddress); |
| 801 | RelocOffset -= SectionAddress; |
| 802 | |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 803 | symbol_iterator RelocSym = Reloc.getSymbol(); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 804 | |
Rafael Espindola | 806f006 | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 805 | Relocs.push_back(std::make_pair(RelocOffset, *RelocSym)); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 806 | } |
| 807 | array_pod_sort(Relocs.begin(), Relocs.end()); |
| 808 | |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 809 | // Create a map of symbol addresses to symbol names for use by |
| 810 | // the SymbolizerSymbolLookUp() routine. |
| 811 | SymbolAddressMap AddrMap; |
| 812 | for (const SymbolRef &Symbol : MachOOF->symbols()) { |
| 813 | SymbolRef::Type ST; |
| 814 | Symbol.getType(ST); |
| 815 | if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data || |
| 816 | ST == SymbolRef::ST_Other) { |
| 817 | uint64_t Address; |
| 818 | Symbol.getAddress(Address); |
| 819 | StringRef SymName; |
| 820 | Symbol.getName(SymName); |
| 821 | AddrMap[Address] = SymName; |
| 822 | } |
| 823 | } |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 824 | // Set up the block of info used by the Symbolizer call backs. |
| 825 | SymbolizerInfo.verbose = true; |
| 826 | SymbolizerInfo.O = MachOOF; |
| 827 | SymbolizerInfo.S = Sections[SectIdx]; |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 828 | SymbolizerInfo.AddrMap = &AddrMap; |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 829 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 830 | // Disassemble symbol by symbol. |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 831 | for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) { |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 832 | StringRef SymName; |
| 833 | Symbols[SymIdx].getName(SymName); |
| 834 | |
| 835 | SymbolRef::Type ST; |
| 836 | Symbols[SymIdx].getType(ST); |
| 837 | if (ST != SymbolRef::ST_Function) |
| 838 | continue; |
| 839 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 840 | // Make sure the symbol is defined in this section. |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 841 | bool containsSym = false; |
| 842 | Sections[SectIdx].containsSymbol(Symbols[SymIdx], containsSym); |
| 843 | if (!containsSym) |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 844 | continue; |
| 845 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 846 | // 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] | 847 | uint64_t SectionAddress = 0; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 848 | uint64_t Start = 0; |
Cameron Zwarich | 54478a5 | 2012-02-03 05:42:17 +0000 | [diff] [blame] | 849 | Sections[SectIdx].getAddress(SectionAddress); |
Danil Malyshev | cbe72fc | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 850 | Symbols[SymIdx].getAddress(Start); |
Cameron Zwarich | 54478a5 | 2012-02-03 05:42:17 +0000 | [diff] [blame] | 851 | Start -= SectionAddress; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 852 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 853 | // Stop disassembling either at the beginning of the next symbol or at |
| 854 | // the end of the section. |
Kevin Enderby | edd5872 | 2012-05-15 18:57:14 +0000 | [diff] [blame] | 855 | bool containsNextSym = false; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 856 | uint64_t NextSym = 0; |
| 857 | uint64_t NextSymIdx = SymIdx+1; |
| 858 | while (Symbols.size() > NextSymIdx) { |
| 859 | SymbolRef::Type NextSymType; |
| 860 | Symbols[NextSymIdx].getType(NextSymType); |
| 861 | if (NextSymType == SymbolRef::ST_Function) { |
| 862 | Sections[SectIdx].containsSymbol(Symbols[NextSymIdx], |
| 863 | containsNextSym); |
Danil Malyshev | cbe72fc | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 864 | Symbols[NextSymIdx].getAddress(NextSym); |
Cameron Zwarich | 54478a5 | 2012-02-03 05:42:17 +0000 | [diff] [blame] | 865 | NextSym -= SectionAddress; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 866 | break; |
| 867 | } |
| 868 | ++NextSymIdx; |
| 869 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 870 | |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 871 | uint64_t SectSize; |
| 872 | Sections[SectIdx].getSize(SectSize); |
| 873 | uint64_t End = containsNextSym ? NextSym : SectSize; |
| 874 | uint64_t Size; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 875 | |
| 876 | symbolTableWorked = true; |
| 877 | |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 878 | DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl(); |
| 879 | bool isThumb = |
| 880 | (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget; |
| 881 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 882 | outs() << SymName << ":\n"; |
| 883 | DILineInfo lastLine; |
| 884 | for (uint64_t Index = Start; Index < End; Index += Size) { |
| 885 | MCInst Inst; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 886 | |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 887 | uint64_t PC = SectAddress + Index; |
| 888 | if (FullLeadingAddr) { |
| 889 | if (MachOOF->is64Bit()) |
| 890 | outs() << format("%016" PRIx64, PC); |
| 891 | else |
| 892 | outs() << format("%08" PRIx64, PC); |
| 893 | } else { |
| 894 | outs() << format("%8" PRIx64 ":", PC); |
| 895 | } |
| 896 | if (!NoShowRawInsn) |
| 897 | outs() << "\t"; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 898 | |
| 899 | // Check the data in code table here to see if this is data not an |
| 900 | // instruction to be disassembled. |
| 901 | DiceTable Dice; |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 902 | Dice.push_back(std::make_pair(PC, DiceRef())); |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 903 | dice_table_iterator DTI = std::search(Dices.begin(), Dices.end(), |
| 904 | Dice.begin(), Dice.end(), |
| 905 | compareDiceTableEntries); |
| 906 | if (DTI != Dices.end()){ |
| 907 | uint16_t Length; |
| 908 | DTI->second.getLength(Length); |
| 909 | DumpBytes(StringRef(Bytes.data() + Index, Length)); |
| 910 | uint16_t Kind; |
| 911 | DTI->second.getKind(Kind); |
| 912 | DumpDataInCode(Bytes.data() + Index, Length, Kind); |
| 913 | continue; |
| 914 | } |
| 915 | |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 916 | SmallVector<char, 64> AnnotationsBytes; |
| 917 | raw_svector_ostream Annotations(AnnotationsBytes); |
| 918 | |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 919 | bool gotInst; |
| 920 | if (isThumb) |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 921 | gotInst = ThumbDisAsm->getInstruction(Inst, Size, MemoryObject, PC, |
| 922 | DebugOut, Annotations); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 923 | else |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 924 | gotInst = DisAsm->getInstruction(Inst, Size, MemoryObject, PC, |
| 925 | DebugOut, Annotations); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 926 | if (gotInst) { |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 927 | if (!NoShowRawInsn) { |
| 928 | DumpBytes(StringRef(Bytes.data() + Index, Size)); |
| 929 | } |
| 930 | formatted_raw_ostream FormattedOS(outs()); |
| 931 | Annotations.flush(); |
| 932 | StringRef AnnotationsStr = Annotations.str(); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 933 | if (isThumb) |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 934 | ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 935 | else |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 936 | IP->printInst(&Inst, FormattedOS, AnnotationsStr); |
| 937 | emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 938 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 939 | // Print debug info. |
| 940 | if (diContext) { |
| 941 | DILineInfo dli = |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 942 | diContext->getLineInfoForAddress(PC); |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 943 | // Print valid line info if it changed. |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 944 | if (dli != lastLine && dli.Line != 0) |
| 945 | outs() << "\t## " << dli.FileName << ':' << dli.Line << ':' |
| 946 | << dli.Column; |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 947 | lastLine = dli; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 948 | } |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 949 | outs() << "\n"; |
| 950 | } else { |
| 951 | errs() << "llvm-objdump: warning: invalid instruction encoding\n"; |
| 952 | if (Size == 0) |
| 953 | Size = 1; // skip illegible bytes |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 954 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 955 | } |
| 956 | } |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 957 | if (!symbolTableWorked) { |
Kevin Enderby | badd100 | 2012-05-18 00:13:56 +0000 | [diff] [blame] | 958 | // Reading the symbol table didn't work, disassemble the whole section. |
| 959 | uint64_t SectAddress; |
| 960 | Sections[SectIdx].getAddress(SectAddress); |
| 961 | uint64_t SectSize; |
| 962 | Sections[SectIdx].getSize(SectSize); |
| 963 | uint64_t InstSize; |
| 964 | for (uint64_t Index = 0; Index < SectSize; Index += InstSize) { |
Bill Wendling | 4e68e06 | 2012-07-19 00:17:40 +0000 | [diff] [blame] | 965 | MCInst Inst; |
Kevin Enderby | badd100 | 2012-05-18 00:13:56 +0000 | [diff] [blame] | 966 | |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 967 | uint64_t PC = SectAddress + Index; |
| 968 | if (DisAsm->getInstruction(Inst, InstSize, MemoryObject, PC, |
Bill Wendling | 4e68e06 | 2012-07-19 00:17:40 +0000 | [diff] [blame] | 969 | DebugOut, nulls())) { |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame^] | 970 | if (FullLeadingAddr) { |
| 971 | if (MachOOF->is64Bit()) |
| 972 | outs() << format("%016" PRIx64, PC); |
| 973 | else |
| 974 | outs() << format("%08" PRIx64, PC); |
| 975 | } else { |
| 976 | outs() << format("%8" PRIx64 ":", PC); |
| 977 | } |
| 978 | if (!NoShowRawInsn) { |
| 979 | outs() << "\t"; |
| 980 | DumpBytes(StringRef(Bytes.data() + Index, InstSize)); |
| 981 | } |
Bill Wendling | 4e68e06 | 2012-07-19 00:17:40 +0000 | [diff] [blame] | 982 | IP->printInst(&Inst, outs(), ""); |
| 983 | outs() << "\n"; |
| 984 | } else { |
| 985 | errs() << "llvm-objdump: warning: invalid instruction encoding\n"; |
| 986 | if (InstSize == 0) |
| 987 | InstSize = 1; // skip illegible bytes |
| 988 | } |
Kevin Enderby | badd100 | 2012-05-18 00:13:56 +0000 | [diff] [blame] | 989 | } |
| 990 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 991 | } |
| 992 | } |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 993 | |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 994 | |
| 995 | //===----------------------------------------------------------------------===// |
| 996 | // __compact_unwind section dumping |
| 997 | //===----------------------------------------------------------------------===// |
| 998 | |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 999 | namespace { |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 1000 | |
| 1001 | template <typename T> static uint64_t readNext(const char *&Buf) { |
| 1002 | using llvm::support::little; |
| 1003 | using llvm::support::unaligned; |
| 1004 | |
| 1005 | uint64_t Val = support::endian::read<T, little, unaligned>(Buf); |
| 1006 | Buf += sizeof(T); |
| 1007 | return Val; |
| 1008 | } |
| 1009 | |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 1010 | struct CompactUnwindEntry { |
| 1011 | uint32_t OffsetInSection; |
| 1012 | |
| 1013 | uint64_t FunctionAddr; |
| 1014 | uint32_t Length; |
| 1015 | uint32_t CompactEncoding; |
| 1016 | uint64_t PersonalityAddr; |
| 1017 | uint64_t LSDAAddr; |
| 1018 | |
| 1019 | RelocationRef FunctionReloc; |
| 1020 | RelocationRef PersonalityReloc; |
| 1021 | RelocationRef LSDAReloc; |
| 1022 | |
| 1023 | CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64) |
| 1024 | : OffsetInSection(Offset) { |
| 1025 | if (Is64) |
| 1026 | read<uint64_t>(Contents.data() + Offset); |
| 1027 | else |
| 1028 | read<uint32_t>(Contents.data() + Offset); |
| 1029 | } |
| 1030 | |
| 1031 | private: |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 1032 | template<typename UIntPtr> |
| 1033 | void read(const char *Buf) { |
| 1034 | FunctionAddr = readNext<UIntPtr>(Buf); |
| 1035 | Length = readNext<uint32_t>(Buf); |
| 1036 | CompactEncoding = readNext<uint32_t>(Buf); |
| 1037 | PersonalityAddr = readNext<UIntPtr>(Buf); |
| 1038 | LSDAAddr = readNext<UIntPtr>(Buf); |
| 1039 | } |
| 1040 | }; |
| 1041 | } |
| 1042 | |
| 1043 | /// Given a relocation from __compact_unwind, consisting of the RelocationRef |
| 1044 | /// and data being relocated, determine the best base Name and Addend to use for |
| 1045 | /// display purposes. |
| 1046 | /// |
| 1047 | /// 1. An Extern relocation will directly reference a symbol (and the data is |
| 1048 | /// then already an addend), so use that. |
| 1049 | /// 2. Otherwise the data is an offset in the object file's layout; try to find |
| 1050 | // a symbol before it in the same section, and use the offset from there. |
| 1051 | /// 3. Finally, if all that fails, fall back to an offset from the start of the |
| 1052 | /// referenced section. |
| 1053 | static void findUnwindRelocNameAddend(const MachOObjectFile *Obj, |
| 1054 | std::map<uint64_t, SymbolRef> &Symbols, |
| 1055 | const RelocationRef &Reloc, |
| 1056 | uint64_t Addr, |
| 1057 | StringRef &Name, uint64_t &Addend) { |
| 1058 | if (Reloc.getSymbol() != Obj->symbol_end()) { |
| 1059 | Reloc.getSymbol()->getName(Name); |
| 1060 | Addend = Addr; |
| 1061 | return; |
| 1062 | } |
| 1063 | |
| 1064 | auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl()); |
| 1065 | SectionRef RelocSection = Obj->getRelocationSection(RE); |
| 1066 | |
| 1067 | uint64_t SectionAddr; |
| 1068 | RelocSection.getAddress(SectionAddr); |
| 1069 | |
| 1070 | auto Sym = Symbols.upper_bound(Addr); |
| 1071 | if (Sym == Symbols.begin()) { |
| 1072 | // The first symbol in the object is after this reference, the best we can |
| 1073 | // do is section-relative notation. |
| 1074 | RelocSection.getName(Name); |
| 1075 | Addend = Addr - SectionAddr; |
| 1076 | return; |
| 1077 | } |
| 1078 | |
| 1079 | // Go back one so that SymbolAddress <= Addr. |
| 1080 | --Sym; |
| 1081 | |
| 1082 | section_iterator SymSection = Obj->section_end(); |
| 1083 | Sym->second.getSection(SymSection); |
| 1084 | if (RelocSection == *SymSection) { |
| 1085 | // There's a valid symbol in the same section before this reference. |
| 1086 | Sym->second.getName(Name); |
| 1087 | Addend = Addr - Sym->first; |
| 1088 | return; |
| 1089 | } |
| 1090 | |
| 1091 | // There is a symbol before this reference, but it's in a different |
| 1092 | // section. Probably not helpful to mention it, so use the section name. |
| 1093 | RelocSection.getName(Name); |
| 1094 | Addend = Addr - SectionAddr; |
| 1095 | } |
| 1096 | |
| 1097 | static void printUnwindRelocDest(const MachOObjectFile *Obj, |
| 1098 | std::map<uint64_t, SymbolRef> &Symbols, |
| 1099 | const RelocationRef &Reloc, |
| 1100 | uint64_t Addr) { |
| 1101 | StringRef Name; |
| 1102 | uint64_t Addend; |
| 1103 | |
Tim Northover | 0b0add5 | 2014-09-09 10:45:06 +0000 | [diff] [blame] | 1104 | if (!Reloc.getObjectFile()) |
| 1105 | return; |
| 1106 | |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 1107 | findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend); |
| 1108 | |
| 1109 | outs() << Name; |
| 1110 | if (Addend) |
Tim Northover | 63a2562 | 2014-08-11 09:14:06 +0000 | [diff] [blame] | 1111 | outs() << " + " << format("0x%" PRIx64, Addend); |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | static void |
| 1115 | printMachOCompactUnwindSection(const MachOObjectFile *Obj, |
| 1116 | std::map<uint64_t, SymbolRef> &Symbols, |
| 1117 | const SectionRef &CompactUnwind) { |
| 1118 | |
| 1119 | assert(Obj->isLittleEndian() && |
| 1120 | "There should not be a big-endian .o with __compact_unwind"); |
| 1121 | |
| 1122 | bool Is64 = Obj->is64Bit(); |
| 1123 | uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t); |
| 1124 | uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t); |
| 1125 | |
| 1126 | StringRef Contents; |
| 1127 | CompactUnwind.getContents(Contents); |
| 1128 | |
| 1129 | SmallVector<CompactUnwindEntry, 4> CompactUnwinds; |
| 1130 | |
| 1131 | // First populate the initial raw offsets, encodings and so on from the entry. |
| 1132 | for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) { |
| 1133 | CompactUnwindEntry Entry(Contents.data(), Offset, Is64); |
| 1134 | CompactUnwinds.push_back(Entry); |
| 1135 | } |
| 1136 | |
| 1137 | // Next we need to look at the relocations to find out what objects are |
| 1138 | // actually being referred to. |
| 1139 | for (const RelocationRef &Reloc : CompactUnwind.relocations()) { |
| 1140 | uint64_t RelocAddress; |
| 1141 | Reloc.getOffset(RelocAddress); |
| 1142 | |
| 1143 | uint32_t EntryIdx = RelocAddress / EntrySize; |
| 1144 | uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize; |
| 1145 | CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx]; |
| 1146 | |
| 1147 | if (OffsetInEntry == 0) |
| 1148 | Entry.FunctionReloc = Reloc; |
| 1149 | else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t)) |
| 1150 | Entry.PersonalityReloc = Reloc; |
| 1151 | else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t)) |
| 1152 | Entry.LSDAReloc = Reloc; |
| 1153 | else |
| 1154 | llvm_unreachable("Unexpected relocation in __compact_unwind section"); |
| 1155 | } |
| 1156 | |
| 1157 | // Finally, we're ready to print the data we've gathered. |
| 1158 | outs() << "Contents of __compact_unwind section:\n"; |
| 1159 | for (auto &Entry : CompactUnwinds) { |
Tim Northover | 06af260 | 2014-08-08 12:08:51 +0000 | [diff] [blame] | 1160 | outs() << " Entry at offset " |
| 1161 | << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n"; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 1162 | |
| 1163 | // 1. Start of the region this entry applies to. |
| 1164 | outs() << " start: " |
Tim Northover | b911bf8 | 2014-08-08 12:00:09 +0000 | [diff] [blame] | 1165 | << format("0x%" PRIx64, Entry.FunctionAddr) << ' '; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 1166 | printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, |
| 1167 | Entry.FunctionAddr); |
| 1168 | outs() << '\n'; |
| 1169 | |
| 1170 | // 2. Length of the region this entry applies to. |
| 1171 | outs() << " length: " |
Tim Northover | b911bf8 | 2014-08-08 12:00:09 +0000 | [diff] [blame] | 1172 | << format("0x%" PRIx32, Entry.Length) << '\n'; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 1173 | // 3. The 32-bit compact encoding. |
| 1174 | outs() << " compact encoding: " |
Tim Northover | b911bf8 | 2014-08-08 12:00:09 +0000 | [diff] [blame] | 1175 | << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n'; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 1176 | |
| 1177 | // 4. The personality function, if present. |
| 1178 | if (Entry.PersonalityReloc.getObjectFile()) { |
| 1179 | outs() << " personality function: " |
Tim Northover | b911bf8 | 2014-08-08 12:00:09 +0000 | [diff] [blame] | 1180 | << format("0x%" PRIx64, Entry.PersonalityAddr) << ' '; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 1181 | printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc, |
| 1182 | Entry.PersonalityAddr); |
| 1183 | outs() << '\n'; |
| 1184 | } |
| 1185 | |
| 1186 | // 5. This entry's language-specific data area. |
| 1187 | if (Entry.LSDAReloc.getObjectFile()) { |
| 1188 | outs() << " LSDA: " |
Tim Northover | b911bf8 | 2014-08-08 12:00:09 +0000 | [diff] [blame] | 1189 | << format("0x%" PRIx64, Entry.LSDAAddr) << ' '; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 1190 | printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr); |
| 1191 | outs() << '\n'; |
| 1192 | } |
| 1193 | } |
| 1194 | } |
| 1195 | |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 1196 | //===----------------------------------------------------------------------===// |
| 1197 | // __unwind_info section dumping |
| 1198 | //===----------------------------------------------------------------------===// |
| 1199 | |
| 1200 | static void printRegularSecondLevelUnwindPage(const char *PageStart) { |
| 1201 | const char *Pos = PageStart; |
| 1202 | uint32_t Kind = readNext<uint32_t>(Pos); |
| 1203 | (void)Kind; |
| 1204 | assert(Kind == 2 && "kind for a regular 2nd level index should be 2"); |
| 1205 | |
| 1206 | uint16_t EntriesStart = readNext<uint16_t>(Pos); |
| 1207 | uint16_t NumEntries = readNext<uint16_t>(Pos); |
| 1208 | |
| 1209 | Pos = PageStart + EntriesStart; |
| 1210 | for (unsigned i = 0; i < NumEntries; ++i) { |
| 1211 | uint32_t FunctionOffset = readNext<uint32_t>(Pos); |
| 1212 | uint32_t Encoding = readNext<uint32_t>(Pos); |
| 1213 | |
| 1214 | outs() << " [" << i << "]: " |
| 1215 | << "function offset=" |
| 1216 | << format("0x%08" PRIx32, FunctionOffset) << ", " |
| 1217 | << "encoding=" |
| 1218 | << format("0x%08" PRIx32, Encoding) |
| 1219 | << '\n'; |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | static void printCompressedSecondLevelUnwindPage( |
| 1224 | const char *PageStart, uint32_t FunctionBase, |
| 1225 | const SmallVectorImpl<uint32_t> &CommonEncodings) { |
| 1226 | const char *Pos = PageStart; |
| 1227 | uint32_t Kind = readNext<uint32_t>(Pos); |
| 1228 | (void)Kind; |
| 1229 | assert(Kind == 3 && "kind for a compressed 2nd level index should be 3"); |
| 1230 | |
| 1231 | uint16_t EntriesStart = readNext<uint16_t>(Pos); |
| 1232 | uint16_t NumEntries = readNext<uint16_t>(Pos); |
| 1233 | |
| 1234 | uint16_t EncodingsStart = readNext<uint16_t>(Pos); |
| 1235 | readNext<uint16_t>(Pos); |
Aaron Ballman | 80930af | 2014-08-14 13:53:19 +0000 | [diff] [blame] | 1236 | const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>( |
| 1237 | PageStart + EncodingsStart); |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 1238 | |
| 1239 | Pos = PageStart + EntriesStart; |
| 1240 | for (unsigned i = 0; i < NumEntries; ++i) { |
| 1241 | uint32_t Entry = readNext<uint32_t>(Pos); |
| 1242 | uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff); |
| 1243 | uint32_t EncodingIdx = Entry >> 24; |
| 1244 | |
| 1245 | uint32_t Encoding; |
| 1246 | if (EncodingIdx < CommonEncodings.size()) |
| 1247 | Encoding = CommonEncodings[EncodingIdx]; |
| 1248 | else |
| 1249 | Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()]; |
| 1250 | |
| 1251 | outs() << " [" << i << "]: " |
| 1252 | << "function offset=" |
| 1253 | << format("0x%08" PRIx32, FunctionOffset) << ", " |
| 1254 | << "encoding[" << EncodingIdx << "]=" |
| 1255 | << format("0x%08" PRIx32, Encoding) |
| 1256 | << '\n'; |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | static void |
| 1261 | printMachOUnwindInfoSection(const MachOObjectFile *Obj, |
| 1262 | std::map<uint64_t, SymbolRef> &Symbols, |
| 1263 | const SectionRef &UnwindInfo) { |
| 1264 | |
| 1265 | assert(Obj->isLittleEndian() && |
| 1266 | "There should not be a big-endian .o with __unwind_info"); |
| 1267 | |
| 1268 | outs() << "Contents of __unwind_info section:\n"; |
| 1269 | |
| 1270 | StringRef Contents; |
| 1271 | UnwindInfo.getContents(Contents); |
| 1272 | const char *Pos = Contents.data(); |
| 1273 | |
| 1274 | //===---------------------------------- |
| 1275 | // Section header |
| 1276 | //===---------------------------------- |
| 1277 | |
| 1278 | uint32_t Version = readNext<uint32_t>(Pos); |
| 1279 | outs() << " Version: " |
| 1280 | << format("0x%" PRIx32, Version) << '\n'; |
| 1281 | assert(Version == 1 && "only understand version 1"); |
| 1282 | |
| 1283 | uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos); |
| 1284 | outs() << " Common encodings array section offset: " |
| 1285 | << format("0x%" PRIx32, CommonEncodingsStart) << '\n'; |
| 1286 | uint32_t NumCommonEncodings = readNext<uint32_t>(Pos); |
| 1287 | outs() << " Number of common encodings in array: " |
| 1288 | << format("0x%" PRIx32, NumCommonEncodings) << '\n'; |
| 1289 | |
| 1290 | uint32_t PersonalitiesStart = readNext<uint32_t>(Pos); |
| 1291 | outs() << " Personality function array section offset: " |
| 1292 | << format("0x%" PRIx32, PersonalitiesStart) << '\n'; |
| 1293 | uint32_t NumPersonalities = readNext<uint32_t>(Pos); |
| 1294 | outs() << " Number of personality functions in array: " |
| 1295 | << format("0x%" PRIx32, NumPersonalities) << '\n'; |
| 1296 | |
| 1297 | uint32_t IndicesStart = readNext<uint32_t>(Pos); |
| 1298 | outs() << " Index array section offset: " |
| 1299 | << format("0x%" PRIx32, IndicesStart) << '\n'; |
| 1300 | uint32_t NumIndices = readNext<uint32_t>(Pos); |
| 1301 | outs() << " Number of indices in array: " |
| 1302 | << format("0x%" PRIx32, NumIndices) << '\n'; |
| 1303 | |
| 1304 | //===---------------------------------- |
| 1305 | // A shared list of common encodings |
| 1306 | //===---------------------------------- |
| 1307 | |
| 1308 | // These occupy indices in the range [0, N] whenever an encoding is referenced |
| 1309 | // from a compressed 2nd level index table. In practice the linker only |
| 1310 | // creates ~128 of these, so that indices are available to embed encodings in |
| 1311 | // the 2nd level index. |
| 1312 | |
| 1313 | SmallVector<uint32_t, 64> CommonEncodings; |
| 1314 | outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n"; |
| 1315 | Pos = Contents.data() + CommonEncodingsStart; |
| 1316 | for (unsigned i = 0; i < NumCommonEncodings; ++i) { |
| 1317 | uint32_t Encoding = readNext<uint32_t>(Pos); |
| 1318 | CommonEncodings.push_back(Encoding); |
| 1319 | |
| 1320 | outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding) |
| 1321 | << '\n'; |
| 1322 | } |
| 1323 | |
| 1324 | |
| 1325 | //===---------------------------------- |
| 1326 | // Personality functions used in this executable |
| 1327 | //===---------------------------------- |
| 1328 | |
| 1329 | // There should be only a handful of these (one per source language, |
| 1330 | // roughly). Particularly since they only get 2 bits in the compact encoding. |
| 1331 | |
| 1332 | outs() << " Personality functions: (count = " << NumPersonalities << ")\n"; |
| 1333 | Pos = Contents.data() + PersonalitiesStart; |
| 1334 | for (unsigned i = 0; i < NumPersonalities; ++i) { |
| 1335 | uint32_t PersonalityFn = readNext<uint32_t>(Pos); |
| 1336 | outs() << " personality[" << i + 1 |
| 1337 | << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n'; |
| 1338 | } |
| 1339 | |
| 1340 | //===---------------------------------- |
| 1341 | // The level 1 index entries |
| 1342 | //===---------------------------------- |
| 1343 | |
| 1344 | // These specify an approximate place to start searching for the more detailed |
| 1345 | // information, sorted by PC. |
| 1346 | |
| 1347 | struct IndexEntry { |
| 1348 | uint32_t FunctionOffset; |
| 1349 | uint32_t SecondLevelPageStart; |
| 1350 | uint32_t LSDAStart; |
| 1351 | }; |
| 1352 | |
| 1353 | SmallVector<IndexEntry, 4> IndexEntries; |
| 1354 | |
| 1355 | outs() << " Top level indices: (count = " << NumIndices << ")\n"; |
| 1356 | Pos = Contents.data() + IndicesStart; |
| 1357 | for (unsigned i = 0; i < NumIndices; ++i) { |
| 1358 | IndexEntry Entry; |
| 1359 | |
| 1360 | Entry.FunctionOffset = readNext<uint32_t>(Pos); |
| 1361 | Entry.SecondLevelPageStart = readNext<uint32_t>(Pos); |
| 1362 | Entry.LSDAStart = readNext<uint32_t>(Pos); |
| 1363 | IndexEntries.push_back(Entry); |
| 1364 | |
| 1365 | outs() << " [" << i << "]: " |
| 1366 | << "function offset=" |
| 1367 | << format("0x%08" PRIx32, Entry.FunctionOffset) << ", " |
| 1368 | << "2nd level page offset=" |
| 1369 | << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", " |
| 1370 | << "LSDA offset=" |
| 1371 | << format("0x%08" PRIx32, Entry.LSDAStart) << '\n'; |
| 1372 | } |
| 1373 | |
| 1374 | |
| 1375 | //===---------------------------------- |
| 1376 | // Next come the LSDA tables |
| 1377 | //===---------------------------------- |
| 1378 | |
| 1379 | // The LSDA layout is rather implicit: it's a contiguous array of entries from |
| 1380 | // the first top-level index's LSDAOffset to the last (sentinel). |
| 1381 | |
| 1382 | outs() << " LSDA descriptors:\n"; |
| 1383 | Pos = Contents.data() + IndexEntries[0].LSDAStart; |
| 1384 | int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) / |
| 1385 | (2 * sizeof(uint32_t)); |
| 1386 | for (int i = 0; i < NumLSDAs; ++i) { |
| 1387 | uint32_t FunctionOffset = readNext<uint32_t>(Pos); |
| 1388 | uint32_t LSDAOffset = readNext<uint32_t>(Pos); |
| 1389 | outs() << " [" << i << "]: " |
| 1390 | << "function offset=" |
| 1391 | << format("0x%08" PRIx32, FunctionOffset) << ", " |
| 1392 | << "LSDA offset=" |
| 1393 | << format("0x%08" PRIx32, LSDAOffset) << '\n'; |
| 1394 | } |
| 1395 | |
| 1396 | //===---------------------------------- |
| 1397 | // Finally, the 2nd level indices |
| 1398 | //===---------------------------------- |
| 1399 | |
| 1400 | // Generally these are 4K in size, and have 2 possible forms: |
| 1401 | // + Regular stores up to 511 entries with disparate encodings |
| 1402 | // + Compressed stores up to 1021 entries if few enough compact encoding |
| 1403 | // values are used. |
| 1404 | outs() << " Second level indices:\n"; |
| 1405 | for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) { |
| 1406 | // The final sentinel top-level index has no associated 2nd level page |
| 1407 | if (IndexEntries[i].SecondLevelPageStart == 0) |
| 1408 | break; |
| 1409 | |
| 1410 | outs() << " Second level index[" << i << "]: " |
| 1411 | << "offset in section=" |
| 1412 | << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart) |
| 1413 | << ", " |
| 1414 | << "base function offset=" |
| 1415 | << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n'; |
| 1416 | |
| 1417 | Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart; |
Aaron Ballman | 80930af | 2014-08-14 13:53:19 +0000 | [diff] [blame] | 1418 | uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos); |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 1419 | if (Kind == 2) |
| 1420 | printRegularSecondLevelUnwindPage(Pos); |
| 1421 | else if (Kind == 3) |
| 1422 | printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset, |
| 1423 | CommonEncodings); |
| 1424 | else |
| 1425 | llvm_unreachable("Do not know how to print this kind of 2nd level page"); |
| 1426 | |
| 1427 | } |
| 1428 | } |
| 1429 | |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 1430 | void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) { |
| 1431 | std::map<uint64_t, SymbolRef> Symbols; |
| 1432 | for (const SymbolRef &SymRef : Obj->symbols()) { |
| 1433 | // Discard any undefined or absolute symbols. They're not going to take part |
| 1434 | // in the convenience lookup for unwind info and just take up resources. |
| 1435 | section_iterator Section = Obj->section_end(); |
| 1436 | SymRef.getSection(Section); |
| 1437 | if (Section == Obj->section_end()) |
| 1438 | continue; |
| 1439 | |
| 1440 | uint64_t Addr; |
| 1441 | SymRef.getAddress(Addr); |
| 1442 | Symbols.insert(std::make_pair(Addr, SymRef)); |
| 1443 | } |
| 1444 | |
| 1445 | for (const SectionRef &Section : Obj->sections()) { |
| 1446 | StringRef SectName; |
| 1447 | Section.getName(SectName); |
| 1448 | if (SectName == "__compact_unwind") |
| 1449 | printMachOCompactUnwindSection(Obj, Symbols, Section); |
| 1450 | else if (SectName == "__unwind_info") |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 1451 | printMachOUnwindInfoSection(Obj, Symbols, Section); |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 1452 | else if (SectName == "__eh_frame") |
| 1453 | outs() << "llvm-objdump: warning: unhandled __eh_frame section\n"; |
| 1454 | |
| 1455 | } |
| 1456 | } |
Kevin Enderby | b76d386 | 2014-08-22 20:35:18 +0000 | [diff] [blame] | 1457 | |
| 1458 | static void PrintMachHeader(uint32_t magic, uint32_t cputype, |
| 1459 | uint32_t cpusubtype, uint32_t filetype, |
| 1460 | uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags, |
| 1461 | bool verbose) { |
| 1462 | outs() << "Mach header\n"; |
| 1463 | outs() << " magic cputype cpusubtype caps filetype ncmds " |
| 1464 | "sizeofcmds flags\n"; |
| 1465 | if (verbose) { |
| 1466 | if (magic == MachO::MH_MAGIC) |
| 1467 | outs() << " MH_MAGIC"; |
| 1468 | else if (magic == MachO::MH_MAGIC_64) |
| 1469 | outs() << "MH_MAGIC_64"; |
| 1470 | else |
| 1471 | outs() << format(" 0x%08" PRIx32, magic); |
| 1472 | switch (cputype) { |
| 1473 | case MachO::CPU_TYPE_I386: |
| 1474 | outs() << " I386"; |
| 1475 | switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { |
| 1476 | case MachO::CPU_SUBTYPE_I386_ALL: |
| 1477 | outs() << " ALL"; |
| 1478 | break; |
| 1479 | default: |
| 1480 | outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); |
| 1481 | break; |
| 1482 | } |
| 1483 | break; |
| 1484 | case MachO::CPU_TYPE_X86_64: |
| 1485 | outs() << " X86_64"; |
| 1486 | case MachO::CPU_SUBTYPE_X86_64_ALL: |
| 1487 | outs() << " ALL"; |
| 1488 | break; |
| 1489 | case MachO::CPU_SUBTYPE_X86_64_H: |
| 1490 | outs() << " Haswell"; |
Aaron Ballman | 9d515ff | 2014-08-24 13:25:16 +0000 | [diff] [blame] | 1491 | outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); |
Kevin Enderby | b76d386 | 2014-08-22 20:35:18 +0000 | [diff] [blame] | 1492 | break; |
| 1493 | case MachO::CPU_TYPE_ARM: |
| 1494 | outs() << " ARM"; |
| 1495 | switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { |
| 1496 | case MachO::CPU_SUBTYPE_ARM_ALL: |
| 1497 | outs() << " ALL"; |
| 1498 | break; |
| 1499 | case MachO::CPU_SUBTYPE_ARM_V4T: |
| 1500 | outs() << " V4T"; |
| 1501 | break; |
| 1502 | case MachO::CPU_SUBTYPE_ARM_V5TEJ: |
| 1503 | outs() << " V5TEJ"; |
| 1504 | break; |
| 1505 | case MachO::CPU_SUBTYPE_ARM_XSCALE: |
| 1506 | outs() << " XSCALE"; |
| 1507 | break; |
| 1508 | case MachO::CPU_SUBTYPE_ARM_V6: |
| 1509 | outs() << " V6"; |
| 1510 | break; |
| 1511 | case MachO::CPU_SUBTYPE_ARM_V6M: |
| 1512 | outs() << " V6M"; |
| 1513 | break; |
| 1514 | case MachO::CPU_SUBTYPE_ARM_V7: |
| 1515 | outs() << " V7"; |
| 1516 | break; |
| 1517 | case MachO::CPU_SUBTYPE_ARM_V7EM: |
| 1518 | outs() << " V7EM"; |
| 1519 | break; |
| 1520 | case MachO::CPU_SUBTYPE_ARM_V7K: |
| 1521 | outs() << " V7K"; |
| 1522 | break; |
| 1523 | case MachO::CPU_SUBTYPE_ARM_V7M: |
| 1524 | outs() << " V7M"; |
| 1525 | break; |
| 1526 | case MachO::CPU_SUBTYPE_ARM_V7S: |
| 1527 | outs() << " V7S"; |
| 1528 | break; |
| 1529 | default: |
| 1530 | outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); |
| 1531 | break; |
| 1532 | } |
| 1533 | break; |
| 1534 | case MachO::CPU_TYPE_ARM64: |
| 1535 | outs() << " ARM64"; |
| 1536 | switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { |
| 1537 | case MachO::CPU_SUBTYPE_ARM64_ALL: |
| 1538 | outs() << " ALL"; |
| 1539 | break; |
| 1540 | default: |
| 1541 | outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); |
| 1542 | break; |
| 1543 | } |
| 1544 | break; |
| 1545 | case MachO::CPU_TYPE_POWERPC: |
| 1546 | outs() << " PPC"; |
| 1547 | switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { |
| 1548 | case MachO::CPU_SUBTYPE_POWERPC_ALL: |
| 1549 | outs() << " ALL"; |
| 1550 | break; |
| 1551 | default: |
| 1552 | outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); |
| 1553 | break; |
| 1554 | } |
| 1555 | break; |
| 1556 | case MachO::CPU_TYPE_POWERPC64: |
| 1557 | outs() << " PPC64"; |
| 1558 | switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { |
| 1559 | case MachO::CPU_SUBTYPE_POWERPC_ALL: |
| 1560 | outs() << " ALL"; |
| 1561 | break; |
| 1562 | default: |
| 1563 | outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); |
| 1564 | break; |
| 1565 | } |
| 1566 | break; |
| 1567 | } |
| 1568 | if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) { |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 1569 | outs() << " LIB64"; |
Kevin Enderby | b76d386 | 2014-08-22 20:35:18 +0000 | [diff] [blame] | 1570 | } else { |
| 1571 | outs() << format(" 0x%02" PRIx32, |
| 1572 | (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24); |
| 1573 | } |
| 1574 | switch (filetype) { |
| 1575 | case MachO::MH_OBJECT: |
| 1576 | outs() << " OBJECT"; |
| 1577 | break; |
| 1578 | case MachO::MH_EXECUTE: |
| 1579 | outs() << " EXECUTE"; |
| 1580 | break; |
| 1581 | case MachO::MH_FVMLIB: |
| 1582 | outs() << " FVMLIB"; |
| 1583 | break; |
| 1584 | case MachO::MH_CORE: |
| 1585 | outs() << " CORE"; |
| 1586 | break; |
| 1587 | case MachO::MH_PRELOAD: |
| 1588 | outs() << " PRELOAD"; |
| 1589 | break; |
| 1590 | case MachO::MH_DYLIB: |
| 1591 | outs() << " DYLIB"; |
| 1592 | break; |
| 1593 | case MachO::MH_DYLIB_STUB: |
| 1594 | outs() << " DYLIB_STUB"; |
| 1595 | break; |
| 1596 | case MachO::MH_DYLINKER: |
| 1597 | outs() << " DYLINKER"; |
| 1598 | break; |
| 1599 | case MachO::MH_BUNDLE: |
| 1600 | outs() << " BUNDLE"; |
| 1601 | break; |
| 1602 | case MachO::MH_DSYM: |
| 1603 | outs() << " DSYM"; |
| 1604 | break; |
| 1605 | case MachO::MH_KEXT_BUNDLE: |
| 1606 | outs() << " KEXTBUNDLE"; |
| 1607 | break; |
| 1608 | default: |
| 1609 | outs() << format(" %10u", filetype); |
| 1610 | break; |
| 1611 | } |
| 1612 | outs() << format(" %5u", ncmds); |
| 1613 | outs() << format(" %10u", sizeofcmds); |
| 1614 | uint32_t f = flags; |
| 1615 | if (f & MachO::MH_NOUNDEFS) { |
| 1616 | outs() << " NOUNDEFS"; |
| 1617 | f &= ~MachO::MH_NOUNDEFS; |
| 1618 | } |
| 1619 | if (f & MachO::MH_INCRLINK) { |
| 1620 | outs() << " INCRLINK"; |
| 1621 | f &= ~MachO::MH_INCRLINK; |
| 1622 | } |
| 1623 | if (f & MachO::MH_DYLDLINK) { |
| 1624 | outs() << " DYLDLINK"; |
| 1625 | f &= ~MachO::MH_DYLDLINK; |
| 1626 | } |
| 1627 | if (f & MachO::MH_BINDATLOAD) { |
| 1628 | outs() << " BINDATLOAD"; |
| 1629 | f &= ~MachO::MH_BINDATLOAD; |
| 1630 | } |
| 1631 | if (f & MachO::MH_PREBOUND) { |
| 1632 | outs() << " PREBOUND"; |
| 1633 | f &= ~MachO::MH_PREBOUND; |
| 1634 | } |
| 1635 | if (f & MachO::MH_SPLIT_SEGS) { |
| 1636 | outs() << " SPLIT_SEGS"; |
| 1637 | f &= ~MachO::MH_SPLIT_SEGS; |
| 1638 | } |
| 1639 | if (f & MachO::MH_LAZY_INIT) { |
| 1640 | outs() << " LAZY_INIT"; |
| 1641 | f &= ~MachO::MH_LAZY_INIT; |
| 1642 | } |
| 1643 | if (f & MachO::MH_TWOLEVEL) { |
| 1644 | outs() << " TWOLEVEL"; |
| 1645 | f &= ~MachO::MH_TWOLEVEL; |
| 1646 | } |
| 1647 | if (f & MachO::MH_FORCE_FLAT) { |
| 1648 | outs() << " FORCE_FLAT"; |
| 1649 | f &= ~MachO::MH_FORCE_FLAT; |
| 1650 | } |
| 1651 | if (f & MachO::MH_NOMULTIDEFS) { |
| 1652 | outs() << " NOMULTIDEFS"; |
| 1653 | f &= ~MachO::MH_NOMULTIDEFS; |
| 1654 | } |
| 1655 | if (f & MachO::MH_NOFIXPREBINDING) { |
| 1656 | outs() << " NOFIXPREBINDING"; |
| 1657 | f &= ~MachO::MH_NOFIXPREBINDING; |
| 1658 | } |
| 1659 | if (f & MachO::MH_PREBINDABLE) { |
| 1660 | outs() << " PREBINDABLE"; |
| 1661 | f &= ~MachO::MH_PREBINDABLE; |
| 1662 | } |
| 1663 | if (f & MachO::MH_ALLMODSBOUND) { |
| 1664 | outs() << " ALLMODSBOUND"; |
| 1665 | f &= ~MachO::MH_ALLMODSBOUND; |
| 1666 | } |
| 1667 | if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) { |
| 1668 | outs() << " SUBSECTIONS_VIA_SYMBOLS"; |
| 1669 | f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS; |
| 1670 | } |
| 1671 | if (f & MachO::MH_CANONICAL) { |
| 1672 | outs() << " CANONICAL"; |
| 1673 | f &= ~MachO::MH_CANONICAL; |
| 1674 | } |
| 1675 | if (f & MachO::MH_WEAK_DEFINES) { |
| 1676 | outs() << " WEAK_DEFINES"; |
| 1677 | f &= ~MachO::MH_WEAK_DEFINES; |
| 1678 | } |
| 1679 | if (f & MachO::MH_BINDS_TO_WEAK) { |
| 1680 | outs() << " BINDS_TO_WEAK"; |
| 1681 | f &= ~MachO::MH_BINDS_TO_WEAK; |
| 1682 | } |
| 1683 | if (f & MachO::MH_ALLOW_STACK_EXECUTION) { |
| 1684 | outs() << " ALLOW_STACK_EXECUTION"; |
| 1685 | f &= ~MachO::MH_ALLOW_STACK_EXECUTION; |
| 1686 | } |
| 1687 | if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) { |
| 1688 | outs() << " DEAD_STRIPPABLE_DYLIB"; |
| 1689 | f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB; |
| 1690 | } |
| 1691 | if (f & MachO::MH_PIE) { |
| 1692 | outs() << " PIE"; |
| 1693 | f &= ~MachO::MH_PIE; |
| 1694 | } |
| 1695 | if (f & MachO::MH_NO_REEXPORTED_DYLIBS) { |
| 1696 | outs() << " NO_REEXPORTED_DYLIBS"; |
| 1697 | f &= ~MachO::MH_NO_REEXPORTED_DYLIBS; |
| 1698 | } |
| 1699 | if (f & MachO::MH_HAS_TLV_DESCRIPTORS) { |
| 1700 | outs() << " MH_HAS_TLV_DESCRIPTORS"; |
| 1701 | f &= ~MachO::MH_HAS_TLV_DESCRIPTORS; |
| 1702 | } |
| 1703 | if (f & MachO::MH_NO_HEAP_EXECUTION) { |
| 1704 | outs() << " MH_NO_HEAP_EXECUTION"; |
| 1705 | f &= ~MachO::MH_NO_HEAP_EXECUTION; |
| 1706 | } |
| 1707 | if (f & MachO::MH_APP_EXTENSION_SAFE) { |
| 1708 | outs() << " APP_EXTENSION_SAFE"; |
| 1709 | f &= ~MachO::MH_APP_EXTENSION_SAFE; |
| 1710 | } |
| 1711 | if (f != 0 || flags == 0) |
| 1712 | outs() << format(" 0x%08" PRIx32, f); |
| 1713 | } else { |
| 1714 | outs() << format(" 0x%08" PRIx32, magic); |
| 1715 | outs() << format(" %7d", cputype); |
| 1716 | outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); |
| 1717 | outs() << format(" 0x%02" PRIx32, |
| 1718 | (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24); |
| 1719 | outs() << format(" %10u", filetype); |
| 1720 | outs() << format(" %5u", ncmds); |
| 1721 | outs() << format(" %10u", sizeofcmds); |
| 1722 | outs() << format(" 0x%08" PRIx32, flags); |
| 1723 | } |
| 1724 | outs() << "\n"; |
| 1725 | } |
| 1726 | |
Kevin Enderby | 956366c | 2014-08-29 22:30:52 +0000 | [diff] [blame] | 1727 | static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize, |
| 1728 | StringRef SegName, uint64_t vmaddr, |
| 1729 | uint64_t vmsize, uint64_t fileoff, |
| 1730 | uint64_t filesize, uint32_t maxprot, |
| 1731 | uint32_t initprot, uint32_t nsects, |
| 1732 | uint32_t flags, uint32_t object_size, |
| 1733 | bool verbose) { |
| 1734 | uint64_t expected_cmdsize; |
| 1735 | if (cmd == MachO::LC_SEGMENT) { |
| 1736 | outs() << " cmd LC_SEGMENT\n"; |
| 1737 | expected_cmdsize = nsects; |
| 1738 | expected_cmdsize *= sizeof(struct MachO::section); |
| 1739 | expected_cmdsize += sizeof(struct MachO::segment_command); |
| 1740 | } else { |
| 1741 | outs() << " cmd LC_SEGMENT_64\n"; |
| 1742 | expected_cmdsize = nsects; |
| 1743 | expected_cmdsize *= sizeof(struct MachO::section_64); |
| 1744 | expected_cmdsize += sizeof(struct MachO::segment_command_64); |
| 1745 | } |
| 1746 | outs() << " cmdsize " << cmdsize; |
| 1747 | if (cmdsize != expected_cmdsize) |
| 1748 | outs() << " Inconsistent size\n"; |
| 1749 | else |
| 1750 | outs() << "\n"; |
| 1751 | outs() << " segname " << SegName << "\n"; |
| 1752 | if (cmd == MachO::LC_SEGMENT_64) { |
| 1753 | outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n"; |
| 1754 | outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n"; |
| 1755 | } else { |
| 1756 | outs() << " vmaddr " << format("0x%08" PRIx32, vmaddr) << "\n"; |
| 1757 | outs() << " vmsize " << format("0x%08" PRIx32, vmsize) << "\n"; |
| 1758 | } |
| 1759 | outs() << " fileoff " << fileoff; |
| 1760 | if (fileoff > object_size) |
| 1761 | outs() << " (past end of file)\n"; |
| 1762 | else |
| 1763 | outs() << "\n"; |
| 1764 | outs() << " filesize " << filesize; |
| 1765 | if (fileoff + filesize > object_size) |
| 1766 | outs() << " (past end of file)\n"; |
| 1767 | else |
| 1768 | outs() << "\n"; |
| 1769 | if (verbose) { |
| 1770 | if ((maxprot & |
| 1771 | ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | |
| 1772 | MachO::VM_PROT_EXECUTE)) != 0) |
| 1773 | outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n"; |
| 1774 | else { |
| 1775 | if (maxprot & MachO::VM_PROT_READ) |
| 1776 | outs() << " maxprot r"; |
| 1777 | else |
| 1778 | outs() << " maxprot -"; |
| 1779 | if (maxprot & MachO::VM_PROT_WRITE) |
| 1780 | outs() << "w"; |
| 1781 | else |
| 1782 | outs() << "-"; |
| 1783 | if (maxprot & MachO::VM_PROT_EXECUTE) |
| 1784 | outs() << "x\n"; |
| 1785 | else |
| 1786 | outs() << "-\n"; |
| 1787 | } |
| 1788 | if ((initprot & |
| 1789 | ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | |
| 1790 | MachO::VM_PROT_EXECUTE)) != 0) |
| 1791 | outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n"; |
| 1792 | else { |
| 1793 | if (initprot & MachO::VM_PROT_READ) |
| 1794 | outs() << " initprot r"; |
| 1795 | else |
| 1796 | outs() << " initprot -"; |
| 1797 | if (initprot & MachO::VM_PROT_WRITE) |
| 1798 | outs() << "w"; |
| 1799 | else |
| 1800 | outs() << "-"; |
| 1801 | if (initprot & MachO::VM_PROT_EXECUTE) |
| 1802 | outs() << "x\n"; |
| 1803 | else |
| 1804 | outs() << "-\n"; |
| 1805 | } |
| 1806 | } else { |
| 1807 | outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n"; |
| 1808 | outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n"; |
| 1809 | } |
| 1810 | outs() << " nsects " << nsects << "\n"; |
| 1811 | if (verbose) { |
| 1812 | outs() << " flags"; |
| 1813 | if (flags == 0) |
| 1814 | outs() << " (none)\n"; |
| 1815 | else { |
| 1816 | if (flags & MachO::SG_HIGHVM) { |
| 1817 | outs() << " HIGHVM"; |
| 1818 | flags &= ~MachO::SG_HIGHVM; |
| 1819 | } |
| 1820 | if (flags & MachO::SG_FVMLIB) { |
| 1821 | outs() << " FVMLIB"; |
| 1822 | flags &= ~MachO::SG_FVMLIB; |
| 1823 | } |
| 1824 | if (flags & MachO::SG_NORELOC) { |
| 1825 | outs() << " NORELOC"; |
| 1826 | flags &= ~MachO::SG_NORELOC; |
| 1827 | } |
| 1828 | if (flags & MachO::SG_PROTECTED_VERSION_1) { |
| 1829 | outs() << " PROTECTED_VERSION_1"; |
| 1830 | flags &= ~MachO::SG_PROTECTED_VERSION_1; |
| 1831 | } |
| 1832 | if (flags) |
| 1833 | outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n"; |
| 1834 | else |
| 1835 | outs() << "\n"; |
| 1836 | } |
| 1837 | } else { |
| 1838 | outs() << " flags " << format("0x%" PRIx32, flags) << "\n"; |
| 1839 | } |
| 1840 | } |
| 1841 | |
| 1842 | static void PrintSection(const char *sectname, const char *segname, |
| 1843 | uint64_t addr, uint64_t size, uint32_t offset, |
| 1844 | uint32_t align, uint32_t reloff, uint32_t nreloc, |
| 1845 | uint32_t flags, uint32_t reserved1, uint32_t reserved2, |
| 1846 | uint32_t cmd, const char *sg_segname, |
| 1847 | uint32_t filetype, uint32_t object_size, |
| 1848 | bool verbose) { |
| 1849 | outs() << "Section\n"; |
| 1850 | outs() << " sectname " << format("%.16s\n", sectname); |
| 1851 | outs() << " segname " << format("%.16s", segname); |
| 1852 | if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0) |
| 1853 | outs() << " (does not match segment)\n"; |
| 1854 | else |
| 1855 | outs() << "\n"; |
| 1856 | if (cmd == MachO::LC_SEGMENT_64) { |
| 1857 | outs() << " addr " << format("0x%016" PRIx64, addr) << "\n"; |
| 1858 | outs() << " size " << format("0x%016" PRIx64, size); |
| 1859 | } else { |
| 1860 | outs() << " addr " << format("0x%08" PRIx32, addr) << "\n"; |
| 1861 | outs() << " size " << format("0x%08" PRIx32, size); |
| 1862 | } |
| 1863 | if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size) |
| 1864 | outs() << " (past end of file)\n"; |
| 1865 | else |
| 1866 | outs() << "\n"; |
| 1867 | outs() << " offset " << offset; |
| 1868 | if (offset > object_size) |
| 1869 | outs() << " (past end of file)\n"; |
| 1870 | else |
| 1871 | outs() << "\n"; |
| 1872 | uint32_t align_shifted = 1 << align; |
| 1873 | outs() << " align 2^" << align << " (" << align_shifted << ")\n"; |
| 1874 | outs() << " reloff " << reloff; |
| 1875 | if (reloff > object_size) |
| 1876 | outs() << " (past end of file)\n"; |
| 1877 | else |
| 1878 | outs() << "\n"; |
| 1879 | outs() << " nreloc " << nreloc; |
| 1880 | if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size) |
| 1881 | outs() << " (past end of file)\n"; |
| 1882 | else |
| 1883 | outs() << "\n"; |
| 1884 | uint32_t section_type = flags & MachO::SECTION_TYPE; |
| 1885 | if (verbose) { |
| 1886 | outs() << " type"; |
| 1887 | if (section_type == MachO::S_REGULAR) |
| 1888 | outs() << " S_REGULAR\n"; |
| 1889 | else if (section_type == MachO::S_ZEROFILL) |
| 1890 | outs() << " S_ZEROFILL\n"; |
| 1891 | else if (section_type == MachO::S_CSTRING_LITERALS) |
| 1892 | outs() << " S_CSTRING_LITERALS\n"; |
| 1893 | else if (section_type == MachO::S_4BYTE_LITERALS) |
| 1894 | outs() << " S_4BYTE_LITERALS\n"; |
| 1895 | else if (section_type == MachO::S_8BYTE_LITERALS) |
| 1896 | outs() << " S_8BYTE_LITERALS\n"; |
| 1897 | else if (section_type == MachO::S_16BYTE_LITERALS) |
| 1898 | outs() << " S_16BYTE_LITERALS\n"; |
| 1899 | else if (section_type == MachO::S_LITERAL_POINTERS) |
| 1900 | outs() << " S_LITERAL_POINTERS\n"; |
| 1901 | else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS) |
| 1902 | outs() << " S_NON_LAZY_SYMBOL_POINTERS\n"; |
| 1903 | else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS) |
| 1904 | outs() << " S_LAZY_SYMBOL_POINTERS\n"; |
| 1905 | else if (section_type == MachO::S_SYMBOL_STUBS) |
| 1906 | outs() << " S_SYMBOL_STUBS\n"; |
| 1907 | else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS) |
| 1908 | outs() << " S_MOD_INIT_FUNC_POINTERS\n"; |
| 1909 | else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS) |
| 1910 | outs() << " S_MOD_TERM_FUNC_POINTERS\n"; |
| 1911 | else if (section_type == MachO::S_COALESCED) |
| 1912 | outs() << " S_COALESCED\n"; |
| 1913 | else if (section_type == MachO::S_INTERPOSING) |
| 1914 | outs() << " S_INTERPOSING\n"; |
| 1915 | else if (section_type == MachO::S_DTRACE_DOF) |
| 1916 | outs() << " S_DTRACE_DOF\n"; |
| 1917 | else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS) |
| 1918 | outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n"; |
| 1919 | else if (section_type == MachO::S_THREAD_LOCAL_REGULAR) |
| 1920 | outs() << " S_THREAD_LOCAL_REGULAR\n"; |
| 1921 | else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL) |
| 1922 | outs() << " S_THREAD_LOCAL_ZEROFILL\n"; |
| 1923 | else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES) |
| 1924 | outs() << " S_THREAD_LOCAL_VARIABLES\n"; |
| 1925 | else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS) |
| 1926 | outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n"; |
| 1927 | else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS) |
| 1928 | outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n"; |
| 1929 | else |
| 1930 | outs() << format("0x%08" PRIx32, section_type) << "\n"; |
| 1931 | outs() << "attributes"; |
| 1932 | uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES; |
| 1933 | if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS) |
| 1934 | outs() << " PURE_INSTRUCTIONS"; |
| 1935 | if (section_attributes & MachO::S_ATTR_NO_TOC) |
| 1936 | outs() << " NO_TOC"; |
| 1937 | if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS) |
| 1938 | outs() << " STRIP_STATIC_SYMS"; |
| 1939 | if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP) |
| 1940 | outs() << " NO_DEAD_STRIP"; |
| 1941 | if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT) |
| 1942 | outs() << " LIVE_SUPPORT"; |
| 1943 | if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE) |
| 1944 | outs() << " SELF_MODIFYING_CODE"; |
| 1945 | if (section_attributes & MachO::S_ATTR_DEBUG) |
| 1946 | outs() << " DEBUG"; |
| 1947 | if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS) |
| 1948 | outs() << " SOME_INSTRUCTIONS"; |
| 1949 | if (section_attributes & MachO::S_ATTR_EXT_RELOC) |
| 1950 | outs() << " EXT_RELOC"; |
| 1951 | if (section_attributes & MachO::S_ATTR_LOC_RELOC) |
| 1952 | outs() << " LOC_RELOC"; |
| 1953 | if (section_attributes == 0) |
| 1954 | outs() << " (none)"; |
| 1955 | outs() << "\n"; |
| 1956 | } else |
| 1957 | outs() << " flags " << format("0x%08" PRIx32, flags) << "\n"; |
| 1958 | outs() << " reserved1 " << reserved1; |
| 1959 | if (section_type == MachO::S_SYMBOL_STUBS || |
| 1960 | section_type == MachO::S_LAZY_SYMBOL_POINTERS || |
| 1961 | section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS || |
| 1962 | section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS || |
| 1963 | section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS) |
| 1964 | outs() << " (index into indirect symbol table)\n"; |
| 1965 | else |
| 1966 | outs() << "\n"; |
| 1967 | outs() << " reserved2 " << reserved2; |
| 1968 | if (section_type == MachO::S_SYMBOL_STUBS) |
| 1969 | outs() << " (size of stubs)\n"; |
| 1970 | else |
| 1971 | outs() << "\n"; |
| 1972 | } |
| 1973 | |
| 1974 | static void PrintSymtabLoadCommand(MachO::symtab_command st, uint32_t cputype, |
| 1975 | uint32_t object_size) { |
| 1976 | outs() << " cmd LC_SYMTAB\n"; |
| 1977 | outs() << " cmdsize " << st.cmdsize; |
| 1978 | if (st.cmdsize != sizeof(struct MachO::symtab_command)) |
| 1979 | outs() << " Incorrect size\n"; |
| 1980 | else |
| 1981 | outs() << "\n"; |
| 1982 | outs() << " symoff " << st.symoff; |
| 1983 | if (st.symoff > object_size) |
| 1984 | outs() << " (past end of file)\n"; |
| 1985 | else |
| 1986 | outs() << "\n"; |
| 1987 | outs() << " nsyms " << st.nsyms; |
| 1988 | uint64_t big_size; |
| 1989 | if (cputype & MachO::CPU_ARCH_ABI64) { |
| 1990 | big_size = st.nsyms; |
| 1991 | big_size *= sizeof(struct MachO::nlist_64); |
| 1992 | big_size += st.symoff; |
| 1993 | if (big_size > object_size) |
| 1994 | outs() << " (past end of file)\n"; |
| 1995 | else |
| 1996 | outs() << "\n"; |
| 1997 | } else { |
| 1998 | big_size = st.nsyms; |
| 1999 | big_size *= sizeof(struct MachO::nlist); |
| 2000 | big_size += st.symoff; |
| 2001 | if (big_size > object_size) |
| 2002 | outs() << " (past end of file)\n"; |
| 2003 | else |
| 2004 | outs() << "\n"; |
| 2005 | } |
| 2006 | outs() << " stroff " << st.stroff; |
| 2007 | if (st.stroff > object_size) |
| 2008 | outs() << " (past end of file)\n"; |
| 2009 | else |
| 2010 | outs() << "\n"; |
| 2011 | outs() << " strsize " << st.strsize; |
| 2012 | big_size = st.stroff; |
| 2013 | big_size += st.strsize; |
| 2014 | if (big_size > object_size) |
| 2015 | outs() << " (past end of file)\n"; |
| 2016 | else |
| 2017 | outs() << "\n"; |
| 2018 | } |
| 2019 | |
| 2020 | static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst, |
| 2021 | uint32_t nsyms, uint32_t object_size, |
| 2022 | uint32_t cputype) { |
| 2023 | outs() << " cmd LC_DYSYMTAB\n"; |
| 2024 | outs() << " cmdsize " << dyst.cmdsize; |
| 2025 | if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command)) |
| 2026 | outs() << " Incorrect size\n"; |
| 2027 | else |
| 2028 | outs() << "\n"; |
| 2029 | outs() << " ilocalsym " << dyst.ilocalsym; |
| 2030 | if (dyst.ilocalsym > nsyms) |
| 2031 | outs() << " (greater than the number of symbols)\n"; |
| 2032 | else |
| 2033 | outs() << "\n"; |
| 2034 | outs() << " nlocalsym " << dyst.nlocalsym; |
| 2035 | uint64_t big_size; |
| 2036 | big_size = dyst.ilocalsym; |
| 2037 | big_size += dyst.nlocalsym; |
| 2038 | if (big_size > nsyms) |
| 2039 | outs() << " (past the end of the symbol table)\n"; |
| 2040 | else |
| 2041 | outs() << "\n"; |
| 2042 | outs() << " iextdefsym " << dyst.iextdefsym; |
| 2043 | if (dyst.iextdefsym > nsyms) |
| 2044 | outs() << " (greater than the number of symbols)\n"; |
| 2045 | else |
| 2046 | outs() << "\n"; |
| 2047 | outs() << " nextdefsym " << dyst.nextdefsym; |
| 2048 | big_size = dyst.iextdefsym; |
| 2049 | big_size += dyst.nextdefsym; |
| 2050 | if (big_size > nsyms) |
| 2051 | outs() << " (past the end of the symbol table)\n"; |
| 2052 | else |
| 2053 | outs() << "\n"; |
| 2054 | outs() << " iundefsym " << dyst.iundefsym; |
| 2055 | if (dyst.iundefsym > nsyms) |
| 2056 | outs() << " (greater than the number of symbols)\n"; |
| 2057 | else |
| 2058 | outs() << "\n"; |
| 2059 | outs() << " nundefsym " << dyst.nundefsym; |
| 2060 | big_size = dyst.iundefsym; |
| 2061 | big_size += dyst.nundefsym; |
| 2062 | if (big_size > nsyms) |
| 2063 | outs() << " (past the end of the symbol table)\n"; |
| 2064 | else |
| 2065 | outs() << "\n"; |
| 2066 | outs() << " tocoff " << dyst.tocoff; |
| 2067 | if (dyst.tocoff > object_size) |
| 2068 | outs() << " (past end of file)\n"; |
| 2069 | else |
| 2070 | outs() << "\n"; |
| 2071 | outs() << " ntoc " << dyst.ntoc; |
| 2072 | big_size = dyst.ntoc; |
| 2073 | big_size *= sizeof(struct MachO::dylib_table_of_contents); |
| 2074 | big_size += dyst.tocoff; |
| 2075 | if (big_size > object_size) |
| 2076 | outs() << " (past end of file)\n"; |
| 2077 | else |
| 2078 | outs() << "\n"; |
| 2079 | outs() << " modtaboff " << dyst.modtaboff; |
| 2080 | if (dyst.modtaboff > object_size) |
| 2081 | outs() << " (past end of file)\n"; |
| 2082 | else |
| 2083 | outs() << "\n"; |
| 2084 | outs() << " nmodtab " << dyst.nmodtab; |
| 2085 | uint64_t modtabend; |
| 2086 | if (cputype & MachO::CPU_ARCH_ABI64) { |
| 2087 | modtabend = dyst.nmodtab; |
| 2088 | modtabend *= sizeof(struct MachO::dylib_module_64); |
| 2089 | modtabend += dyst.modtaboff; |
| 2090 | } else { |
| 2091 | modtabend = dyst.nmodtab; |
| 2092 | modtabend *= sizeof(struct MachO::dylib_module); |
| 2093 | modtabend += dyst.modtaboff; |
| 2094 | } |
| 2095 | if (modtabend > object_size) |
| 2096 | outs() << " (past end of file)\n"; |
| 2097 | else |
| 2098 | outs() << "\n"; |
| 2099 | outs() << " extrefsymoff " << dyst.extrefsymoff; |
| 2100 | if (dyst.extrefsymoff > object_size) |
| 2101 | outs() << " (past end of file)\n"; |
| 2102 | else |
| 2103 | outs() << "\n"; |
| 2104 | outs() << " nextrefsyms " << dyst.nextrefsyms; |
| 2105 | big_size = dyst.nextrefsyms; |
| 2106 | big_size *= sizeof(struct MachO::dylib_reference); |
| 2107 | big_size += dyst.extrefsymoff; |
| 2108 | if (big_size > object_size) |
| 2109 | outs() << " (past end of file)\n"; |
| 2110 | else |
| 2111 | outs() << "\n"; |
| 2112 | outs() << " indirectsymoff " << dyst.indirectsymoff; |
| 2113 | if (dyst.indirectsymoff > object_size) |
| 2114 | outs() << " (past end of file)\n"; |
| 2115 | else |
| 2116 | outs() << "\n"; |
| 2117 | outs() << " nindirectsyms " << dyst.nindirectsyms; |
| 2118 | big_size = dyst.nindirectsyms; |
| 2119 | big_size *= sizeof(uint32_t); |
| 2120 | big_size += dyst.indirectsymoff; |
| 2121 | if (big_size > object_size) |
| 2122 | outs() << " (past end of file)\n"; |
| 2123 | else |
| 2124 | outs() << "\n"; |
| 2125 | outs() << " extreloff " << dyst.extreloff; |
| 2126 | if (dyst.extreloff > object_size) |
| 2127 | outs() << " (past end of file)\n"; |
| 2128 | else |
| 2129 | outs() << "\n"; |
| 2130 | outs() << " nextrel " << dyst.nextrel; |
| 2131 | big_size = dyst.nextrel; |
| 2132 | big_size *= sizeof(struct MachO::relocation_info); |
| 2133 | big_size += dyst.extreloff; |
| 2134 | if (big_size > object_size) |
| 2135 | outs() << " (past end of file)\n"; |
| 2136 | else |
| 2137 | outs() << "\n"; |
| 2138 | outs() << " locreloff " << dyst.locreloff; |
| 2139 | if (dyst.locreloff > object_size) |
| 2140 | outs() << " (past end of file)\n"; |
| 2141 | else |
| 2142 | outs() << "\n"; |
| 2143 | outs() << " nlocrel " << dyst.nlocrel; |
| 2144 | big_size = dyst.nlocrel; |
| 2145 | big_size *= sizeof(struct MachO::relocation_info); |
| 2146 | big_size += dyst.locreloff; |
| 2147 | if (big_size > object_size) |
| 2148 | outs() << " (past end of file)\n"; |
| 2149 | else |
| 2150 | outs() << "\n"; |
| 2151 | } |
| 2152 | |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 2153 | static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc, |
| 2154 | uint32_t object_size) { |
| 2155 | if (dc.cmd == MachO::LC_DYLD_INFO) |
| 2156 | outs() << " cmd LC_DYLD_INFO\n"; |
| 2157 | else |
| 2158 | outs() << " cmd LC_DYLD_INFO_ONLY\n"; |
| 2159 | outs() << " cmdsize " << dc.cmdsize; |
| 2160 | if (dc.cmdsize != sizeof(struct MachO::dyld_info_command)) |
| 2161 | outs() << " Incorrect size\n"; |
| 2162 | else |
| 2163 | outs() << "\n"; |
| 2164 | outs() << " rebase_off " << dc.rebase_off; |
| 2165 | if (dc.rebase_off > object_size) |
| 2166 | outs() << " (past end of file)\n"; |
| 2167 | else |
| 2168 | outs() << "\n"; |
| 2169 | outs() << " rebase_size " << dc.rebase_size; |
| 2170 | uint64_t big_size; |
| 2171 | big_size = dc.rebase_off; |
| 2172 | big_size += dc.rebase_size; |
| 2173 | if (big_size > object_size) |
| 2174 | outs() << " (past end of file)\n"; |
| 2175 | else |
| 2176 | outs() << "\n"; |
| 2177 | outs() << " bind_off " << dc.bind_off; |
| 2178 | if (dc.bind_off > object_size) |
| 2179 | outs() << " (past end of file)\n"; |
| 2180 | else |
| 2181 | outs() << "\n"; |
| 2182 | outs() << " bind_size " << dc.bind_size; |
| 2183 | big_size = dc.bind_off; |
| 2184 | big_size += dc.bind_size; |
| 2185 | if (big_size > object_size) |
| 2186 | outs() << " (past end of file)\n"; |
| 2187 | else |
| 2188 | outs() << "\n"; |
| 2189 | outs() << " weak_bind_off " << dc.weak_bind_off; |
| 2190 | if (dc.weak_bind_off > object_size) |
| 2191 | outs() << " (past end of file)\n"; |
| 2192 | else |
| 2193 | outs() << "\n"; |
| 2194 | outs() << " weak_bind_size " << dc.weak_bind_size; |
| 2195 | big_size = dc.weak_bind_off; |
| 2196 | big_size += dc.weak_bind_size; |
| 2197 | if (big_size > object_size) |
| 2198 | outs() << " (past end of file)\n"; |
| 2199 | else |
| 2200 | outs() << "\n"; |
| 2201 | outs() << " lazy_bind_off " << dc.lazy_bind_off; |
| 2202 | if (dc.lazy_bind_off > object_size) |
| 2203 | outs() << " (past end of file)\n"; |
| 2204 | else |
| 2205 | outs() << "\n"; |
| 2206 | outs() << " lazy_bind_size " << dc.lazy_bind_size; |
| 2207 | big_size = dc.lazy_bind_off; |
| 2208 | big_size += dc.lazy_bind_size; |
| 2209 | if (big_size > object_size) |
| 2210 | outs() << " (past end of file)\n"; |
| 2211 | else |
| 2212 | outs() << "\n"; |
| 2213 | outs() << " export_off " << dc.export_off; |
| 2214 | if (dc.export_off > object_size) |
| 2215 | outs() << " (past end of file)\n"; |
| 2216 | else |
| 2217 | outs() << "\n"; |
| 2218 | outs() << " export_size " << dc.export_size; |
| 2219 | big_size = dc.export_off; |
| 2220 | big_size += dc.export_size; |
| 2221 | if (big_size > object_size) |
| 2222 | outs() << " (past end of file)\n"; |
| 2223 | else |
| 2224 | outs() << "\n"; |
| 2225 | } |
| 2226 | |
| 2227 | static void PrintDyldLoadCommand(MachO::dylinker_command dyld, |
| 2228 | const char *Ptr) { |
| 2229 | if (dyld.cmd == MachO::LC_ID_DYLINKER) |
| 2230 | outs() << " cmd LC_ID_DYLINKER\n"; |
| 2231 | else if (dyld.cmd == MachO::LC_LOAD_DYLINKER) |
| 2232 | outs() << " cmd LC_LOAD_DYLINKER\n"; |
| 2233 | else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT) |
| 2234 | outs() << " cmd LC_DYLD_ENVIRONMENT\n"; |
| 2235 | else |
| 2236 | outs() << " cmd ?(" << dyld.cmd << ")\n"; |
| 2237 | outs() << " cmdsize " << dyld.cmdsize; |
| 2238 | if (dyld.cmdsize < sizeof(struct MachO::dylinker_command)) |
| 2239 | outs() << " Incorrect size\n"; |
| 2240 | else |
| 2241 | outs() << "\n"; |
| 2242 | if (dyld.name >= dyld.cmdsize) |
| 2243 | outs() << " name ?(bad offset " << dyld.name << ")\n"; |
| 2244 | else { |
| 2245 | const char *P = (const char *)(Ptr)+dyld.name; |
| 2246 | outs() << " name " << P << " (offset " << dyld.name << ")\n"; |
| 2247 | } |
| 2248 | } |
| 2249 | |
| 2250 | static void PrintUuidLoadCommand(MachO::uuid_command uuid) { |
| 2251 | outs() << " cmd LC_UUID\n"; |
| 2252 | outs() << " cmdsize " << uuid.cmdsize; |
| 2253 | if (uuid.cmdsize != sizeof(struct MachO::uuid_command)) |
| 2254 | outs() << " Incorrect size\n"; |
| 2255 | else |
| 2256 | outs() << "\n"; |
| 2257 | outs() << " uuid "; |
| 2258 | outs() << format("%02" PRIX32, uuid.uuid[0]); |
| 2259 | outs() << format("%02" PRIX32, uuid.uuid[1]); |
| 2260 | outs() << format("%02" PRIX32, uuid.uuid[2]); |
| 2261 | outs() << format("%02" PRIX32, uuid.uuid[3]); |
| 2262 | outs() << "-"; |
| 2263 | outs() << format("%02" PRIX32, uuid.uuid[4]); |
| 2264 | outs() << format("%02" PRIX32, uuid.uuid[5]); |
| 2265 | outs() << "-"; |
| 2266 | outs() << format("%02" PRIX32, uuid.uuid[6]); |
| 2267 | outs() << format("%02" PRIX32, uuid.uuid[7]); |
| 2268 | outs() << "-"; |
| 2269 | outs() << format("%02" PRIX32, uuid.uuid[8]); |
| 2270 | outs() << format("%02" PRIX32, uuid.uuid[9]); |
| 2271 | outs() << "-"; |
| 2272 | outs() << format("%02" PRIX32, uuid.uuid[10]); |
| 2273 | outs() << format("%02" PRIX32, uuid.uuid[11]); |
| 2274 | outs() << format("%02" PRIX32, uuid.uuid[12]); |
| 2275 | outs() << format("%02" PRIX32, uuid.uuid[13]); |
| 2276 | outs() << format("%02" PRIX32, uuid.uuid[14]); |
| 2277 | outs() << format("%02" PRIX32, uuid.uuid[15]); |
| 2278 | outs() << "\n"; |
| 2279 | } |
| 2280 | |
| 2281 | static void PrintVersionMinLoadCommand(MachO::version_min_command vd) { |
| 2282 | if (vd.cmd == MachO::LC_VERSION_MIN_MACOSX) |
| 2283 | outs() << " cmd LC_VERSION_MIN_MACOSX\n"; |
| 2284 | else if (vd.cmd == MachO::LC_VERSION_MIN_IPHONEOS) |
| 2285 | outs() << " cmd LC_VERSION_MIN_IPHONEOS\n"; |
| 2286 | else |
| 2287 | outs() << " cmd " << vd.cmd << " (?)\n"; |
| 2288 | outs() << " cmdsize " << vd.cmdsize; |
| 2289 | if (vd.cmdsize != sizeof(struct MachO::version_min_command)) |
| 2290 | outs() << " Incorrect size\n"; |
| 2291 | else |
| 2292 | outs() << "\n"; |
| 2293 | outs() << " version " << ((vd.version >> 16) & 0xffff) << "." |
| 2294 | << ((vd.version >> 8) & 0xff); |
| 2295 | if ((vd.version & 0xff) != 0) |
| 2296 | outs() << "." << (vd.version & 0xff); |
| 2297 | outs() << "\n"; |
| 2298 | if (vd.sdk == 0) |
| 2299 | outs() << " sdk n/a\n"; |
| 2300 | else { |
| 2301 | outs() << " sdk " << ((vd.sdk >> 16) & 0xffff) << "." |
| 2302 | << ((vd.sdk >> 8) & 0xff); |
| 2303 | } |
| 2304 | if ((vd.sdk & 0xff) != 0) |
| 2305 | outs() << "." << (vd.sdk & 0xff); |
| 2306 | outs() << "\n"; |
| 2307 | } |
| 2308 | |
| 2309 | static void PrintSourceVersionCommand(MachO::source_version_command sd) { |
| 2310 | outs() << " cmd LC_SOURCE_VERSION\n"; |
| 2311 | outs() << " cmdsize " << sd.cmdsize; |
| 2312 | if (sd.cmdsize != sizeof(struct MachO::source_version_command)) |
| 2313 | outs() << " Incorrect size\n"; |
| 2314 | else |
| 2315 | outs() << "\n"; |
| 2316 | uint64_t a = (sd.version >> 40) & 0xffffff; |
| 2317 | uint64_t b = (sd.version >> 30) & 0x3ff; |
| 2318 | uint64_t c = (sd.version >> 20) & 0x3ff; |
| 2319 | uint64_t d = (sd.version >> 10) & 0x3ff; |
| 2320 | uint64_t e = sd.version & 0x3ff; |
| 2321 | outs() << " version " << a << "." << b; |
| 2322 | if (e != 0) |
| 2323 | outs() << "." << c << "." << d << "." << e; |
| 2324 | else if (d != 0) |
| 2325 | outs() << "." << c << "." << d; |
| 2326 | else if (c != 0) |
| 2327 | outs() << "." << c; |
| 2328 | outs() << "\n"; |
| 2329 | } |
| 2330 | |
| 2331 | static void PrintEntryPointCommand(MachO::entry_point_command ep) { |
| 2332 | outs() << " cmd LC_MAIN\n"; |
| 2333 | outs() << " cmdsize " << ep.cmdsize; |
| 2334 | if (ep.cmdsize != sizeof(struct MachO::entry_point_command)) |
| 2335 | outs() << " Incorrect size\n"; |
| 2336 | else |
| 2337 | outs() << "\n"; |
| 2338 | outs() << " entryoff " << ep.entryoff << "\n"; |
| 2339 | outs() << " stacksize " << ep.stacksize << "\n"; |
| 2340 | } |
| 2341 | |
| 2342 | static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) { |
| 2343 | if (dl.cmd == MachO::LC_ID_DYLIB) |
| 2344 | outs() << " cmd LC_ID_DYLIB\n"; |
| 2345 | else if (dl.cmd == MachO::LC_LOAD_DYLIB) |
| 2346 | outs() << " cmd LC_LOAD_DYLIB\n"; |
| 2347 | else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB) |
| 2348 | outs() << " cmd LC_LOAD_WEAK_DYLIB\n"; |
| 2349 | else if (dl.cmd == MachO::LC_REEXPORT_DYLIB) |
| 2350 | outs() << " cmd LC_REEXPORT_DYLIB\n"; |
| 2351 | else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB) |
| 2352 | outs() << " cmd LC_LAZY_LOAD_DYLIB\n"; |
| 2353 | else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB) |
| 2354 | outs() << " cmd LC_LOAD_UPWARD_DYLIB\n"; |
| 2355 | else |
| 2356 | outs() << " cmd " << dl.cmd << " (unknown)\n"; |
| 2357 | outs() << " cmdsize " << dl.cmdsize; |
| 2358 | if (dl.cmdsize < sizeof(struct MachO::dylib_command)) |
| 2359 | outs() << " Incorrect size\n"; |
| 2360 | else |
| 2361 | outs() << "\n"; |
| 2362 | if (dl.dylib.name < dl.cmdsize) { |
| 2363 | const char *P = (const char *)(Ptr)+dl.dylib.name; |
| 2364 | outs() << " name " << P << " (offset " << dl.dylib.name << ")\n"; |
| 2365 | } else { |
| 2366 | outs() << " name ?(bad offset " << dl.dylib.name << ")\n"; |
| 2367 | } |
| 2368 | outs() << " time stamp " << dl.dylib.timestamp << " "; |
| 2369 | time_t t = dl.dylib.timestamp; |
| 2370 | outs() << ctime(&t); |
| 2371 | outs() << " current version "; |
| 2372 | if (dl.dylib.current_version == 0xffffffff) |
| 2373 | outs() << "n/a\n"; |
| 2374 | else |
| 2375 | outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "." |
| 2376 | << ((dl.dylib.current_version >> 8) & 0xff) << "." |
| 2377 | << (dl.dylib.current_version & 0xff) << "\n"; |
| 2378 | outs() << "compatibility version "; |
| 2379 | if (dl.dylib.compatibility_version == 0xffffffff) |
| 2380 | outs() << "n/a\n"; |
| 2381 | else |
| 2382 | outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "." |
| 2383 | << ((dl.dylib.compatibility_version >> 8) & 0xff) << "." |
| 2384 | << (dl.dylib.compatibility_version & 0xff) << "\n"; |
| 2385 | } |
| 2386 | |
| 2387 | static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld, |
| 2388 | uint32_t object_size) { |
| 2389 | if (ld.cmd == MachO::LC_CODE_SIGNATURE) |
| 2390 | outs() << " cmd LC_FUNCTION_STARTS\n"; |
| 2391 | else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO) |
| 2392 | outs() << " cmd LC_SEGMENT_SPLIT_INFO\n"; |
| 2393 | else if (ld.cmd == MachO::LC_FUNCTION_STARTS) |
| 2394 | outs() << " cmd LC_FUNCTION_STARTS\n"; |
| 2395 | else if (ld.cmd == MachO::LC_DATA_IN_CODE) |
| 2396 | outs() << " cmd LC_DATA_IN_CODE\n"; |
| 2397 | else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS) |
| 2398 | outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n"; |
| 2399 | else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) |
| 2400 | outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n"; |
| 2401 | else |
| 2402 | outs() << " cmd " << ld.cmd << " (?)\n"; |
| 2403 | outs() << " cmdsize " << ld.cmdsize; |
| 2404 | if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command)) |
| 2405 | outs() << " Incorrect size\n"; |
| 2406 | else |
| 2407 | outs() << "\n"; |
| 2408 | outs() << " dataoff " << ld.dataoff; |
| 2409 | if (ld.dataoff > object_size) |
| 2410 | outs() << " (past end of file)\n"; |
| 2411 | else |
| 2412 | outs() << "\n"; |
| 2413 | outs() << " datasize " << ld.datasize; |
| 2414 | uint64_t big_size = ld.dataoff; |
| 2415 | big_size += ld.datasize; |
| 2416 | if (big_size > object_size) |
| 2417 | outs() << " (past end of file)\n"; |
| 2418 | else |
| 2419 | outs() << "\n"; |
| 2420 | } |
| 2421 | |
Kevin Enderby | 956366c | 2014-08-29 22:30:52 +0000 | [diff] [blame] | 2422 | static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t ncmds, |
| 2423 | uint32_t filetype, uint32_t cputype, |
| 2424 | bool verbose) { |
| 2425 | StringRef Buf = Obj->getData(); |
| 2426 | MachOObjectFile::LoadCommandInfo Command = Obj->getFirstLoadCommandInfo(); |
| 2427 | for (unsigned i = 0;; ++i) { |
| 2428 | outs() << "Load command " << i << "\n"; |
| 2429 | if (Command.C.cmd == MachO::LC_SEGMENT) { |
| 2430 | MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command); |
| 2431 | const char *sg_segname = SLC.segname; |
| 2432 | PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr, |
| 2433 | SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot, |
| 2434 | SLC.initprot, SLC.nsects, SLC.flags, Buf.size(), |
| 2435 | verbose); |
| 2436 | for (unsigned j = 0; j < SLC.nsects; j++) { |
| 2437 | MachO::section_64 S = Obj->getSection64(Command, j); |
| 2438 | PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align, |
| 2439 | S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2, |
| 2440 | SLC.cmd, sg_segname, filetype, Buf.size(), verbose); |
| 2441 | } |
| 2442 | } else if (Command.C.cmd == MachO::LC_SEGMENT_64) { |
| 2443 | MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command); |
| 2444 | const char *sg_segname = SLC_64.segname; |
| 2445 | PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname, |
| 2446 | SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff, |
| 2447 | SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot, |
| 2448 | SLC_64.nsects, SLC_64.flags, Buf.size(), verbose); |
| 2449 | for (unsigned j = 0; j < SLC_64.nsects; j++) { |
| 2450 | MachO::section_64 S_64 = Obj->getSection64(Command, j); |
| 2451 | PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size, |
| 2452 | S_64.offset, S_64.align, S_64.reloff, S_64.nreloc, |
| 2453 | S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd, |
| 2454 | sg_segname, filetype, Buf.size(), verbose); |
| 2455 | } |
| 2456 | } else if (Command.C.cmd == MachO::LC_SYMTAB) { |
| 2457 | MachO::symtab_command Symtab = Obj->getSymtabLoadCommand(); |
| 2458 | PrintSymtabLoadCommand(Symtab, cputype, Buf.size()); |
| 2459 | } else if (Command.C.cmd == MachO::LC_DYSYMTAB) { |
| 2460 | MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand(); |
| 2461 | MachO::symtab_command Symtab = Obj->getSymtabLoadCommand(); |
| 2462 | PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(), cputype); |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 2463 | } else if (Command.C.cmd == MachO::LC_DYLD_INFO || |
| 2464 | Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) { |
| 2465 | MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command); |
| 2466 | PrintDyldInfoLoadCommand(DyldInfo, Buf.size()); |
| 2467 | } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER || |
| 2468 | Command.C.cmd == MachO::LC_ID_DYLINKER || |
| 2469 | Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) { |
| 2470 | MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command); |
| 2471 | PrintDyldLoadCommand(Dyld, Command.Ptr); |
| 2472 | } else if (Command.C.cmd == MachO::LC_UUID) { |
| 2473 | MachO::uuid_command Uuid = Obj->getUuidCommand(Command); |
| 2474 | PrintUuidLoadCommand(Uuid); |
| 2475 | } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX) { |
| 2476 | MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command); |
| 2477 | PrintVersionMinLoadCommand(Vd); |
| 2478 | } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) { |
| 2479 | MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command); |
| 2480 | PrintSourceVersionCommand(Sd); |
| 2481 | } else if (Command.C.cmd == MachO::LC_MAIN) { |
| 2482 | MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command); |
| 2483 | PrintEntryPointCommand(Ep); |
| 2484 | } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB) { |
| 2485 | MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command); |
| 2486 | PrintDylibCommand(Dl, Command.Ptr); |
| 2487 | } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE || |
| 2488 | Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO || |
| 2489 | Command.C.cmd == MachO::LC_FUNCTION_STARTS || |
| 2490 | Command.C.cmd == MachO::LC_DATA_IN_CODE || |
| 2491 | Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS || |
| 2492 | Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) { |
| 2493 | MachO::linkedit_data_command Ld = |
| 2494 | Obj->getLinkeditDataLoadCommand(Command); |
| 2495 | PrintLinkEditDataCommand(Ld, Buf.size()); |
Kevin Enderby | 956366c | 2014-08-29 22:30:52 +0000 | [diff] [blame] | 2496 | } else { |
| 2497 | outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd) |
| 2498 | << ")\n"; |
| 2499 | outs() << " cmdsize " << Command.C.cmdsize << "\n"; |
| 2500 | // TODO: get and print the raw bytes of the load command. |
| 2501 | } |
| 2502 | // TODO: print all the other kinds of load commands. |
| 2503 | if (i == ncmds - 1) |
| 2504 | break; |
| 2505 | else |
| 2506 | Command = Obj->getNextLoadCommandInfo(Command); |
| 2507 | } |
| 2508 | } |
| 2509 | |
| 2510 | static void getAndPrintMachHeader(const MachOObjectFile *Obj, uint32_t &ncmds, |
| 2511 | uint32_t &filetype, uint32_t &cputype, |
| 2512 | bool verbose) { |
Kevin Enderby | b76d386 | 2014-08-22 20:35:18 +0000 | [diff] [blame] | 2513 | if (Obj->is64Bit()) { |
| 2514 | MachO::mach_header_64 H_64; |
| 2515 | H_64 = Obj->getHeader64(); |
| 2516 | PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype, |
| 2517 | H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose); |
Kevin Enderby | 956366c | 2014-08-29 22:30:52 +0000 | [diff] [blame] | 2518 | ncmds = H_64.ncmds; |
| 2519 | filetype = H_64.filetype; |
| 2520 | cputype = H_64.cputype; |
Kevin Enderby | b76d386 | 2014-08-22 20:35:18 +0000 | [diff] [blame] | 2521 | } else { |
| 2522 | MachO::mach_header H; |
| 2523 | H = Obj->getHeader(); |
| 2524 | PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds, |
| 2525 | H.sizeofcmds, H.flags, verbose); |
Kevin Enderby | 956366c | 2014-08-29 22:30:52 +0000 | [diff] [blame] | 2526 | ncmds = H.ncmds; |
| 2527 | filetype = H.filetype; |
| 2528 | cputype = H.cputype; |
Kevin Enderby | b76d386 | 2014-08-22 20:35:18 +0000 | [diff] [blame] | 2529 | } |
| 2530 | } |
| 2531 | |
| 2532 | void llvm::printMachOFileHeader(const object::ObjectFile *Obj) { |
| 2533 | const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj); |
Kevin Enderby | 956366c | 2014-08-29 22:30:52 +0000 | [diff] [blame] | 2534 | uint32_t ncmds = 0; |
| 2535 | uint32_t filetype = 0; |
| 2536 | uint32_t cputype = 0; |
| 2537 | getAndPrintMachHeader(file, ncmds, filetype, cputype, true); |
| 2538 | PrintLoadCommands(file, ncmds, filetype, cputype, true); |
Kevin Enderby | b76d386 | 2014-08-22 20:35:18 +0000 | [diff] [blame] | 2539 | } |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2540 | |
| 2541 | //===----------------------------------------------------------------------===// |
| 2542 | // export trie dumping |
| 2543 | //===----------------------------------------------------------------------===// |
| 2544 | |
| 2545 | void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) { |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 2546 | for (const llvm::object::ExportEntry &Entry : Obj->exports()) { |
| 2547 | uint64_t Flags = Entry.flags(); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2548 | bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT); |
| 2549 | bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION); |
| 2550 | bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) == |
| 2551 | MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL); |
| 2552 | bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) == |
| 2553 | MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE); |
| 2554 | bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER); |
| 2555 | if (ReExport) |
| 2556 | outs() << "[re-export] "; |
| 2557 | else |
| 2558 | outs() |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 2559 | << format("0x%08llX ", Entry.address()); // FIXME:add in base address |
| 2560 | outs() << Entry.name(); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2561 | if (WeakDef || ThreadLocal || Resolver || Abs) { |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 2562 | bool NeedsComma = false; |
Nick Kledzik | 1d1ac4b | 2014-09-03 01:12:52 +0000 | [diff] [blame] | 2563 | outs() << " ["; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2564 | if (WeakDef) { |
| 2565 | outs() << "weak_def"; |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 2566 | NeedsComma = true; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2567 | } |
| 2568 | if (ThreadLocal) { |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 2569 | if (NeedsComma) |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2570 | outs() << ", "; |
| 2571 | outs() << "per-thread"; |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 2572 | NeedsComma = true; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2573 | } |
| 2574 | if (Abs) { |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 2575 | if (NeedsComma) |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2576 | outs() << ", "; |
| 2577 | outs() << "absolute"; |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 2578 | NeedsComma = true; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2579 | } |
| 2580 | if (Resolver) { |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 2581 | if (NeedsComma) |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2582 | outs() << ", "; |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 2583 | outs() << format("resolver=0x%08llX", Entry.other()); |
| 2584 | NeedsComma = true; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2585 | } |
| 2586 | outs() << "]"; |
| 2587 | } |
| 2588 | if (ReExport) { |
| 2589 | StringRef DylibName = "unknown"; |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 2590 | int Ordinal = Entry.other() - 1; |
| 2591 | Obj->getLibraryShortNameByIndex(Ordinal, DylibName); |
| 2592 | if (Entry.otherName().empty()) |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2593 | outs() << " (from " << DylibName << ")"; |
| 2594 | else |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 2595 | outs() << " (" << Entry.otherName() << " from " << DylibName << ")"; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2596 | } |
| 2597 | outs() << "\n"; |
| 2598 | } |
| 2599 | } |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2600 | |
| 2601 | |
| 2602 | //===----------------------------------------------------------------------===// |
| 2603 | // rebase table dumping |
| 2604 | //===----------------------------------------------------------------------===// |
| 2605 | |
| 2606 | namespace { |
| 2607 | class SegInfo { |
| 2608 | public: |
| 2609 | SegInfo(const object::MachOObjectFile *Obj); |
| 2610 | |
| 2611 | StringRef segmentName(uint32_t SegIndex); |
| 2612 | StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset); |
| 2613 | uint64_t address(uint32_t SegIndex, uint64_t SegOffset); |
| 2614 | |
| 2615 | private: |
| 2616 | struct SectionInfo { |
| 2617 | uint64_t Address; |
| 2618 | uint64_t Size; |
| 2619 | StringRef SectionName; |
| 2620 | StringRef SegmentName; |
| 2621 | uint64_t OffsetInSegment; |
| 2622 | uint64_t SegmentStartAddress; |
| 2623 | uint32_t SegmentIndex; |
| 2624 | }; |
| 2625 | const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset); |
| 2626 | SmallVector<SectionInfo, 32> Sections; |
| 2627 | }; |
| 2628 | } |
| 2629 | |
| 2630 | SegInfo::SegInfo(const object::MachOObjectFile *Obj) { |
| 2631 | // Build table of sections so segIndex/offset pairs can be translated. |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2632 | uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0; |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2633 | StringRef CurSegName; |
| 2634 | uint64_t CurSegAddress; |
| 2635 | for (const SectionRef &Section : Obj->sections()) { |
| 2636 | SectionInfo Info; |
| 2637 | if (error(Section.getName(Info.SectionName))) |
| 2638 | return; |
| 2639 | if (error(Section.getAddress(Info.Address))) |
| 2640 | return; |
| 2641 | if (error(Section.getSize(Info.Size))) |
| 2642 | return; |
| 2643 | Info.SegmentName = |
| 2644 | Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl()); |
| 2645 | if (!Info.SegmentName.equals(CurSegName)) { |
| 2646 | ++CurSegIndex; |
| 2647 | CurSegName = Info.SegmentName; |
| 2648 | CurSegAddress = Info.Address; |
| 2649 | } |
| 2650 | Info.SegmentIndex = CurSegIndex - 1; |
| 2651 | Info.OffsetInSegment = Info.Address - CurSegAddress; |
| 2652 | Info.SegmentStartAddress = CurSegAddress; |
| 2653 | Sections.push_back(Info); |
| 2654 | } |
| 2655 | } |
| 2656 | |
| 2657 | StringRef SegInfo::segmentName(uint32_t SegIndex) { |
| 2658 | for (const SectionInfo &SI : Sections) { |
| 2659 | if (SI.SegmentIndex == SegIndex) |
| 2660 | return SI.SegmentName; |
| 2661 | } |
| 2662 | llvm_unreachable("invalid segIndex"); |
| 2663 | } |
| 2664 | |
| 2665 | const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex, |
| 2666 | uint64_t OffsetInSeg) { |
| 2667 | for (const SectionInfo &SI : Sections) { |
| 2668 | if (SI.SegmentIndex != SegIndex) |
| 2669 | continue; |
| 2670 | if (SI.OffsetInSegment > OffsetInSeg) |
| 2671 | continue; |
| 2672 | if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size)) |
| 2673 | continue; |
| 2674 | return SI; |
| 2675 | } |
| 2676 | llvm_unreachable("segIndex and offset not in any section"); |
| 2677 | } |
| 2678 | |
| 2679 | StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) { |
| 2680 | return findSection(SegIndex, OffsetInSeg).SectionName; |
| 2681 | } |
| 2682 | |
| 2683 | uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) { |
| 2684 | const SectionInfo &SI = findSection(SegIndex, OffsetInSeg); |
| 2685 | return SI.SegmentStartAddress + OffsetInSeg; |
| 2686 | } |
| 2687 | |
| 2688 | void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) { |
| 2689 | // Build table of sections so names can used in final output. |
| 2690 | SegInfo sectionTable(Obj); |
| 2691 | |
| 2692 | outs() << "segment section address type\n"; |
| 2693 | for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) { |
| 2694 | uint32_t SegIndex = Entry.segmentIndex(); |
| 2695 | uint64_t OffsetInSeg = Entry.segmentOffset(); |
| 2696 | StringRef SegmentName = sectionTable.segmentName(SegIndex); |
| 2697 | StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg); |
| 2698 | uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg); |
| 2699 | |
| 2700 | // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer |
Nick Kledzik | 3df5fb8 | 2014-09-13 00:18:40 +0000 | [diff] [blame] | 2701 | outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n", |
| 2702 | SegmentName.str().c_str(), |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2703 | SectionName.str().c_str(), Address, |
| 2704 | Entry.typeName().str().c_str()); |
| 2705 | } |
| 2706 | } |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2707 | |
| 2708 | static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) { |
| 2709 | StringRef DylibName; |
| 2710 | switch (Ordinal) { |
| 2711 | case MachO::BIND_SPECIAL_DYLIB_SELF: |
| 2712 | return "this-image"; |
| 2713 | case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE: |
| 2714 | return "main-executable"; |
| 2715 | case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP: |
| 2716 | return "flat-namespace"; |
| 2717 | default: |
Nick Kledzik | abd2987 | 2014-09-16 22:03:13 +0000 | [diff] [blame] | 2718 | if (Ordinal > 0) { |
| 2719 | std::error_code EC = Obj->getLibraryShortNameByIndex(Ordinal-1, |
| 2720 | DylibName); |
| 2721 | if (EC) |
| 2722 | return "<<ordinal too big>>"; |
| 2723 | return DylibName; |
| 2724 | } |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2725 | } |
Nick Kledzik | abd2987 | 2014-09-16 22:03:13 +0000 | [diff] [blame] | 2726 | return "<<unknown special ordinal>>"; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2727 | } |
| 2728 | |
| 2729 | //===----------------------------------------------------------------------===// |
| 2730 | // bind table dumping |
| 2731 | //===----------------------------------------------------------------------===// |
| 2732 | |
| 2733 | void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) { |
| 2734 | // Build table of sections so names can used in final output. |
| 2735 | SegInfo sectionTable(Obj); |
| 2736 | |
| 2737 | outs() << "segment section address type " |
| 2738 | "addend dylib symbol\n"; |
| 2739 | for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) { |
| 2740 | uint32_t SegIndex = Entry.segmentIndex(); |
| 2741 | uint64_t OffsetInSeg = Entry.segmentOffset(); |
| 2742 | StringRef SegmentName = sectionTable.segmentName(SegIndex); |
| 2743 | StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg); |
| 2744 | uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg); |
| 2745 | |
| 2746 | // Table lines look like: |
| 2747 | // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard |
| 2748 | outs() << format("%-8s %-18s 0x%08" PRIX64 " %-8s %-8" PRId64 " %-20s", |
| 2749 | SegmentName.str().c_str(), |
| 2750 | SectionName.str().c_str(), |
| 2751 | Address, |
| 2752 | Entry.typeName().str().c_str(), |
| 2753 | Entry.addend(), |
Nick Kledzik | 3006130 | 2014-09-17 00:25:22 +0000 | [diff] [blame] | 2754 | ordinalName(Obj, Entry.ordinal()).str().c_str()) |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2755 | << Entry.symbolName(); |
| 2756 | if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT) |
| 2757 | outs() << " (weak_import)\n"; |
| 2758 | else |
| 2759 | outs() << "\n"; |
| 2760 | } |
| 2761 | } |
| 2762 | |
| 2763 | //===----------------------------------------------------------------------===// |
| 2764 | // lazy bind table dumping |
| 2765 | //===----------------------------------------------------------------------===// |
| 2766 | |
| 2767 | void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) { |
| 2768 | // Build table of sections so names can used in final output. |
| 2769 | SegInfo sectionTable(Obj); |
| 2770 | |
| 2771 | outs() << "segment section address " |
| 2772 | "dylib symbol\n"; |
| 2773 | for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) { |
| 2774 | uint32_t SegIndex = Entry.segmentIndex(); |
| 2775 | uint64_t OffsetInSeg = Entry.segmentOffset(); |
| 2776 | StringRef SegmentName = sectionTable.segmentName(SegIndex); |
| 2777 | StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg); |
| 2778 | uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg); |
| 2779 | |
| 2780 | // Table lines look like: |
| 2781 | // __DATA __got 0x00012010 libSystem ___stack_chk_guard |
| 2782 | outs() << format("%-8s %-18s 0x%08" PRIX64 " %-20s", |
| 2783 | SegmentName.str().c_str(), |
| 2784 | SectionName.str().c_str(), |
| 2785 | Address, |
Nick Kledzik | 3006130 | 2014-09-17 00:25:22 +0000 | [diff] [blame] | 2786 | ordinalName(Obj, Entry.ordinal()).str().c_str()) |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2787 | << Entry.symbolName() << "\n"; |
| 2788 | } |
| 2789 | } |
| 2790 | |
| 2791 | |
| 2792 | //===----------------------------------------------------------------------===// |
| 2793 | // weak bind table dumping |
| 2794 | //===----------------------------------------------------------------------===// |
| 2795 | |
| 2796 | void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) { |
| 2797 | // Build table of sections so names can used in final output. |
| 2798 | SegInfo sectionTable(Obj); |
| 2799 | |
| 2800 | outs() << "segment section address " |
| 2801 | "type addend symbol\n"; |
| 2802 | for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) { |
| 2803 | // Strong symbols don't have a location to update. |
| 2804 | if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) { |
| 2805 | outs() << " strong " |
| 2806 | << Entry.symbolName() << "\n"; |
| 2807 | continue; |
| 2808 | } |
| 2809 | uint32_t SegIndex = Entry.segmentIndex(); |
| 2810 | uint64_t OffsetInSeg = Entry.segmentOffset(); |
| 2811 | StringRef SegmentName = sectionTable.segmentName(SegIndex); |
| 2812 | StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg); |
| 2813 | uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg); |
| 2814 | |
| 2815 | // Table lines look like: |
| 2816 | // __DATA __data 0x00001000 pointer 0 _foo |
| 2817 | outs() << format("%-8s %-18s 0x%08" PRIX64 " %-8s %-8" PRId64 " ", |
| 2818 | SegmentName.str().c_str(), |
| 2819 | SectionName.str().c_str(), |
| 2820 | Address, |
| 2821 | Entry.typeName().str().c_str(), |
| 2822 | Entry.addend()) |
| 2823 | << Entry.symbolName() << "\n"; |
| 2824 | } |
| 2825 | } |
| 2826 | |
| 2827 | |