macho-dump.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. #include "llvm/Object/MachO.h"
  14. #include "llvm/ADT/StringExtras.h"
  15. #include "llvm/ADT/Twine.h"
  16. #include "llvm/Support/Casting.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 <system_error>
  23. using namespace llvm;
  24. using namespace llvm::object;
  25. static cl::opt<std::string>
  26. InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-"));
  27. static cl::opt<bool>
  28. ShowSectionData("dump-section-data", cl::desc("Dump the contents of sections"),
  29. cl::init(false));
  30. ///
  31. static const char *ProgramName;
  32. static void Message(const char *Type, const Twine &Msg) {
  33. errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
  34. }
  35. static int Error(const Twine &Msg) {
  36. Message("error", Msg);
  37. return 1;
  38. }
  39. static void Warning(const Twine &Msg) {
  40. Message("warning", Msg);
  41. }
  42. ///
  43. static void DumpSegmentCommandData(StringRef Name,
  44. uint64_t VMAddr, uint64_t VMSize,
  45. uint64_t FileOffset, uint64_t FileSize,
  46. uint32_t MaxProt, uint32_t InitProt,
  47. uint32_t NumSections, uint32_t Flags) {
  48. outs() << " ('segment_name', '";
  49. outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n";
  50. outs() << " ('vm_addr', " << VMAddr << ")\n";
  51. outs() << " ('vm_size', " << VMSize << ")\n";
  52. outs() << " ('file_offset', " << FileOffset << ")\n";
  53. outs() << " ('file_size', " << FileSize << ")\n";
  54. outs() << " ('maxprot', " << MaxProt << ")\n";
  55. outs() << " ('initprot', " << InitProt << ")\n";
  56. outs() << " ('num_sections', " << NumSections << ")\n";
  57. outs() << " ('flags', " << Flags << ")\n";
  58. }
  59. static int DumpSectionData(const MachOObjectFile &Obj, unsigned Index,
  60. StringRef Name,
  61. StringRef SegmentName, uint64_t Address,
  62. uint64_t Size, uint32_t Offset,
  63. uint32_t Align, uint32_t RelocationTableOffset,
  64. uint32_t NumRelocationTableEntries,
  65. uint32_t Flags, uint32_t Reserved1,
  66. uint32_t Reserved2, uint64_t Reserved3 = ~0ULL) {
  67. outs() << " # Section " << Index << "\n";
  68. outs() << " (('section_name', '";
  69. outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n";
  70. outs() << " ('segment_name', '";
  71. outs().write_escaped(SegmentName, /*UseHexEscapes=*/true) << "')\n";
  72. outs() << " ('address', " << Address << ")\n";
  73. outs() << " ('size', " << Size << ")\n";
  74. outs() << " ('offset', " << Offset << ")\n";
  75. outs() << " ('alignment', " << Align << ")\n";
  76. outs() << " ('reloc_offset', " << RelocationTableOffset << ")\n";
  77. outs() << " ('num_reloc', " << NumRelocationTableEntries << ")\n";
  78. outs() << " ('flags', " << format("0x%x", Flags) << ")\n";
  79. outs() << " ('reserved1', " << Reserved1 << ")\n";
  80. outs() << " ('reserved2', " << Reserved2 << ")\n";
  81. if (Reserved3 != ~0ULL)
  82. outs() << " ('reserved3', " << Reserved3 << ")\n";
  83. outs() << " ),\n";
  84. // Dump the relocation entries.
  85. outs() << " ('_relocations', [\n";
  86. unsigned RelNum = 0;
  87. for (relocation_iterator I = Obj.section_rel_begin(Index),
  88. E = Obj.section_rel_end(Index);
  89. I != E; ++I, ++RelNum) {
  90. MachO::any_relocation_info RE = Obj.getRelocation(I->getRawDataRefImpl());
  91. outs() << " # Relocation " << RelNum << "\n";
  92. outs() << " (('word-0', " << format("0x%x", RE.r_word0) << "),\n";
  93. outs() << " ('word-1', " << format("0x%x", RE.r_word1) << ")),\n";
  94. }
  95. outs() << " ])\n";
  96. // Dump the section data, if requested.
  97. if (ShowSectionData) {
  98. outs() << " ('_section_data', '";
  99. StringRef Data = Obj.getData().substr(Offset, Size);
  100. for (unsigned i = 0; i != Data.size(); ++i) {
  101. if (i && (i % 4) == 0)
  102. outs() << ' ';
  103. outs() << hexdigit((Data[i] >> 4) & 0xF, /*LowerCase=*/true);
  104. outs() << hexdigit((Data[i] >> 0) & 0xF, /*LowerCase=*/true);
  105. }
  106. outs() << "')\n";
  107. }
  108. return 0;
  109. }
  110. static int DumpSegmentCommand(const MachOObjectFile &Obj,
  111. const MachOObjectFile::LoadCommandInfo &LCI) {
  112. MachO::segment_command SLC = Obj.getSegmentLoadCommand(LCI);
  113. DumpSegmentCommandData(StringRef(SLC.segname, 16), SLC.vmaddr,
  114. SLC.vmsize, SLC.fileoff, SLC.filesize,
  115. SLC.maxprot, SLC.initprot, SLC.nsects, SLC.flags);
  116. // Dump the sections.
  117. outs() << " ('sections', [\n";
  118. for (unsigned i = 0; i != SLC.nsects; ++i) {
  119. MachO::section Sect = Obj.getSection(LCI, i);
  120. DumpSectionData(Obj, i, StringRef(Sect.sectname, 16),
  121. StringRef(Sect.segname, 16), Sect.addr,
  122. Sect.size, Sect.offset, Sect.align,
  123. Sect.reloff, Sect.nreloc, Sect.flags,
  124. Sect.reserved1, Sect.reserved2);
  125. }
  126. outs() << " ])\n";
  127. return 0;
  128. }
  129. static int DumpSegment64Command(const MachOObjectFile &Obj,
  130. const MachOObjectFile::LoadCommandInfo &LCI) {
  131. MachO::segment_command_64 SLC = Obj.getSegment64LoadCommand(LCI);
  132. DumpSegmentCommandData(StringRef(SLC.segname, 16), SLC.vmaddr,
  133. SLC.vmsize, SLC.fileoff, SLC.filesize,
  134. SLC.maxprot, SLC.initprot, SLC.nsects, SLC.flags);
  135. // Dump the sections.
  136. outs() << " ('sections', [\n";
  137. for (unsigned i = 0; i != SLC.nsects; ++i) {
  138. MachO::section_64 Sect = Obj.getSection64(LCI, i);
  139. DumpSectionData(Obj, i, StringRef(Sect.sectname, 16),
  140. StringRef(Sect.segname, 16), Sect.addr,
  141. Sect.size, Sect.offset, Sect.align,
  142. Sect.reloff, Sect.nreloc, Sect.flags,
  143. Sect.reserved1, Sect.reserved2,
  144. Sect.reserved3);
  145. }
  146. outs() << " ])\n";
  147. return 0;
  148. }
  149. static void DumpSymbolTableEntryData(const MachOObjectFile &Obj,
  150. unsigned Index, uint32_t StringIndex,
  151. uint8_t Type, uint8_t SectionIndex,
  152. uint16_t Flags, uint64_t Value,
  153. StringRef StringTable) {
  154. const char *Name = &StringTable.data()[StringIndex];
  155. outs() << " # Symbol " << Index << "\n";
  156. outs() << " (('n_strx', " << StringIndex << ")\n";
  157. outs() << " ('n_type', " << format("0x%x", Type) << ")\n";
  158. outs() << " ('n_sect', " << uint32_t(SectionIndex) << ")\n";
  159. outs() << " ('n_desc', " << Flags << ")\n";
  160. outs() << " ('n_value', " << Value << ")\n";
  161. outs() << " ('_string', '" << Name << "')\n";
  162. outs() << " ),\n";
  163. }
  164. static int DumpSymtabCommand(const MachOObjectFile &Obj) {
  165. MachO::symtab_command SLC = Obj.getSymtabLoadCommand();
  166. outs() << " ('symoff', " << SLC.symoff << ")\n";
  167. outs() << " ('nsyms', " << SLC.nsyms << ")\n";
  168. outs() << " ('stroff', " << SLC.stroff << ")\n";
  169. outs() << " ('strsize', " << SLC.strsize << ")\n";
  170. // Dump the string data.
  171. outs() << " ('_string_data', '";
  172. StringRef StringTable = Obj.getStringTableData();
  173. outs().write_escaped(StringTable,
  174. /*UseHexEscapes=*/true) << "')\n";
  175. // Dump the symbol table.
  176. outs() << " ('_symbols', [\n";
  177. unsigned SymNum = 0;
  178. for (const SymbolRef &Symbol : Obj.symbols()) {
  179. DataRefImpl DRI = Symbol.getRawDataRefImpl();
  180. if (Obj.is64Bit()) {
  181. MachO::nlist_64 STE = Obj.getSymbol64TableEntry(DRI);
  182. DumpSymbolTableEntryData(Obj, SymNum, STE.n_strx, STE.n_type,
  183. STE.n_sect, STE.n_desc, STE.n_value,
  184. StringTable);
  185. } else {
  186. MachO::nlist STE = Obj.getSymbolTableEntry(DRI);
  187. DumpSymbolTableEntryData(Obj, SymNum, STE.n_strx, STE.n_type,
  188. STE.n_sect, STE.n_desc, STE.n_value,
  189. StringTable);
  190. }
  191. SymNum++;
  192. }
  193. outs() << " ])\n";
  194. return 0;
  195. }
  196. static int DumpDysymtabCommand(const MachOObjectFile &Obj) {
  197. MachO::dysymtab_command DLC = Obj.getDysymtabLoadCommand();
  198. outs() << " ('ilocalsym', " << DLC.ilocalsym << ")\n";
  199. outs() << " ('nlocalsym', " << DLC.nlocalsym << ")\n";
  200. outs() << " ('iextdefsym', " << DLC.iextdefsym << ")\n";
  201. outs() << " ('nextdefsym', " << DLC.nextdefsym << ")\n";
  202. outs() << " ('iundefsym', " << DLC.iundefsym << ")\n";
  203. outs() << " ('nundefsym', " << DLC.nundefsym << ")\n";
  204. outs() << " ('tocoff', " << DLC.tocoff << ")\n";
  205. outs() << " ('ntoc', " << DLC.ntoc << ")\n";
  206. outs() << " ('modtaboff', " << DLC.modtaboff << ")\n";
  207. outs() << " ('nmodtab', " << DLC.nmodtab << ")\n";
  208. outs() << " ('extrefsymoff', " << DLC.extrefsymoff << ")\n";
  209. outs() << " ('nextrefsyms', " << DLC.nextrefsyms << ")\n";
  210. outs() << " ('indirectsymoff', " << DLC.indirectsymoff << ")\n";
  211. outs() << " ('nindirectsyms', " << DLC.nindirectsyms << ")\n";
  212. outs() << " ('extreloff', " << DLC.extreloff << ")\n";
  213. outs() << " ('nextrel', " << DLC.nextrel << ")\n";
  214. outs() << " ('locreloff', " << DLC.locreloff << ")\n";
  215. outs() << " ('nlocrel', " << DLC.nlocrel << ")\n";
  216. // Dump the indirect symbol table.
  217. outs() << " ('_indirect_symbols', [\n";
  218. for (unsigned i = 0; i != DLC.nindirectsyms; ++i) {
  219. uint32_t ISTE = Obj.getIndirectSymbolTableEntry(DLC, i);
  220. outs() << " # Indirect Symbol " << i << "\n";
  221. outs() << " (('symbol_index', " << format("0x%x", ISTE) << "),),\n";
  222. }
  223. outs() << " ])\n";
  224. return 0;
  225. }
  226. static int
  227. DumpLinkeditDataCommand(const MachOObjectFile &Obj,
  228. const MachOObjectFile::LoadCommandInfo &LCI) {
  229. MachO::linkedit_data_command LLC = Obj.getLinkeditDataLoadCommand(LCI);
  230. outs() << " ('dataoff', " << LLC.dataoff << ")\n"
  231. << " ('datasize', " << LLC.datasize << ")\n"
  232. << " ('_addresses', [\n";
  233. SmallVector<uint64_t, 8> Addresses;
  234. Obj.ReadULEB128s(LLC.dataoff, Addresses);
  235. for (unsigned i = 0, e = Addresses.size(); i != e; ++i)
  236. outs() << " # Address " << i << '\n'
  237. << " ('address', " << format("0x%x", Addresses[i]) << "),\n";
  238. outs() << " ])\n";
  239. return 0;
  240. }
  241. static int
  242. DumpDataInCodeDataCommand(const MachOObjectFile &Obj,
  243. const MachOObjectFile::LoadCommandInfo &LCI) {
  244. MachO::linkedit_data_command LLC = Obj.getLinkeditDataLoadCommand(LCI);
  245. outs() << " ('dataoff', " << LLC.dataoff << ")\n"
  246. << " ('datasize', " << LLC.datasize << ")\n"
  247. << " ('_data_regions', [\n";
  248. unsigned NumRegions = LLC.datasize / sizeof(MachO::data_in_code_entry);
  249. for (unsigned i = 0; i < NumRegions; ++i) {
  250. MachO::data_in_code_entry DICE= Obj.getDataInCodeTableEntry(LLC.dataoff, i);
  251. outs() << " # DICE " << i << "\n"
  252. << " ('offset', " << DICE.offset << ")\n"
  253. << " ('length', " << DICE.length << ")\n"
  254. << " ('kind', " << DICE.kind << ")\n";
  255. }
  256. outs() <<" ])\n";
  257. return 0;
  258. }
  259. static int
  260. DumpLinkerOptionsCommand(const MachOObjectFile &Obj,
  261. const MachOObjectFile::LoadCommandInfo &LCI) {
  262. MachO::linker_option_command LOLC = Obj.getLinkerOptionLoadCommand(LCI);
  263. outs() << " ('count', " << LOLC.count << ")\n"
  264. << " ('_strings', [\n";
  265. uint64_t DataSize = LOLC.cmdsize - sizeof(MachO::linker_option_command);
  266. const char *P = LCI.Ptr + sizeof(MachO::linker_option_command);
  267. StringRef Data(P, DataSize);
  268. for (unsigned i = 0; i != LOLC.count; ++i) {
  269. std::pair<StringRef,StringRef> Split = Data.split('\0');
  270. outs() << "\t\"";
  271. outs().write_escaped(Split.first);
  272. outs() << "\",\n";
  273. Data = Split.second;
  274. }
  275. outs() <<" ])\n";
  276. return 0;
  277. }
  278. static int
  279. DumpVersionMin(const MachOObjectFile &Obj,
  280. const MachOObjectFile::LoadCommandInfo &LCI) {
  281. MachO::version_min_command VMLC = Obj.getVersionMinLoadCommand(LCI);
  282. outs() << " ('version, " << VMLC.version << ")\n"
  283. << " ('sdk, " << VMLC.sdk << ")\n";
  284. return 0;
  285. }
  286. static int
  287. DumpDylibID(const MachOObjectFile &Obj,
  288. const MachOObjectFile::LoadCommandInfo &LCI) {
  289. MachO::dylib_command DLLC = Obj.getDylibIDLoadCommand(LCI);
  290. outs() << " ('install_name', '" << LCI.Ptr + DLLC.dylib.name << "')\n"
  291. << " ('timestamp, " << DLLC.dylib.timestamp << ")\n"
  292. << " ('cur_version, " << DLLC.dylib.current_version << ")\n"
  293. << " ('compat_version, " << DLLC.dylib.compatibility_version << ")\n";
  294. return 0;
  295. }
  296. static int DumpLoadCommand(const MachOObjectFile &Obj,
  297. const MachOObjectFile::LoadCommandInfo &LCI) {
  298. switch (LCI.C.cmd) {
  299. case MachO::LC_SEGMENT:
  300. return DumpSegmentCommand(Obj, LCI);
  301. case MachO::LC_SEGMENT_64:
  302. return DumpSegment64Command(Obj, LCI);
  303. case MachO::LC_SYMTAB:
  304. return DumpSymtabCommand(Obj);
  305. case MachO::LC_DYSYMTAB:
  306. return DumpDysymtabCommand(Obj);
  307. case MachO::LC_CODE_SIGNATURE:
  308. case MachO::LC_SEGMENT_SPLIT_INFO:
  309. case MachO::LC_FUNCTION_STARTS:
  310. return DumpLinkeditDataCommand(Obj, LCI);
  311. case MachO::LC_DATA_IN_CODE:
  312. return DumpDataInCodeDataCommand(Obj, LCI);
  313. case MachO::LC_LINKER_OPTION:
  314. return DumpLinkerOptionsCommand(Obj, LCI);
  315. case MachO::LC_VERSION_MIN_IPHONEOS:
  316. case MachO::LC_VERSION_MIN_MACOSX:
  317. return DumpVersionMin(Obj, LCI);
  318. case MachO::LC_ID_DYLIB:
  319. return DumpDylibID(Obj, LCI);
  320. default:
  321. Warning("unknown load command: " + Twine(LCI.C.cmd));
  322. return 0;
  323. }
  324. }
  325. static int DumpLoadCommand(const MachOObjectFile &Obj, unsigned Index,
  326. const MachOObjectFile::LoadCommandInfo &LCI) {
  327. outs() << " # Load Command " << Index << "\n"
  328. << " (('command', " << LCI.C.cmd << ")\n"
  329. << " ('size', " << LCI.C.cmdsize << ")\n";
  330. int Res = DumpLoadCommand(Obj, LCI);
  331. outs() << " ),\n";
  332. return Res;
  333. }
  334. static void printHeader(const MachOObjectFile *Obj,
  335. const MachO::mach_header &Header) {
  336. outs() << "('cputype', " << Header.cputype << ")\n";
  337. outs() << "('cpusubtype', " << Header.cpusubtype << ")\n";
  338. outs() << "('filetype', " << Header.filetype << ")\n";
  339. outs() << "('num_load_commands', " << Header.ncmds << ")\n";
  340. outs() << "('load_commands_size', " << Header.sizeofcmds << ")\n";
  341. outs() << "('flag', " << Header.flags << ")\n";
  342. // Print extended header if 64-bit.
  343. if (Obj->is64Bit()) {
  344. const MachO::mach_header_64 *Header64 =
  345. reinterpret_cast<const MachO::mach_header_64 *>(&Header);
  346. outs() << "('reserved', " << Header64->reserved << ")\n";
  347. }
  348. }
  349. // HLSL Change: changed calling convention to __cdecl
  350. int __cdecl main(int argc, char **argv) {
  351. ProgramName = argv[0];
  352. llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
  353. cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");
  354. ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(InputFile);
  355. if (std::error_code EC = BinaryOrErr.getError())
  356. return Error("unable to read input: '" + EC.message() + "'");
  357. Binary &Binary = *BinaryOrErr.get().getBinary();
  358. const MachOObjectFile *InputObject = dyn_cast<MachOObjectFile>(&Binary);
  359. if (!InputObject)
  360. return Error("Not a MachO object");
  361. // Print the header
  362. MachO::mach_header_64 Header64;
  363. MachO::mach_header *Header = reinterpret_cast<MachO::mach_header*>(&Header64);
  364. if (InputObject->is64Bit())
  365. Header64 = InputObject->getHeader64();
  366. else
  367. *Header = InputObject->getHeader();
  368. printHeader(InputObject, *Header);
  369. // Print the load commands.
  370. int Res = 0;
  371. unsigned Index = 0;
  372. outs() << "('load_commands', [\n";
  373. for (const auto &Load : InputObject->load_commands()) {
  374. if (DumpLoadCommand(*InputObject, Index++, Load))
  375. break;
  376. }
  377. outs() << "])\n";
  378. return Res;
  379. }