1 //===- llvm-vtabledump.cpp - Dump vtables 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 VTables resident in object files and archives. Note, it currently only
11 // supports MS-ABI style object files.
13 //===----------------------------------------------------------------------===//
15 #include "llvm-vtabledump.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/Object/Archive.h"
19 #include "llvm/Object/ObjectFile.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"
30 #include <system_error>
33 using namespace llvm::object;
34 using namespace llvm::support;
37 cl::list<std::string> InputFilenames(cl::Positional,
38 cl::desc("<input object files>"),
42 static int ReturnValue = EXIT_SUCCESS;
46 bool error(std::error_code EC) {
50 ReturnValue = EXIT_FAILURE;
51 outs() << "\nError reading file: " << EC.message() << ".\n";
58 static void reportError(StringRef Input, StringRef Message) {
62 errs() << Input << ": " << Message << "\n";
64 ReturnValue = EXIT_FAILURE;
67 static void reportError(StringRef Input, std::error_code EC) {
68 reportError(Input, EC.message());
71 static SmallVectorImpl<SectionRef> &getRelocSections(const ObjectFile *Obj,
72 const SectionRef &Sec) {
73 static bool MappingDone = false;
74 static std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
76 for (const SectionRef &Section : Obj->sections()) {
77 section_iterator Sec2 = Section.getRelocatedSection();
78 if (Sec2 != Obj->section_end())
79 SectionRelocMap[*Sec2].push_back(Section);
83 return SectionRelocMap[Sec];
86 static bool collectRelocatedSymbols(const ObjectFile *Obj,
87 const SectionRef &Sec, uint64_t SecAddress,
88 uint64_t SymAddress, uint64_t SymSize,
89 StringRef *I, StringRef *E) {
90 uint64_t SymOffset = SymAddress - SecAddress;
91 uint64_t SymEnd = SymOffset + SymSize;
92 for (const SectionRef &SR : getRelocSections(Obj, Sec)) {
93 for (const object::RelocationRef &Reloc : SR.relocations()) {
96 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
97 if (RelocSymI == Obj->symbol_end())
99 StringRef RelocSymName;
100 if (error(RelocSymI->getName(RelocSymName)))
103 if (error(Reloc.getOffset(Offset)))
105 if (Offset >= SymOffset && Offset < SymEnd) {
114 static bool collectRelocationOffsets(
115 const ObjectFile *Obj, const SectionRef &Sec, uint64_t SecAddress,
116 uint64_t SymAddress, uint64_t SymSize, StringRef SymName,
117 std::map<std::pair<StringRef, uint64_t>, StringRef> &Collection) {
118 uint64_t SymOffset = SymAddress - SecAddress;
119 uint64_t SymEnd = SymOffset + SymSize;
120 for (const SectionRef &SR : getRelocSections(Obj, Sec)) {
121 for (const object::RelocationRef &Reloc : SR.relocations()) {
122 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
123 if (RelocSymI == Obj->symbol_end())
125 StringRef RelocSymName;
126 if (error(RelocSymI->getName(RelocSymName)))
129 if (error(Reloc.getOffset(Offset)))
131 if (Offset >= SymOffset && Offset < SymEnd)
132 Collection[std::make_pair(SymName, Offset - SymOffset)] = RelocSymName;
138 static void dumpVTables(const ObjectFile *Obj) {
139 struct CompleteObjectLocator {
140 StringRef Symbols[2];
141 ArrayRef<little32_t> Data;
143 struct ClassHierarchyDescriptor {
144 StringRef Symbols[1];
145 ArrayRef<little32_t> Data;
147 struct BaseClassDescriptor {
148 StringRef Symbols[2];
149 ArrayRef<little32_t> Data;
151 struct TypeDescriptor {
152 StringRef Symbols[1];
154 StringRef MangledName;
156 std::map<std::pair<StringRef, uint64_t>, StringRef> VFTableEntries;
157 std::map<StringRef, ArrayRef<little32_t>> VBTables;
158 std::map<StringRef, CompleteObjectLocator> COLs;
159 std::map<StringRef, ClassHierarchyDescriptor> CHDs;
160 std::map<std::pair<StringRef, uint64_t>, StringRef> BCAEntries;
161 std::map<StringRef, BaseClassDescriptor> BCDs;
162 std::map<StringRef, TypeDescriptor> TDs;
164 std::map<std::pair<StringRef, uint64_t>, StringRef> VTableSymEntries;
165 std::map<std::pair<StringRef, uint64_t>, int64_t> VTableDataEntries;
166 std::map<std::pair<StringRef, uint64_t>, StringRef> VTTEntries;
167 std::map<StringRef, StringRef> TINames;
169 uint8_t BytesInAddress = Obj->getBytesInAddress();
171 for (const object::SymbolRef &Sym : Obj->symbols()) {
173 if (error(Sym.getName(SymName)))
175 object::section_iterator SecI(Obj->section_begin());
176 if (error(Sym.getSection(SecI)))
178 // Skip external symbols.
179 if (SecI == Obj->section_end())
181 const SectionRef &Sec = *SecI;
182 // Skip virtual or BSS sections.
183 if (Sec.isBSS() || Sec.isVirtual())
185 StringRef SecContents;
186 if (error(Sec.getContents(SecContents)))
188 uint64_t SymAddress, SymSize;
189 if (error(Sym.getAddress(SymAddress)) || error(Sym.getSize(SymSize)))
191 uint64_t SecAddress = Sec.getAddress();
192 uint64_t SecSize = Sec.getSize();
193 uint64_t SymOffset = SymAddress - SecAddress;
194 StringRef SymContents = SecContents.substr(SymOffset, SymSize);
196 // VFTables in the MS-ABI start with '??_7' and are contained within their
197 // own COMDAT section. We then determine the contents of the VFTable by
198 // looking at each relocation in the section.
199 if (SymName.startswith("??_7")) {
200 // Each relocation either names a virtual method or a thunk. We note the
201 // offset into the section and the symbol used for the relocation.
202 collectRelocationOffsets(Obj, Sec, SecAddress, SecAddress, SecSize,
203 SymName, VFTableEntries);
205 // VBTables in the MS-ABI start with '??_8' and are filled with 32-bit
206 // offsets of virtual bases.
207 else if (SymName.startswith("??_8")) {
208 ArrayRef<little32_t> VBTableData(
209 reinterpret_cast<const little32_t *>(SymContents.data()),
210 SymContents.size() / sizeof(little32_t));
211 VBTables[SymName] = VBTableData;
213 // Complete object locators in the MS-ABI start with '??_R4'
214 else if (SymName.startswith("??_R4")) {
215 CompleteObjectLocator COL;
216 COL.Data = ArrayRef<little32_t>(
217 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
218 StringRef *I = std::begin(COL.Symbols), *E = std::end(COL.Symbols);
219 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
224 // Class hierarchy descriptors in the MS-ABI start with '??_R3'
225 else if (SymName.startswith("??_R3")) {
226 ClassHierarchyDescriptor CHD;
227 CHD.Data = ArrayRef<little32_t>(
228 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
229 StringRef *I = std::begin(CHD.Symbols), *E = std::end(CHD.Symbols);
230 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
235 // Class hierarchy descriptors in the MS-ABI start with '??_R2'
236 else if (SymName.startswith("??_R2")) {
237 // Each relocation names a base class descriptor. We note the offset into
238 // the section and the symbol used for the relocation.
239 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
240 SymName, BCAEntries);
242 // Base class descriptors in the MS-ABI start with '??_R1'
243 else if (SymName.startswith("??_R1")) {
244 BaseClassDescriptor BCD;
245 BCD.Data = ArrayRef<little32_t>(
246 reinterpret_cast<const little32_t *>(SymContents.data()) + 1, 5);
247 StringRef *I = std::begin(BCD.Symbols), *E = std::end(BCD.Symbols);
248 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
253 // Type descriptors in the MS-ABI start with '??_R0'
254 else if (SymName.startswith("??_R0")) {
255 const char *DataPtr = SymContents.drop_front(BytesInAddress).data();
257 if (BytesInAddress == 8)
258 TD.AlwaysZero = *reinterpret_cast<const little64_t *>(DataPtr);
260 TD.AlwaysZero = *reinterpret_cast<const little32_t *>(DataPtr);
261 TD.MangledName = SymContents.drop_front(BytesInAddress * 2);
262 StringRef *I = std::begin(TD.Symbols), *E = std::end(TD.Symbols);
263 if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I,
268 // Construction vtables in the Itanium ABI start with '_ZTT' or '__ZTT'.
269 else if (SymName.startswith("_ZTT") || SymName.startswith("__ZTT")) {
270 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
271 SymName, VTTEntries);
273 // Typeinfo names in the Itanium ABI start with '_ZTS' or '__ZTS'.
274 else if (SymName.startswith("_ZTS") || SymName.startswith("__ZTS")) {
275 TINames[SymName] = SymContents.slice(0, SymContents.find('\0'));
277 // Vtables in the Itanium ABI start with '_ZTV' or '__ZTV'.
278 else if (SymName.startswith("_ZTV") || SymName.startswith("__ZTV")) {
279 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
280 SymName, VTableSymEntries);
281 for (uint64_t SymOffI = 0; SymOffI < SymSize; SymOffI += BytesInAddress) {
282 auto Key = std::make_pair(SymName, SymOffI);
283 if (VTableSymEntries.count(Key))
285 const char *DataPtr = SymContents.substr(SymOffI, BytesInAddress).data();
287 if (BytesInAddress == 8)
288 VData = *reinterpret_cast<const little64_t *>(DataPtr);
290 VData = *reinterpret_cast<const little32_t *>(DataPtr);
291 VTableDataEntries[Key] = VData;
294 // Typeinfo structures in the Itanium ABI start with '_ZTI' or '__ZTI'.
295 else if (SymName.startswith("_ZTI") || SymName.startswith("__ZTI")) {
296 // FIXME: Do something with these!
299 for (const std::pair<std::pair<StringRef, uint64_t>, StringRef> &VFTableEntry :
301 StringRef VFTableName = VFTableEntry.first.first;
302 uint64_t Offset = VFTableEntry.first.second;
303 StringRef SymName = VFTableEntry.second;
304 outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n';
306 for (const std::pair<StringRef, ArrayRef<little32_t>> &VBTable : VBTables) {
307 StringRef VBTableName = VBTable.first;
309 for (little32_t Offset : VBTable.second) {
310 outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n';
311 Idx += sizeof(Offset);
314 for (const std::pair<StringRef, CompleteObjectLocator> &COLPair : COLs) {
315 StringRef COLName = COLPair.first;
316 const CompleteObjectLocator &COL = COLPair.second;
317 outs() << COLName << "[IsImageRelative]: " << COL.Data[0] << '\n';
318 outs() << COLName << "[OffsetToTop]: " << COL.Data[1] << '\n';
319 outs() << COLName << "[VFPtrOffset]: " << COL.Data[2] << '\n';
320 outs() << COLName << "[TypeDescriptor]: " << COL.Symbols[0] << '\n';
321 outs() << COLName << "[ClassHierarchyDescriptor]: " << COL.Symbols[1] << '\n';
323 for (const std::pair<StringRef, ClassHierarchyDescriptor> &CHDPair : CHDs) {
324 StringRef CHDName = CHDPair.first;
325 const ClassHierarchyDescriptor &CHD = CHDPair.second;
326 outs() << CHDName << "[AlwaysZero]: " << CHD.Data[0] << '\n';
327 outs() << CHDName << "[Flags]: " << CHD.Data[1] << '\n';
328 outs() << CHDName << "[NumClasses]: " << CHD.Data[2] << '\n';
329 outs() << CHDName << "[BaseClassArray]: " << CHD.Symbols[0] << '\n';
331 for (const std::pair<std::pair<StringRef, uint64_t>, StringRef> &BCAEntry :
333 StringRef BCAName = BCAEntry.first.first;
334 uint64_t Offset = BCAEntry.first.second;
335 StringRef SymName = BCAEntry.second;
336 outs() << BCAName << '[' << Offset << "]: " << SymName << '\n';
338 for (const std::pair<StringRef, BaseClassDescriptor> &BCDPair : BCDs) {
339 StringRef BCDName = BCDPair.first;
340 const BaseClassDescriptor &BCD = BCDPair.second;
341 outs() << BCDName << "[TypeDescriptor]: " << BCD.Symbols[0] << '\n';
342 outs() << BCDName << "[NumBases]: " << BCD.Data[0] << '\n';
343 outs() << BCDName << "[OffsetInVBase]: " << BCD.Data[1] << '\n';
344 outs() << BCDName << "[VBPtrOffset]: " << BCD.Data[2] << '\n';
345 outs() << BCDName << "[OffsetInVBTable]: " << BCD.Data[3] << '\n';
346 outs() << BCDName << "[Flags]: " << BCD.Data[4] << '\n';
347 outs() << BCDName << "[ClassHierarchyDescriptor]: " << BCD.Symbols[1] << '\n';
349 for (const std::pair<StringRef, TypeDescriptor> &TDPair : TDs) {
350 StringRef TDName = TDPair.first;
351 const TypeDescriptor &TD = TDPair.second;
352 outs() << TDName << "[VFPtr]: " << TD.Symbols[0] << '\n';
353 outs() << TDName << "[AlwaysZero]: " << TD.AlwaysZero << '\n';
354 outs() << TDName << "[MangledName]: ";
355 outs().write_escaped(TD.MangledName.rtrim(StringRef("\0", 1)),
356 /*UseHexEscapes=*/true)
359 for (const std::pair<std::pair<StringRef, uint64_t>, StringRef> &VTTPair :
361 StringRef VTTName = VTTPair.first.first;
362 uint64_t VTTOffset = VTTPair.first.second;
363 StringRef VTTEntry = VTTPair.second;
364 outs() << VTTName << '[' << VTTOffset << "]: " << VTTEntry << '\n';
366 for (const std::pair<StringRef, StringRef> &TIPair : TINames) {
367 StringRef TIName = TIPair.first;
368 outs() << TIName << ": " << TIPair.second << '\n';
370 auto VTableSymI = VTableSymEntries.begin();
371 auto VTableSymE = VTableSymEntries.end();
372 auto VTableDataI = VTableDataEntries.begin();
373 auto VTableDataE = VTableDataEntries.end();
375 bool SymDone = VTableSymI == VTableSymE;
376 bool DataDone = VTableDataI == VTableDataE;
377 if (SymDone && DataDone)
379 if (!SymDone && (DataDone || VTableSymI->first < VTableDataI->first)) {
380 StringRef VTableName = VTableSymI->first.first;
381 uint64_t Offset = VTableSymI->first.second;
382 StringRef VTableEntry = VTableSymI->second;
383 outs() << VTableName << '[' << Offset << "]: ";
384 outs() << VTableEntry;
389 if (!DataDone && (SymDone || VTableDataI->first < VTableSymI->first)) {
390 StringRef VTableName = VTableDataI->first.first;
391 uint64_t Offset = VTableDataI->first.second;
392 int64_t VTableEntry = VTableDataI->second;
393 outs() << VTableName << '[' << Offset << "]: ";
394 outs() << VTableEntry;
402 static void dumpArchive(const Archive *Arc) {
403 for (const Archive::Child &ArcC : Arc->children()) {
404 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcC.getAsBinary();
405 if (std::error_code EC = ChildOrErr.getError()) {
406 // Ignore non-object files.
407 if (EC != object_error::invalid_file_type)
408 reportError(Arc->getFileName(), EC.message());
412 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
415 reportError(Arc->getFileName(),
416 vtabledump_error::unrecognized_file_format);
420 static void dumpInput(StringRef File) {
421 // If file isn't stdin, check that it exists.
422 if (File != "-" && !sys::fs::exists(File)) {
423 reportError(File, vtabledump_error::file_not_found);
427 // Attempt to open the binary.
428 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
429 if (std::error_code EC = BinaryOrErr.getError()) {
430 reportError(File, EC);
433 Binary &Binary = *BinaryOrErr.get().getBinary();
435 if (Archive *Arc = dyn_cast<Archive>(&Binary))
437 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
440 reportError(File, vtabledump_error::unrecognized_file_format);
443 int main(int argc, const char *argv[]) {
444 sys::PrintStackTraceOnErrorSignal();
445 PrettyStackTraceProgram X(argc, argv);
448 // Initialize targets.
449 llvm::InitializeAllTargetInfos();
451 // Register the target printer for --version.
452 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
454 cl::ParseCommandLineOptions(argc, argv, "LLVM VTable Dumper\n");
456 // Default to stdin if no filename is specified.
457 if (opts::InputFilenames.size() == 0)
458 opts::InputFilenames.push_back("-");
460 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),