llvm-cxxdump.cpp
21.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//===- llvm-cxxdump.cpp - Dump C++ data in an Object File -------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Dumps C++ data resident in object files and archives.
//
//===----------------------------------------------------------------------===//
#include "llvm-cxxdump.h"
#include "Error.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Object/Archive.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Object/SymbolSize.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/WithColor.h"
#include "llvm/Support/raw_ostream.h"
#include <map>
#include <string>
#include <system_error>
using namespace llvm;
using namespace llvm::object;
using namespace llvm::support;
namespace opts {
cl::list<std::string> InputFilenames(cl::Positional,
cl::desc("<input object files>"),
cl::ZeroOrMore);
} // namespace opts
namespace llvm {
static void error(std::error_code EC) {
if (!EC)
return;
WithColor::error(outs(), "") << "reading file: " << EC.message() << ".\n";
outs().flush();
exit(1);
}
LLVM_ATTRIBUTE_NORETURN static void error(Error Err) {
logAllUnhandledErrors(std::move(Err), WithColor::error(outs()),
"reading file: ");
outs().flush();
exit(1);
}
template <typename T>
T unwrapOrError(Expected<T> EO) {
if (!EO)
error(EO.takeError());
return std::move(*EO);
}
} // namespace llvm
static void reportError(StringRef Input, StringRef Message) {
if (Input == "-")
Input = "<stdin>";
WithColor::error(errs(), Input) << Message << "\n";
errs().flush();
exit(1);
}
static void reportError(StringRef Input, std::error_code EC) {
reportError(Input, EC.message());
}
static std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
static void collectRelocatedSymbols(const ObjectFile *Obj,
const SectionRef &Sec, uint64_t SecAddress,
uint64_t SymAddress, uint64_t SymSize,
StringRef *I, StringRef *E) {
uint64_t SymOffset = SymAddress - SecAddress;
uint64_t SymEnd = SymOffset + SymSize;
for (const SectionRef &SR : SectionRelocMap[Sec]) {
for (const object::RelocationRef &Reloc : SR.relocations()) {
if (I == E)
break;
const object::symbol_iterator RelocSymI = Reloc.getSymbol();
if (RelocSymI == Obj->symbol_end())
continue;
Expected<StringRef> RelocSymName = RelocSymI->getName();
error(errorToErrorCode(RelocSymName.takeError()));
uint64_t Offset = Reloc.getOffset();
if (Offset >= SymOffset && Offset < SymEnd) {
*I = *RelocSymName;
++I;
}
}
}
}
static void collectRelocationOffsets(
const ObjectFile *Obj, const SectionRef &Sec, uint64_t SecAddress,
uint64_t SymAddress, uint64_t SymSize, StringRef SymName,
std::map<std::pair<StringRef, uint64_t>, StringRef> &Collection) {
uint64_t SymOffset = SymAddress - SecAddress;
uint64_t SymEnd = SymOffset + SymSize;
for (const SectionRef &SR : SectionRelocMap[Sec]) {
for (const object::RelocationRef &Reloc : SR.relocations()) {
const object::symbol_iterator RelocSymI = Reloc.getSymbol();
if (RelocSymI == Obj->symbol_end())
continue;
Expected<StringRef> RelocSymName = RelocSymI->getName();
error(errorToErrorCode(RelocSymName.takeError()));
uint64_t Offset = Reloc.getOffset();
if (Offset >= SymOffset && Offset < SymEnd)
Collection[std::make_pair(SymName, Offset - SymOffset)] = *RelocSymName;
}
}
}
static void dumpCXXData(const ObjectFile *Obj) {
struct CompleteObjectLocator {
StringRef Symbols[2];
ArrayRef<little32_t> Data;
};
struct ClassHierarchyDescriptor {
StringRef Symbols[1];
ArrayRef<little32_t> Data;
};
struct BaseClassDescriptor {
StringRef Symbols[2];
ArrayRef<little32_t> Data;
};
struct TypeDescriptor {
StringRef Symbols[1];
uint64_t AlwaysZero;
StringRef MangledName;
};
struct ThrowInfo {
uint32_t Flags;
};
struct CatchableTypeArray {
uint32_t NumEntries;
};
struct CatchableType {
uint32_t Flags;
uint32_t NonVirtualBaseAdjustmentOffset;
int32_t VirtualBasePointerOffset;
uint32_t VirtualBaseAdjustmentOffset;
uint32_t Size;
StringRef Symbols[2];
};
std::map<std::pair<StringRef, uint64_t>, StringRef> VFTableEntries;
std::map<std::pair<StringRef, uint64_t>, StringRef> TIEntries;
std::map<std::pair<StringRef, uint64_t>, StringRef> CTAEntries;
std::map<StringRef, ArrayRef<little32_t>> VBTables;
std::map<StringRef, CompleteObjectLocator> COLs;
std::map<StringRef, ClassHierarchyDescriptor> CHDs;
std::map<std::pair<StringRef, uint64_t>, StringRef> BCAEntries;
std::map<StringRef, BaseClassDescriptor> BCDs;
std::map<StringRef, TypeDescriptor> TDs;
std::map<StringRef, ThrowInfo> TIs;
std::map<StringRef, CatchableTypeArray> CTAs;
std::map<StringRef, CatchableType> CTs;
std::map<std::pair<StringRef, uint64_t>, StringRef> VTableSymEntries;
std::map<std::pair<StringRef, uint64_t>, int64_t> VTableDataEntries;
std::map<std::pair<StringRef, uint64_t>, StringRef> VTTEntries;
std::map<StringRef, StringRef> TINames;
SectionRelocMap.clear();
for (const SectionRef &Section : Obj->sections()) {
Expected<section_iterator> ErrOrSec = Section.getRelocatedSection();
if (!ErrOrSec)
error(ErrOrSec.takeError());
section_iterator Sec2 = *ErrOrSec;
if (Sec2 != Obj->section_end())
SectionRelocMap[*Sec2].push_back(Section);
}
uint8_t BytesInAddress = Obj->getBytesInAddress();
std::vector<std::pair<SymbolRef, uint64_t>> SymAddr =
object::computeSymbolSizes(*Obj);
for (auto &P : SymAddr) {
object::SymbolRef Sym = P.first;
uint64_t SymSize = P.second;
Expected<StringRef> SymNameOrErr = Sym.getName();
error(errorToErrorCode(SymNameOrErr.takeError()));
StringRef SymName = *SymNameOrErr;
Expected<object::section_iterator> SecIOrErr = Sym.getSection();
error(errorToErrorCode(SecIOrErr.takeError()));
object::section_iterator SecI = *SecIOrErr;
// Skip external symbols.
if (SecI == Obj->section_end())
continue;
const SectionRef &Sec = *SecI;
// Skip virtual or BSS sections.
if (Sec.isBSS() || Sec.isVirtual())
continue;
StringRef SecContents = unwrapOrError(Sec.getContents());
Expected<uint64_t> SymAddressOrErr = Sym.getAddress();
error(errorToErrorCode(SymAddressOrErr.takeError()));
uint64_t SymAddress = *SymAddressOrErr;
uint64_t SecAddress = Sec.getAddress();
uint64_t SecSize = Sec.getSize();
uint64_t SymOffset = SymAddress - SecAddress;
StringRef SymContents = SecContents.substr(SymOffset, SymSize);
// VFTables in the MS-ABI start with '??_7' and are contained within their
// own COMDAT section. We then determine the contents of the VFTable by
// looking at each relocation in the section.
if (SymName.startswith("??_7")) {
// Each relocation either names a virtual method or a thunk. We note the
// offset into the section and the symbol used for the relocation.
collectRelocationOffsets(Obj, Sec, SecAddress, SecAddress, SecSize,
SymName, VFTableEntries);
}
// VBTables in the MS-ABI start with '??_8' and are filled with 32-bit
// offsets of virtual bases.
else if (SymName.startswith("??_8")) {
ArrayRef<little32_t> VBTableData(
reinterpret_cast<const little32_t *>(SymContents.data()),
SymContents.size() / sizeof(little32_t));
VBTables[SymName] = VBTableData;
}
// Complete object locators in the MS-ABI start with '??_R4'
else if (SymName.startswith("??_R4")) {
CompleteObjectLocator COL;
COL.Data = makeArrayRef(
reinterpret_cast<const little32_t *>(SymContents.data()), 3);
StringRef *I = std::begin(COL.Symbols), *E = std::end(COL.Symbols);
collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
COLs[SymName] = COL;
}
// Class hierarchy descriptors in the MS-ABI start with '??_R3'
else if (SymName.startswith("??_R3")) {
ClassHierarchyDescriptor CHD;
CHD.Data = makeArrayRef(
reinterpret_cast<const little32_t *>(SymContents.data()), 3);
StringRef *I = std::begin(CHD.Symbols), *E = std::end(CHD.Symbols);
collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
CHDs[SymName] = CHD;
}
// Class hierarchy descriptors in the MS-ABI start with '??_R2'
else if (SymName.startswith("??_R2")) {
// Each relocation names a base class descriptor. We note the offset into
// the section and the symbol used for the relocation.
collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
SymName, BCAEntries);
}
// Base class descriptors in the MS-ABI start with '??_R1'
else if (SymName.startswith("??_R1")) {
BaseClassDescriptor BCD;
BCD.Data = makeArrayRef(
reinterpret_cast<const little32_t *>(SymContents.data()) + 1, 5);
StringRef *I = std::begin(BCD.Symbols), *E = std::end(BCD.Symbols);
collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
BCDs[SymName] = BCD;
}
// Type descriptors in the MS-ABI start with '??_R0'
else if (SymName.startswith("??_R0")) {
const char *DataPtr = SymContents.drop_front(BytesInAddress).data();
TypeDescriptor TD;
if (BytesInAddress == 8)
TD.AlwaysZero = *reinterpret_cast<const little64_t *>(DataPtr);
else
TD.AlwaysZero = *reinterpret_cast<const little32_t *>(DataPtr);
TD.MangledName = SymContents.drop_front(BytesInAddress * 2);
StringRef *I = std::begin(TD.Symbols), *E = std::end(TD.Symbols);
collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
TDs[SymName] = TD;
}
// Throw descriptors in the MS-ABI start with '_TI'
else if (SymName.startswith("_TI") || SymName.startswith("__TI")) {
ThrowInfo TI;
TI.Flags = *reinterpret_cast<const little32_t *>(SymContents.data());
collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
SymName, TIEntries);
TIs[SymName] = TI;
}
// Catchable type arrays in the MS-ABI start with _CTA or __CTA.
else if (SymName.startswith("_CTA") || SymName.startswith("__CTA")) {
CatchableTypeArray CTA;
CTA.NumEntries =
*reinterpret_cast<const little32_t *>(SymContents.data());
collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
SymName, CTAEntries);
CTAs[SymName] = CTA;
}
// Catchable types in the MS-ABI start with _CT or __CT.
else if (SymName.startswith("_CT") || SymName.startswith("__CT")) {
const little32_t *DataPtr =
reinterpret_cast<const little32_t *>(SymContents.data());
CatchableType CT;
CT.Flags = DataPtr[0];
CT.NonVirtualBaseAdjustmentOffset = DataPtr[2];
CT.VirtualBasePointerOffset = DataPtr[3];
CT.VirtualBaseAdjustmentOffset = DataPtr[4];
CT.Size = DataPtr[5];
StringRef *I = std::begin(CT.Symbols), *E = std::end(CT.Symbols);
collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
CTs[SymName] = CT;
}
// Construction vtables in the Itanium ABI start with '_ZTT' or '__ZTT'.
else if (SymName.startswith("_ZTT") || SymName.startswith("__ZTT")) {
collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
SymName, VTTEntries);
}
// Typeinfo names in the Itanium ABI start with '_ZTS' or '__ZTS'.
else if (SymName.startswith("_ZTS") || SymName.startswith("__ZTS")) {
TINames[SymName] = SymContents.slice(0, SymContents.find('\0'));
}
// Vtables in the Itanium ABI start with '_ZTV' or '__ZTV'.
else if (SymName.startswith("_ZTV") || SymName.startswith("__ZTV")) {
collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
SymName, VTableSymEntries);
for (uint64_t SymOffI = 0; SymOffI < SymSize; SymOffI += BytesInAddress) {
auto Key = std::make_pair(SymName, SymOffI);
if (VTableSymEntries.count(Key))
continue;
const char *DataPtr =
SymContents.substr(SymOffI, BytesInAddress).data();
int64_t VData;
if (BytesInAddress == 8)
VData = *reinterpret_cast<const little64_t *>(DataPtr);
else
VData = *reinterpret_cast<const little32_t *>(DataPtr);
VTableDataEntries[Key] = VData;
}
}
// Typeinfo structures in the Itanium ABI start with '_ZTI' or '__ZTI'.
else if (SymName.startswith("_ZTI") || SymName.startswith("__ZTI")) {
// FIXME: Do something with these!
}
}
for (const auto &VFTableEntry : VFTableEntries) {
StringRef VFTableName = VFTableEntry.first.first;
uint64_t Offset = VFTableEntry.first.second;
StringRef SymName = VFTableEntry.second;
outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n';
}
for (const auto &VBTable : VBTables) {
StringRef VBTableName = VBTable.first;
uint32_t Idx = 0;
for (little32_t Offset : VBTable.second) {
outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n';
Idx += sizeof(Offset);
}
}
for (const auto &COLPair : COLs) {
StringRef COLName = COLPair.first;
const CompleteObjectLocator &COL = COLPair.second;
outs() << COLName << "[IsImageRelative]: " << COL.Data[0] << '\n';
outs() << COLName << "[OffsetToTop]: " << COL.Data[1] << '\n';
outs() << COLName << "[VFPtrOffset]: " << COL.Data[2] << '\n';
outs() << COLName << "[TypeDescriptor]: " << COL.Symbols[0] << '\n';
outs() << COLName << "[ClassHierarchyDescriptor]: " << COL.Symbols[1]
<< '\n';
}
for (const auto &CHDPair : CHDs) {
StringRef CHDName = CHDPair.first;
const ClassHierarchyDescriptor &CHD = CHDPair.second;
outs() << CHDName << "[AlwaysZero]: " << CHD.Data[0] << '\n';
outs() << CHDName << "[Flags]: " << CHD.Data[1] << '\n';
outs() << CHDName << "[NumClasses]: " << CHD.Data[2] << '\n';
outs() << CHDName << "[BaseClassArray]: " << CHD.Symbols[0] << '\n';
}
for (const auto &BCAEntry : BCAEntries) {
StringRef BCAName = BCAEntry.first.first;
uint64_t Offset = BCAEntry.first.second;
StringRef SymName = BCAEntry.second;
outs() << BCAName << '[' << Offset << "]: " << SymName << '\n';
}
for (const auto &BCDPair : BCDs) {
StringRef BCDName = BCDPair.first;
const BaseClassDescriptor &BCD = BCDPair.second;
outs() << BCDName << "[TypeDescriptor]: " << BCD.Symbols[0] << '\n';
outs() << BCDName << "[NumBases]: " << BCD.Data[0] << '\n';
outs() << BCDName << "[OffsetInVBase]: " << BCD.Data[1] << '\n';
outs() << BCDName << "[VBPtrOffset]: " << BCD.Data[2] << '\n';
outs() << BCDName << "[OffsetInVBTable]: " << BCD.Data[3] << '\n';
outs() << BCDName << "[Flags]: " << BCD.Data[4] << '\n';
outs() << BCDName << "[ClassHierarchyDescriptor]: " << BCD.Symbols[1]
<< '\n';
}
for (const auto &TDPair : TDs) {
StringRef TDName = TDPair.first;
const TypeDescriptor &TD = TDPair.second;
outs() << TDName << "[VFPtr]: " << TD.Symbols[0] << '\n';
outs() << TDName << "[AlwaysZero]: " << TD.AlwaysZero << '\n';
outs() << TDName << "[MangledName]: ";
outs().write_escaped(TD.MangledName.rtrim(StringRef("\0", 1)),
/*UseHexEscapes=*/true)
<< '\n';
}
for (const auto &TIPair : TIs) {
StringRef TIName = TIPair.first;
const ThrowInfo &TI = TIPair.second;
auto dumpThrowInfoFlag = [&](const char *Name, uint32_t Flag) {
outs() << TIName << "[Flags." << Name
<< "]: " << (TI.Flags & Flag ? "true" : "false") << '\n';
};
auto dumpThrowInfoSymbol = [&](const char *Name, int Offset) {
outs() << TIName << '[' << Name << "]: ";
auto Entry = TIEntries.find(std::make_pair(TIName, Offset));
outs() << (Entry == TIEntries.end() ? "null" : Entry->second) << '\n';
};
outs() << TIName << "[Flags]: " << TI.Flags << '\n';
dumpThrowInfoFlag("Const", 1);
dumpThrowInfoFlag("Volatile", 2);
dumpThrowInfoSymbol("CleanupFn", 4);
dumpThrowInfoSymbol("ForwardCompat", 8);
dumpThrowInfoSymbol("CatchableTypeArray", 12);
}
for (const auto &CTAPair : CTAs) {
StringRef CTAName = CTAPair.first;
const CatchableTypeArray &CTA = CTAPair.second;
outs() << CTAName << "[NumEntries]: " << CTA.NumEntries << '\n';
unsigned Idx = 0;
for (auto I = CTAEntries.lower_bound(std::make_pair(CTAName, 0)),
E = CTAEntries.upper_bound(std::make_pair(CTAName, UINT64_MAX));
I != E; ++I)
outs() << CTAName << '[' << Idx++ << "]: " << I->second << '\n';
}
for (const auto &CTPair : CTs) {
StringRef CTName = CTPair.first;
const CatchableType &CT = CTPair.second;
auto dumpCatchableTypeFlag = [&](const char *Name, uint32_t Flag) {
outs() << CTName << "[Flags." << Name
<< "]: " << (CT.Flags & Flag ? "true" : "false") << '\n';
};
outs() << CTName << "[Flags]: " << CT.Flags << '\n';
dumpCatchableTypeFlag("ScalarType", 1);
dumpCatchableTypeFlag("VirtualInheritance", 4);
outs() << CTName << "[TypeDescriptor]: " << CT.Symbols[0] << '\n';
outs() << CTName << "[NonVirtualBaseAdjustmentOffset]: "
<< CT.NonVirtualBaseAdjustmentOffset << '\n';
outs() << CTName
<< "[VirtualBasePointerOffset]: " << CT.VirtualBasePointerOffset
<< '\n';
outs() << CTName << "[VirtualBaseAdjustmentOffset]: "
<< CT.VirtualBaseAdjustmentOffset << '\n';
outs() << CTName << "[Size]: " << CT.Size << '\n';
outs() << CTName
<< "[CopyCtor]: " << (CT.Symbols[1].empty() ? "null" : CT.Symbols[1])
<< '\n';
}
for (const auto &VTTPair : VTTEntries) {
StringRef VTTName = VTTPair.first.first;
uint64_t VTTOffset = VTTPair.first.second;
StringRef VTTEntry = VTTPair.second;
outs() << VTTName << '[' << VTTOffset << "]: " << VTTEntry << '\n';
}
for (const auto &TIPair : TINames) {
StringRef TIName = TIPair.first;
outs() << TIName << ": " << TIPair.second << '\n';
}
auto VTableSymI = VTableSymEntries.begin();
auto VTableSymE = VTableSymEntries.end();
auto VTableDataI = VTableDataEntries.begin();
auto VTableDataE = VTableDataEntries.end();
for (;;) {
bool SymDone = VTableSymI == VTableSymE;
bool DataDone = VTableDataI == VTableDataE;
if (SymDone && DataDone)
break;
if (!SymDone && (DataDone || VTableSymI->first < VTableDataI->first)) {
StringRef VTableName = VTableSymI->first.first;
uint64_t Offset = VTableSymI->first.second;
StringRef VTableEntry = VTableSymI->second;
outs() << VTableName << '[' << Offset << "]: ";
outs() << VTableEntry;
outs() << '\n';
++VTableSymI;
continue;
}
if (!DataDone && (SymDone || VTableDataI->first < VTableSymI->first)) {
StringRef VTableName = VTableDataI->first.first;
uint64_t Offset = VTableDataI->first.second;
int64_t VTableEntry = VTableDataI->second;
outs() << VTableName << '[' << Offset << "]: ";
outs() << VTableEntry;
outs() << '\n';
++VTableDataI;
continue;
}
}
}
static void dumpArchive(const Archive *Arc) {
Error Err = Error::success();
for (auto &ArcC : Arc->children(Err)) {
Expected<std::unique_ptr<Binary>> ChildOrErr = ArcC.getAsBinary();
if (!ChildOrErr) {
// Ignore non-object files.
if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) {
std::string Buf;
raw_string_ostream OS(Buf);
logAllUnhandledErrors(std::move(E), OS);
OS.flush();
reportError(Arc->getFileName(), Buf);
}
consumeError(ChildOrErr.takeError());
continue;
}
if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
dumpCXXData(Obj);
else
reportError(Arc->getFileName(), cxxdump_error::unrecognized_file_format);
}
if (Err)
error(std::move(Err));
}
static void dumpInput(StringRef File) {
// Attempt to open the binary.
Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
if (!BinaryOrErr) {
auto EC = errorToErrorCode(BinaryOrErr.takeError());
reportError(File, EC);
return;
}
Binary &Binary = *BinaryOrErr.get().getBinary();
if (Archive *Arc = dyn_cast<Archive>(&Binary))
dumpArchive(Arc);
else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
dumpCXXData(Obj);
else
reportError(File, cxxdump_error::unrecognized_file_format);
}
int main(int argc, const char *argv[]) {
InitLLVM X(argc, argv);
// Initialize targets.
llvm::InitializeAllTargetInfos();
// Register the target printer for --version.
cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
cl::ParseCommandLineOptions(argc, argv, "LLVM C++ ABI Data Dumper\n");
// Default to stdin if no filename is specified.
if (opts::InputFilenames.size() == 0)
opts::InputFilenames.push_back("-");
llvm::for_each(opts::InputFilenames, dumpInput);
return EXIT_SUCCESS;
}