blob: ee905c6f696bb41fb4aceecbd730c2647fe4ffac [file] [log] [blame]
David Majnemerf45bbd02015-03-15 01:30:58 +00001//===- llvm-cxxdump.cpp - Dump C++ data in an Object File -------*- C++ -*-===//
David Majnemer72ab1a52014-07-24 23:14:40 +00002//
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//
David Majnemerf45bbd02015-03-15 01:30:58 +000010// Dumps C++ data resident in object files and archives.
David Majnemer72ab1a52014-07-24 23:14:40 +000011//
12//===----------------------------------------------------------------------===//
13
David Majnemerf45bbd02015-03-15 01:30:58 +000014#include "llvm-cxxdump.h"
David Majnemer72ab1a52014-07-24 23:14:40 +000015#include "Error.h"
16#include "llvm/ADT/ArrayRef.h"
David Majnemer72ab1a52014-07-24 23:14:40 +000017#include "llvm/Object/Archive.h"
18#include "llvm/Object/ObjectFile.h"
Rafael Espindolaa4a40932015-06-23 02:20:37 +000019#include "llvm/Object/SymbolSize.h"
David Majnemer72ab1a52014-07-24 23:14:40 +000020#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"
Benjamin Kramer16132e62015-03-23 18:07:13 +000028#include "llvm/Support/raw_ostream.h"
David Majnemer72ab1a52014-07-24 23:14:40 +000029#include <map>
30#include <string>
31#include <system_error>
32
33using namespace llvm;
34using namespace llvm::object;
35using namespace llvm::support;
36
37namespace opts {
38cl::list<std::string> InputFilenames(cl::Positional,
39 cl::desc("<input object files>"),
40 cl::ZeroOrMore);
41} // namespace opts
42
David Majnemer72ab1a52014-07-24 23:14:40 +000043namespace llvm {
44
Davide Italiano81032942015-07-17 06:18:36 +000045static void error(std::error_code EC) {
David Majnemer72ab1a52014-07-24 23:14:40 +000046 if (!EC)
Davide Italiano81032942015-07-17 06:18:36 +000047 return;
David Majnemer72ab1a52014-07-24 23:14:40 +000048 outs() << "\nError reading file: " << EC.message() << ".\n";
49 outs().flush();
Davide Italiano81032942015-07-17 06:18:36 +000050 exit(1);
David Majnemer72ab1a52014-07-24 23:14:40 +000051}
52
53} // namespace llvm
54
55static void reportError(StringRef Input, StringRef Message) {
56 if (Input == "-")
57 Input = "<stdin>";
David Majnemer72ab1a52014-07-24 23:14:40 +000058 errs() << Input << ": " << Message << "\n";
59 errs().flush();
Davide Italiano81032942015-07-17 06:18:36 +000060 exit(1);
David Majnemer72ab1a52014-07-24 23:14:40 +000061}
62
63static void reportError(StringRef Input, std::error_code EC) {
64 reportError(Input, EC.message());
65}
66
David Majnemere2683612014-11-03 07:23:25 +000067static SmallVectorImpl<SectionRef> &getRelocSections(const ObjectFile *Obj,
68 const SectionRef &Sec) {
69 static bool MappingDone = false;
70 static std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
71 if (!MappingDone) {
72 for (const SectionRef &Section : Obj->sections()) {
73 section_iterator Sec2 = Section.getRelocatedSection();
74 if (Sec2 != Obj->section_end())
75 SectionRelocMap[*Sec2].push_back(Section);
76 }
77 MappingDone = true;
78 }
79 return SectionRelocMap[Sec];
80}
81
Davide Italiano81032942015-07-17 06:18:36 +000082static void collectRelocatedSymbols(const ObjectFile *Obj,
David Majnemere2683612014-11-03 07:23:25 +000083 const SectionRef &Sec, uint64_t SecAddress,
84 uint64_t SymAddress, uint64_t SymSize,
85 StringRef *I, StringRef *E) {
86 uint64_t SymOffset = SymAddress - SecAddress;
87 uint64_t SymEnd = SymOffset + SymSize;
88 for (const SectionRef &SR : getRelocSections(Obj, Sec)) {
89 for (const object::RelocationRef &Reloc : SR.relocations()) {
90 if (I == E)
91 break;
92 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
93 if (RelocSymI == Obj->symbol_end())
94 continue;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +000095 ErrorOr<StringRef> RelocSymName = RelocSymI->getName();
Davide Italiano81032942015-07-17 06:18:36 +000096 error(RelocSymName.getError());
Rafael Espindola96d071c2015-06-29 23:29:12 +000097 uint64_t Offset = Reloc.getOffset();
David Majnemere2683612014-11-03 07:23:25 +000098 if (Offset >= SymOffset && Offset < SymEnd) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +000099 *I = *RelocSymName;
David Majnemere2683612014-11-03 07:23:25 +0000100 ++I;
101 }
102 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000103 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000104}
105
Davide Italiano81032942015-07-17 06:18:36 +0000106static void collectRelocationOffsets(
David Majnemere2683612014-11-03 07:23:25 +0000107 const ObjectFile *Obj, const SectionRef &Sec, uint64_t SecAddress,
108 uint64_t SymAddress, uint64_t SymSize, StringRef SymName,
David Majnemer1ac52eb2014-09-26 04:21:51 +0000109 std::map<std::pair<StringRef, uint64_t>, StringRef> &Collection) {
David Majnemere2683612014-11-03 07:23:25 +0000110 uint64_t SymOffset = SymAddress - SecAddress;
111 uint64_t SymEnd = SymOffset + SymSize;
112 for (const SectionRef &SR : getRelocSections(Obj, Sec)) {
113 for (const object::RelocationRef &Reloc : SR.relocations()) {
114 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
115 if (RelocSymI == Obj->symbol_end())
116 continue;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000117 ErrorOr<StringRef> RelocSymName = RelocSymI->getName();
Davide Italiano81032942015-07-17 06:18:36 +0000118 error(RelocSymName.getError());
Rafael Espindola96d071c2015-06-29 23:29:12 +0000119 uint64_t Offset = Reloc.getOffset();
David Majnemere2683612014-11-03 07:23:25 +0000120 if (Offset >= SymOffset && Offset < SymEnd)
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000121 Collection[std::make_pair(SymName, Offset - SymOffset)] = *RelocSymName;
David Majnemere2683612014-11-03 07:23:25 +0000122 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000123 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000124}
125
David Majnemerf45bbd02015-03-15 01:30:58 +0000126static void dumpCXXData(const ObjectFile *Obj) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000127 struct CompleteObjectLocator {
128 StringRef Symbols[2];
David Majnemer6887a252014-09-26 08:01:23 +0000129 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000130 };
131 struct ClassHierarchyDescriptor {
132 StringRef Symbols[1];
David Majnemer6887a252014-09-26 08:01:23 +0000133 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000134 };
135 struct BaseClassDescriptor {
136 StringRef Symbols[2];
David Majnemer6887a252014-09-26 08:01:23 +0000137 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000138 };
139 struct TypeDescriptor {
140 StringRef Symbols[1];
David Majnemer6887a252014-09-26 08:01:23 +0000141 uint64_t AlwaysZero;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000142 StringRef MangledName;
143 };
David Majnemerf50d0a52015-02-27 00:43:58 +0000144 struct ThrowInfo {
145 uint32_t Flags;
146 };
147 struct CatchableTypeArray {
148 uint32_t NumEntries;
149 };
150 struct CatchableType {
151 uint32_t Flags;
152 uint32_t NonVirtualBaseAdjustmentOffset;
153 int32_t VirtualBasePointerOffset;
154 uint32_t VirtualBaseAdjustmentOffset;
David Majnemer86ee1732015-02-27 22:35:25 +0000155 uint32_t Size;
David Majnemerf50d0a52015-02-27 00:43:58 +0000156 StringRef Symbols[2];
157 };
David Majnemer72ab1a52014-07-24 23:14:40 +0000158 std::map<std::pair<StringRef, uint64_t>, StringRef> VFTableEntries;
David Majnemerf50d0a52015-02-27 00:43:58 +0000159 std::map<std::pair<StringRef, uint64_t>, StringRef> TIEntries;
160 std::map<std::pair<StringRef, uint64_t>, StringRef> CTAEntries;
David Majnemer6887a252014-09-26 08:01:23 +0000161 std::map<StringRef, ArrayRef<little32_t>> VBTables;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000162 std::map<StringRef, CompleteObjectLocator> COLs;
163 std::map<StringRef, ClassHierarchyDescriptor> CHDs;
164 std::map<std::pair<StringRef, uint64_t>, StringRef> BCAEntries;
165 std::map<StringRef, BaseClassDescriptor> BCDs;
166 std::map<StringRef, TypeDescriptor> TDs;
David Majnemerf50d0a52015-02-27 00:43:58 +0000167 std::map<StringRef, ThrowInfo> TIs;
168 std::map<StringRef, CatchableTypeArray> CTAs;
169 std::map<StringRef, CatchableType> CTs;
David Majnemere2683612014-11-03 07:23:25 +0000170
171 std::map<std::pair<StringRef, uint64_t>, StringRef> VTableSymEntries;
172 std::map<std::pair<StringRef, uint64_t>, int64_t> VTableDataEntries;
173 std::map<std::pair<StringRef, uint64_t>, StringRef> VTTEntries;
174 std::map<StringRef, StringRef> TINames;
175
176 uint8_t BytesInAddress = Obj->getBytesInAddress();
177
Rafael Espindola6bf32212015-06-24 19:57:32 +0000178 std::vector<std::pair<SymbolRef, uint64_t>> SymAddr =
Rafael Espindolaa4a40932015-06-23 02:20:37 +0000179 object::computeSymbolSizes(*Obj);
Rafael Espindolaa4a40932015-06-23 02:20:37 +0000180
Rafael Espindola6bf32212015-06-24 19:57:32 +0000181 for (auto &P : SymAddr) {
Rafael Espindolaa4a40932015-06-23 02:20:37 +0000182 object::SymbolRef Sym = P.first;
183 uint64_t SymSize = P.second;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000184 ErrorOr<StringRef> SymNameOrErr = Sym.getName();
Davide Italiano81032942015-07-17 06:18:36 +0000185 error(SymNameOrErr.getError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000186 StringRef SymName = *SymNameOrErr;
David Majnemer601327c2014-09-26 22:32:19 +0000187 object::section_iterator SecI(Obj->section_begin());
Davide Italiano81032942015-07-17 06:18:36 +0000188 error(Sym.getSection(SecI));
David Majnemer601327c2014-09-26 22:32:19 +0000189 // Skip external symbols.
190 if (SecI == Obj->section_end())
191 continue;
David Majnemere2683612014-11-03 07:23:25 +0000192 const SectionRef &Sec = *SecI;
David Majnemer601327c2014-09-26 22:32:19 +0000193 // Skip virtual or BSS sections.
David Majnemere2683612014-11-03 07:23:25 +0000194 if (Sec.isBSS() || Sec.isVirtual())
David Majnemer601327c2014-09-26 22:32:19 +0000195 continue;
196 StringRef SecContents;
Davide Italiano81032942015-07-17 06:18:36 +0000197 error(Sec.getContents(SecContents));
Rafael Espindolaed067c42015-07-03 18:19:00 +0000198 ErrorOr<uint64_t> SymAddressOrErr = Sym.getAddress();
Davide Italiano81032942015-07-17 06:18:36 +0000199 error(SymAddressOrErr.getError());
Rafael Espindolaed067c42015-07-03 18:19:00 +0000200 uint64_t SymAddress = *SymAddressOrErr;
David Majnemere2683612014-11-03 07:23:25 +0000201 uint64_t SecAddress = Sec.getAddress();
202 uint64_t SecSize = Sec.getSize();
203 uint64_t SymOffset = SymAddress - SecAddress;
204 StringRef SymContents = SecContents.substr(SymOffset, SymSize);
205
David Majnemer72ab1a52014-07-24 23:14:40 +0000206 // VFTables in the MS-ABI start with '??_7' and are contained within their
207 // own COMDAT section. We then determine the contents of the VFTable by
208 // looking at each relocation in the section.
209 if (SymName.startswith("??_7")) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000210 // Each relocation either names a virtual method or a thunk. We note the
211 // offset into the section and the symbol used for the relocation.
David Majnemere2683612014-11-03 07:23:25 +0000212 collectRelocationOffsets(Obj, Sec, SecAddress, SecAddress, SecSize,
213 SymName, VFTableEntries);
David Majnemer72ab1a52014-07-24 23:14:40 +0000214 }
215 // VBTables in the MS-ABI start with '??_8' and are filled with 32-bit
216 // offsets of virtual bases.
217 else if (SymName.startswith("??_8")) {
David Majnemer6887a252014-09-26 08:01:23 +0000218 ArrayRef<little32_t> VBTableData(
David Majnemere2683612014-11-03 07:23:25 +0000219 reinterpret_cast<const little32_t *>(SymContents.data()),
220 SymContents.size() / sizeof(little32_t));
David Majnemer72ab1a52014-07-24 23:14:40 +0000221 VBTables[SymName] = VBTableData;
222 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000223 // Complete object locators in the MS-ABI start with '??_R4'
224 else if (SymName.startswith("??_R4")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000225 CompleteObjectLocator COL;
David Majnemer6887a252014-09-26 08:01:23 +0000226 COL.Data = ArrayRef<little32_t>(
David Majnemere2683612014-11-03 07:23:25 +0000227 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000228 StringRef *I = std::begin(COL.Symbols), *E = std::end(COL.Symbols);
Davide Italiano81032942015-07-17 06:18:36 +0000229 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000230 COLs[SymName] = COL;
231 }
232 // Class hierarchy descriptors in the MS-ABI start with '??_R3'
233 else if (SymName.startswith("??_R3")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000234 ClassHierarchyDescriptor CHD;
David Majnemer6887a252014-09-26 08:01:23 +0000235 CHD.Data = ArrayRef<little32_t>(
David Majnemere2683612014-11-03 07:23:25 +0000236 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000237 StringRef *I = std::begin(CHD.Symbols), *E = std::end(CHD.Symbols);
Davide Italiano81032942015-07-17 06:18:36 +0000238 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000239 CHDs[SymName] = CHD;
240 }
241 // Class hierarchy descriptors in the MS-ABI start with '??_R2'
242 else if (SymName.startswith("??_R2")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000243 // Each relocation names a base class descriptor. We note the offset into
244 // the section and the symbol used for the relocation.
David Majnemere2683612014-11-03 07:23:25 +0000245 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
246 SymName, BCAEntries);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000247 }
248 // Base class descriptors in the MS-ABI start with '??_R1'
249 else if (SymName.startswith("??_R1")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000250 BaseClassDescriptor BCD;
David Majnemer6887a252014-09-26 08:01:23 +0000251 BCD.Data = ArrayRef<little32_t>(
David Majnemere2683612014-11-03 07:23:25 +0000252 reinterpret_cast<const little32_t *>(SymContents.data()) + 1, 5);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000253 StringRef *I = std::begin(BCD.Symbols), *E = std::end(BCD.Symbols);
Davide Italiano81032942015-07-17 06:18:36 +0000254 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000255 BCDs[SymName] = BCD;
256 }
257 // Type descriptors in the MS-ABI start with '??_R0'
258 else if (SymName.startswith("??_R0")) {
David Majnemere2683612014-11-03 07:23:25 +0000259 const char *DataPtr = SymContents.drop_front(BytesInAddress).data();
David Majnemer1ac52eb2014-09-26 04:21:51 +0000260 TypeDescriptor TD;
David Majnemer6887a252014-09-26 08:01:23 +0000261 if (BytesInAddress == 8)
262 TD.AlwaysZero = *reinterpret_cast<const little64_t *>(DataPtr);
263 else
264 TD.AlwaysZero = *reinterpret_cast<const little32_t *>(DataPtr);
David Majnemere2683612014-11-03 07:23:25 +0000265 TD.MangledName = SymContents.drop_front(BytesInAddress * 2);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000266 StringRef *I = std::begin(TD.Symbols), *E = std::end(TD.Symbols);
Davide Italiano81032942015-07-17 06:18:36 +0000267 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000268 TDs[SymName] = TD;
269 }
David Majnemerf50d0a52015-02-27 00:43:58 +0000270 // Throw descriptors in the MS-ABI start with '_TI'
271 else if (SymName.startswith("_TI") || SymName.startswith("__TI")) {
272 ThrowInfo TI;
273 TI.Flags = *reinterpret_cast<const little32_t *>(SymContents.data());
274 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
275 SymName, TIEntries);
276 TIs[SymName] = TI;
277 }
278 // Catchable type arrays in the MS-ABI start with _CTA or __CTA.
279 else if (SymName.startswith("_CTA") || SymName.startswith("__CTA")) {
280 CatchableTypeArray CTA;
281 CTA.NumEntries =
282 *reinterpret_cast<const little32_t *>(SymContents.data());
283 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
284 SymName, CTAEntries);
285 CTAs[SymName] = CTA;
286 }
287 // Catchable types in the MS-ABI start with _CT or __CT.
288 else if (SymName.startswith("_CT") || SymName.startswith("__CT")) {
289 const little32_t *DataPtr =
290 reinterpret_cast<const little32_t *>(SymContents.data());
291 CatchableType CT;
292 CT.Flags = DataPtr[0];
293 CT.NonVirtualBaseAdjustmentOffset = DataPtr[2];
294 CT.VirtualBasePointerOffset = DataPtr[3];
295 CT.VirtualBaseAdjustmentOffset = DataPtr[4];
David Majnemer86ee1732015-02-27 22:35:25 +0000296 CT.Size = DataPtr[5];
David Majnemerf50d0a52015-02-27 00:43:58 +0000297 StringRef *I = std::begin(CT.Symbols), *E = std::end(CT.Symbols);
Davide Italiano81032942015-07-17 06:18:36 +0000298 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
David Majnemerf50d0a52015-02-27 00:43:58 +0000299 CTs[SymName] = CT;
300 }
David Majnemere2683612014-11-03 07:23:25 +0000301 // Construction vtables in the Itanium ABI start with '_ZTT' or '__ZTT'.
302 else if (SymName.startswith("_ZTT") || SymName.startswith("__ZTT")) {
303 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
304 SymName, VTTEntries);
305 }
306 // Typeinfo names in the Itanium ABI start with '_ZTS' or '__ZTS'.
307 else if (SymName.startswith("_ZTS") || SymName.startswith("__ZTS")) {
308 TINames[SymName] = SymContents.slice(0, SymContents.find('\0'));
309 }
310 // Vtables in the Itanium ABI start with '_ZTV' or '__ZTV'.
311 else if (SymName.startswith("_ZTV") || SymName.startswith("__ZTV")) {
312 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
313 SymName, VTableSymEntries);
314 for (uint64_t SymOffI = 0; SymOffI < SymSize; SymOffI += BytesInAddress) {
315 auto Key = std::make_pair(SymName, SymOffI);
316 if (VTableSymEntries.count(Key))
317 continue;
David Majnemerf45bbd02015-03-15 01:30:58 +0000318 const char *DataPtr =
319 SymContents.substr(SymOffI, BytesInAddress).data();
David Majnemere2683612014-11-03 07:23:25 +0000320 int64_t VData;
321 if (BytesInAddress == 8)
322 VData = *reinterpret_cast<const little64_t *>(DataPtr);
323 else
324 VData = *reinterpret_cast<const little32_t *>(DataPtr);
325 VTableDataEntries[Key] = VData;
326 }
327 }
328 // Typeinfo structures in the Itanium ABI start with '_ZTI' or '__ZTI'.
329 else if (SymName.startswith("_ZTI") || SymName.startswith("__ZTI")) {
330 // FIXME: Do something with these!
331 }
David Majnemer72ab1a52014-07-24 23:14:40 +0000332 }
David Majnemerf45bbd02015-03-15 01:30:58 +0000333 for (const auto &VFTableEntry : VFTableEntries) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000334 StringRef VFTableName = VFTableEntry.first.first;
335 uint64_t Offset = VFTableEntry.first.second;
336 StringRef SymName = VFTableEntry.second;
337 outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n';
338 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000339 for (const auto &VBTable : VBTables) {
David Majnemerbf32f772014-07-25 04:30:11 +0000340 StringRef VBTableName = VBTable.first;
David Majnemer72ab1a52014-07-24 23:14:40 +0000341 uint32_t Idx = 0;
David Majnemer6887a252014-09-26 08:01:23 +0000342 for (little32_t Offset : VBTable.second) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000343 outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n';
David Majnemerbf32f772014-07-25 04:30:11 +0000344 Idx += sizeof(Offset);
David Majnemer72ab1a52014-07-24 23:14:40 +0000345 }
346 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000347 for (const auto &COLPair : COLs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000348 StringRef COLName = COLPair.first;
349 const CompleteObjectLocator &COL = COLPair.second;
350 outs() << COLName << "[IsImageRelative]: " << COL.Data[0] << '\n';
351 outs() << COLName << "[OffsetToTop]: " << COL.Data[1] << '\n';
352 outs() << COLName << "[VFPtrOffset]: " << COL.Data[2] << '\n';
353 outs() << COLName << "[TypeDescriptor]: " << COL.Symbols[0] << '\n';
David Majnemerf45bbd02015-03-15 01:30:58 +0000354 outs() << COLName << "[ClassHierarchyDescriptor]: " << COL.Symbols[1]
355 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000356 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000357 for (const auto &CHDPair : CHDs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000358 StringRef CHDName = CHDPair.first;
359 const ClassHierarchyDescriptor &CHD = CHDPair.second;
360 outs() << CHDName << "[AlwaysZero]: " << CHD.Data[0] << '\n';
361 outs() << CHDName << "[Flags]: " << CHD.Data[1] << '\n';
362 outs() << CHDName << "[NumClasses]: " << CHD.Data[2] << '\n';
363 outs() << CHDName << "[BaseClassArray]: " << CHD.Symbols[0] << '\n';
364 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000365 for (const auto &BCAEntry : BCAEntries) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000366 StringRef BCAName = BCAEntry.first.first;
367 uint64_t Offset = BCAEntry.first.second;
368 StringRef SymName = BCAEntry.second;
369 outs() << BCAName << '[' << Offset << "]: " << SymName << '\n';
370 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000371 for (const auto &BCDPair : BCDs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000372 StringRef BCDName = BCDPair.first;
373 const BaseClassDescriptor &BCD = BCDPair.second;
374 outs() << BCDName << "[TypeDescriptor]: " << BCD.Symbols[0] << '\n';
375 outs() << BCDName << "[NumBases]: " << BCD.Data[0] << '\n';
376 outs() << BCDName << "[OffsetInVBase]: " << BCD.Data[1] << '\n';
377 outs() << BCDName << "[VBPtrOffset]: " << BCD.Data[2] << '\n';
378 outs() << BCDName << "[OffsetInVBTable]: " << BCD.Data[3] << '\n';
379 outs() << BCDName << "[Flags]: " << BCD.Data[4] << '\n';
David Majnemerf45bbd02015-03-15 01:30:58 +0000380 outs() << BCDName << "[ClassHierarchyDescriptor]: " << BCD.Symbols[1]
381 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000382 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000383 for (const auto &TDPair : TDs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000384 StringRef TDName = TDPair.first;
385 const TypeDescriptor &TD = TDPair.second;
386 outs() << TDName << "[VFPtr]: " << TD.Symbols[0] << '\n';
David Majnemer6887a252014-09-26 08:01:23 +0000387 outs() << TDName << "[AlwaysZero]: " << TD.AlwaysZero << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000388 outs() << TDName << "[MangledName]: ";
David Majnemer56167c32014-09-26 05:50:45 +0000389 outs().write_escaped(TD.MangledName.rtrim(StringRef("\0", 1)),
390 /*UseHexEscapes=*/true)
391 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000392 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000393 for (const auto &TIPair : TIs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000394 StringRef TIName = TIPair.first;
395 const ThrowInfo &TI = TIPair.second;
396 auto dumpThrowInfoFlag = [&](const char *Name, uint32_t Flag) {
397 outs() << TIName << "[Flags." << Name
398 << "]: " << (TI.Flags & Flag ? "true" : "false") << '\n';
399 };
400 auto dumpThrowInfoSymbol = [&](const char *Name, int Offset) {
401 outs() << TIName << '[' << Name << "]: ";
402 auto Entry = TIEntries.find(std::make_pair(TIName, Offset));
403 outs() << (Entry == TIEntries.end() ? "null" : Entry->second) << '\n';
404 };
405 outs() << TIName << "[Flags]: " << TI.Flags << '\n';
406 dumpThrowInfoFlag("Const", 1);
407 dumpThrowInfoFlag("Volatile", 2);
408 dumpThrowInfoSymbol("CleanupFn", 4);
409 dumpThrowInfoSymbol("ForwardCompat", 8);
410 dumpThrowInfoSymbol("CatchableTypeArray", 12);
411 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000412 for (const auto &CTAPair : CTAs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000413 StringRef CTAName = CTAPair.first;
414 const CatchableTypeArray &CTA = CTAPair.second;
415
416 outs() << CTAName << "[NumEntries]: " << CTA.NumEntries << '\n';
417
418 unsigned Idx = 0;
419 for (auto I = CTAEntries.lower_bound(std::make_pair(CTAName, 0)),
420 E = CTAEntries.upper_bound(std::make_pair(CTAName, UINT64_MAX));
421 I != E; ++I)
422 outs() << CTAName << '[' << Idx++ << "]: " << I->second << '\n';
423 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000424 for (const auto &CTPair : CTs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000425 StringRef CTName = CTPair.first;
426 const CatchableType &CT = CTPair.second;
427 auto dumpCatchableTypeFlag = [&](const char *Name, uint32_t Flag) {
428 outs() << CTName << "[Flags." << Name
429 << "]: " << (CT.Flags & Flag ? "true" : "false") << '\n';
430 };
431 outs() << CTName << "[Flags]: " << CT.Flags << '\n';
432 dumpCatchableTypeFlag("ScalarType", 1);
433 dumpCatchableTypeFlag("VirtualInheritance", 4);
434 outs() << CTName << "[TypeDescriptor]: " << CT.Symbols[0] << '\n';
435 outs() << CTName << "[NonVirtualBaseAdjustmentOffset]: "
436 << CT.NonVirtualBaseAdjustmentOffset << '\n';
437 outs() << CTName
438 << "[VirtualBasePointerOffset]: " << CT.VirtualBasePointerOffset
439 << '\n';
440 outs() << CTName << "[VirtualBaseAdjustmentOffset]: "
441 << CT.VirtualBaseAdjustmentOffset << '\n';
David Majnemer86ee1732015-02-27 22:35:25 +0000442 outs() << CTName << "[Size]: " << CT.Size << '\n';
David Majnemerf50d0a52015-02-27 00:43:58 +0000443 outs() << CTName
444 << "[CopyCtor]: " << (CT.Symbols[1].empty() ? "null" : CT.Symbols[1])
445 << '\n';
446 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000447 for (const auto &VTTPair : VTTEntries) {
David Majnemere2683612014-11-03 07:23:25 +0000448 StringRef VTTName = VTTPair.first.first;
449 uint64_t VTTOffset = VTTPair.first.second;
450 StringRef VTTEntry = VTTPair.second;
451 outs() << VTTName << '[' << VTTOffset << "]: " << VTTEntry << '\n';
452 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000453 for (const auto &TIPair : TINames) {
David Majnemere2683612014-11-03 07:23:25 +0000454 StringRef TIName = TIPair.first;
455 outs() << TIName << ": " << TIPair.second << '\n';
456 }
457 auto VTableSymI = VTableSymEntries.begin();
458 auto VTableSymE = VTableSymEntries.end();
459 auto VTableDataI = VTableDataEntries.begin();
460 auto VTableDataE = VTableDataEntries.end();
461 for (;;) {
462 bool SymDone = VTableSymI == VTableSymE;
463 bool DataDone = VTableDataI == VTableDataE;
464 if (SymDone && DataDone)
465 break;
466 if (!SymDone && (DataDone || VTableSymI->first < VTableDataI->first)) {
467 StringRef VTableName = VTableSymI->first.first;
468 uint64_t Offset = VTableSymI->first.second;
469 StringRef VTableEntry = VTableSymI->second;
470 outs() << VTableName << '[' << Offset << "]: ";
471 outs() << VTableEntry;
472 outs() << '\n';
473 ++VTableSymI;
474 continue;
475 }
476 if (!DataDone && (SymDone || VTableDataI->first < VTableSymI->first)) {
477 StringRef VTableName = VTableDataI->first.first;
478 uint64_t Offset = VTableDataI->first.second;
479 int64_t VTableEntry = VTableDataI->second;
480 outs() << VTableName << '[' << Offset << "]: ";
481 outs() << VTableEntry;
482 outs() << '\n';
483 ++VTableDataI;
484 continue;
485 }
486 }
David Majnemer72ab1a52014-07-24 23:14:40 +0000487}
488
489static void dumpArchive(const Archive *Arc) {
David Majnemereac48b62014-09-25 22:56:54 +0000490 for (const Archive::Child &ArcC : Arc->children()) {
491 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcC.getAsBinary();
David Majnemer72ab1a52014-07-24 23:14:40 +0000492 if (std::error_code EC = ChildOrErr.getError()) {
493 // Ignore non-object files.
494 if (EC != object_error::invalid_file_type)
495 reportError(Arc->getFileName(), EC.message());
496 continue;
497 }
498
499 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
David Majnemerf45bbd02015-03-15 01:30:58 +0000500 dumpCXXData(Obj);
David Majnemer72ab1a52014-07-24 23:14:40 +0000501 else
David Majnemerf45bbd02015-03-15 01:30:58 +0000502 reportError(Arc->getFileName(), cxxdump_error::unrecognized_file_format);
David Majnemer72ab1a52014-07-24 23:14:40 +0000503 }
504}
505
506static void dumpInput(StringRef File) {
507 // If file isn't stdin, check that it exists.
508 if (File != "-" && !sys::fs::exists(File)) {
David Majnemerf45bbd02015-03-15 01:30:58 +0000509 reportError(File, cxxdump_error::file_not_found);
David Majnemer72ab1a52014-07-24 23:14:40 +0000510 return;
511 }
512
513 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000514 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
David Majnemer72ab1a52014-07-24 23:14:40 +0000515 if (std::error_code EC = BinaryOrErr.getError()) {
516 reportError(File, EC);
517 return;
518 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000519 Binary &Binary = *BinaryOrErr.get().getBinary();
David Majnemer72ab1a52014-07-24 23:14:40 +0000520
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000521 if (Archive *Arc = dyn_cast<Archive>(&Binary))
David Majnemer72ab1a52014-07-24 23:14:40 +0000522 dumpArchive(Arc);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000523 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
David Majnemerf45bbd02015-03-15 01:30:58 +0000524 dumpCXXData(Obj);
David Majnemer72ab1a52014-07-24 23:14:40 +0000525 else
David Majnemerf45bbd02015-03-15 01:30:58 +0000526 reportError(File, cxxdump_error::unrecognized_file_format);
David Majnemer72ab1a52014-07-24 23:14:40 +0000527}
528
529int main(int argc, const char *argv[]) {
530 sys::PrintStackTraceOnErrorSignal();
531 PrettyStackTraceProgram X(argc, argv);
532 llvm_shutdown_obj Y;
533
534 // Initialize targets.
535 llvm::InitializeAllTargetInfos();
536
537 // Register the target printer for --version.
538 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
539
David Majnemerf45bbd02015-03-15 01:30:58 +0000540 cl::ParseCommandLineOptions(argc, argv, "LLVM C++ ABI Data Dumper\n");
David Majnemer72ab1a52014-07-24 23:14:40 +0000541
542 // Default to stdin if no filename is specified.
543 if (opts::InputFilenames.size() == 0)
544 opts::InputFilenames.push_back("-");
545
546 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
547 dumpInput);
548
Davide Italiano81032942015-07-17 06:18:36 +0000549 return EXIT_SUCCESS;
David Majnemer72ab1a52014-07-24 23:14:40 +0000550}