blob: c005d7f95a9384361c4d4f6b9c2b0866e0cfcee5 [file] [log] [blame]
David Majnemer72ab1a52014-07-24 23:14:40 +00001//===- 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"
David Majnemer72ab1a52014-07-24 23:14:40 +000018#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
32using namespace llvm;
33using namespace llvm::object;
34using namespace llvm::support;
35
36namespace opts {
37cl::list<std::string> InputFilenames(cl::Positional,
38 cl::desc("<input object files>"),
39 cl::ZeroOrMore);
40} // namespace opts
41
42static int ReturnValue = EXIT_SUCCESS;
43
44namespace llvm {
45
46bool 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
58static 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
67static void reportError(StringRef Input, std::error_code EC) {
68 reportError(Input, EC.message());
69}
70
David Majnemer1ac52eb2014-09-26 04:21:51 +000071static bool collectRelocatedSymbols(const ObjectFile *Obj,
72 object::section_iterator SecI, StringRef *I,
73 StringRef *E) {
74 for (const object::RelocationRef &Reloc : SecI->relocations()) {
75 if (I == E)
76 break;
77 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
78 if (RelocSymI == Obj->symbol_end())
79 continue;
80 StringRef RelocSymName;
81 if (error(RelocSymI->getName(RelocSymName)))
82 return true;
83 *I = RelocSymName;
84 ++I;
85 }
86 return false;
87}
88
89static bool collectRelocationOffsets(
90 const ObjectFile *Obj, object::section_iterator SecI, StringRef SymName,
91 std::map<std::pair<StringRef, uint64_t>, StringRef> &Collection) {
92 for (const object::RelocationRef &Reloc : SecI->relocations()) {
93 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
94 if (RelocSymI == Obj->symbol_end())
95 continue;
96 StringRef RelocSymName;
97 if (error(RelocSymI->getName(RelocSymName)))
98 return true;
99 uint64_t Offset;
100 if (error(Reloc.getOffset(Offset)))
101 return true;
102 Collection[std::make_pair(SymName, Offset)] = RelocSymName;
103 }
104 return false;
105}
106
David Majnemer72ab1a52014-07-24 23:14:40 +0000107static void dumpVTables(const ObjectFile *Obj) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000108 struct CompleteObjectLocator {
109 StringRef Symbols[2];
David Majnemer6887a252014-09-26 08:01:23 +0000110 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000111 };
112 struct ClassHierarchyDescriptor {
113 StringRef Symbols[1];
David Majnemer6887a252014-09-26 08:01:23 +0000114 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000115 };
116 struct BaseClassDescriptor {
117 StringRef Symbols[2];
David Majnemer6887a252014-09-26 08:01:23 +0000118 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000119 };
120 struct TypeDescriptor {
121 StringRef Symbols[1];
David Majnemer6887a252014-09-26 08:01:23 +0000122 uint64_t AlwaysZero;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000123 StringRef MangledName;
124 };
David Majnemer72ab1a52014-07-24 23:14:40 +0000125 std::map<std::pair<StringRef, uint64_t>, StringRef> VFTableEntries;
David Majnemer6887a252014-09-26 08:01:23 +0000126 std::map<StringRef, ArrayRef<little32_t>> VBTables;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000127 std::map<StringRef, CompleteObjectLocator> COLs;
128 std::map<StringRef, ClassHierarchyDescriptor> CHDs;
129 std::map<std::pair<StringRef, uint64_t>, StringRef> BCAEntries;
130 std::map<StringRef, BaseClassDescriptor> BCDs;
131 std::map<StringRef, TypeDescriptor> TDs;
David Majnemer72ab1a52014-07-24 23:14:40 +0000132 for (const object::SymbolRef &Sym : Obj->symbols()) {
133 StringRef SymName;
134 if (error(Sym.getName(SymName)))
135 return;
David Majnemer601327c2014-09-26 22:32:19 +0000136 object::section_iterator SecI(Obj->section_begin());
137 if (error(Sym.getSection(SecI)))
138 return;
139 // Skip external symbols.
140 if (SecI == Obj->section_end())
141 continue;
Rafael Espindola80291272014-10-08 15:28:58 +0000142 bool IsBSS = SecI->isBSS();
143 bool IsVirtual = SecI->isVirtual();
David Majnemer601327c2014-09-26 22:32:19 +0000144 // Skip virtual or BSS sections.
145 if (IsBSS || IsVirtual)
146 continue;
147 StringRef SecContents;
148 if (error(SecI->getContents(SecContents)))
149 return;
David Majnemer72ab1a52014-07-24 23:14:40 +0000150 // VFTables in the MS-ABI start with '??_7' and are contained within their
151 // own COMDAT section. We then determine the contents of the VFTable by
152 // looking at each relocation in the section.
153 if (SymName.startswith("??_7")) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000154 // Each relocation either names a virtual method or a thunk. We note the
155 // offset into the section and the symbol used for the relocation.
David Majnemer1ac52eb2014-09-26 04:21:51 +0000156 collectRelocationOffsets(Obj, SecI, SymName, VFTableEntries);
David Majnemer72ab1a52014-07-24 23:14:40 +0000157 }
158 // VBTables in the MS-ABI start with '??_8' and are filled with 32-bit
159 // offsets of virtual bases.
160 else if (SymName.startswith("??_8")) {
David Majnemer6887a252014-09-26 08:01:23 +0000161 ArrayRef<little32_t> VBTableData(
162 reinterpret_cast<const little32_t *>(SecContents.data()),
163 SecContents.size() / sizeof(little32_t));
David Majnemer72ab1a52014-07-24 23:14:40 +0000164 VBTables[SymName] = VBTableData;
165 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000166 // Complete object locators in the MS-ABI start with '??_R4'
167 else if (SymName.startswith("??_R4")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000168 CompleteObjectLocator COL;
David Majnemer6887a252014-09-26 08:01:23 +0000169 COL.Data = ArrayRef<little32_t>(
170 reinterpret_cast<const little32_t *>(SecContents.data()), 3);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000171 StringRef *I = std::begin(COL.Symbols), *E = std::end(COL.Symbols);
172 if (collectRelocatedSymbols(Obj, SecI, I, E))
173 return;
174 COLs[SymName] = COL;
175 }
176 // Class hierarchy descriptors in the MS-ABI start with '??_R3'
177 else if (SymName.startswith("??_R3")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000178 ClassHierarchyDescriptor CHD;
David Majnemer6887a252014-09-26 08:01:23 +0000179 CHD.Data = ArrayRef<little32_t>(
180 reinterpret_cast<const little32_t *>(SecContents.data()), 3);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000181 StringRef *I = std::begin(CHD.Symbols), *E = std::end(CHD.Symbols);
182 if (collectRelocatedSymbols(Obj, SecI, I, E))
183 return;
184 CHDs[SymName] = CHD;
185 }
186 // Class hierarchy descriptors in the MS-ABI start with '??_R2'
187 else if (SymName.startswith("??_R2")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000188 // Each relocation names a base class descriptor. We note the offset into
189 // the section and the symbol used for the relocation.
190 collectRelocationOffsets(Obj, SecI, SymName, BCAEntries);
191 }
192 // Base class descriptors in the MS-ABI start with '??_R1'
193 else if (SymName.startswith("??_R1")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000194 BaseClassDescriptor BCD;
David Majnemer6887a252014-09-26 08:01:23 +0000195 BCD.Data = ArrayRef<little32_t>(
196 reinterpret_cast<const little32_t *>(SecContents.data()) + 1,
David Majnemer1ac52eb2014-09-26 04:21:51 +0000197 5);
198 StringRef *I = std::begin(BCD.Symbols), *E = std::end(BCD.Symbols);
199 if (collectRelocatedSymbols(Obj, SecI, I, E))
200 return;
201 BCDs[SymName] = BCD;
202 }
203 // Type descriptors in the MS-ABI start with '??_R0'
204 else if (SymName.startswith("??_R0")) {
David Majnemer6887a252014-09-26 08:01:23 +0000205 uint8_t BytesInAddress = Obj->getBytesInAddress();
206 const char *DataPtr =
207 SecContents.drop_front(Obj->getBytesInAddress()).data();
David Majnemer1ac52eb2014-09-26 04:21:51 +0000208 TypeDescriptor TD;
David Majnemer6887a252014-09-26 08:01:23 +0000209 if (BytesInAddress == 8)
210 TD.AlwaysZero = *reinterpret_cast<const little64_t *>(DataPtr);
211 else
212 TD.AlwaysZero = *reinterpret_cast<const little32_t *>(DataPtr);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000213 TD.MangledName = SecContents.drop_front(Obj->getBytesInAddress() * 2);
214 StringRef *I = std::begin(TD.Symbols), *E = std::end(TD.Symbols);
215 if (collectRelocatedSymbols(Obj, SecI, I, E))
216 return;
217 TDs[SymName] = TD;
218 }
David Majnemer72ab1a52014-07-24 23:14:40 +0000219 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000220 for (const std::pair<std::pair<StringRef, uint64_t>, StringRef> &VFTableEntry :
221 VFTableEntries) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000222 StringRef VFTableName = VFTableEntry.first.first;
223 uint64_t Offset = VFTableEntry.first.second;
224 StringRef SymName = VFTableEntry.second;
225 outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n';
226 }
David Majnemer6887a252014-09-26 08:01:23 +0000227 for (const std::pair<StringRef, ArrayRef<little32_t>> &VBTable :
David Majnemerbf32f772014-07-25 04:30:11 +0000228 VBTables) {
229 StringRef VBTableName = VBTable.first;
David Majnemer72ab1a52014-07-24 23:14:40 +0000230 uint32_t Idx = 0;
David Majnemer6887a252014-09-26 08:01:23 +0000231 for (little32_t Offset : VBTable.second) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000232 outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n';
David Majnemerbf32f772014-07-25 04:30:11 +0000233 Idx += sizeof(Offset);
David Majnemer72ab1a52014-07-24 23:14:40 +0000234 }
235 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000236 for (const std::pair<StringRef, CompleteObjectLocator> &COLPair : COLs) {
237 StringRef COLName = COLPair.first;
238 const CompleteObjectLocator &COL = COLPair.second;
239 outs() << COLName << "[IsImageRelative]: " << COL.Data[0] << '\n';
240 outs() << COLName << "[OffsetToTop]: " << COL.Data[1] << '\n';
241 outs() << COLName << "[VFPtrOffset]: " << COL.Data[2] << '\n';
242 outs() << COLName << "[TypeDescriptor]: " << COL.Symbols[0] << '\n';
243 outs() << COLName << "[ClassHierarchyDescriptor]: " << COL.Symbols[1] << '\n';
244 }
245 for (const std::pair<StringRef, ClassHierarchyDescriptor> &CHDPair : CHDs) {
246 StringRef CHDName = CHDPair.first;
247 const ClassHierarchyDescriptor &CHD = CHDPair.second;
248 outs() << CHDName << "[AlwaysZero]: " << CHD.Data[0] << '\n';
249 outs() << CHDName << "[Flags]: " << CHD.Data[1] << '\n';
250 outs() << CHDName << "[NumClasses]: " << CHD.Data[2] << '\n';
251 outs() << CHDName << "[BaseClassArray]: " << CHD.Symbols[0] << '\n';
252 }
253 for (const std::pair<std::pair<StringRef, uint64_t>, StringRef> &BCAEntry :
254 BCAEntries) {
255 StringRef BCAName = BCAEntry.first.first;
256 uint64_t Offset = BCAEntry.first.second;
257 StringRef SymName = BCAEntry.second;
258 outs() << BCAName << '[' << Offset << "]: " << SymName << '\n';
259 }
260 for (const std::pair<StringRef, BaseClassDescriptor> &BCDPair : BCDs) {
261 StringRef BCDName = BCDPair.first;
262 const BaseClassDescriptor &BCD = BCDPair.second;
263 outs() << BCDName << "[TypeDescriptor]: " << BCD.Symbols[0] << '\n';
264 outs() << BCDName << "[NumBases]: " << BCD.Data[0] << '\n';
265 outs() << BCDName << "[OffsetInVBase]: " << BCD.Data[1] << '\n';
266 outs() << BCDName << "[VBPtrOffset]: " << BCD.Data[2] << '\n';
267 outs() << BCDName << "[OffsetInVBTable]: " << BCD.Data[3] << '\n';
268 outs() << BCDName << "[Flags]: " << BCD.Data[4] << '\n';
269 outs() << BCDName << "[ClassHierarchyDescriptor]: " << BCD.Symbols[1] << '\n';
270 }
271 for (const std::pair<StringRef, TypeDescriptor> &TDPair : TDs) {
272 StringRef TDName = TDPair.first;
273 const TypeDescriptor &TD = TDPair.second;
274 outs() << TDName << "[VFPtr]: " << TD.Symbols[0] << '\n';
David Majnemer6887a252014-09-26 08:01:23 +0000275 outs() << TDName << "[AlwaysZero]: " << TD.AlwaysZero << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000276 outs() << TDName << "[MangledName]: ";
David Majnemer56167c32014-09-26 05:50:45 +0000277 outs().write_escaped(TD.MangledName.rtrim(StringRef("\0", 1)),
278 /*UseHexEscapes=*/true)
279 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000280 }
David Majnemer72ab1a52014-07-24 23:14:40 +0000281}
282
283static void dumpArchive(const Archive *Arc) {
David Majnemereac48b62014-09-25 22:56:54 +0000284 for (const Archive::Child &ArcC : Arc->children()) {
285 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcC.getAsBinary();
David Majnemer72ab1a52014-07-24 23:14:40 +0000286 if (std::error_code EC = ChildOrErr.getError()) {
287 // Ignore non-object files.
288 if (EC != object_error::invalid_file_type)
289 reportError(Arc->getFileName(), EC.message());
290 continue;
291 }
292
293 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
294 dumpVTables(Obj);
295 else
296 reportError(Arc->getFileName(),
297 vtabledump_error::unrecognized_file_format);
298 }
299}
300
301static void dumpInput(StringRef File) {
302 // If file isn't stdin, check that it exists.
303 if (File != "-" && !sys::fs::exists(File)) {
304 reportError(File, vtabledump_error::file_not_found);
305 return;
306 }
307
308 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000309 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
David Majnemer72ab1a52014-07-24 23:14:40 +0000310 if (std::error_code EC = BinaryOrErr.getError()) {
311 reportError(File, EC);
312 return;
313 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000314 Binary &Binary = *BinaryOrErr.get().getBinary();
David Majnemer72ab1a52014-07-24 23:14:40 +0000315
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000316 if (Archive *Arc = dyn_cast<Archive>(&Binary))
David Majnemer72ab1a52014-07-24 23:14:40 +0000317 dumpArchive(Arc);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000318 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
David Majnemer72ab1a52014-07-24 23:14:40 +0000319 dumpVTables(Obj);
320 else
321 reportError(File, vtabledump_error::unrecognized_file_format);
322}
323
324int main(int argc, const char *argv[]) {
325 sys::PrintStackTraceOnErrorSignal();
326 PrettyStackTraceProgram X(argc, argv);
327 llvm_shutdown_obj Y;
328
329 // Initialize targets.
330 llvm::InitializeAllTargetInfos();
331
332 // Register the target printer for --version.
333 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
334
335 cl::ParseCommandLineOptions(argc, argv, "LLVM VTable Dumper\n");
336
337 // Default to stdin if no filename is specified.
338 if (opts::InputFilenames.size() == 0)
339 opts::InputFilenames.push_back("-");
340
341 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
342 dumpInput);
343
344 return ReturnValue;
345}