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" 23 using namespace llvm; 24 using namespace llvm::object; 25 26 static cl::opt<std::string> 27 InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-")); 28 29 static cl::opt<bool> 30 ShowSectionData("dump-section-data", cl::desc("Dump the contents of sections"), 31 cl::init(false)); 32 33 /// 34 35 static const char *ProgramName; 36 37 static void Message(const char *Type, const Twine &Msg) { 38 errs() << ProgramName << ": " << Type << ": " << Msg << "\n"; 39 } 40 41 static int Error(const Twine &Msg) { 42 Message("error", Msg); 43 return 1; 44 } 45 46 static void Warning(const Twine &Msg) { 47 Message("warning", Msg); 48 } 49 50 /// 51 52 static 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 69 static 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 127 static 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 163 static 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 0; 198 } 199 200 static 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 214 static 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 265 static 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 313 static 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 335 336 static int DumpLoadCommand(MachOObject &Obj, unsigned Index) { 337 const MachOObject::LoadCommandInfo &LCI = Obj.getLoadCommandInfo(Index); 338 int Res = 0; 339 340 outs() << " # Load Command " << Index << "\n" 341 << " (('command', " << LCI.Command.Type << ")\n" 342 << " ('size', " << LCI.Command.Size << ")\n"; 343 switch (LCI.Command.Type) { 344 case macho::LCT_Segment: 345 Res = DumpSegmentCommand(Obj, LCI); 346 break; 347 case macho::LCT_Segment64: 348 Res = DumpSegment64Command(Obj, LCI); 349 break; 350 case macho::LCT_Symtab: 351 Res = DumpSymtabCommand(Obj, LCI); 352 break; 353 case macho::LCT_Dysymtab: 354 Res = DumpDysymtabCommand(Obj, LCI); 355 break; 356 case macho::LCT_CodeSignature: 357 case macho::LCT_SegmentSplitInfo: 358 case macho::LCT_FunctionStarts: 359 Res = DumpLinkeditDataCommand(Obj, LCI); 360 break; 361 default: 362 Warning("unknown load command: " + Twine(LCI.Command.Type)); 363 break; 364 } 365 outs() << " ),\n"; 366 367 return Res; 368 } 369 370 int main(int argc, char **argv) { 371 ProgramName = argv[0]; 372 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 373 374 cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n"); 375 376 // Load the input file. 377 std::string ErrorStr; 378 OwningPtr<MemoryBuffer> InputBuffer; 379 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFile, InputBuffer)) 380 return Error("unable to read input: '" + ec.message() + "'"); 381 382 // Construct the Mach-O wrapper object. 383 OwningPtr<MachOObject> InputObject( 384 MachOObject::LoadFromBuffer(InputBuffer.take(), &ErrorStr)); 385 if (!InputObject) 386 return Error("unable to load object: '" + ErrorStr + "'"); 387 388 // Print the header 389 InputObject->printHeader(outs()); 390 391 // Print the load commands. 392 int Res = 0; 393 outs() << "('load_commands', [\n"; 394 for (unsigned i = 0; i != InputObject->getHeader().NumLoadCommands; ++i) 395 if ((Res = DumpLoadCommand(*InputObject, i))) 396 break; 397 outs() << "])\n"; 398 399 return Res; 400 } 401