llvm-cxxdump.cpp 21 KB

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