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/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"
31 #include <system_error>
34 using namespace llvm::object;
35 using namespace llvm::support;
38 cl::list<std::string> InputFilenames(cl::Positional,
39 cl::desc("<input object files>"),
45 static void error(std::error_code EC) {
48 outs() << "\nError reading file: " << EC.message() << ".\n";
55 static void reportError(StringRef Input, StringRef Message) {
58 errs() << Input << ": " << Message << "\n";
63 static void reportError(StringRef Input, std::error_code EC) {
64 reportError(Input, EC.message());
67 static std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
69 static void collectRelocatedSymbols(const ObjectFile *Obj,
70 const SectionRef &Sec, uint64_t SecAddress,
71 uint64_t SymAddress, uint64_t SymSize,
72 StringRef *I, StringRef *E) {
73 uint64_t SymOffset = SymAddress - SecAddress;
74 uint64_t SymEnd = SymOffset + SymSize;
75 for (const SectionRef &SR : SectionRelocMap[Sec]) {
76 for (const object::RelocationRef &Reloc : SR.relocations()) {
79 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
80 if (RelocSymI == Obj->symbol_end())
82 ErrorOr<StringRef> RelocSymName = RelocSymI->getName();
83 error(RelocSymName.getError());
84 uint64_t Offset = Reloc.getOffset();
85 if (Offset >= SymOffset && Offset < SymEnd) {
93 static void collectRelocationOffsets(
94 const ObjectFile *Obj, const SectionRef &Sec, uint64_t SecAddress,
95 uint64_t SymAddress, uint64_t SymSize, StringRef SymName,
96 std::map<std::pair<StringRef, uint64_t>, StringRef> &Collection) {
97 uint64_t SymOffset = SymAddress - SecAddress;
98 uint64_t SymEnd = SymOffset + SymSize;
99 for (const SectionRef &SR : SectionRelocMap[Sec]) {
100 for (const object::RelocationRef &Reloc : SR.relocations()) {
101 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
102 if (RelocSymI == Obj->symbol_end())
104 ErrorOr<StringRef> RelocSymName = RelocSymI->getName();
105 error(RelocSymName.getError());
106 uint64_t Offset = Reloc.getOffset();
107 if (Offset >= SymOffset && Offset < SymEnd)
108 Collection[std::make_pair(SymName, Offset - SymOffset)] = *RelocSymName;
113 static void dumpCXXData(const ObjectFile *Obj) {
114 struct CompleteObjectLocator {
115 StringRef Symbols[2];
116 ArrayRef<little32_t> Data;
118 struct ClassHierarchyDescriptor {
119 StringRef Symbols[1];
120 ArrayRef<little32_t> Data;
122 struct BaseClassDescriptor {
123 StringRef Symbols[2];
124 ArrayRef<little32_t> Data;
126 struct TypeDescriptor {
127 StringRef Symbols[1];
129 StringRef MangledName;
134 struct CatchableTypeArray {
137 struct CatchableType {
139 uint32_t NonVirtualBaseAdjustmentOffset;
140 int32_t VirtualBasePointerOffset;
141 uint32_t VirtualBaseAdjustmentOffset;
143 StringRef Symbols[2];
145 std::map<std::pair<StringRef, uint64_t>, StringRef> VFTableEntries;
146 std::map<std::pair<StringRef, uint64_t>, StringRef> TIEntries;
147 std::map<std::pair<StringRef, uint64_t>, StringRef> CTAEntries;
148 std::map<StringRef, ArrayRef<little32_t>> VBTables;
149 std::map<StringRef, CompleteObjectLocator> COLs;
150 std::map<StringRef, ClassHierarchyDescriptor> CHDs;
151 std::map<std::pair<StringRef, uint64_t>, StringRef> BCAEntries;
152 std::map<StringRef, BaseClassDescriptor> BCDs;
153 std::map<StringRef, TypeDescriptor> TDs;
154 std::map<StringRef, ThrowInfo> TIs;
155 std::map<StringRef, CatchableTypeArray> CTAs;
156 std::map<StringRef, CatchableType> CTs;
158 std::map<std::pair<StringRef, uint64_t>, StringRef> VTableSymEntries;
159 std::map<std::pair<StringRef, uint64_t>, int64_t> VTableDataEntries;
160 std::map<std::pair<StringRef, uint64_t>, StringRef> VTTEntries;
161 std::map<StringRef, StringRef> TINames;
163 SectionRelocMap.clear();
164 for (const SectionRef &Section : Obj->sections()) {
165 section_iterator Sec2 = Section.getRelocatedSection();
166 if (Sec2 != Obj->section_end())
167 SectionRelocMap[*Sec2].push_back(Section);
170 uint8_t BytesInAddress = Obj->getBytesInAddress();
172 std::vector<std::pair<SymbolRef, uint64_t>> SymAddr =
173 object::computeSymbolSizes(*Obj);
175 for (auto &P : SymAddr) {
176 object::SymbolRef Sym = P.first;
177 uint64_t SymSize = P.second;
178 ErrorOr<StringRef> SymNameOrErr = Sym.getName();
179 error(SymNameOrErr.getError());
180 StringRef SymName = *SymNameOrErr;
181 ErrorOr<object::section_iterator> SecIOrErr = Sym.getSection();
182 error(SecIOrErr.getError());
183 object::section_iterator SecI = *SecIOrErr;
184 // Skip external symbols.
185 if (SecI == Obj->section_end())
187 const SectionRef &Sec = *SecI;
188 // Skip virtual or BSS sections.
189 if (Sec.isBSS() || Sec.isVirtual())
191 StringRef SecContents;
192 error(Sec.getContents(SecContents));
193 ErrorOr<uint64_t> SymAddressOrErr = Sym.getAddress();
194 error(SymAddressOrErr.getError());
195 uint64_t SymAddress = *SymAddressOrErr;
196 uint64_t SecAddress = Sec.getAddress();
197 uint64_t SecSize = Sec.getSize();
198 uint64_t SymOffset = SymAddress - SecAddress;
199 StringRef SymContents = SecContents.substr(SymOffset, SymSize);
201 // VFTables in the MS-ABI start with '??_7' and are contained within their
202 // own COMDAT section. We then determine the contents of the VFTable by
203 // looking at each relocation in the section.
204 if (SymName.startswith("??_7")) {
205 // Each relocation either names a virtual method or a thunk. We note the
206 // offset into the section and the symbol used for the relocation.
207 collectRelocationOffsets(Obj, Sec, SecAddress, SecAddress, SecSize,
208 SymName, VFTableEntries);
210 // VBTables in the MS-ABI start with '??_8' and are filled with 32-bit
211 // offsets of virtual bases.
212 else if (SymName.startswith("??_8")) {
213 ArrayRef<little32_t> VBTableData(
214 reinterpret_cast<const little32_t *>(SymContents.data()),
215 SymContents.size() / sizeof(little32_t));
216 VBTables[SymName] = VBTableData;
218 // Complete object locators in the MS-ABI start with '??_R4'
219 else if (SymName.startswith("??_R4")) {
220 CompleteObjectLocator COL;
221 COL.Data = ArrayRef<little32_t>(
222 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
223 StringRef *I = std::begin(COL.Symbols), *E = std::end(COL.Symbols);
224 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
227 // Class hierarchy descriptors in the MS-ABI start with '??_R3'
228 else if (SymName.startswith("??_R3")) {
229 ClassHierarchyDescriptor CHD;
230 CHD.Data = ArrayRef<little32_t>(
231 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
232 StringRef *I = std::begin(CHD.Symbols), *E = std::end(CHD.Symbols);
233 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
236 // Class hierarchy descriptors in the MS-ABI start with '??_R2'
237 else if (SymName.startswith("??_R2")) {
238 // Each relocation names a base class descriptor. We note the offset into
239 // the section and the symbol used for the relocation.
240 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
241 SymName, BCAEntries);
243 // Base class descriptors in the MS-ABI start with '??_R1'
244 else if (SymName.startswith("??_R1")) {
245 BaseClassDescriptor BCD;
246 BCD.Data = ArrayRef<little32_t>(
247 reinterpret_cast<const little32_t *>(SymContents.data()) + 1, 5);
248 StringRef *I = std::begin(BCD.Symbols), *E = std::end(BCD.Symbols);
249 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
252 // Type descriptors in the MS-ABI start with '??_R0'
253 else if (SymName.startswith("??_R0")) {
254 const char *DataPtr = SymContents.drop_front(BytesInAddress).data();
256 if (BytesInAddress == 8)
257 TD.AlwaysZero = *reinterpret_cast<const little64_t *>(DataPtr);
259 TD.AlwaysZero = *reinterpret_cast<const little32_t *>(DataPtr);
260 TD.MangledName = SymContents.drop_front(BytesInAddress * 2);
261 StringRef *I = std::begin(TD.Symbols), *E = std::end(TD.Symbols);
262 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
265 // Throw descriptors in the MS-ABI start with '_TI'
266 else if (SymName.startswith("_TI") || SymName.startswith("__TI")) {
268 TI.Flags = *reinterpret_cast<const little32_t *>(SymContents.data());
269 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
273 // Catchable type arrays in the MS-ABI start with _CTA or __CTA.
274 else if (SymName.startswith("_CTA") || SymName.startswith("__CTA")) {
275 CatchableTypeArray CTA;
277 *reinterpret_cast<const little32_t *>(SymContents.data());
278 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
279 SymName, CTAEntries);
282 // Catchable types in the MS-ABI start with _CT or __CT.
283 else if (SymName.startswith("_CT") || SymName.startswith("__CT")) {
284 const little32_t *DataPtr =
285 reinterpret_cast<const little32_t *>(SymContents.data());
287 CT.Flags = DataPtr[0];
288 CT.NonVirtualBaseAdjustmentOffset = DataPtr[2];
289 CT.VirtualBasePointerOffset = DataPtr[3];
290 CT.VirtualBaseAdjustmentOffset = DataPtr[4];
291 CT.Size = DataPtr[5];
292 StringRef *I = std::begin(CT.Symbols), *E = std::end(CT.Symbols);
293 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
296 // Construction vtables in the Itanium ABI start with '_ZTT' or '__ZTT'.
297 else if (SymName.startswith("_ZTT") || SymName.startswith("__ZTT")) {
298 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
299 SymName, VTTEntries);
301 // Typeinfo names in the Itanium ABI start with '_ZTS' or '__ZTS'.
302 else if (SymName.startswith("_ZTS") || SymName.startswith("__ZTS")) {
303 TINames[SymName] = SymContents.slice(0, SymContents.find('\0'));
305 // Vtables in the Itanium ABI start with '_ZTV' or '__ZTV'.
306 else if (SymName.startswith("_ZTV") || SymName.startswith("__ZTV")) {
307 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
308 SymName, VTableSymEntries);
309 for (uint64_t SymOffI = 0; SymOffI < SymSize; SymOffI += BytesInAddress) {
310 auto Key = std::make_pair(SymName, SymOffI);
311 if (VTableSymEntries.count(Key))
313 const char *DataPtr =
314 SymContents.substr(SymOffI, BytesInAddress).data();
316 if (BytesInAddress == 8)
317 VData = *reinterpret_cast<const little64_t *>(DataPtr);
319 VData = *reinterpret_cast<const little32_t *>(DataPtr);
320 VTableDataEntries[Key] = VData;
323 // Typeinfo structures in the Itanium ABI start with '_ZTI' or '__ZTI'.
324 else if (SymName.startswith("_ZTI") || SymName.startswith("__ZTI")) {
325 // FIXME: Do something with these!
328 for (const auto &VFTableEntry : VFTableEntries) {
329 StringRef VFTableName = VFTableEntry.first.first;
330 uint64_t Offset = VFTableEntry.first.second;
331 StringRef SymName = VFTableEntry.second;
332 outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n';
334 for (const auto &VBTable : VBTables) {
335 StringRef VBTableName = VBTable.first;
337 for (little32_t Offset : VBTable.second) {
338 outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n';
339 Idx += sizeof(Offset);
342 for (const auto &COLPair : COLs) {
343 StringRef COLName = COLPair.first;
344 const CompleteObjectLocator &COL = COLPair.second;
345 outs() << COLName << "[IsImageRelative]: " << COL.Data[0] << '\n';
346 outs() << COLName << "[OffsetToTop]: " << COL.Data[1] << '\n';
347 outs() << COLName << "[VFPtrOffset]: " << COL.Data[2] << '\n';
348 outs() << COLName << "[TypeDescriptor]: " << COL.Symbols[0] << '\n';
349 outs() << COLName << "[ClassHierarchyDescriptor]: " << COL.Symbols[1]
352 for (const auto &CHDPair : CHDs) {
353 StringRef CHDName = CHDPair.first;
354 const ClassHierarchyDescriptor &CHD = CHDPair.second;
355 outs() << CHDName << "[AlwaysZero]: " << CHD.Data[0] << '\n';
356 outs() << CHDName << "[Flags]: " << CHD.Data[1] << '\n';
357 outs() << CHDName << "[NumClasses]: " << CHD.Data[2] << '\n';
358 outs() << CHDName << "[BaseClassArray]: " << CHD.Symbols[0] << '\n';
360 for (const auto &BCAEntry : BCAEntries) {
361 StringRef BCAName = BCAEntry.first.first;
362 uint64_t Offset = BCAEntry.first.second;
363 StringRef SymName = BCAEntry.second;
364 outs() << BCAName << '[' << Offset << "]: " << SymName << '\n';
366 for (const auto &BCDPair : BCDs) {
367 StringRef BCDName = BCDPair.first;
368 const BaseClassDescriptor &BCD = BCDPair.second;
369 outs() << BCDName << "[TypeDescriptor]: " << BCD.Symbols[0] << '\n';
370 outs() << BCDName << "[NumBases]: " << BCD.Data[0] << '\n';
371 outs() << BCDName << "[OffsetInVBase]: " << BCD.Data[1] << '\n';
372 outs() << BCDName << "[VBPtrOffset]: " << BCD.Data[2] << '\n';
373 outs() << BCDName << "[OffsetInVBTable]: " << BCD.Data[3] << '\n';
374 outs() << BCDName << "[Flags]: " << BCD.Data[4] << '\n';
375 outs() << BCDName << "[ClassHierarchyDescriptor]: " << BCD.Symbols[1]
378 for (const auto &TDPair : TDs) {
379 StringRef TDName = TDPair.first;
380 const TypeDescriptor &TD = TDPair.second;
381 outs() << TDName << "[VFPtr]: " << TD.Symbols[0] << '\n';
382 outs() << TDName << "[AlwaysZero]: " << TD.AlwaysZero << '\n';
383 outs() << TDName << "[MangledName]: ";
384 outs().write_escaped(TD.MangledName.rtrim(StringRef("\0", 1)),
385 /*UseHexEscapes=*/true)
388 for (const auto &TIPair : TIs) {
389 StringRef TIName = TIPair.first;
390 const ThrowInfo &TI = TIPair.second;
391 auto dumpThrowInfoFlag = [&](const char *Name, uint32_t Flag) {
392 outs() << TIName << "[Flags." << Name
393 << "]: " << (TI.Flags & Flag ? "true" : "false") << '\n';
395 auto dumpThrowInfoSymbol = [&](const char *Name, int Offset) {
396 outs() << TIName << '[' << Name << "]: ";
397 auto Entry = TIEntries.find(std::make_pair(TIName, Offset));
398 outs() << (Entry == TIEntries.end() ? "null" : Entry->second) << '\n';
400 outs() << TIName << "[Flags]: " << TI.Flags << '\n';
401 dumpThrowInfoFlag("Const", 1);
402 dumpThrowInfoFlag("Volatile", 2);
403 dumpThrowInfoSymbol("CleanupFn", 4);
404 dumpThrowInfoSymbol("ForwardCompat", 8);
405 dumpThrowInfoSymbol("CatchableTypeArray", 12);
407 for (const auto &CTAPair : CTAs) {
408 StringRef CTAName = CTAPair.first;
409 const CatchableTypeArray &CTA = CTAPair.second;
411 outs() << CTAName << "[NumEntries]: " << CTA.NumEntries << '\n';
414 for (auto I = CTAEntries.lower_bound(std::make_pair(CTAName, 0)),
415 E = CTAEntries.upper_bound(std::make_pair(CTAName, UINT64_MAX));
417 outs() << CTAName << '[' << Idx++ << "]: " << I->second << '\n';
419 for (const auto &CTPair : CTs) {
420 StringRef CTName = CTPair.first;
421 const CatchableType &CT = CTPair.second;
422 auto dumpCatchableTypeFlag = [&](const char *Name, uint32_t Flag) {
423 outs() << CTName << "[Flags." << Name
424 << "]: " << (CT.Flags & Flag ? "true" : "false") << '\n';
426 outs() << CTName << "[Flags]: " << CT.Flags << '\n';
427 dumpCatchableTypeFlag("ScalarType", 1);
428 dumpCatchableTypeFlag("VirtualInheritance", 4);
429 outs() << CTName << "[TypeDescriptor]: " << CT.Symbols[0] << '\n';
430 outs() << CTName << "[NonVirtualBaseAdjustmentOffset]: "
431 << CT.NonVirtualBaseAdjustmentOffset << '\n';
433 << "[VirtualBasePointerOffset]: " << CT.VirtualBasePointerOffset
435 outs() << CTName << "[VirtualBaseAdjustmentOffset]: "
436 << CT.VirtualBaseAdjustmentOffset << '\n';
437 outs() << CTName << "[Size]: " << CT.Size << '\n';
439 << "[CopyCtor]: " << (CT.Symbols[1].empty() ? "null" : CT.Symbols[1])
442 for (const auto &VTTPair : VTTEntries) {
443 StringRef VTTName = VTTPair.first.first;
444 uint64_t VTTOffset = VTTPair.first.second;
445 StringRef VTTEntry = VTTPair.second;
446 outs() << VTTName << '[' << VTTOffset << "]: " << VTTEntry << '\n';
448 for (const auto &TIPair : TINames) {
449 StringRef TIName = TIPair.first;
450 outs() << TIName << ": " << TIPair.second << '\n';
452 auto VTableSymI = VTableSymEntries.begin();
453 auto VTableSymE = VTableSymEntries.end();
454 auto VTableDataI = VTableDataEntries.begin();
455 auto VTableDataE = VTableDataEntries.end();
457 bool SymDone = VTableSymI == VTableSymE;
458 bool DataDone = VTableDataI == VTableDataE;
459 if (SymDone && DataDone)
461 if (!SymDone && (DataDone || VTableSymI->first < VTableDataI->first)) {
462 StringRef VTableName = VTableSymI->first.first;
463 uint64_t Offset = VTableSymI->first.second;
464 StringRef VTableEntry = VTableSymI->second;
465 outs() << VTableName << '[' << Offset << "]: ";
466 outs() << VTableEntry;
471 if (!DataDone && (SymDone || VTableDataI->first < VTableSymI->first)) {
472 StringRef VTableName = VTableDataI->first.first;
473 uint64_t Offset = VTableDataI->first.second;
474 int64_t VTableEntry = VTableDataI->second;
475 outs() << VTableName << '[' << Offset << "]: ";
476 outs() << VTableEntry;
484 static void dumpArchive(const Archive *Arc) {
485 for (const Archive::Child &ArcC : Arc->children()) {
486 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcC.getAsBinary();
487 if (std::error_code EC = ChildOrErr.getError()) {
488 // Ignore non-object files.
489 if (EC != object_error::invalid_file_type)
490 reportError(Arc->getFileName(), EC.message());
494 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
497 reportError(Arc->getFileName(), cxxdump_error::unrecognized_file_format);
501 static void dumpInput(StringRef File) {
502 // Attempt to open the binary.
503 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
504 if (std::error_code EC = BinaryOrErr.getError()) {
505 reportError(File, EC);
508 Binary &Binary = *BinaryOrErr.get().getBinary();
510 if (Archive *Arc = dyn_cast<Archive>(&Binary))
512 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
515 reportError(File, cxxdump_error::unrecognized_file_format);
518 int main(int argc, const char *argv[]) {
519 sys::PrintStackTraceOnErrorSignal();
520 PrettyStackTraceProgram X(argc, argv);
523 // Initialize targets.
524 llvm::InitializeAllTargetInfos();
526 // Register the target printer for --version.
527 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
529 cl::ParseCommandLineOptions(argc, argv, "LLVM C++ ABI Data Dumper\n");
531 // Default to stdin if no filename is specified.
532 if (opts::InputFilenames.size() == 0)
533 opts::InputFilenames.push_back("-");
535 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),