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