llvm-cxxdump.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. //===- llvm-cxxdump.cpp - Dump C++ data in an Object File -------*- C++ -*-===//
  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. // Dumps C++ data resident in object files and archives.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm-cxxdump.h"
  14. #include "Error.h"
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/Object/Archive.h"
  17. #include "llvm/Object/ObjectFile.h"
  18. #include "llvm/Support/Debug.h"
  19. #include "llvm/Support/Endian.h"
  20. #include "llvm/Support/FileSystem.h"
  21. #include "llvm/Support/ManagedStatic.h"
  22. #include "llvm/Support/PrettyStackTrace.h"
  23. #include "llvm/Support/Signals.h"
  24. #include "llvm/Support/TargetRegistry.h"
  25. #include "llvm/Support/TargetSelect.h"
  26. #include "llvm/Support/raw_ostream.h"
  27. #include <map>
  28. #include <string>
  29. #include <system_error>
  30. using namespace llvm;
  31. using namespace llvm::object;
  32. using namespace llvm::support;
  33. namespace opts {
  34. cl::list<std::string> InputFilenames(cl::Positional,
  35. cl::desc("<input object files>"),
  36. cl::ZeroOrMore);
  37. } // namespace opts
  38. static int ReturnValue = EXIT_SUCCESS;
  39. namespace llvm {
  40. static bool error(std::error_code EC) {
  41. if (!EC)
  42. return false;
  43. ReturnValue = EXIT_FAILURE;
  44. outs() << "\nError reading file: " << EC.message() << ".\n";
  45. outs().flush();
  46. return true;
  47. }
  48. } // namespace llvm
  49. static void reportError(StringRef Input, StringRef Message) {
  50. if (Input == "-")
  51. Input = "<stdin>";
  52. errs() << Input << ": " << Message << "\n";
  53. errs().flush();
  54. ReturnValue = EXIT_FAILURE;
  55. }
  56. static void reportError(StringRef Input, std::error_code EC) {
  57. reportError(Input, EC.message());
  58. }
  59. static SmallVectorImpl<SectionRef> &getRelocSections(const ObjectFile *Obj,
  60. const SectionRef &Sec) {
  61. static bool MappingDone = false;
  62. static std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
  63. if (!MappingDone) {
  64. for (const SectionRef &Section : Obj->sections()) {
  65. section_iterator Sec2 = Section.getRelocatedSection();
  66. if (Sec2 != Obj->section_end())
  67. SectionRelocMap[*Sec2].push_back(Section);
  68. }
  69. MappingDone = true;
  70. }
  71. return SectionRelocMap[Sec];
  72. }
  73. static bool collectRelocatedSymbols(const ObjectFile *Obj,
  74. const SectionRef &Sec, uint64_t SecAddress,
  75. uint64_t SymAddress, uint64_t SymSize,
  76. StringRef *I, StringRef *E) {
  77. uint64_t SymOffset = SymAddress - SecAddress;
  78. uint64_t SymEnd = SymOffset + SymSize;
  79. for (const SectionRef &SR : getRelocSections(Obj, Sec)) {
  80. for (const object::RelocationRef &Reloc : SR.relocations()) {
  81. if (I == E)
  82. break;
  83. const object::symbol_iterator RelocSymI = Reloc.getSymbol();
  84. if (RelocSymI == Obj->symbol_end())
  85. continue;
  86. StringRef RelocSymName;
  87. if (error(RelocSymI->getName(RelocSymName)))
  88. return true;
  89. uint64_t Offset;
  90. if (error(Reloc.getOffset(Offset)))
  91. return true;
  92. if (Offset >= SymOffset && Offset < SymEnd) {
  93. *I = RelocSymName;
  94. ++I;
  95. }
  96. }
  97. }
  98. return false;
  99. }
  100. static bool collectRelocationOffsets(
  101. const ObjectFile *Obj, const SectionRef &Sec, uint64_t SecAddress,
  102. uint64_t SymAddress, uint64_t SymSize, StringRef SymName,
  103. std::map<std::pair<StringRef, uint64_t>, StringRef> &Collection) {
  104. uint64_t SymOffset = SymAddress - SecAddress;
  105. uint64_t SymEnd = SymOffset + SymSize;
  106. for (const SectionRef &SR : getRelocSections(Obj, Sec)) {
  107. for (const object::RelocationRef &Reloc : SR.relocations()) {
  108. const object::symbol_iterator RelocSymI = Reloc.getSymbol();
  109. if (RelocSymI == Obj->symbol_end())
  110. continue;
  111. StringRef RelocSymName;
  112. if (error(RelocSymI->getName(RelocSymName)))
  113. return true;
  114. uint64_t Offset;
  115. if (error(Reloc.getOffset(Offset)))
  116. return true;
  117. if (Offset >= SymOffset && Offset < SymEnd)
  118. Collection[std::make_pair(SymName, Offset - SymOffset)] = RelocSymName;
  119. }
  120. }
  121. return false;
  122. }
  123. static void dumpCXXData(const ObjectFile *Obj) {
  124. struct CompleteObjectLocator {
  125. StringRef Symbols[2];
  126. ArrayRef<little32_t> Data;
  127. };
  128. struct ClassHierarchyDescriptor {
  129. StringRef Symbols[1];
  130. ArrayRef<little32_t> Data;
  131. };
  132. struct BaseClassDescriptor {
  133. StringRef Symbols[2];
  134. ArrayRef<little32_t> Data;
  135. };
  136. struct TypeDescriptor {
  137. StringRef Symbols[1];
  138. uint64_t AlwaysZero;
  139. StringRef MangledName;
  140. };
  141. struct ThrowInfo {
  142. uint32_t Flags;
  143. };
  144. struct CatchableTypeArray {
  145. uint32_t NumEntries;
  146. };
  147. struct CatchableType {
  148. uint32_t Flags;
  149. uint32_t NonVirtualBaseAdjustmentOffset;
  150. int32_t VirtualBasePointerOffset;
  151. uint32_t VirtualBaseAdjustmentOffset;
  152. uint32_t Size;
  153. StringRef Symbols[2];
  154. };
  155. std::map<std::pair<StringRef, uint64_t>, StringRef> VFTableEntries;
  156. std::map<std::pair<StringRef, uint64_t>, StringRef> TIEntries;
  157. std::map<std::pair<StringRef, uint64_t>, StringRef> CTAEntries;
  158. std::map<StringRef, ArrayRef<little32_t>> VBTables;
  159. std::map<StringRef, CompleteObjectLocator> COLs;
  160. std::map<StringRef, ClassHierarchyDescriptor> CHDs;
  161. std::map<std::pair<StringRef, uint64_t>, StringRef> BCAEntries;
  162. std::map<StringRef, BaseClassDescriptor> BCDs;
  163. std::map<StringRef, TypeDescriptor> TDs;
  164. std::map<StringRef, ThrowInfo> TIs;
  165. std::map<StringRef, CatchableTypeArray> CTAs;
  166. std::map<StringRef, CatchableType> CTs;
  167. std::map<std::pair<StringRef, uint64_t>, StringRef> VTableSymEntries;
  168. std::map<std::pair<StringRef, uint64_t>, int64_t> VTableDataEntries;
  169. std::map<std::pair<StringRef, uint64_t>, StringRef> VTTEntries;
  170. std::map<StringRef, StringRef> TINames;
  171. uint8_t BytesInAddress = Obj->getBytesInAddress();
  172. for (const object::SymbolRef &Sym : Obj->symbols()) {
  173. StringRef SymName;
  174. if (error(Sym.getName(SymName)))
  175. return;
  176. object::section_iterator SecI(Obj->section_begin());
  177. if (error(Sym.getSection(SecI)))
  178. return;
  179. // Skip external symbols.
  180. if (SecI == Obj->section_end())
  181. continue;
  182. const SectionRef &Sec = *SecI;
  183. // Skip virtual or BSS sections.
  184. if (Sec.isBSS() || Sec.isVirtual())
  185. continue;
  186. StringRef SecContents;
  187. if (error(Sec.getContents(SecContents)))
  188. return;
  189. uint64_t SymAddress;
  190. if (error(Sym.getAddress(SymAddress)))
  191. return;
  192. uint64_t SymSize = Sym.getSize();
  193. uint64_t SecAddress = Sec.getAddress();
  194. uint64_t SecSize = Sec.getSize();
  195. uint64_t SymOffset = SymAddress - SecAddress;
  196. StringRef SymContents = SecContents.substr(SymOffset, SymSize);
  197. // VFTables in the MS-ABI start with '??_7' and are contained within their
  198. // own COMDAT section. We then determine the contents of the VFTable by
  199. // looking at each relocation in the section.
  200. if (SymName.startswith("??_7")) {
  201. // Each relocation either names a virtual method or a thunk. We note the
  202. // offset into the section and the symbol used for the relocation.
  203. collectRelocationOffsets(Obj, Sec, SecAddress, SecAddress, SecSize,
  204. SymName, VFTableEntries);
  205. }
  206. // VBTables in the MS-ABI start with '??_8' and are filled with 32-bit
  207. // offsets of virtual bases.
  208. else if (SymName.startswith("??_8")) {
  209. ArrayRef<little32_t> VBTableData(
  210. reinterpret_cast<const little32_t *>(SymContents.data()),
  211. SymContents.size() / sizeof(little32_t));
  212. VBTables[SymName] = VBTableData;
  213. }
  214. // Complete object locators in the MS-ABI start with '??_R4'
  215. else if (SymName.startswith("??_R4")) {
  216. CompleteObjectLocator COL;
  217. COL.Data = ArrayRef<little32_t>(
  218. reinterpret_cast<const little32_t *>(SymContents.data()), 3);
  219. StringRef *I = std::begin(COL.Symbols), *E = std::end(COL.Symbols);
  220. if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
  221. E))
  222. return;
  223. COLs[SymName] = COL;
  224. }
  225. // Class hierarchy descriptors in the MS-ABI start with '??_R3'
  226. else if (SymName.startswith("??_R3")) {
  227. ClassHierarchyDescriptor CHD;
  228. CHD.Data = ArrayRef<little32_t>(
  229. reinterpret_cast<const little32_t *>(SymContents.data()), 3);
  230. StringRef *I = std::begin(CHD.Symbols), *E = std::end(CHD.Symbols);
  231. if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
  232. E))
  233. return;
  234. CHDs[SymName] = CHD;
  235. }
  236. // Class hierarchy descriptors in the MS-ABI start with '??_R2'
  237. else if (SymName.startswith("??_R2")) {
  238. // Each relocation names a base class descriptor. We note the offset into
  239. // the section and the symbol used for the relocation.
  240. collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
  241. SymName, BCAEntries);
  242. }
  243. // Base class descriptors in the MS-ABI start with '??_R1'
  244. else if (SymName.startswith("??_R1")) {
  245. BaseClassDescriptor BCD;
  246. BCD.Data = ArrayRef<little32_t>(
  247. reinterpret_cast<const little32_t *>(SymContents.data()) + 1, 5);
  248. StringRef *I = std::begin(BCD.Symbols), *E = std::end(BCD.Symbols);
  249. if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
  250. E))
  251. return;
  252. BCDs[SymName] = BCD;
  253. }
  254. // Type descriptors in the MS-ABI start with '??_R0'
  255. else if (SymName.startswith("??_R0")) {
  256. const char *DataPtr = SymContents.drop_front(BytesInAddress).data();
  257. TypeDescriptor TD;
  258. if (BytesInAddress == 8)
  259. TD.AlwaysZero = *reinterpret_cast<const little64_t *>(DataPtr);
  260. else
  261. TD.AlwaysZero = *reinterpret_cast<const little32_t *>(DataPtr);
  262. TD.MangledName = SymContents.drop_front(BytesInAddress * 2);
  263. StringRef *I = std::begin(TD.Symbols), *E = std::end(TD.Symbols);
  264. if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
  265. E))
  266. return;
  267. TDs[SymName] = TD;
  268. }
  269. // Throw descriptors in the MS-ABI start with '_TI'
  270. else if (SymName.startswith("_TI") || SymName.startswith("__TI")) {
  271. ThrowInfo TI;
  272. TI.Flags = *reinterpret_cast<const little32_t *>(SymContents.data());
  273. collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
  274. SymName, TIEntries);
  275. TIs[SymName] = TI;
  276. }
  277. // Catchable type arrays in the MS-ABI start with _CTA or __CTA.
  278. else if (SymName.startswith("_CTA") || SymName.startswith("__CTA")) {
  279. CatchableTypeArray CTA;
  280. CTA.NumEntries =
  281. *reinterpret_cast<const little32_t *>(SymContents.data());
  282. collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
  283. SymName, CTAEntries);
  284. CTAs[SymName] = CTA;
  285. }
  286. // Catchable types in the MS-ABI start with _CT or __CT.
  287. else if (SymName.startswith("_CT") || SymName.startswith("__CT")) {
  288. const little32_t *DataPtr =
  289. reinterpret_cast<const little32_t *>(SymContents.data());
  290. CatchableType CT;
  291. CT.Flags = DataPtr[0];
  292. CT.NonVirtualBaseAdjustmentOffset = DataPtr[2];
  293. CT.VirtualBasePointerOffset = DataPtr[3];
  294. CT.VirtualBaseAdjustmentOffset = DataPtr[4];
  295. CT.Size = DataPtr[5];
  296. StringRef *I = std::begin(CT.Symbols), *E = std::end(CT.Symbols);
  297. if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
  298. E))
  299. return;
  300. CTs[SymName] = CT;
  301. }
  302. // Construction vtables in the Itanium ABI start with '_ZTT' or '__ZTT'.
  303. else if (SymName.startswith("_ZTT") || SymName.startswith("__ZTT")) {
  304. collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
  305. SymName, VTTEntries);
  306. }
  307. // Typeinfo names in the Itanium ABI start with '_ZTS' or '__ZTS'.
  308. else if (SymName.startswith("_ZTS") || SymName.startswith("__ZTS")) {
  309. TINames[SymName] = SymContents.slice(0, SymContents.find('\0'));
  310. }
  311. // Vtables in the Itanium ABI start with '_ZTV' or '__ZTV'.
  312. else if (SymName.startswith("_ZTV") || SymName.startswith("__ZTV")) {
  313. collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
  314. SymName, VTableSymEntries);
  315. for (uint64_t SymOffI = 0; SymOffI < SymSize; SymOffI += BytesInAddress) {
  316. auto Key = std::make_pair(SymName, SymOffI);
  317. if (VTableSymEntries.count(Key))
  318. continue;
  319. const char *DataPtr =
  320. SymContents.substr(SymOffI, BytesInAddress).data();
  321. int64_t VData;
  322. if (BytesInAddress == 8)
  323. VData = *reinterpret_cast<const little64_t *>(DataPtr);
  324. else
  325. VData = *reinterpret_cast<const little32_t *>(DataPtr);
  326. VTableDataEntries[Key] = VData;
  327. }
  328. }
  329. // Typeinfo structures in the Itanium ABI start with '_ZTI' or '__ZTI'.
  330. else if (SymName.startswith("_ZTI") || SymName.startswith("__ZTI")) {
  331. // FIXME: Do something with these!
  332. }
  333. }
  334. for (const auto &VFTableEntry : VFTableEntries) {
  335. StringRef VFTableName = VFTableEntry.first.first;
  336. uint64_t Offset = VFTableEntry.first.second;
  337. StringRef SymName = VFTableEntry.second;
  338. outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n';
  339. }
  340. for (const auto &VBTable : VBTables) {
  341. StringRef VBTableName = VBTable.first;
  342. uint32_t Idx = 0;
  343. for (little32_t Offset : VBTable.second) {
  344. outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n';
  345. Idx += sizeof(Offset);
  346. }
  347. }
  348. for (const auto &COLPair : COLs) {
  349. StringRef COLName = COLPair.first;
  350. const CompleteObjectLocator &COL = COLPair.second;
  351. outs() << COLName << "[IsImageRelative]: " << COL.Data[0] << '\n';
  352. outs() << COLName << "[OffsetToTop]: " << COL.Data[1] << '\n';
  353. outs() << COLName << "[VFPtrOffset]: " << COL.Data[2] << '\n';
  354. outs() << COLName << "[TypeDescriptor]: " << COL.Symbols[0] << '\n';
  355. outs() << COLName << "[ClassHierarchyDescriptor]: " << COL.Symbols[1]
  356. << '\n';
  357. }
  358. for (const auto &CHDPair : CHDs) {
  359. StringRef CHDName = CHDPair.first;
  360. const ClassHierarchyDescriptor &CHD = CHDPair.second;
  361. outs() << CHDName << "[AlwaysZero]: " << CHD.Data[0] << '\n';
  362. outs() << CHDName << "[Flags]: " << CHD.Data[1] << '\n';
  363. outs() << CHDName << "[NumClasses]: " << CHD.Data[2] << '\n';
  364. outs() << CHDName << "[BaseClassArray]: " << CHD.Symbols[0] << '\n';
  365. }
  366. for (const auto &BCAEntry : BCAEntries) {
  367. StringRef BCAName = BCAEntry.first.first;
  368. uint64_t Offset = BCAEntry.first.second;
  369. StringRef SymName = BCAEntry.second;
  370. outs() << BCAName << '[' << Offset << "]: " << SymName << '\n';
  371. }
  372. for (const auto &BCDPair : BCDs) {
  373. StringRef BCDName = BCDPair.first;
  374. const BaseClassDescriptor &BCD = BCDPair.second;
  375. outs() << BCDName << "[TypeDescriptor]: " << BCD.Symbols[0] << '\n';
  376. outs() << BCDName << "[NumBases]: " << BCD.Data[0] << '\n';
  377. outs() << BCDName << "[OffsetInVBase]: " << BCD.Data[1] << '\n';
  378. outs() << BCDName << "[VBPtrOffset]: " << BCD.Data[2] << '\n';
  379. outs() << BCDName << "[OffsetInVBTable]: " << BCD.Data[3] << '\n';
  380. outs() << BCDName << "[Flags]: " << BCD.Data[4] << '\n';
  381. outs() << BCDName << "[ClassHierarchyDescriptor]: " << BCD.Symbols[1]
  382. << '\n';
  383. }
  384. for (const auto &TDPair : TDs) {
  385. StringRef TDName = TDPair.first;
  386. const TypeDescriptor &TD = TDPair.second;
  387. outs() << TDName << "[VFPtr]: " << TD.Symbols[0] << '\n';
  388. outs() << TDName << "[AlwaysZero]: " << TD.AlwaysZero << '\n';
  389. outs() << TDName << "[MangledName]: ";
  390. outs().write_escaped(TD.MangledName.rtrim(StringRef("\0", 1)),
  391. /*UseHexEscapes=*/true)
  392. << '\n';
  393. }
  394. for (const auto &TIPair : TIs) {
  395. StringRef TIName = TIPair.first;
  396. const ThrowInfo &TI = TIPair.second;
  397. auto dumpThrowInfoFlag = [&](const char *Name, uint32_t Flag) {
  398. outs() << TIName << "[Flags." << Name
  399. << "]: " << (TI.Flags & Flag ? "true" : "false") << '\n';
  400. };
  401. auto dumpThrowInfoSymbol = [&](const char *Name, int Offset) {
  402. outs() << TIName << '[' << Name << "]: ";
  403. auto Entry = TIEntries.find(std::make_pair(TIName, Offset));
  404. outs() << (Entry == TIEntries.end() ? "null" : Entry->second) << '\n';
  405. };
  406. outs() << TIName << "[Flags]: " << TI.Flags << '\n';
  407. dumpThrowInfoFlag("Const", 1);
  408. dumpThrowInfoFlag("Volatile", 2);
  409. dumpThrowInfoSymbol("CleanupFn", 4);
  410. dumpThrowInfoSymbol("ForwardCompat", 8);
  411. dumpThrowInfoSymbol("CatchableTypeArray", 12);
  412. }
  413. for (const auto &CTAPair : CTAs) {
  414. StringRef CTAName = CTAPair.first;
  415. const CatchableTypeArray &CTA = CTAPair.second;
  416. outs() << CTAName << "[NumEntries]: " << CTA.NumEntries << '\n';
  417. unsigned Idx = 0;
  418. for (auto I = CTAEntries.lower_bound(std::make_pair(CTAName, 0)),
  419. E = CTAEntries.upper_bound(std::make_pair(CTAName, UINT64_MAX));
  420. I != E; ++I)
  421. outs() << CTAName << '[' << Idx++ << "]: " << I->second << '\n';
  422. }
  423. for (const auto &CTPair : CTs) {
  424. StringRef CTName = CTPair.first;
  425. const CatchableType &CT = CTPair.second;
  426. auto dumpCatchableTypeFlag = [&](const char *Name, uint32_t Flag) {
  427. outs() << CTName << "[Flags." << Name
  428. << "]: " << (CT.Flags & Flag ? "true" : "false") << '\n';
  429. };
  430. outs() << CTName << "[Flags]: " << CT.Flags << '\n';
  431. dumpCatchableTypeFlag("ScalarType", 1);
  432. dumpCatchableTypeFlag("VirtualInheritance", 4);
  433. outs() << CTName << "[TypeDescriptor]: " << CT.Symbols[0] << '\n';
  434. outs() << CTName << "[NonVirtualBaseAdjustmentOffset]: "
  435. << CT.NonVirtualBaseAdjustmentOffset << '\n';
  436. outs() << CTName
  437. << "[VirtualBasePointerOffset]: " << CT.VirtualBasePointerOffset
  438. << '\n';
  439. outs() << CTName << "[VirtualBaseAdjustmentOffset]: "
  440. << CT.VirtualBaseAdjustmentOffset << '\n';
  441. outs() << CTName << "[Size]: " << CT.Size << '\n';
  442. outs() << CTName
  443. << "[CopyCtor]: " << (CT.Symbols[1].empty() ? "null" : CT.Symbols[1])
  444. << '\n';
  445. }
  446. for (const auto &VTTPair : VTTEntries) {
  447. StringRef VTTName = VTTPair.first.first;
  448. uint64_t VTTOffset = VTTPair.first.second;
  449. StringRef VTTEntry = VTTPair.second;
  450. outs() << VTTName << '[' << VTTOffset << "]: " << VTTEntry << '\n';
  451. }
  452. for (const auto &TIPair : TINames) {
  453. StringRef TIName = TIPair.first;
  454. outs() << TIName << ": " << TIPair.second << '\n';
  455. }
  456. auto VTableSymI = VTableSymEntries.begin();
  457. auto VTableSymE = VTableSymEntries.end();
  458. auto VTableDataI = VTableDataEntries.begin();
  459. auto VTableDataE = VTableDataEntries.end();
  460. for (;;) {
  461. bool SymDone = VTableSymI == VTableSymE;
  462. bool DataDone = VTableDataI == VTableDataE;
  463. if (SymDone && DataDone)
  464. break;
  465. if (!SymDone && (DataDone || VTableSymI->first < VTableDataI->first)) {
  466. StringRef VTableName = VTableSymI->first.first;
  467. uint64_t Offset = VTableSymI->first.second;
  468. StringRef VTableEntry = VTableSymI->second;
  469. outs() << VTableName << '[' << Offset << "]: ";
  470. outs() << VTableEntry;
  471. outs() << '\n';
  472. ++VTableSymI;
  473. continue;
  474. }
  475. if (!DataDone && (SymDone || VTableDataI->first < VTableSymI->first)) {
  476. StringRef VTableName = VTableDataI->first.first;
  477. uint64_t Offset = VTableDataI->first.second;
  478. int64_t VTableEntry = VTableDataI->second;
  479. outs() << VTableName << '[' << Offset << "]: ";
  480. outs() << VTableEntry;
  481. outs() << '\n';
  482. ++VTableDataI;
  483. continue;
  484. }
  485. }
  486. }
  487. static void dumpArchive(const Archive *Arc) {
  488. for (const Archive::Child &ArcC : Arc->children()) {
  489. ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcC.getAsBinary();
  490. if (std::error_code EC = ChildOrErr.getError()) {
  491. // Ignore non-object files.
  492. if (EC != object_error::invalid_file_type)
  493. reportError(Arc->getFileName(), EC.message());
  494. continue;
  495. }
  496. if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
  497. dumpCXXData(Obj);
  498. else
  499. reportError(Arc->getFileName(), cxxdump_error::unrecognized_file_format);
  500. }
  501. }
  502. static void dumpInput(StringRef File) {
  503. // If file isn't stdin, check that it exists.
  504. if (File != "-" && !sys::fs::exists(File)) {
  505. reportError(File, cxxdump_error::file_not_found);
  506. return;
  507. }
  508. // Attempt to open the binary.
  509. ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
  510. if (std::error_code EC = BinaryOrErr.getError()) {
  511. reportError(File, EC);
  512. return;
  513. }
  514. Binary &Binary = *BinaryOrErr.get().getBinary();
  515. if (Archive *Arc = dyn_cast<Archive>(&Binary))
  516. dumpArchive(Arc);
  517. else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
  518. dumpCXXData(Obj);
  519. else
  520. reportError(File, cxxdump_error::unrecognized_file_format);
  521. }
  522. int main(int argc, const char *argv[]) {
  523. sys::PrintStackTraceOnErrorSignal();
  524. PrettyStackTraceProgram X(argc, argv);
  525. llvm_shutdown_obj Y;
  526. // Initialize targets.
  527. llvm::InitializeAllTargetInfos();
  528. // Register the target printer for --version.
  529. cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
  530. cl::ParseCommandLineOptions(argc, argv, "LLVM C++ ABI Data Dumper\n");
  531. // Default to stdin if no filename is specified.
  532. if (opts::InputFilenames.size() == 0)
  533. opts::InputFilenames.push_back("-");
  534. std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
  535. dumpInput);
  536. return ReturnValue;
  537. }