blob: c627a662a96254475b5a30674bb69a0f9262153f [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;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000100 ErrorOr<StringRef> RelocSymName = RelocSymI->getName();
101 if (error(RelocSymName.getError()))
David Majnemere2683612014-11-03 07:23:25 +0000102 return true;
Rafael Espindola96d071c2015-06-29 23:29:12 +0000103 uint64_t Offset = Reloc.getOffset();
David Majnemere2683612014-11-03 07:23:25 +0000104 if (Offset >= SymOffset && Offset < SymEnd) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000105 *I = *RelocSymName;
David Majnemere2683612014-11-03 07:23:25 +0000106 ++I;
107 }
108 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000109 }
110 return false;
111}
112
113static bool collectRelocationOffsets(
David Majnemere2683612014-11-03 07:23:25 +0000114 const ObjectFile *Obj, const SectionRef &Sec, uint64_t SecAddress,
115 uint64_t SymAddress, uint64_t SymSize, StringRef SymName,
David Majnemer1ac52eb2014-09-26 04:21:51 +0000116 std::map<std::pair<StringRef, uint64_t>, StringRef> &Collection) {
David Majnemere2683612014-11-03 07:23:25 +0000117 uint64_t SymOffset = SymAddress - SecAddress;
118 uint64_t SymEnd = SymOffset + SymSize;
119 for (const SectionRef &SR : getRelocSections(Obj, Sec)) {
120 for (const object::RelocationRef &Reloc : SR.relocations()) {
121 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
122 if (RelocSymI == Obj->symbol_end())
123 continue;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000124 ErrorOr<StringRef> RelocSymName = RelocSymI->getName();
125 if (error(RelocSymName.getError()))
David Majnemere2683612014-11-03 07:23:25 +0000126 return true;
Rafael Espindola96d071c2015-06-29 23:29:12 +0000127 uint64_t Offset = Reloc.getOffset();
David Majnemere2683612014-11-03 07:23:25 +0000128 if (Offset >= SymOffset && Offset < SymEnd)
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000129 Collection[std::make_pair(SymName, Offset - SymOffset)] = *RelocSymName;
David Majnemere2683612014-11-03 07:23:25 +0000130 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000131 }
132 return false;
133}
134
David Majnemerf45bbd02015-03-15 01:30:58 +0000135static void dumpCXXData(const ObjectFile *Obj) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000136 struct CompleteObjectLocator {
137 StringRef Symbols[2];
David Majnemer6887a252014-09-26 08:01:23 +0000138 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000139 };
140 struct ClassHierarchyDescriptor {
141 StringRef Symbols[1];
David Majnemer6887a252014-09-26 08:01:23 +0000142 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000143 };
144 struct BaseClassDescriptor {
145 StringRef Symbols[2];
David Majnemer6887a252014-09-26 08:01:23 +0000146 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000147 };
148 struct TypeDescriptor {
149 StringRef Symbols[1];
David Majnemer6887a252014-09-26 08:01:23 +0000150 uint64_t AlwaysZero;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000151 StringRef MangledName;
152 };
David Majnemerf50d0a52015-02-27 00:43:58 +0000153 struct ThrowInfo {
154 uint32_t Flags;
155 };
156 struct CatchableTypeArray {
157 uint32_t NumEntries;
158 };
159 struct CatchableType {
160 uint32_t Flags;
161 uint32_t NonVirtualBaseAdjustmentOffset;
162 int32_t VirtualBasePointerOffset;
163 uint32_t VirtualBaseAdjustmentOffset;
David Majnemer86ee1732015-02-27 22:35:25 +0000164 uint32_t Size;
David Majnemerf50d0a52015-02-27 00:43:58 +0000165 StringRef Symbols[2];
166 };
David Majnemer72ab1a52014-07-24 23:14:40 +0000167 std::map<std::pair<StringRef, uint64_t>, StringRef> VFTableEntries;
David Majnemerf50d0a52015-02-27 00:43:58 +0000168 std::map<std::pair<StringRef, uint64_t>, StringRef> TIEntries;
169 std::map<std::pair<StringRef, uint64_t>, StringRef> CTAEntries;
David Majnemer6887a252014-09-26 08:01:23 +0000170 std::map<StringRef, ArrayRef<little32_t>> VBTables;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000171 std::map<StringRef, CompleteObjectLocator> COLs;
172 std::map<StringRef, ClassHierarchyDescriptor> CHDs;
173 std::map<std::pair<StringRef, uint64_t>, StringRef> BCAEntries;
174 std::map<StringRef, BaseClassDescriptor> BCDs;
175 std::map<StringRef, TypeDescriptor> TDs;
David Majnemerf50d0a52015-02-27 00:43:58 +0000176 std::map<StringRef, ThrowInfo> TIs;
177 std::map<StringRef, CatchableTypeArray> CTAs;
178 std::map<StringRef, CatchableType> CTs;
David Majnemere2683612014-11-03 07:23:25 +0000179
180 std::map<std::pair<StringRef, uint64_t>, StringRef> VTableSymEntries;
181 std::map<std::pair<StringRef, uint64_t>, int64_t> VTableDataEntries;
182 std::map<std::pair<StringRef, uint64_t>, StringRef> VTTEntries;
183 std::map<StringRef, StringRef> TINames;
184
185 uint8_t BytesInAddress = Obj->getBytesInAddress();
186
Rafael Espindola6bf32212015-06-24 19:57:32 +0000187 std::vector<std::pair<SymbolRef, uint64_t>> SymAddr =
Rafael Espindolaa4a40932015-06-23 02:20:37 +0000188 object::computeSymbolSizes(*Obj);
Rafael Espindolaa4a40932015-06-23 02:20:37 +0000189
Rafael Espindola6bf32212015-06-24 19:57:32 +0000190 for (auto &P : SymAddr) {
Rafael Espindolaa4a40932015-06-23 02:20:37 +0000191 object::SymbolRef Sym = P.first;
192 uint64_t SymSize = P.second;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000193 ErrorOr<StringRef> SymNameOrErr = Sym.getName();
194 if (error(SymNameOrErr.getError()))
David Majnemer72ab1a52014-07-24 23:14:40 +0000195 return;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000196 StringRef SymName = *SymNameOrErr;
David Majnemer601327c2014-09-26 22:32:19 +0000197 object::section_iterator SecI(Obj->section_begin());
198 if (error(Sym.getSection(SecI)))
199 return;
200 // Skip external symbols.
201 if (SecI == Obj->section_end())
202 continue;
David Majnemere2683612014-11-03 07:23:25 +0000203 const SectionRef &Sec = *SecI;
David Majnemer601327c2014-09-26 22:32:19 +0000204 // Skip virtual or BSS sections.
David Majnemere2683612014-11-03 07:23:25 +0000205 if (Sec.isBSS() || Sec.isVirtual())
David Majnemer601327c2014-09-26 22:32:19 +0000206 continue;
207 StringRef SecContents;
David Majnemere2683612014-11-03 07:23:25 +0000208 if (error(Sec.getContents(SecContents)))
David Majnemer601327c2014-09-26 22:32:19 +0000209 return;
Rafael Espindola5eb02e42015-06-01 00:27:26 +0000210 uint64_t SymAddress;
211 if (error(Sym.getAddress(SymAddress)))
David Majnemere2683612014-11-03 07:23:25 +0000212 return;
213 uint64_t SecAddress = Sec.getAddress();
214 uint64_t SecSize = Sec.getSize();
215 uint64_t SymOffset = SymAddress - SecAddress;
216 StringRef SymContents = SecContents.substr(SymOffset, SymSize);
217
David Majnemer72ab1a52014-07-24 23:14:40 +0000218 // VFTables in the MS-ABI start with '??_7' and are contained within their
219 // own COMDAT section. We then determine the contents of the VFTable by
220 // looking at each relocation in the section.
221 if (SymName.startswith("??_7")) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000222 // Each relocation either names a virtual method or a thunk. We note the
223 // offset into the section and the symbol used for the relocation.
David Majnemere2683612014-11-03 07:23:25 +0000224 collectRelocationOffsets(Obj, Sec, SecAddress, SecAddress, SecSize,
225 SymName, VFTableEntries);
David Majnemer72ab1a52014-07-24 23:14:40 +0000226 }
227 // VBTables in the MS-ABI start with '??_8' and are filled with 32-bit
228 // offsets of virtual bases.
229 else if (SymName.startswith("??_8")) {
David Majnemer6887a252014-09-26 08:01:23 +0000230 ArrayRef<little32_t> VBTableData(
David Majnemere2683612014-11-03 07:23:25 +0000231 reinterpret_cast<const little32_t *>(SymContents.data()),
232 SymContents.size() / sizeof(little32_t));
David Majnemer72ab1a52014-07-24 23:14:40 +0000233 VBTables[SymName] = VBTableData;
234 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000235 // Complete object locators in the MS-ABI start with '??_R4'
236 else if (SymName.startswith("??_R4")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000237 CompleteObjectLocator COL;
David Majnemer6887a252014-09-26 08:01:23 +0000238 COL.Data = ArrayRef<little32_t>(
David Majnemere2683612014-11-03 07:23:25 +0000239 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000240 StringRef *I = std::begin(COL.Symbols), *E = std::end(COL.Symbols);
David Majnemere2683612014-11-03 07:23:25 +0000241 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
242 E))
David Majnemer1ac52eb2014-09-26 04:21:51 +0000243 return;
244 COLs[SymName] = COL;
245 }
246 // Class hierarchy descriptors in the MS-ABI start with '??_R3'
247 else if (SymName.startswith("??_R3")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000248 ClassHierarchyDescriptor CHD;
David Majnemer6887a252014-09-26 08:01:23 +0000249 CHD.Data = ArrayRef<little32_t>(
David Majnemere2683612014-11-03 07:23:25 +0000250 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000251 StringRef *I = std::begin(CHD.Symbols), *E = std::end(CHD.Symbols);
David Majnemere2683612014-11-03 07:23:25 +0000252 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
253 E))
David Majnemer1ac52eb2014-09-26 04:21:51 +0000254 return;
255 CHDs[SymName] = CHD;
256 }
257 // Class hierarchy descriptors in the MS-ABI start with '??_R2'
258 else if (SymName.startswith("??_R2")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000259 // Each relocation names a base class descriptor. We note the offset into
260 // the section and the symbol used for the relocation.
David Majnemere2683612014-11-03 07:23:25 +0000261 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
262 SymName, BCAEntries);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000263 }
264 // Base class descriptors in the MS-ABI start with '??_R1'
265 else if (SymName.startswith("??_R1")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000266 BaseClassDescriptor BCD;
David Majnemer6887a252014-09-26 08:01:23 +0000267 BCD.Data = ArrayRef<little32_t>(
David Majnemere2683612014-11-03 07:23:25 +0000268 reinterpret_cast<const little32_t *>(SymContents.data()) + 1, 5);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000269 StringRef *I = std::begin(BCD.Symbols), *E = std::end(BCD.Symbols);
David Majnemere2683612014-11-03 07:23:25 +0000270 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
271 E))
David Majnemer1ac52eb2014-09-26 04:21:51 +0000272 return;
273 BCDs[SymName] = BCD;
274 }
275 // Type descriptors in the MS-ABI start with '??_R0'
276 else if (SymName.startswith("??_R0")) {
David Majnemere2683612014-11-03 07:23:25 +0000277 const char *DataPtr = SymContents.drop_front(BytesInAddress).data();
David Majnemer1ac52eb2014-09-26 04:21:51 +0000278 TypeDescriptor TD;
David Majnemer6887a252014-09-26 08:01:23 +0000279 if (BytesInAddress == 8)
280 TD.AlwaysZero = *reinterpret_cast<const little64_t *>(DataPtr);
281 else
282 TD.AlwaysZero = *reinterpret_cast<const little32_t *>(DataPtr);
David Majnemere2683612014-11-03 07:23:25 +0000283 TD.MangledName = SymContents.drop_front(BytesInAddress * 2);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000284 StringRef *I = std::begin(TD.Symbols), *E = std::end(TD.Symbols);
David Majnemere2683612014-11-03 07:23:25 +0000285 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
286 E))
David Majnemer1ac52eb2014-09-26 04:21:51 +0000287 return;
288 TDs[SymName] = TD;
289 }
David Majnemerf50d0a52015-02-27 00:43:58 +0000290 // Throw descriptors in the MS-ABI start with '_TI'
291 else if (SymName.startswith("_TI") || SymName.startswith("__TI")) {
292 ThrowInfo TI;
293 TI.Flags = *reinterpret_cast<const little32_t *>(SymContents.data());
294 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
295 SymName, TIEntries);
296 TIs[SymName] = TI;
297 }
298 // Catchable type arrays in the MS-ABI start with _CTA or __CTA.
299 else if (SymName.startswith("_CTA") || SymName.startswith("__CTA")) {
300 CatchableTypeArray CTA;
301 CTA.NumEntries =
302 *reinterpret_cast<const little32_t *>(SymContents.data());
303 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
304 SymName, CTAEntries);
305 CTAs[SymName] = CTA;
306 }
307 // Catchable types in the MS-ABI start with _CT or __CT.
308 else if (SymName.startswith("_CT") || SymName.startswith("__CT")) {
309 const little32_t *DataPtr =
310 reinterpret_cast<const little32_t *>(SymContents.data());
311 CatchableType CT;
312 CT.Flags = DataPtr[0];
313 CT.NonVirtualBaseAdjustmentOffset = DataPtr[2];
314 CT.VirtualBasePointerOffset = DataPtr[3];
315 CT.VirtualBaseAdjustmentOffset = DataPtr[4];
David Majnemer86ee1732015-02-27 22:35:25 +0000316 CT.Size = DataPtr[5];
David Majnemerf50d0a52015-02-27 00:43:58 +0000317 StringRef *I = std::begin(CT.Symbols), *E = std::end(CT.Symbols);
318 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
319 E))
320 return;
321 CTs[SymName] = CT;
322 }
David Majnemere2683612014-11-03 07:23:25 +0000323 // Construction vtables in the Itanium ABI start with '_ZTT' or '__ZTT'.
324 else if (SymName.startswith("_ZTT") || SymName.startswith("__ZTT")) {
325 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
326 SymName, VTTEntries);
327 }
328 // Typeinfo names in the Itanium ABI start with '_ZTS' or '__ZTS'.
329 else if (SymName.startswith("_ZTS") || SymName.startswith("__ZTS")) {
330 TINames[SymName] = SymContents.slice(0, SymContents.find('\0'));
331 }
332 // Vtables in the Itanium ABI start with '_ZTV' or '__ZTV'.
333 else if (SymName.startswith("_ZTV") || SymName.startswith("__ZTV")) {
334 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
335 SymName, VTableSymEntries);
336 for (uint64_t SymOffI = 0; SymOffI < SymSize; SymOffI += BytesInAddress) {
337 auto Key = std::make_pair(SymName, SymOffI);
338 if (VTableSymEntries.count(Key))
339 continue;
David Majnemerf45bbd02015-03-15 01:30:58 +0000340 const char *DataPtr =
341 SymContents.substr(SymOffI, BytesInAddress).data();
David Majnemere2683612014-11-03 07:23:25 +0000342 int64_t VData;
343 if (BytesInAddress == 8)
344 VData = *reinterpret_cast<const little64_t *>(DataPtr);
345 else
346 VData = *reinterpret_cast<const little32_t *>(DataPtr);
347 VTableDataEntries[Key] = VData;
348 }
349 }
350 // Typeinfo structures in the Itanium ABI start with '_ZTI' or '__ZTI'.
351 else if (SymName.startswith("_ZTI") || SymName.startswith("__ZTI")) {
352 // FIXME: Do something with these!
353 }
David Majnemer72ab1a52014-07-24 23:14:40 +0000354 }
David Majnemerf45bbd02015-03-15 01:30:58 +0000355 for (const auto &VFTableEntry : VFTableEntries) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000356 StringRef VFTableName = VFTableEntry.first.first;
357 uint64_t Offset = VFTableEntry.first.second;
358 StringRef SymName = VFTableEntry.second;
359 outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n';
360 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000361 for (const auto &VBTable : VBTables) {
David Majnemerbf32f772014-07-25 04:30:11 +0000362 StringRef VBTableName = VBTable.first;
David Majnemer72ab1a52014-07-24 23:14:40 +0000363 uint32_t Idx = 0;
David Majnemer6887a252014-09-26 08:01:23 +0000364 for (little32_t Offset : VBTable.second) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000365 outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n';
David Majnemerbf32f772014-07-25 04:30:11 +0000366 Idx += sizeof(Offset);
David Majnemer72ab1a52014-07-24 23:14:40 +0000367 }
368 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000369 for (const auto &COLPair : COLs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000370 StringRef COLName = COLPair.first;
371 const CompleteObjectLocator &COL = COLPair.second;
372 outs() << COLName << "[IsImageRelative]: " << COL.Data[0] << '\n';
373 outs() << COLName << "[OffsetToTop]: " << COL.Data[1] << '\n';
374 outs() << COLName << "[VFPtrOffset]: " << COL.Data[2] << '\n';
375 outs() << COLName << "[TypeDescriptor]: " << COL.Symbols[0] << '\n';
David Majnemerf45bbd02015-03-15 01:30:58 +0000376 outs() << COLName << "[ClassHierarchyDescriptor]: " << COL.Symbols[1]
377 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000378 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000379 for (const auto &CHDPair : 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 Trieu46f2cc92015-04-15 03:17:49 +0000387 for (const auto &BCAEntry : BCAEntries) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000388 StringRef BCAName = BCAEntry.first.first;
389 uint64_t Offset = BCAEntry.first.second;
390 StringRef SymName = BCAEntry.second;
391 outs() << BCAName << '[' << Offset << "]: " << SymName << '\n';
392 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000393 for (const auto &BCDPair : BCDs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000394 StringRef BCDName = BCDPair.first;
395 const BaseClassDescriptor &BCD = BCDPair.second;
396 outs() << BCDName << "[TypeDescriptor]: " << BCD.Symbols[0] << '\n';
397 outs() << BCDName << "[NumBases]: " << BCD.Data[0] << '\n';
398 outs() << BCDName << "[OffsetInVBase]: " << BCD.Data[1] << '\n';
399 outs() << BCDName << "[VBPtrOffset]: " << BCD.Data[2] << '\n';
400 outs() << BCDName << "[OffsetInVBTable]: " << BCD.Data[3] << '\n';
401 outs() << BCDName << "[Flags]: " << BCD.Data[4] << '\n';
David Majnemerf45bbd02015-03-15 01:30:58 +0000402 outs() << BCDName << "[ClassHierarchyDescriptor]: " << BCD.Symbols[1]
403 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000404 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000405 for (const auto &TDPair : TDs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000406 StringRef TDName = TDPair.first;
407 const TypeDescriptor &TD = TDPair.second;
408 outs() << TDName << "[VFPtr]: " << TD.Symbols[0] << '\n';
David Majnemer6887a252014-09-26 08:01:23 +0000409 outs() << TDName << "[AlwaysZero]: " << TD.AlwaysZero << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000410 outs() << TDName << "[MangledName]: ";
David Majnemer56167c32014-09-26 05:50:45 +0000411 outs().write_escaped(TD.MangledName.rtrim(StringRef("\0", 1)),
412 /*UseHexEscapes=*/true)
413 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000414 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000415 for (const auto &TIPair : TIs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000416 StringRef TIName = TIPair.first;
417 const ThrowInfo &TI = TIPair.second;
418 auto dumpThrowInfoFlag = [&](const char *Name, uint32_t Flag) {
419 outs() << TIName << "[Flags." << Name
420 << "]: " << (TI.Flags & Flag ? "true" : "false") << '\n';
421 };
422 auto dumpThrowInfoSymbol = [&](const char *Name, int Offset) {
423 outs() << TIName << '[' << Name << "]: ";
424 auto Entry = TIEntries.find(std::make_pair(TIName, Offset));
425 outs() << (Entry == TIEntries.end() ? "null" : Entry->second) << '\n';
426 };
427 outs() << TIName << "[Flags]: " << TI.Flags << '\n';
428 dumpThrowInfoFlag("Const", 1);
429 dumpThrowInfoFlag("Volatile", 2);
430 dumpThrowInfoSymbol("CleanupFn", 4);
431 dumpThrowInfoSymbol("ForwardCompat", 8);
432 dumpThrowInfoSymbol("CatchableTypeArray", 12);
433 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000434 for (const auto &CTAPair : CTAs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000435 StringRef CTAName = CTAPair.first;
436 const CatchableTypeArray &CTA = CTAPair.second;
437
438 outs() << CTAName << "[NumEntries]: " << CTA.NumEntries << '\n';
439
440 unsigned Idx = 0;
441 for (auto I = CTAEntries.lower_bound(std::make_pair(CTAName, 0)),
442 E = CTAEntries.upper_bound(std::make_pair(CTAName, UINT64_MAX));
443 I != E; ++I)
444 outs() << CTAName << '[' << Idx++ << "]: " << I->second << '\n';
445 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000446 for (const auto &CTPair : CTs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000447 StringRef CTName = CTPair.first;
448 const CatchableType &CT = CTPair.second;
449 auto dumpCatchableTypeFlag = [&](const char *Name, uint32_t Flag) {
450 outs() << CTName << "[Flags." << Name
451 << "]: " << (CT.Flags & Flag ? "true" : "false") << '\n';
452 };
453 outs() << CTName << "[Flags]: " << CT.Flags << '\n';
454 dumpCatchableTypeFlag("ScalarType", 1);
455 dumpCatchableTypeFlag("VirtualInheritance", 4);
456 outs() << CTName << "[TypeDescriptor]: " << CT.Symbols[0] << '\n';
457 outs() << CTName << "[NonVirtualBaseAdjustmentOffset]: "
458 << CT.NonVirtualBaseAdjustmentOffset << '\n';
459 outs() << CTName
460 << "[VirtualBasePointerOffset]: " << CT.VirtualBasePointerOffset
461 << '\n';
462 outs() << CTName << "[VirtualBaseAdjustmentOffset]: "
463 << CT.VirtualBaseAdjustmentOffset << '\n';
David Majnemer86ee1732015-02-27 22:35:25 +0000464 outs() << CTName << "[Size]: " << CT.Size << '\n';
David Majnemerf50d0a52015-02-27 00:43:58 +0000465 outs() << CTName
466 << "[CopyCtor]: " << (CT.Symbols[1].empty() ? "null" : CT.Symbols[1])
467 << '\n';
468 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000469 for (const auto &VTTPair : VTTEntries) {
David Majnemere2683612014-11-03 07:23:25 +0000470 StringRef VTTName = VTTPair.first.first;
471 uint64_t VTTOffset = VTTPair.first.second;
472 StringRef VTTEntry = VTTPair.second;
473 outs() << VTTName << '[' << VTTOffset << "]: " << VTTEntry << '\n';
474 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000475 for (const auto &TIPair : TINames) {
David Majnemere2683612014-11-03 07:23:25 +0000476 StringRef TIName = TIPair.first;
477 outs() << TIName << ": " << TIPair.second << '\n';
478 }
479 auto VTableSymI = VTableSymEntries.begin();
480 auto VTableSymE = VTableSymEntries.end();
481 auto VTableDataI = VTableDataEntries.begin();
482 auto VTableDataE = VTableDataEntries.end();
483 for (;;) {
484 bool SymDone = VTableSymI == VTableSymE;
485 bool DataDone = VTableDataI == VTableDataE;
486 if (SymDone && DataDone)
487 break;
488 if (!SymDone && (DataDone || VTableSymI->first < VTableDataI->first)) {
489 StringRef VTableName = VTableSymI->first.first;
490 uint64_t Offset = VTableSymI->first.second;
491 StringRef VTableEntry = VTableSymI->second;
492 outs() << VTableName << '[' << Offset << "]: ";
493 outs() << VTableEntry;
494 outs() << '\n';
495 ++VTableSymI;
496 continue;
497 }
498 if (!DataDone && (SymDone || VTableDataI->first < VTableSymI->first)) {
499 StringRef VTableName = VTableDataI->first.first;
500 uint64_t Offset = VTableDataI->first.second;
501 int64_t VTableEntry = VTableDataI->second;
502 outs() << VTableName << '[' << Offset << "]: ";
503 outs() << VTableEntry;
504 outs() << '\n';
505 ++VTableDataI;
506 continue;
507 }
508 }
David Majnemer72ab1a52014-07-24 23:14:40 +0000509}
510
511static void dumpArchive(const Archive *Arc) {
David Majnemereac48b62014-09-25 22:56:54 +0000512 for (const Archive::Child &ArcC : Arc->children()) {
513 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcC.getAsBinary();
David Majnemer72ab1a52014-07-24 23:14:40 +0000514 if (std::error_code EC = ChildOrErr.getError()) {
515 // Ignore non-object files.
516 if (EC != object_error::invalid_file_type)
517 reportError(Arc->getFileName(), EC.message());
518 continue;
519 }
520
521 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
David Majnemerf45bbd02015-03-15 01:30:58 +0000522 dumpCXXData(Obj);
David Majnemer72ab1a52014-07-24 23:14:40 +0000523 else
David Majnemerf45bbd02015-03-15 01:30:58 +0000524 reportError(Arc->getFileName(), cxxdump_error::unrecognized_file_format);
David Majnemer72ab1a52014-07-24 23:14:40 +0000525 }
526}
527
528static void dumpInput(StringRef File) {
529 // If file isn't stdin, check that it exists.
530 if (File != "-" && !sys::fs::exists(File)) {
David Majnemerf45bbd02015-03-15 01:30:58 +0000531 reportError(File, cxxdump_error::file_not_found);
David Majnemer72ab1a52014-07-24 23:14:40 +0000532 return;
533 }
534
535 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000536 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
David Majnemer72ab1a52014-07-24 23:14:40 +0000537 if (std::error_code EC = BinaryOrErr.getError()) {
538 reportError(File, EC);
539 return;
540 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000541 Binary &Binary = *BinaryOrErr.get().getBinary();
David Majnemer72ab1a52014-07-24 23:14:40 +0000542
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000543 if (Archive *Arc = dyn_cast<Archive>(&Binary))
David Majnemer72ab1a52014-07-24 23:14:40 +0000544 dumpArchive(Arc);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000545 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
David Majnemerf45bbd02015-03-15 01:30:58 +0000546 dumpCXXData(Obj);
David Majnemer72ab1a52014-07-24 23:14:40 +0000547 else
David Majnemerf45bbd02015-03-15 01:30:58 +0000548 reportError(File, cxxdump_error::unrecognized_file_format);
David Majnemer72ab1a52014-07-24 23:14:40 +0000549}
550
551int main(int argc, const char *argv[]) {
552 sys::PrintStackTraceOnErrorSignal();
553 PrettyStackTraceProgram X(argc, argv);
554 llvm_shutdown_obj Y;
555
556 // Initialize targets.
557 llvm::InitializeAllTargetInfos();
558
559 // Register the target printer for --version.
560 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
561
David Majnemerf45bbd02015-03-15 01:30:58 +0000562 cl::ParseCommandLineOptions(argc, argv, "LLVM C++ ABI Data Dumper\n");
David Majnemer72ab1a52014-07-24 23:14:40 +0000563
564 // Default to stdin if no filename is specified.
565 if (opts::InputFilenames.size() == 0)
566 opts::InputFilenames.push_back("-");
567
568 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
569 dumpInput);
570
571 return ReturnValue;
572}