xref: /llvm-project/llvm/tools/llvm-cxxdump/llvm-cxxdump.cpp (revision 5d0c2ffadf84839b951f12a23a163acbd8162d05)
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/Object/SymbolSize.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 "llvm/Support/raw_ostream.h"
29 #include <map>
30 #include <string>
31 #include <system_error>
32 
33 using namespace llvm;
34 using namespace llvm::object;
35 using namespace llvm::support;
36 
37 namespace opts {
38 cl::list<std::string> InputFilenames(cl::Positional,
39                                      cl::desc("<input object files>"),
40                                      cl::ZeroOrMore);
41 } // namespace opts
42 
43 static int ReturnValue = EXIT_SUCCESS;
44 
45 namespace llvm {
46 
47 static bool error(std::error_code EC) {
48   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 
59 static 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 
68 static void reportError(StringRef Input, std::error_code EC) {
69   reportError(Input, EC.message());
70 }
71 
72 static 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 
87 static bool collectRelocatedSymbols(const ObjectFile *Obj,
88                                     const SectionRef &Sec, uint64_t SecAddress,
89                                     uint64_t SymAddress, uint64_t SymSize,
90                                     StringRef *I, StringRef *E) {
91   uint64_t SymOffset = SymAddress - SecAddress;
92   uint64_t SymEnd = SymOffset + SymSize;
93   for (const SectionRef &SR : getRelocSections(Obj, Sec)) {
94     for (const object::RelocationRef &Reloc : SR.relocations()) {
95       if (I == E)
96         break;
97       const object::symbol_iterator RelocSymI = Reloc.getSymbol();
98       if (RelocSymI == Obj->symbol_end())
99         continue;
100       ErrorOr<StringRef> RelocSymName = RelocSymI->getName();
101       if (error(RelocSymName.getError()))
102         return true;
103       uint64_t Offset = Reloc.getOffset();
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       ErrorOr<StringRef> RelocSymName = RelocSymI->getName();
125       if (error(RelocSymName.getError()))
126         return true;
127       uint64_t Offset = Reloc.getOffset();
128       if (Offset >= SymOffset && Offset < SymEnd)
129         Collection[std::make_pair(SymName, Offset - SymOffset)] = *RelocSymName;
130     }
131   }
132   return false;
133 }
134 
135 static void dumpCXXData(const ObjectFile *Obj) {
136   struct CompleteObjectLocator {
137     StringRef Symbols[2];
138     ArrayRef<little32_t> Data;
139   };
140   struct ClassHierarchyDescriptor {
141     StringRef Symbols[1];
142     ArrayRef<little32_t> Data;
143   };
144   struct BaseClassDescriptor {
145     StringRef Symbols[2];
146     ArrayRef<little32_t> Data;
147   };
148   struct TypeDescriptor {
149     StringRef Symbols[1];
150     uint64_t AlwaysZero;
151     StringRef MangledName;
152   };
153   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;
164     uint32_t Size;
165     StringRef Symbols[2];
166   };
167   std::map<std::pair<StringRef, uint64_t>, StringRef> VFTableEntries;
168   std::map<std::pair<StringRef, uint64_t>, StringRef> TIEntries;
169   std::map<std::pair<StringRef, uint64_t>, StringRef> CTAEntries;
170   std::map<StringRef, ArrayRef<little32_t>> VBTables;
171   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;
176   std::map<StringRef, ThrowInfo> TIs;
177   std::map<StringRef, CatchableTypeArray> CTAs;
178   std::map<StringRef, CatchableType> CTs;
179 
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 
187   std::vector<std::pair<SymbolRef, uint64_t>> SymAddr =
188       object::computeSymbolSizes(*Obj);
189 
190   for (auto &P : SymAddr) {
191     object::SymbolRef Sym = P.first;
192     uint64_t SymSize = P.second;
193     ErrorOr<StringRef> SymNameOrErr = Sym.getName();
194     if (error(SymNameOrErr.getError()))
195       return;
196     StringRef SymName = *SymNameOrErr;
197     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;
203     const SectionRef &Sec = *SecI;
204     // Skip virtual or BSS sections.
205     if (Sec.isBSS() || Sec.isVirtual())
206       continue;
207     StringRef SecContents;
208     if (error(Sec.getContents(SecContents)))
209       return;
210     uint64_t SymAddress;
211     if (error(Sym.getAddress(SymAddress)))
212       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 
218     // 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")) {
222       // 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.
224       collectRelocationOffsets(Obj, Sec, SecAddress, SecAddress, SecSize,
225                                SymName, VFTableEntries);
226     }
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")) {
230       ArrayRef<little32_t> VBTableData(
231           reinterpret_cast<const little32_t *>(SymContents.data()),
232           SymContents.size() / sizeof(little32_t));
233       VBTables[SymName] = VBTableData;
234     }
235     // Complete object locators in the MS-ABI start with '??_R4'
236     else if (SymName.startswith("??_R4")) {
237       CompleteObjectLocator COL;
238       COL.Data = ArrayRef<little32_t>(
239           reinterpret_cast<const little32_t *>(SymContents.data()), 3);
240       StringRef *I = std::begin(COL.Symbols), *E = std::end(COL.Symbols);
241       if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
242                                   E))
243         return;
244       COLs[SymName] = COL;
245     }
246     // Class hierarchy descriptors in the MS-ABI start with '??_R3'
247     else if (SymName.startswith("??_R3")) {
248       ClassHierarchyDescriptor CHD;
249       CHD.Data = ArrayRef<little32_t>(
250           reinterpret_cast<const little32_t *>(SymContents.data()), 3);
251       StringRef *I = std::begin(CHD.Symbols), *E = std::end(CHD.Symbols);
252       if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
253                                   E))
254         return;
255       CHDs[SymName] = CHD;
256     }
257     // Class hierarchy descriptors in the MS-ABI start with '??_R2'
258     else if (SymName.startswith("??_R2")) {
259       // Each relocation names a base class descriptor.  We note the offset into
260       // the section and the symbol used for the relocation.
261       collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
262                                SymName, BCAEntries);
263     }
264     // Base class descriptors in the MS-ABI start with '??_R1'
265     else if (SymName.startswith("??_R1")) {
266       BaseClassDescriptor BCD;
267       BCD.Data = ArrayRef<little32_t>(
268           reinterpret_cast<const little32_t *>(SymContents.data()) + 1, 5);
269       StringRef *I = std::begin(BCD.Symbols), *E = std::end(BCD.Symbols);
270       if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
271                                   E))
272         return;
273       BCDs[SymName] = BCD;
274     }
275     // Type descriptors in the MS-ABI start with '??_R0'
276     else if (SymName.startswith("??_R0")) {
277       const char *DataPtr = SymContents.drop_front(BytesInAddress).data();
278       TypeDescriptor TD;
279       if (BytesInAddress == 8)
280         TD.AlwaysZero = *reinterpret_cast<const little64_t *>(DataPtr);
281       else
282         TD.AlwaysZero = *reinterpret_cast<const little32_t *>(DataPtr);
283       TD.MangledName = SymContents.drop_front(BytesInAddress * 2);
284       StringRef *I = std::begin(TD.Symbols), *E = std::end(TD.Symbols);
285       if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
286                                   E))
287         return;
288       TDs[SymName] = TD;
289     }
290     // 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];
316       CT.Size = DataPtr[5];
317       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     }
323     // 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;
340         const char *DataPtr =
341             SymContents.substr(SymOffI, BytesInAddress).data();
342         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     }
354   }
355   for (const auto &VFTableEntry : VFTableEntries) {
356     StringRef VFTableName = VFTableEntry.first.first;
357     uint64_t Offset = VFTableEntry.first.second;
358     StringRef SymName = VFTableEntry.second;
359     outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n';
360   }
361   for (const auto &VBTable : VBTables) {
362     StringRef VBTableName = VBTable.first;
363     uint32_t Idx = 0;
364     for (little32_t Offset : VBTable.second) {
365       outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n';
366       Idx += sizeof(Offset);
367     }
368   }
369   for (const auto &COLPair : COLs) {
370     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';
376     outs() << COLName << "[ClassHierarchyDescriptor]: " << COL.Symbols[1]
377            << '\n';
378   }
379   for (const auto &CHDPair : CHDs) {
380     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   }
387   for (const auto &BCAEntry : BCAEntries) {
388     StringRef BCAName = BCAEntry.first.first;
389     uint64_t Offset = BCAEntry.first.second;
390     StringRef SymName = BCAEntry.second;
391     outs() << BCAName << '[' << Offset << "]: " << SymName << '\n';
392   }
393   for (const auto &BCDPair : BCDs) {
394     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';
402     outs() << BCDName << "[ClassHierarchyDescriptor]: " << BCD.Symbols[1]
403            << '\n';
404   }
405   for (const auto &TDPair : TDs) {
406     StringRef TDName = TDPair.first;
407     const TypeDescriptor &TD = TDPair.second;
408     outs() << TDName << "[VFPtr]: " << TD.Symbols[0] << '\n';
409     outs() << TDName << "[AlwaysZero]: " << TD.AlwaysZero << '\n';
410     outs() << TDName << "[MangledName]: ";
411     outs().write_escaped(TD.MangledName.rtrim(StringRef("\0", 1)),
412                          /*UseHexEscapes=*/true)
413         << '\n';
414   }
415   for (const auto &TIPair : TIs) {
416     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   }
434   for (const auto &CTAPair : CTAs) {
435     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   }
446   for (const auto &CTPair : CTs) {
447     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';
464     outs() << CTName << "[Size]: " << CT.Size << '\n';
465     outs() << CTName
466            << "[CopyCtor]: " << (CT.Symbols[1].empty() ? "null" : CT.Symbols[1])
467            << '\n';
468   }
469   for (const auto &VTTPair : VTTEntries) {
470     StringRef VTTName = VTTPair.first.first;
471     uint64_t VTTOffset = VTTPair.first.second;
472     StringRef VTTEntry = VTTPair.second;
473     outs() << VTTName << '[' << VTTOffset << "]: " << VTTEntry << '\n';
474   }
475   for (const auto &TIPair : TINames) {
476     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   }
509 }
510 
511 static void dumpArchive(const Archive *Arc) {
512   for (const Archive::Child &ArcC : Arc->children()) {
513     ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcC.getAsBinary();
514     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()))
522       dumpCXXData(Obj);
523     else
524       reportError(Arc->getFileName(), cxxdump_error::unrecognized_file_format);
525   }
526 }
527 
528 static void dumpInput(StringRef File) {
529   // If file isn't stdin, check that it exists.
530   if (File != "-" && !sys::fs::exists(File)) {
531     reportError(File, cxxdump_error::file_not_found);
532     return;
533   }
534 
535   // Attempt to open the binary.
536   ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
537   if (std::error_code EC = BinaryOrErr.getError()) {
538     reportError(File, EC);
539     return;
540   }
541   Binary &Binary = *BinaryOrErr.get().getBinary();
542 
543   if (Archive *Arc = dyn_cast<Archive>(&Binary))
544     dumpArchive(Arc);
545   else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
546     dumpCXXData(Obj);
547   else
548     reportError(File, cxxdump_error::unrecognized_file_format);
549 }
550 
551 int 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 
562   cl::ParseCommandLineOptions(argc, argv, "LLVM C++ ABI Data Dumper\n");
563 
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 }
573