blob: a21acae02c3a5f035a11ff3bff91d69519cdebf7 [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//
10// Dumps VTables resident in object files and archives. Note, it currently only
11// supports MS-ABI style object files.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm-vtabledump.h"
16#include "Error.h"
17#include "llvm/ADT/ArrayRef.h"
David Majnemer72ab1a52014-07-24 23:14:40 +000018#include "llvm/Object/Archive.h"
19#include "llvm/Object/ObjectFile.h"
20#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"
28#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
46bool error(std::error_code EC) {
47 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 Majnemer72ab1a52014-07-24 23:14:40 +0000138static void dumpVTables(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 Majnemer72ab1a52014-07-24 23:14:40 +0000156 std::map<std::pair<StringRef, uint64_t>, StringRef> VFTableEntries;
David Majnemer6887a252014-09-26 08:01:23 +0000157 std::map<StringRef, ArrayRef<little32_t>> VBTables;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000158 std::map<StringRef, CompleteObjectLocator> COLs;
159 std::map<StringRef, ClassHierarchyDescriptor> CHDs;
160 std::map<std::pair<StringRef, uint64_t>, StringRef> BCAEntries;
161 std::map<StringRef, BaseClassDescriptor> BCDs;
162 std::map<StringRef, TypeDescriptor> TDs;
David Majnemere2683612014-11-03 07:23:25 +0000163
164 std::map<std::pair<StringRef, uint64_t>, StringRef> VTableSymEntries;
165 std::map<std::pair<StringRef, uint64_t>, int64_t> VTableDataEntries;
166 std::map<std::pair<StringRef, uint64_t>, StringRef> VTTEntries;
167 std::map<StringRef, StringRef> TINames;
168
169 uint8_t BytesInAddress = Obj->getBytesInAddress();
170
David Majnemer72ab1a52014-07-24 23:14:40 +0000171 for (const object::SymbolRef &Sym : Obj->symbols()) {
172 StringRef SymName;
173 if (error(Sym.getName(SymName)))
174 return;
David Majnemer601327c2014-09-26 22:32:19 +0000175 object::section_iterator SecI(Obj->section_begin());
176 if (error(Sym.getSection(SecI)))
177 return;
178 // Skip external symbols.
179 if (SecI == Obj->section_end())
180 continue;
David Majnemere2683612014-11-03 07:23:25 +0000181 const SectionRef &Sec = *SecI;
David Majnemer601327c2014-09-26 22:32:19 +0000182 // Skip virtual or BSS sections.
David Majnemere2683612014-11-03 07:23:25 +0000183 if (Sec.isBSS() || Sec.isVirtual())
David Majnemer601327c2014-09-26 22:32:19 +0000184 continue;
185 StringRef SecContents;
David Majnemere2683612014-11-03 07:23:25 +0000186 if (error(Sec.getContents(SecContents)))
David Majnemer601327c2014-09-26 22:32:19 +0000187 return;
David Majnemere2683612014-11-03 07:23:25 +0000188 uint64_t SymAddress, SymSize;
189 if (error(Sym.getAddress(SymAddress)) || error(Sym.getSize(SymSize)))
190 return;
191 uint64_t SecAddress = Sec.getAddress();
192 uint64_t SecSize = Sec.getSize();
193 uint64_t SymOffset = SymAddress - SecAddress;
194 StringRef SymContents = SecContents.substr(SymOffset, SymSize);
195
David Majnemer72ab1a52014-07-24 23:14:40 +0000196 // VFTables in the MS-ABI start with '??_7' and are contained within their
197 // own COMDAT section. We then determine the contents of the VFTable by
198 // looking at each relocation in the section.
199 if (SymName.startswith("??_7")) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000200 // Each relocation either names a virtual method or a thunk. We note the
201 // offset into the section and the symbol used for the relocation.
David Majnemere2683612014-11-03 07:23:25 +0000202 collectRelocationOffsets(Obj, Sec, SecAddress, SecAddress, SecSize,
203 SymName, VFTableEntries);
David Majnemer72ab1a52014-07-24 23:14:40 +0000204 }
205 // VBTables in the MS-ABI start with '??_8' and are filled with 32-bit
206 // offsets of virtual bases.
207 else if (SymName.startswith("??_8")) {
David Majnemer6887a252014-09-26 08:01:23 +0000208 ArrayRef<little32_t> VBTableData(
David Majnemere2683612014-11-03 07:23:25 +0000209 reinterpret_cast<const little32_t *>(SymContents.data()),
210 SymContents.size() / sizeof(little32_t));
David Majnemer72ab1a52014-07-24 23:14:40 +0000211 VBTables[SymName] = VBTableData;
212 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000213 // Complete object locators in the MS-ABI start with '??_R4'
214 else if (SymName.startswith("??_R4")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000215 CompleteObjectLocator COL;
David Majnemer6887a252014-09-26 08:01:23 +0000216 COL.Data = ArrayRef<little32_t>(
David Majnemere2683612014-11-03 07:23:25 +0000217 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000218 StringRef *I = std::begin(COL.Symbols), *E = std::end(COL.Symbols);
David Majnemere2683612014-11-03 07:23:25 +0000219 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
220 E))
David Majnemer1ac52eb2014-09-26 04:21:51 +0000221 return;
222 COLs[SymName] = COL;
223 }
224 // Class hierarchy descriptors in the MS-ABI start with '??_R3'
225 else if (SymName.startswith("??_R3")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000226 ClassHierarchyDescriptor CHD;
David Majnemer6887a252014-09-26 08:01:23 +0000227 CHD.Data = ArrayRef<little32_t>(
David Majnemere2683612014-11-03 07:23:25 +0000228 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000229 StringRef *I = std::begin(CHD.Symbols), *E = std::end(CHD.Symbols);
David Majnemere2683612014-11-03 07:23:25 +0000230 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
231 E))
David Majnemer1ac52eb2014-09-26 04:21:51 +0000232 return;
233 CHDs[SymName] = CHD;
234 }
235 // Class hierarchy descriptors in the MS-ABI start with '??_R2'
236 else if (SymName.startswith("??_R2")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000237 // Each relocation names a base class descriptor. We note the offset into
238 // the section and the symbol used for the relocation.
David Majnemere2683612014-11-03 07:23:25 +0000239 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
240 SymName, BCAEntries);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000241 }
242 // Base class descriptors in the MS-ABI start with '??_R1'
243 else if (SymName.startswith("??_R1")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000244 BaseClassDescriptor BCD;
David Majnemer6887a252014-09-26 08:01:23 +0000245 BCD.Data = ArrayRef<little32_t>(
David Majnemere2683612014-11-03 07:23:25 +0000246 reinterpret_cast<const little32_t *>(SymContents.data()) + 1, 5);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000247 StringRef *I = std::begin(BCD.Symbols), *E = std::end(BCD.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 BCDs[SymName] = BCD;
252 }
253 // Type descriptors in the MS-ABI start with '??_R0'
254 else if (SymName.startswith("??_R0")) {
David Majnemere2683612014-11-03 07:23:25 +0000255 const char *DataPtr = SymContents.drop_front(BytesInAddress).data();
David Majnemer1ac52eb2014-09-26 04:21:51 +0000256 TypeDescriptor TD;
David Majnemer6887a252014-09-26 08:01:23 +0000257 if (BytesInAddress == 8)
258 TD.AlwaysZero = *reinterpret_cast<const little64_t *>(DataPtr);
259 else
260 TD.AlwaysZero = *reinterpret_cast<const little32_t *>(DataPtr);
David Majnemere2683612014-11-03 07:23:25 +0000261 TD.MangledName = SymContents.drop_front(BytesInAddress * 2);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000262 StringRef *I = std::begin(TD.Symbols), *E = std::end(TD.Symbols);
David Majnemere2683612014-11-03 07:23:25 +0000263 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
264 E))
David Majnemer1ac52eb2014-09-26 04:21:51 +0000265 return;
266 TDs[SymName] = TD;
267 }
David Majnemere2683612014-11-03 07:23:25 +0000268 // Construction vtables in the Itanium ABI start with '_ZTT' or '__ZTT'.
269 else if (SymName.startswith("_ZTT") || SymName.startswith("__ZTT")) {
270 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
271 SymName, VTTEntries);
272 }
273 // Typeinfo names in the Itanium ABI start with '_ZTS' or '__ZTS'.
274 else if (SymName.startswith("_ZTS") || SymName.startswith("__ZTS")) {
275 TINames[SymName] = SymContents.slice(0, SymContents.find('\0'));
276 }
277 // Vtables in the Itanium ABI start with '_ZTV' or '__ZTV'.
278 else if (SymName.startswith("_ZTV") || SymName.startswith("__ZTV")) {
279 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
280 SymName, VTableSymEntries);
281 for (uint64_t SymOffI = 0; SymOffI < SymSize; SymOffI += BytesInAddress) {
282 auto Key = std::make_pair(SymName, SymOffI);
283 if (VTableSymEntries.count(Key))
284 continue;
285 const char *DataPtr = SymContents.substr(SymOffI, BytesInAddress).data();
286 int64_t VData;
287 if (BytesInAddress == 8)
288 VData = *reinterpret_cast<const little64_t *>(DataPtr);
289 else
290 VData = *reinterpret_cast<const little32_t *>(DataPtr);
291 VTableDataEntries[Key] = VData;
292 }
293 }
294 // Typeinfo structures in the Itanium ABI start with '_ZTI' or '__ZTI'.
295 else if (SymName.startswith("_ZTI") || SymName.startswith("__ZTI")) {
296 // FIXME: Do something with these!
297 }
David Majnemer72ab1a52014-07-24 23:14:40 +0000298 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000299 for (const std::pair<std::pair<StringRef, uint64_t>, StringRef> &VFTableEntry :
300 VFTableEntries) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000301 StringRef VFTableName = VFTableEntry.first.first;
302 uint64_t Offset = VFTableEntry.first.second;
303 StringRef SymName = VFTableEntry.second;
304 outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n';
305 }
David Majnemere2683612014-11-03 07:23:25 +0000306 for (const std::pair<StringRef, ArrayRef<little32_t>> &VBTable : VBTables) {
David Majnemerbf32f772014-07-25 04:30:11 +0000307 StringRef VBTableName = VBTable.first;
David Majnemer72ab1a52014-07-24 23:14:40 +0000308 uint32_t Idx = 0;
David Majnemer6887a252014-09-26 08:01:23 +0000309 for (little32_t Offset : VBTable.second) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000310 outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n';
David Majnemerbf32f772014-07-25 04:30:11 +0000311 Idx += sizeof(Offset);
David Majnemer72ab1a52014-07-24 23:14:40 +0000312 }
313 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000314 for (const std::pair<StringRef, CompleteObjectLocator> &COLPair : COLs) {
315 StringRef COLName = COLPair.first;
316 const CompleteObjectLocator &COL = COLPair.second;
317 outs() << COLName << "[IsImageRelative]: " << COL.Data[0] << '\n';
318 outs() << COLName << "[OffsetToTop]: " << COL.Data[1] << '\n';
319 outs() << COLName << "[VFPtrOffset]: " << COL.Data[2] << '\n';
320 outs() << COLName << "[TypeDescriptor]: " << COL.Symbols[0] << '\n';
321 outs() << COLName << "[ClassHierarchyDescriptor]: " << COL.Symbols[1] << '\n';
322 }
323 for (const std::pair<StringRef, ClassHierarchyDescriptor> &CHDPair : CHDs) {
324 StringRef CHDName = CHDPair.first;
325 const ClassHierarchyDescriptor &CHD = CHDPair.second;
326 outs() << CHDName << "[AlwaysZero]: " << CHD.Data[0] << '\n';
327 outs() << CHDName << "[Flags]: " << CHD.Data[1] << '\n';
328 outs() << CHDName << "[NumClasses]: " << CHD.Data[2] << '\n';
329 outs() << CHDName << "[BaseClassArray]: " << CHD.Symbols[0] << '\n';
330 }
331 for (const std::pair<std::pair<StringRef, uint64_t>, StringRef> &BCAEntry :
332 BCAEntries) {
333 StringRef BCAName = BCAEntry.first.first;
334 uint64_t Offset = BCAEntry.first.second;
335 StringRef SymName = BCAEntry.second;
336 outs() << BCAName << '[' << Offset << "]: " << SymName << '\n';
337 }
338 for (const std::pair<StringRef, BaseClassDescriptor> &BCDPair : BCDs) {
339 StringRef BCDName = BCDPair.first;
340 const BaseClassDescriptor &BCD = BCDPair.second;
341 outs() << BCDName << "[TypeDescriptor]: " << BCD.Symbols[0] << '\n';
342 outs() << BCDName << "[NumBases]: " << BCD.Data[0] << '\n';
343 outs() << BCDName << "[OffsetInVBase]: " << BCD.Data[1] << '\n';
344 outs() << BCDName << "[VBPtrOffset]: " << BCD.Data[2] << '\n';
345 outs() << BCDName << "[OffsetInVBTable]: " << BCD.Data[3] << '\n';
346 outs() << BCDName << "[Flags]: " << BCD.Data[4] << '\n';
347 outs() << BCDName << "[ClassHierarchyDescriptor]: " << BCD.Symbols[1] << '\n';
348 }
349 for (const std::pair<StringRef, TypeDescriptor> &TDPair : TDs) {
350 StringRef TDName = TDPair.first;
351 const TypeDescriptor &TD = TDPair.second;
352 outs() << TDName << "[VFPtr]: " << TD.Symbols[0] << '\n';
David Majnemer6887a252014-09-26 08:01:23 +0000353 outs() << TDName << "[AlwaysZero]: " << TD.AlwaysZero << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000354 outs() << TDName << "[MangledName]: ";
David Majnemer56167c32014-09-26 05:50:45 +0000355 outs().write_escaped(TD.MangledName.rtrim(StringRef("\0", 1)),
356 /*UseHexEscapes=*/true)
357 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000358 }
David Majnemere2683612014-11-03 07:23:25 +0000359 for (const std::pair<std::pair<StringRef, uint64_t>, StringRef> &VTTPair :
360 VTTEntries) {
361 StringRef VTTName = VTTPair.first.first;
362 uint64_t VTTOffset = VTTPair.first.second;
363 StringRef VTTEntry = VTTPair.second;
364 outs() << VTTName << '[' << VTTOffset << "]: " << VTTEntry << '\n';
365 }
366 for (const std::pair<StringRef, StringRef> &TIPair : TINames) {
367 StringRef TIName = TIPair.first;
368 outs() << TIName << ": " << TIPair.second << '\n';
369 }
370 auto VTableSymI = VTableSymEntries.begin();
371 auto VTableSymE = VTableSymEntries.end();
372 auto VTableDataI = VTableDataEntries.begin();
373 auto VTableDataE = VTableDataEntries.end();
374 for (;;) {
375 bool SymDone = VTableSymI == VTableSymE;
376 bool DataDone = VTableDataI == VTableDataE;
377 if (SymDone && DataDone)
378 break;
379 if (!SymDone && (DataDone || VTableSymI->first < VTableDataI->first)) {
380 StringRef VTableName = VTableSymI->first.first;
381 uint64_t Offset = VTableSymI->first.second;
382 StringRef VTableEntry = VTableSymI->second;
383 outs() << VTableName << '[' << Offset << "]: ";
384 outs() << VTableEntry;
385 outs() << '\n';
386 ++VTableSymI;
387 continue;
388 }
389 if (!DataDone && (SymDone || VTableDataI->first < VTableSymI->first)) {
390 StringRef VTableName = VTableDataI->first.first;
391 uint64_t Offset = VTableDataI->first.second;
392 int64_t VTableEntry = VTableDataI->second;
393 outs() << VTableName << '[' << Offset << "]: ";
394 outs() << VTableEntry;
395 outs() << '\n';
396 ++VTableDataI;
397 continue;
398 }
399 }
David Majnemer72ab1a52014-07-24 23:14:40 +0000400}
401
402static void dumpArchive(const Archive *Arc) {
David Majnemereac48b62014-09-25 22:56:54 +0000403 for (const Archive::Child &ArcC : Arc->children()) {
404 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcC.getAsBinary();
David Majnemer72ab1a52014-07-24 23:14:40 +0000405 if (std::error_code EC = ChildOrErr.getError()) {
406 // Ignore non-object files.
407 if (EC != object_error::invalid_file_type)
408 reportError(Arc->getFileName(), EC.message());
409 continue;
410 }
411
412 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
413 dumpVTables(Obj);
414 else
415 reportError(Arc->getFileName(),
416 vtabledump_error::unrecognized_file_format);
417 }
418}
419
420static void dumpInput(StringRef File) {
421 // If file isn't stdin, check that it exists.
422 if (File != "-" && !sys::fs::exists(File)) {
423 reportError(File, vtabledump_error::file_not_found);
424 return;
425 }
426
427 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000428 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
David Majnemer72ab1a52014-07-24 23:14:40 +0000429 if (std::error_code EC = BinaryOrErr.getError()) {
430 reportError(File, EC);
431 return;
432 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000433 Binary &Binary = *BinaryOrErr.get().getBinary();
David Majnemer72ab1a52014-07-24 23:14:40 +0000434
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000435 if (Archive *Arc = dyn_cast<Archive>(&Binary))
David Majnemer72ab1a52014-07-24 23:14:40 +0000436 dumpArchive(Arc);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000437 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
David Majnemer72ab1a52014-07-24 23:14:40 +0000438 dumpVTables(Obj);
439 else
440 reportError(File, vtabledump_error::unrecognized_file_format);
441}
442
443int main(int argc, const char *argv[]) {
444 sys::PrintStackTraceOnErrorSignal();
445 PrettyStackTraceProgram X(argc, argv);
446 llvm_shutdown_obj Y;
447
448 // Initialize targets.
449 llvm::InitializeAllTargetInfos();
450
451 // Register the target printer for --version.
452 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
453
454 cl::ParseCommandLineOptions(argc, argv, "LLVM VTable Dumper\n");
455
456 // Default to stdin if no filename is specified.
457 if (opts::InputFilenames.size() == 0)
458 opts::InputFilenames.push_back("-");
459
460 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
461 dumpInput);
462
463 return ReturnValue;
464}