blob: 09e40d9b0db75751264e3284a1abc5570c17c4d3 [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"
Rui Ueyama197194b2018-04-13 18:26:06 +000023#include "llvm/Support/InitLLVM.h"
David Majnemer72ab1a52014-07-24 23:14:40 +000024#include "llvm/Support/TargetRegistry.h"
25#include "llvm/Support/TargetSelect.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000026#include "llvm/Support/raw_ostream.h"
David Majnemer72ab1a52014-07-24 23:14:40 +000027#include <map>
28#include <string>
29#include <system_error>
30
31using namespace llvm;
32using namespace llvm::object;
33using namespace llvm::support;
34
35namespace opts {
36cl::list<std::string> InputFilenames(cl::Positional,
37 cl::desc("<input object files>"),
38 cl::ZeroOrMore);
39} // namespace opts
40
David Majnemer72ab1a52014-07-24 23:14:40 +000041namespace llvm {
42
Davide Italiano81032942015-07-17 06:18:36 +000043static void error(std::error_code EC) {
David Majnemer72ab1a52014-07-24 23:14:40 +000044 if (!EC)
Davide Italiano81032942015-07-17 06:18:36 +000045 return;
David Majnemer72ab1a52014-07-24 23:14:40 +000046 outs() << "\nError reading file: " << EC.message() << ".\n";
47 outs().flush();
Davide Italiano81032942015-07-17 06:18:36 +000048 exit(1);
David Majnemer72ab1a52014-07-24 23:14:40 +000049}
50
Lang Hamesfc209622016-07-14 02:24:01 +000051static void error(Error Err) {
52 if (Err) {
53 logAllUnhandledErrors(std::move(Err), outs(), "Error reading file: ");
54 outs().flush();
55 exit(1);
56 }
57}
58
David Majnemer72ab1a52014-07-24 23:14:40 +000059} // namespace llvm
60
61static void reportError(StringRef Input, StringRef Message) {
62 if (Input == "-")
63 Input = "<stdin>";
David Majnemer72ab1a52014-07-24 23:14:40 +000064 errs() << Input << ": " << Message << "\n";
65 errs().flush();
Davide Italiano81032942015-07-17 06:18:36 +000066 exit(1);
David Majnemer72ab1a52014-07-24 23:14:40 +000067}
68
69static void reportError(StringRef Input, std::error_code EC) {
70 reportError(Input, EC.message());
71}
72
David Majnemerd7fafa02015-08-13 18:31:43 +000073static std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
David Majnemere2683612014-11-03 07:23:25 +000074
Davide Italiano81032942015-07-17 06:18:36 +000075static void collectRelocatedSymbols(const ObjectFile *Obj,
David Majnemere2683612014-11-03 07:23:25 +000076 const SectionRef &Sec, uint64_t SecAddress,
77 uint64_t SymAddress, uint64_t SymSize,
78 StringRef *I, StringRef *E) {
79 uint64_t SymOffset = SymAddress - SecAddress;
80 uint64_t SymEnd = SymOffset + SymSize;
David Majnemerd7fafa02015-08-13 18:31:43 +000081 for (const SectionRef &SR : SectionRelocMap[Sec]) {
David Majnemere2683612014-11-03 07:23:25 +000082 for (const object::RelocationRef &Reloc : SR.relocations()) {
83 if (I == E)
84 break;
85 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
86 if (RelocSymI == Obj->symbol_end())
87 continue;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +000088 Expected<StringRef> RelocSymName = RelocSymI->getName();
89 error(errorToErrorCode(RelocSymName.takeError()));
Rafael Espindola96d071c2015-06-29 23:29:12 +000090 uint64_t Offset = Reloc.getOffset();
David Majnemere2683612014-11-03 07:23:25 +000091 if (Offset >= SymOffset && Offset < SymEnd) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +000092 *I = *RelocSymName;
David Majnemere2683612014-11-03 07:23:25 +000093 ++I;
94 }
95 }
David Majnemer1ac52eb2014-09-26 04:21:51 +000096 }
David Majnemer1ac52eb2014-09-26 04:21:51 +000097}
98
Davide Italiano81032942015-07-17 06:18:36 +000099static void collectRelocationOffsets(
David Majnemere2683612014-11-03 07:23:25 +0000100 const ObjectFile *Obj, const SectionRef &Sec, uint64_t SecAddress,
101 uint64_t SymAddress, uint64_t SymSize, StringRef SymName,
David Majnemer1ac52eb2014-09-26 04:21:51 +0000102 std::map<std::pair<StringRef, uint64_t>, StringRef> &Collection) {
David Majnemere2683612014-11-03 07:23:25 +0000103 uint64_t SymOffset = SymAddress - SecAddress;
104 uint64_t SymEnd = SymOffset + SymSize;
David Majnemerd7fafa02015-08-13 18:31:43 +0000105 for (const SectionRef &SR : SectionRelocMap[Sec]) {
David Majnemere2683612014-11-03 07:23:25 +0000106 for (const object::RelocationRef &Reloc : SR.relocations()) {
107 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
108 if (RelocSymI == Obj->symbol_end())
109 continue;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000110 Expected<StringRef> RelocSymName = RelocSymI->getName();
111 error(errorToErrorCode(RelocSymName.takeError()));
Rafael Espindola96d071c2015-06-29 23:29:12 +0000112 uint64_t Offset = Reloc.getOffset();
David Majnemere2683612014-11-03 07:23:25 +0000113 if (Offset >= SymOffset && Offset < SymEnd)
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000114 Collection[std::make_pair(SymName, Offset - SymOffset)] = *RelocSymName;
David Majnemere2683612014-11-03 07:23:25 +0000115 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000116 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000117}
118
David Majnemerf45bbd02015-03-15 01:30:58 +0000119static void dumpCXXData(const ObjectFile *Obj) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000120 struct CompleteObjectLocator {
121 StringRef Symbols[2];
David Majnemer6887a252014-09-26 08:01:23 +0000122 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000123 };
124 struct ClassHierarchyDescriptor {
125 StringRef Symbols[1];
David Majnemer6887a252014-09-26 08:01:23 +0000126 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000127 };
128 struct BaseClassDescriptor {
129 StringRef Symbols[2];
David Majnemer6887a252014-09-26 08:01:23 +0000130 ArrayRef<little32_t> Data;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000131 };
132 struct TypeDescriptor {
133 StringRef Symbols[1];
David Majnemer6887a252014-09-26 08:01:23 +0000134 uint64_t AlwaysZero;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000135 StringRef MangledName;
136 };
David Majnemerf50d0a52015-02-27 00:43:58 +0000137 struct ThrowInfo {
138 uint32_t Flags;
139 };
140 struct CatchableTypeArray {
141 uint32_t NumEntries;
142 };
143 struct CatchableType {
144 uint32_t Flags;
145 uint32_t NonVirtualBaseAdjustmentOffset;
146 int32_t VirtualBasePointerOffset;
147 uint32_t VirtualBaseAdjustmentOffset;
David Majnemer86ee1732015-02-27 22:35:25 +0000148 uint32_t Size;
David Majnemerf50d0a52015-02-27 00:43:58 +0000149 StringRef Symbols[2];
150 };
David Majnemer72ab1a52014-07-24 23:14:40 +0000151 std::map<std::pair<StringRef, uint64_t>, StringRef> VFTableEntries;
David Majnemerf50d0a52015-02-27 00:43:58 +0000152 std::map<std::pair<StringRef, uint64_t>, StringRef> TIEntries;
153 std::map<std::pair<StringRef, uint64_t>, StringRef> CTAEntries;
David Majnemer6887a252014-09-26 08:01:23 +0000154 std::map<StringRef, ArrayRef<little32_t>> VBTables;
David Majnemer1ac52eb2014-09-26 04:21:51 +0000155 std::map<StringRef, CompleteObjectLocator> COLs;
156 std::map<StringRef, ClassHierarchyDescriptor> CHDs;
157 std::map<std::pair<StringRef, uint64_t>, StringRef> BCAEntries;
158 std::map<StringRef, BaseClassDescriptor> BCDs;
159 std::map<StringRef, TypeDescriptor> TDs;
David Majnemerf50d0a52015-02-27 00:43:58 +0000160 std::map<StringRef, ThrowInfo> TIs;
161 std::map<StringRef, CatchableTypeArray> CTAs;
162 std::map<StringRef, CatchableType> CTs;
David Majnemere2683612014-11-03 07:23:25 +0000163
164 std::map<std::pair<StringRef, uint64_t>, StringRef> VTableSymEntries;
165 std::map<std::pair<StringRef, uint64_t>, int64_t> VTableDataEntries;
166 std::map<std::pair<StringRef, uint64_t>, StringRef> VTTEntries;
167 std::map<StringRef, StringRef> TINames;
168
David Majnemerd7fafa02015-08-13 18:31:43 +0000169 SectionRelocMap.clear();
170 for (const SectionRef &Section : Obj->sections()) {
171 section_iterator Sec2 = Section.getRelocatedSection();
172 if (Sec2 != Obj->section_end())
173 SectionRelocMap[*Sec2].push_back(Section);
174 }
175
David Majnemere2683612014-11-03 07:23:25 +0000176 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;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000184 Expected<StringRef> SymNameOrErr = Sym.getName();
185 error(errorToErrorCode(SymNameOrErr.takeError()));
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000186 StringRef SymName = *SymNameOrErr;
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000187 Expected<object::section_iterator> SecIOrErr = Sym.getSection();
188 error(errorToErrorCode(SecIOrErr.takeError()));
Rafael Espindola8bab8892015-08-07 23:27:14 +0000189 object::section_iterator SecI = *SecIOrErr;
David Majnemer601327c2014-09-26 22:32:19 +0000190 // Skip external symbols.
191 if (SecI == Obj->section_end())
192 continue;
David Majnemere2683612014-11-03 07:23:25 +0000193 const SectionRef &Sec = *SecI;
David Majnemer601327c2014-09-26 22:32:19 +0000194 // Skip virtual or BSS sections.
David Majnemere2683612014-11-03 07:23:25 +0000195 if (Sec.isBSS() || Sec.isVirtual())
David Majnemer601327c2014-09-26 22:32:19 +0000196 continue;
197 StringRef SecContents;
Davide Italiano81032942015-07-17 06:18:36 +0000198 error(Sec.getContents(SecContents));
Kevin Enderby931cb652016-06-24 18:24:42 +0000199 Expected<uint64_t> SymAddressOrErr = Sym.getAddress();
200 error(errorToErrorCode(SymAddressOrErr.takeError()));
Rafael Espindolaed067c42015-07-03 18:19:00 +0000201 uint64_t SymAddress = *SymAddressOrErr;
David Majnemere2683612014-11-03 07:23:25 +0000202 uint64_t SecAddress = Sec.getAddress();
203 uint64_t SecSize = Sec.getSize();
204 uint64_t SymOffset = SymAddress - SecAddress;
205 StringRef SymContents = SecContents.substr(SymOffset, SymSize);
206
David Majnemer72ab1a52014-07-24 23:14:40 +0000207 // VFTables in the MS-ABI start with '??_7' and are contained within their
208 // own COMDAT section. We then determine the contents of the VFTable by
209 // looking at each relocation in the section.
210 if (SymName.startswith("??_7")) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000211 // Each relocation either names a virtual method or a thunk. We note the
212 // offset into the section and the symbol used for the relocation.
David Majnemere2683612014-11-03 07:23:25 +0000213 collectRelocationOffsets(Obj, Sec, SecAddress, SecAddress, SecSize,
214 SymName, VFTableEntries);
David Majnemer72ab1a52014-07-24 23:14:40 +0000215 }
216 // VBTables in the MS-ABI start with '??_8' and are filled with 32-bit
217 // offsets of virtual bases.
218 else if (SymName.startswith("??_8")) {
David Majnemer6887a252014-09-26 08:01:23 +0000219 ArrayRef<little32_t> VBTableData(
David Majnemere2683612014-11-03 07:23:25 +0000220 reinterpret_cast<const little32_t *>(SymContents.data()),
221 SymContents.size() / sizeof(little32_t));
David Majnemer72ab1a52014-07-24 23:14:40 +0000222 VBTables[SymName] = VBTableData;
223 }
David Majnemer1ac52eb2014-09-26 04:21:51 +0000224 // Complete object locators in the MS-ABI start with '??_R4'
225 else if (SymName.startswith("??_R4")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000226 CompleteObjectLocator COL;
Craig Topper0013be12015-09-21 05:32:41 +0000227 COL.Data = makeArrayRef(
David Majnemere2683612014-11-03 07:23:25 +0000228 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000229 StringRef *I = std::begin(COL.Symbols), *E = std::end(COL.Symbols);
Davide Italiano81032942015-07-17 06:18:36 +0000230 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000231 COLs[SymName] = COL;
232 }
233 // Class hierarchy descriptors in the MS-ABI start with '??_R3'
234 else if (SymName.startswith("??_R3")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000235 ClassHierarchyDescriptor CHD;
Craig Topper0013be12015-09-21 05:32:41 +0000236 CHD.Data = makeArrayRef(
David Majnemere2683612014-11-03 07:23:25 +0000237 reinterpret_cast<const little32_t *>(SymContents.data()), 3);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000238 StringRef *I = std::begin(CHD.Symbols), *E = std::end(CHD.Symbols);
Davide Italiano81032942015-07-17 06:18:36 +0000239 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000240 CHDs[SymName] = CHD;
241 }
242 // Class hierarchy descriptors in the MS-ABI start with '??_R2'
243 else if (SymName.startswith("??_R2")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000244 // Each relocation names a base class descriptor. We note the offset into
245 // the section and the symbol used for the relocation.
David Majnemere2683612014-11-03 07:23:25 +0000246 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
247 SymName, BCAEntries);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000248 }
249 // Base class descriptors in the MS-ABI start with '??_R1'
250 else if (SymName.startswith("??_R1")) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000251 BaseClassDescriptor BCD;
Craig Topper0013be12015-09-21 05:32:41 +0000252 BCD.Data = makeArrayRef(
David Majnemere2683612014-11-03 07:23:25 +0000253 reinterpret_cast<const little32_t *>(SymContents.data()) + 1, 5);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000254 StringRef *I = std::begin(BCD.Symbols), *E = std::end(BCD.Symbols);
Davide Italiano81032942015-07-17 06:18:36 +0000255 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000256 BCDs[SymName] = BCD;
257 }
258 // Type descriptors in the MS-ABI start with '??_R0'
259 else if (SymName.startswith("??_R0")) {
David Majnemere2683612014-11-03 07:23:25 +0000260 const char *DataPtr = SymContents.drop_front(BytesInAddress).data();
David Majnemer1ac52eb2014-09-26 04:21:51 +0000261 TypeDescriptor TD;
David Majnemer6887a252014-09-26 08:01:23 +0000262 if (BytesInAddress == 8)
263 TD.AlwaysZero = *reinterpret_cast<const little64_t *>(DataPtr);
264 else
265 TD.AlwaysZero = *reinterpret_cast<const little32_t *>(DataPtr);
David Majnemere2683612014-11-03 07:23:25 +0000266 TD.MangledName = SymContents.drop_front(BytesInAddress * 2);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000267 StringRef *I = std::begin(TD.Symbols), *E = std::end(TD.Symbols);
Davide Italiano81032942015-07-17 06:18:36 +0000268 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
David Majnemer1ac52eb2014-09-26 04:21:51 +0000269 TDs[SymName] = TD;
270 }
David Majnemerf50d0a52015-02-27 00:43:58 +0000271 // Throw descriptors in the MS-ABI start with '_TI'
272 else if (SymName.startswith("_TI") || SymName.startswith("__TI")) {
273 ThrowInfo TI;
274 TI.Flags = *reinterpret_cast<const little32_t *>(SymContents.data());
275 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
276 SymName, TIEntries);
277 TIs[SymName] = TI;
278 }
279 // Catchable type arrays in the MS-ABI start with _CTA or __CTA.
280 else if (SymName.startswith("_CTA") || SymName.startswith("__CTA")) {
281 CatchableTypeArray CTA;
282 CTA.NumEntries =
283 *reinterpret_cast<const little32_t *>(SymContents.data());
284 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
285 SymName, CTAEntries);
286 CTAs[SymName] = CTA;
287 }
288 // Catchable types in the MS-ABI start with _CT or __CT.
289 else if (SymName.startswith("_CT") || SymName.startswith("__CT")) {
290 const little32_t *DataPtr =
291 reinterpret_cast<const little32_t *>(SymContents.data());
292 CatchableType CT;
293 CT.Flags = DataPtr[0];
294 CT.NonVirtualBaseAdjustmentOffset = DataPtr[2];
295 CT.VirtualBasePointerOffset = DataPtr[3];
296 CT.VirtualBaseAdjustmentOffset = DataPtr[4];
David Majnemer86ee1732015-02-27 22:35:25 +0000297 CT.Size = DataPtr[5];
David Majnemerf50d0a52015-02-27 00:43:58 +0000298 StringRef *I = std::begin(CT.Symbols), *E = std::end(CT.Symbols);
Davide Italiano81032942015-07-17 06:18:36 +0000299 collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, E);
David Majnemerf50d0a52015-02-27 00:43:58 +0000300 CTs[SymName] = CT;
301 }
David Majnemere2683612014-11-03 07:23:25 +0000302 // Construction vtables in the Itanium ABI start with '_ZTT' or '__ZTT'.
303 else if (SymName.startswith("_ZTT") || SymName.startswith("__ZTT")) {
304 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
305 SymName, VTTEntries);
306 }
307 // Typeinfo names in the Itanium ABI start with '_ZTS' or '__ZTS'.
308 else if (SymName.startswith("_ZTS") || SymName.startswith("__ZTS")) {
309 TINames[SymName] = SymContents.slice(0, SymContents.find('\0'));
310 }
311 // Vtables in the Itanium ABI start with '_ZTV' or '__ZTV'.
312 else if (SymName.startswith("_ZTV") || SymName.startswith("__ZTV")) {
313 collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize,
314 SymName, VTableSymEntries);
315 for (uint64_t SymOffI = 0; SymOffI < SymSize; SymOffI += BytesInAddress) {
316 auto Key = std::make_pair(SymName, SymOffI);
317 if (VTableSymEntries.count(Key))
318 continue;
David Majnemerf45bbd02015-03-15 01:30:58 +0000319 const char *DataPtr =
320 SymContents.substr(SymOffI, BytesInAddress).data();
David Majnemere2683612014-11-03 07:23:25 +0000321 int64_t VData;
322 if (BytesInAddress == 8)
323 VData = *reinterpret_cast<const little64_t *>(DataPtr);
324 else
325 VData = *reinterpret_cast<const little32_t *>(DataPtr);
326 VTableDataEntries[Key] = VData;
327 }
328 }
329 // Typeinfo structures in the Itanium ABI start with '_ZTI' or '__ZTI'.
330 else if (SymName.startswith("_ZTI") || SymName.startswith("__ZTI")) {
331 // FIXME: Do something with these!
332 }
David Majnemer72ab1a52014-07-24 23:14:40 +0000333 }
David Majnemerf45bbd02015-03-15 01:30:58 +0000334 for (const auto &VFTableEntry : VFTableEntries) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000335 StringRef VFTableName = VFTableEntry.first.first;
336 uint64_t Offset = VFTableEntry.first.second;
337 StringRef SymName = VFTableEntry.second;
338 outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n';
339 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000340 for (const auto &VBTable : VBTables) {
David Majnemerbf32f772014-07-25 04:30:11 +0000341 StringRef VBTableName = VBTable.first;
David Majnemer72ab1a52014-07-24 23:14:40 +0000342 uint32_t Idx = 0;
David Majnemer6887a252014-09-26 08:01:23 +0000343 for (little32_t Offset : VBTable.second) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000344 outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n';
David Majnemerbf32f772014-07-25 04:30:11 +0000345 Idx += sizeof(Offset);
David Majnemer72ab1a52014-07-24 23:14:40 +0000346 }
347 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000348 for (const auto &COLPair : COLs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000349 StringRef COLName = COLPair.first;
350 const CompleteObjectLocator &COL = COLPair.second;
351 outs() << COLName << "[IsImageRelative]: " << COL.Data[0] << '\n';
352 outs() << COLName << "[OffsetToTop]: " << COL.Data[1] << '\n';
353 outs() << COLName << "[VFPtrOffset]: " << COL.Data[2] << '\n';
354 outs() << COLName << "[TypeDescriptor]: " << COL.Symbols[0] << '\n';
David Majnemerf45bbd02015-03-15 01:30:58 +0000355 outs() << COLName << "[ClassHierarchyDescriptor]: " << COL.Symbols[1]
356 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000357 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000358 for (const auto &CHDPair : CHDs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000359 StringRef CHDName = CHDPair.first;
360 const ClassHierarchyDescriptor &CHD = CHDPair.second;
361 outs() << CHDName << "[AlwaysZero]: " << CHD.Data[0] << '\n';
362 outs() << CHDName << "[Flags]: " << CHD.Data[1] << '\n';
363 outs() << CHDName << "[NumClasses]: " << CHD.Data[2] << '\n';
364 outs() << CHDName << "[BaseClassArray]: " << CHD.Symbols[0] << '\n';
365 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000366 for (const auto &BCAEntry : BCAEntries) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000367 StringRef BCAName = BCAEntry.first.first;
368 uint64_t Offset = BCAEntry.first.second;
369 StringRef SymName = BCAEntry.second;
370 outs() << BCAName << '[' << Offset << "]: " << SymName << '\n';
371 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000372 for (const auto &BCDPair : BCDs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000373 StringRef BCDName = BCDPair.first;
374 const BaseClassDescriptor &BCD = BCDPair.second;
375 outs() << BCDName << "[TypeDescriptor]: " << BCD.Symbols[0] << '\n';
376 outs() << BCDName << "[NumBases]: " << BCD.Data[0] << '\n';
377 outs() << BCDName << "[OffsetInVBase]: " << BCD.Data[1] << '\n';
378 outs() << BCDName << "[VBPtrOffset]: " << BCD.Data[2] << '\n';
379 outs() << BCDName << "[OffsetInVBTable]: " << BCD.Data[3] << '\n';
380 outs() << BCDName << "[Flags]: " << BCD.Data[4] << '\n';
David Majnemerf45bbd02015-03-15 01:30:58 +0000381 outs() << BCDName << "[ClassHierarchyDescriptor]: " << BCD.Symbols[1]
382 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000383 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000384 for (const auto &TDPair : TDs) {
David Majnemer1ac52eb2014-09-26 04:21:51 +0000385 StringRef TDName = TDPair.first;
386 const TypeDescriptor &TD = TDPair.second;
387 outs() << TDName << "[VFPtr]: " << TD.Symbols[0] << '\n';
David Majnemer6887a252014-09-26 08:01:23 +0000388 outs() << TDName << "[AlwaysZero]: " << TD.AlwaysZero << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000389 outs() << TDName << "[MangledName]: ";
David Majnemer56167c32014-09-26 05:50:45 +0000390 outs().write_escaped(TD.MangledName.rtrim(StringRef("\0", 1)),
391 /*UseHexEscapes=*/true)
392 << '\n';
David Majnemer1ac52eb2014-09-26 04:21:51 +0000393 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000394 for (const auto &TIPair : TIs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000395 StringRef TIName = TIPair.first;
396 const ThrowInfo &TI = TIPair.second;
397 auto dumpThrowInfoFlag = [&](const char *Name, uint32_t Flag) {
398 outs() << TIName << "[Flags." << Name
399 << "]: " << (TI.Flags & Flag ? "true" : "false") << '\n';
400 };
401 auto dumpThrowInfoSymbol = [&](const char *Name, int Offset) {
402 outs() << TIName << '[' << Name << "]: ";
403 auto Entry = TIEntries.find(std::make_pair(TIName, Offset));
404 outs() << (Entry == TIEntries.end() ? "null" : Entry->second) << '\n';
405 };
406 outs() << TIName << "[Flags]: " << TI.Flags << '\n';
407 dumpThrowInfoFlag("Const", 1);
408 dumpThrowInfoFlag("Volatile", 2);
409 dumpThrowInfoSymbol("CleanupFn", 4);
410 dumpThrowInfoSymbol("ForwardCompat", 8);
411 dumpThrowInfoSymbol("CatchableTypeArray", 12);
412 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000413 for (const auto &CTAPair : CTAs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000414 StringRef CTAName = CTAPair.first;
415 const CatchableTypeArray &CTA = CTAPair.second;
416
417 outs() << CTAName << "[NumEntries]: " << CTA.NumEntries << '\n';
418
419 unsigned Idx = 0;
420 for (auto I = CTAEntries.lower_bound(std::make_pair(CTAName, 0)),
421 E = CTAEntries.upper_bound(std::make_pair(CTAName, UINT64_MAX));
422 I != E; ++I)
423 outs() << CTAName << '[' << Idx++ << "]: " << I->second << '\n';
424 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000425 for (const auto &CTPair : CTs) {
David Majnemerf50d0a52015-02-27 00:43:58 +0000426 StringRef CTName = CTPair.first;
427 const CatchableType &CT = CTPair.second;
428 auto dumpCatchableTypeFlag = [&](const char *Name, uint32_t Flag) {
429 outs() << CTName << "[Flags." << Name
430 << "]: " << (CT.Flags & Flag ? "true" : "false") << '\n';
431 };
432 outs() << CTName << "[Flags]: " << CT.Flags << '\n';
433 dumpCatchableTypeFlag("ScalarType", 1);
434 dumpCatchableTypeFlag("VirtualInheritance", 4);
435 outs() << CTName << "[TypeDescriptor]: " << CT.Symbols[0] << '\n';
436 outs() << CTName << "[NonVirtualBaseAdjustmentOffset]: "
437 << CT.NonVirtualBaseAdjustmentOffset << '\n';
438 outs() << CTName
439 << "[VirtualBasePointerOffset]: " << CT.VirtualBasePointerOffset
440 << '\n';
441 outs() << CTName << "[VirtualBaseAdjustmentOffset]: "
442 << CT.VirtualBaseAdjustmentOffset << '\n';
David Majnemer86ee1732015-02-27 22:35:25 +0000443 outs() << CTName << "[Size]: " << CT.Size << '\n';
David Majnemerf50d0a52015-02-27 00:43:58 +0000444 outs() << CTName
445 << "[CopyCtor]: " << (CT.Symbols[1].empty() ? "null" : CT.Symbols[1])
446 << '\n';
447 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000448 for (const auto &VTTPair : VTTEntries) {
David Majnemere2683612014-11-03 07:23:25 +0000449 StringRef VTTName = VTTPair.first.first;
450 uint64_t VTTOffset = VTTPair.first.second;
451 StringRef VTTEntry = VTTPair.second;
452 outs() << VTTName << '[' << VTTOffset << "]: " << VTTEntry << '\n';
453 }
Richard Trieu46f2cc92015-04-15 03:17:49 +0000454 for (const auto &TIPair : TINames) {
David Majnemere2683612014-11-03 07:23:25 +0000455 StringRef TIName = TIPair.first;
456 outs() << TIName << ": " << TIPair.second << '\n';
457 }
458 auto VTableSymI = VTableSymEntries.begin();
459 auto VTableSymE = VTableSymEntries.end();
460 auto VTableDataI = VTableDataEntries.begin();
461 auto VTableDataE = VTableDataEntries.end();
462 for (;;) {
463 bool SymDone = VTableSymI == VTableSymE;
464 bool DataDone = VTableDataI == VTableDataE;
465 if (SymDone && DataDone)
466 break;
467 if (!SymDone && (DataDone || VTableSymI->first < VTableDataI->first)) {
468 StringRef VTableName = VTableSymI->first.first;
469 uint64_t Offset = VTableSymI->first.second;
470 StringRef VTableEntry = VTableSymI->second;
471 outs() << VTableName << '[' << Offset << "]: ";
472 outs() << VTableEntry;
473 outs() << '\n';
474 ++VTableSymI;
475 continue;
476 }
477 if (!DataDone && (SymDone || VTableDataI->first < VTableSymI->first)) {
478 StringRef VTableName = VTableDataI->first.first;
479 uint64_t Offset = VTableDataI->first.second;
480 int64_t VTableEntry = VTableDataI->second;
481 outs() << VTableName << '[' << Offset << "]: ";
482 outs() << VTableEntry;
483 outs() << '\n';
484 ++VTableDataI;
485 continue;
486 }
487 }
David Majnemer72ab1a52014-07-24 23:14:40 +0000488}
489
490static void dumpArchive(const Archive *Arc) {
Mehdi Amini41af4302016-11-11 04:28:40 +0000491 Error Err = Error::success();
Lang Hamesfc209622016-07-14 02:24:01 +0000492 for (auto &ArcC : Arc->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000493 Expected<std::unique_ptr<Binary>> ChildOrErr = ArcC.getAsBinary();
494 if (!ChildOrErr) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000495 // Ignore non-object files.
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000496 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) {
497 std::string Buf;
498 raw_string_ostream OS(Buf);
499 logAllUnhandledErrors(std::move(E), OS, "");
500 OS.flush();
501 reportError(Arc->getFileName(), Buf);
502 }
Kevin Enderby2f9d8d02016-10-21 20:03:14 +0000503 consumeError(ChildOrErr.takeError());
David Majnemer72ab1a52014-07-24 23:14:40 +0000504 continue;
505 }
506
507 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
David Majnemerf45bbd02015-03-15 01:30:58 +0000508 dumpCXXData(Obj);
David Majnemer72ab1a52014-07-24 23:14:40 +0000509 else
David Majnemerf45bbd02015-03-15 01:30:58 +0000510 reportError(Arc->getFileName(), cxxdump_error::unrecognized_file_format);
David Majnemer72ab1a52014-07-24 23:14:40 +0000511 }
Lang Hamesfc209622016-07-14 02:24:01 +0000512 error(std::move(Err));
David Majnemer72ab1a52014-07-24 23:14:40 +0000513}
514
515static void dumpInput(StringRef File) {
David Majnemer72ab1a52014-07-24 23:14:40 +0000516 // Attempt to open the binary.
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000517 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
518 if (!BinaryOrErr) {
519 auto EC = errorToErrorCode(BinaryOrErr.takeError());
David Majnemer72ab1a52014-07-24 23:14:40 +0000520 reportError(File, EC);
521 return;
522 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000523 Binary &Binary = *BinaryOrErr.get().getBinary();
David Majnemer72ab1a52014-07-24 23:14:40 +0000524
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000525 if (Archive *Arc = dyn_cast<Archive>(&Binary))
David Majnemer72ab1a52014-07-24 23:14:40 +0000526 dumpArchive(Arc);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000527 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
David Majnemerf45bbd02015-03-15 01:30:58 +0000528 dumpCXXData(Obj);
David Majnemer72ab1a52014-07-24 23:14:40 +0000529 else
David Majnemerf45bbd02015-03-15 01:30:58 +0000530 reportError(File, cxxdump_error::unrecognized_file_format);
David Majnemer72ab1a52014-07-24 23:14:40 +0000531}
532
533int main(int argc, const char *argv[]) {
Rui Ueyama197194b2018-04-13 18:26:06 +0000534 InitLLVM X(argc, argv);
David Majnemer72ab1a52014-07-24 23:14:40 +0000535
536 // Initialize targets.
537 llvm::InitializeAllTargetInfos();
538
539 // Register the target printer for --version.
540 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
541
David Majnemerf45bbd02015-03-15 01:30:58 +0000542 cl::ParseCommandLineOptions(argc, argv, "LLVM C++ ABI Data Dumper\n");
David Majnemer72ab1a52014-07-24 23:14:40 +0000543
544 // Default to stdin if no filename is specified.
Dimitry Andrice4f5d012017-12-18 19:46:56 +0000545 if (opts::InputFilenames.size() == 0)
546 opts::InputFilenames.push_back("-");
547
548 llvm::for_each(opts::InputFilenames, dumpInput);
549
550 return EXIT_SUCCESS;
551}