blob: 0c408ccb5baac7c28a36a337fe0355c356c4cb20 [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 Majnemerd7fafa02015-08-13 18:31:43 +000067static std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
David Majnemere2683612014-11-03 07:23:25 +000068
Davide Italiano81032942015-07-17 06:18:36 +000069static void collectRelocatedSymbols(const ObjectFile *Obj,
David Majnemere2683612014-11-03 07:23:25 +000070 const SectionRef &Sec, uint64_t SecAddress,
71 uint64_t SymAddress, uint64_t SymSize,
72 StringRef *I, StringRef *E) {
73 uint64_t SymOffset = SymAddress - SecAddress;
74 uint64_t SymEnd = SymOffset + SymSize;
David Majnemerd7fafa02015-08-13 18:31:43 +000075 for (const SectionRef &SR : SectionRelocMap[Sec]) {
David Majnemere2683612014-11-03 07:23:25 +000076 for (const object::RelocationRef &Reloc : SR.relocations()) {
77 if (I == E)
78 break;
79 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
80 if (RelocSymI == Obj->symbol_end())
81 continue;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +000082 Expected<StringRef> RelocSymName = RelocSymI->getName();
83 error(errorToErrorCode(RelocSymName.takeError()));
Rafael Espindola96d071c2015-06-29 23:29:12 +000084 uint64_t Offset = Reloc.getOffset();
David Majnemere2683612014-11-03 07:23:25 +000085 if (Offset >= SymOffset && Offset < SymEnd) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +000086 *I = *RelocSymName;
David Majnemere2683612014-11-03 07:23:25 +000087 ++I;
88 }
89 }
David Majnemer1ac52eb2014-09-26 04:21:51 +000090 }
David Majnemer1ac52eb2014-09-26 04:21:51 +000091}
92
Davide Italiano81032942015-07-17 06:18:36 +000093static void collectRelocationOffsets(
David Majnemere2683612014-11-03 07:23:25 +000094 const ObjectFile *Obj, const SectionRef &Sec, uint64_t SecAddress,
95 uint64_t SymAddress, uint64_t SymSize, StringRef SymName,
David Majnemer1ac52eb2014-09-26 04:21:51 +000096 std::map<std::pair<StringRef, uint64_t>, StringRef> &Collection) {
David Majnemere2683612014-11-03 07:23:25 +000097 uint64_t SymOffset = SymAddress - SecAddress;
98 uint64_t SymEnd = SymOffset + SymSize;
David Majnemerd7fafa02015-08-13 18:31:43 +000099 for (const SectionRef &SR : SectionRelocMap[Sec]) {
David Majnemere2683612014-11-03 07:23:25 +0000100 for (const object::RelocationRef &Reloc : SR.relocations()) {
101 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
102 if (RelocSymI == Obj->symbol_end())
103 continue;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000104 Expected<StringRef> RelocSymName = RelocSymI->getName();
105 error(errorToErrorCode(RelocSymName.takeError()));
Rafael Espindola96d071c2015-06-29 23:29:12 +0000106 uint64_t Offset = Reloc.getOffset();
David Majnemere2683612014-11-03 07:23:25 +0000107 if (Offset >= SymOffset && Offset < SymEnd)
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000108 Collection[std::make_pair(SymName, Offset - SymOffset)] = *RelocSymName;
David Majnemere2683612014-11-03 07:23:25 +0000109 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000110 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000111}
112
David Majnemerf45bbd02015-03-15 01:30:58 +0000113static void dumpCXXData(const ObjectFile *Obj) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000114 struct CompleteObjectLocator {
115 StringRef Symbols[2];
David Majnemer6887a252014-09-26 08:01:23 +0000116 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000117 };
118 struct ClassHierarchyDescriptor {
119 StringRef Symbols[1];
David Majnemer6887a252014-09-26 08:01:23 +0000120 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000121 };
122 struct BaseClassDescriptor {
123 StringRef Symbols[2];
David Majnemer6887a252014-09-26 08:01:23 +0000124 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000125 };
126 struct TypeDescriptor {
127 StringRef Symbols[1];
David Majnemer6887a252014-09-26 08:01:23 +0000128 uint64_t AlwaysZero;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000129 StringRef MangledName;
130 };
David Majnemerf50d0a52015-02-27 00:43:58 +0000131 struct ThrowInfo {
132 uint32_t Flags;
133 };
134 struct CatchableTypeArray {
135 uint32_t NumEntries;
136 };
137 struct CatchableType {
138 uint32_t Flags;
139 uint32_t NonVirtualBaseAdjustmentOffset;
140 int32_t VirtualBasePointerOffset;
141 uint32_t VirtualBaseAdjustmentOffset;
David Majnemer86ee1732015-02-27 22:35:25 +0000142 uint32_t Size;
David Majnemerf50d0a52015-02-27 00:43:58 +0000143 StringRef Symbols[2];
144 };
David Majnemer72ab1a52014-07-24 23:14:40 +0000145 std::map<std::pair<StringRef, uint64_t>, StringRef> VFTableEntries;
David Majnemerf50d0a52015-02-27 00:43:58 +0000146 std::map<std::pair<StringRef, uint64_t>, StringRef> TIEntries;
147 std::map<std::pair<StringRef, uint64_t>, StringRef> CTAEntries;
David Majnemer6887a252014-09-26 08:01:23 +0000148 std::map<StringRef, ArrayRef<little32_t>> VBTables;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000149 std::map<StringRef, CompleteObjectLocator> COLs;
150 std::map<StringRef, ClassHierarchyDescriptor> CHDs;
151 std::map<std::pair<StringRef, uint64_t>, StringRef> BCAEntries;
152 std::map<StringRef, BaseClassDescriptor> BCDs;
153 std::map<StringRef, TypeDescriptor> TDs;
David Majnemerf50d0a52015-02-27 00:43:58 +0000154 std::map<StringRef, ThrowInfo> TIs;
155 std::map<StringRef, CatchableTypeArray> CTAs;
156 std::map<StringRef, CatchableType> CTs;
David Majnemere2683612014-11-03 07:23:25 +0000157
158 std::map<std::pair<StringRef, uint64_t>, StringRef> VTableSymEntries;
159 std::map<std::pair<StringRef, uint64_t>, int64_t> VTableDataEntries;
160 std::map<std::pair<StringRef, uint64_t>, StringRef> VTTEntries;
161 std::map<StringRef, StringRef> TINames;
162
David Majnemerd7fafa02015-08-13 18:31:43 +0000163 SectionRelocMap.clear();
164 for (const SectionRef &Section : Obj->sections()) {
165 section_iterator Sec2 = Section.getRelocatedSection();
166 if (Sec2 != Obj->section_end())
167 SectionRelocMap[*Sec2].push_back(Section);
168 }
169
David Majnemere2683612014-11-03 07:23:25 +0000170 uint8_t BytesInAddress = Obj->getBytesInAddress();
171
Rafael Espindola6bf32212015-06-24 19:57:32 +0000172 std::vector<std::pair<SymbolRef, uint64_t>> SymAddr =
Rafael Espindolaa4a40932015-06-23 02:20:37 +0000173 object::computeSymbolSizes(*Obj);
Rafael Espindolaa4a40932015-06-23 02:20:37 +0000174
Rafael Espindola6bf32212015-06-24 19:57:32 +0000175 for (auto &P : SymAddr) {
Rafael Espindolaa4a40932015-06-23 02:20:37 +0000176 object::SymbolRef Sym = P.first;
177 uint64_t SymSize = P.second;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000178 Expected<StringRef> SymNameOrErr = Sym.getName();
179 error(errorToErrorCode(SymNameOrErr.takeError()));
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000180 StringRef SymName = *SymNameOrErr;
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000181 Expected<object::section_iterator> SecIOrErr = Sym.getSection();
182 error(errorToErrorCode(SecIOrErr.takeError()));
Rafael Espindola8bab8892015-08-07 23:27:14 +0000183 object::section_iterator SecI = *SecIOrErr;
David Majnemer601327c2014-09-26 22:32:19 +0000184 // Skip external symbols.
185 if (SecI == Obj->section_end())
186 continue;
David Majnemere2683612014-11-03 07:23:25 +0000187 const SectionRef &Sec = *SecI;
David Majnemer601327c2014-09-26 22:32:19 +0000188 // Skip virtual or BSS sections.
David Majnemere2683612014-11-03 07:23:25 +0000189 if (Sec.isBSS() || Sec.isVirtual())
David Majnemer601327c2014-09-26 22:32:19 +0000190 continue;
191 StringRef SecContents;
Davide Italiano81032942015-07-17 06:18:36 +0000192 error(Sec.getContents(SecContents));
Kevin Enderby931cb652016-06-24 18:24:42 +0000193 Expected<uint64_t> SymAddressOrErr = Sym.getAddress();
194 error(errorToErrorCode(SymAddressOrErr.takeError()));
Rafael Espindolaed067c42015-07-03 18:19:00 +0000195 uint64_t SymAddress = *SymAddressOrErr;
David Majnemere2683612014-11-03 07:23:25 +0000196 uint64_t SecAddress = Sec.getAddress();
197 uint64_t SecSize = Sec.getSize();
198 uint64_t SymOffset = SymAddress - SecAddress;
199 StringRef SymContents = SecContents.substr(SymOffset, SymSize);
200
David Majnemer72ab1a52014-07-24 23:14:40 +0000201 // VFTables in the MS-ABI start with '??_7' and are contained within their
202 // own COMDAT section. We then determine the contents of the VFTable by
203 // looking at each relocation in the section.
204 if (SymName.startswith("??_7")) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000205 // Each relocation either names a virtual method or a thunk. We note the
206 // offset into the section and the symbol used for the relocation.
David Majnemere2683612014-11-03 07:23:25 +0000207 collectRelocationOffsets(Obj, Sec, SecAddress, SecAddress, SecSize,
208 SymName, VFTableEntries);
David Majnemer72ab1a52014-07-24 23:14:40 +0000209 }
210 // VBTables in the MS-ABI start with '??_8' and are filled with 32-bit
211 // offsets of virtual bases.
212 else if (SymName.startswith("??_8")) {
David Majnemer6887a252014-09-26 08:01:23 +0000213 ArrayRef<little32_t> VBTableData(
David Majnemere2683612014-11-03 07:23:25 +0000214 reinterpret_cast<const little32_t *>(SymContents.data()),
215 SymContents.size() / sizeof(little32_t));
David Majnemer72ab1a52014-07-24 23:14:40 +0000216 VBTables[SymName] = VBTableData;
217 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000218 // Complete object locators in the MS-ABI start with '??_R4'
219 else if (SymName.startswith("??_R4")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000220 CompleteObjectLocator COL;
Craig Topper0013be12015-09-21 05:32:41 +0000221 COL.Data = makeArrayRef(
David Majnemere2683612014-11-03 07:23:25 +0000222 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000223 StringRef *I = std::begin(COL.Symbols), *E = std::end(COL.Symbols);
Davide Italiano81032942015-07-17 06:18:36 +0000224 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000225 COLs[SymName] = COL;
226 }
227 // Class hierarchy descriptors in the MS-ABI start with '??_R3'
228 else if (SymName.startswith("??_R3")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000229 ClassHierarchyDescriptor CHD;
Craig Topper0013be12015-09-21 05:32:41 +0000230 CHD.Data = makeArrayRef(
David Majnemere2683612014-11-03 07:23:25 +0000231 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000232 StringRef *I = std::begin(CHD.Symbols), *E = std::end(CHD.Symbols);
Davide Italiano81032942015-07-17 06:18:36 +0000233 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000234 CHDs[SymName] = CHD;
235 }
236 // Class hierarchy descriptors in the MS-ABI start with '??_R2'
237 else if (SymName.startswith("??_R2")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000238 // Each relocation names a base class descriptor. We note the offset into
239 // the section and the symbol used for the relocation.
David Majnemere2683612014-11-03 07:23:25 +0000240 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
241 SymName, BCAEntries);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000242 }
243 // Base class descriptors in the MS-ABI start with '??_R1'
244 else if (SymName.startswith("??_R1")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000245 BaseClassDescriptor BCD;
Craig Topper0013be12015-09-21 05:32:41 +0000246 BCD.Data = makeArrayRef(
David Majnemere2683612014-11-03 07:23:25 +0000247 reinterpret_cast<const little32_t *>(SymContents.data()) + 1, 5);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000248 StringRef *I = std::begin(BCD.Symbols), *E = std::end(BCD.Symbols);
Davide Italiano81032942015-07-17 06:18:36 +0000249 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000250 BCDs[SymName] = BCD;
251 }
252 // Type descriptors in the MS-ABI start with '??_R0'
253 else if (SymName.startswith("??_R0")) {
David Majnemere2683612014-11-03 07:23:25 +0000254 const char *DataPtr = SymContents.drop_front(BytesInAddress).data();
David Majnemer1ac52eb2014-09-26 04:21:51 +0000255 TypeDescriptor TD;
David Majnemer6887a252014-09-26 08:01:23 +0000256 if (BytesInAddress == 8)
257 TD.AlwaysZero = *reinterpret_cast<const little64_t *>(DataPtr);
258 else
259 TD.AlwaysZero = *reinterpret_cast<const little32_t *>(DataPtr);
David Majnemere2683612014-11-03 07:23:25 +0000260 TD.MangledName = SymContents.drop_front(BytesInAddress * 2);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000261 StringRef *I = std::begin(TD.Symbols), *E = std::end(TD.Symbols);
Davide Italiano81032942015-07-17 06:18:36 +0000262 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000263 TDs[SymName] = TD;
264 }
David Majnemerf50d0a52015-02-27 00:43:58 +0000265 // Throw descriptors in the MS-ABI start with '_TI'
266 else if (SymName.startswith("_TI") || SymName.startswith("__TI")) {
267 ThrowInfo TI;
268 TI.Flags = *reinterpret_cast<const little32_t *>(SymContents.data());
269 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
270 SymName, TIEntries);
271 TIs[SymName] = TI;
272 }
273 // Catchable type arrays in the MS-ABI start with _CTA or __CTA.
274 else if (SymName.startswith("_CTA") || SymName.startswith("__CTA")) {
275 CatchableTypeArray CTA;
276 CTA.NumEntries =
277 *reinterpret_cast<const little32_t *>(SymContents.data());
278 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
279 SymName, CTAEntries);
280 CTAs[SymName] = CTA;
281 }
282 // Catchable types in the MS-ABI start with _CT or __CT.
283 else if (SymName.startswith("_CT") || SymName.startswith("__CT")) {
284 const little32_t *DataPtr =
285 reinterpret_cast<const little32_t *>(SymContents.data());
286 CatchableType CT;
287 CT.Flags = DataPtr[0];
288 CT.NonVirtualBaseAdjustmentOffset = DataPtr[2];
289 CT.VirtualBasePointerOffset = DataPtr[3];
290 CT.VirtualBaseAdjustmentOffset = DataPtr[4];
David Majnemer86ee1732015-02-27 22:35:25 +0000291 CT.Size = DataPtr[5];
David Majnemerf50d0a52015-02-27 00:43:58 +0000292 StringRef *I = std::begin(CT.Symbols), *E = std::end(CT.Symbols);
Davide Italiano81032942015-07-17 06:18:36 +0000293 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
David Majnemerf50d0a52015-02-27 00:43:58 +0000294 CTs[SymName] = CT;
295 }
David Majnemere2683612014-11-03 07:23:25 +0000296 // Construction vtables in the Itanium ABI start with '_ZTT' or '__ZTT'.
297 else if (SymName.startswith("_ZTT") || SymName.startswith("__ZTT")) {
298 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
299 SymName, VTTEntries);
300 }
301 // Typeinfo names in the Itanium ABI start with '_ZTS' or '__ZTS'.
302 else if (SymName.startswith("_ZTS") || SymName.startswith("__ZTS")) {
303 TINames[SymName] = SymContents.slice(0, SymContents.find('\0'));
304 }
305 // Vtables in the Itanium ABI start with '_ZTV' or '__ZTV'.
306 else if (SymName.startswith("_ZTV") || SymName.startswith("__ZTV")) {
307 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
308 SymName, VTableSymEntries);
309 for (uint64_t SymOffI = 0; SymOffI < SymSize; SymOffI += BytesInAddress) {
310 auto Key = std::make_pair(SymName, SymOffI);
311 if (VTableSymEntries.count(Key))
312 continue;
David Majnemerf45bbd02015-03-15 01:30:58 +0000313 const char *DataPtr =
314 SymContents.substr(SymOffI, BytesInAddress).data();
David Majnemere2683612014-11-03 07:23:25 +0000315 int64_t VData;
316 if (BytesInAddress == 8)
317 VData = *reinterpret_cast<const little64_t *>(DataPtr);
318 else
319 VData = *reinterpret_cast<const little32_t *>(DataPtr);
320 VTableDataEntries[Key] = VData;
321 }
322 }
323 // Typeinfo structures in the Itanium ABI start with '_ZTI' or '__ZTI'.
324 else if (SymName.startswith("_ZTI") || SymName.startswith("__ZTI")) {
325 // FIXME: Do something with these!
326 }
David Majnemer72ab1a52014-07-24 23:14:40 +0000327 }
David Majnemerf45bbd02015-03-15 01:30:58 +0000328 for (const auto &VFTableEntry : VFTableEntries) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000329 StringRef VFTableName = VFTableEntry.first.first;
330 uint64_t Offset = VFTableEntry.first.second;
331 StringRef SymName = VFTableEntry.second;
332 outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n';
333 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000334 for (const auto &VBTable : VBTables) {
David Majnemerbf32f772014-07-25 04:30:11 +0000335 StringRef VBTableName = VBTable.first;
David Majnemer72ab1a52014-07-24 23:14:40 +0000336 uint32_t Idx = 0;
David Majnemer6887a252014-09-26 08:01:23 +0000337 for (little32_t Offset : VBTable.second) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000338 outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n';
David Majnemerbf32f772014-07-25 04:30:11 +0000339 Idx += sizeof(Offset);
David Majnemer72ab1a52014-07-24 23:14:40 +0000340 }
341 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000342 for (const auto &COLPair : COLs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000343 StringRef COLName = COLPair.first;
344 const CompleteObjectLocator &COL = COLPair.second;
345 outs() << COLName << "[IsImageRelative]: " << COL.Data[0] << '\n';
346 outs() << COLName << "[OffsetToTop]: " << COL.Data[1] << '\n';
347 outs() << COLName << "[VFPtrOffset]: " << COL.Data[2] << '\n';
348 outs() << COLName << "[TypeDescriptor]: " << COL.Symbols[0] << '\n';
David Majnemerf45bbd02015-03-15 01:30:58 +0000349 outs() << COLName << "[ClassHierarchyDescriptor]: " << COL.Symbols[1]
350 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000351 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000352 for (const auto &CHDPair : CHDs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000353 StringRef CHDName = CHDPair.first;
354 const ClassHierarchyDescriptor &CHD = CHDPair.second;
355 outs() << CHDName << "[AlwaysZero]: " << CHD.Data[0] << '\n';
356 outs() << CHDName << "[Flags]: " << CHD.Data[1] << '\n';
357 outs() << CHDName << "[NumClasses]: " << CHD.Data[2] << '\n';
358 outs() << CHDName << "[BaseClassArray]: " << CHD.Symbols[0] << '\n';
359 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000360 for (const auto &BCAEntry : BCAEntries) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000361 StringRef BCAName = BCAEntry.first.first;
362 uint64_t Offset = BCAEntry.first.second;
363 StringRef SymName = BCAEntry.second;
364 outs() << BCAName << '[' << Offset << "]: " << SymName << '\n';
365 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000366 for (const auto &BCDPair : BCDs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000367 StringRef BCDName = BCDPair.first;
368 const BaseClassDescriptor &BCD = BCDPair.second;
369 outs() << BCDName << "[TypeDescriptor]: " << BCD.Symbols[0] << '\n';
370 outs() << BCDName << "[NumBases]: " << BCD.Data[0] << '\n';
371 outs() << BCDName << "[OffsetInVBase]: " << BCD.Data[1] << '\n';
372 outs() << BCDName << "[VBPtrOffset]: " << BCD.Data[2] << '\n';
373 outs() << BCDName << "[OffsetInVBTable]: " << BCD.Data[3] << '\n';
374 outs() << BCDName << "[Flags]: " << BCD.Data[4] << '\n';
David Majnemerf45bbd02015-03-15 01:30:58 +0000375 outs() << BCDName << "[ClassHierarchyDescriptor]: " << BCD.Symbols[1]
376 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000377 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000378 for (const auto &TDPair : TDs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000379 StringRef TDName = TDPair.first;
380 const TypeDescriptor &TD = TDPair.second;
381 outs() << TDName << "[VFPtr]: " << TD.Symbols[0] << '\n';
David Majnemer6887a252014-09-26 08:01:23 +0000382 outs() << TDName << "[AlwaysZero]: " << TD.AlwaysZero << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000383 outs() << TDName << "[MangledName]: ";
David Majnemer56167c32014-09-26 05:50:45 +0000384 outs().write_escaped(TD.MangledName.rtrim(StringRef("\0", 1)),
385 /*UseHexEscapes=*/true)
386 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000387 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000388 for (const auto &TIPair : TIs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000389 StringRef TIName = TIPair.first;
390 const ThrowInfo &TI = TIPair.second;
391 auto dumpThrowInfoFlag = [&](const char *Name, uint32_t Flag) {
392 outs() << TIName << "[Flags." << Name
393 << "]: " << (TI.Flags & Flag ? "true" : "false") << '\n';
394 };
395 auto dumpThrowInfoSymbol = [&](const char *Name, int Offset) {
396 outs() << TIName << '[' << Name << "]: ";
397 auto Entry = TIEntries.find(std::make_pair(TIName, Offset));
398 outs() << (Entry == TIEntries.end() ? "null" : Entry->second) << '\n';
399 };
400 outs() << TIName << "[Flags]: " << TI.Flags << '\n';
401 dumpThrowInfoFlag("Const", 1);
402 dumpThrowInfoFlag("Volatile", 2);
403 dumpThrowInfoSymbol("CleanupFn", 4);
404 dumpThrowInfoSymbol("ForwardCompat", 8);
405 dumpThrowInfoSymbol("CatchableTypeArray", 12);
406 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000407 for (const auto &CTAPair : CTAs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000408 StringRef CTAName = CTAPair.first;
409 const CatchableTypeArray &CTA = CTAPair.second;
410
411 outs() << CTAName << "[NumEntries]: " << CTA.NumEntries << '\n';
412
413 unsigned Idx = 0;
414 for (auto I = CTAEntries.lower_bound(std::make_pair(CTAName, 0)),
415 E = CTAEntries.upper_bound(std::make_pair(CTAName, UINT64_MAX));
416 I != E; ++I)
417 outs() << CTAName << '[' << Idx++ << "]: " << I->second << '\n';
418 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000419 for (const auto &CTPair : CTs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000420 StringRef CTName = CTPair.first;
421 const CatchableType &CT = CTPair.second;
422 auto dumpCatchableTypeFlag = [&](const char *Name, uint32_t Flag) {
423 outs() << CTName << "[Flags." << Name
424 << "]: " << (CT.Flags & Flag ? "true" : "false") << '\n';
425 };
426 outs() << CTName << "[Flags]: " << CT.Flags << '\n';
427 dumpCatchableTypeFlag("ScalarType", 1);
428 dumpCatchableTypeFlag("VirtualInheritance", 4);
429 outs() << CTName << "[TypeDescriptor]: " << CT.Symbols[0] << '\n';
430 outs() << CTName << "[NonVirtualBaseAdjustmentOffset]: "
431 << CT.NonVirtualBaseAdjustmentOffset << '\n';
432 outs() << CTName
433 << "[VirtualBasePointerOffset]: " << CT.VirtualBasePointerOffset
434 << '\n';
435 outs() << CTName << "[VirtualBaseAdjustmentOffset]: "
436 << CT.VirtualBaseAdjustmentOffset << '\n';
David Majnemer86ee1732015-02-27 22:35:25 +0000437 outs() << CTName << "[Size]: " << CT.Size << '\n';
David Majnemerf50d0a52015-02-27 00:43:58 +0000438 outs() << CTName
439 << "[CopyCtor]: " << (CT.Symbols[1].empty() ? "null" : CT.Symbols[1])
440 << '\n';
441 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000442 for (const auto &VTTPair : VTTEntries) {
David Majnemere2683612014-11-03 07:23:25 +0000443 StringRef VTTName = VTTPair.first.first;
444 uint64_t VTTOffset = VTTPair.first.second;
445 StringRef VTTEntry = VTTPair.second;
446 outs() << VTTName << '[' << VTTOffset << "]: " << VTTEntry << '\n';
447 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000448 for (const auto &TIPair : TINames) {
David Majnemere2683612014-11-03 07:23:25 +0000449 StringRef TIName = TIPair.first;
450 outs() << TIName << ": " << TIPair.second << '\n';
451 }
452 auto VTableSymI = VTableSymEntries.begin();
453 auto VTableSymE = VTableSymEntries.end();
454 auto VTableDataI = VTableDataEntries.begin();
455 auto VTableDataE = VTableDataEntries.end();
456 for (;;) {
457 bool SymDone = VTableSymI == VTableSymE;
458 bool DataDone = VTableDataI == VTableDataE;
459 if (SymDone && DataDone)
460 break;
461 if (!SymDone && (DataDone || VTableSymI->first < VTableDataI->first)) {
462 StringRef VTableName = VTableSymI->first.first;
463 uint64_t Offset = VTableSymI->first.second;
464 StringRef VTableEntry = VTableSymI->second;
465 outs() << VTableName << '[' << Offset << "]: ";
466 outs() << VTableEntry;
467 outs() << '\n';
468 ++VTableSymI;
469 continue;
470 }
471 if (!DataDone && (SymDone || VTableDataI->first < VTableSymI->first)) {
472 StringRef VTableName = VTableDataI->first.first;
473 uint64_t Offset = VTableDataI->first.second;
474 int64_t VTableEntry = VTableDataI->second;
475 outs() << VTableName << '[' << Offset << "]: ";
476 outs() << VTableEntry;
477 outs() << '\n';
478 ++VTableDataI;
479 continue;
480 }
481 }
David Majnemer72ab1a52014-07-24 23:14:40 +0000482}
483
484static void dumpArchive(const Archive *Arc) {
Lang Hamesae610ab2016-07-14 00:37:04 +0000485 for (auto &ErrorOrChild : Arc->children()) {
486 error(ErrorOrChild.getError());
487 const Archive::Child &ArcC = *ErrorOrChild;
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000488 Expected<std::unique_ptr<Binary>> ChildOrErr = ArcC.getAsBinary();
489 if (!ChildOrErr) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000490 // Ignore non-object files.
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000491 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) {
492 std::string Buf;
493 raw_string_ostream OS(Buf);
494 logAllUnhandledErrors(std::move(E), OS, "");
495 OS.flush();
496 reportError(Arc->getFileName(), Buf);
497 }
498 ChildOrErr.takeError();
David Majnemer72ab1a52014-07-24 23:14:40 +0000499 continue;
500 }
501
502 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
David Majnemerf45bbd02015-03-15 01:30:58 +0000503 dumpCXXData(Obj);
David Majnemer72ab1a52014-07-24 23:14:40 +0000504 else
David Majnemerf45bbd02015-03-15 01:30:58 +0000505 reportError(Arc->getFileName(), cxxdump_error::unrecognized_file_format);
David Majnemer72ab1a52014-07-24 23:14:40 +0000506 }
507}
508
509static void dumpInput(StringRef File) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000510 // Attempt to open the binary.
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000511 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
512 if (!BinaryOrErr) {
513 auto EC = errorToErrorCode(BinaryOrErr.takeError());
David Majnemer72ab1a52014-07-24 23:14:40 +0000514 reportError(File, EC);
515 return;
516 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000517 Binary &Binary = *BinaryOrErr.get().getBinary();
David Majnemer72ab1a52014-07-24 23:14:40 +0000518
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000519 if (Archive *Arc = dyn_cast<Archive>(&Binary))
David Majnemer72ab1a52014-07-24 23:14:40 +0000520 dumpArchive(Arc);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000521 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
David Majnemerf45bbd02015-03-15 01:30:58 +0000522 dumpCXXData(Obj);
David Majnemer72ab1a52014-07-24 23:14:40 +0000523 else
David Majnemerf45bbd02015-03-15 01:30:58 +0000524 reportError(File, cxxdump_error::unrecognized_file_format);
David Majnemer72ab1a52014-07-24 23:14:40 +0000525}
526
527int main(int argc, const char *argv[]) {
Richard Smith2ad6d482016-06-09 00:53:21 +0000528 sys::PrintStackTraceOnErrorSignal(argv[0]);
David Majnemer72ab1a52014-07-24 23:14:40 +0000529 PrettyStackTraceProgram X(argc, argv);
530 llvm_shutdown_obj Y;
531
532 // Initialize targets.
533 llvm::InitializeAllTargetInfos();
534
535 // Register the target printer for --version.
536 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
537
David Majnemerf45bbd02015-03-15 01:30:58 +0000538 cl::ParseCommandLineOptions(argc, argv, "LLVM C++ ABI Data Dumper\n");
David Majnemer72ab1a52014-07-24 23:14:40 +0000539
540 // Default to stdin if no filename is specified.
541 if (opts::InputFilenames.size() == 0)
542 opts::InputFilenames.push_back("-");
543
544 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
545 dumpInput);
546
Davide Italiano81032942015-07-17 06:18:36 +0000547 return EXIT_SUCCESS;
David Majnemer72ab1a52014-07-24 23:14:40 +0000548}