blob: e4d32abb95c9a3c24f40cb4200abdcaa067bfa8f [file] [log] [blame]
David Majnemerf45bbd02015-03-15 01:30:58 +00001//===- llvm-cxxdump.cpp - Dump C++ data in an Object File -------*- C++ -*-===//
David Majnemer72ab1a52014-07-24 23:14:40 +00002//
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//
David Majnemerf45bbd02015-03-15 01:30:58 +000010// Dumps C++ data resident in object files and archives.
David Majnemer72ab1a52014-07-24 23:14:40 +000011//
12//===----------------------------------------------------------------------===//
13
David Majnemerf45bbd02015-03-15 01:30:58 +000014#include "llvm-cxxdump.h"
David Majnemer72ab1a52014-07-24 23:14:40 +000015#include "Error.h"
16#include "llvm/ADT/ArrayRef.h"
David Majnemer72ab1a52014-07-24 23:14:40 +000017#include "llvm/Object/Archive.h"
18#include "llvm/Object/ObjectFile.h"
Rafael Espindolaa4a40932015-06-23 02:20:37 +000019#include "llvm/Object/SymbolSize.h"
David Majnemer72ab1a52014-07-24 23:14:40 +000020#include "llvm/Support/Debug.h"
21#include "llvm/Support/Endian.h"
22#include "llvm/Support/FileSystem.h"
23#include "llvm/Support/ManagedStatic.h"
24#include "llvm/Support/PrettyStackTrace.h"
25#include "llvm/Support/Signals.h"
26#include "llvm/Support/TargetRegistry.h"
27#include "llvm/Support/TargetSelect.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000028#include "llvm/Support/raw_ostream.h"
David Majnemer72ab1a52014-07-24 23:14:40 +000029#include <map>
30#include <string>
31#include <system_error>
32
33using namespace llvm;
34using namespace llvm::object;
35using namespace llvm::support;
36
37namespace opts {
38cl::list<std::string> InputFilenames(cl::Positional,
39 cl::desc("<input object files>"),
40 cl::ZeroOrMore);
41} // namespace opts
42
43static int ReturnValue = EXIT_SUCCESS;
44
45namespace llvm {
46
Benjamin Kramerf044d3f2015-03-09 16:23:46 +000047static bool error(std::error_code EC) {
David Majnemer72ab1a52014-07-24 23:14:40 +000048 if (!EC)
49 return false;
50
51 ReturnValue = EXIT_FAILURE;
52 outs() << "\nError reading file: " << EC.message() << ".\n";
53 outs().flush();
54 return true;
55}
56
57} // namespace llvm
58
59static void reportError(StringRef Input, StringRef Message) {
60 if (Input == "-")
61 Input = "<stdin>";
62
63 errs() << Input << ": " << Message << "\n";
64 errs().flush();
65 ReturnValue = EXIT_FAILURE;
66}
67
68static void reportError(StringRef Input, std::error_code EC) {
69 reportError(Input, EC.message());
70}
71
David Majnemere2683612014-11-03 07:23:25 +000072static SmallVectorImpl<SectionRef> &getRelocSections(const ObjectFile *Obj,
73 const SectionRef &Sec) {
74 static bool MappingDone = false;
75 static std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
76 if (!MappingDone) {
77 for (const SectionRef &Section : Obj->sections()) {
78 section_iterator Sec2 = Section.getRelocatedSection();
79 if (Sec2 != Obj->section_end())
80 SectionRelocMap[*Sec2].push_back(Section);
81 }
82 MappingDone = true;
83 }
84 return SectionRelocMap[Sec];
85}
86
David Majnemer1ac52eb2014-09-26 04:21:51 +000087static bool collectRelocatedSymbols(const ObjectFile *Obj,
David Majnemere2683612014-11-03 07:23:25 +000088 const SectionRef &Sec, uint64_t SecAddress,
89 uint64_t SymAddress, uint64_t SymSize,
90 StringRef *I, StringRef *E) {
91 uint64_t SymOffset = SymAddress - SecAddress;
92 uint64_t SymEnd = SymOffset + SymSize;
93 for (const SectionRef &SR : getRelocSections(Obj, Sec)) {
94 for (const object::RelocationRef &Reloc : SR.relocations()) {
95 if (I == E)
96 break;
97 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
98 if (RelocSymI == Obj->symbol_end())
99 continue;
100 StringRef RelocSymName;
101 if (error(RelocSymI->getName(RelocSymName)))
102 return true;
103 uint64_t Offset;
104 if (error(Reloc.getOffset(Offset)))
105 return true;
106 if (Offset >= SymOffset && Offset < SymEnd) {
107 *I = RelocSymName;
108 ++I;
109 }
110 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000111 }
112 return false;
113}
114
115static bool collectRelocationOffsets(
David Majnemere2683612014-11-03 07:23:25 +0000116 const ObjectFile *Obj, const SectionRef &Sec, uint64_t SecAddress,
117 uint64_t SymAddress, uint64_t SymSize, StringRef SymName,
David Majnemer1ac52eb2014-09-26 04:21:51 +0000118 std::map<std::pair<StringRef, uint64_t>, StringRef> &Collection) {
David Majnemere2683612014-11-03 07:23:25 +0000119 uint64_t SymOffset = SymAddress - SecAddress;
120 uint64_t SymEnd = SymOffset + SymSize;
121 for (const SectionRef &SR : getRelocSections(Obj, Sec)) {
122 for (const object::RelocationRef &Reloc : SR.relocations()) {
123 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
124 if (RelocSymI == Obj->symbol_end())
125 continue;
126 StringRef RelocSymName;
127 if (error(RelocSymI->getName(RelocSymName)))
128 return true;
129 uint64_t Offset;
130 if (error(Reloc.getOffset(Offset)))
131 return true;
132 if (Offset >= SymOffset && Offset < SymEnd)
133 Collection[std::make_pair(SymName, Offset - SymOffset)] = RelocSymName;
134 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000135 }
136 return false;
137}
138
David Majnemerf45bbd02015-03-15 01:30:58 +0000139static void dumpCXXData(const ObjectFile *Obj) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000140 struct CompleteObjectLocator {
141 StringRef Symbols[2];
David Majnemer6887a252014-09-26 08:01:23 +0000142 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000143 };
144 struct ClassHierarchyDescriptor {
145 StringRef Symbols[1];
David Majnemer6887a252014-09-26 08:01:23 +0000146 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000147 };
148 struct BaseClassDescriptor {
149 StringRef Symbols[2];
David Majnemer6887a252014-09-26 08:01:23 +0000150 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000151 };
152 struct TypeDescriptor {
153 StringRef Symbols[1];
David Majnemer6887a252014-09-26 08:01:23 +0000154 uint64_t AlwaysZero;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000155 StringRef MangledName;
156 };
David Majnemerf50d0a52015-02-27 00:43:58 +0000157 struct ThrowInfo {
158 uint32_t Flags;
159 };
160 struct CatchableTypeArray {
161 uint32_t NumEntries;
162 };
163 struct CatchableType {
164 uint32_t Flags;
165 uint32_t NonVirtualBaseAdjustmentOffset;
166 int32_t VirtualBasePointerOffset;
167 uint32_t VirtualBaseAdjustmentOffset;
David Majnemer86ee1732015-02-27 22:35:25 +0000168 uint32_t Size;
David Majnemerf50d0a52015-02-27 00:43:58 +0000169 StringRef Symbols[2];
170 };
David Majnemer72ab1a52014-07-24 23:14:40 +0000171 std::map<std::pair<StringRef, uint64_t>, StringRef> VFTableEntries;
David Majnemerf50d0a52015-02-27 00:43:58 +0000172 std::map<std::pair<StringRef, uint64_t>, StringRef> TIEntries;
173 std::map<std::pair<StringRef, uint64_t>, StringRef> CTAEntries;
David Majnemer6887a252014-09-26 08:01:23 +0000174 std::map<StringRef, ArrayRef<little32_t>> VBTables;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000175 std::map<StringRef, CompleteObjectLocator> COLs;
176 std::map<StringRef, ClassHierarchyDescriptor> CHDs;
177 std::map<std::pair<StringRef, uint64_t>, StringRef> BCAEntries;
178 std::map<StringRef, BaseClassDescriptor> BCDs;
179 std::map<StringRef, TypeDescriptor> TDs;
David Majnemerf50d0a52015-02-27 00:43:58 +0000180 std::map<StringRef, ThrowInfo> TIs;
181 std::map<StringRef, CatchableTypeArray> CTAs;
182 std::map<StringRef, CatchableType> CTs;
David Majnemere2683612014-11-03 07:23:25 +0000183
184 std::map<std::pair<StringRef, uint64_t>, StringRef> VTableSymEntries;
185 std::map<std::pair<StringRef, uint64_t>, int64_t> VTableDataEntries;
186 std::map<std::pair<StringRef, uint64_t>, StringRef> VTTEntries;
187 std::map<StringRef, StringRef> TINames;
188
189 uint8_t BytesInAddress = Obj->getBytesInAddress();
190
Rafael Espindola6bf32212015-06-24 19:57:32 +0000191 std::vector<std::pair<SymbolRef, uint64_t>> SymAddr =
Rafael Espindolaa4a40932015-06-23 02:20:37 +0000192 object::computeSymbolSizes(*Obj);
Rafael Espindolaa4a40932015-06-23 02:20:37 +0000193
Rafael Espindola6bf32212015-06-24 19:57:32 +0000194 for (auto &P : SymAddr) {
Rafael Espindolaa4a40932015-06-23 02:20:37 +0000195 object::SymbolRef Sym = P.first;
196 uint64_t SymSize = P.second;
David Majnemer72ab1a52014-07-24 23:14:40 +0000197 StringRef SymName;
198 if (error(Sym.getName(SymName)))
199 return;
David Majnemer601327c2014-09-26 22:32:19 +0000200 object::section_iterator SecI(Obj->section_begin());
201 if (error(Sym.getSection(SecI)))
202 return;
203 // Skip external symbols.
204 if (SecI == Obj->section_end())
205 continue;
David Majnemere2683612014-11-03 07:23:25 +0000206 const SectionRef &Sec = *SecI;
David Majnemer601327c2014-09-26 22:32:19 +0000207 // Skip virtual or BSS sections.
David Majnemere2683612014-11-03 07:23:25 +0000208 if (Sec.isBSS() || Sec.isVirtual())
David Majnemer601327c2014-09-26 22:32:19 +0000209 continue;
210 StringRef SecContents;
David Majnemere2683612014-11-03 07:23:25 +0000211 if (error(Sec.getContents(SecContents)))
David Majnemer601327c2014-09-26 22:32:19 +0000212 return;
Rafael Espindola5eb02e42015-06-01 00:27:26 +0000213 uint64_t SymAddress;
214 if (error(Sym.getAddress(SymAddress)))
David Majnemere2683612014-11-03 07:23:25 +0000215 return;
216 uint64_t SecAddress = Sec.getAddress();
217 uint64_t SecSize = Sec.getSize();
218 uint64_t SymOffset = SymAddress - SecAddress;
219 StringRef SymContents = SecContents.substr(SymOffset, SymSize);
220
David Majnemer72ab1a52014-07-24 23:14:40 +0000221 // VFTables in the MS-ABI start with '??_7' and are contained within their
222 // own COMDAT section. We then determine the contents of the VFTable by
223 // looking at each relocation in the section.
224 if (SymName.startswith("??_7")) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000225 // Each relocation either names a virtual method or a thunk. We note the
226 // offset into the section and the symbol used for the relocation.
David Majnemere2683612014-11-03 07:23:25 +0000227 collectRelocationOffsets(Obj, Sec, SecAddress, SecAddress, SecSize,
228 SymName, VFTableEntries);
David Majnemer72ab1a52014-07-24 23:14:40 +0000229 }
230 // VBTables in the MS-ABI start with '??_8' and are filled with 32-bit
231 // offsets of virtual bases.
232 else if (SymName.startswith("??_8")) {
David Majnemer6887a252014-09-26 08:01:23 +0000233 ArrayRef<little32_t> VBTableData(
David Majnemere2683612014-11-03 07:23:25 +0000234 reinterpret_cast<const little32_t *>(SymContents.data()),
235 SymContents.size() / sizeof(little32_t));
David Majnemer72ab1a52014-07-24 23:14:40 +0000236 VBTables[SymName] = VBTableData;
237 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000238 // Complete object locators in the MS-ABI start with '??_R4'
239 else if (SymName.startswith("??_R4")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000240 CompleteObjectLocator COL;
David Majnemer6887a252014-09-26 08:01:23 +0000241 COL.Data = ArrayRef<little32_t>(
David Majnemere2683612014-11-03 07:23:25 +0000242 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000243 StringRef *I = std::begin(COL.Symbols), *E = std::end(COL.Symbols);
David Majnemere2683612014-11-03 07:23:25 +0000244 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
245 E))
David Majnemer1ac52eb2014-09-26 04:21:51 +0000246 return;
247 COLs[SymName] = COL;
248 }
249 // Class hierarchy descriptors in the MS-ABI start with '??_R3'
250 else if (SymName.startswith("??_R3")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000251 ClassHierarchyDescriptor CHD;
David Majnemer6887a252014-09-26 08:01:23 +0000252 CHD.Data = ArrayRef<little32_t>(
David Majnemere2683612014-11-03 07:23:25 +0000253 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000254 StringRef *I = std::begin(CHD.Symbols), *E = std::end(CHD.Symbols);
David Majnemere2683612014-11-03 07:23:25 +0000255 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
256 E))
David Majnemer1ac52eb2014-09-26 04:21:51 +0000257 return;
258 CHDs[SymName] = CHD;
259 }
260 // Class hierarchy descriptors in the MS-ABI start with '??_R2'
261 else if (SymName.startswith("??_R2")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000262 // Each relocation names a base class descriptor. We note the offset into
263 // the section and the symbol used for the relocation.
David Majnemere2683612014-11-03 07:23:25 +0000264 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
265 SymName, BCAEntries);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000266 }
267 // Base class descriptors in the MS-ABI start with '??_R1'
268 else if (SymName.startswith("??_R1")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000269 BaseClassDescriptor BCD;
David Majnemer6887a252014-09-26 08:01:23 +0000270 BCD.Data = ArrayRef<little32_t>(
David Majnemere2683612014-11-03 07:23:25 +0000271 reinterpret_cast<const little32_t *>(SymContents.data()) + 1, 5);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000272 StringRef *I = std::begin(BCD.Symbols), *E = std::end(BCD.Symbols);
David Majnemere2683612014-11-03 07:23:25 +0000273 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
274 E))
David Majnemer1ac52eb2014-09-26 04:21:51 +0000275 return;
276 BCDs[SymName] = BCD;
277 }
278 // Type descriptors in the MS-ABI start with '??_R0'
279 else if (SymName.startswith("??_R0")) {
David Majnemere2683612014-11-03 07:23:25 +0000280 const char *DataPtr = SymContents.drop_front(BytesInAddress).data();
David Majnemer1ac52eb2014-09-26 04:21:51 +0000281 TypeDescriptor TD;
David Majnemer6887a252014-09-26 08:01:23 +0000282 if (BytesInAddress == 8)
283 TD.AlwaysZero = *reinterpret_cast<const little64_t *>(DataPtr);
284 else
285 TD.AlwaysZero = *reinterpret_cast<const little32_t *>(DataPtr);
David Majnemere2683612014-11-03 07:23:25 +0000286 TD.MangledName = SymContents.drop_front(BytesInAddress * 2);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000287 StringRef *I = std::begin(TD.Symbols), *E = std::end(TD.Symbols);
David Majnemere2683612014-11-03 07:23:25 +0000288 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
289 E))
David Majnemer1ac52eb2014-09-26 04:21:51 +0000290 return;
291 TDs[SymName] = TD;
292 }
David Majnemerf50d0a52015-02-27 00:43:58 +0000293 // Throw descriptors in the MS-ABI start with '_TI'
294 else if (SymName.startswith("_TI") || SymName.startswith("__TI")) {
295 ThrowInfo TI;
296 TI.Flags = *reinterpret_cast<const little32_t *>(SymContents.data());
297 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
298 SymName, TIEntries);
299 TIs[SymName] = TI;
300 }
301 // Catchable type arrays in the MS-ABI start with _CTA or __CTA.
302 else if (SymName.startswith("_CTA") || SymName.startswith("__CTA")) {
303 CatchableTypeArray CTA;
304 CTA.NumEntries =
305 *reinterpret_cast<const little32_t *>(SymContents.data());
306 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
307 SymName, CTAEntries);
308 CTAs[SymName] = CTA;
309 }
310 // Catchable types in the MS-ABI start with _CT or __CT.
311 else if (SymName.startswith("_CT") || SymName.startswith("__CT")) {
312 const little32_t *DataPtr =
313 reinterpret_cast<const little32_t *>(SymContents.data());
314 CatchableType CT;
315 CT.Flags = DataPtr[0];
316 CT.NonVirtualBaseAdjustmentOffset = DataPtr[2];
317 CT.VirtualBasePointerOffset = DataPtr[3];
318 CT.VirtualBaseAdjustmentOffset = DataPtr[4];
David Majnemer86ee1732015-02-27 22:35:25 +0000319 CT.Size = DataPtr[5];
David Majnemerf50d0a52015-02-27 00:43:58 +0000320 StringRef *I = std::begin(CT.Symbols), *E = std::end(CT.Symbols);
321 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
322 E))
323 return;
324 CTs[SymName] = CT;
325 }
David Majnemere2683612014-11-03 07:23:25 +0000326 // Construction vtables in the Itanium ABI start with '_ZTT' or '__ZTT'.
327 else if (SymName.startswith("_ZTT") || SymName.startswith("__ZTT")) {
328 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
329 SymName, VTTEntries);
330 }
331 // Typeinfo names in the Itanium ABI start with '_ZTS' or '__ZTS'.
332 else if (SymName.startswith("_ZTS") || SymName.startswith("__ZTS")) {
333 TINames[SymName] = SymContents.slice(0, SymContents.find('\0'));
334 }
335 // Vtables in the Itanium ABI start with '_ZTV' or '__ZTV'.
336 else if (SymName.startswith("_ZTV") || SymName.startswith("__ZTV")) {
337 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
338 SymName, VTableSymEntries);
339 for (uint64_t SymOffI = 0; SymOffI < SymSize; SymOffI += BytesInAddress) {
340 auto Key = std::make_pair(SymName, SymOffI);
341 if (VTableSymEntries.count(Key))
342 continue;
David Majnemerf45bbd02015-03-15 01:30:58 +0000343 const char *DataPtr =
344 SymContents.substr(SymOffI, BytesInAddress).data();
David Majnemere2683612014-11-03 07:23:25 +0000345 int64_t VData;
346 if (BytesInAddress == 8)
347 VData = *reinterpret_cast<const little64_t *>(DataPtr);
348 else
349 VData = *reinterpret_cast<const little32_t *>(DataPtr);
350 VTableDataEntries[Key] = VData;
351 }
352 }
353 // Typeinfo structures in the Itanium ABI start with '_ZTI' or '__ZTI'.
354 else if (SymName.startswith("_ZTI") || SymName.startswith("__ZTI")) {
355 // FIXME: Do something with these!
356 }
David Majnemer72ab1a52014-07-24 23:14:40 +0000357 }
David Majnemerf45bbd02015-03-15 01:30:58 +0000358 for (const auto &VFTableEntry : VFTableEntries) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000359 StringRef VFTableName = VFTableEntry.first.first;
360 uint64_t Offset = VFTableEntry.first.second;
361 StringRef SymName = VFTableEntry.second;
362 outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n';
363 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000364 for (const auto &VBTable : VBTables) {
David Majnemerbf32f772014-07-25 04:30:11 +0000365 StringRef VBTableName = VBTable.first;
David Majnemer72ab1a52014-07-24 23:14:40 +0000366 uint32_t Idx = 0;
David Majnemer6887a252014-09-26 08:01:23 +0000367 for (little32_t Offset : VBTable.second) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000368 outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n';
David Majnemerbf32f772014-07-25 04:30:11 +0000369 Idx += sizeof(Offset);
David Majnemer72ab1a52014-07-24 23:14:40 +0000370 }
371 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000372 for (const auto &COLPair : COLs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000373 StringRef COLName = COLPair.first;
374 const CompleteObjectLocator &COL = COLPair.second;
375 outs() << COLName << "[IsImageRelative]: " << COL.Data[0] << '\n';
376 outs() << COLName << "[OffsetToTop]: " << COL.Data[1] << '\n';
377 outs() << COLName << "[VFPtrOffset]: " << COL.Data[2] << '\n';
378 outs() << COLName << "[TypeDescriptor]: " << COL.Symbols[0] << '\n';
David Majnemerf45bbd02015-03-15 01:30:58 +0000379 outs() << COLName << "[ClassHierarchyDescriptor]: " << COL.Symbols[1]
380 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000381 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000382 for (const auto &CHDPair : CHDs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000383 StringRef CHDName = CHDPair.first;
384 const ClassHierarchyDescriptor &CHD = CHDPair.second;
385 outs() << CHDName << "[AlwaysZero]: " << CHD.Data[0] << '\n';
386 outs() << CHDName << "[Flags]: " << CHD.Data[1] << '\n';
387 outs() << CHDName << "[NumClasses]: " << CHD.Data[2] << '\n';
388 outs() << CHDName << "[BaseClassArray]: " << CHD.Symbols[0] << '\n';
389 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000390 for (const auto &BCAEntry : BCAEntries) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000391 StringRef BCAName = BCAEntry.first.first;
392 uint64_t Offset = BCAEntry.first.second;
393 StringRef SymName = BCAEntry.second;
394 outs() << BCAName << '[' << Offset << "]: " << SymName << '\n';
395 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000396 for (const auto &BCDPair : BCDs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000397 StringRef BCDName = BCDPair.first;
398 const BaseClassDescriptor &BCD = BCDPair.second;
399 outs() << BCDName << "[TypeDescriptor]: " << BCD.Symbols[0] << '\n';
400 outs() << BCDName << "[NumBases]: " << BCD.Data[0] << '\n';
401 outs() << BCDName << "[OffsetInVBase]: " << BCD.Data[1] << '\n';
402 outs() << BCDName << "[VBPtrOffset]: " << BCD.Data[2] << '\n';
403 outs() << BCDName << "[OffsetInVBTable]: " << BCD.Data[3] << '\n';
404 outs() << BCDName << "[Flags]: " << BCD.Data[4] << '\n';
David Majnemerf45bbd02015-03-15 01:30:58 +0000405 outs() << BCDName << "[ClassHierarchyDescriptor]: " << BCD.Symbols[1]
406 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000407 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000408 for (const auto &TDPair : TDs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000409 StringRef TDName = TDPair.first;
410 const TypeDescriptor &TD = TDPair.second;
411 outs() << TDName << "[VFPtr]: " << TD.Symbols[0] << '\n';
David Majnemer6887a252014-09-26 08:01:23 +0000412 outs() << TDName << "[AlwaysZero]: " << TD.AlwaysZero << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000413 outs() << TDName << "[MangledName]: ";
David Majnemer56167c32014-09-26 05:50:45 +0000414 outs().write_escaped(TD.MangledName.rtrim(StringRef("\0", 1)),
415 /*UseHexEscapes=*/true)
416 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000417 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000418 for (const auto &TIPair : TIs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000419 StringRef TIName = TIPair.first;
420 const ThrowInfo &TI = TIPair.second;
421 auto dumpThrowInfoFlag = [&](const char *Name, uint32_t Flag) {
422 outs() << TIName << "[Flags." << Name
423 << "]: " << (TI.Flags & Flag ? "true" : "false") << '\n';
424 };
425 auto dumpThrowInfoSymbol = [&](const char *Name, int Offset) {
426 outs() << TIName << '[' << Name << "]: ";
427 auto Entry = TIEntries.find(std::make_pair(TIName, Offset));
428 outs() << (Entry == TIEntries.end() ? "null" : Entry->second) << '\n';
429 };
430 outs() << TIName << "[Flags]: " << TI.Flags << '\n';
431 dumpThrowInfoFlag("Const", 1);
432 dumpThrowInfoFlag("Volatile", 2);
433 dumpThrowInfoSymbol("CleanupFn", 4);
434 dumpThrowInfoSymbol("ForwardCompat", 8);
435 dumpThrowInfoSymbol("CatchableTypeArray", 12);
436 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000437 for (const auto &CTAPair : CTAs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000438 StringRef CTAName = CTAPair.first;
439 const CatchableTypeArray &CTA = CTAPair.second;
440
441 outs() << CTAName << "[NumEntries]: " << CTA.NumEntries << '\n';
442
443 unsigned Idx = 0;
444 for (auto I = CTAEntries.lower_bound(std::make_pair(CTAName, 0)),
445 E = CTAEntries.upper_bound(std::make_pair(CTAName, UINT64_MAX));
446 I != E; ++I)
447 outs() << CTAName << '[' << Idx++ << "]: " << I->second << '\n';
448 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000449 for (const auto &CTPair : CTs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000450 StringRef CTName = CTPair.first;
451 const CatchableType &CT = CTPair.second;
452 auto dumpCatchableTypeFlag = [&](const char *Name, uint32_t Flag) {
453 outs() << CTName << "[Flags." << Name
454 << "]: " << (CT.Flags & Flag ? "true" : "false") << '\n';
455 };
456 outs() << CTName << "[Flags]: " << CT.Flags << '\n';
457 dumpCatchableTypeFlag("ScalarType", 1);
458 dumpCatchableTypeFlag("VirtualInheritance", 4);
459 outs() << CTName << "[TypeDescriptor]: " << CT.Symbols[0] << '\n';
460 outs() << CTName << "[NonVirtualBaseAdjustmentOffset]: "
461 << CT.NonVirtualBaseAdjustmentOffset << '\n';
462 outs() << CTName
463 << "[VirtualBasePointerOffset]: " << CT.VirtualBasePointerOffset
464 << '\n';
465 outs() << CTName << "[VirtualBaseAdjustmentOffset]: "
466 << CT.VirtualBaseAdjustmentOffset << '\n';
David Majnemer86ee1732015-02-27 22:35:25 +0000467 outs() << CTName << "[Size]: " << CT.Size << '\n';
David Majnemerf50d0a52015-02-27 00:43:58 +0000468 outs() << CTName
469 << "[CopyCtor]: " << (CT.Symbols[1].empty() ? "null" : CT.Symbols[1])
470 << '\n';
471 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000472 for (const auto &VTTPair : VTTEntries) {
David Majnemere2683612014-11-03 07:23:25 +0000473 StringRef VTTName = VTTPair.first.first;
474 uint64_t VTTOffset = VTTPair.first.second;
475 StringRef VTTEntry = VTTPair.second;
476 outs() << VTTName << '[' << VTTOffset << "]: " << VTTEntry << '\n';
477 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000478 for (const auto &TIPair : TINames) {
David Majnemere2683612014-11-03 07:23:25 +0000479 StringRef TIName = TIPair.first;
480 outs() << TIName << ": " << TIPair.second << '\n';
481 }
482 auto VTableSymI = VTableSymEntries.begin();
483 auto VTableSymE = VTableSymEntries.end();
484 auto VTableDataI = VTableDataEntries.begin();
485 auto VTableDataE = VTableDataEntries.end();
486 for (;;) {
487 bool SymDone = VTableSymI == VTableSymE;
488 bool DataDone = VTableDataI == VTableDataE;
489 if (SymDone && DataDone)
490 break;
491 if (!SymDone && (DataDone || VTableSymI->first < VTableDataI->first)) {
492 StringRef VTableName = VTableSymI->first.first;
493 uint64_t Offset = VTableSymI->first.second;
494 StringRef VTableEntry = VTableSymI->second;
495 outs() << VTableName << '[' << Offset << "]: ";
496 outs() << VTableEntry;
497 outs() << '\n';
498 ++VTableSymI;
499 continue;
500 }
501 if (!DataDone && (SymDone || VTableDataI->first < VTableSymI->first)) {
502 StringRef VTableName = VTableDataI->first.first;
503 uint64_t Offset = VTableDataI->first.second;
504 int64_t VTableEntry = VTableDataI->second;
505 outs() << VTableName << '[' << Offset << "]: ";
506 outs() << VTableEntry;
507 outs() << '\n';
508 ++VTableDataI;
509 continue;
510 }
511 }
David Majnemer72ab1a52014-07-24 23:14:40 +0000512}
513
514static void dumpArchive(const Archive *Arc) {
David Majnemereac48b62014-09-25 22:56:54 +0000515 for (const Archive::Child &ArcC : Arc->children()) {
516 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcC.getAsBinary();
David Majnemer72ab1a52014-07-24 23:14:40 +0000517 if (std::error_code EC = ChildOrErr.getError()) {
518 // Ignore non-object files.
519 if (EC != object_error::invalid_file_type)
520 reportError(Arc->getFileName(), EC.message());
521 continue;
522 }
523
524 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
David Majnemerf45bbd02015-03-15 01:30:58 +0000525 dumpCXXData(Obj);
David Majnemer72ab1a52014-07-24 23:14:40 +0000526 else
David Majnemerf45bbd02015-03-15 01:30:58 +0000527 reportError(Arc->getFileName(), cxxdump_error::unrecognized_file_format);
David Majnemer72ab1a52014-07-24 23:14:40 +0000528 }
529}
530
531static void dumpInput(StringRef File) {
532 // If file isn't stdin, check that it exists.
533 if (File != "-" && !sys::fs::exists(File)) {
David Majnemerf45bbd02015-03-15 01:30:58 +0000534 reportError(File, cxxdump_error::file_not_found);
David Majnemer72ab1a52014-07-24 23:14:40 +0000535 return;
536 }
537
538 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000539 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
David Majnemer72ab1a52014-07-24 23:14:40 +0000540 if (std::error_code EC = BinaryOrErr.getError()) {
541 reportError(File, EC);
542 return;
543 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000544 Binary &Binary = *BinaryOrErr.get().getBinary();
David Majnemer72ab1a52014-07-24 23:14:40 +0000545
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000546 if (Archive *Arc = dyn_cast<Archive>(&Binary))
David Majnemer72ab1a52014-07-24 23:14:40 +0000547 dumpArchive(Arc);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000548 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
David Majnemerf45bbd02015-03-15 01:30:58 +0000549 dumpCXXData(Obj);
David Majnemer72ab1a52014-07-24 23:14:40 +0000550 else
David Majnemerf45bbd02015-03-15 01:30:58 +0000551 reportError(File, cxxdump_error::unrecognized_file_format);
David Majnemer72ab1a52014-07-24 23:14:40 +0000552}
553
554int main(int argc, const char *argv[]) {
555 sys::PrintStackTraceOnErrorSignal();
556 PrettyStackTraceProgram X(argc, argv);
557 llvm_shutdown_obj Y;
558
559 // Initialize targets.
560 llvm::InitializeAllTargetInfos();
561
562 // Register the target printer for --version.
563 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
564
David Majnemerf45bbd02015-03-15 01:30:58 +0000565 cl::ParseCommandLineOptions(argc, argv, "LLVM C++ ABI Data Dumper\n");
David Majnemer72ab1a52014-07-24 23:14:40 +0000566
567 // Default to stdin if no filename is specified.
568 if (opts::InputFilenames.size() == 0)
569 opts::InputFilenames.push_back("-");
570
571 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
572 dumpInput);
573
574 return ReturnValue;
575}