llvm-cxxdump: Rename llvm-vtabledump to llvm-cxxdump
[oota-llvm.git] / tools / llvm-cxxdump / llvm-cxxdump.cpp
1 //===- llvm-cxxdump.cpp - Dump C++ data 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 C++ data resident in object files and archives.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm-cxxdump.h"
15 #include "Error.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #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
31 using namespace llvm;
32 using namespace llvm::object;
33 using namespace llvm::support;
34
35 namespace opts {
36 cl::list<std::string> InputFilenames(cl::Positional,
37                                      cl::desc("<input object files>"),
38                                      cl::ZeroOrMore);
39 } // namespace opts
40
41 static int ReturnValue = EXIT_SUCCESS;
42
43 namespace llvm {
44
45 static bool 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
57 static 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
66 static void reportError(StringRef Input, std::error_code EC) {
67   reportError(Input, EC.message());
68 }
69
70 static 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
85 static bool collectRelocatedSymbols(const ObjectFile *Obj,
86                                     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     }
109   }
110   return false;
111 }
112
113 static bool collectRelocationOffsets(
114     const ObjectFile *Obj, const SectionRef &Sec, uint64_t SecAddress,
115     uint64_t SymAddress, uint64_t SymSize, StringRef SymName,
116     std::map<std::pair<StringRef, uint64_t>, StringRef> &Collection) {
117   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     }
133   }
134   return false;
135 }
136
137 static void dumpCXXData(const ObjectFile *Obj) {
138   struct CompleteObjectLocator {
139     StringRef Symbols[2];
140     ArrayRef<little32_t> Data;
141   };
142   struct ClassHierarchyDescriptor {
143     StringRef Symbols[1];
144     ArrayRef<little32_t> Data;
145   };
146   struct BaseClassDescriptor {
147     StringRef Symbols[2];
148     ArrayRef<little32_t> Data;
149   };
150   struct TypeDescriptor {
151     StringRef Symbols[1];
152     uint64_t AlwaysZero;
153     StringRef MangledName;
154   };
155   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 Size;
167     StringRef Symbols[2];
168   };
169   std::map<std::pair<StringRef, uint64_t>, StringRef> VFTableEntries;
170   std::map<std::pair<StringRef, uint64_t>, StringRef> TIEntries;
171   std::map<std::pair<StringRef, uint64_t>, StringRef> CTAEntries;
172   std::map<StringRef, ArrayRef<little32_t>> VBTables;
173   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;
178   std::map<StringRef, ThrowInfo> TIs;
179   std::map<StringRef, CatchableTypeArray> CTAs;
180   std::map<StringRef, CatchableType> CTs;
181
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
189   for (const object::SymbolRef &Sym : Obj->symbols()) {
190     StringRef SymName;
191     if (error(Sym.getName(SymName)))
192       return;
193     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;
199     const SectionRef &Sec = *SecI;
200     // Skip virtual or BSS sections.
201     if (Sec.isBSS() || Sec.isVirtual())
202       continue;
203     StringRef SecContents;
204     if (error(Sec.getContents(SecContents)))
205       return;
206     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
214     // 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")) {
218       // 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.
220       collectRelocationOffsets(Obj, Sec, SecAddress, SecAddress, SecSize,
221                                SymName, VFTableEntries);
222     }
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")) {
226       ArrayRef<little32_t> VBTableData(
227           reinterpret_cast<const little32_t *>(SymContents.data()),
228           SymContents.size() / sizeof(little32_t));
229       VBTables[SymName] = VBTableData;
230     }
231     // Complete object locators in the MS-ABI start with '??_R4'
232     else if (SymName.startswith("??_R4")) {
233       CompleteObjectLocator COL;
234       COL.Data = ArrayRef<little32_t>(
235           reinterpret_cast<const little32_t *>(SymContents.data()), 3);
236       StringRef *I = std::begin(COL.Symbols), *E = std::end(COL.Symbols);
237       if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
238                                   E))
239         return;
240       COLs[SymName] = COL;
241     }
242     // Class hierarchy descriptors in the MS-ABI start with '??_R3'
243     else if (SymName.startswith("??_R3")) {
244       ClassHierarchyDescriptor CHD;
245       CHD.Data = ArrayRef<little32_t>(
246           reinterpret_cast<const little32_t *>(SymContents.data()), 3);
247       StringRef *I = std::begin(CHD.Symbols), *E = std::end(CHD.Symbols);
248       if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
249                                   E))
250         return;
251       CHDs[SymName] = CHD;
252     }
253     // Class hierarchy descriptors in the MS-ABI start with '??_R2'
254     else if (SymName.startswith("??_R2")) {
255       // Each relocation names a base class descriptor.  We note the offset into
256       // the section and the symbol used for the relocation.
257       collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
258                                SymName, BCAEntries);
259     }
260     // Base class descriptors in the MS-ABI start with '??_R1'
261     else if (SymName.startswith("??_R1")) {
262       BaseClassDescriptor BCD;
263       BCD.Data = ArrayRef<little32_t>(
264           reinterpret_cast<const little32_t *>(SymContents.data()) + 1, 5);
265       StringRef *I = std::begin(BCD.Symbols), *E = std::end(BCD.Symbols);
266       if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
267                                   E))
268         return;
269       BCDs[SymName] = BCD;
270     }
271     // Type descriptors in the MS-ABI start with '??_R0'
272     else if (SymName.startswith("??_R0")) {
273       const char *DataPtr = SymContents.drop_front(BytesInAddress).data();
274       TypeDescriptor TD;
275       if (BytesInAddress == 8)
276         TD.AlwaysZero = *reinterpret_cast<const little64_t *>(DataPtr);
277       else
278         TD.AlwaysZero = *reinterpret_cast<const little32_t *>(DataPtr);
279       TD.MangledName = SymContents.drop_front(BytesInAddress * 2);
280       StringRef *I = std::begin(TD.Symbols), *E = std::end(TD.Symbols);
281       if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
282                                   E))
283         return;
284       TDs[SymName] = TD;
285     }
286     // 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.Size = 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     }
319     // 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 =
337             SymContents.substr(SymOffI, BytesInAddress).data();
338         int64_t VData;
339         if (BytesInAddress == 8)
340           VData = *reinterpret_cast<const little64_t *>(DataPtr);
341         else
342           VData = *reinterpret_cast<const little32_t *>(DataPtr);
343         VTableDataEntries[Key] = VData;
344       }
345     }
346     // Typeinfo structures in the Itanium ABI start with '_ZTI' or '__ZTI'.
347     else if (SymName.startswith("_ZTI") || SymName.startswith("__ZTI")) {
348       // FIXME: Do something with these!
349     }
350   }
351   for (const auto &VFTableEntry : VFTableEntries) {
352     StringRef VFTableName = VFTableEntry.first.first;
353     uint64_t Offset = VFTableEntry.first.second;
354     StringRef SymName = VFTableEntry.second;
355     outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n';
356   }
357   for (const std::pair<StringRef, ArrayRef<little32_t>> &VBTable : VBTables) {
358     StringRef VBTableName = VBTable.first;
359     uint32_t Idx = 0;
360     for (little32_t Offset : VBTable.second) {
361       outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n';
362       Idx += sizeof(Offset);
363     }
364   }
365   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]
373            << '\n';
374   }
375   for (const std::pair<StringRef, ClassHierarchyDescriptor> &CHDPair : CHDs) {
376     StringRef CHDName = CHDPair.first;
377     const ClassHierarchyDescriptor &CHD = CHDPair.second;
378     outs() << CHDName << "[AlwaysZero]: " << CHD.Data[0] << '\n';
379     outs() << CHDName << "[Flags]: " << CHD.Data[1] << '\n';
380     outs() << CHDName << "[NumClasses]: " << CHD.Data[2] << '\n';
381     outs() << CHDName << "[BaseClassArray]: " << CHD.Symbols[0] << '\n';
382   }
383   for (const std::pair<std::pair<StringRef, uint64_t>, StringRef> &BCAEntry :
384        BCAEntries) {
385     StringRef BCAName = BCAEntry.first.first;
386     uint64_t Offset = BCAEntry.first.second;
387     StringRef SymName = BCAEntry.second;
388     outs() << BCAName << '[' << Offset << "]: " << SymName << '\n';
389   }
390   for (const std::pair<StringRef, BaseClassDescriptor> &BCDPair : BCDs) {
391     StringRef BCDName = BCDPair.first;
392     const BaseClassDescriptor &BCD = BCDPair.second;
393     outs() << BCDName << "[TypeDescriptor]: " << BCD.Symbols[0] << '\n';
394     outs() << BCDName << "[NumBases]: " << BCD.Data[0] << '\n';
395     outs() << BCDName << "[OffsetInVBase]: " << BCD.Data[1] << '\n';
396     outs() << BCDName << "[VBPtrOffset]: " << BCD.Data[2] << '\n';
397     outs() << BCDName << "[OffsetInVBTable]: " << BCD.Data[3] << '\n';
398     outs() << BCDName << "[Flags]: " << BCD.Data[4] << '\n';
399     outs() << BCDName << "[ClassHierarchyDescriptor]: " << BCD.Symbols[1]
400            << '\n';
401   }
402   for (const std::pair<StringRef, TypeDescriptor> &TDPair : TDs) {
403     StringRef TDName = TDPair.first;
404     const TypeDescriptor &TD = TDPair.second;
405     outs() << TDName << "[VFPtr]: " << TD.Symbols[0] << '\n';
406     outs() << TDName << "[AlwaysZero]: " << TD.AlwaysZero << '\n';
407     outs() << TDName << "[MangledName]: ";
408     outs().write_escaped(TD.MangledName.rtrim(StringRef("\0", 1)),
409                          /*UseHexEscapes=*/true)
410         << '\n';
411   }
412   for (const std::pair<StringRef, ThrowInfo> &TIPair : TIs) {
413     StringRef TIName = TIPair.first;
414     const ThrowInfo &TI = TIPair.second;
415     auto dumpThrowInfoFlag = [&](const char *Name, uint32_t Flag) {
416       outs() << TIName << "[Flags." << Name
417              << "]: " << (TI.Flags & Flag ? "true" : "false") << '\n';
418     };
419     auto dumpThrowInfoSymbol = [&](const char *Name, int Offset) {
420       outs() << TIName << '[' << Name << "]: ";
421       auto Entry = TIEntries.find(std::make_pair(TIName, Offset));
422       outs() << (Entry == TIEntries.end() ? "null" : Entry->second) << '\n';
423     };
424     outs() << TIName << "[Flags]: " << TI.Flags << '\n';
425     dumpThrowInfoFlag("Const", 1);
426     dumpThrowInfoFlag("Volatile", 2);
427     dumpThrowInfoSymbol("CleanupFn", 4);
428     dumpThrowInfoSymbol("ForwardCompat", 8);
429     dumpThrowInfoSymbol("CatchableTypeArray", 12);
430   }
431   for (const std::pair<StringRef, CatchableTypeArray> &CTAPair : CTAs) {
432     StringRef CTAName = CTAPair.first;
433     const CatchableTypeArray &CTA = CTAPair.second;
434
435     outs() << CTAName << "[NumEntries]: " << CTA.NumEntries << '\n';
436
437     unsigned Idx = 0;
438     for (auto I = CTAEntries.lower_bound(std::make_pair(CTAName, 0)),
439               E = CTAEntries.upper_bound(std::make_pair(CTAName, UINT64_MAX));
440          I != E; ++I)
441       outs() << CTAName << '[' << Idx++ << "]: " << I->second << '\n';
442   }
443   for (const std::pair<StringRef, CatchableType> &CTPair : CTs) {
444     StringRef CTName = CTPair.first;
445     const CatchableType &CT = CTPair.second;
446     auto dumpCatchableTypeFlag = [&](const char *Name, uint32_t Flag) {
447       outs() << CTName << "[Flags." << Name
448              << "]: " << (CT.Flags & Flag ? "true" : "false") << '\n';
449     };
450     outs() << CTName << "[Flags]: " << CT.Flags << '\n';
451     dumpCatchableTypeFlag("ScalarType", 1);
452     dumpCatchableTypeFlag("VirtualInheritance", 4);
453     outs() << CTName << "[TypeDescriptor]: " << CT.Symbols[0] << '\n';
454     outs() << CTName << "[NonVirtualBaseAdjustmentOffset]: "
455            << CT.NonVirtualBaseAdjustmentOffset << '\n';
456     outs() << CTName
457            << "[VirtualBasePointerOffset]: " << CT.VirtualBasePointerOffset
458            << '\n';
459     outs() << CTName << "[VirtualBaseAdjustmentOffset]: "
460            << CT.VirtualBaseAdjustmentOffset << '\n';
461     outs() << CTName << "[Size]: " << CT.Size << '\n';
462     outs() << CTName
463            << "[CopyCtor]: " << (CT.Symbols[1].empty() ? "null" : CT.Symbols[1])
464            << '\n';
465   }
466   for (const std::pair<std::pair<StringRef, uint64_t>, StringRef> &VTTPair :
467        VTTEntries) {
468     StringRef VTTName = VTTPair.first.first;
469     uint64_t VTTOffset = VTTPair.first.second;
470     StringRef VTTEntry = VTTPair.second;
471     outs() << VTTName << '[' << VTTOffset << "]: " << VTTEntry << '\n';
472   }
473   for (const std::pair<StringRef, StringRef> &TIPair : TINames) {
474     StringRef TIName = TIPair.first;
475     outs() << TIName << ": " << TIPair.second << '\n';
476   }
477   auto VTableSymI = VTableSymEntries.begin();
478   auto VTableSymE = VTableSymEntries.end();
479   auto VTableDataI = VTableDataEntries.begin();
480   auto VTableDataE = VTableDataEntries.end();
481   for (;;) {
482     bool SymDone = VTableSymI == VTableSymE;
483     bool DataDone = VTableDataI == VTableDataE;
484     if (SymDone && DataDone)
485       break;
486     if (!SymDone && (DataDone || VTableSymI->first < VTableDataI->first)) {
487       StringRef VTableName = VTableSymI->first.first;
488       uint64_t Offset = VTableSymI->first.second;
489       StringRef VTableEntry = VTableSymI->second;
490       outs() << VTableName << '[' << Offset << "]: ";
491       outs() << VTableEntry;
492       outs() << '\n';
493       ++VTableSymI;
494       continue;
495     }
496     if (!DataDone && (SymDone || VTableDataI->first < VTableSymI->first)) {
497       StringRef VTableName = VTableDataI->first.first;
498       uint64_t Offset = VTableDataI->first.second;
499       int64_t VTableEntry = VTableDataI->second;
500       outs() << VTableName << '[' << Offset << "]: ";
501       outs() << VTableEntry;
502       outs() << '\n';
503       ++VTableDataI;
504       continue;
505     }
506   }
507 }
508
509 static void dumpArchive(const Archive *Arc) {
510   for (const Archive::Child &ArcC : Arc->children()) {
511     ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcC.getAsBinary();
512     if (std::error_code EC = ChildOrErr.getError()) {
513       // Ignore non-object files.
514       if (EC != object_error::invalid_file_type)
515         reportError(Arc->getFileName(), EC.message());
516       continue;
517     }
518
519     if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
520       dumpCXXData(Obj);
521     else
522       reportError(Arc->getFileName(), cxxdump_error::unrecognized_file_format);
523   }
524 }
525
526 static void dumpInput(StringRef File) {
527   // If file isn't stdin, check that it exists.
528   if (File != "-" && !sys::fs::exists(File)) {
529     reportError(File, cxxdump_error::file_not_found);
530     return;
531   }
532
533   // Attempt to open the binary.
534   ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
535   if (std::error_code EC = BinaryOrErr.getError()) {
536     reportError(File, EC);
537     return;
538   }
539   Binary &Binary = *BinaryOrErr.get().getBinary();
540
541   if (Archive *Arc = dyn_cast<Archive>(&Binary))
542     dumpArchive(Arc);
543   else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
544     dumpCXXData(Obj);
545   else
546     reportError(File, cxxdump_error::unrecognized_file_format);
547 }
548
549 int main(int argc, const char *argv[]) {
550   sys::PrintStackTraceOnErrorSignal();
551   PrettyStackTraceProgram X(argc, argv);
552   llvm_shutdown_obj Y;
553
554   // Initialize targets.
555   llvm::InitializeAllTargetInfos();
556
557   // Register the target printer for --version.
558   cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
559
560   cl::ParseCommandLineOptions(argc, argv, "LLVM C++ ABI Data Dumper\n");
561
562   // Default to stdin if no filename is specified.
563   if (opts::InputFilenames.size() == 0)
564     opts::InputFilenames.push_back("-");
565
566   std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
567                 dumpInput);
568
569   return ReturnValue;
570 }