llvm-cxxdump.cpp 22 KB

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