1//===-- macho-dump.cpp - Mach Object Dumping Tool -------------------------===// 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 is a testing tool for use with the MC/Mach-O LLVM components. 11// 12//===----------------------------------------------------------------------===// 13 14#include "llvm/Object/MachOObject.h" 15#include "llvm/ADT/StringExtras.h" 16#include "llvm/ADT/Twine.h" 17#include "llvm/Support/CommandLine.h" 18#include "llvm/Support/Format.h" 19#include "llvm/Support/ManagedStatic.h" 20#include "llvm/Support/MemoryBuffer.h" 21#include "llvm/Support/raw_ostream.h" 22#include "llvm/Support/system_error.h" 23using namespace llvm; 24using namespace llvm::object; 25 26static cl::opt<std::string> 27InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-")); 28 29static cl::opt<bool> 30ShowSectionData("dump-section-data", cl::desc("Dump the contents of sections"), 31 cl::init(false)); 32 33/// 34 35static const char *ProgramName; 36 37static void Message(const char *Type, const Twine &Msg) { 38 errs() << ProgramName << ": " << Type << ": " << Msg << "\n"; 39} 40 41static int Error(const Twine &Msg) { 42 Message("error", Msg); 43 return 1; 44} 45 46static void Warning(const Twine &Msg) { 47 Message("warning", Msg); 48} 49 50/// 51 52static void DumpSegmentCommandData(StringRef Name, 53 uint64_t VMAddr, uint64_t VMSize, 54 uint64_t FileOffset, uint64_t FileSize, 55 uint32_t MaxProt, uint32_t InitProt, 56 uint32_t NumSections, uint32_t Flags) { 57 outs() << " ('segment_name', '"; 58 outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n"; 59 outs() << " ('vm_addr', " << VMAddr << ")\n"; 60 outs() << " ('vm_size', " << VMSize << ")\n"; 61 outs() << " ('file_offset', " << FileOffset << ")\n"; 62 outs() << " ('file_size', " << FileSize << ")\n"; 63 outs() << " ('maxprot', " << MaxProt << ")\n"; 64 outs() << " ('initprot', " << InitProt << ")\n"; 65 outs() << " ('num_sections', " << NumSections << ")\n"; 66 outs() << " ('flags', " << Flags << ")\n"; 67} 68 69static int DumpSectionData(MachOObject &Obj, unsigned Index, StringRef Name, 70 StringRef SegmentName, uint64_t Address, 71 uint64_t Size, uint32_t Offset, 72 uint32_t Align, uint32_t RelocationTableOffset, 73 uint32_t NumRelocationTableEntries, 74 uint32_t Flags, uint32_t Reserved1, 75 uint32_t Reserved2, uint64_t Reserved3 = ~0ULL) { 76 outs() << " # Section " << Index << "\n"; 77 outs() << " (('section_name', '"; 78 outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n"; 79 outs() << " ('segment_name', '"; 80 outs().write_escaped(SegmentName, /*UseHexEscapes=*/true) << "')\n"; 81 outs() << " ('address', " << Address << ")\n"; 82 outs() << " ('size', " << Size << ")\n"; 83 outs() << " ('offset', " << Offset << ")\n"; 84 outs() << " ('alignment', " << Align << ")\n"; 85 outs() << " ('reloc_offset', " << RelocationTableOffset << ")\n"; 86 outs() << " ('num_reloc', " << NumRelocationTableEntries << ")\n"; 87 outs() << " ('flags', " << format("0x%x", Flags) << ")\n"; 88 outs() << " ('reserved1', " << Reserved1 << ")\n"; 89 outs() << " ('reserved2', " << Reserved2 << ")\n"; 90 if (Reserved3 != ~0ULL) 91 outs() << " ('reserved3', " << Reserved3 << ")\n"; 92 outs() << " ),\n"; 93 94 // Dump the relocation entries. 95 int Res = 0; 96 outs() << " ('_relocations', [\n"; 97 for (unsigned i = 0; i != NumRelocationTableEntries; ++i) { 98 InMemoryStruct<macho::RelocationEntry> RE; 99 Obj.ReadRelocationEntry(RelocationTableOffset, i, RE); 100 if (!RE) { 101 Res = Error("unable to read relocation table entry '" + Twine(i) + "'"); 102 break; 103 } 104 105 outs() << " # Relocation " << i << "\n"; 106 outs() << " (('word-0', " << format("0x%x", RE->Word0) << "),\n"; 107 outs() << " ('word-1', " << format("0x%x", RE->Word1) << ")),\n"; 108 } 109 outs() << " ])\n"; 110 111 // Dump the section data, if requested. 112 if (ShowSectionData) { 113 outs() << " ('_section_data', '"; 114 StringRef Data = Obj.getData(Offset, Size); 115 for (unsigned i = 0; i != Data.size(); ++i) { 116 if (i && (i % 4) == 0) 117 outs() << ' '; 118 outs() << hexdigit((Data[i] >> 4) & 0xF, /*LowerCase=*/true); 119 outs() << hexdigit((Data[i] >> 0) & 0xF, /*LowerCase=*/true); 120 } 121 outs() << "')\n"; 122 } 123 124 return Res; 125} 126 127static int DumpSegmentCommand(MachOObject &Obj, 128 const MachOObject::LoadCommandInfo &LCI) { 129 InMemoryStruct<macho::SegmentLoadCommand> SLC; 130 Obj.ReadSegmentLoadCommand(LCI, SLC); 131 if (!SLC) 132 return Error("unable to read segment load command"); 133 134 DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress, 135 SLC->VMSize, SLC->FileOffset, SLC->FileSize, 136 SLC->MaxVMProtection, SLC->InitialVMProtection, 137 SLC->NumSections, SLC->Flags); 138 139 // Dump the sections. 140 int Res = 0; 141 outs() << " ('sections', [\n"; 142 for (unsigned i = 0; i != SLC->NumSections; ++i) { 143 InMemoryStruct<macho::Section> Sect; 144 Obj.ReadSection(LCI, i, Sect); 145 if (!SLC) { 146 Res = Error("unable to read section '" + Twine(i) + "'"); 147 break; 148 } 149 150 if ((Res = DumpSectionData(Obj, i, StringRef(Sect->Name, 16), 151 StringRef(Sect->SegmentName, 16), Sect->Address, 152 Sect->Size, Sect->Offset, Sect->Align, 153 Sect->RelocationTableOffset, 154 Sect->NumRelocationTableEntries, Sect->Flags, 155 Sect->Reserved1, Sect->Reserved2))) 156 break; 157 } 158 outs() << " ])\n"; 159 160 return Res; 161} 162 163static int DumpSegment64Command(MachOObject &Obj, 164 const MachOObject::LoadCommandInfo &LCI) { 165 InMemoryStruct<macho::Segment64LoadCommand> SLC; 166 Obj.ReadSegment64LoadCommand(LCI, SLC); 167 if (!SLC) 168 return Error("unable to read segment load command"); 169 170 DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress, 171 SLC->VMSize, SLC->FileOffset, SLC->FileSize, 172 SLC->MaxVMProtection, SLC->InitialVMProtection, 173 SLC->NumSections, SLC->Flags); 174 175 // Dump the sections. 176 int Res = 0; 177 outs() << " ('sections', [\n"; 178 for (unsigned i = 0; i != SLC->NumSections; ++i) { 179 InMemoryStruct<macho::Section64> Sect; 180 Obj.ReadSection64(LCI, i, Sect); 181 if (!SLC) { 182 Res = Error("unable to read section '" + Twine(i) + "'"); 183 break; 184 } 185 186 if ((Res = DumpSectionData(Obj, i, StringRef(Sect->Name, 16), 187 StringRef(Sect->SegmentName, 16), Sect->Address, 188 Sect->Size, Sect->Offset, Sect->Align, 189 Sect->RelocationTableOffset, 190 Sect->NumRelocationTableEntries, Sect->Flags, 191 Sect->Reserved1, Sect->Reserved2, 192 Sect->Reserved3))) 193 break; 194 } 195 outs() << " ])\n"; 196 197 return Res; 198} 199 200static void DumpSymbolTableEntryData(MachOObject &Obj, 201 unsigned Index, uint32_t StringIndex, 202 uint8_t Type, uint8_t SectionIndex, 203 uint16_t Flags, uint64_t Value) { 204 outs() << " # Symbol " << Index << "\n"; 205 outs() << " (('n_strx', " << StringIndex << ")\n"; 206 outs() << " ('n_type', " << format("0x%x", Type) << ")\n"; 207 outs() << " ('n_sect', " << uint32_t(SectionIndex) << ")\n"; 208 outs() << " ('n_desc', " << Flags << ")\n"; 209 outs() << " ('n_value', " << Value << ")\n"; 210 outs() << " ('_string', '" << Obj.getStringAtIndex(StringIndex) << "')\n"; 211 outs() << " ),\n"; 212} 213 214static int DumpSymtabCommand(MachOObject &Obj, 215 const MachOObject::LoadCommandInfo &LCI) { 216 InMemoryStruct<macho::SymtabLoadCommand> SLC; 217 Obj.ReadSymtabLoadCommand(LCI, SLC); 218 if (!SLC) 219 return Error("unable to read segment load command"); 220 221 outs() << " ('symoff', " << SLC->SymbolTableOffset << ")\n"; 222 outs() << " ('nsyms', " << SLC->NumSymbolTableEntries << ")\n"; 223 outs() << " ('stroff', " << SLC->StringTableOffset << ")\n"; 224 outs() << " ('strsize', " << SLC->StringTableSize << ")\n"; 225 226 // Cache the string table data. 227 Obj.RegisterStringTable(*SLC); 228 229 // Dump the string data. 230 outs() << " ('_string_data', '"; 231 outs().write_escaped(Obj.getStringTableData(), 232 /*UseHexEscapes=*/true) << "')\n"; 233 234 // Dump the symbol table. 235 int Res = 0; 236 outs() << " ('_symbols', [\n"; 237 for (unsigned i = 0; i != SLC->NumSymbolTableEntries; ++i) { 238 if (Obj.is64Bit()) { 239 InMemoryStruct<macho::Symbol64TableEntry> STE; 240 Obj.ReadSymbol64TableEntry(SLC->SymbolTableOffset, i, STE); 241 if (!STE) { 242 Res = Error("unable to read symbol: '" + Twine(i) + "'"); 243 break; 244 } 245 246 DumpSymbolTableEntryData(Obj, i, STE->StringIndex, STE->Type, 247 STE->SectionIndex, STE->Flags, STE->Value); 248 } else { 249 InMemoryStruct<macho::SymbolTableEntry> STE; 250 Obj.ReadSymbolTableEntry(SLC->SymbolTableOffset, i, STE); 251 if (!SLC) { 252 Res = Error("unable to read symbol: '" + Twine(i) + "'"); 253 break; 254 } 255 256 DumpSymbolTableEntryData(Obj, i, STE->StringIndex, STE->Type, 257 STE->SectionIndex, STE->Flags, STE->Value); 258 } 259 } 260 outs() << " ])\n"; 261 262 return Res; 263} 264 265static int DumpDysymtabCommand(MachOObject &Obj, 266 const MachOObject::LoadCommandInfo &LCI) { 267 InMemoryStruct<macho::DysymtabLoadCommand> DLC; 268 Obj.ReadDysymtabLoadCommand(LCI, DLC); 269 if (!DLC) 270 return Error("unable to read segment load command"); 271 272 outs() << " ('ilocalsym', " << DLC->LocalSymbolsIndex << ")\n"; 273 outs() << " ('nlocalsym', " << DLC->NumLocalSymbols << ")\n"; 274 outs() << " ('iextdefsym', " << DLC->ExternalSymbolsIndex << ")\n"; 275 outs() << " ('nextdefsym', " << DLC->NumExternalSymbols << ")\n"; 276 outs() << " ('iundefsym', " << DLC->UndefinedSymbolsIndex << ")\n"; 277 outs() << " ('nundefsym', " << DLC->NumUndefinedSymbols << ")\n"; 278 outs() << " ('tocoff', " << DLC->TOCOffset << ")\n"; 279 outs() << " ('ntoc', " << DLC->NumTOCEntries << ")\n"; 280 outs() << " ('modtaboff', " << DLC->ModuleTableOffset << ")\n"; 281 outs() << " ('nmodtab', " << DLC->NumModuleTableEntries << ")\n"; 282 outs() << " ('extrefsymoff', " << DLC->ReferenceSymbolTableOffset << ")\n"; 283 outs() << " ('nextrefsyms', " 284 << DLC->NumReferencedSymbolTableEntries << ")\n"; 285 outs() << " ('indirectsymoff', " << DLC->IndirectSymbolTableOffset << ")\n"; 286 outs() << " ('nindirectsyms', " 287 << DLC->NumIndirectSymbolTableEntries << ")\n"; 288 outs() << " ('extreloff', " << DLC->ExternalRelocationTableOffset << ")\n"; 289 outs() << " ('nextrel', " << DLC->NumExternalRelocationTableEntries << ")\n"; 290 outs() << " ('locreloff', " << DLC->LocalRelocationTableOffset << ")\n"; 291 outs() << " ('nlocrel', " << DLC->NumLocalRelocationTableEntries << ")\n"; 292 293 // Dump the indirect symbol table. 294 int Res = 0; 295 outs() << " ('_indirect_symbols', [\n"; 296 for (unsigned i = 0; i != DLC->NumIndirectSymbolTableEntries; ++i) { 297 InMemoryStruct<macho::IndirectSymbolTableEntry> ISTE; 298 Obj.ReadIndirectSymbolTableEntry(*DLC, i, ISTE); 299 if (!ISTE) { 300 Res = Error("unable to read segment load command"); 301 break; 302 } 303 304 outs() << " # Indirect Symbol " << i << "\n"; 305 outs() << " (('symbol_index', " 306 << format("0x%x", ISTE->Index) << "),),\n"; 307 } 308 outs() << " ])\n"; 309 310 return Res; 311} 312 313static int DumpLinkeditDataCommand(MachOObject &Obj, 314 const MachOObject::LoadCommandInfo &LCI) { 315 InMemoryStruct<macho::LinkeditDataLoadCommand> LLC; 316 Obj.ReadLinkeditDataLoadCommand(LCI, LLC); 317 if (!LLC) 318 return Error("unable to read segment load command"); 319 320 outs() << " ('dataoff', " << LLC->DataOffset << ")\n" 321 << " ('datasize', " << LLC->DataSize << ")\n" 322 << " ('_addresses', [\n"; 323 324 SmallVector<uint64_t, 8> Addresses; 325 Obj.ReadULEB128s(LLC->DataOffset, Addresses); 326 for (unsigned i = 0, e = Addresses.size(); i != e; ++i) 327 outs() << " # Address " << i << '\n' 328 << " ('address', " << format("0x%x", Addresses[i]) << "),\n"; 329 330 outs() << " ])\n"; 331 332 return 0; 333} 334 335static int DumpDataInCodeDataCommand(MachOObject &Obj, 336 const MachOObject::LoadCommandInfo &LCI) { 337 InMemoryStruct<macho::LinkeditDataLoadCommand> LLC; 338 Obj.ReadLinkeditDataLoadCommand(LCI, LLC); 339 if (!LLC) 340 return Error("unable to read segment load command"); 341 342 outs() << " ('dataoff', " << LLC->DataOffset << ")\n" 343 << " ('datasize', " << LLC->DataSize << ")\n" 344 << " ('_data_regions', [\n"; 345 346 347 unsigned NumRegions = LLC->DataSize / 8; 348 for (unsigned i = 0; i < NumRegions; ++i) { 349 InMemoryStruct<macho::DataInCodeTableEntry> DICE; 350 Obj.ReadDataInCodeTableEntry(LLC->DataOffset, i, DICE); 351 if (!DICE) 352 return Error("unable to read DataInCodeTableEntry"); 353 outs() << " # DICE " << i << "\n" 354 << " ('offset', " << DICE->Offset << ")\n" 355 << " ('length', " << DICE->Length << ")\n" 356 << " ('kind', " << DICE->Kind << ")\n"; 357 } 358 359 outs() <<" ])\n"; 360 361 return 0; 362} 363 364 365static int DumpLoadCommand(MachOObject &Obj, unsigned Index) { 366 const MachOObject::LoadCommandInfo &LCI = Obj.getLoadCommandInfo(Index); 367 int Res = 0; 368 369 outs() << " # Load Command " << Index << "\n" 370 << " (('command', " << LCI.Command.Type << ")\n" 371 << " ('size', " << LCI.Command.Size << ")\n"; 372 switch (LCI.Command.Type) { 373 case macho::LCT_Segment: 374 Res = DumpSegmentCommand(Obj, LCI); 375 break; 376 case macho::LCT_Segment64: 377 Res = DumpSegment64Command(Obj, LCI); 378 break; 379 case macho::LCT_Symtab: 380 Res = DumpSymtabCommand(Obj, LCI); 381 break; 382 case macho::LCT_Dysymtab: 383 Res = DumpDysymtabCommand(Obj, LCI); 384 break; 385 case macho::LCT_CodeSignature: 386 case macho::LCT_SegmentSplitInfo: 387 case macho::LCT_FunctionStarts: 388 Res = DumpLinkeditDataCommand(Obj, LCI); 389 break; 390 case macho::LCT_DataInCode: 391 Res = DumpDataInCodeDataCommand(Obj, LCI); 392 break; 393 default: 394 Warning("unknown load command: " + Twine(LCI.Command.Type)); 395 break; 396 } 397 outs() << " ),\n"; 398 399 return Res; 400} 401 402int main(int argc, char **argv) { 403 ProgramName = argv[0]; 404 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 405 406 cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n"); 407 408 // Load the input file. 409 std::string ErrorStr; 410 OwningPtr<MemoryBuffer> InputBuffer; 411 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFile, InputBuffer)) 412 return Error("unable to read input: '" + ec.message() + "'"); 413 414 // Construct the Mach-O wrapper object. 415 OwningPtr<MachOObject> InputObject( 416 MachOObject::LoadFromBuffer(InputBuffer.take(), &ErrorStr)); 417 if (!InputObject) 418 return Error("unable to load object: '" + ErrorStr + "'"); 419 420 // Print the header 421 InputObject->printHeader(outs()); 422 423 // Print the load commands. 424 int Res = 0; 425 outs() << "('load_commands', [\n"; 426 for (unsigned i = 0; i != InputObject->getHeader().NumLoadCommands; ++i) 427 if ((Res = DumpLoadCommand(*InputObject, i))) 428 break; 429 outs() << "])\n"; 430 431 return Res; 432} 433