llvm-cxxdump.cpp 22 KB

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