a21acae02c3a5f035a11ff3bff91d69519cdebf7
[oota-llvm.git] / tools / llvm-vtabledump / llvm-vtabledump.cpp
1 //===- llvm-vtabledump.cpp - Dump vtables in an Object File -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Dumps VTables resident in object files and archives.  Note, it currently only
11 // supports MS-ABI style object files.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm-vtabledump.h"
16 #include "Error.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"
28 #include <map>
29 #include <string>
30 #include <system_error>
31
32 using namespace llvm;
33 using namespace llvm::object;
34 using namespace llvm::support;
35
36 namespace opts {
37 cl::list<std::string> InputFilenames(cl::Positional,
38                                      cl::desc("<input object files>"),
39                                      cl::ZeroOrMore);
40 } // namespace opts
41
42 static int ReturnValue = EXIT_SUCCESS;
43
44 namespace llvm {
45
46 bool error(std::error_code EC) {
47   if (!EC)
48     return false;
49
50   ReturnValue = EXIT_FAILURE;
51   outs() << "\nError reading file: " << EC.message() << ".\n";
52   outs().flush();
53   return true;
54 }
55
56 } // namespace llvm
57
58 static void reportError(StringRef Input, StringRef Message) {
59   if (Input == "-")
60     Input = "<stdin>";
61
62   errs() << Input << ": " << Message << "\n";
63   errs().flush();
64   ReturnValue = EXIT_FAILURE;
65 }
66
67 static void reportError(StringRef Input, std::error_code EC) {
68   reportError(Input, EC.message());
69 }
70
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;
75   if (!MappingDone) {
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);
80     }
81     MappingDone = true;
82   }
83   return SectionRelocMap[Sec];
84 }
85
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()) {
94       if (I == E)
95         break;
96       const object::symbol_iterator RelocSymI = Reloc.getSymbol();
97       if (RelocSymI == Obj->symbol_end())
98         continue;
99       StringRef RelocSymName;
100       if (error(RelocSymI->getName(RelocSymName)))
101         return true;
102       uint64_t Offset;
103       if (error(Reloc.getOffset(Offset)))
104         return true;
105       if (Offset >= SymOffset && Offset < SymEnd) {
106         *I = RelocSymName;
107         ++I;
108       }
109     }
110   }
111   return false;
112 }
113
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())
124         continue;
125       StringRef RelocSymName;
126       if (error(RelocSymI->getName(RelocSymName)))
127         return true;
128       uint64_t Offset;
129       if (error(Reloc.getOffset(Offset)))
130         return true;
131       if (Offset >= SymOffset && Offset < SymEnd)
132         Collection[std::make_pair(SymName, Offset - SymOffset)] = RelocSymName;
133     }
134   }
135   return false;
136 }
137
138 static void dumpVTables(const ObjectFile *Obj) {
139   struct CompleteObjectLocator {
140     StringRef Symbols[2];
141     ArrayRef<little32_t> Data;
142   };
143   struct ClassHierarchyDescriptor {
144     StringRef Symbols[1];
145     ArrayRef<little32_t> Data;
146   };
147   struct BaseClassDescriptor {
148     StringRef Symbols[2];
149     ArrayRef<little32_t> Data;
150   };
151   struct TypeDescriptor {
152     StringRef Symbols[1];
153     uint64_t AlwaysZero;
154     StringRef MangledName;
155   };
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;
163
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;
168
169   uint8_t BytesInAddress = Obj->getBytesInAddress();
170
171   for (const object::SymbolRef &Sym : Obj->symbols()) {
172     StringRef SymName;
173     if (error(Sym.getName(SymName)))
174       return;
175     object::section_iterator SecI(Obj->section_begin());
176     if (error(Sym.getSection(SecI)))
177       return;
178     // Skip external symbols.
179     if (SecI == Obj->section_end())
180       continue;
181     const SectionRef &Sec = *SecI;
182     // Skip virtual or BSS sections.
183     if (Sec.isBSS() || Sec.isVirtual())
184       continue;
185     StringRef SecContents;
186     if (error(Sec.getContents(SecContents)))
187       return;
188     uint64_t SymAddress, SymSize;
189     if (error(Sym.getAddress(SymAddress)) || error(Sym.getSize(SymSize)))
190       return;
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);
195
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);
204     }
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;
212     }
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,
220                                   E))
221         return;
222       COLs[SymName] = COL;
223     }
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,
231                                   E))
232         return;
233       CHDs[SymName] = CHD;
234     }
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);
241     }
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,
249                                   E))
250         return;
251       BCDs[SymName] = BCD;
252     }
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();
256       TypeDescriptor TD;
257       if (BytesInAddress == 8)
258         TD.AlwaysZero = *reinterpret_cast<const little64_t *>(DataPtr);
259       else
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,
264                                   E))
265         return;
266       TDs[SymName] = TD;
267     }
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);
272     }
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'));
276     }
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))
284           continue;
285         const char *DataPtr = SymContents.substr(SymOffI, BytesInAddress).data();
286         int64_t VData;
287         if (BytesInAddress == 8)
288           VData = *reinterpret_cast<const little64_t *>(DataPtr);
289         else
290           VData = *reinterpret_cast<const little32_t *>(DataPtr);
291         VTableDataEntries[Key] = VData;
292       }
293     }
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!
297     }
298   }
299   for (const std::pair<std::pair<StringRef, uint64_t>, StringRef> &VFTableEntry :
300        VFTableEntries) {
301     StringRef VFTableName = VFTableEntry.first.first;
302     uint64_t Offset = VFTableEntry.first.second;
303     StringRef SymName = VFTableEntry.second;
304     outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n';
305   }
306   for (const std::pair<StringRef, ArrayRef<little32_t>> &VBTable : VBTables) {
307     StringRef VBTableName = VBTable.first;
308     uint32_t Idx = 0;
309     for (little32_t Offset : VBTable.second) {
310       outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n';
311       Idx += sizeof(Offset);
312     }
313   }
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';
322   }
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';
330   }
331   for (const std::pair<std::pair<StringRef, uint64_t>, StringRef> &BCAEntry :
332        BCAEntries) {
333     StringRef BCAName = BCAEntry.first.first;
334     uint64_t Offset = BCAEntry.first.second;
335     StringRef SymName = BCAEntry.second;
336     outs() << BCAName << '[' << Offset << "]: " << SymName << '\n';
337   }
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';
348   }
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)
357         << '\n';
358   }
359   for (const std::pair<std::pair<StringRef, uint64_t>, StringRef> &VTTPair :
360        VTTEntries) {
361     StringRef VTTName = VTTPair.first.first;
362     uint64_t VTTOffset = VTTPair.first.second;
363     StringRef VTTEntry = VTTPair.second;
364     outs() << VTTName << '[' << VTTOffset << "]: " << VTTEntry << '\n';
365   }
366   for (const std::pair<StringRef, StringRef> &TIPair : TINames) {
367     StringRef TIName = TIPair.first;
368     outs() << TIName << ": " << TIPair.second << '\n';
369   }
370   auto VTableSymI = VTableSymEntries.begin();
371   auto VTableSymE = VTableSymEntries.end();
372   auto VTableDataI = VTableDataEntries.begin();
373   auto VTableDataE = VTableDataEntries.end();
374   for (;;) {
375     bool SymDone = VTableSymI == VTableSymE;
376     bool DataDone = VTableDataI == VTableDataE;
377     if (SymDone && DataDone)
378       break;
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;
385       outs() << '\n';
386       ++VTableSymI;
387       continue;
388     }
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;
395       outs() << '\n';
396       ++VTableDataI;
397       continue;
398     }
399   }
400 }
401
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());
409       continue;
410     }
411
412     if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
413       dumpVTables(Obj);
414     else
415       reportError(Arc->getFileName(),
416                   vtabledump_error::unrecognized_file_format);
417   }
418 }
419
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);
424     return;
425   }
426
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);
431     return;
432   }
433   Binary &Binary = *BinaryOrErr.get().getBinary();
434
435   if (Archive *Arc = dyn_cast<Archive>(&Binary))
436     dumpArchive(Arc);
437   else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
438     dumpVTables(Obj);
439   else
440     reportError(File, vtabledump_error::unrecognized_file_format);
441 }
442
443 int main(int argc, const char *argv[]) {
444   sys::PrintStackTraceOnErrorSignal();
445   PrettyStackTraceProgram X(argc, argv);
446   llvm_shutdown_obj Y;
447
448   // Initialize targets.
449   llvm::InitializeAllTargetInfos();
450
451   // Register the target printer for --version.
452   cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
453
454   cl::ParseCommandLineOptions(argc, argv, "LLVM VTable Dumper\n");
455
456   // Default to stdin if no filename is specified.
457   if (opts::InputFilenames.size() == 0)
458     opts::InputFilenames.push_back("-");
459
460   std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
461                 dumpInput);
462
463   return ReturnValue;
464 }