blob: 044798297d8c7ed02a31a080a03a5d3d8b50c46f [file] [log] [blame]
David Majnemer72ab1a52014-07-24 23:14:40 +00001//===- llvm-vtabledump.cpp - Dump vtables in an Object File -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
David Majnemerf50d0a52015-02-27 00:43:58 +000010// Dumps VTables resident in object files and archives.
David Majnemer72ab1a52014-07-24 23:14:40 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm-vtabledump.h"
15#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"
27#include <map>
28#include <string>
29#include <system_error>
30
31using namespace llvm;
32using namespace llvm::object;
33using namespace llvm::support;
34
35namespace opts {
36cl::list<std::string> InputFilenames(cl::Positional,
37 cl::desc("<input object files>"),
38 cl::ZeroOrMore);
39} // namespace opts
40
41static int ReturnValue = EXIT_SUCCESS;
42
43namespace llvm {
44
45bool error(std::error_code EC) {
46 if (!EC)
47 return false;
48
49 ReturnValue = EXIT_FAILURE;
50 outs() << "\nError reading file: " << EC.message() << ".\n";
51 outs().flush();
52 return true;
53}
54
55} // namespace llvm
56
57static void reportError(StringRef Input, StringRef Message) {
58 if (Input == "-")
59 Input = "<stdin>";
60
61 errs() << Input << ": " << Message << "\n";
62 errs().flush();
63 ReturnValue = EXIT_FAILURE;
64}
65
66static void reportError(StringRef Input, std::error_code EC) {
67 reportError(Input, EC.message());
68}
69
David Majnemere2683612014-11-03 07:23:25 +000070static SmallVectorImpl<SectionRef> &getRelocSections(const ObjectFile *Obj,
71 const SectionRef &Sec) {
72 static bool MappingDone = false;
73 static std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
74 if (!MappingDone) {
75 for (const SectionRef &Section : Obj->sections()) {
76 section_iterator Sec2 = Section.getRelocatedSection();
77 if (Sec2 != Obj->section_end())
78 SectionRelocMap[*Sec2].push_back(Section);
79 }
80 MappingDone = true;
81 }
82 return SectionRelocMap[Sec];
83}
84
David Majnemer1ac52eb2014-09-26 04:21:51 +000085static bool collectRelocatedSymbols(const ObjectFile *Obj,
David Majnemere2683612014-11-03 07:23:25 +000086 const SectionRef &Sec, uint64_t SecAddress,
87 uint64_t SymAddress, uint64_t SymSize,
88 StringRef *I, StringRef *E) {
89 uint64_t SymOffset = SymAddress - SecAddress;
90 uint64_t SymEnd = SymOffset + SymSize;
91 for (const SectionRef &SR : getRelocSections(Obj, Sec)) {
92 for (const object::RelocationRef &Reloc : SR.relocations()) {
93 if (I == E)
94 break;
95 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
96 if (RelocSymI == Obj->symbol_end())
97 continue;
98 StringRef RelocSymName;
99 if (error(RelocSymI->getName(RelocSymName)))
100 return true;
101 uint64_t Offset;
102 if (error(Reloc.getOffset(Offset)))
103 return true;
104 if (Offset >= SymOffset && Offset < SymEnd) {
105 *I = RelocSymName;
106 ++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;
124 StringRef RelocSymName;
125 if (error(RelocSymI->getName(RelocSymName)))
126 return true;
127 uint64_t Offset;
128 if (error(Reloc.getOffset(Offset)))
129 return true;
130 if (Offset >= SymOffset && Offset < SymEnd)
131 Collection[std::make_pair(SymName, Offset - SymOffset)] = RelocSymName;
132 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000133 }
134 return false;
135}
136
David Majnemer72ab1a52014-07-24 23:14:40 +0000137static void dumpVTables(const ObjectFile *Obj) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000138 struct CompleteObjectLocator {
139 StringRef Symbols[2];
David Majnemer6887a252014-09-26 08:01:23 +0000140 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000141 };
142 struct ClassHierarchyDescriptor {
143 StringRef Symbols[1];
David Majnemer6887a252014-09-26 08:01:23 +0000144 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000145 };
146 struct BaseClassDescriptor {
147 StringRef Symbols[2];
David Majnemer6887a252014-09-26 08:01:23 +0000148 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000149 };
150 struct TypeDescriptor {
151 StringRef Symbols[1];
David Majnemer6887a252014-09-26 08:01:23 +0000152 uint64_t AlwaysZero;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000153 StringRef MangledName;
154 };
David Majnemerf50d0a52015-02-27 00:43:58 +0000155 struct ThrowInfo {
156 uint32_t Flags;
157 };
158 struct CatchableTypeArray {
159 uint32_t NumEntries;
160 };
161 struct CatchableType {
162 uint32_t Flags;
163 uint32_t NonVirtualBaseAdjustmentOffset;
164 int32_t VirtualBasePointerOffset;
165 uint32_t VirtualBaseAdjustmentOffset;
166 uint32_t SizeOrOffset;
167 StringRef Symbols[2];
168 };
David Majnemer72ab1a52014-07-24 23:14:40 +0000169 std::map<std::pair<StringRef, uint64_t>, StringRef> VFTableEntries;
David Majnemerf50d0a52015-02-27 00:43:58 +0000170 std::map<std::pair<StringRef, uint64_t>, StringRef> TIEntries;
171 std::map<std::pair<StringRef, uint64_t>, StringRef> CTAEntries;
David Majnemer6887a252014-09-26 08:01:23 +0000172 std::map<StringRef, ArrayRef<little32_t>> VBTables;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000173 std::map<StringRef, CompleteObjectLocator> COLs;
174 std::map<StringRef, ClassHierarchyDescriptor> CHDs;
175 std::map<std::pair<StringRef, uint64_t>, StringRef> BCAEntries;
176 std::map<StringRef, BaseClassDescriptor> BCDs;
177 std::map<StringRef, TypeDescriptor> TDs;
David Majnemerf50d0a52015-02-27 00:43:58 +0000178 std::map<StringRef, ThrowInfo> TIs;
179 std::map<StringRef, CatchableTypeArray> CTAs;
180 std::map<StringRef, CatchableType> CTs;
David Majnemere2683612014-11-03 07:23:25 +0000181
182 std::map<std::pair<StringRef, uint64_t>, StringRef> VTableSymEntries;
183 std::map<std::pair<StringRef, uint64_t>, int64_t> VTableDataEntries;
184 std::map<std::pair<StringRef, uint64_t>, StringRef> VTTEntries;
185 std::map<StringRef, StringRef> TINames;
186
187 uint8_t BytesInAddress = Obj->getBytesInAddress();
188
David Majnemer72ab1a52014-07-24 23:14:40 +0000189 for (const object::SymbolRef &Sym : Obj->symbols()) {
190 StringRef SymName;
191 if (error(Sym.getName(SymName)))
192 return;
David Majnemer601327c2014-09-26 22:32:19 +0000193 object::section_iterator SecI(Obj->section_begin());
194 if (error(Sym.getSection(SecI)))
195 return;
196 // Skip external symbols.
197 if (SecI == Obj->section_end())
198 continue;
David Majnemere2683612014-11-03 07:23:25 +0000199 const SectionRef &Sec = *SecI;
David Majnemer601327c2014-09-26 22:32:19 +0000200 // Skip virtual or BSS sections.
David Majnemere2683612014-11-03 07:23:25 +0000201 if (Sec.isBSS() || Sec.isVirtual())
David Majnemer601327c2014-09-26 22:32:19 +0000202 continue;
203 StringRef SecContents;
David Majnemere2683612014-11-03 07:23:25 +0000204 if (error(Sec.getContents(SecContents)))
David Majnemer601327c2014-09-26 22:32:19 +0000205 return;
David Majnemere2683612014-11-03 07:23:25 +0000206 uint64_t SymAddress, SymSize;
207 if (error(Sym.getAddress(SymAddress)) || error(Sym.getSize(SymSize)))
208 return;
209 uint64_t SecAddress = Sec.getAddress();
210 uint64_t SecSize = Sec.getSize();
211 uint64_t SymOffset = SymAddress - SecAddress;
212 StringRef SymContents = SecContents.substr(SymOffset, SymSize);
213
David Majnemer72ab1a52014-07-24 23:14:40 +0000214 // VFTables in the MS-ABI start with '??_7' and are contained within their
215 // own COMDAT section. We then determine the contents of the VFTable by
216 // looking at each relocation in the section.
217 if (SymName.startswith("??_7")) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000218 // Each relocation either names a virtual method or a thunk. We note the
219 // offset into the section and the symbol used for the relocation.
David Majnemere2683612014-11-03 07:23:25 +0000220 collectRelocationOffsets(Obj, Sec, SecAddress, SecAddress, SecSize,
221 SymName, VFTableEntries);
David Majnemer72ab1a52014-07-24 23:14:40 +0000222 }
223 // VBTables in the MS-ABI start with '??_8' and are filled with 32-bit
224 // offsets of virtual bases.
225 else if (SymName.startswith("??_8")) {
David Majnemer6887a252014-09-26 08:01:23 +0000226 ArrayRef<little32_t> VBTableData(
David Majnemere2683612014-11-03 07:23:25 +0000227 reinterpret_cast<const little32_t *>(SymContents.data()),
228 SymContents.size() / sizeof(little32_t));
David Majnemer72ab1a52014-07-24 23:14:40 +0000229 VBTables[SymName] = VBTableData;
230 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000231 // Complete object locators in the MS-ABI start with '??_R4'
232 else if (SymName.startswith("??_R4")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000233 CompleteObjectLocator COL;
David Majnemer6887a252014-09-26 08:01:23 +0000234 COL.Data = ArrayRef<little32_t>(
David Majnemere2683612014-11-03 07:23:25 +0000235 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000236 StringRef *I = std::begin(COL.Symbols), *E = std::end(COL.Symbols);
David Majnemere2683612014-11-03 07:23:25 +0000237 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
238 E))
David Majnemer1ac52eb2014-09-26 04:21:51 +0000239 return;
240 COLs[SymName] = COL;
241 }
242 // Class hierarchy descriptors in the MS-ABI start with '??_R3'
243 else if (SymName.startswith("??_R3")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000244 ClassHierarchyDescriptor CHD;
David Majnemer6887a252014-09-26 08:01:23 +0000245 CHD.Data = ArrayRef<little32_t>(
David Majnemere2683612014-11-03 07:23:25 +0000246 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000247 StringRef *I = std::begin(CHD.Symbols), *E = std::end(CHD.Symbols);
David Majnemere2683612014-11-03 07:23:25 +0000248 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
249 E))
David Majnemer1ac52eb2014-09-26 04:21:51 +0000250 return;
251 CHDs[SymName] = CHD;
252 }
253 // Class hierarchy descriptors in the MS-ABI start with '??_R2'
254 else if (SymName.startswith("??_R2")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000255 // Each relocation names a base class descriptor. We note the offset into
256 // the section and the symbol used for the relocation.
David Majnemere2683612014-11-03 07:23:25 +0000257 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
258 SymName, BCAEntries);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000259 }
260 // Base class descriptors in the MS-ABI start with '??_R1'
261 else if (SymName.startswith("??_R1")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000262 BaseClassDescriptor BCD;
David Majnemer6887a252014-09-26 08:01:23 +0000263 BCD.Data = ArrayRef<little32_t>(
David Majnemere2683612014-11-03 07:23:25 +0000264 reinterpret_cast<const little32_t *>(SymContents.data()) + 1, 5);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000265 StringRef *I = std::begin(BCD.Symbols), *E = std::end(BCD.Symbols);
David Majnemere2683612014-11-03 07:23:25 +0000266 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
267 E))
David Majnemer1ac52eb2014-09-26 04:21:51 +0000268 return;
269 BCDs[SymName] = BCD;
270 }
271 // Type descriptors in the MS-ABI start with '??_R0'
272 else if (SymName.startswith("??_R0")) {
David Majnemere2683612014-11-03 07:23:25 +0000273 const char *DataPtr = SymContents.drop_front(BytesInAddress).data();
David Majnemer1ac52eb2014-09-26 04:21:51 +0000274 TypeDescriptor TD;
David Majnemer6887a252014-09-26 08:01:23 +0000275 if (BytesInAddress == 8)
276 TD.AlwaysZero = *reinterpret_cast<const little64_t *>(DataPtr);
277 else
278 TD.AlwaysZero = *reinterpret_cast<const little32_t *>(DataPtr);
David Majnemere2683612014-11-03 07:23:25 +0000279 TD.MangledName = SymContents.drop_front(BytesInAddress * 2);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000280 StringRef *I = std::begin(TD.Symbols), *E = std::end(TD.Symbols);
David Majnemere2683612014-11-03 07:23:25 +0000281 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
282 E))
David Majnemer1ac52eb2014-09-26 04:21:51 +0000283 return;
284 TDs[SymName] = TD;
285 }
David Majnemerf50d0a52015-02-27 00:43:58 +0000286 // Throw descriptors in the MS-ABI start with '_TI'
287 else if (SymName.startswith("_TI") || SymName.startswith("__TI")) {
288 ThrowInfo TI;
289 TI.Flags = *reinterpret_cast<const little32_t *>(SymContents.data());
290 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
291 SymName, TIEntries);
292 TIs[SymName] = TI;
293 }
294 // Catchable type arrays in the MS-ABI start with _CTA or __CTA.
295 else if (SymName.startswith("_CTA") || SymName.startswith("__CTA")) {
296 CatchableTypeArray CTA;
297 CTA.NumEntries =
298 *reinterpret_cast<const little32_t *>(SymContents.data());
299 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
300 SymName, CTAEntries);
301 CTAs[SymName] = CTA;
302 }
303 // Catchable types in the MS-ABI start with _CT or __CT.
304 else if (SymName.startswith("_CT") || SymName.startswith("__CT")) {
305 const little32_t *DataPtr =
306 reinterpret_cast<const little32_t *>(SymContents.data());
307 CatchableType CT;
308 CT.Flags = DataPtr[0];
309 CT.NonVirtualBaseAdjustmentOffset = DataPtr[2];
310 CT.VirtualBasePointerOffset = DataPtr[3];
311 CT.VirtualBaseAdjustmentOffset = DataPtr[4];
312 CT.SizeOrOffset = DataPtr[5];
313 StringRef *I = std::begin(CT.Symbols), *E = std::end(CT.Symbols);
314 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
315 E))
316 return;
317 CTs[SymName] = CT;
318 }
David Majnemere2683612014-11-03 07:23:25 +0000319 // Construction vtables in the Itanium ABI start with '_ZTT' or '__ZTT'.
320 else if (SymName.startswith("_ZTT") || SymName.startswith("__ZTT")) {
321 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
322 SymName, VTTEntries);
323 }
324 // Typeinfo names in the Itanium ABI start with '_ZTS' or '__ZTS'.
325 else if (SymName.startswith("_ZTS") || SymName.startswith("__ZTS")) {
326 TINames[SymName] = SymContents.slice(0, SymContents.find('\0'));
327 }
328 // Vtables in the Itanium ABI start with '_ZTV' or '__ZTV'.
329 else if (SymName.startswith("_ZTV") || SymName.startswith("__ZTV")) {
330 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
331 SymName, VTableSymEntries);
332 for (uint64_t SymOffI = 0; SymOffI < SymSize; SymOffI += BytesInAddress) {
333 auto Key = std::make_pair(SymName, SymOffI);
334 if (VTableSymEntries.count(Key))
335 continue;
336 const char *DataPtr = SymContents.substr(SymOffI, BytesInAddress).data();
337 int64_t VData;
338 if (BytesInAddress == 8)
339 VData = *reinterpret_cast<const little64_t *>(DataPtr);
340 else
341 VData = *reinterpret_cast<const little32_t *>(DataPtr);
342 VTableDataEntries[Key] = VData;
343 }
344 }
345 // Typeinfo structures in the Itanium ABI start with '_ZTI' or '__ZTI'.
346 else if (SymName.startswith("_ZTI") || SymName.startswith("__ZTI")) {
347 // FIXME: Do something with these!
348 }
David Majnemer72ab1a52014-07-24 23:14:40 +0000349 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000350 for (const std::pair<std::pair<StringRef, uint64_t>, StringRef> &VFTableEntry :
351 VFTableEntries) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000352 StringRef VFTableName = VFTableEntry.first.first;
353 uint64_t Offset = VFTableEntry.first.second;
354 StringRef SymName = VFTableEntry.second;
355 outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n';
356 }
David Majnemere2683612014-11-03 07:23:25 +0000357 for (const std::pair<StringRef, ArrayRef<little32_t>> &VBTable : VBTables) {
David Majnemerbf32f772014-07-25 04:30:11 +0000358 StringRef VBTableName = VBTable.first;
David Majnemer72ab1a52014-07-24 23:14:40 +0000359 uint32_t Idx = 0;
David Majnemer6887a252014-09-26 08:01:23 +0000360 for (little32_t Offset : VBTable.second) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000361 outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n';
David Majnemerbf32f772014-07-25 04:30:11 +0000362 Idx += sizeof(Offset);
David Majnemer72ab1a52014-07-24 23:14:40 +0000363 }
364 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000365 for (const std::pair<StringRef, CompleteObjectLocator> &COLPair : COLs) {
366 StringRef COLName = COLPair.first;
367 const CompleteObjectLocator &COL = COLPair.second;
368 outs() << COLName << "[IsImageRelative]: " << COL.Data[0] << '\n';
369 outs() << COLName << "[OffsetToTop]: " << COL.Data[1] << '\n';
370 outs() << COLName << "[VFPtrOffset]: " << COL.Data[2] << '\n';
371 outs() << COLName << "[TypeDescriptor]: " << COL.Symbols[0] << '\n';
372 outs() << COLName << "[ClassHierarchyDescriptor]: " << COL.Symbols[1] << '\n';
373 }
374 for (const std::pair<StringRef, ClassHierarchyDescriptor> &CHDPair : CHDs) {
375 StringRef CHDName = CHDPair.first;
376 const ClassHierarchyDescriptor &CHD = CHDPair.second;
377 outs() << CHDName << "[AlwaysZero]: " << CHD.Data[0] << '\n';
378 outs() << CHDName << "[Flags]: " << CHD.Data[1] << '\n';
379 outs() << CHDName << "[NumClasses]: " << CHD.Data[2] << '\n';
380 outs() << CHDName << "[BaseClassArray]: " << CHD.Symbols[0] << '\n';
381 }
382 for (const std::pair<std::pair<StringRef, uint64_t>, StringRef> &BCAEntry :
383 BCAEntries) {
384 StringRef BCAName = BCAEntry.first.first;
385 uint64_t Offset = BCAEntry.first.second;
386 StringRef SymName = BCAEntry.second;
387 outs() << BCAName << '[' << Offset << "]: " << SymName << '\n';
388 }
389 for (const std::pair<StringRef, BaseClassDescriptor> &BCDPair : BCDs) {
390 StringRef BCDName = BCDPair.first;
391 const BaseClassDescriptor &BCD = BCDPair.second;
392 outs() << BCDName << "[TypeDescriptor]: " << BCD.Symbols[0] << '\n';
393 outs() << BCDName << "[NumBases]: " << BCD.Data[0] << '\n';
394 outs() << BCDName << "[OffsetInVBase]: " << BCD.Data[1] << '\n';
395 outs() << BCDName << "[VBPtrOffset]: " << BCD.Data[2] << '\n';
396 outs() << BCDName << "[OffsetInVBTable]: " << BCD.Data[3] << '\n';
397 outs() << BCDName << "[Flags]: " << BCD.Data[4] << '\n';
398 outs() << BCDName << "[ClassHierarchyDescriptor]: " << BCD.Symbols[1] << '\n';
399 }
400 for (const std::pair<StringRef, TypeDescriptor> &TDPair : TDs) {
401 StringRef TDName = TDPair.first;
402 const TypeDescriptor &TD = TDPair.second;
403 outs() << TDName << "[VFPtr]: " << TD.Symbols[0] << '\n';
David Majnemer6887a252014-09-26 08:01:23 +0000404 outs() << TDName << "[AlwaysZero]: " << TD.AlwaysZero << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000405 outs() << TDName << "[MangledName]: ";
David Majnemer56167c32014-09-26 05:50:45 +0000406 outs().write_escaped(TD.MangledName.rtrim(StringRef("\0", 1)),
407 /*UseHexEscapes=*/true)
408 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000409 }
David Majnemerf50d0a52015-02-27 00:43:58 +0000410 for (const std::pair<StringRef, ThrowInfo> &TIPair : TIs) {
411 StringRef TIName = TIPair.first;
412 const ThrowInfo &TI = TIPair.second;
413 auto dumpThrowInfoFlag = [&](const char *Name, uint32_t Flag) {
414 outs() << TIName << "[Flags." << Name
415 << "]: " << (TI.Flags & Flag ? "true" : "false") << '\n';
416 };
417 auto dumpThrowInfoSymbol = [&](const char *Name, int Offset) {
418 outs() << TIName << '[' << Name << "]: ";
419 auto Entry = TIEntries.find(std::make_pair(TIName, Offset));
420 outs() << (Entry == TIEntries.end() ? "null" : Entry->second) << '\n';
421 };
422 outs() << TIName << "[Flags]: " << TI.Flags << '\n';
423 dumpThrowInfoFlag("Const", 1);
424 dumpThrowInfoFlag("Volatile", 2);
425 dumpThrowInfoSymbol("CleanupFn", 4);
426 dumpThrowInfoSymbol("ForwardCompat", 8);
427 dumpThrowInfoSymbol("CatchableTypeArray", 12);
428 }
429 for (const std::pair<StringRef, CatchableTypeArray> &CTAPair : CTAs) {
430 StringRef CTAName = CTAPair.first;
431 const CatchableTypeArray &CTA = CTAPair.second;
432
433 outs() << CTAName << "[NumEntries]: " << CTA.NumEntries << '\n';
434
435 unsigned Idx = 0;
436 for (auto I = CTAEntries.lower_bound(std::make_pair(CTAName, 0)),
437 E = CTAEntries.upper_bound(std::make_pair(CTAName, UINT64_MAX));
438 I != E; ++I)
439 outs() << CTAName << '[' << Idx++ << "]: " << I->second << '\n';
440 }
441 for (const std::pair<StringRef, CatchableType> &CTPair : CTs) {
442 StringRef CTName = CTPair.first;
443 const CatchableType &CT = CTPair.second;
444 auto dumpCatchableTypeFlag = [&](const char *Name, uint32_t Flag) {
445 outs() << CTName << "[Flags." << Name
446 << "]: " << (CT.Flags & Flag ? "true" : "false") << '\n';
447 };
448 outs() << CTName << "[Flags]: " << CT.Flags << '\n';
449 dumpCatchableTypeFlag("ScalarType", 1);
450 dumpCatchableTypeFlag("VirtualInheritance", 4);
451 outs() << CTName << "[TypeDescriptor]: " << CT.Symbols[0] << '\n';
452 outs() << CTName << "[NonVirtualBaseAdjustmentOffset]: "
453 << CT.NonVirtualBaseAdjustmentOffset << '\n';
454 outs() << CTName
455 << "[VirtualBasePointerOffset]: " << CT.VirtualBasePointerOffset
456 << '\n';
457 outs() << CTName << "[VirtualBaseAdjustmentOffset]: "
458 << CT.VirtualBaseAdjustmentOffset << '\n';
459 outs() << CTName << "[SizeOrOffset]: " << CT.SizeOrOffset << '\n';
460 outs() << CTName
461 << "[CopyCtor]: " << (CT.Symbols[1].empty() ? "null" : CT.Symbols[1])
462 << '\n';
463 }
David Majnemere2683612014-11-03 07:23:25 +0000464 for (const std::pair<std::pair<StringRef, uint64_t>, StringRef> &VTTPair :
465 VTTEntries) {
466 StringRef VTTName = VTTPair.first.first;
467 uint64_t VTTOffset = VTTPair.first.second;
468 StringRef VTTEntry = VTTPair.second;
469 outs() << VTTName << '[' << VTTOffset << "]: " << VTTEntry << '\n';
470 }
471 for (const std::pair<StringRef, StringRef> &TIPair : TINames) {
472 StringRef TIName = TIPair.first;
473 outs() << TIName << ": " << TIPair.second << '\n';
474 }
475 auto VTableSymI = VTableSymEntries.begin();
476 auto VTableSymE = VTableSymEntries.end();
477 auto VTableDataI = VTableDataEntries.begin();
478 auto VTableDataE = VTableDataEntries.end();
479 for (;;) {
480 bool SymDone = VTableSymI == VTableSymE;
481 bool DataDone = VTableDataI == VTableDataE;
482 if (SymDone && DataDone)
483 break;
484 if (!SymDone && (DataDone || VTableSymI->first < VTableDataI->first)) {
485 StringRef VTableName = VTableSymI->first.first;
486 uint64_t Offset = VTableSymI->first.second;
487 StringRef VTableEntry = VTableSymI->second;
488 outs() << VTableName << '[' << Offset << "]: ";
489 outs() << VTableEntry;
490 outs() << '\n';
491 ++VTableSymI;
492 continue;
493 }
494 if (!DataDone && (SymDone || VTableDataI->first < VTableSymI->first)) {
495 StringRef VTableName = VTableDataI->first.first;
496 uint64_t Offset = VTableDataI->first.second;
497 int64_t VTableEntry = VTableDataI->second;
498 outs() << VTableName << '[' << Offset << "]: ";
499 outs() << VTableEntry;
500 outs() << '\n';
501 ++VTableDataI;
502 continue;
503 }
504 }
David Majnemer72ab1a52014-07-24 23:14:40 +0000505}
506
507static void dumpArchive(const Archive *Arc) {
David Majnemereac48b62014-09-25 22:56:54 +0000508 for (const Archive::Child &ArcC : Arc->children()) {
509 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcC.getAsBinary();
David Majnemer72ab1a52014-07-24 23:14:40 +0000510 if (std::error_code EC = ChildOrErr.getError()) {
511 // Ignore non-object files.
512 if (EC != object_error::invalid_file_type)
513 reportError(Arc->getFileName(), EC.message());
514 continue;
515 }
516
517 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
518 dumpVTables(Obj);
519 else
520 reportError(Arc->getFileName(),
521 vtabledump_error::unrecognized_file_format);
522 }
523}
524
525static void dumpInput(StringRef File) {
526 // If file isn't stdin, check that it exists.
527 if (File != "-" && !sys::fs::exists(File)) {
528 reportError(File, vtabledump_error::file_not_found);
529 return;
530 }
531
532 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000533 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
David Majnemer72ab1a52014-07-24 23:14:40 +0000534 if (std::error_code EC = BinaryOrErr.getError()) {
535 reportError(File, EC);
536 return;
537 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000538 Binary &Binary = *BinaryOrErr.get().getBinary();
David Majnemer72ab1a52014-07-24 23:14:40 +0000539
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000540 if (Archive *Arc = dyn_cast<Archive>(&Binary))
David Majnemer72ab1a52014-07-24 23:14:40 +0000541 dumpArchive(Arc);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000542 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
David Majnemer72ab1a52014-07-24 23:14:40 +0000543 dumpVTables(Obj);
544 else
545 reportError(File, vtabledump_error::unrecognized_file_format);
546}
547
548int main(int argc, const char *argv[]) {
549 sys::PrintStackTraceOnErrorSignal();
550 PrettyStackTraceProgram X(argc, argv);
551 llvm_shutdown_obj Y;
552
553 // Initialize targets.
554 llvm::InitializeAllTargetInfos();
555
556 // Register the target printer for --version.
557 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
558
559 cl::ParseCommandLineOptions(argc, argv, "LLVM VTable Dumper\n");
560
561 // Default to stdin if no filename is specified.
562 if (opts::InputFilenames.size() == 0)
563 opts::InputFilenames.push_back("-");
564
565 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
566 dumpInput);
567
568 return ReturnValue;
569}