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";
53 static void error(Error Err) {
55 logAllUnhandledErrors(std::move(Err), outs(), "Error reading file: ");
63 static void reportError(StringRef Input, StringRef Message) {
66 errs() << Input << ": " << Message << "\n";
71 static void reportError(StringRef Input, std::error_code EC) {
72 reportError(Input, EC.message());
75 static std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
77 static void collectRelocatedSymbols(const ObjectFile *Obj,
78 const SectionRef &Sec, uint64_t SecAddress,
79 uint64_t SymAddress, uint64_t SymSize,
80 StringRef *I, StringRef *E) {
81 uint64_t SymOffset = SymAddress - SecAddress;
82 uint64_t SymEnd = SymOffset + SymSize;
83 for (const SectionRef &SR : SectionRelocMap[Sec]) {
84 for (const object::RelocationRef &Reloc : SR.relocations()) {
87 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
88 if (RelocSymI == Obj->symbol_end())
90 Expected<StringRef> RelocSymName = RelocSymI->getName();
91 error(errorToErrorCode(RelocSymName.takeError()));
92 uint64_t Offset = Reloc.getOffset();
93 if (Offset >= SymOffset && Offset < SymEnd) {
101 static void collectRelocationOffsets(
102 const ObjectFile *Obj, const SectionRef &Sec, uint64_t SecAddress,
103 uint64_t SymAddress, uint64_t SymSize, StringRef SymName,
104 std::map<std::pair<StringRef, uint64_t>, StringRef> &Collection) {
105 uint64_t SymOffset = SymAddress - SecAddress;
106 uint64_t SymEnd = SymOffset + SymSize;
107 for (const SectionRef &SR : SectionRelocMap[Sec]) {
108 for (const object::RelocationRef &Reloc : SR.relocations()) {
109 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
110 if (RelocSymI == Obj->symbol_end())
112 Expected<StringRef> RelocSymName = RelocSymI->getName();
113 error(errorToErrorCode(RelocSymName.takeError()));
114 uint64_t Offset = Reloc.getOffset();
115 if (Offset >= SymOffset && Offset < SymEnd)
116 Collection[std::make_pair(SymName, Offset - SymOffset)] = *RelocSymName;
121 static void dumpCXXData(const ObjectFile *Obj) {
122 struct CompleteObjectLocator {
123 StringRef Symbols[2];
124 ArrayRef<little32_t> Data;
126 struct ClassHierarchyDescriptor {
127 StringRef Symbols[1];
128 ArrayRef<little32_t> Data;
130 struct BaseClassDescriptor {
131 StringRef Symbols[2];
132 ArrayRef<little32_t> Data;
134 struct TypeDescriptor {
135 StringRef Symbols[1];
137 StringRef MangledName;
142 struct CatchableTypeArray {
145 struct CatchableType {
147 uint32_t NonVirtualBaseAdjustmentOffset;
148 int32_t VirtualBasePointerOffset;
149 uint32_t VirtualBaseAdjustmentOffset;
151 StringRef Symbols[2];
153 std::map<std::pair<StringRef, uint64_t>, StringRef> VFTableEntries;
154 std::map<std::pair<StringRef, uint64_t>, StringRef> TIEntries;
155 std::map<std::pair<StringRef, uint64_t>, StringRef> CTAEntries;
156 std::map<StringRef, ArrayRef<little32_t>> VBTables;
157 std::map<StringRef, CompleteObjectLocator> COLs;
158 std::map<StringRef, ClassHierarchyDescriptor> CHDs;
159 std::map<std::pair<StringRef, uint64_t>, StringRef> BCAEntries;
160 std::map<StringRef, BaseClassDescriptor> BCDs;
161 std::map<StringRef, TypeDescriptor> TDs;
162 std::map<StringRef, ThrowInfo> TIs;
163 std::map<StringRef, CatchableTypeArray> CTAs;
164 std::map<StringRef, CatchableType> CTs;
166 std::map<std::pair<StringRef, uint64_t>, StringRef> VTableSymEntries;
167 std::map<std::pair<StringRef, uint64_t>, int64_t> VTableDataEntries;
168 std::map<std::pair<StringRef, uint64_t>, StringRef> VTTEntries;
169 std::map<StringRef, StringRef> TINames;
171 SectionRelocMap.clear();
172 for (const SectionRef &Section : Obj->sections()) {
173 section_iterator Sec2 = Section.getRelocatedSection();
174 if (Sec2 != Obj->section_end())
175 SectionRelocMap[*Sec2].push_back(Section);
178 uint8_t BytesInAddress = Obj->getBytesInAddress();
180 std::vector<std::pair<SymbolRef, uint64_t>> SymAddr =
181 object::computeSymbolSizes(*Obj);
183 for (auto &P : SymAddr) {
184 object::SymbolRef Sym = P.first;
185 uint64_t SymSize = P.second;
186 Expected<StringRef> SymNameOrErr = Sym.getName();
187 error(errorToErrorCode(SymNameOrErr.takeError()));
188 StringRef SymName = *SymNameOrErr;
189 Expected<object::section_iterator> SecIOrErr = Sym.getSection();
190 error(errorToErrorCode(SecIOrErr.takeError()));
191 object::section_iterator SecI = *SecIOrErr;
192 // Skip external symbols.
193 if (SecI == Obj->section_end())
195 const SectionRef &Sec = *SecI;
196 // Skip virtual or BSS sections.
197 if (Sec.isBSS() || Sec.isVirtual())
199 StringRef SecContents;
200 error(Sec.getContents(SecContents));
201 Expected<uint64_t> SymAddressOrErr = Sym.getAddress();
202 error(errorToErrorCode(SymAddressOrErr.takeError()));
203 uint64_t SymAddress = *SymAddressOrErr;
204 uint64_t SecAddress = Sec.getAddress();
205 uint64_t SecSize = Sec.getSize();
206 uint64_t SymOffset = SymAddress - SecAddress;
207 StringRef SymContents = SecContents.substr(SymOffset, SymSize);
209 // VFTables in the MS-ABI start with '??_7' and are contained within their
210 // own COMDAT section. We then determine the contents of the VFTable by
211 // looking at each relocation in the section.
212 if (SymName.startswith("??_7")) {
213 // Each relocation either names a virtual method or a thunk. We note the
214 // offset into the section and the symbol used for the relocation.
215 collectRelocationOffsets(Obj, Sec, SecAddress, SecAddress, SecSize,
216 SymName, VFTableEntries);
218 // VBTables in the MS-ABI start with '??_8' and are filled with 32-bit
219 // offsets of virtual bases.
220 else if (SymName.startswith("??_8")) {
221 ArrayRef<little32_t> VBTableData(
222 reinterpret_cast<const little32_t *>(SymContents.data()),
223 SymContents.size() / sizeof(little32_t));
224 VBTables[SymName] = VBTableData;
226 // Complete object locators in the MS-ABI start with '??_R4'
227 else if (SymName.startswith("??_R4")) {
228 CompleteObjectLocator COL;
229 COL.Data = makeArrayRef(
230 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
231 StringRef *I = std::begin(COL.Symbols), *E = std::end(COL.Symbols);
232 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
235 // Class hierarchy descriptors in the MS-ABI start with '??_R3'
236 else if (SymName.startswith("??_R3")) {
237 ClassHierarchyDescriptor CHD;
238 CHD.Data = makeArrayRef(
239 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
240 StringRef *I = std::begin(CHD.Symbols), *E = std::end(CHD.Symbols);
241 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
244 // Class hierarchy descriptors in the MS-ABI start with '??_R2'
245 else if (SymName.startswith("??_R2")) {
246 // Each relocation names a base class descriptor. We note the offset into
247 // the section and the symbol used for the relocation.
248 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
249 SymName, BCAEntries);
251 // Base class descriptors in the MS-ABI start with '??_R1'
252 else if (SymName.startswith("??_R1")) {
253 BaseClassDescriptor BCD;
254 BCD.Data = makeArrayRef(
255 reinterpret_cast<const little32_t *>(SymContents.data()) + 1, 5);
256 StringRef *I = std::begin(BCD.Symbols), *E = std::end(BCD.Symbols);
257 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
260 // Type descriptors in the MS-ABI start with '??_R0'
261 else if (SymName.startswith("??_R0")) {
262 const char *DataPtr = SymContents.drop_front(BytesInAddress).data();
264 if (BytesInAddress == 8)
265 TD.AlwaysZero = *reinterpret_cast<const little64_t *>(DataPtr);
267 TD.AlwaysZero = *reinterpret_cast<const little32_t *>(DataPtr);
268 TD.MangledName = SymContents.drop_front(BytesInAddress * 2);
269 StringRef *I = std::begin(TD.Symbols), *E = std::end(TD.Symbols);
270 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
273 // Throw descriptors in the MS-ABI start with '_TI'
274 else if (SymName.startswith("_TI") || SymName.startswith("__TI")) {
276 TI.Flags = *reinterpret_cast<const little32_t *>(SymContents.data());
277 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
281 // Catchable type arrays in the MS-ABI start with _CTA or __CTA.
282 else if (SymName.startswith("_CTA") || SymName.startswith("__CTA")) {
283 CatchableTypeArray CTA;
285 *reinterpret_cast<const little32_t *>(SymContents.data());
286 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
287 SymName, CTAEntries);
290 // Catchable types in the MS-ABI start with _CT or __CT.
291 else if (SymName.startswith("_CT") || SymName.startswith("__CT")) {
292 const little32_t *DataPtr =
293 reinterpret_cast<const little32_t *>(SymContents.data());
295 CT.Flags = DataPtr[0];
296 CT.NonVirtualBaseAdjustmentOffset = DataPtr[2];
297 CT.VirtualBasePointerOffset = DataPtr[3];
298 CT.VirtualBaseAdjustmentOffset = DataPtr[4];
299 CT.Size = DataPtr[5];
300 StringRef *I = std::begin(CT.Symbols), *E = std::end(CT.Symbols);
301 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
304 // Construction vtables in the Itanium ABI start with '_ZTT' or '__ZTT'.
305 else if (SymName.startswith("_ZTT") || SymName.startswith("__ZTT")) {
306 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
307 SymName, VTTEntries);
309 // Typeinfo names in the Itanium ABI start with '_ZTS' or '__ZTS'.
310 else if (SymName.startswith("_ZTS") || SymName.startswith("__ZTS")) {
311 TINames[SymName] = SymContents.slice(0, SymContents.find('\0'));
313 // Vtables in the Itanium ABI start with '_ZTV' or '__ZTV'.
314 else if (SymName.startswith("_ZTV") || SymName.startswith("__ZTV")) {
315 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
316 SymName, VTableSymEntries);
317 for (uint64_t SymOffI = 0; SymOffI < SymSize; SymOffI += BytesInAddress) {
318 auto Key = std::make_pair(SymName, SymOffI);
319 if (VTableSymEntries.count(Key))
321 const char *DataPtr =
322 SymContents.substr(SymOffI, BytesInAddress).data();
324 if (BytesInAddress == 8)
325 VData = *reinterpret_cast<const little64_t *>(DataPtr);
327 VData = *reinterpret_cast<const little32_t *>(DataPtr);
328 VTableDataEntries[Key] = VData;
331 // Typeinfo structures in the Itanium ABI start with '_ZTI' or '__ZTI'.
332 else if (SymName.startswith("_ZTI") || SymName.startswith("__ZTI")) {
333 // FIXME: Do something with these!
336 for (const auto &VFTableEntry : VFTableEntries) {
337 StringRef VFTableName = VFTableEntry.first.first;
338 uint64_t Offset = VFTableEntry.first.second;
339 StringRef SymName = VFTableEntry.second;
340 outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n';
342 for (const auto &VBTable : VBTables) {
343 StringRef VBTableName = VBTable.first;
345 for (little32_t Offset : VBTable.second) {
346 outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n';
347 Idx += sizeof(Offset);
350 for (const auto &COLPair : COLs) {
351 StringRef COLName = COLPair.first;
352 const CompleteObjectLocator &COL = COLPair.second;
353 outs() << COLName << "[IsImageRelative]: " << COL.Data[0] << '\n';
354 outs() << COLName << "[OffsetToTop]: " << COL.Data[1] << '\n';
355 outs() << COLName << "[VFPtrOffset]: " << COL.Data[2] << '\n';
356 outs() << COLName << "[TypeDescriptor]: " << COL.Symbols[0] << '\n';
357 outs() << COLName << "[ClassHierarchyDescriptor]: " << COL.Symbols[1]
360 for (const auto &CHDPair : CHDs) {
361 StringRef CHDName = CHDPair.first;
362 const ClassHierarchyDescriptor &CHD = CHDPair.second;
363 outs() << CHDName << "[AlwaysZero]: " << CHD.Data[0] << '\n';
364 outs() << CHDName << "[Flags]: " << CHD.Data[1] << '\n';
365 outs() << CHDName << "[NumClasses]: " << CHD.Data[2] << '\n';
366 outs() << CHDName << "[BaseClassArray]: " << CHD.Symbols[0] << '\n';
368 for (const auto &BCAEntry : BCAEntries) {
369 StringRef BCAName = BCAEntry.first.first;
370 uint64_t Offset = BCAEntry.first.second;
371 StringRef SymName = BCAEntry.second;
372 outs() << BCAName << '[' << Offset << "]: " << SymName << '\n';
374 for (const auto &BCDPair : BCDs) {
375 StringRef BCDName = BCDPair.first;
376 const BaseClassDescriptor &BCD = BCDPair.second;
377 outs() << BCDName << "[TypeDescriptor]: " << BCD.Symbols[0] << '\n';
378 outs() << BCDName << "[NumBases]: " << BCD.Data[0] << '\n';
379 outs() << BCDName << "[OffsetInVBase]: " << BCD.Data[1] << '\n';
380 outs() << BCDName << "[VBPtrOffset]: " << BCD.Data[2] << '\n';
381 outs() << BCDName << "[OffsetInVBTable]: " << BCD.Data[3] << '\n';
382 outs() << BCDName << "[Flags]: " << BCD.Data[4] << '\n';
383 outs() << BCDName << "[ClassHierarchyDescriptor]: " << BCD.Symbols[1]
386 for (const auto &TDPair : TDs) {
387 StringRef TDName = TDPair.first;
388 const TypeDescriptor &TD = TDPair.second;
389 outs() << TDName << "[VFPtr]: " << TD.Symbols[0] << '\n';
390 outs() << TDName << "[AlwaysZero]: " << TD.AlwaysZero << '\n';
391 outs() << TDName << "[MangledName]: ";
392 outs().write_escaped(TD.MangledName.rtrim(StringRef("\0", 1)),
393 /*UseHexEscapes=*/true)
396 for (const auto &TIPair : TIs) {
397 StringRef TIName = TIPair.first;
398 const ThrowInfo &TI = TIPair.second;
399 auto dumpThrowInfoFlag = [&](const char *Name, uint32_t Flag) {
400 outs() << TIName << "[Flags." << Name
401 << "]: " << (TI.Flags & Flag ? "true" : "false") << '\n';
403 auto dumpThrowInfoSymbol = [&](const char *Name, int Offset) {
404 outs() << TIName << '[' << Name << "]: ";
405 auto Entry = TIEntries.find(std::make_pair(TIName, Offset));
406 outs() << (Entry == TIEntries.end() ? "null" : Entry->second) << '\n';
408 outs() << TIName << "[Flags]: " << TI.Flags << '\n';
409 dumpThrowInfoFlag("Const", 1);
410 dumpThrowInfoFlag("Volatile", 2);
411 dumpThrowInfoSymbol("CleanupFn", 4);
412 dumpThrowInfoSymbol("ForwardCompat", 8);
413 dumpThrowInfoSymbol("CatchableTypeArray", 12);
415 for (const auto &CTAPair : CTAs) {
416 StringRef CTAName = CTAPair.first;
417 const CatchableTypeArray &CTA = CTAPair.second;
419 outs() << CTAName << "[NumEntries]: " << CTA.NumEntries << '\n';
422 for (auto I = CTAEntries.lower_bound(std::make_pair(CTAName, 0)),
423 E = CTAEntries.upper_bound(std::make_pair(CTAName, UINT64_MAX));
425 outs() << CTAName << '[' << Idx++ << "]: " << I->second << '\n';
427 for (const auto &CTPair : CTs) {
428 StringRef CTName = CTPair.first;
429 const CatchableType &CT = CTPair.second;
430 auto dumpCatchableTypeFlag = [&](const char *Name, uint32_t Flag) {
431 outs() << CTName << "[Flags." << Name
432 << "]: " << (CT.Flags & Flag ? "true" : "false") << '\n';
434 outs() << CTName << "[Flags]: " << CT.Flags << '\n';
435 dumpCatchableTypeFlag("ScalarType", 1);
436 dumpCatchableTypeFlag("VirtualInheritance", 4);
437 outs() << CTName << "[TypeDescriptor]: " << CT.Symbols[0] << '\n';
438 outs() << CTName << "[NonVirtualBaseAdjustmentOffset]: "
439 << CT.NonVirtualBaseAdjustmentOffset << '\n';
441 << "[VirtualBasePointerOffset]: " << CT.VirtualBasePointerOffset
443 outs() << CTName << "[VirtualBaseAdjustmentOffset]: "
444 << CT.VirtualBaseAdjustmentOffset << '\n';
445 outs() << CTName << "[Size]: " << CT.Size << '\n';
447 << "[CopyCtor]: " << (CT.Symbols[1].empty() ? "null" : CT.Symbols[1])
450 for (const auto &VTTPair : VTTEntries) {
451 StringRef VTTName = VTTPair.first.first;
452 uint64_t VTTOffset = VTTPair.first.second;
453 StringRef VTTEntry = VTTPair.second;
454 outs() << VTTName << '[' << VTTOffset << "]: " << VTTEntry << '\n';
456 for (const auto &TIPair : TINames) {
457 StringRef TIName = TIPair.first;
458 outs() << TIName << ": " << TIPair.second << '\n';
460 auto VTableSymI = VTableSymEntries.begin();
461 auto VTableSymE = VTableSymEntries.end();
462 auto VTableDataI = VTableDataEntries.begin();
463 auto VTableDataE = VTableDataEntries.end();
465 bool SymDone = VTableSymI == VTableSymE;
466 bool DataDone = VTableDataI == VTableDataE;
467 if (SymDone && DataDone)
469 if (!SymDone && (DataDone || VTableSymI->first < VTableDataI->first)) {
470 StringRef VTableName = VTableSymI->first.first;
471 uint64_t Offset = VTableSymI->first.second;
472 StringRef VTableEntry = VTableSymI->second;
473 outs() << VTableName << '[' << Offset << "]: ";
474 outs() << VTableEntry;
479 if (!DataDone && (SymDone || VTableDataI->first < VTableSymI->first)) {
480 StringRef VTableName = VTableDataI->first.first;
481 uint64_t Offset = VTableDataI->first.second;
482 int64_t VTableEntry = VTableDataI->second;
483 outs() << VTableName << '[' << Offset << "]: ";
484 outs() << VTableEntry;
492 static void dumpArchive(const Archive *Arc) {
493 Error Err = Error::success();
494 for (auto &ArcC : Arc->children(Err)) {
495 Expected<std::unique_ptr<Binary>> ChildOrErr = ArcC.getAsBinary();
497 // Ignore non-object files.
498 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) {
500 raw_string_ostream OS(Buf);
501 logAllUnhandledErrors(std::move(E), OS, "");
503 reportError(Arc->getFileName(), Buf);
505 consumeError(ChildOrErr.takeError());
509 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
512 reportError(Arc->getFileName(), cxxdump_error::unrecognized_file_format);
514 error(std::move(Err));
517 static void dumpInput(StringRef File) {
518 // Attempt to open the binary.
519 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
521 auto EC = errorToErrorCode(BinaryOrErr.takeError());
522 reportError(File, EC);
525 Binary &Binary = *BinaryOrErr.get().getBinary();
527 if (Archive *Arc = dyn_cast<Archive>(&Binary))
529 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
532 reportError(File, cxxdump_error::unrecognized_file_format);
535 int main(int argc, const char *argv[]) {
536 sys::PrintStackTraceOnErrorSignal(argv[0]);
537 PrettyStackTraceProgram X(argc, argv);
540 // Initialize targets.
541 llvm::InitializeAllTargetInfos();
543 // Register the target printer for --version.
544 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
546 cl::ParseCommandLineOptions(argc, argv, "LLVM C++ ABI Data Dumper\n");
548 // Default to stdin if no filename is specified.
549 if (opts::InputFilenames.size() == 0)
550 opts::InputFilenames.push_back("-");
552 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),