llvm-cxxdump.cpp 21 KB

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