macho-dump.cpp revision 263508
11573Srgrimes//===-- macho-dump.cpp - Mach Object Dumping Tool -------------------------===// 21573Srgrimes// 31573Srgrimes// The LLVM Compiler Infrastructure 41573Srgrimes// 51573Srgrimes// This file is distributed under the University of Illinois Open Source 61573Srgrimes// License. See LICENSE.TXT for details. 71573Srgrimes// 81573Srgrimes//===----------------------------------------------------------------------===// 91573Srgrimes// 101573Srgrimes// This is a testing tool for use with the MC/Mach-O LLVM components. 111573Srgrimes// 121573Srgrimes//===----------------------------------------------------------------------===// 131573Srgrimes 141573Srgrimes#include "llvm/Object/MachO.h" 151573Srgrimes#include "llvm/ADT/StringExtras.h" 16148834Sstefanf#include "llvm/ADT/Twine.h" 171573Srgrimes#include "llvm/Support/Casting.h" 181573Srgrimes#include "llvm/Support/CommandLine.h" 191573Srgrimes#include "llvm/Support/Format.h" 201573Srgrimes#include "llvm/Support/ManagedStatic.h" 211573Srgrimes#include "llvm/Support/MemoryBuffer.h" 221573Srgrimes#include "llvm/Support/raw_ostream.h" 231573Srgrimes#include "llvm/Support/system_error.h" 241573Srgrimesusing namespace llvm; 251573Srgrimesusing namespace llvm::object; 261573Srgrimes 271573Srgrimesstatic cl::opt<std::string> 281573SrgrimesInputFile(cl::Positional, cl::desc("<input file>"), cl::init("-")); 291573Srgrimes 301573Srgrimesstatic cl::opt<bool> 311573SrgrimesShowSectionData("dump-section-data", cl::desc("Dump the contents of sections"), 321573Srgrimes cl::init(false)); 33148834Sstefanf 3484260Sobrien/// 351573Srgrimes 361573Srgrimesstatic const char *ProgramName; 371573Srgrimes 381573Srgrimesstatic void Message(const char *Type, const Twine &Msg) { 391573Srgrimes errs() << ProgramName << ": " << Type << ": " << Msg << "\n"; 401573Srgrimes} 4184260Sobrien 421573Srgrimesstatic int Error(const Twine &Msg) { 431573Srgrimes Message("error", Msg); 441573Srgrimes return 1; 451573Srgrimes} 4684260Sobrien 4784260Sobrienstatic void Warning(const Twine &Msg) { 4884260Sobrien Message("warning", Msg); 491573Srgrimes} 501573Srgrimes 5184260Sobrien/// 5284260Sobrien 5384260Sobrienstatic void DumpSegmentCommandData(StringRef Name, 5484260Sobrien uint64_t VMAddr, uint64_t VMSize, 5584260Sobrien uint64_t FileOffset, uint64_t FileSize, 5684260Sobrien uint32_t MaxProt, uint32_t InitProt, 5784260Sobrien uint32_t NumSections, uint32_t Flags) { 581573Srgrimes outs() << " ('segment_name', '"; 591573Srgrimes outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n"; 60 outs() << " ('vm_addr', " << VMAddr << ")\n"; 61 outs() << " ('vm_size', " << VMSize << ")\n"; 62 outs() << " ('file_offset', " << FileOffset << ")\n"; 63 outs() << " ('file_size', " << FileSize << ")\n"; 64 outs() << " ('maxprot', " << MaxProt << ")\n"; 65 outs() << " ('initprot', " << InitProt << ")\n"; 66 outs() << " ('num_sections', " << NumSections << ")\n"; 67 outs() << " ('flags', " << Flags << ")\n"; 68} 69 70static int DumpSectionData(const MachOObjectFile &Obj, unsigned Index, 71 StringRef Name, 72 StringRef SegmentName, uint64_t Address, 73 uint64_t Size, uint32_t Offset, 74 uint32_t Align, uint32_t RelocationTableOffset, 75 uint32_t NumRelocationTableEntries, 76 uint32_t Flags, uint32_t Reserved1, 77 uint32_t Reserved2, uint64_t Reserved3 = ~0ULL) { 78 outs() << " # Section " << Index << "\n"; 79 outs() << " (('section_name', '"; 80 outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n"; 81 outs() << " ('segment_name', '"; 82 outs().write_escaped(SegmentName, /*UseHexEscapes=*/true) << "')\n"; 83 outs() << " ('address', " << Address << ")\n"; 84 outs() << " ('size', " << Size << ")\n"; 85 outs() << " ('offset', " << Offset << ")\n"; 86 outs() << " ('alignment', " << Align << ")\n"; 87 outs() << " ('reloc_offset', " << RelocationTableOffset << ")\n"; 88 outs() << " ('num_reloc', " << NumRelocationTableEntries << ")\n"; 89 outs() << " ('flags', " << format("0x%x", Flags) << ")\n"; 90 outs() << " ('reserved1', " << Reserved1 << ")\n"; 91 outs() << " ('reserved2', " << Reserved2 << ")\n"; 92 if (Reserved3 != ~0ULL) 93 outs() << " ('reserved3', " << Reserved3 << ")\n"; 94 outs() << " ),\n"; 95 96 // Dump the relocation entries. 97 outs() << " ('_relocations', [\n"; 98 unsigned RelNum = 0; 99 error_code EC; 100 for (relocation_iterator I = Obj.section_rel_begin(Index), 101 E = Obj.section_rel_end(Index); I != E; I.increment(EC), ++RelNum) { 102 MachO::any_relocation_info RE = Obj.getRelocation(I->getRawDataRefImpl()); 103 outs() << " # Relocation " << RelNum << "\n"; 104 outs() << " (('word-0', " << format("0x%x", RE.r_word0) << "),\n"; 105 outs() << " ('word-1', " << format("0x%x", RE.r_word1) << ")),\n"; 106 } 107 outs() << " ])\n"; 108 109 // Dump the section data, if requested. 110 if (ShowSectionData) { 111 outs() << " ('_section_data', '"; 112 StringRef Data = Obj.getData().substr(Offset, Size); 113 for (unsigned i = 0; i != Data.size(); ++i) { 114 if (i && (i % 4) == 0) 115 outs() << ' '; 116 outs() << hexdigit((Data[i] >> 4) & 0xF, /*LowerCase=*/true); 117 outs() << hexdigit((Data[i] >> 0) & 0xF, /*LowerCase=*/true); 118 } 119 outs() << "')\n"; 120 } 121 122 return 0; 123} 124 125static int DumpSegmentCommand(const MachOObjectFile &Obj, 126 const MachOObjectFile::LoadCommandInfo &LCI) { 127 MachO::segment_command SLC = Obj.getSegmentLoadCommand(LCI); 128 129 DumpSegmentCommandData(StringRef(SLC.segname, 16), SLC.vmaddr, 130 SLC.vmsize, SLC.fileoff, SLC.filesize, 131 SLC.maxprot, SLC.initprot, SLC.nsects, SLC.flags); 132 133 // Dump the sections. 134 outs() << " ('sections', [\n"; 135 for (unsigned i = 0; i != SLC.nsects; ++i) { 136 MachO::section Sect = Obj.getSection(LCI, i); 137 DumpSectionData(Obj, i, StringRef(Sect.sectname, 16), 138 StringRef(Sect.segname, 16), Sect.addr, 139 Sect.size, Sect.offset, Sect.align, 140 Sect.reloff, Sect.nreloc, Sect.flags, 141 Sect.reserved1, Sect.reserved2); 142 } 143 outs() << " ])\n"; 144 145 return 0; 146} 147 148static int DumpSegment64Command(const MachOObjectFile &Obj, 149 const MachOObjectFile::LoadCommandInfo &LCI) { 150 MachO::segment_command_64 SLC = Obj.getSegment64LoadCommand(LCI); 151 DumpSegmentCommandData(StringRef(SLC.segname, 16), SLC.vmaddr, 152 SLC.vmsize, SLC.fileoff, SLC.filesize, 153 SLC.maxprot, SLC.initprot, SLC.nsects, SLC.flags); 154 155 // Dump the sections. 156 outs() << " ('sections', [\n"; 157 for (unsigned i = 0; i != SLC.nsects; ++i) { 158 MachO::section_64 Sect = Obj.getSection64(LCI, i); 159 160 DumpSectionData(Obj, i, StringRef(Sect.sectname, 16), 161 StringRef(Sect.segname, 16), Sect.addr, 162 Sect.size, Sect.offset, Sect.align, 163 Sect.reloff, Sect.nreloc, Sect.flags, 164 Sect.reserved1, Sect.reserved2, 165 Sect.reserved3); 166 } 167 outs() << " ])\n"; 168 169 return 0; 170} 171 172static void DumpSymbolTableEntryData(const MachOObjectFile &Obj, 173 unsigned Index, uint32_t StringIndex, 174 uint8_t Type, uint8_t SectionIndex, 175 uint16_t Flags, uint64_t Value, 176 StringRef StringTable) { 177 const char *Name = &StringTable.data()[StringIndex]; 178 outs() << " # Symbol " << Index << "\n"; 179 outs() << " (('n_strx', " << StringIndex << ")\n"; 180 outs() << " ('n_type', " << format("0x%x", Type) << ")\n"; 181 outs() << " ('n_sect', " << uint32_t(SectionIndex) << ")\n"; 182 outs() << " ('n_desc', " << Flags << ")\n"; 183 outs() << " ('n_value', " << Value << ")\n"; 184 outs() << " ('_string', '" << Name << "')\n"; 185 outs() << " ),\n"; 186} 187 188static int DumpSymtabCommand(const MachOObjectFile &Obj) { 189 MachO::symtab_command SLC = Obj.getSymtabLoadCommand(); 190 191 outs() << " ('symoff', " << SLC.symoff << ")\n"; 192 outs() << " ('nsyms', " << SLC.nsyms << ")\n"; 193 outs() << " ('stroff', " << SLC.stroff << ")\n"; 194 outs() << " ('strsize', " << SLC.strsize << ")\n"; 195 196 // Dump the string data. 197 outs() << " ('_string_data', '"; 198 StringRef StringTable = Obj.getStringTableData(); 199 outs().write_escaped(StringTable, 200 /*UseHexEscapes=*/true) << "')\n"; 201 202 // Dump the symbol table. 203 outs() << " ('_symbols', [\n"; 204 error_code EC; 205 unsigned SymNum = 0; 206 for (symbol_iterator I = Obj.begin_symbols(), E = Obj.end_symbols(); I != E; 207 I.increment(EC), ++SymNum) { 208 DataRefImpl DRI = I->getRawDataRefImpl(); 209 if (Obj.is64Bit()) { 210 MachO::nlist_64 STE = Obj.getSymbol64TableEntry(DRI); 211 DumpSymbolTableEntryData(Obj, SymNum, STE.n_strx, STE.n_type, 212 STE.n_sect, STE.n_desc, STE.n_value, 213 StringTable); 214 } else { 215 MachO::nlist STE = Obj.getSymbolTableEntry(DRI); 216 DumpSymbolTableEntryData(Obj, SymNum, STE.n_strx, STE.n_type, 217 STE.n_sect, STE.n_desc, STE.n_value, 218 StringTable); 219 } 220 } 221 outs() << " ])\n"; 222 223 return 0; 224} 225 226static int DumpDysymtabCommand(const MachOObjectFile &Obj) { 227 MachO::dysymtab_command DLC = Obj.getDysymtabLoadCommand(); 228 229 outs() << " ('ilocalsym', " << DLC.ilocalsym << ")\n"; 230 outs() << " ('nlocalsym', " << DLC.nlocalsym << ")\n"; 231 outs() << " ('iextdefsym', " << DLC.iextdefsym << ")\n"; 232 outs() << " ('nextdefsym', " << DLC.nextdefsym << ")\n"; 233 outs() << " ('iundefsym', " << DLC.iundefsym << ")\n"; 234 outs() << " ('nundefsym', " << DLC.nundefsym << ")\n"; 235 outs() << " ('tocoff', " << DLC.tocoff << ")\n"; 236 outs() << " ('ntoc', " << DLC.ntoc << ")\n"; 237 outs() << " ('modtaboff', " << DLC.modtaboff << ")\n"; 238 outs() << " ('nmodtab', " << DLC.nmodtab << ")\n"; 239 outs() << " ('extrefsymoff', " << DLC.extrefsymoff << ")\n"; 240 outs() << " ('nextrefsyms', " << DLC.nextrefsyms << ")\n"; 241 outs() << " ('indirectsymoff', " << DLC.indirectsymoff << ")\n"; 242 outs() << " ('nindirectsyms', " << DLC.nindirectsyms << ")\n"; 243 outs() << " ('extreloff', " << DLC.extreloff << ")\n"; 244 outs() << " ('nextrel', " << DLC.nextrel << ")\n"; 245 outs() << " ('locreloff', " << DLC.locreloff << ")\n"; 246 outs() << " ('nlocrel', " << DLC.nlocrel << ")\n"; 247 248 // Dump the indirect symbol table. 249 outs() << " ('_indirect_symbols', [\n"; 250 for (unsigned i = 0; i != DLC.nindirectsyms; ++i) { 251 uint32_t ISTE = Obj.getIndirectSymbolTableEntry(DLC, i); 252 outs() << " # Indirect Symbol " << i << "\n"; 253 outs() << " (('symbol_index', " << format("0x%x", ISTE) << "),),\n"; 254 } 255 outs() << " ])\n"; 256 257 return 0; 258} 259 260static int 261DumpLinkeditDataCommand(const MachOObjectFile &Obj, 262 const MachOObjectFile::LoadCommandInfo &LCI) { 263 MachO::linkedit_data_command LLC = Obj.getLinkeditDataLoadCommand(LCI); 264 outs() << " ('dataoff', " << LLC.dataoff << ")\n" 265 << " ('datasize', " << LLC.datasize << ")\n" 266 << " ('_addresses', [\n"; 267 268 SmallVector<uint64_t, 8> Addresses; 269 Obj.ReadULEB128s(LLC.dataoff, Addresses); 270 for (unsigned i = 0, e = Addresses.size(); i != e; ++i) 271 outs() << " # Address " << i << '\n' 272 << " ('address', " << format("0x%x", Addresses[i]) << "),\n"; 273 274 outs() << " ])\n"; 275 276 return 0; 277} 278 279static int 280DumpDataInCodeDataCommand(const MachOObjectFile &Obj, 281 const MachOObjectFile::LoadCommandInfo &LCI) { 282 MachO::linkedit_data_command LLC = Obj.getLinkeditDataLoadCommand(LCI); 283 outs() << " ('dataoff', " << LLC.dataoff << ")\n" 284 << " ('datasize', " << LLC.datasize << ")\n" 285 << " ('_data_regions', [\n"; 286 287 unsigned NumRegions = LLC.datasize / sizeof(MachO::data_in_code_entry); 288 for (unsigned i = 0; i < NumRegions; ++i) { 289 MachO::data_in_code_entry DICE= Obj.getDataInCodeTableEntry(LLC.dataoff, i); 290 outs() << " # DICE " << i << "\n" 291 << " ('offset', " << DICE.offset << ")\n" 292 << " ('length', " << DICE.length << ")\n" 293 << " ('kind', " << DICE.kind << ")\n"; 294 } 295 296 outs() <<" ])\n"; 297 298 return 0; 299} 300 301static int 302DumpLinkerOptionsCommand(const MachOObjectFile &Obj, 303 const MachOObjectFile::LoadCommandInfo &LCI) { 304 MachO::linker_options_command LOLC = Obj.getLinkerOptionsLoadCommand(LCI); 305 outs() << " ('count', " << LOLC.count << ")\n" 306 << " ('_strings', [\n"; 307 308 uint64_t DataSize = LOLC.cmdsize - sizeof(MachO::linker_options_command); 309 const char *P = LCI.Ptr + sizeof(MachO::linker_options_command); 310 StringRef Data(P, DataSize); 311 for (unsigned i = 0; i != LOLC.count; ++i) { 312 std::pair<StringRef,StringRef> Split = Data.split('\0'); 313 outs() << "\t\""; 314 outs().write_escaped(Split.first); 315 outs() << "\",\n"; 316 Data = Split.second; 317 } 318 outs() <<" ])\n"; 319 320 return 0; 321} 322 323static int DumpLoadCommand(const MachOObjectFile &Obj, 324 MachOObjectFile::LoadCommandInfo &LCI) { 325 switch (LCI.C.cmd) { 326 case MachO::LC_SEGMENT: 327 return DumpSegmentCommand(Obj, LCI); 328 case MachO::LC_SEGMENT_64: 329 return DumpSegment64Command(Obj, LCI); 330 case MachO::LC_SYMTAB: 331 return DumpSymtabCommand(Obj); 332 case MachO::LC_DYSYMTAB: 333 return DumpDysymtabCommand(Obj); 334 case MachO::LC_CODE_SIGNATURE: 335 case MachO::LC_SEGMENT_SPLIT_INFO: 336 case MachO::LC_FUNCTION_STARTS: 337 return DumpLinkeditDataCommand(Obj, LCI); 338 case MachO::LC_DATA_IN_CODE: 339 return DumpDataInCodeDataCommand(Obj, LCI); 340 case MachO::LC_LINKER_OPTIONS: 341 return DumpLinkerOptionsCommand(Obj, LCI); 342 default: 343 Warning("unknown load command: " + Twine(LCI.C.cmd)); 344 return 0; 345 } 346} 347 348 349static int DumpLoadCommand(const MachOObjectFile &Obj, unsigned Index, 350 MachOObjectFile::LoadCommandInfo &LCI) { 351 outs() << " # Load Command " << Index << "\n" 352 << " (('command', " << LCI.C.cmd << ")\n" 353 << " ('size', " << LCI.C.cmdsize << ")\n"; 354 int Res = DumpLoadCommand(Obj, LCI); 355 outs() << " ),\n"; 356 return Res; 357} 358 359static void printHeader(const MachOObjectFile *Obj, 360 const MachO::mach_header &Header) { 361 outs() << "('cputype', " << Header.cputype << ")\n"; 362 outs() << "('cpusubtype', " << Header.cpusubtype << ")\n"; 363 outs() << "('filetype', " << Header.filetype << ")\n"; 364 outs() << "('num_load_commands', " << Header.ncmds << ")\n"; 365 outs() << "('load_commands_size', " << Header.sizeofcmds << ")\n"; 366 outs() << "('flag', " << Header.flags << ")\n"; 367 368 // Print extended header if 64-bit. 369 if (Obj->is64Bit()) { 370 const MachO::mach_header_64 *Header64 = 371 reinterpret_cast<const MachO::mach_header_64 *>(&Header); 372 outs() << "('reserved', " << Header64->reserved << ")\n"; 373 } 374} 375 376int main(int argc, char **argv) { 377 ProgramName = argv[0]; 378 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 379 380 cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n"); 381 382 OwningPtr<Binary> Binary; 383 if (error_code EC = createBinary(InputFile, Binary)) 384 return Error("unable to read input: '" + EC.message() + "'"); 385 386 const MachOObjectFile *InputObject = dyn_cast<MachOObjectFile>(Binary.get()); 387 if (!InputObject) 388 return Error("Not a MachO object"); 389 390 // Print the header 391 MachO::mach_header_64 Header64; 392 MachO::mach_header *Header = reinterpret_cast<MachO::mach_header*>(&Header64); 393 if (InputObject->is64Bit()) 394 Header64 = InputObject->getHeader64(); 395 else 396 *Header = InputObject->getHeader(); 397 printHeader(InputObject, *Header); 398 399 // Print the load commands. 400 int Res = 0; 401 MachOObjectFile::LoadCommandInfo Command = 402 InputObject->getFirstLoadCommandInfo(); 403 outs() << "('load_commands', [\n"; 404 for (unsigned i = 0; ; ++i) { 405 if (DumpLoadCommand(*InputObject, i, Command)) 406 break; 407 408 if (i == Header->ncmds - 1) 409 break; 410 Command = InputObject->getNextLoadCommandInfo(Command); 411 } 412 outs() << "])\n"; 413 414 return Res; 415} 416