blob: bd9cf7d5df2533b4d82c6803c5181272181e69fa [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 Espindolaa4a40932015-06-23 02:20:37 +0000191 ErrorOr<std::vector<std::pair<SymbolRef, uint64_t>>> SymAddrOrErr =
192 object::computeSymbolSizes(*Obj);
193 if (error(SymAddrOrErr.getError()))
194 return;
195
196 for (auto &P : *SymAddrOrErr) {
197 object::SymbolRef Sym = P.first;
198 uint64_t SymSize = P.second;
David Majnemer72ab1a52014-07-24 23:14:40 +0000199 StringRef SymName;
200 if (error(Sym.getName(SymName)))
201 return;
David Majnemer601327c2014-09-26 22:32:19 +0000202 object::section_iterator SecI(Obj->section_begin());
203 if (error(Sym.getSection(SecI)))
204 return;
205 // Skip external symbols.
206 if (SecI == Obj->section_end())
207 continue;
David Majnemere2683612014-11-03 07:23:25 +0000208 const SectionRef &Sec = *SecI;
David Majnemer601327c2014-09-26 22:32:19 +0000209 // Skip virtual or BSS sections.
David Majnemere2683612014-11-03 07:23:25 +0000210 if (Sec.isBSS() || Sec.isVirtual())
David Majnemer601327c2014-09-26 22:32:19 +0000211 continue;
212 StringRef SecContents;
David Majnemere2683612014-11-03 07:23:25 +0000213 if (error(Sec.getContents(SecContents)))
David Majnemer601327c2014-09-26 22:32:19 +0000214 return;
Rafael Espindola5eb02e42015-06-01 00:27:26 +0000215 uint64_t SymAddress;
216 if (error(Sym.getAddress(SymAddress)))
David Majnemere2683612014-11-03 07:23:25 +0000217 return;
218 uint64_t SecAddress = Sec.getAddress();
219 uint64_t SecSize = Sec.getSize();
220 uint64_t SymOffset = SymAddress - SecAddress;
221 StringRef SymContents = SecContents.substr(SymOffset, SymSize);
222
David Majnemer72ab1a52014-07-24 23:14:40 +0000223 // VFTables in the MS-ABI start with '??_7' and are contained within their
224 // own COMDAT section. We then determine the contents of the VFTable by
225 // looking at each relocation in the section.
226 if (SymName.startswith("??_7")) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000227 // Each relocation either names a virtual method or a thunk. We note the
228 // offset into the section and the symbol used for the relocation.
David Majnemere2683612014-11-03 07:23:25 +0000229 collectRelocationOffsets(Obj, Sec, SecAddress, SecAddress, SecSize,
230 SymName, VFTableEntries);
David Majnemer72ab1a52014-07-24 23:14:40 +0000231 }
232 // VBTables in the MS-ABI start with '??_8' and are filled with 32-bit
233 // offsets of virtual bases.
234 else if (SymName.startswith("??_8")) {
David Majnemer6887a252014-09-26 08:01:23 +0000235 ArrayRef<little32_t> VBTableData(
David Majnemere2683612014-11-03 07:23:25 +0000236 reinterpret_cast<const little32_t *>(SymContents.data()),
237 SymContents.size() / sizeof(little32_t));
David Majnemer72ab1a52014-07-24 23:14:40 +0000238 VBTables[SymName] = VBTableData;
239 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000240 // Complete object locators in the MS-ABI start with '??_R4'
241 else if (SymName.startswith("??_R4")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000242 CompleteObjectLocator COL;
David Majnemer6887a252014-09-26 08:01:23 +0000243 COL.Data = ArrayRef<little32_t>(
David Majnemere2683612014-11-03 07:23:25 +0000244 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000245 StringRef *I = std::begin(COL.Symbols), *E = std::end(COL.Symbols);
David Majnemere2683612014-11-03 07:23:25 +0000246 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
247 E))
David Majnemer1ac52eb2014-09-26 04:21:51 +0000248 return;
249 COLs[SymName] = COL;
250 }
251 // Class hierarchy descriptors in the MS-ABI start with '??_R3'
252 else if (SymName.startswith("??_R3")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000253 ClassHierarchyDescriptor CHD;
David Majnemer6887a252014-09-26 08:01:23 +0000254 CHD.Data = ArrayRef<little32_t>(
David Majnemere2683612014-11-03 07:23:25 +0000255 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000256 StringRef *I = std::begin(CHD.Symbols), *E = std::end(CHD.Symbols);
David Majnemere2683612014-11-03 07:23:25 +0000257 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
258 E))
David Majnemer1ac52eb2014-09-26 04:21:51 +0000259 return;
260 CHDs[SymName] = CHD;
261 }
262 // Class hierarchy descriptors in the MS-ABI start with '??_R2'
263 else if (SymName.startswith("??_R2")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000264 // Each relocation names a base class descriptor. We note the offset into
265 // the section and the symbol used for the relocation.
David Majnemere2683612014-11-03 07:23:25 +0000266 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
267 SymName, BCAEntries);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000268 }
269 // Base class descriptors in the MS-ABI start with '??_R1'
270 else if (SymName.startswith("??_R1")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000271 BaseClassDescriptor BCD;
David Majnemer6887a252014-09-26 08:01:23 +0000272 BCD.Data = ArrayRef<little32_t>(
David Majnemere2683612014-11-03 07:23:25 +0000273 reinterpret_cast<const little32_t *>(SymContents.data()) + 1, 5);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000274 StringRef *I = std::begin(BCD.Symbols), *E = std::end(BCD.Symbols);
David Majnemere2683612014-11-03 07:23:25 +0000275 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
276 E))
David Majnemer1ac52eb2014-09-26 04:21:51 +0000277 return;
278 BCDs[SymName] = BCD;
279 }
280 // Type descriptors in the MS-ABI start with '??_R0'
281 else if (SymName.startswith("??_R0")) {
David Majnemere2683612014-11-03 07:23:25 +0000282 const char *DataPtr = SymContents.drop_front(BytesInAddress).data();
David Majnemer1ac52eb2014-09-26 04:21:51 +0000283 TypeDescriptor TD;
David Majnemer6887a252014-09-26 08:01:23 +0000284 if (BytesInAddress == 8)
285 TD.AlwaysZero = *reinterpret_cast<const little64_t *>(DataPtr);
286 else
287 TD.AlwaysZero = *reinterpret_cast<const little32_t *>(DataPtr);
David Majnemere2683612014-11-03 07:23:25 +0000288 TD.MangledName = SymContents.drop_front(BytesInAddress * 2);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000289 StringRef *I = std::begin(TD.Symbols), *E = std::end(TD.Symbols);
David Majnemere2683612014-11-03 07:23:25 +0000290 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
291 E))
David Majnemer1ac52eb2014-09-26 04:21:51 +0000292 return;
293 TDs[SymName] = TD;
294 }
David Majnemerf50d0a52015-02-27 00:43:58 +0000295 // Throw descriptors in the MS-ABI start with '_TI'
296 else if (SymName.startswith("_TI") || SymName.startswith("__TI")) {
297 ThrowInfo TI;
298 TI.Flags = *reinterpret_cast<const little32_t *>(SymContents.data());
299 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
300 SymName, TIEntries);
301 TIs[SymName] = TI;
302 }
303 // Catchable type arrays in the MS-ABI start with _CTA or __CTA.
304 else if (SymName.startswith("_CTA") || SymName.startswith("__CTA")) {
305 CatchableTypeArray CTA;
306 CTA.NumEntries =
307 *reinterpret_cast<const little32_t *>(SymContents.data());
308 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
309 SymName, CTAEntries);
310 CTAs[SymName] = CTA;
311 }
312 // Catchable types in the MS-ABI start with _CT or __CT.
313 else if (SymName.startswith("_CT") || SymName.startswith("__CT")) {
314 const little32_t *DataPtr =
315 reinterpret_cast<const little32_t *>(SymContents.data());
316 CatchableType CT;
317 CT.Flags = DataPtr[0];
318 CT.NonVirtualBaseAdjustmentOffset = DataPtr[2];
319 CT.VirtualBasePointerOffset = DataPtr[3];
320 CT.VirtualBaseAdjustmentOffset = DataPtr[4];
David Majnemer86ee1732015-02-27 22:35:25 +0000321 CT.Size = DataPtr[5];
David Majnemerf50d0a52015-02-27 00:43:58 +0000322 StringRef *I = std::begin(CT.Symbols), *E = std::end(CT.Symbols);
323 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
324 E))
325 return;
326 CTs[SymName] = CT;
327 }
David Majnemere2683612014-11-03 07:23:25 +0000328 // Construction vtables in the Itanium ABI start with '_ZTT' or '__ZTT'.
329 else if (SymName.startswith("_ZTT") || SymName.startswith("__ZTT")) {
330 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
331 SymName, VTTEntries);
332 }
333 // Typeinfo names in the Itanium ABI start with '_ZTS' or '__ZTS'.
334 else if (SymName.startswith("_ZTS") || SymName.startswith("__ZTS")) {
335 TINames[SymName] = SymContents.slice(0, SymContents.find('\0'));
336 }
337 // Vtables in the Itanium ABI start with '_ZTV' or '__ZTV'.
338 else if (SymName.startswith("_ZTV") || SymName.startswith("__ZTV")) {
339 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
340 SymName, VTableSymEntries);
341 for (uint64_t SymOffI = 0; SymOffI < SymSize; SymOffI += BytesInAddress) {
342 auto Key = std::make_pair(SymName, SymOffI);
343 if (VTableSymEntries.count(Key))
344 continue;
David Majnemerf45bbd02015-03-15 01:30:58 +0000345 const char *DataPtr =
346 SymContents.substr(SymOffI, BytesInAddress).data();
David Majnemere2683612014-11-03 07:23:25 +0000347 int64_t VData;
348 if (BytesInAddress == 8)
349 VData = *reinterpret_cast<const little64_t *>(DataPtr);
350 else
351 VData = *reinterpret_cast<const little32_t *>(DataPtr);
352 VTableDataEntries[Key] = VData;
353 }
354 }
355 // Typeinfo structures in the Itanium ABI start with '_ZTI' or '__ZTI'.
356 else if (SymName.startswith("_ZTI") || SymName.startswith("__ZTI")) {
357 // FIXME: Do something with these!
358 }
David Majnemer72ab1a52014-07-24 23:14:40 +0000359 }
David Majnemerf45bbd02015-03-15 01:30:58 +0000360 for (const auto &VFTableEntry : VFTableEntries) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000361 StringRef VFTableName = VFTableEntry.first.first;
362 uint64_t Offset = VFTableEntry.first.second;
363 StringRef SymName = VFTableEntry.second;
364 outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n';
365 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000366 for (const auto &VBTable : VBTables) {
David Majnemerbf32f772014-07-25 04:30:11 +0000367 StringRef VBTableName = VBTable.first;
David Majnemer72ab1a52014-07-24 23:14:40 +0000368 uint32_t Idx = 0;
David Majnemer6887a252014-09-26 08:01:23 +0000369 for (little32_t Offset : VBTable.second) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000370 outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n';
David Majnemerbf32f772014-07-25 04:30:11 +0000371 Idx += sizeof(Offset);
David Majnemer72ab1a52014-07-24 23:14:40 +0000372 }
373 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000374 for (const auto &COLPair : COLs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000375 StringRef COLName = COLPair.first;
376 const CompleteObjectLocator &COL = COLPair.second;
377 outs() << COLName << "[IsImageRelative]: " << COL.Data[0] << '\n';
378 outs() << COLName << "[OffsetToTop]: " << COL.Data[1] << '\n';
379 outs() << COLName << "[VFPtrOffset]: " << COL.Data[2] << '\n';
380 outs() << COLName << "[TypeDescriptor]: " << COL.Symbols[0] << '\n';
David Majnemerf45bbd02015-03-15 01:30:58 +0000381 outs() << COLName << "[ClassHierarchyDescriptor]: " << COL.Symbols[1]
382 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000383 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000384 for (const auto &CHDPair : CHDs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000385 StringRef CHDName = CHDPair.first;
386 const ClassHierarchyDescriptor &CHD = CHDPair.second;
387 outs() << CHDName << "[AlwaysZero]: " << CHD.Data[0] << '\n';
388 outs() << CHDName << "[Flags]: " << CHD.Data[1] << '\n';
389 outs() << CHDName << "[NumClasses]: " << CHD.Data[2] << '\n';
390 outs() << CHDName << "[BaseClassArray]: " << CHD.Symbols[0] << '\n';
391 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000392 for (const auto &BCAEntry : BCAEntries) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000393 StringRef BCAName = BCAEntry.first.first;
394 uint64_t Offset = BCAEntry.first.second;
395 StringRef SymName = BCAEntry.second;
396 outs() << BCAName << '[' << Offset << "]: " << SymName << '\n';
397 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000398 for (const auto &BCDPair : BCDs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000399 StringRef BCDName = BCDPair.first;
400 const BaseClassDescriptor &BCD = BCDPair.second;
401 outs() << BCDName << "[TypeDescriptor]: " << BCD.Symbols[0] << '\n';
402 outs() << BCDName << "[NumBases]: " << BCD.Data[0] << '\n';
403 outs() << BCDName << "[OffsetInVBase]: " << BCD.Data[1] << '\n';
404 outs() << BCDName << "[VBPtrOffset]: " << BCD.Data[2] << '\n';
405 outs() << BCDName << "[OffsetInVBTable]: " << BCD.Data[3] << '\n';
406 outs() << BCDName << "[Flags]: " << BCD.Data[4] << '\n';
David Majnemerf45bbd02015-03-15 01:30:58 +0000407 outs() << BCDName << "[ClassHierarchyDescriptor]: " << BCD.Symbols[1]
408 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000409 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000410 for (const auto &TDPair : TDs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000411 StringRef TDName = TDPair.first;
412 const TypeDescriptor &TD = TDPair.second;
413 outs() << TDName << "[VFPtr]: " << TD.Symbols[0] << '\n';
David Majnemer6887a252014-09-26 08:01:23 +0000414 outs() << TDName << "[AlwaysZero]: " << TD.AlwaysZero << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000415 outs() << TDName << "[MangledName]: ";
David Majnemer56167c32014-09-26 05:50:45 +0000416 outs().write_escaped(TD.MangledName.rtrim(StringRef("\0", 1)),
417 /*UseHexEscapes=*/true)
418 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000419 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000420 for (const auto &TIPair : TIs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000421 StringRef TIName = TIPair.first;
422 const ThrowInfo &TI = TIPair.second;
423 auto dumpThrowInfoFlag = [&](const char *Name, uint32_t Flag) {
424 outs() << TIName << "[Flags." << Name
425 << "]: " << (TI.Flags & Flag ? "true" : "false") << '\n';
426 };
427 auto dumpThrowInfoSymbol = [&](const char *Name, int Offset) {
428 outs() << TIName << '[' << Name << "]: ";
429 auto Entry = TIEntries.find(std::make_pair(TIName, Offset));
430 outs() << (Entry == TIEntries.end() ? "null" : Entry->second) << '\n';
431 };
432 outs() << TIName << "[Flags]: " << TI.Flags << '\n';
433 dumpThrowInfoFlag("Const", 1);
434 dumpThrowInfoFlag("Volatile", 2);
435 dumpThrowInfoSymbol("CleanupFn", 4);
436 dumpThrowInfoSymbol("ForwardCompat", 8);
437 dumpThrowInfoSymbol("CatchableTypeArray", 12);
438 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000439 for (const auto &CTAPair : CTAs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000440 StringRef CTAName = CTAPair.first;
441 const CatchableTypeArray &CTA = CTAPair.second;
442
443 outs() << CTAName << "[NumEntries]: " << CTA.NumEntries << '\n';
444
445 unsigned Idx = 0;
446 for (auto I = CTAEntries.lower_bound(std::make_pair(CTAName, 0)),
447 E = CTAEntries.upper_bound(std::make_pair(CTAName, UINT64_MAX));
448 I != E; ++I)
449 outs() << CTAName << '[' << Idx++ << "]: " << I->second << '\n';
450 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000451 for (const auto &CTPair : CTs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000452 StringRef CTName = CTPair.first;
453 const CatchableType &CT = CTPair.second;
454 auto dumpCatchableTypeFlag = [&](const char *Name, uint32_t Flag) {
455 outs() << CTName << "[Flags." << Name
456 << "]: " << (CT.Flags & Flag ? "true" : "false") << '\n';
457 };
458 outs() << CTName << "[Flags]: " << CT.Flags << '\n';
459 dumpCatchableTypeFlag("ScalarType", 1);
460 dumpCatchableTypeFlag("VirtualInheritance", 4);
461 outs() << CTName << "[TypeDescriptor]: " << CT.Symbols[0] << '\n';
462 outs() << CTName << "[NonVirtualBaseAdjustmentOffset]: "
463 << CT.NonVirtualBaseAdjustmentOffset << '\n';
464 outs() << CTName
465 << "[VirtualBasePointerOffset]: " << CT.VirtualBasePointerOffset
466 << '\n';
467 outs() << CTName << "[VirtualBaseAdjustmentOffset]: "
468 << CT.VirtualBaseAdjustmentOffset << '\n';
David Majnemer86ee1732015-02-27 22:35:25 +0000469 outs() << CTName << "[Size]: " << CT.Size << '\n';
David Majnemerf50d0a52015-02-27 00:43:58 +0000470 outs() << CTName
471 << "[CopyCtor]: " << (CT.Symbols[1].empty() ? "null" : CT.Symbols[1])
472 << '\n';
473 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000474 for (const auto &VTTPair : VTTEntries) {
David Majnemere2683612014-11-03 07:23:25 +0000475 StringRef VTTName = VTTPair.first.first;
476 uint64_t VTTOffset = VTTPair.first.second;
477 StringRef VTTEntry = VTTPair.second;
478 outs() << VTTName << '[' << VTTOffset << "]: " << VTTEntry << '\n';
479 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000480 for (const auto &TIPair : TINames) {
David Majnemere2683612014-11-03 07:23:25 +0000481 StringRef TIName = TIPair.first;
482 outs() << TIName << ": " << TIPair.second << '\n';
483 }
484 auto VTableSymI = VTableSymEntries.begin();
485 auto VTableSymE = VTableSymEntries.end();
486 auto VTableDataI = VTableDataEntries.begin();
487 auto VTableDataE = VTableDataEntries.end();
488 for (;;) {
489 bool SymDone = VTableSymI == VTableSymE;
490 bool DataDone = VTableDataI == VTableDataE;
491 if (SymDone && DataDone)
492 break;
493 if (!SymDone && (DataDone || VTableSymI->first < VTableDataI->first)) {
494 StringRef VTableName = VTableSymI->first.first;
495 uint64_t Offset = VTableSymI->first.second;
496 StringRef VTableEntry = VTableSymI->second;
497 outs() << VTableName << '[' << Offset << "]: ";
498 outs() << VTableEntry;
499 outs() << '\n';
500 ++VTableSymI;
501 continue;
502 }
503 if (!DataDone && (SymDone || VTableDataI->first < VTableSymI->first)) {
504 StringRef VTableName = VTableDataI->first.first;
505 uint64_t Offset = VTableDataI->first.second;
506 int64_t VTableEntry = VTableDataI->second;
507 outs() << VTableName << '[' << Offset << "]: ";
508 outs() << VTableEntry;
509 outs() << '\n';
510 ++VTableDataI;
511 continue;
512 }
513 }
David Majnemer72ab1a52014-07-24 23:14:40 +0000514}
515
516static void dumpArchive(const Archive *Arc) {
David Majnemereac48b62014-09-25 22:56:54 +0000517 for (const Archive::Child &ArcC : Arc->children()) {
518 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcC.getAsBinary();
David Majnemer72ab1a52014-07-24 23:14:40 +0000519 if (std::error_code EC = ChildOrErr.getError()) {
520 // Ignore non-object files.
521 if (EC != object_error::invalid_file_type)
522 reportError(Arc->getFileName(), EC.message());
523 continue;
524 }
525
526 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
David Majnemerf45bbd02015-03-15 01:30:58 +0000527 dumpCXXData(Obj);
David Majnemer72ab1a52014-07-24 23:14:40 +0000528 else
David Majnemerf45bbd02015-03-15 01:30:58 +0000529 reportError(Arc->getFileName(), cxxdump_error::unrecognized_file_format);
David Majnemer72ab1a52014-07-24 23:14:40 +0000530 }
531}
532
533static void dumpInput(StringRef File) {
534 // If file isn't stdin, check that it exists.
535 if (File != "-" && !sys::fs::exists(File)) {
David Majnemerf45bbd02015-03-15 01:30:58 +0000536 reportError(File, cxxdump_error::file_not_found);
David Majnemer72ab1a52014-07-24 23:14:40 +0000537 return;
538 }
539
540 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000541 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
David Majnemer72ab1a52014-07-24 23:14:40 +0000542 if (std::error_code EC = BinaryOrErr.getError()) {
543 reportError(File, EC);
544 return;
545 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000546 Binary &Binary = *BinaryOrErr.get().getBinary();
David Majnemer72ab1a52014-07-24 23:14:40 +0000547
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000548 if (Archive *Arc = dyn_cast<Archive>(&Binary))
David Majnemer72ab1a52014-07-24 23:14:40 +0000549 dumpArchive(Arc);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000550 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
David Majnemerf45bbd02015-03-15 01:30:58 +0000551 dumpCXXData(Obj);
David Majnemer72ab1a52014-07-24 23:14:40 +0000552 else
David Majnemerf45bbd02015-03-15 01:30:58 +0000553 reportError(File, cxxdump_error::unrecognized_file_format);
David Majnemer72ab1a52014-07-24 23:14:40 +0000554}
555
556int main(int argc, const char *argv[]) {
557 sys::PrintStackTraceOnErrorSignal();
558 PrettyStackTraceProgram X(argc, argv);
559 llvm_shutdown_obj Y;
560
561 // Initialize targets.
562 llvm::InitializeAllTargetInfos();
563
564 // Register the target printer for --version.
565 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
566
David Majnemerf45bbd02015-03-15 01:30:58 +0000567 cl::ParseCommandLineOptions(argc, argv, "LLVM C++ ABI Data Dumper\n");
David Majnemer72ab1a52014-07-24 23:14:40 +0000568
569 // Default to stdin if no filename is specified.
570 if (opts::InputFilenames.size() == 0)
571 opts::InputFilenames.push_back("-");
572
573 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
574 dumpInput);
575
576 return ReturnValue;
577}