blob: dacb48ab01f836d7682ad7fb5360264cd5adc3f3 [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"
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"
Benjamin Kramer16132e62015-03-23 18:07:13 +000027#include "llvm/Support/raw_ostream.h"
David Majnemer72ab1a52014-07-24 23:14:40 +000028#include <map>
29#include <string>
30#include <system_error>
31
32using namespace llvm;
33using namespace llvm::object;
34using namespace llvm::support;
35
36namespace opts {
37cl::list<std::string> InputFilenames(cl::Positional,
38 cl::desc("<input object files>"),
39 cl::ZeroOrMore);
40} // namespace opts
41
42static int ReturnValue = EXIT_SUCCESS;
43
44namespace llvm {
45
Benjamin Kramerf044d3f2015-03-09 16:23:46 +000046static bool error(std::error_code EC) {
David Majnemer72ab1a52014-07-24 23:14:40 +000047 if (!EC)
48 return false;
49
50 ReturnValue = EXIT_FAILURE;
51 outs() << "\nError reading file: " << EC.message() << ".\n";
52 outs().flush();
53 return true;
54}
55
56} // namespace llvm
57
58static void reportError(StringRef Input, StringRef Message) {
59 if (Input == "-")
60 Input = "<stdin>";
61
62 errs() << Input << ": " << Message << "\n";
63 errs().flush();
64 ReturnValue = EXIT_FAILURE;
65}
66
67static void reportError(StringRef Input, std::error_code EC) {
68 reportError(Input, EC.message());
69}
70
David Majnemere2683612014-11-03 07:23:25 +000071static SmallVectorImpl<SectionRef> &getRelocSections(const ObjectFile *Obj,
72 const SectionRef &Sec) {
73 static bool MappingDone = false;
74 static std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
75 if (!MappingDone) {
76 for (const SectionRef &Section : Obj->sections()) {
77 section_iterator Sec2 = Section.getRelocatedSection();
78 if (Sec2 != Obj->section_end())
79 SectionRelocMap[*Sec2].push_back(Section);
80 }
81 MappingDone = true;
82 }
83 return SectionRelocMap[Sec];
84}
85
David Majnemer1ac52eb2014-09-26 04:21:51 +000086static bool collectRelocatedSymbols(const ObjectFile *Obj,
David Majnemere2683612014-11-03 07:23:25 +000087 const SectionRef &Sec, uint64_t SecAddress,
88 uint64_t SymAddress, uint64_t SymSize,
89 StringRef *I, StringRef *E) {
90 uint64_t SymOffset = SymAddress - SecAddress;
91 uint64_t SymEnd = SymOffset + SymSize;
92 for (const SectionRef &SR : getRelocSections(Obj, Sec)) {
93 for (const object::RelocationRef &Reloc : SR.relocations()) {
94 if (I == E)
95 break;
96 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
97 if (RelocSymI == Obj->symbol_end())
98 continue;
99 StringRef RelocSymName;
100 if (error(RelocSymI->getName(RelocSymName)))
101 return true;
102 uint64_t Offset;
103 if (error(Reloc.getOffset(Offset)))
104 return true;
105 if (Offset >= SymOffset && Offset < SymEnd) {
106 *I = RelocSymName;
107 ++I;
108 }
109 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000110 }
111 return false;
112}
113
114static bool collectRelocationOffsets(
David Majnemere2683612014-11-03 07:23:25 +0000115 const ObjectFile *Obj, const SectionRef &Sec, uint64_t SecAddress,
116 uint64_t SymAddress, uint64_t SymSize, StringRef SymName,
David Majnemer1ac52eb2014-09-26 04:21:51 +0000117 std::map<std::pair<StringRef, uint64_t>, StringRef> &Collection) {
David Majnemere2683612014-11-03 07:23:25 +0000118 uint64_t SymOffset = SymAddress - SecAddress;
119 uint64_t SymEnd = SymOffset + SymSize;
120 for (const SectionRef &SR : getRelocSections(Obj, Sec)) {
121 for (const object::RelocationRef &Reloc : SR.relocations()) {
122 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
123 if (RelocSymI == Obj->symbol_end())
124 continue;
125 StringRef RelocSymName;
126 if (error(RelocSymI->getName(RelocSymName)))
127 return true;
128 uint64_t Offset;
129 if (error(Reloc.getOffset(Offset)))
130 return true;
131 if (Offset >= SymOffset && Offset < SymEnd)
132 Collection[std::make_pair(SymName, Offset - SymOffset)] = RelocSymName;
133 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000134 }
135 return false;
136}
137
David Majnemerf45bbd02015-03-15 01:30:58 +0000138static void dumpCXXData(const ObjectFile *Obj) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000139 struct CompleteObjectLocator {
140 StringRef Symbols[2];
David Majnemer6887a252014-09-26 08:01:23 +0000141 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000142 };
143 struct ClassHierarchyDescriptor {
144 StringRef Symbols[1];
David Majnemer6887a252014-09-26 08:01:23 +0000145 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000146 };
147 struct BaseClassDescriptor {
148 StringRef Symbols[2];
David Majnemer6887a252014-09-26 08:01:23 +0000149 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000150 };
151 struct TypeDescriptor {
152 StringRef Symbols[1];
David Majnemer6887a252014-09-26 08:01:23 +0000153 uint64_t AlwaysZero;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000154 StringRef MangledName;
155 };
David Majnemerf50d0a52015-02-27 00:43:58 +0000156 struct ThrowInfo {
157 uint32_t Flags;
158 };
159 struct CatchableTypeArray {
160 uint32_t NumEntries;
161 };
162 struct CatchableType {
163 uint32_t Flags;
164 uint32_t NonVirtualBaseAdjustmentOffset;
165 int32_t VirtualBasePointerOffset;
166 uint32_t VirtualBaseAdjustmentOffset;
David Majnemer86ee1732015-02-27 22:35:25 +0000167 uint32_t Size;
David Majnemerf50d0a52015-02-27 00:43:58 +0000168 StringRef Symbols[2];
169 };
David Majnemer72ab1a52014-07-24 23:14:40 +0000170 std::map<std::pair<StringRef, uint64_t>, StringRef> VFTableEntries;
David Majnemerf50d0a52015-02-27 00:43:58 +0000171 std::map<std::pair<StringRef, uint64_t>, StringRef> TIEntries;
172 std::map<std::pair<StringRef, uint64_t>, StringRef> CTAEntries;
David Majnemer6887a252014-09-26 08:01:23 +0000173 std::map<StringRef, ArrayRef<little32_t>> VBTables;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000174 std::map<StringRef, CompleteObjectLocator> COLs;
175 std::map<StringRef, ClassHierarchyDescriptor> CHDs;
176 std::map<std::pair<StringRef, uint64_t>, StringRef> BCAEntries;
177 std::map<StringRef, BaseClassDescriptor> BCDs;
178 std::map<StringRef, TypeDescriptor> TDs;
David Majnemerf50d0a52015-02-27 00:43:58 +0000179 std::map<StringRef, ThrowInfo> TIs;
180 std::map<StringRef, CatchableTypeArray> CTAs;
181 std::map<StringRef, CatchableType> CTs;
David Majnemere2683612014-11-03 07:23:25 +0000182
183 std::map<std::pair<StringRef, uint64_t>, StringRef> VTableSymEntries;
184 std::map<std::pair<StringRef, uint64_t>, int64_t> VTableDataEntries;
185 std::map<std::pair<StringRef, uint64_t>, StringRef> VTTEntries;
186 std::map<StringRef, StringRef> TINames;
187
188 uint8_t BytesInAddress = Obj->getBytesInAddress();
189
David Majnemer72ab1a52014-07-24 23:14:40 +0000190 for (const object::SymbolRef &Sym : Obj->symbols()) {
191 StringRef SymName;
192 if (error(Sym.getName(SymName)))
193 return;
David Majnemer601327c2014-09-26 22:32:19 +0000194 object::section_iterator SecI(Obj->section_begin());
195 if (error(Sym.getSection(SecI)))
196 return;
197 // Skip external symbols.
198 if (SecI == Obj->section_end())
199 continue;
David Majnemere2683612014-11-03 07:23:25 +0000200 const SectionRef &Sec = *SecI;
David Majnemer601327c2014-09-26 22:32:19 +0000201 // Skip virtual or BSS sections.
David Majnemere2683612014-11-03 07:23:25 +0000202 if (Sec.isBSS() || Sec.isVirtual())
David Majnemer601327c2014-09-26 22:32:19 +0000203 continue;
204 StringRef SecContents;
David Majnemere2683612014-11-03 07:23:25 +0000205 if (error(Sec.getContents(SecContents)))
David Majnemer601327c2014-09-26 22:32:19 +0000206 return;
David Majnemere2683612014-11-03 07:23:25 +0000207 uint64_t SymAddress, SymSize;
208 if (error(Sym.getAddress(SymAddress)) || error(Sym.getSize(SymSize)))
209 return;
210 uint64_t SecAddress = Sec.getAddress();
211 uint64_t SecSize = Sec.getSize();
212 uint64_t SymOffset = SymAddress - SecAddress;
213 StringRef SymContents = SecContents.substr(SymOffset, SymSize);
214
David Majnemer72ab1a52014-07-24 23:14:40 +0000215 // VFTables in the MS-ABI start with '??_7' and are contained within their
216 // own COMDAT section. We then determine the contents of the VFTable by
217 // looking at each relocation in the section.
218 if (SymName.startswith("??_7")) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000219 // Each relocation either names a virtual method or a thunk. We note the
220 // offset into the section and the symbol used for the relocation.
David Majnemere2683612014-11-03 07:23:25 +0000221 collectRelocationOffsets(Obj, Sec, SecAddress, SecAddress, SecSize,
222 SymName, VFTableEntries);
David Majnemer72ab1a52014-07-24 23:14:40 +0000223 }
224 // VBTables in the MS-ABI start with '??_8' and are filled with 32-bit
225 // offsets of virtual bases.
226 else if (SymName.startswith("??_8")) {
David Majnemer6887a252014-09-26 08:01:23 +0000227 ArrayRef<little32_t> VBTableData(
David Majnemere2683612014-11-03 07:23:25 +0000228 reinterpret_cast<const little32_t *>(SymContents.data()),
229 SymContents.size() / sizeof(little32_t));
David Majnemer72ab1a52014-07-24 23:14:40 +0000230 VBTables[SymName] = VBTableData;
231 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000232 // Complete object locators in the MS-ABI start with '??_R4'
233 else if (SymName.startswith("??_R4")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000234 CompleteObjectLocator COL;
David Majnemer6887a252014-09-26 08:01:23 +0000235 COL.Data = ArrayRef<little32_t>(
David Majnemere2683612014-11-03 07:23:25 +0000236 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000237 StringRef *I = std::begin(COL.Symbols), *E = std::end(COL.Symbols);
David Majnemere2683612014-11-03 07:23:25 +0000238 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
239 E))
David Majnemer1ac52eb2014-09-26 04:21:51 +0000240 return;
241 COLs[SymName] = COL;
242 }
243 // Class hierarchy descriptors in the MS-ABI start with '??_R3'
244 else if (SymName.startswith("??_R3")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000245 ClassHierarchyDescriptor CHD;
David Majnemer6887a252014-09-26 08:01:23 +0000246 CHD.Data = ArrayRef<little32_t>(
David Majnemere2683612014-11-03 07:23:25 +0000247 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000248 StringRef *I = std::begin(CHD.Symbols), *E = std::end(CHD.Symbols);
David Majnemere2683612014-11-03 07:23:25 +0000249 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
250 E))
David Majnemer1ac52eb2014-09-26 04:21:51 +0000251 return;
252 CHDs[SymName] = CHD;
253 }
254 // Class hierarchy descriptors in the MS-ABI start with '??_R2'
255 else if (SymName.startswith("??_R2")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000256 // Each relocation names a base class descriptor. We note the offset into
257 // the section and the symbol used for the relocation.
David Majnemere2683612014-11-03 07:23:25 +0000258 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
259 SymName, BCAEntries);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000260 }
261 // Base class descriptors in the MS-ABI start with '??_R1'
262 else if (SymName.startswith("??_R1")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000263 BaseClassDescriptor BCD;
David Majnemer6887a252014-09-26 08:01:23 +0000264 BCD.Data = ArrayRef<little32_t>(
David Majnemere2683612014-11-03 07:23:25 +0000265 reinterpret_cast<const little32_t *>(SymContents.data()) + 1, 5);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000266 StringRef *I = std::begin(BCD.Symbols), *E = std::end(BCD.Symbols);
David Majnemere2683612014-11-03 07:23:25 +0000267 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
268 E))
David Majnemer1ac52eb2014-09-26 04:21:51 +0000269 return;
270 BCDs[SymName] = BCD;
271 }
272 // Type descriptors in the MS-ABI start with '??_R0'
273 else if (SymName.startswith("??_R0")) {
David Majnemere2683612014-11-03 07:23:25 +0000274 const char *DataPtr = SymContents.drop_front(BytesInAddress).data();
David Majnemer1ac52eb2014-09-26 04:21:51 +0000275 TypeDescriptor TD;
David Majnemer6887a252014-09-26 08:01:23 +0000276 if (BytesInAddress == 8)
277 TD.AlwaysZero = *reinterpret_cast<const little64_t *>(DataPtr);
278 else
279 TD.AlwaysZero = *reinterpret_cast<const little32_t *>(DataPtr);
David Majnemere2683612014-11-03 07:23:25 +0000280 TD.MangledName = SymContents.drop_front(BytesInAddress * 2);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000281 StringRef *I = std::begin(TD.Symbols), *E = std::end(TD.Symbols);
David Majnemere2683612014-11-03 07:23:25 +0000282 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
283 E))
David Majnemer1ac52eb2014-09-26 04:21:51 +0000284 return;
285 TDs[SymName] = TD;
286 }
David Majnemerf50d0a52015-02-27 00:43:58 +0000287 // Throw descriptors in the MS-ABI start with '_TI'
288 else if (SymName.startswith("_TI") || SymName.startswith("__TI")) {
289 ThrowInfo TI;
290 TI.Flags = *reinterpret_cast<const little32_t *>(SymContents.data());
291 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
292 SymName, TIEntries);
293 TIs[SymName] = TI;
294 }
295 // Catchable type arrays in the MS-ABI start with _CTA or __CTA.
296 else if (SymName.startswith("_CTA") || SymName.startswith("__CTA")) {
297 CatchableTypeArray CTA;
298 CTA.NumEntries =
299 *reinterpret_cast<const little32_t *>(SymContents.data());
300 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
301 SymName, CTAEntries);
302 CTAs[SymName] = CTA;
303 }
304 // Catchable types in the MS-ABI start with _CT or __CT.
305 else if (SymName.startswith("_CT") || SymName.startswith("__CT")) {
306 const little32_t *DataPtr =
307 reinterpret_cast<const little32_t *>(SymContents.data());
308 CatchableType CT;
309 CT.Flags = DataPtr[0];
310 CT.NonVirtualBaseAdjustmentOffset = DataPtr[2];
311 CT.VirtualBasePointerOffset = DataPtr[3];
312 CT.VirtualBaseAdjustmentOffset = DataPtr[4];
David Majnemer86ee1732015-02-27 22:35:25 +0000313 CT.Size = DataPtr[5];
David Majnemerf50d0a52015-02-27 00:43:58 +0000314 StringRef *I = std::begin(CT.Symbols), *E = std::end(CT.Symbols);
315 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
316 E))
317 return;
318 CTs[SymName] = CT;
319 }
David Majnemere2683612014-11-03 07:23:25 +0000320 // Construction vtables in the Itanium ABI start with '_ZTT' or '__ZTT'.
321 else if (SymName.startswith("_ZTT") || SymName.startswith("__ZTT")) {
322 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
323 SymName, VTTEntries);
324 }
325 // Typeinfo names in the Itanium ABI start with '_ZTS' or '__ZTS'.
326 else if (SymName.startswith("_ZTS") || SymName.startswith("__ZTS")) {
327 TINames[SymName] = SymContents.slice(0, SymContents.find('\0'));
328 }
329 // Vtables in the Itanium ABI start with '_ZTV' or '__ZTV'.
330 else if (SymName.startswith("_ZTV") || SymName.startswith("__ZTV")) {
331 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
332 SymName, VTableSymEntries);
333 for (uint64_t SymOffI = 0; SymOffI < SymSize; SymOffI += BytesInAddress) {
334 auto Key = std::make_pair(SymName, SymOffI);
335 if (VTableSymEntries.count(Key))
336 continue;
David Majnemerf45bbd02015-03-15 01:30:58 +0000337 const char *DataPtr =
338 SymContents.substr(SymOffI, BytesInAddress).data();
David Majnemere2683612014-11-03 07:23:25 +0000339 int64_t VData;
340 if (BytesInAddress == 8)
341 VData = *reinterpret_cast<const little64_t *>(DataPtr);
342 else
343 VData = *reinterpret_cast<const little32_t *>(DataPtr);
344 VTableDataEntries[Key] = VData;
345 }
346 }
347 // Typeinfo structures in the Itanium ABI start with '_ZTI' or '__ZTI'.
348 else if (SymName.startswith("_ZTI") || SymName.startswith("__ZTI")) {
349 // FIXME: Do something with these!
350 }
David Majnemer72ab1a52014-07-24 23:14:40 +0000351 }
David Majnemerf45bbd02015-03-15 01:30:58 +0000352 for (const auto &VFTableEntry : VFTableEntries) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000353 StringRef VFTableName = VFTableEntry.first.first;
354 uint64_t Offset = VFTableEntry.first.second;
355 StringRef SymName = VFTableEntry.second;
356 outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n';
357 }
Richard Trieu6b1aa5f2015-04-15 01:21:15 +0000358 for (const std::pair<const StringRef, ArrayRef<little32_t>> &VBTable :
359 VBTables) {
David Majnemerbf32f772014-07-25 04:30:11 +0000360 StringRef VBTableName = VBTable.first;
David Majnemer72ab1a52014-07-24 23:14:40 +0000361 uint32_t Idx = 0;
David Majnemer6887a252014-09-26 08:01:23 +0000362 for (little32_t Offset : VBTable.second) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000363 outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n';
David Majnemerbf32f772014-07-25 04:30:11 +0000364 Idx += sizeof(Offset);
David Majnemer72ab1a52014-07-24 23:14:40 +0000365 }
366 }
Richard Trieu6b1aa5f2015-04-15 01:21:15 +0000367 for (const std::pair<const StringRef, CompleteObjectLocator> &COLPair :
368 COLs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000369 StringRef COLName = COLPair.first;
370 const CompleteObjectLocator &COL = COLPair.second;
371 outs() << COLName << "[IsImageRelative]: " << COL.Data[0] << '\n';
372 outs() << COLName << "[OffsetToTop]: " << COL.Data[1] << '\n';
373 outs() << COLName << "[VFPtrOffset]: " << COL.Data[2] << '\n';
374 outs() << COLName << "[TypeDescriptor]: " << COL.Symbols[0] << '\n';
David Majnemerf45bbd02015-03-15 01:30:58 +0000375 outs() << COLName << "[ClassHierarchyDescriptor]: " << COL.Symbols[1]
376 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000377 }
Richard Trieu6b1aa5f2015-04-15 01:21:15 +0000378 for (const std::pair<const StringRef, ClassHierarchyDescriptor> &CHDPair :
379 CHDs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000380 StringRef CHDName = CHDPair.first;
381 const ClassHierarchyDescriptor &CHD = CHDPair.second;
382 outs() << CHDName << "[AlwaysZero]: " << CHD.Data[0] << '\n';
383 outs() << CHDName << "[Flags]: " << CHD.Data[1] << '\n';
384 outs() << CHDName << "[NumClasses]: " << CHD.Data[2] << '\n';
385 outs() << CHDName << "[BaseClassArray]: " << CHD.Symbols[0] << '\n';
386 }
Richard Trieu6b1aa5f2015-04-15 01:21:15 +0000387 for (const std::pair<const std::pair<StringRef, uint64_t>, StringRef>
388 &BCAEntry : BCAEntries) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000389 StringRef BCAName = BCAEntry.first.first;
390 uint64_t Offset = BCAEntry.first.second;
391 StringRef SymName = BCAEntry.second;
392 outs() << BCAName << '[' << Offset << "]: " << SymName << '\n';
393 }
Richard Trieu6b1aa5f2015-04-15 01:21:15 +0000394 for (const std::pair<const StringRef, BaseClassDescriptor> &BCDPair : BCDs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000395 StringRef BCDName = BCDPair.first;
396 const BaseClassDescriptor &BCD = BCDPair.second;
397 outs() << BCDName << "[TypeDescriptor]: " << BCD.Symbols[0] << '\n';
398 outs() << BCDName << "[NumBases]: " << BCD.Data[0] << '\n';
399 outs() << BCDName << "[OffsetInVBase]: " << BCD.Data[1] << '\n';
400 outs() << BCDName << "[VBPtrOffset]: " << BCD.Data[2] << '\n';
401 outs() << BCDName << "[OffsetInVBTable]: " << BCD.Data[3] << '\n';
402 outs() << BCDName << "[Flags]: " << BCD.Data[4] << '\n';
David Majnemerf45bbd02015-03-15 01:30:58 +0000403 outs() << BCDName << "[ClassHierarchyDescriptor]: " << BCD.Symbols[1]
404 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000405 }
Richard Trieu6b1aa5f2015-04-15 01:21:15 +0000406 for (const std::pair<const StringRef, TypeDescriptor> &TDPair : TDs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000407 StringRef TDName = TDPair.first;
408 const TypeDescriptor &TD = TDPair.second;
409 outs() << TDName << "[VFPtr]: " << TD.Symbols[0] << '\n';
David Majnemer6887a252014-09-26 08:01:23 +0000410 outs() << TDName << "[AlwaysZero]: " << TD.AlwaysZero << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000411 outs() << TDName << "[MangledName]: ";
David Majnemer56167c32014-09-26 05:50:45 +0000412 outs().write_escaped(TD.MangledName.rtrim(StringRef("\0", 1)),
413 /*UseHexEscapes=*/true)
414 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000415 }
Richard Trieu6b1aa5f2015-04-15 01:21:15 +0000416 for (const std::pair<const StringRef, ThrowInfo> &TIPair : TIs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000417 StringRef TIName = TIPair.first;
418 const ThrowInfo &TI = TIPair.second;
419 auto dumpThrowInfoFlag = [&](const char *Name, uint32_t Flag) {
420 outs() << TIName << "[Flags." << Name
421 << "]: " << (TI.Flags & Flag ? "true" : "false") << '\n';
422 };
423 auto dumpThrowInfoSymbol = [&](const char *Name, int Offset) {
424 outs() << TIName << '[' << Name << "]: ";
425 auto Entry = TIEntries.find(std::make_pair(TIName, Offset));
426 outs() << (Entry == TIEntries.end() ? "null" : Entry->second) << '\n';
427 };
428 outs() << TIName << "[Flags]: " << TI.Flags << '\n';
429 dumpThrowInfoFlag("Const", 1);
430 dumpThrowInfoFlag("Volatile", 2);
431 dumpThrowInfoSymbol("CleanupFn", 4);
432 dumpThrowInfoSymbol("ForwardCompat", 8);
433 dumpThrowInfoSymbol("CatchableTypeArray", 12);
434 }
Richard Trieu6b1aa5f2015-04-15 01:21:15 +0000435 for (const std::pair<const StringRef, CatchableTypeArray> &CTAPair : CTAs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000436 StringRef CTAName = CTAPair.first;
437 const CatchableTypeArray &CTA = CTAPair.second;
438
439 outs() << CTAName << "[NumEntries]: " << CTA.NumEntries << '\n';
440
441 unsigned Idx = 0;
442 for (auto I = CTAEntries.lower_bound(std::make_pair(CTAName, 0)),
443 E = CTAEntries.upper_bound(std::make_pair(CTAName, UINT64_MAX));
444 I != E; ++I)
445 outs() << CTAName << '[' << Idx++ << "]: " << I->second << '\n';
446 }
Richard Trieu6b1aa5f2015-04-15 01:21:15 +0000447 for (const std::pair<const StringRef, CatchableType> &CTPair : CTs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000448 StringRef CTName = CTPair.first;
449 const CatchableType &CT = CTPair.second;
450 auto dumpCatchableTypeFlag = [&](const char *Name, uint32_t Flag) {
451 outs() << CTName << "[Flags." << Name
452 << "]: " << (CT.Flags & Flag ? "true" : "false") << '\n';
453 };
454 outs() << CTName << "[Flags]: " << CT.Flags << '\n';
455 dumpCatchableTypeFlag("ScalarType", 1);
456 dumpCatchableTypeFlag("VirtualInheritance", 4);
457 outs() << CTName << "[TypeDescriptor]: " << CT.Symbols[0] << '\n';
458 outs() << CTName << "[NonVirtualBaseAdjustmentOffset]: "
459 << CT.NonVirtualBaseAdjustmentOffset << '\n';
460 outs() << CTName
461 << "[VirtualBasePointerOffset]: " << CT.VirtualBasePointerOffset
462 << '\n';
463 outs() << CTName << "[VirtualBaseAdjustmentOffset]: "
464 << CT.VirtualBaseAdjustmentOffset << '\n';
David Majnemer86ee1732015-02-27 22:35:25 +0000465 outs() << CTName << "[Size]: " << CT.Size << '\n';
David Majnemerf50d0a52015-02-27 00:43:58 +0000466 outs() << CTName
467 << "[CopyCtor]: " << (CT.Symbols[1].empty() ? "null" : CT.Symbols[1])
468 << '\n';
469 }
Richard Trieu6b1aa5f2015-04-15 01:21:15 +0000470 for (const std::pair<const std::pair<StringRef, uint64_t>, StringRef>
471 &VTTPair : VTTEntries) {
David Majnemere2683612014-11-03 07:23:25 +0000472 StringRef VTTName = VTTPair.first.first;
473 uint64_t VTTOffset = VTTPair.first.second;
474 StringRef VTTEntry = VTTPair.second;
475 outs() << VTTName << '[' << VTTOffset << "]: " << VTTEntry << '\n';
476 }
Richard Trieu6b1aa5f2015-04-15 01:21:15 +0000477 for (const std::pair<const StringRef, StringRef> &TIPair : TINames) {
David Majnemere2683612014-11-03 07:23:25 +0000478 StringRef TIName = TIPair.first;
479 outs() << TIName << ": " << TIPair.second << '\n';
480 }
481 auto VTableSymI = VTableSymEntries.begin();
482 auto VTableSymE = VTableSymEntries.end();
483 auto VTableDataI = VTableDataEntries.begin();
484 auto VTableDataE = VTableDataEntries.end();
485 for (;;) {
486 bool SymDone = VTableSymI == VTableSymE;
487 bool DataDone = VTableDataI == VTableDataE;
488 if (SymDone && DataDone)
489 break;
490 if (!SymDone && (DataDone || VTableSymI->first < VTableDataI->first)) {
491 StringRef VTableName = VTableSymI->first.first;
492 uint64_t Offset = VTableSymI->first.second;
493 StringRef VTableEntry = VTableSymI->second;
494 outs() << VTableName << '[' << Offset << "]: ";
495 outs() << VTableEntry;
496 outs() << '\n';
497 ++VTableSymI;
498 continue;
499 }
500 if (!DataDone && (SymDone || VTableDataI->first < VTableSymI->first)) {
501 StringRef VTableName = VTableDataI->first.first;
502 uint64_t Offset = VTableDataI->first.second;
503 int64_t VTableEntry = VTableDataI->second;
504 outs() << VTableName << '[' << Offset << "]: ";
505 outs() << VTableEntry;
506 outs() << '\n';
507 ++VTableDataI;
508 continue;
509 }
510 }
David Majnemer72ab1a52014-07-24 23:14:40 +0000511}
512
513static void dumpArchive(const Archive *Arc) {
David Majnemereac48b62014-09-25 22:56:54 +0000514 for (const Archive::Child &ArcC : Arc->children()) {
515 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcC.getAsBinary();
David Majnemer72ab1a52014-07-24 23:14:40 +0000516 if (std::error_code EC = ChildOrErr.getError()) {
517 // Ignore non-object files.
518 if (EC != object_error::invalid_file_type)
519 reportError(Arc->getFileName(), EC.message());
520 continue;
521 }
522
523 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
David Majnemerf45bbd02015-03-15 01:30:58 +0000524 dumpCXXData(Obj);
David Majnemer72ab1a52014-07-24 23:14:40 +0000525 else
David Majnemerf45bbd02015-03-15 01:30:58 +0000526 reportError(Arc->getFileName(), cxxdump_error::unrecognized_file_format);
David Majnemer72ab1a52014-07-24 23:14:40 +0000527 }
528}
529
530static void dumpInput(StringRef File) {
531 // If file isn't stdin, check that it exists.
532 if (File != "-" && !sys::fs::exists(File)) {
David Majnemerf45bbd02015-03-15 01:30:58 +0000533 reportError(File, cxxdump_error::file_not_found);
David Majnemer72ab1a52014-07-24 23:14:40 +0000534 return;
535 }
536
537 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000538 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
David Majnemer72ab1a52014-07-24 23:14:40 +0000539 if (std::error_code EC = BinaryOrErr.getError()) {
540 reportError(File, EC);
541 return;
542 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000543 Binary &Binary = *BinaryOrErr.get().getBinary();
David Majnemer72ab1a52014-07-24 23:14:40 +0000544
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000545 if (Archive *Arc = dyn_cast<Archive>(&Binary))
David Majnemer72ab1a52014-07-24 23:14:40 +0000546 dumpArchive(Arc);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000547 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
David Majnemerf45bbd02015-03-15 01:30:58 +0000548 dumpCXXData(Obj);
David Majnemer72ab1a52014-07-24 23:14:40 +0000549 else
David Majnemerf45bbd02015-03-15 01:30:58 +0000550 reportError(File, cxxdump_error::unrecognized_file_format);
David Majnemer72ab1a52014-07-24 23:14:40 +0000551}
552
553int main(int argc, const char *argv[]) {
554 sys::PrintStackTraceOnErrorSignal();
555 PrettyStackTraceProgram X(argc, argv);
556 llvm_shutdown_obj Y;
557
558 // Initialize targets.
559 llvm::InitializeAllTargetInfos();
560
561 // Register the target printer for --version.
562 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
563
David Majnemerf45bbd02015-03-15 01:30:58 +0000564 cl::ParseCommandLineOptions(argc, argv, "LLVM C++ ABI Data Dumper\n");
David Majnemer72ab1a52014-07-24 23:14:40 +0000565
566 // Default to stdin if no filename is specified.
567 if (opts::InputFilenames.size() == 0)
568 opts::InputFilenames.push_back("-");
569
570 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
571 dumpInput);
572
573 return ReturnValue;
574}