blob: c92d20d6ccf170d719e5de3a79b1163b2e7e8cbd [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
Lang Hamesfc209622016-07-14 02:24:01 +000053static void error(Error Err) {
54 if (Err) {
55 logAllUnhandledErrors(std::move(Err), outs(), "Error reading file: ");
56 outs().flush();
57 exit(1);
58 }
59}
60
David Majnemer72ab1a52014-07-24 23:14:40 +000061} // namespace llvm
62
63static void reportError(StringRef Input, StringRef Message) {
64 if (Input == "-")
65 Input = "<stdin>";
David Majnemer72ab1a52014-07-24 23:14:40 +000066 errs() << Input << ": " << Message << "\n";
67 errs().flush();
Davide Italiano81032942015-07-17 06:18:36 +000068 exit(1);
David Majnemer72ab1a52014-07-24 23:14:40 +000069}
70
71static void reportError(StringRef Input, std::error_code EC) {
72 reportError(Input, EC.message());
73}
74
David Majnemerd7fafa02015-08-13 18:31:43 +000075static std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
David Majnemere2683612014-11-03 07:23:25 +000076
Davide Italiano81032942015-07-17 06:18:36 +000077static void collectRelocatedSymbols(const ObjectFile *Obj,
David Majnemere2683612014-11-03 07:23:25 +000078 const SectionRef &Sec, uint64_t SecAddress,
79 uint64_t SymAddress, uint64_t SymSize,
80 StringRef *I, StringRef *E) {
81 uint64_t SymOffset = SymAddress - SecAddress;
82 uint64_t SymEnd = SymOffset + SymSize;
David Majnemerd7fafa02015-08-13 18:31:43 +000083 for (const SectionRef &SR : SectionRelocMap[Sec]) {
David Majnemere2683612014-11-03 07:23:25 +000084 for (const object::RelocationRef &Reloc : SR.relocations()) {
85 if (I == E)
86 break;
87 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
88 if (RelocSymI == Obj->symbol_end())
89 continue;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +000090 Expected<StringRef> RelocSymName = RelocSymI->getName();
91 error(errorToErrorCode(RelocSymName.takeError()));
Rafael Espindola96d071c2015-06-29 23:29:12 +000092 uint64_t Offset = Reloc.getOffset();
David Majnemere2683612014-11-03 07:23:25 +000093 if (Offset >= SymOffset && Offset < SymEnd) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +000094 *I = *RelocSymName;
David Majnemere2683612014-11-03 07:23:25 +000095 ++I;
96 }
97 }
David Majnemer1ac52eb2014-09-26 04:21:51 +000098 }
David Majnemer1ac52eb2014-09-26 04:21:51 +000099}
100
Davide Italiano81032942015-07-17 06:18:36 +0000101static void collectRelocationOffsets(
David Majnemere2683612014-11-03 07:23:25 +0000102 const ObjectFile *Obj, const SectionRef &Sec, uint64_t SecAddress,
103 uint64_t SymAddress, uint64_t SymSize, StringRef SymName,
David Majnemer1ac52eb2014-09-26 04:21:51 +0000104 std::map<std::pair<StringRef, uint64_t>, StringRef> &Collection) {
David Majnemere2683612014-11-03 07:23:25 +0000105 uint64_t SymOffset = SymAddress - SecAddress;
106 uint64_t SymEnd = SymOffset + SymSize;
David Majnemerd7fafa02015-08-13 18:31:43 +0000107 for (const SectionRef &SR : SectionRelocMap[Sec]) {
David Majnemere2683612014-11-03 07:23:25 +0000108 for (const object::RelocationRef &Reloc : SR.relocations()) {
109 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
110 if (RelocSymI == Obj->symbol_end())
111 continue;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000112 Expected<StringRef> RelocSymName = RelocSymI->getName();
113 error(errorToErrorCode(RelocSymName.takeError()));
Rafael Espindola96d071c2015-06-29 23:29:12 +0000114 uint64_t Offset = Reloc.getOffset();
David Majnemere2683612014-11-03 07:23:25 +0000115 if (Offset >= SymOffset && Offset < SymEnd)
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000116 Collection[std::make_pair(SymName, Offset - SymOffset)] = *RelocSymName;
David Majnemere2683612014-11-03 07:23:25 +0000117 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000118 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000119}
120
David Majnemerf45bbd02015-03-15 01:30:58 +0000121static void dumpCXXData(const ObjectFile *Obj) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000122 struct CompleteObjectLocator {
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 ClassHierarchyDescriptor {
127 StringRef Symbols[1];
David Majnemer6887a252014-09-26 08:01:23 +0000128 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000129 };
130 struct BaseClassDescriptor {
131 StringRef Symbols[2];
David Majnemer6887a252014-09-26 08:01:23 +0000132 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000133 };
134 struct TypeDescriptor {
135 StringRef Symbols[1];
David Majnemer6887a252014-09-26 08:01:23 +0000136 uint64_t AlwaysZero;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000137 StringRef MangledName;
138 };
David Majnemerf50d0a52015-02-27 00:43:58 +0000139 struct ThrowInfo {
140 uint32_t Flags;
141 };
142 struct CatchableTypeArray {
143 uint32_t NumEntries;
144 };
145 struct CatchableType {
146 uint32_t Flags;
147 uint32_t NonVirtualBaseAdjustmentOffset;
148 int32_t VirtualBasePointerOffset;
149 uint32_t VirtualBaseAdjustmentOffset;
David Majnemer86ee1732015-02-27 22:35:25 +0000150 uint32_t Size;
David Majnemerf50d0a52015-02-27 00:43:58 +0000151 StringRef Symbols[2];
152 };
David Majnemer72ab1a52014-07-24 23:14:40 +0000153 std::map<std::pair<StringRef, uint64_t>, StringRef> VFTableEntries;
David Majnemerf50d0a52015-02-27 00:43:58 +0000154 std::map<std::pair<StringRef, uint64_t>, StringRef> TIEntries;
155 std::map<std::pair<StringRef, uint64_t>, StringRef> CTAEntries;
David Majnemer6887a252014-09-26 08:01:23 +0000156 std::map<StringRef, ArrayRef<little32_t>> VBTables;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000157 std::map<StringRef, CompleteObjectLocator> COLs;
158 std::map<StringRef, ClassHierarchyDescriptor> CHDs;
159 std::map<std::pair<StringRef, uint64_t>, StringRef> BCAEntries;
160 std::map<StringRef, BaseClassDescriptor> BCDs;
161 std::map<StringRef, TypeDescriptor> TDs;
David Majnemerf50d0a52015-02-27 00:43:58 +0000162 std::map<StringRef, ThrowInfo> TIs;
163 std::map<StringRef, CatchableTypeArray> CTAs;
164 std::map<StringRef, CatchableType> CTs;
David Majnemere2683612014-11-03 07:23:25 +0000165
166 std::map<std::pair<StringRef, uint64_t>, StringRef> VTableSymEntries;
167 std::map<std::pair<StringRef, uint64_t>, int64_t> VTableDataEntries;
168 std::map<std::pair<StringRef, uint64_t>, StringRef> VTTEntries;
169 std::map<StringRef, StringRef> TINames;
170
David Majnemerd7fafa02015-08-13 18:31:43 +0000171 SectionRelocMap.clear();
172 for (const SectionRef &Section : Obj->sections()) {
173 section_iterator Sec2 = Section.getRelocatedSection();
174 if (Sec2 != Obj->section_end())
175 SectionRelocMap[*Sec2].push_back(Section);
176 }
177
David Majnemere2683612014-11-03 07:23:25 +0000178 uint8_t BytesInAddress = Obj->getBytesInAddress();
179
Rafael Espindola6bf32212015-06-24 19:57:32 +0000180 std::vector<std::pair<SymbolRef, uint64_t>> SymAddr =
Rafael Espindolaa4a40932015-06-23 02:20:37 +0000181 object::computeSymbolSizes(*Obj);
Rafael Espindolaa4a40932015-06-23 02:20:37 +0000182
Rafael Espindola6bf32212015-06-24 19:57:32 +0000183 for (auto &P : SymAddr) {
Rafael Espindolaa4a40932015-06-23 02:20:37 +0000184 object::SymbolRef Sym = P.first;
185 uint64_t SymSize = P.second;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000186 Expected<StringRef> SymNameOrErr = Sym.getName();
187 error(errorToErrorCode(SymNameOrErr.takeError()));
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000188 StringRef SymName = *SymNameOrErr;
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000189 Expected<object::section_iterator> SecIOrErr = Sym.getSection();
190 error(errorToErrorCode(SecIOrErr.takeError()));
Rafael Espindola8bab8892015-08-07 23:27:14 +0000191 object::section_iterator SecI = *SecIOrErr;
David Majnemer601327c2014-09-26 22:32:19 +0000192 // Skip external symbols.
193 if (SecI == Obj->section_end())
194 continue;
David Majnemere2683612014-11-03 07:23:25 +0000195 const SectionRef &Sec = *SecI;
David Majnemer601327c2014-09-26 22:32:19 +0000196 // Skip virtual or BSS sections.
David Majnemere2683612014-11-03 07:23:25 +0000197 if (Sec.isBSS() || Sec.isVirtual())
David Majnemer601327c2014-09-26 22:32:19 +0000198 continue;
199 StringRef SecContents;
Davide Italiano81032942015-07-17 06:18:36 +0000200 error(Sec.getContents(SecContents));
Kevin Enderby931cb652016-06-24 18:24:42 +0000201 Expected<uint64_t> SymAddressOrErr = Sym.getAddress();
202 error(errorToErrorCode(SymAddressOrErr.takeError()));
Rafael Espindolaed067c42015-07-03 18:19:00 +0000203 uint64_t SymAddress = *SymAddressOrErr;
David Majnemere2683612014-11-03 07:23:25 +0000204 uint64_t SecAddress = Sec.getAddress();
205 uint64_t SecSize = Sec.getSize();
206 uint64_t SymOffset = SymAddress - SecAddress;
207 StringRef SymContents = SecContents.substr(SymOffset, SymSize);
208
David Majnemer72ab1a52014-07-24 23:14:40 +0000209 // VFTables in the MS-ABI start with '??_7' and are contained within their
210 // own COMDAT section. We then determine the contents of the VFTable by
211 // looking at each relocation in the section.
212 if (SymName.startswith("??_7")) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000213 // Each relocation either names a virtual method or a thunk. We note the
214 // offset into the section and the symbol used for the relocation.
David Majnemere2683612014-11-03 07:23:25 +0000215 collectRelocationOffsets(Obj, Sec, SecAddress, SecAddress, SecSize,
216 SymName, VFTableEntries);
David Majnemer72ab1a52014-07-24 23:14:40 +0000217 }
218 // VBTables in the MS-ABI start with '??_8' and are filled with 32-bit
219 // offsets of virtual bases.
220 else if (SymName.startswith("??_8")) {
David Majnemer6887a252014-09-26 08:01:23 +0000221 ArrayRef<little32_t> VBTableData(
David Majnemere2683612014-11-03 07:23:25 +0000222 reinterpret_cast<const little32_t *>(SymContents.data()),
223 SymContents.size() / sizeof(little32_t));
David Majnemer72ab1a52014-07-24 23:14:40 +0000224 VBTables[SymName] = VBTableData;
225 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000226 // Complete object locators in the MS-ABI start with '??_R4'
227 else if (SymName.startswith("??_R4")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000228 CompleteObjectLocator COL;
Craig Topper0013be12015-09-21 05:32:41 +0000229 COL.Data = makeArrayRef(
David Majnemere2683612014-11-03 07:23:25 +0000230 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000231 StringRef *I = std::begin(COL.Symbols), *E = std::end(COL.Symbols);
Davide Italiano81032942015-07-17 06:18:36 +0000232 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000233 COLs[SymName] = COL;
234 }
235 // Class hierarchy descriptors in the MS-ABI start with '??_R3'
236 else if (SymName.startswith("??_R3")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000237 ClassHierarchyDescriptor CHD;
Craig Topper0013be12015-09-21 05:32:41 +0000238 CHD.Data = makeArrayRef(
David Majnemere2683612014-11-03 07:23:25 +0000239 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000240 StringRef *I = std::begin(CHD.Symbols), *E = std::end(CHD.Symbols);
Davide Italiano81032942015-07-17 06:18:36 +0000241 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000242 CHDs[SymName] = CHD;
243 }
244 // Class hierarchy descriptors in the MS-ABI start with '??_R2'
245 else if (SymName.startswith("??_R2")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000246 // Each relocation names a base class descriptor. We note the offset into
247 // the section and the symbol used for the relocation.
David Majnemere2683612014-11-03 07:23:25 +0000248 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
249 SymName, BCAEntries);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000250 }
251 // Base class descriptors in the MS-ABI start with '??_R1'
252 else if (SymName.startswith("??_R1")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000253 BaseClassDescriptor BCD;
Craig Topper0013be12015-09-21 05:32:41 +0000254 BCD.Data = makeArrayRef(
David Majnemere2683612014-11-03 07:23:25 +0000255 reinterpret_cast<const little32_t *>(SymContents.data()) + 1, 5);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000256 StringRef *I = std::begin(BCD.Symbols), *E = std::end(BCD.Symbols);
Davide Italiano81032942015-07-17 06:18:36 +0000257 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000258 BCDs[SymName] = BCD;
259 }
260 // Type descriptors in the MS-ABI start with '??_R0'
261 else if (SymName.startswith("??_R0")) {
David Majnemere2683612014-11-03 07:23:25 +0000262 const char *DataPtr = SymContents.drop_front(BytesInAddress).data();
David Majnemer1ac52eb2014-09-26 04:21:51 +0000263 TypeDescriptor TD;
David Majnemer6887a252014-09-26 08:01:23 +0000264 if (BytesInAddress == 8)
265 TD.AlwaysZero = *reinterpret_cast<const little64_t *>(DataPtr);
266 else
267 TD.AlwaysZero = *reinterpret_cast<const little32_t *>(DataPtr);
David Majnemere2683612014-11-03 07:23:25 +0000268 TD.MangledName = SymContents.drop_front(BytesInAddress * 2);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000269 StringRef *I = std::begin(TD.Symbols), *E = std::end(TD.Symbols);
Davide Italiano81032942015-07-17 06:18:36 +0000270 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000271 TDs[SymName] = TD;
272 }
David Majnemerf50d0a52015-02-27 00:43:58 +0000273 // Throw descriptors in the MS-ABI start with '_TI'
274 else if (SymName.startswith("_TI") || SymName.startswith("__TI")) {
275 ThrowInfo TI;
276 TI.Flags = *reinterpret_cast<const little32_t *>(SymContents.data());
277 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
278 SymName, TIEntries);
279 TIs[SymName] = TI;
280 }
281 // Catchable type arrays in the MS-ABI start with _CTA or __CTA.
282 else if (SymName.startswith("_CTA") || SymName.startswith("__CTA")) {
283 CatchableTypeArray CTA;
284 CTA.NumEntries =
285 *reinterpret_cast<const little32_t *>(SymContents.data());
286 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
287 SymName, CTAEntries);
288 CTAs[SymName] = CTA;
289 }
290 // Catchable types in the MS-ABI start with _CT or __CT.
291 else if (SymName.startswith("_CT") || SymName.startswith("__CT")) {
292 const little32_t *DataPtr =
293 reinterpret_cast<const little32_t *>(SymContents.data());
294 CatchableType CT;
295 CT.Flags = DataPtr[0];
296 CT.NonVirtualBaseAdjustmentOffset = DataPtr[2];
297 CT.VirtualBasePointerOffset = DataPtr[3];
298 CT.VirtualBaseAdjustmentOffset = DataPtr[4];
David Majnemer86ee1732015-02-27 22:35:25 +0000299 CT.Size = DataPtr[5];
David Majnemerf50d0a52015-02-27 00:43:58 +0000300 StringRef *I = std::begin(CT.Symbols), *E = std::end(CT.Symbols);
Davide Italiano81032942015-07-17 06:18:36 +0000301 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
David Majnemerf50d0a52015-02-27 00:43:58 +0000302 CTs[SymName] = CT;
303 }
David Majnemere2683612014-11-03 07:23:25 +0000304 // Construction vtables in the Itanium ABI start with '_ZTT' or '__ZTT'.
305 else if (SymName.startswith("_ZTT") || SymName.startswith("__ZTT")) {
306 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
307 SymName, VTTEntries);
308 }
309 // Typeinfo names in the Itanium ABI start with '_ZTS' or '__ZTS'.
310 else if (SymName.startswith("_ZTS") || SymName.startswith("__ZTS")) {
311 TINames[SymName] = SymContents.slice(0, SymContents.find('\0'));
312 }
313 // Vtables in the Itanium ABI start with '_ZTV' or '__ZTV'.
314 else if (SymName.startswith("_ZTV") || SymName.startswith("__ZTV")) {
315 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
316 SymName, VTableSymEntries);
317 for (uint64_t SymOffI = 0; SymOffI < SymSize; SymOffI += BytesInAddress) {
318 auto Key = std::make_pair(SymName, SymOffI);
319 if (VTableSymEntries.count(Key))
320 continue;
David Majnemerf45bbd02015-03-15 01:30:58 +0000321 const char *DataPtr =
322 SymContents.substr(SymOffI, BytesInAddress).data();
David Majnemere2683612014-11-03 07:23:25 +0000323 int64_t VData;
324 if (BytesInAddress == 8)
325 VData = *reinterpret_cast<const little64_t *>(DataPtr);
326 else
327 VData = *reinterpret_cast<const little32_t *>(DataPtr);
328 VTableDataEntries[Key] = VData;
329 }
330 }
331 // Typeinfo structures in the Itanium ABI start with '_ZTI' or '__ZTI'.
332 else if (SymName.startswith("_ZTI") || SymName.startswith("__ZTI")) {
333 // FIXME: Do something with these!
334 }
David Majnemer72ab1a52014-07-24 23:14:40 +0000335 }
David Majnemerf45bbd02015-03-15 01:30:58 +0000336 for (const auto &VFTableEntry : VFTableEntries) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000337 StringRef VFTableName = VFTableEntry.first.first;
338 uint64_t Offset = VFTableEntry.first.second;
339 StringRef SymName = VFTableEntry.second;
340 outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n';
341 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000342 for (const auto &VBTable : VBTables) {
David Majnemerbf32f772014-07-25 04:30:11 +0000343 StringRef VBTableName = VBTable.first;
David Majnemer72ab1a52014-07-24 23:14:40 +0000344 uint32_t Idx = 0;
David Majnemer6887a252014-09-26 08:01:23 +0000345 for (little32_t Offset : VBTable.second) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000346 outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n';
David Majnemerbf32f772014-07-25 04:30:11 +0000347 Idx += sizeof(Offset);
David Majnemer72ab1a52014-07-24 23:14:40 +0000348 }
349 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000350 for (const auto &COLPair : COLs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000351 StringRef COLName = COLPair.first;
352 const CompleteObjectLocator &COL = COLPair.second;
353 outs() << COLName << "[IsImageRelative]: " << COL.Data[0] << '\n';
354 outs() << COLName << "[OffsetToTop]: " << COL.Data[1] << '\n';
355 outs() << COLName << "[VFPtrOffset]: " << COL.Data[2] << '\n';
356 outs() << COLName << "[TypeDescriptor]: " << COL.Symbols[0] << '\n';
David Majnemerf45bbd02015-03-15 01:30:58 +0000357 outs() << COLName << "[ClassHierarchyDescriptor]: " << COL.Symbols[1]
358 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000359 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000360 for (const auto &CHDPair : CHDs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000361 StringRef CHDName = CHDPair.first;
362 const ClassHierarchyDescriptor &CHD = CHDPair.second;
363 outs() << CHDName << "[AlwaysZero]: " << CHD.Data[0] << '\n';
364 outs() << CHDName << "[Flags]: " << CHD.Data[1] << '\n';
365 outs() << CHDName << "[NumClasses]: " << CHD.Data[2] << '\n';
366 outs() << CHDName << "[BaseClassArray]: " << CHD.Symbols[0] << '\n';
367 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000368 for (const auto &BCAEntry : BCAEntries) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000369 StringRef BCAName = BCAEntry.first.first;
370 uint64_t Offset = BCAEntry.first.second;
371 StringRef SymName = BCAEntry.second;
372 outs() << BCAName << '[' << Offset << "]: " << SymName << '\n';
373 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000374 for (const auto &BCDPair : BCDs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000375 StringRef BCDName = BCDPair.first;
376 const BaseClassDescriptor &BCD = BCDPair.second;
377 outs() << BCDName << "[TypeDescriptor]: " << BCD.Symbols[0] << '\n';
378 outs() << BCDName << "[NumBases]: " << BCD.Data[0] << '\n';
379 outs() << BCDName << "[OffsetInVBase]: " << BCD.Data[1] << '\n';
380 outs() << BCDName << "[VBPtrOffset]: " << BCD.Data[2] << '\n';
381 outs() << BCDName << "[OffsetInVBTable]: " << BCD.Data[3] << '\n';
382 outs() << BCDName << "[Flags]: " << BCD.Data[4] << '\n';
David Majnemerf45bbd02015-03-15 01:30:58 +0000383 outs() << BCDName << "[ClassHierarchyDescriptor]: " << BCD.Symbols[1]
384 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000385 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000386 for (const auto &TDPair : TDs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000387 StringRef TDName = TDPair.first;
388 const TypeDescriptor &TD = TDPair.second;
389 outs() << TDName << "[VFPtr]: " << TD.Symbols[0] << '\n';
David Majnemer6887a252014-09-26 08:01:23 +0000390 outs() << TDName << "[AlwaysZero]: " << TD.AlwaysZero << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000391 outs() << TDName << "[MangledName]: ";
David Majnemer56167c32014-09-26 05:50:45 +0000392 outs().write_escaped(TD.MangledName.rtrim(StringRef("\0", 1)),
393 /*UseHexEscapes=*/true)
394 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000395 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000396 for (const auto &TIPair : TIs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000397 StringRef TIName = TIPair.first;
398 const ThrowInfo &TI = TIPair.second;
399 auto dumpThrowInfoFlag = [&](const char *Name, uint32_t Flag) {
400 outs() << TIName << "[Flags." << Name
401 << "]: " << (TI.Flags & Flag ? "true" : "false") << '\n';
402 };
403 auto dumpThrowInfoSymbol = [&](const char *Name, int Offset) {
404 outs() << TIName << '[' << Name << "]: ";
405 auto Entry = TIEntries.find(std::make_pair(TIName, Offset));
406 outs() << (Entry == TIEntries.end() ? "null" : Entry->second) << '\n';
407 };
408 outs() << TIName << "[Flags]: " << TI.Flags << '\n';
409 dumpThrowInfoFlag("Const", 1);
410 dumpThrowInfoFlag("Volatile", 2);
411 dumpThrowInfoSymbol("CleanupFn", 4);
412 dumpThrowInfoSymbol("ForwardCompat", 8);
413 dumpThrowInfoSymbol("CatchableTypeArray", 12);
414 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000415 for (const auto &CTAPair : CTAs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000416 StringRef CTAName = CTAPair.first;
417 const CatchableTypeArray &CTA = CTAPair.second;
418
419 outs() << CTAName << "[NumEntries]: " << CTA.NumEntries << '\n';
420
421 unsigned Idx = 0;
422 for (auto I = CTAEntries.lower_bound(std::make_pair(CTAName, 0)),
423 E = CTAEntries.upper_bound(std::make_pair(CTAName, UINT64_MAX));
424 I != E; ++I)
425 outs() << CTAName << '[' << Idx++ << "]: " << I->second << '\n';
426 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000427 for (const auto &CTPair : CTs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000428 StringRef CTName = CTPair.first;
429 const CatchableType &CT = CTPair.second;
430 auto dumpCatchableTypeFlag = [&](const char *Name, uint32_t Flag) {
431 outs() << CTName << "[Flags." << Name
432 << "]: " << (CT.Flags & Flag ? "true" : "false") << '\n';
433 };
434 outs() << CTName << "[Flags]: " << CT.Flags << '\n';
435 dumpCatchableTypeFlag("ScalarType", 1);
436 dumpCatchableTypeFlag("VirtualInheritance", 4);
437 outs() << CTName << "[TypeDescriptor]: " << CT.Symbols[0] << '\n';
438 outs() << CTName << "[NonVirtualBaseAdjustmentOffset]: "
439 << CT.NonVirtualBaseAdjustmentOffset << '\n';
440 outs() << CTName
441 << "[VirtualBasePointerOffset]: " << CT.VirtualBasePointerOffset
442 << '\n';
443 outs() << CTName << "[VirtualBaseAdjustmentOffset]: "
444 << CT.VirtualBaseAdjustmentOffset << '\n';
David Majnemer86ee1732015-02-27 22:35:25 +0000445 outs() << CTName << "[Size]: " << CT.Size << '\n';
David Majnemerf50d0a52015-02-27 00:43:58 +0000446 outs() << CTName
447 << "[CopyCtor]: " << (CT.Symbols[1].empty() ? "null" : CT.Symbols[1])
448 << '\n';
449 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000450 for (const auto &VTTPair : VTTEntries) {
David Majnemere2683612014-11-03 07:23:25 +0000451 StringRef VTTName = VTTPair.first.first;
452 uint64_t VTTOffset = VTTPair.first.second;
453 StringRef VTTEntry = VTTPair.second;
454 outs() << VTTName << '[' << VTTOffset << "]: " << VTTEntry << '\n';
455 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000456 for (const auto &TIPair : TINames) {
David Majnemere2683612014-11-03 07:23:25 +0000457 StringRef TIName = TIPair.first;
458 outs() << TIName << ": " << TIPair.second << '\n';
459 }
460 auto VTableSymI = VTableSymEntries.begin();
461 auto VTableSymE = VTableSymEntries.end();
462 auto VTableDataI = VTableDataEntries.begin();
463 auto VTableDataE = VTableDataEntries.end();
464 for (;;) {
465 bool SymDone = VTableSymI == VTableSymE;
466 bool DataDone = VTableDataI == VTableDataE;
467 if (SymDone && DataDone)
468 break;
469 if (!SymDone && (DataDone || VTableSymI->first < VTableDataI->first)) {
470 StringRef VTableName = VTableSymI->first.first;
471 uint64_t Offset = VTableSymI->first.second;
472 StringRef VTableEntry = VTableSymI->second;
473 outs() << VTableName << '[' << Offset << "]: ";
474 outs() << VTableEntry;
475 outs() << '\n';
476 ++VTableSymI;
477 continue;
478 }
479 if (!DataDone && (SymDone || VTableDataI->first < VTableSymI->first)) {
480 StringRef VTableName = VTableDataI->first.first;
481 uint64_t Offset = VTableDataI->first.second;
482 int64_t VTableEntry = VTableDataI->second;
483 outs() << VTableName << '[' << Offset << "]: ";
484 outs() << VTableEntry;
485 outs() << '\n';
486 ++VTableDataI;
487 continue;
488 }
489 }
David Majnemer72ab1a52014-07-24 23:14:40 +0000490}
491
492static void dumpArchive(const Archive *Arc) {
Lang Hamesfc209622016-07-14 02:24:01 +0000493 Error Err;
494 for (auto &ArcC : Arc->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000495 Expected<std::unique_ptr<Binary>> ChildOrErr = ArcC.getAsBinary();
496 if (!ChildOrErr) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000497 // Ignore non-object files.
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000498 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) {
499 std::string Buf;
500 raw_string_ostream OS(Buf);
501 logAllUnhandledErrors(std::move(E), OS, "");
502 OS.flush();
503 reportError(Arc->getFileName(), Buf);
504 }
505 ChildOrErr.takeError();
David Majnemer72ab1a52014-07-24 23:14:40 +0000506 continue;
507 }
508
509 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
David Majnemerf45bbd02015-03-15 01:30:58 +0000510 dumpCXXData(Obj);
David Majnemer72ab1a52014-07-24 23:14:40 +0000511 else
David Majnemerf45bbd02015-03-15 01:30:58 +0000512 reportError(Arc->getFileName(), cxxdump_error::unrecognized_file_format);
David Majnemer72ab1a52014-07-24 23:14:40 +0000513 }
Lang Hamesfc209622016-07-14 02:24:01 +0000514 error(std::move(Err));
David Majnemer72ab1a52014-07-24 23:14:40 +0000515}
516
517static void dumpInput(StringRef File) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000518 // Attempt to open the binary.
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000519 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
520 if (!BinaryOrErr) {
521 auto EC = errorToErrorCode(BinaryOrErr.takeError());
David Majnemer72ab1a52014-07-24 23:14:40 +0000522 reportError(File, EC);
523 return;
524 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000525 Binary &Binary = *BinaryOrErr.get().getBinary();
David Majnemer72ab1a52014-07-24 23:14:40 +0000526
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000527 if (Archive *Arc = dyn_cast<Archive>(&Binary))
David Majnemer72ab1a52014-07-24 23:14:40 +0000528 dumpArchive(Arc);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000529 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
David Majnemerf45bbd02015-03-15 01:30:58 +0000530 dumpCXXData(Obj);
David Majnemer72ab1a52014-07-24 23:14:40 +0000531 else
David Majnemerf45bbd02015-03-15 01:30:58 +0000532 reportError(File, cxxdump_error::unrecognized_file_format);
David Majnemer72ab1a52014-07-24 23:14:40 +0000533}
534
535int main(int argc, const char *argv[]) {
Richard Smith2ad6d482016-06-09 00:53:21 +0000536 sys::PrintStackTraceOnErrorSignal(argv[0]);
David Majnemer72ab1a52014-07-24 23:14:40 +0000537 PrettyStackTraceProgram X(argc, argv);
538 llvm_shutdown_obj Y;
539
540 // Initialize targets.
541 llvm::InitializeAllTargetInfos();
542
543 // Register the target printer for --version.
544 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
545
David Majnemerf45bbd02015-03-15 01:30:58 +0000546 cl::ParseCommandLineOptions(argc, argv, "LLVM C++ ABI Data Dumper\n");
David Majnemer72ab1a52014-07-24 23:14:40 +0000547
548 // Default to stdin if no filename is specified.
549 if (opts::InputFilenames.size() == 0)
550 opts::InputFilenames.push_back("-");
551
552 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
553 dumpInput);
554
Davide Italiano81032942015-07-17 06:18:36 +0000555 return EXIT_SUCCESS;
David Majnemer72ab1a52014-07-24 23:14:40 +0000556}