1 //===- llvm-cxxdump.cpp - Dump C++ data in an Object File -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Dumps C++ data resident in object files and archives.
12 //===----------------------------------------------------------------------===//
14 #include "llvm-cxxdump.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"
29 #include <system_error>
32 using namespace llvm::object;
33 using namespace llvm::support;
36 cl::list<std::string> InputFilenames(cl::Positional,
37 cl::desc("<input object files>"),
41 static int ReturnValue = EXIT_SUCCESS;
45 static bool error(std::error_code EC) {
49 ReturnValue = EXIT_FAILURE;
50 outs() << "\nError reading file: " << EC.message() << ".\n";
57 static void reportError(StringRef Input, StringRef Message) {
61 errs() << Input << ": " << Message << "\n";
63 ReturnValue = EXIT_FAILURE;
66 static void reportError(StringRef Input, std::error_code EC) {
67 reportError(Input, EC.message());
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;
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);
82 return SectionRelocMap[Sec];
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()) {
95 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
96 if (RelocSymI == Obj->symbol_end())
98 StringRef RelocSymName;
99 if (error(RelocSymI->getName(RelocSymName)))
102 if (error(Reloc.getOffset(Offset)))
104 if (Offset >= SymOffset && Offset < SymEnd) {
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())
124 StringRef RelocSymName;
125 if (error(RelocSymI->getName(RelocSymName)))
128 if (error(Reloc.getOffset(Offset)))
130 if (Offset >= SymOffset && Offset < SymEnd)
131 Collection[std::make_pair(SymName, Offset - SymOffset)] = RelocSymName;
137 static void dumpCXXData(const ObjectFile *Obj) {
138 struct CompleteObjectLocator {
139 StringRef Symbols[2];
140 ArrayRef<little32_t> Data;
142 struct ClassHierarchyDescriptor {
143 StringRef Symbols[1];
144 ArrayRef<little32_t> Data;
146 struct BaseClassDescriptor {
147 StringRef Symbols[2];
148 ArrayRef<little32_t> Data;
150 struct TypeDescriptor {
151 StringRef Symbols[1];
153 StringRef MangledName;
158 struct CatchableTypeArray {
161 struct CatchableType {
163 uint32_t NonVirtualBaseAdjustmentOffset;
164 int32_t VirtualBasePointerOffset;
165 uint32_t VirtualBaseAdjustmentOffset;
167 StringRef Symbols[2];
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;
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;
187 uint8_t BytesInAddress = Obj->getBytesInAddress();
189 for (const object::SymbolRef &Sym : Obj->symbols()) {
191 if (error(Sym.getName(SymName)))
193 object::section_iterator SecI(Obj->section_begin());
194 if (error(Sym.getSection(SecI)))
196 // Skip external symbols.
197 if (SecI == Obj->section_end())
199 const SectionRef &Sec = *SecI;
200 // Skip virtual or BSS sections.
201 if (Sec.isBSS() || Sec.isVirtual())
203 StringRef SecContents;
204 if (error(Sec.getContents(SecContents)))
206 uint64_t SymAddress, SymSize;
207 if (error(Sym.getAddress(SymAddress)) || error(Sym.getSize(SymSize)))
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);
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);
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;
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,
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,
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);
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,
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();
275 if (BytesInAddress == 8)
276 TD.AlwaysZero = *reinterpret_cast<const little64_t *>(DataPtr);
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,
286 // Throw descriptors in the MS-ABI start with '_TI'
287 else if (SymName.startswith("_TI") || SymName.startswith("__TI")) {
289 TI.Flags = *reinterpret_cast<const little32_t *>(SymContents.data());
290 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
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;
298 *reinterpret_cast<const little32_t *>(SymContents.data());
299 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
300 SymName, CTAEntries);
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());
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,
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);
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'));
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))
336 const char *DataPtr =
337 SymContents.substr(SymOffI, BytesInAddress).data();
339 if (BytesInAddress == 8)
340 VData = *reinterpret_cast<const little64_t *>(DataPtr);
342 VData = *reinterpret_cast<const little32_t *>(DataPtr);
343 VTableDataEntries[Key] = VData;
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!
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';
357 for (const std::pair<StringRef, ArrayRef<little32_t>> &VBTable : VBTables) {
358 StringRef VBTableName = VBTable.first;
360 for (little32_t Offset : VBTable.second) {
361 outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n';
362 Idx += sizeof(Offset);
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]
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';
383 for (const std::pair<std::pair<StringRef, uint64_t>, StringRef> &BCAEntry :
385 StringRef BCAName = BCAEntry.first.first;
386 uint64_t Offset = BCAEntry.first.second;
387 StringRef SymName = BCAEntry.second;
388 outs() << BCAName << '[' << Offset << "]: " << SymName << '\n';
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]
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)
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';
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';
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);
431 for (const std::pair<StringRef, CatchableTypeArray> &CTAPair : CTAs) {
432 StringRef CTAName = CTAPair.first;
433 const CatchableTypeArray &CTA = CTAPair.second;
435 outs() << CTAName << "[NumEntries]: " << CTA.NumEntries << '\n';
438 for (auto I = CTAEntries.lower_bound(std::make_pair(CTAName, 0)),
439 E = CTAEntries.upper_bound(std::make_pair(CTAName, UINT64_MAX));
441 outs() << CTAName << '[' << Idx++ << "]: " << I->second << '\n';
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';
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';
457 << "[VirtualBasePointerOffset]: " << CT.VirtualBasePointerOffset
459 outs() << CTName << "[VirtualBaseAdjustmentOffset]: "
460 << CT.VirtualBaseAdjustmentOffset << '\n';
461 outs() << CTName << "[Size]: " << CT.Size << '\n';
463 << "[CopyCtor]: " << (CT.Symbols[1].empty() ? "null" : CT.Symbols[1])
466 for (const std::pair<std::pair<StringRef, uint64_t>, StringRef> &VTTPair :
468 StringRef VTTName = VTTPair.first.first;
469 uint64_t VTTOffset = VTTPair.first.second;
470 StringRef VTTEntry = VTTPair.second;
471 outs() << VTTName << '[' << VTTOffset << "]: " << VTTEntry << '\n';
473 for (const std::pair<StringRef, StringRef> &TIPair : TINames) {
474 StringRef TIName = TIPair.first;
475 outs() << TIName << ": " << TIPair.second << '\n';
477 auto VTableSymI = VTableSymEntries.begin();
478 auto VTableSymE = VTableSymEntries.end();
479 auto VTableDataI = VTableDataEntries.begin();
480 auto VTableDataE = VTableDataEntries.end();
482 bool SymDone = VTableSymI == VTableSymE;
483 bool DataDone = VTableDataI == VTableDataE;
484 if (SymDone && DataDone)
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;
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;
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());
519 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
522 reportError(Arc->getFileName(), cxxdump_error::unrecognized_file_format);
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);
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);
539 Binary &Binary = *BinaryOrErr.get().getBinary();
541 if (Archive *Arc = dyn_cast<Archive>(&Binary))
543 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
546 reportError(File, cxxdump_error::unrecognized_file_format);
549 int main(int argc, const char *argv[]) {
550 sys::PrintStackTraceOnErrorSignal();
551 PrettyStackTraceProgram X(argc, argv);
554 // Initialize targets.
555 llvm::InitializeAllTargetInfos();
557 // Register the target printer for --version.
558 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
560 cl::ParseCommandLineOptions(argc, argv, "LLVM C++ ABI Data Dumper\n");
562 // Default to stdin if no filename is specified.
563 if (opts::InputFilenames.size() == 0)
564 opts::InputFilenames.push_back("-");
566 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),