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