File: | include/llvm/Object/ObjectFile.h |
Warning: | line 417, column 10 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===-- COFFDumper.cpp - COFF-specific dumper -------------------*- C++ -*-===// | |||
2 | // | |||
3 | // The LLVM Compiler Infrastructure | |||
4 | // | |||
5 | // This file is distributed under the University of Illinois Open Source | |||
6 | // License. See LICENSE.TXT for details. | |||
7 | // | |||
8 | //===----------------------------------------------------------------------===// | |||
9 | /// | |||
10 | /// \file | |||
11 | /// This file implements the COFF-specific dumper for llvm-readobj. | |||
12 | /// | |||
13 | //===----------------------------------------------------------------------===// | |||
14 | ||||
15 | #include "ARMWinEHPrinter.h" | |||
16 | #include "Error.h" | |||
17 | #include "ObjDumper.h" | |||
18 | #include "StackMapPrinter.h" | |||
19 | #include "Win64EHDumper.h" | |||
20 | #include "llvm-readobj.h" | |||
21 | #include "llvm/ADT/DenseMap.h" | |||
22 | #include "llvm/ADT/SmallString.h" | |||
23 | #include "llvm/ADT/StringExtras.h" | |||
24 | #include "llvm/BinaryFormat/COFF.h" | |||
25 | #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h" | |||
26 | #include "llvm/DebugInfo/CodeView/CodeView.h" | |||
27 | #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h" | |||
28 | #include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.h" | |||
29 | #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h" | |||
30 | #include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h" | |||
31 | #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h" | |||
32 | #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h" | |||
33 | #include "llvm/DebugInfo/CodeView/Line.h" | |||
34 | #include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h" | |||
35 | #include "llvm/DebugInfo/CodeView/RecordSerialization.h" | |||
36 | #include "llvm/DebugInfo/CodeView/SymbolDumpDelegate.h" | |||
37 | #include "llvm/DebugInfo/CodeView/SymbolDumper.h" | |||
38 | #include "llvm/DebugInfo/CodeView/SymbolRecord.h" | |||
39 | #include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h" | |||
40 | #include "llvm/DebugInfo/CodeView/TypeHashing.h" | |||
41 | #include "llvm/DebugInfo/CodeView/TypeIndex.h" | |||
42 | #include "llvm/DebugInfo/CodeView/TypeRecord.h" | |||
43 | #include "llvm/DebugInfo/CodeView/TypeStreamMerger.h" | |||
44 | #include "llvm/DebugInfo/CodeView/TypeTableCollection.h" | |||
45 | #include "llvm/Object/COFF.h" | |||
46 | #include "llvm/Object/ObjectFile.h" | |||
47 | #include "llvm/Support/BinaryStreamReader.h" | |||
48 | #include "llvm/Support/Casting.h" | |||
49 | #include "llvm/Support/Compiler.h" | |||
50 | #include "llvm/Support/ConvertUTF.h" | |||
51 | #include "llvm/Support/FormatVariadic.h" | |||
52 | #include "llvm/Support/ScopedPrinter.h" | |||
53 | #include "llvm/Support/LEB128.h" | |||
54 | #include "llvm/Support/Win64EH.h" | |||
55 | #include "llvm/Support/raw_ostream.h" | |||
56 | ||||
57 | using namespace llvm; | |||
58 | using namespace llvm::object; | |||
59 | using namespace llvm::codeview; | |||
60 | using namespace llvm::support; | |||
61 | using namespace llvm::Win64EH; | |||
62 | ||||
63 | namespace { | |||
64 | ||||
65 | struct LoadConfigTables { | |||
66 | uint64_t SEHTableVA = 0; | |||
67 | uint64_t SEHTableCount = 0; | |||
68 | uint32_t GuardFlags = 0; | |||
69 | uint64_t GuardFidTableVA = 0; | |||
70 | uint64_t GuardFidTableCount = 0; | |||
71 | uint64_t GuardLJmpTableVA = 0; | |||
72 | uint64_t GuardLJmpTableCount = 0; | |||
73 | }; | |||
74 | ||||
75 | class COFFDumper : public ObjDumper { | |||
76 | public: | |||
77 | friend class COFFObjectDumpDelegate; | |||
78 | COFFDumper(const llvm::object::COFFObjectFile *Obj, ScopedPrinter &Writer) | |||
79 | : ObjDumper(Writer), Obj(Obj), Writer(Writer), Types(100) {} | |||
80 | ||||
81 | void printFileHeaders() override; | |||
82 | void printSections() override; | |||
83 | void printRelocations() override; | |||
84 | void printSymbols() override; | |||
85 | void printDynamicSymbols() override; | |||
86 | void printUnwindInfo() override; | |||
87 | ||||
88 | void printNeededLibraries() override; | |||
89 | ||||
90 | void printCOFFImports() override; | |||
91 | void printCOFFExports() override; | |||
92 | void printCOFFDirectives() override; | |||
93 | void printCOFFBaseReloc() override; | |||
94 | void printCOFFDebugDirectory() override; | |||
95 | void printCOFFResources() override; | |||
96 | void printCOFFLoadConfig() override; | |||
97 | void printCodeViewDebugInfo() override; | |||
98 | void | |||
99 | mergeCodeViewTypes(llvm::codeview::MergingTypeTableBuilder &CVIDs, | |||
100 | llvm::codeview::MergingTypeTableBuilder &CVTypes) override; | |||
101 | void printStackMap() const override; | |||
102 | void printAddrsig() override; | |||
103 | private: | |||
104 | void printSymbol(const SymbolRef &Sym); | |||
105 | void printRelocation(const SectionRef &Section, const RelocationRef &Reloc, | |||
106 | uint64_t Bias = 0); | |||
107 | void printDataDirectory(uint32_t Index, const std::string &FieldName); | |||
108 | ||||
109 | void printDOSHeader(const dos_header *DH); | |||
110 | template <class PEHeader> void printPEHeader(const PEHeader *Hdr); | |||
111 | void printBaseOfDataField(const pe32_header *Hdr); | |||
112 | void printBaseOfDataField(const pe32plus_header *Hdr); | |||
113 | template <typename T> | |||
114 | void printCOFFLoadConfig(const T *Conf, LoadConfigTables &Tables); | |||
115 | typedef void (*PrintExtraCB)(raw_ostream &, const uint8_t *); | |||
116 | void printRVATable(uint64_t TableVA, uint64_t Count, uint64_t EntrySize, | |||
117 | PrintExtraCB PrintExtra = 0); | |||
118 | ||||
119 | void printCodeViewSymbolSection(StringRef SectionName, const SectionRef &Section); | |||
120 | void printCodeViewTypeSection(StringRef SectionName, const SectionRef &Section); | |||
121 | StringRef getTypeName(TypeIndex Ty); | |||
122 | StringRef getFileNameForFileOffset(uint32_t FileOffset); | |||
123 | void printFileNameForOffset(StringRef Label, uint32_t FileOffset); | |||
124 | void printTypeIndex(StringRef FieldName, TypeIndex TI) { | |||
125 | // Forward to CVTypeDumper for simplicity. | |||
126 | codeview::printTypeIndex(Writer, FieldName, TI, Types); | |||
127 | } | |||
128 | ||||
129 | void printCodeViewSymbolsSubsection(StringRef Subsection, | |||
130 | const SectionRef &Section, | |||
131 | StringRef SectionContents); | |||
132 | ||||
133 | void printCodeViewFileChecksums(StringRef Subsection); | |||
134 | ||||
135 | void printCodeViewInlineeLines(StringRef Subsection); | |||
136 | ||||
137 | void printRelocatedField(StringRef Label, const coff_section *Sec, | |||
138 | uint32_t RelocOffset, uint32_t Offset, | |||
139 | StringRef *RelocSym = nullptr); | |||
140 | ||||
141 | uint32_t countTotalTableEntries(ResourceSectionRef RSF, | |||
142 | const coff_resource_dir_table &Table, | |||
143 | StringRef Level); | |||
144 | ||||
145 | void printResourceDirectoryTable(ResourceSectionRef RSF, | |||
146 | const coff_resource_dir_table &Table, | |||
147 | StringRef Level); | |||
148 | ||||
149 | void printBinaryBlockWithRelocs(StringRef Label, const SectionRef &Sec, | |||
150 | StringRef SectionContents, StringRef Block); | |||
151 | ||||
152 | /// Given a .debug$S section, find the string table and file checksum table. | |||
153 | void initializeFileAndStringTables(BinaryStreamReader &Reader); | |||
154 | ||||
155 | void cacheRelocations(); | |||
156 | ||||
157 | std::error_code resolveSymbol(const coff_section *Section, uint64_t Offset, | |||
158 | SymbolRef &Sym); | |||
159 | std::error_code resolveSymbolName(const coff_section *Section, | |||
160 | uint64_t Offset, StringRef &Name); | |||
161 | std::error_code resolveSymbolName(const coff_section *Section, | |||
162 | StringRef SectionContents, | |||
163 | const void *RelocPtr, StringRef &Name); | |||
164 | void printImportedSymbols(iterator_range<imported_symbol_iterator> Range); | |||
165 | void printDelayImportedSymbols( | |||
166 | const DelayImportDirectoryEntryRef &I, | |||
167 | iterator_range<imported_symbol_iterator> Range); | |||
168 | ErrorOr<const coff_resource_dir_entry &> | |||
169 | getResourceDirectoryTableEntry(const coff_resource_dir_table &Table, | |||
170 | uint32_t Index); | |||
171 | ||||
172 | typedef DenseMap<const coff_section*, std::vector<RelocationRef> > RelocMapTy; | |||
173 | ||||
174 | const llvm::object::COFFObjectFile *Obj; | |||
175 | bool RelocCached = false; | |||
176 | RelocMapTy RelocMap; | |||
177 | ||||
178 | DebugChecksumsSubsectionRef CVFileChecksumTable; | |||
179 | ||||
180 | DebugStringTableSubsectionRef CVStringTable; | |||
181 | ||||
182 | /// Track the compilation CPU type. S_COMPILE3 symbol records typically come | |||
183 | /// first, but if we don't see one, just assume an X64 CPU type. It is common. | |||
184 | CPUType CompilationCPUType = CPUType::X64; | |||
185 | ||||
186 | ScopedPrinter &Writer; | |||
187 | BinaryByteStream TypeContents; | |||
188 | LazyRandomTypeCollection Types; | |||
189 | }; | |||
190 | ||||
191 | class COFFObjectDumpDelegate : public SymbolDumpDelegate { | |||
192 | public: | |||
193 | COFFObjectDumpDelegate(COFFDumper &CD, const SectionRef &SR, | |||
194 | const COFFObjectFile *Obj, StringRef SectionContents) | |||
195 | : CD(CD), SR(SR), SectionContents(SectionContents) { | |||
196 | Sec = Obj->getCOFFSection(SR); | |||
197 | } | |||
198 | ||||
199 | uint32_t getRecordOffset(BinaryStreamReader Reader) override { | |||
200 | ArrayRef<uint8_t> Data; | |||
201 | if (auto EC = Reader.readLongestContiguousChunk(Data)) { | |||
202 | llvm::consumeError(std::move(EC)); | |||
203 | return 0; | |||
204 | } | |||
205 | return Data.data() - SectionContents.bytes_begin(); | |||
206 | } | |||
207 | ||||
208 | void printRelocatedField(StringRef Label, uint32_t RelocOffset, | |||
209 | uint32_t Offset, StringRef *RelocSym) override { | |||
210 | CD.printRelocatedField(Label, Sec, RelocOffset, Offset, RelocSym); | |||
211 | } | |||
212 | ||||
213 | void printBinaryBlockWithRelocs(StringRef Label, | |||
214 | ArrayRef<uint8_t> Block) override { | |||
215 | StringRef SBlock(reinterpret_cast<const char *>(Block.data()), | |||
216 | Block.size()); | |||
217 | if (opts::CodeViewSubsectionBytes) | |||
218 | CD.printBinaryBlockWithRelocs(Label, SR, SectionContents, SBlock); | |||
219 | } | |||
220 | ||||
221 | StringRef getFileNameForFileOffset(uint32_t FileOffset) override { | |||
222 | return CD.getFileNameForFileOffset(FileOffset); | |||
223 | } | |||
224 | ||||
225 | DebugStringTableSubsectionRef getStringTable() override { | |||
226 | return CD.CVStringTable; | |||
227 | } | |||
228 | ||||
229 | private: | |||
230 | COFFDumper &CD; | |||
231 | const SectionRef &SR; | |||
232 | const coff_section *Sec; | |||
233 | StringRef SectionContents; | |||
234 | }; | |||
235 | ||||
236 | } // end namespace | |||
237 | ||||
238 | namespace llvm { | |||
239 | ||||
240 | std::error_code createCOFFDumper(const object::ObjectFile *Obj, | |||
241 | ScopedPrinter &Writer, | |||
242 | std::unique_ptr<ObjDumper> &Result) { | |||
243 | const COFFObjectFile *COFFObj = dyn_cast<COFFObjectFile>(Obj); | |||
244 | if (!COFFObj) | |||
245 | return readobj_error::unsupported_obj_file_format; | |||
246 | ||||
247 | Result.reset(new COFFDumper(COFFObj, Writer)); | |||
248 | return readobj_error::success; | |||
249 | } | |||
250 | ||||
251 | } // namespace llvm | |||
252 | ||||
253 | // Given a section and an offset into this section the function returns the | |||
254 | // symbol used for the relocation at the offset. | |||
255 | std::error_code COFFDumper::resolveSymbol(const coff_section *Section, | |||
256 | uint64_t Offset, SymbolRef &Sym) { | |||
257 | cacheRelocations(); | |||
258 | const auto &Relocations = RelocMap[Section]; | |||
259 | auto SymI = Obj->symbol_end(); | |||
260 | for (const auto &Relocation : Relocations) { | |||
261 | uint64_t RelocationOffset = Relocation.getOffset(); | |||
262 | ||||
263 | if (RelocationOffset == Offset) { | |||
264 | SymI = Relocation.getSymbol(); | |||
265 | break; | |||
266 | } | |||
267 | } | |||
268 | if (SymI == Obj->symbol_end()) | |||
269 | return readobj_error::unknown_symbol; | |||
270 | Sym = *SymI; | |||
271 | return readobj_error::success; | |||
272 | } | |||
273 | ||||
274 | // Given a section and an offset into this section the function returns the name | |||
275 | // of the symbol used for the relocation at the offset. | |||
276 | std::error_code COFFDumper::resolveSymbolName(const coff_section *Section, | |||
277 | uint64_t Offset, | |||
278 | StringRef &Name) { | |||
279 | SymbolRef Symbol; | |||
280 | if (std::error_code EC = resolveSymbol(Section, Offset, Symbol)) | |||
281 | return EC; | |||
282 | Expected<StringRef> NameOrErr = Symbol.getName(); | |||
283 | if (!NameOrErr) | |||
284 | return errorToErrorCode(NameOrErr.takeError()); | |||
285 | Name = *NameOrErr; | |||
286 | return std::error_code(); | |||
287 | } | |||
288 | ||||
289 | // Helper for when you have a pointer to real data and you want to know about | |||
290 | // relocations against it. | |||
291 | std::error_code COFFDumper::resolveSymbolName(const coff_section *Section, | |||
292 | StringRef SectionContents, | |||
293 | const void *RelocPtr, | |||
294 | StringRef &Name) { | |||
295 | assert(SectionContents.data() < RelocPtr &&((SectionContents.data() < RelocPtr && RelocPtr < SectionContents.data() + SectionContents.size() && "pointer to relocated object is not in section" ) ? static_cast<void> (0) : __assert_fail ("SectionContents.data() < RelocPtr && RelocPtr < SectionContents.data() + SectionContents.size() && \"pointer to relocated object is not in section\"" , "/build/llvm-toolchain-snapshot-8~svn345461/tools/llvm-readobj/COFFDumper.cpp" , 297, __PRETTY_FUNCTION__)) | |||
296 | RelocPtr < SectionContents.data() + SectionContents.size() &&((SectionContents.data() < RelocPtr && RelocPtr < SectionContents.data() + SectionContents.size() && "pointer to relocated object is not in section" ) ? static_cast<void> (0) : __assert_fail ("SectionContents.data() < RelocPtr && RelocPtr < SectionContents.data() + SectionContents.size() && \"pointer to relocated object is not in section\"" , "/build/llvm-toolchain-snapshot-8~svn345461/tools/llvm-readobj/COFFDumper.cpp" , 297, __PRETTY_FUNCTION__)) | |||
297 | "pointer to relocated object is not in section")((SectionContents.data() < RelocPtr && RelocPtr < SectionContents.data() + SectionContents.size() && "pointer to relocated object is not in section" ) ? static_cast<void> (0) : __assert_fail ("SectionContents.data() < RelocPtr && RelocPtr < SectionContents.data() + SectionContents.size() && \"pointer to relocated object is not in section\"" , "/build/llvm-toolchain-snapshot-8~svn345461/tools/llvm-readobj/COFFDumper.cpp" , 297, __PRETTY_FUNCTION__)); | |||
298 | uint64_t Offset = ptrdiff_t(reinterpret_cast<const char *>(RelocPtr) - | |||
299 | SectionContents.data()); | |||
300 | return resolveSymbolName(Section, Offset, Name); | |||
301 | } | |||
302 | ||||
303 | void COFFDumper::printRelocatedField(StringRef Label, const coff_section *Sec, | |||
304 | uint32_t RelocOffset, uint32_t Offset, | |||
305 | StringRef *RelocSym) { | |||
306 | StringRef SymStorage; | |||
307 | StringRef &Symbol = RelocSym ? *RelocSym : SymStorage; | |||
308 | if (!resolveSymbolName(Sec, RelocOffset, Symbol)) | |||
309 | W.printSymbolOffset(Label, Symbol, Offset); | |||
310 | else | |||
311 | W.printHex(Label, RelocOffset); | |||
312 | } | |||
313 | ||||
314 | void COFFDumper::printBinaryBlockWithRelocs(StringRef Label, | |||
315 | const SectionRef &Sec, | |||
316 | StringRef SectionContents, | |||
317 | StringRef Block) { | |||
318 | W.printBinaryBlock(Label, Block); | |||
319 | ||||
320 | assert(SectionContents.begin() < Block.begin() &&((SectionContents.begin() < Block.begin() && SectionContents .end() >= Block.end() && "Block is not contained in SectionContents" ) ? static_cast<void> (0) : __assert_fail ("SectionContents.begin() < Block.begin() && SectionContents.end() >= Block.end() && \"Block is not contained in SectionContents\"" , "/build/llvm-toolchain-snapshot-8~svn345461/tools/llvm-readobj/COFFDumper.cpp" , 322, __PRETTY_FUNCTION__)) | |||
321 | SectionContents.end() >= Block.end() &&((SectionContents.begin() < Block.begin() && SectionContents .end() >= Block.end() && "Block is not contained in SectionContents" ) ? static_cast<void> (0) : __assert_fail ("SectionContents.begin() < Block.begin() && SectionContents.end() >= Block.end() && \"Block is not contained in SectionContents\"" , "/build/llvm-toolchain-snapshot-8~svn345461/tools/llvm-readobj/COFFDumper.cpp" , 322, __PRETTY_FUNCTION__)) | |||
322 | "Block is not contained in SectionContents")((SectionContents.begin() < Block.begin() && SectionContents .end() >= Block.end() && "Block is not contained in SectionContents" ) ? static_cast<void> (0) : __assert_fail ("SectionContents.begin() < Block.begin() && SectionContents.end() >= Block.end() && \"Block is not contained in SectionContents\"" , "/build/llvm-toolchain-snapshot-8~svn345461/tools/llvm-readobj/COFFDumper.cpp" , 322, __PRETTY_FUNCTION__)); | |||
323 | uint64_t OffsetStart = Block.data() - SectionContents.data(); | |||
324 | uint64_t OffsetEnd = OffsetStart + Block.size(); | |||
325 | ||||
326 | W.flush(); | |||
327 | cacheRelocations(); | |||
328 | ListScope D(W, "BlockRelocations"); | |||
329 | const coff_section *Section = Obj->getCOFFSection(Sec); | |||
330 | const auto &Relocations = RelocMap[Section]; | |||
331 | for (const auto &Relocation : Relocations) { | |||
332 | uint64_t RelocationOffset = Relocation.getOffset(); | |||
333 | if (OffsetStart <= RelocationOffset && RelocationOffset < OffsetEnd) | |||
334 | printRelocation(Sec, Relocation, OffsetStart); | |||
335 | } | |||
336 | } | |||
337 | ||||
338 | static const EnumEntry<COFF::MachineTypes> ImageFileMachineType[] = { | |||
339 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_UNKNOWN ){ "IMAGE_FILE_MACHINE_UNKNOWN", COFF::IMAGE_FILE_MACHINE_UNKNOWN }, | |||
340 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AM33 ){ "IMAGE_FILE_MACHINE_AM33", COFF::IMAGE_FILE_MACHINE_AM33 }, | |||
341 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AMD64 ){ "IMAGE_FILE_MACHINE_AMD64", COFF::IMAGE_FILE_MACHINE_AMD64 }, | |||
342 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARM ){ "IMAGE_FILE_MACHINE_ARM", COFF::IMAGE_FILE_MACHINE_ARM }, | |||
343 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARM64 ){ "IMAGE_FILE_MACHINE_ARM64", COFF::IMAGE_FILE_MACHINE_ARM64 }, | |||
344 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARMNT ){ "IMAGE_FILE_MACHINE_ARMNT", COFF::IMAGE_FILE_MACHINE_ARMNT }, | |||
345 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_EBC ){ "IMAGE_FILE_MACHINE_EBC", COFF::IMAGE_FILE_MACHINE_EBC }, | |||
346 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_I386 ){ "IMAGE_FILE_MACHINE_I386", COFF::IMAGE_FILE_MACHINE_I386 }, | |||
347 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_IA64 ){ "IMAGE_FILE_MACHINE_IA64", COFF::IMAGE_FILE_MACHINE_IA64 }, | |||
348 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_M32R ){ "IMAGE_FILE_MACHINE_M32R", COFF::IMAGE_FILE_MACHINE_M32R }, | |||
349 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPS16 ){ "IMAGE_FILE_MACHINE_MIPS16", COFF::IMAGE_FILE_MACHINE_MIPS16 }, | |||
350 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU ){ "IMAGE_FILE_MACHINE_MIPSFPU", COFF::IMAGE_FILE_MACHINE_MIPSFPU }, | |||
351 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU16){ "IMAGE_FILE_MACHINE_MIPSFPU16", COFF::IMAGE_FILE_MACHINE_MIPSFPU16 }, | |||
352 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPC ){ "IMAGE_FILE_MACHINE_POWERPC", COFF::IMAGE_FILE_MACHINE_POWERPC }, | |||
353 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPCFP){ "IMAGE_FILE_MACHINE_POWERPCFP", COFF::IMAGE_FILE_MACHINE_POWERPCFP }, | |||
354 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_R4000 ){ "IMAGE_FILE_MACHINE_R4000", COFF::IMAGE_FILE_MACHINE_R4000 }, | |||
355 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3 ){ "IMAGE_FILE_MACHINE_SH3", COFF::IMAGE_FILE_MACHINE_SH3 }, | |||
356 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3DSP ){ "IMAGE_FILE_MACHINE_SH3DSP", COFF::IMAGE_FILE_MACHINE_SH3DSP }, | |||
357 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH4 ){ "IMAGE_FILE_MACHINE_SH4", COFF::IMAGE_FILE_MACHINE_SH4 }, | |||
358 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH5 ){ "IMAGE_FILE_MACHINE_SH5", COFF::IMAGE_FILE_MACHINE_SH5 }, | |||
359 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_THUMB ){ "IMAGE_FILE_MACHINE_THUMB", COFF::IMAGE_FILE_MACHINE_THUMB }, | |||
360 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_WCEMIPSV2){ "IMAGE_FILE_MACHINE_WCEMIPSV2", COFF::IMAGE_FILE_MACHINE_WCEMIPSV2 } | |||
361 | }; | |||
362 | ||||
363 | static const EnumEntry<COFF::Characteristics> ImageFileCharacteristics[] = { | |||
364 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_RELOCS_STRIPPED ){ "IMAGE_FILE_RELOCS_STRIPPED", COFF::IMAGE_FILE_RELOCS_STRIPPED }, | |||
365 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_EXECUTABLE_IMAGE ){ "IMAGE_FILE_EXECUTABLE_IMAGE", COFF::IMAGE_FILE_EXECUTABLE_IMAGE }, | |||
366 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LINE_NUMS_STRIPPED ){ "IMAGE_FILE_LINE_NUMS_STRIPPED", COFF::IMAGE_FILE_LINE_NUMS_STRIPPED }, | |||
367 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LOCAL_SYMS_STRIPPED ){ "IMAGE_FILE_LOCAL_SYMS_STRIPPED", COFF::IMAGE_FILE_LOCAL_SYMS_STRIPPED }, | |||
368 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_AGGRESSIVE_WS_TRIM ){ "IMAGE_FILE_AGGRESSIVE_WS_TRIM", COFF::IMAGE_FILE_AGGRESSIVE_WS_TRIM }, | |||
369 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LARGE_ADDRESS_AWARE ){ "IMAGE_FILE_LARGE_ADDRESS_AWARE", COFF::IMAGE_FILE_LARGE_ADDRESS_AWARE }, | |||
370 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_LO ){ "IMAGE_FILE_BYTES_REVERSED_LO", COFF::IMAGE_FILE_BYTES_REVERSED_LO }, | |||
371 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_32BIT_MACHINE ){ "IMAGE_FILE_32BIT_MACHINE", COFF::IMAGE_FILE_32BIT_MACHINE }, | |||
372 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DEBUG_STRIPPED ){ "IMAGE_FILE_DEBUG_STRIPPED", COFF::IMAGE_FILE_DEBUG_STRIPPED }, | |||
373 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP){ "IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP", COFF::IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP }, | |||
374 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_NET_RUN_FROM_SWAP ){ "IMAGE_FILE_NET_RUN_FROM_SWAP", COFF::IMAGE_FILE_NET_RUN_FROM_SWAP }, | |||
375 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_SYSTEM ){ "IMAGE_FILE_SYSTEM", COFF::IMAGE_FILE_SYSTEM }, | |||
376 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DLL ){ "IMAGE_FILE_DLL", COFF::IMAGE_FILE_DLL }, | |||
377 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_UP_SYSTEM_ONLY ){ "IMAGE_FILE_UP_SYSTEM_ONLY", COFF::IMAGE_FILE_UP_SYSTEM_ONLY }, | |||
378 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_HI ){ "IMAGE_FILE_BYTES_REVERSED_HI", COFF::IMAGE_FILE_BYTES_REVERSED_HI } | |||
379 | }; | |||
380 | ||||
381 | static const EnumEntry<COFF::WindowsSubsystem> PEWindowsSubsystem[] = { | |||
382 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_UNKNOWN ){ "IMAGE_SUBSYSTEM_UNKNOWN", COFF::IMAGE_SUBSYSTEM_UNKNOWN }, | |||
383 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_NATIVE ){ "IMAGE_SUBSYSTEM_NATIVE", COFF::IMAGE_SUBSYSTEM_NATIVE }, | |||
384 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_GUI ){ "IMAGE_SUBSYSTEM_WINDOWS_GUI", COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI }, | |||
385 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CUI ){ "IMAGE_SUBSYSTEM_WINDOWS_CUI", COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI }, | |||
386 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_POSIX_CUI ){ "IMAGE_SUBSYSTEM_POSIX_CUI", COFF::IMAGE_SUBSYSTEM_POSIX_CUI }, | |||
387 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI ){ "IMAGE_SUBSYSTEM_WINDOWS_CE_GUI", COFF::IMAGE_SUBSYSTEM_WINDOWS_CE_GUI }, | |||
388 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_APPLICATION ){ "IMAGE_SUBSYSTEM_EFI_APPLICATION", COFF::IMAGE_SUBSYSTEM_EFI_APPLICATION }, | |||
389 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER){ "IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER", COFF::IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER }, | |||
390 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER ){ "IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER", COFF::IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER }, | |||
391 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_ROM ){ "IMAGE_SUBSYSTEM_EFI_ROM", COFF::IMAGE_SUBSYSTEM_EFI_ROM }, | |||
392 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_XBOX ){ "IMAGE_SUBSYSTEM_XBOX", COFF::IMAGE_SUBSYSTEM_XBOX }, | |||
393 | }; | |||
394 | ||||
395 | static const EnumEntry<COFF::DLLCharacteristics> PEDLLCharacteristics[] = { | |||
396 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA ){ "IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA", COFF::IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA }, | |||
397 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE ){ "IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE", COFF::IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE }, | |||
398 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY ){ "IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY", COFF::IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY }, | |||
399 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NX_COMPAT ){ "IMAGE_DLL_CHARACTERISTICS_NX_COMPAT", COFF::IMAGE_DLL_CHARACTERISTICS_NX_COMPAT }, | |||
400 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION ){ "IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION", COFF::IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION }, | |||
401 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_SEH ){ "IMAGE_DLL_CHARACTERISTICS_NO_SEH", COFF::IMAGE_DLL_CHARACTERISTICS_NO_SEH }, | |||
402 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_BIND ){ "IMAGE_DLL_CHARACTERISTICS_NO_BIND", COFF::IMAGE_DLL_CHARACTERISTICS_NO_BIND }, | |||
403 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_APPCONTAINER ){ "IMAGE_DLL_CHARACTERISTICS_APPCONTAINER", COFF::IMAGE_DLL_CHARACTERISTICS_APPCONTAINER }, | |||
404 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER ){ "IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER", COFF::IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER }, | |||
405 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_GUARD_CF ){ "IMAGE_DLL_CHARACTERISTICS_GUARD_CF", COFF::IMAGE_DLL_CHARACTERISTICS_GUARD_CF }, | |||
406 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE){ "IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE", COFF::IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE }, | |||
407 | }; | |||
408 | ||||
409 | static const EnumEntry<COFF::SectionCharacteristics> | |||
410 | ImageSectionCharacteristics[] = { | |||
411 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_TYPE_NOLOAD ){ "IMAGE_SCN_TYPE_NOLOAD", COFF::IMAGE_SCN_TYPE_NOLOAD }, | |||
412 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_TYPE_NO_PAD ){ "IMAGE_SCN_TYPE_NO_PAD", COFF::IMAGE_SCN_TYPE_NO_PAD }, | |||
413 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_CODE ){ "IMAGE_SCN_CNT_CODE", COFF::IMAGE_SCN_CNT_CODE }, | |||
414 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_INITIALIZED_DATA ){ "IMAGE_SCN_CNT_INITIALIZED_DATA", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA }, | |||
415 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_UNINITIALIZED_DATA){ "IMAGE_SCN_CNT_UNINITIALIZED_DATA", COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA }, | |||
416 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_OTHER ){ "IMAGE_SCN_LNK_OTHER", COFF::IMAGE_SCN_LNK_OTHER }, | |||
417 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_INFO ){ "IMAGE_SCN_LNK_INFO", COFF::IMAGE_SCN_LNK_INFO }, | |||
418 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_REMOVE ){ "IMAGE_SCN_LNK_REMOVE", COFF::IMAGE_SCN_LNK_REMOVE }, | |||
419 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_COMDAT ){ "IMAGE_SCN_LNK_COMDAT", COFF::IMAGE_SCN_LNK_COMDAT }, | |||
420 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_GPREL ){ "IMAGE_SCN_GPREL", COFF::IMAGE_SCN_GPREL }, | |||
421 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PURGEABLE ){ "IMAGE_SCN_MEM_PURGEABLE", COFF::IMAGE_SCN_MEM_PURGEABLE }, | |||
422 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_16BIT ){ "IMAGE_SCN_MEM_16BIT", COFF::IMAGE_SCN_MEM_16BIT }, | |||
423 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_LOCKED ){ "IMAGE_SCN_MEM_LOCKED", COFF::IMAGE_SCN_MEM_LOCKED }, | |||
424 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PRELOAD ){ "IMAGE_SCN_MEM_PRELOAD", COFF::IMAGE_SCN_MEM_PRELOAD }, | |||
425 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1BYTES ){ "IMAGE_SCN_ALIGN_1BYTES", COFF::IMAGE_SCN_ALIGN_1BYTES }, | |||
426 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2BYTES ){ "IMAGE_SCN_ALIGN_2BYTES", COFF::IMAGE_SCN_ALIGN_2BYTES }, | |||
427 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4BYTES ){ "IMAGE_SCN_ALIGN_4BYTES", COFF::IMAGE_SCN_ALIGN_4BYTES }, | |||
428 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8BYTES ){ "IMAGE_SCN_ALIGN_8BYTES", COFF::IMAGE_SCN_ALIGN_8BYTES }, | |||
429 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_16BYTES ){ "IMAGE_SCN_ALIGN_16BYTES", COFF::IMAGE_SCN_ALIGN_16BYTES }, | |||
430 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_32BYTES ){ "IMAGE_SCN_ALIGN_32BYTES", COFF::IMAGE_SCN_ALIGN_32BYTES }, | |||
431 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_64BYTES ){ "IMAGE_SCN_ALIGN_64BYTES", COFF::IMAGE_SCN_ALIGN_64BYTES }, | |||
432 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_128BYTES ){ "IMAGE_SCN_ALIGN_128BYTES", COFF::IMAGE_SCN_ALIGN_128BYTES }, | |||
433 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_256BYTES ){ "IMAGE_SCN_ALIGN_256BYTES", COFF::IMAGE_SCN_ALIGN_256BYTES }, | |||
434 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_512BYTES ){ "IMAGE_SCN_ALIGN_512BYTES", COFF::IMAGE_SCN_ALIGN_512BYTES }, | |||
435 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1024BYTES ){ "IMAGE_SCN_ALIGN_1024BYTES", COFF::IMAGE_SCN_ALIGN_1024BYTES }, | |||
436 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2048BYTES ){ "IMAGE_SCN_ALIGN_2048BYTES", COFF::IMAGE_SCN_ALIGN_2048BYTES }, | |||
437 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4096BYTES ){ "IMAGE_SCN_ALIGN_4096BYTES", COFF::IMAGE_SCN_ALIGN_4096BYTES }, | |||
438 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8192BYTES ){ "IMAGE_SCN_ALIGN_8192BYTES", COFF::IMAGE_SCN_ALIGN_8192BYTES }, | |||
439 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_NRELOC_OVFL ){ "IMAGE_SCN_LNK_NRELOC_OVFL", COFF::IMAGE_SCN_LNK_NRELOC_OVFL }, | |||
440 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_DISCARDABLE ){ "IMAGE_SCN_MEM_DISCARDABLE", COFF::IMAGE_SCN_MEM_DISCARDABLE }, | |||
441 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_CACHED ){ "IMAGE_SCN_MEM_NOT_CACHED", COFF::IMAGE_SCN_MEM_NOT_CACHED }, | |||
442 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_PAGED ){ "IMAGE_SCN_MEM_NOT_PAGED", COFF::IMAGE_SCN_MEM_NOT_PAGED }, | |||
443 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_SHARED ){ "IMAGE_SCN_MEM_SHARED", COFF::IMAGE_SCN_MEM_SHARED }, | |||
444 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_EXECUTE ){ "IMAGE_SCN_MEM_EXECUTE", COFF::IMAGE_SCN_MEM_EXECUTE }, | |||
445 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_READ ){ "IMAGE_SCN_MEM_READ", COFF::IMAGE_SCN_MEM_READ }, | |||
446 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_WRITE ){ "IMAGE_SCN_MEM_WRITE", COFF::IMAGE_SCN_MEM_WRITE } | |||
447 | }; | |||
448 | ||||
449 | static const EnumEntry<COFF::SymbolBaseType> ImageSymType[] = { | |||
450 | { "Null" , COFF::IMAGE_SYM_TYPE_NULL }, | |||
451 | { "Void" , COFF::IMAGE_SYM_TYPE_VOID }, | |||
452 | { "Char" , COFF::IMAGE_SYM_TYPE_CHAR }, | |||
453 | { "Short" , COFF::IMAGE_SYM_TYPE_SHORT }, | |||
454 | { "Int" , COFF::IMAGE_SYM_TYPE_INT }, | |||
455 | { "Long" , COFF::IMAGE_SYM_TYPE_LONG }, | |||
456 | { "Float" , COFF::IMAGE_SYM_TYPE_FLOAT }, | |||
457 | { "Double", COFF::IMAGE_SYM_TYPE_DOUBLE }, | |||
458 | { "Struct", COFF::IMAGE_SYM_TYPE_STRUCT }, | |||
459 | { "Union" , COFF::IMAGE_SYM_TYPE_UNION }, | |||
460 | { "Enum" , COFF::IMAGE_SYM_TYPE_ENUM }, | |||
461 | { "MOE" , COFF::IMAGE_SYM_TYPE_MOE }, | |||
462 | { "Byte" , COFF::IMAGE_SYM_TYPE_BYTE }, | |||
463 | { "Word" , COFF::IMAGE_SYM_TYPE_WORD }, | |||
464 | { "UInt" , COFF::IMAGE_SYM_TYPE_UINT }, | |||
465 | { "DWord" , COFF::IMAGE_SYM_TYPE_DWORD } | |||
466 | }; | |||
467 | ||||
468 | static const EnumEntry<COFF::SymbolComplexType> ImageSymDType[] = { | |||
469 | { "Null" , COFF::IMAGE_SYM_DTYPE_NULL }, | |||
470 | { "Pointer" , COFF::IMAGE_SYM_DTYPE_POINTER }, | |||
471 | { "Function", COFF::IMAGE_SYM_DTYPE_FUNCTION }, | |||
472 | { "Array" , COFF::IMAGE_SYM_DTYPE_ARRAY } | |||
473 | }; | |||
474 | ||||
475 | static const EnumEntry<COFF::SymbolStorageClass> ImageSymClass[] = { | |||
476 | { "EndOfFunction" , COFF::IMAGE_SYM_CLASS_END_OF_FUNCTION }, | |||
477 | { "Null" , COFF::IMAGE_SYM_CLASS_NULL }, | |||
478 | { "Automatic" , COFF::IMAGE_SYM_CLASS_AUTOMATIC }, | |||
479 | { "External" , COFF::IMAGE_SYM_CLASS_EXTERNAL }, | |||
480 | { "Static" , COFF::IMAGE_SYM_CLASS_STATIC }, | |||
481 | { "Register" , COFF::IMAGE_SYM_CLASS_REGISTER }, | |||
482 | { "ExternalDef" , COFF::IMAGE_SYM_CLASS_EXTERNAL_DEF }, | |||
483 | { "Label" , COFF::IMAGE_SYM_CLASS_LABEL }, | |||
484 | { "UndefinedLabel" , COFF::IMAGE_SYM_CLASS_UNDEFINED_LABEL }, | |||
485 | { "MemberOfStruct" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_STRUCT }, | |||
486 | { "Argument" , COFF::IMAGE_SYM_CLASS_ARGUMENT }, | |||
487 | { "StructTag" , COFF::IMAGE_SYM_CLASS_STRUCT_TAG }, | |||
488 | { "MemberOfUnion" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_UNION }, | |||
489 | { "UnionTag" , COFF::IMAGE_SYM_CLASS_UNION_TAG }, | |||
490 | { "TypeDefinition" , COFF::IMAGE_SYM_CLASS_TYPE_DEFINITION }, | |||
491 | { "UndefinedStatic", COFF::IMAGE_SYM_CLASS_UNDEFINED_STATIC }, | |||
492 | { "EnumTag" , COFF::IMAGE_SYM_CLASS_ENUM_TAG }, | |||
493 | { "MemberOfEnum" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_ENUM }, | |||
494 | { "RegisterParam" , COFF::IMAGE_SYM_CLASS_REGISTER_PARAM }, | |||
495 | { "BitField" , COFF::IMAGE_SYM_CLASS_BIT_FIELD }, | |||
496 | { "Block" , COFF::IMAGE_SYM_CLASS_BLOCK }, | |||
497 | { "Function" , COFF::IMAGE_SYM_CLASS_FUNCTION }, | |||
498 | { "EndOfStruct" , COFF::IMAGE_SYM_CLASS_END_OF_STRUCT }, | |||
499 | { "File" , COFF::IMAGE_SYM_CLASS_FILE }, | |||
500 | { "Section" , COFF::IMAGE_SYM_CLASS_SECTION }, | |||
501 | { "WeakExternal" , COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL }, | |||
502 | { "CLRToken" , COFF::IMAGE_SYM_CLASS_CLR_TOKEN } | |||
503 | }; | |||
504 | ||||
505 | static const EnumEntry<COFF::COMDATType> ImageCOMDATSelect[] = { | |||
506 | { "NoDuplicates", COFF::IMAGE_COMDAT_SELECT_NODUPLICATES }, | |||
507 | { "Any" , COFF::IMAGE_COMDAT_SELECT_ANY }, | |||
508 | { "SameSize" , COFF::IMAGE_COMDAT_SELECT_SAME_SIZE }, | |||
509 | { "ExactMatch" , COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH }, | |||
510 | { "Associative" , COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE }, | |||
511 | { "Largest" , COFF::IMAGE_COMDAT_SELECT_LARGEST }, | |||
512 | { "Newest" , COFF::IMAGE_COMDAT_SELECT_NEWEST } | |||
513 | }; | |||
514 | ||||
515 | static const EnumEntry<COFF::DebugType> ImageDebugType[] = { | |||
516 | { "Unknown" , COFF::IMAGE_DEBUG_TYPE_UNKNOWN }, | |||
517 | { "COFF" , COFF::IMAGE_DEBUG_TYPE_COFF }, | |||
518 | { "CodeView" , COFF::IMAGE_DEBUG_TYPE_CODEVIEW }, | |||
519 | { "FPO" , COFF::IMAGE_DEBUG_TYPE_FPO }, | |||
520 | { "Misc" , COFF::IMAGE_DEBUG_TYPE_MISC }, | |||
521 | { "Exception" , COFF::IMAGE_DEBUG_TYPE_EXCEPTION }, | |||
522 | { "Fixup" , COFF::IMAGE_DEBUG_TYPE_FIXUP }, | |||
523 | { "OmapToSrc" , COFF::IMAGE_DEBUG_TYPE_OMAP_TO_SRC }, | |||
524 | { "OmapFromSrc", COFF::IMAGE_DEBUG_TYPE_OMAP_FROM_SRC }, | |||
525 | { "Borland" , COFF::IMAGE_DEBUG_TYPE_BORLAND }, | |||
526 | { "Reserved10" , COFF::IMAGE_DEBUG_TYPE_RESERVED10 }, | |||
527 | { "CLSID" , COFF::IMAGE_DEBUG_TYPE_CLSID }, | |||
528 | { "VCFeature" , COFF::IMAGE_DEBUG_TYPE_VC_FEATURE }, | |||
529 | { "POGO" , COFF::IMAGE_DEBUG_TYPE_POGO }, | |||
530 | { "ILTCG" , COFF::IMAGE_DEBUG_TYPE_ILTCG }, | |||
531 | { "MPX" , COFF::IMAGE_DEBUG_TYPE_MPX }, | |||
532 | { "Repro" , COFF::IMAGE_DEBUG_TYPE_REPRO }, | |||
533 | }; | |||
534 | ||||
535 | static const EnumEntry<COFF::WeakExternalCharacteristics> | |||
536 | WeakExternalCharacteristics[] = { | |||
537 | { "NoLibrary", COFF::IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY }, | |||
538 | { "Library" , COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY }, | |||
539 | { "Alias" , COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS } | |||
540 | }; | |||
541 | ||||
542 | static const EnumEntry<uint32_t> SubSectionTypes[] = { | |||
543 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, Symbols){ "Symbols", std::underlying_type<DebugSubsectionKind>:: type(DebugSubsectionKind::Symbols) }, | |||
544 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, Lines){ "Lines", std::underlying_type<DebugSubsectionKind>::type (DebugSubsectionKind::Lines) }, | |||
545 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, StringTable){ "StringTable", std::underlying_type<DebugSubsectionKind> ::type(DebugSubsectionKind::StringTable) }, | |||
546 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, FileChecksums){ "FileChecksums", std::underlying_type<DebugSubsectionKind >::type(DebugSubsectionKind::FileChecksums) }, | |||
547 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, FrameData){ "FrameData", std::underlying_type<DebugSubsectionKind> ::type(DebugSubsectionKind::FrameData) }, | |||
548 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, InlineeLines){ "InlineeLines", std::underlying_type<DebugSubsectionKind >::type(DebugSubsectionKind::InlineeLines) }, | |||
549 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, CrossScopeImports){ "CrossScopeImports", std::underlying_type<DebugSubsectionKind >::type(DebugSubsectionKind::CrossScopeImports) }, | |||
550 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, CrossScopeExports){ "CrossScopeExports", std::underlying_type<DebugSubsectionKind >::type(DebugSubsectionKind::CrossScopeExports) }, | |||
551 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, ILLines){ "ILLines", std::underlying_type<DebugSubsectionKind>:: type(DebugSubsectionKind::ILLines) }, | |||
552 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, FuncMDTokenMap){ "FuncMDTokenMap", std::underlying_type<DebugSubsectionKind >::type(DebugSubsectionKind::FuncMDTokenMap) }, | |||
553 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, TypeMDTokenMap){ "TypeMDTokenMap", std::underlying_type<DebugSubsectionKind >::type(DebugSubsectionKind::TypeMDTokenMap) }, | |||
554 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, MergedAssemblyInput){ "MergedAssemblyInput", std::underlying_type<DebugSubsectionKind >::type(DebugSubsectionKind::MergedAssemblyInput) }, | |||
555 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, CoffSymbolRVA){ "CoffSymbolRVA", std::underlying_type<DebugSubsectionKind >::type(DebugSubsectionKind::CoffSymbolRVA) }, | |||
556 | }; | |||
557 | ||||
558 | static const EnumEntry<uint32_t> FrameDataFlags[] = { | |||
559 | LLVM_READOBJ_ENUM_ENT(FrameData, HasSEH){ "HasSEH", FrameData::HasSEH }, | |||
560 | LLVM_READOBJ_ENUM_ENT(FrameData, HasEH){ "HasEH", FrameData::HasEH }, | |||
561 | LLVM_READOBJ_ENUM_ENT(FrameData, IsFunctionStart){ "IsFunctionStart", FrameData::IsFunctionStart }, | |||
562 | }; | |||
563 | ||||
564 | static const EnumEntry<uint8_t> FileChecksumKindNames[] = { | |||
565 | LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, None){ "None", std::underlying_type<FileChecksumKind>::type( FileChecksumKind::None) }, | |||
566 | LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, MD5){ "MD5", std::underlying_type<FileChecksumKind>::type(FileChecksumKind ::MD5) }, | |||
567 | LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, SHA1){ "SHA1", std::underlying_type<FileChecksumKind>::type( FileChecksumKind::SHA1) }, | |||
568 | LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, SHA256){ "SHA256", std::underlying_type<FileChecksumKind>::type (FileChecksumKind::SHA256) }, | |||
569 | }; | |||
570 | ||||
571 | static const EnumEntry<COFF::ResourceTypeID> ResourceTypeNames[]{ | |||
572 | {"kRT_CURSOR (ID 1)", COFF::RID_Cursor}, | |||
573 | {"kRT_BITMAP (ID 2)", COFF::RID_Bitmap}, | |||
574 | {"kRT_ICON (ID 3)", COFF::RID_Icon}, | |||
575 | {"kRT_MENU (ID 4)", COFF::RID_Menu}, | |||
576 | {"kRT_DIALOG (ID 5)", COFF::RID_Dialog}, | |||
577 | {"kRT_STRING (ID 6)", COFF::RID_String}, | |||
578 | {"kRT_FONTDIR (ID 7)", COFF::RID_FontDir}, | |||
579 | {"kRT_FONT (ID 8)", COFF::RID_Font}, | |||
580 | {"kRT_ACCELERATOR (ID 9)", COFF::RID_Accelerator}, | |||
581 | {"kRT_RCDATA (ID 10)", COFF::RID_RCData}, | |||
582 | {"kRT_MESSAGETABLE (ID 11)", COFF::RID_MessageTable}, | |||
583 | {"kRT_GROUP_CURSOR (ID 12)", COFF::RID_Group_Cursor}, | |||
584 | {"kRT_GROUP_ICON (ID 14)", COFF::RID_Group_Icon}, | |||
585 | {"kRT_VERSION (ID 16)", COFF::RID_Version}, | |||
586 | {"kRT_DLGINCLUDE (ID 17)", COFF::RID_DLGInclude}, | |||
587 | {"kRT_PLUGPLAY (ID 19)", COFF::RID_PlugPlay}, | |||
588 | {"kRT_VXD (ID 20)", COFF::RID_VXD}, | |||
589 | {"kRT_ANICURSOR (ID 21)", COFF::RID_AniCursor}, | |||
590 | {"kRT_ANIICON (ID 22)", COFF::RID_AniIcon}, | |||
591 | {"kRT_HTML (ID 23)", COFF::RID_HTML}, | |||
592 | {"kRT_MANIFEST (ID 24)", COFF::RID_Manifest}}; | |||
593 | ||||
594 | template <typename T> | |||
595 | static std::error_code getSymbolAuxData(const COFFObjectFile *Obj, | |||
596 | COFFSymbolRef Symbol, | |||
597 | uint8_t AuxSymbolIdx, const T *&Aux) { | |||
598 | ArrayRef<uint8_t> AuxData = Obj->getSymbolAuxData(Symbol); | |||
599 | AuxData = AuxData.slice(AuxSymbolIdx * Obj->getSymbolTableEntrySize()); | |||
600 | Aux = reinterpret_cast<const T*>(AuxData.data()); | |||
601 | return readobj_error::success; | |||
602 | } | |||
603 | ||||
604 | void COFFDumper::cacheRelocations() { | |||
605 | if (RelocCached) | |||
606 | return; | |||
607 | RelocCached = true; | |||
608 | ||||
609 | for (const SectionRef &S : Obj->sections()) { | |||
610 | const coff_section *Section = Obj->getCOFFSection(S); | |||
611 | ||||
612 | for (const RelocationRef &Reloc : S.relocations()) | |||
613 | RelocMap[Section].push_back(Reloc); | |||
614 | ||||
615 | // Sort relocations by address. | |||
616 | llvm::sort(RelocMap[Section], relocAddressLess); | |||
617 | } | |||
618 | } | |||
619 | ||||
620 | void COFFDumper::printDataDirectory(uint32_t Index, const std::string &FieldName) { | |||
621 | const data_directory *Data; | |||
622 | if (Obj->getDataDirectory(Index, Data)) | |||
623 | return; | |||
624 | W.printHex(FieldName + "RVA", Data->RelativeVirtualAddress); | |||
625 | W.printHex(FieldName + "Size", Data->Size); | |||
626 | } | |||
627 | ||||
628 | void COFFDumper::printFileHeaders() { | |||
629 | time_t TDS = Obj->getTimeDateStamp(); | |||
630 | char FormattedTime[20] = { }; | |||
631 | strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS)); | |||
632 | ||||
633 | { | |||
634 | DictScope D(W, "ImageFileHeader"); | |||
635 | W.printEnum ("Machine", Obj->getMachine(), | |||
636 | makeArrayRef(ImageFileMachineType)); | |||
637 | W.printNumber("SectionCount", Obj->getNumberOfSections()); | |||
638 | W.printHex ("TimeDateStamp", FormattedTime, Obj->getTimeDateStamp()); | |||
639 | W.printHex ("PointerToSymbolTable", Obj->getPointerToSymbolTable()); | |||
640 | W.printNumber("SymbolCount", Obj->getNumberOfSymbols()); | |||
641 | W.printNumber("OptionalHeaderSize", Obj->getSizeOfOptionalHeader()); | |||
642 | W.printFlags ("Characteristics", Obj->getCharacteristics(), | |||
643 | makeArrayRef(ImageFileCharacteristics)); | |||
644 | } | |||
645 | ||||
646 | // Print PE header. This header does not exist if this is an object file and | |||
647 | // not an executable. | |||
648 | const pe32_header *PEHeader = nullptr; | |||
649 | error(Obj->getPE32Header(PEHeader)); | |||
650 | if (PEHeader) | |||
651 | printPEHeader<pe32_header>(PEHeader); | |||
652 | ||||
653 | const pe32plus_header *PEPlusHeader = nullptr; | |||
654 | error(Obj->getPE32PlusHeader(PEPlusHeader)); | |||
655 | if (PEPlusHeader) | |||
656 | printPEHeader<pe32plus_header>(PEPlusHeader); | |||
657 | ||||
658 | if (const dos_header *DH = Obj->getDOSHeader()) | |||
659 | printDOSHeader(DH); | |||
660 | } | |||
661 | ||||
662 | void COFFDumper::printDOSHeader(const dos_header *DH) { | |||
663 | DictScope D(W, "DOSHeader"); | |||
664 | W.printString("Magic", StringRef(DH->Magic, sizeof(DH->Magic))); | |||
665 | W.printNumber("UsedBytesInTheLastPage", DH->UsedBytesInTheLastPage); | |||
666 | W.printNumber("FileSizeInPages", DH->FileSizeInPages); | |||
667 | W.printNumber("NumberOfRelocationItems", DH->NumberOfRelocationItems); | |||
668 | W.printNumber("HeaderSizeInParagraphs", DH->HeaderSizeInParagraphs); | |||
669 | W.printNumber("MinimumExtraParagraphs", DH->MinimumExtraParagraphs); | |||
670 | W.printNumber("MaximumExtraParagraphs", DH->MaximumExtraParagraphs); | |||
671 | W.printNumber("InitialRelativeSS", DH->InitialRelativeSS); | |||
672 | W.printNumber("InitialSP", DH->InitialSP); | |||
673 | W.printNumber("Checksum", DH->Checksum); | |||
674 | W.printNumber("InitialIP", DH->InitialIP); | |||
675 | W.printNumber("InitialRelativeCS", DH->InitialRelativeCS); | |||
676 | W.printNumber("AddressOfRelocationTable", DH->AddressOfRelocationTable); | |||
677 | W.printNumber("OverlayNumber", DH->OverlayNumber); | |||
678 | W.printNumber("OEMid", DH->OEMid); | |||
679 | W.printNumber("OEMinfo", DH->OEMinfo); | |||
680 | W.printNumber("AddressOfNewExeHeader", DH->AddressOfNewExeHeader); | |||
681 | } | |||
682 | ||||
683 | template <class PEHeader> | |||
684 | void COFFDumper::printPEHeader(const PEHeader *Hdr) { | |||
685 | DictScope D(W, "ImageOptionalHeader"); | |||
686 | W.printHex ("Magic", Hdr->Magic); | |||
687 | W.printNumber("MajorLinkerVersion", Hdr->MajorLinkerVersion); | |||
688 | W.printNumber("MinorLinkerVersion", Hdr->MinorLinkerVersion); | |||
689 | W.printNumber("SizeOfCode", Hdr->SizeOfCode); | |||
690 | W.printNumber("SizeOfInitializedData", Hdr->SizeOfInitializedData); | |||
691 | W.printNumber("SizeOfUninitializedData", Hdr->SizeOfUninitializedData); | |||
692 | W.printHex ("AddressOfEntryPoint", Hdr->AddressOfEntryPoint); | |||
693 | W.printHex ("BaseOfCode", Hdr->BaseOfCode); | |||
694 | printBaseOfDataField(Hdr); | |||
695 | W.printHex ("ImageBase", Hdr->ImageBase); | |||
696 | W.printNumber("SectionAlignment", Hdr->SectionAlignment); | |||
697 | W.printNumber("FileAlignment", Hdr->FileAlignment); | |||
698 | W.printNumber("MajorOperatingSystemVersion", | |||
699 | Hdr->MajorOperatingSystemVersion); | |||
700 | W.printNumber("MinorOperatingSystemVersion", | |||
701 | Hdr->MinorOperatingSystemVersion); | |||
702 | W.printNumber("MajorImageVersion", Hdr->MajorImageVersion); | |||
703 | W.printNumber("MinorImageVersion", Hdr->MinorImageVersion); | |||
704 | W.printNumber("MajorSubsystemVersion", Hdr->MajorSubsystemVersion); | |||
705 | W.printNumber("MinorSubsystemVersion", Hdr->MinorSubsystemVersion); | |||
706 | W.printNumber("SizeOfImage", Hdr->SizeOfImage); | |||
707 | W.printNumber("SizeOfHeaders", Hdr->SizeOfHeaders); | |||
708 | W.printEnum ("Subsystem", Hdr->Subsystem, makeArrayRef(PEWindowsSubsystem)); | |||
709 | W.printFlags ("Characteristics", Hdr->DLLCharacteristics, | |||
710 | makeArrayRef(PEDLLCharacteristics)); | |||
711 | W.printNumber("SizeOfStackReserve", Hdr->SizeOfStackReserve); | |||
712 | W.printNumber("SizeOfStackCommit", Hdr->SizeOfStackCommit); | |||
713 | W.printNumber("SizeOfHeapReserve", Hdr->SizeOfHeapReserve); | |||
714 | W.printNumber("SizeOfHeapCommit", Hdr->SizeOfHeapCommit); | |||
715 | W.printNumber("NumberOfRvaAndSize", Hdr->NumberOfRvaAndSize); | |||
716 | ||||
717 | if (Hdr->NumberOfRvaAndSize > 0) { | |||
718 | DictScope D(W, "DataDirectory"); | |||
719 | static const char * const directory[] = { | |||
720 | "ExportTable", "ImportTable", "ResourceTable", "ExceptionTable", | |||
721 | "CertificateTable", "BaseRelocationTable", "Debug", "Architecture", | |||
722 | "GlobalPtr", "TLSTable", "LoadConfigTable", "BoundImport", "IAT", | |||
723 | "DelayImportDescriptor", "CLRRuntimeHeader", "Reserved" | |||
724 | }; | |||
725 | ||||
726 | for (uint32_t i = 0; i < Hdr->NumberOfRvaAndSize; ++i) | |||
727 | printDataDirectory(i, directory[i]); | |||
728 | } | |||
729 | } | |||
730 | ||||
731 | void COFFDumper::printCOFFDebugDirectory() { | |||
732 | ListScope LS(W, "DebugDirectory"); | |||
733 | for (const debug_directory &D : Obj->debug_directories()) { | |||
734 | char FormattedTime[20] = {}; | |||
735 | time_t TDS = D.TimeDateStamp; | |||
736 | strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS)); | |||
737 | DictScope S(W, "DebugEntry"); | |||
738 | W.printHex("Characteristics", D.Characteristics); | |||
739 | W.printHex("TimeDateStamp", FormattedTime, D.TimeDateStamp); | |||
740 | W.printHex("MajorVersion", D.MajorVersion); | |||
741 | W.printHex("MinorVersion", D.MinorVersion); | |||
742 | W.printEnum("Type", D.Type, makeArrayRef(ImageDebugType)); | |||
743 | W.printHex("SizeOfData", D.SizeOfData); | |||
744 | W.printHex("AddressOfRawData", D.AddressOfRawData); | |||
745 | W.printHex("PointerToRawData", D.PointerToRawData); | |||
746 | if (D.Type == COFF::IMAGE_DEBUG_TYPE_CODEVIEW) { | |||
747 | const codeview::DebugInfo *DebugInfo; | |||
748 | StringRef PDBFileName; | |||
749 | error(Obj->getDebugPDBInfo(&D, DebugInfo, PDBFileName)); | |||
750 | DictScope PDBScope(W, "PDBInfo"); | |||
751 | W.printHex("PDBSignature", DebugInfo->Signature.CVSignature); | |||
752 | if (DebugInfo->Signature.CVSignature == OMF::Signature::PDB70) { | |||
753 | W.printBinary("PDBGUID", makeArrayRef(DebugInfo->PDB70.Signature)); | |||
754 | W.printNumber("PDBAge", DebugInfo->PDB70.Age); | |||
755 | W.printString("PDBFileName", PDBFileName); | |||
756 | } | |||
757 | } else if (D.SizeOfData != 0) { | |||
758 | // FIXME: Type values of 12 and 13 are commonly observed but are not in | |||
759 | // the documented type enum. Figure out what they mean. | |||
760 | ArrayRef<uint8_t> RawData; | |||
761 | error( | |||
762 | Obj->getRvaAndSizeAsBytes(D.AddressOfRawData, D.SizeOfData, RawData)); | |||
763 | W.printBinaryBlock("RawData", RawData); | |||
764 | } | |||
765 | } | |||
766 | } | |||
767 | ||||
768 | void COFFDumper::printRVATable(uint64_t TableVA, uint64_t Count, | |||
769 | uint64_t EntrySize, PrintExtraCB PrintExtra) { | |||
770 | uintptr_t TableStart, TableEnd; | |||
771 | error(Obj->getVaPtr(TableVA, TableStart)); | |||
772 | error(Obj->getVaPtr(TableVA + Count * EntrySize - 1, TableEnd)); | |||
773 | TableEnd++; | |||
774 | for (uintptr_t I = TableStart; I < TableEnd; I += EntrySize) { | |||
775 | uint32_t RVA = *reinterpret_cast<const ulittle32_t *>(I); | |||
776 | raw_ostream &OS = W.startLine(); | |||
777 | OS << W.hex(Obj->getImageBase() + RVA); | |||
778 | if (PrintExtra) | |||
779 | PrintExtra(OS, reinterpret_cast<const uint8_t *>(I)); | |||
780 | OS << '\n'; | |||
781 | } | |||
782 | } | |||
783 | ||||
784 | void COFFDumper::printCOFFLoadConfig() { | |||
785 | LoadConfigTables Tables; | |||
786 | if (Obj->is64()) | |||
787 | printCOFFLoadConfig(Obj->getLoadConfig64(), Tables); | |||
788 | else | |||
789 | printCOFFLoadConfig(Obj->getLoadConfig32(), Tables); | |||
790 | ||||
791 | if (Tables.SEHTableVA) { | |||
792 | ListScope LS(W, "SEHTable"); | |||
793 | printRVATable(Tables.SEHTableVA, Tables.SEHTableCount, 4); | |||
794 | } | |||
795 | ||||
796 | if (Tables.GuardFidTableVA) { | |||
797 | ListScope LS(W, "GuardFidTable"); | |||
798 | if (Tables.GuardFlags & uint32_t(coff_guard_flags::FidTableHasFlags)) { | |||
799 | auto PrintGuardFlags = [](raw_ostream &OS, const uint8_t *Entry) { | |||
800 | uint8_t Flags = *reinterpret_cast<const uint8_t *>(Entry + 4); | |||
801 | if (Flags) | |||
802 | OS << " flags " << utohexstr(Flags); | |||
803 | }; | |||
804 | printRVATable(Tables.GuardFidTableVA, Tables.GuardFidTableCount, 5, | |||
805 | PrintGuardFlags); | |||
806 | } else { | |||
807 | printRVATable(Tables.GuardFidTableVA, Tables.GuardFidTableCount, 4); | |||
808 | } | |||
809 | } | |||
810 | ||||
811 | if (Tables.GuardLJmpTableVA) { | |||
812 | ListScope LS(W, "GuardLJmpTable"); | |||
813 | printRVATable(Tables.GuardLJmpTableVA, Tables.GuardLJmpTableCount, 4); | |||
814 | } | |||
815 | } | |||
816 | ||||
817 | template <typename T> | |||
818 | void COFFDumper::printCOFFLoadConfig(const T *Conf, LoadConfigTables &Tables) { | |||
819 | if (!Conf) | |||
820 | return; | |||
821 | ||||
822 | ListScope LS(W, "LoadConfig"); | |||
823 | char FormattedTime[20] = {}; | |||
824 | time_t TDS = Conf->TimeDateStamp; | |||
825 | strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS)); | |||
826 | W.printHex("Size", Conf->Size); | |||
827 | ||||
828 | // Print everything before SecurityCookie. The vast majority of images today | |||
829 | // have all these fields. | |||
830 | if (Conf->Size < offsetof(T, SEHandlerTable)__builtin_offsetof(T, SEHandlerTable)) | |||
831 | return; | |||
832 | W.printHex("TimeDateStamp", FormattedTime, TDS); | |||
833 | W.printHex("MajorVersion", Conf->MajorVersion); | |||
834 | W.printHex("MinorVersion", Conf->MinorVersion); | |||
835 | W.printHex("GlobalFlagsClear", Conf->GlobalFlagsClear); | |||
836 | W.printHex("GlobalFlagsSet", Conf->GlobalFlagsSet); | |||
837 | W.printHex("CriticalSectionDefaultTimeout", | |||
838 | Conf->CriticalSectionDefaultTimeout); | |||
839 | W.printHex("DeCommitFreeBlockThreshold", Conf->DeCommitFreeBlockThreshold); | |||
840 | W.printHex("DeCommitTotalFreeThreshold", Conf->DeCommitTotalFreeThreshold); | |||
841 | W.printHex("LockPrefixTable", Conf->LockPrefixTable); | |||
842 | W.printHex("MaximumAllocationSize", Conf->MaximumAllocationSize); | |||
843 | W.printHex("VirtualMemoryThreshold", Conf->VirtualMemoryThreshold); | |||
844 | W.printHex("ProcessHeapFlags", Conf->ProcessHeapFlags); | |||
845 | W.printHex("ProcessAffinityMask", Conf->ProcessAffinityMask); | |||
846 | W.printHex("CSDVersion", Conf->CSDVersion); | |||
847 | W.printHex("DependentLoadFlags", Conf->DependentLoadFlags); | |||
848 | W.printHex("EditList", Conf->EditList); | |||
849 | W.printHex("SecurityCookie", Conf->SecurityCookie); | |||
850 | ||||
851 | // Print the safe SEH table if present. | |||
852 | if (Conf->Size < offsetof(coff_load_configuration32, GuardCFCheckFunction)__builtin_offsetof(coff_load_configuration32, GuardCFCheckFunction )) | |||
853 | return; | |||
854 | W.printHex("SEHandlerTable", Conf->SEHandlerTable); | |||
855 | W.printNumber("SEHandlerCount", Conf->SEHandlerCount); | |||
856 | ||||
857 | Tables.SEHTableVA = Conf->SEHandlerTable; | |||
858 | Tables.SEHTableCount = Conf->SEHandlerCount; | |||
859 | ||||
860 | // Print everything before CodeIntegrity. (2015) | |||
861 | if (Conf->Size < offsetof(T, CodeIntegrity)__builtin_offsetof(T, CodeIntegrity)) | |||
862 | return; | |||
863 | W.printHex("GuardCFCheckFunction", Conf->GuardCFCheckFunction); | |||
864 | W.printHex("GuardCFCheckDispatch", Conf->GuardCFCheckDispatch); | |||
865 | W.printHex("GuardCFFunctionTable", Conf->GuardCFFunctionTable); | |||
866 | W.printNumber("GuardCFFunctionCount", Conf->GuardCFFunctionCount); | |||
867 | W.printHex("GuardFlags", Conf->GuardFlags); | |||
868 | ||||
869 | Tables.GuardFidTableVA = Conf->GuardCFFunctionTable; | |||
870 | Tables.GuardFidTableCount = Conf->GuardCFFunctionCount; | |||
871 | Tables.GuardFlags = Conf->GuardFlags; | |||
872 | ||||
873 | // Print the rest. (2017) | |||
874 | if (Conf->Size < sizeof(T)) | |||
875 | return; | |||
876 | W.printHex("GuardAddressTakenIatEntryTable", | |||
877 | Conf->GuardAddressTakenIatEntryTable); | |||
878 | W.printNumber("GuardAddressTakenIatEntryCount", | |||
879 | Conf->GuardAddressTakenIatEntryCount); | |||
880 | W.printHex("GuardLongJumpTargetTable", Conf->GuardLongJumpTargetTable); | |||
881 | W.printNumber("GuardLongJumpTargetCount", Conf->GuardLongJumpTargetCount); | |||
882 | W.printHex("DynamicValueRelocTable", Conf->DynamicValueRelocTable); | |||
883 | W.printHex("CHPEMetadataPointer", Conf->CHPEMetadataPointer); | |||
884 | W.printHex("GuardRFFailureRoutine", Conf->GuardRFFailureRoutine); | |||
885 | W.printHex("GuardRFFailureRoutineFunctionPointer", | |||
886 | Conf->GuardRFFailureRoutineFunctionPointer); | |||
887 | W.printHex("DynamicValueRelocTableOffset", | |||
888 | Conf->DynamicValueRelocTableOffset); | |||
889 | W.printNumber("DynamicValueRelocTableSection", | |||
890 | Conf->DynamicValueRelocTableSection); | |||
891 | W.printHex("GuardRFVerifyStackPointerFunctionPointer", | |||
892 | Conf->GuardRFVerifyStackPointerFunctionPointer); | |||
893 | W.printHex("HotPatchTableOffset", Conf->HotPatchTableOffset); | |||
894 | ||||
895 | Tables.GuardLJmpTableVA = Conf->GuardLongJumpTargetTable; | |||
896 | Tables.GuardLJmpTableCount = Conf->GuardLongJumpTargetCount; | |||
897 | } | |||
898 | ||||
899 | void COFFDumper::printBaseOfDataField(const pe32_header *Hdr) { | |||
900 | W.printHex("BaseOfData", Hdr->BaseOfData); | |||
901 | } | |||
902 | ||||
903 | void COFFDumper::printBaseOfDataField(const pe32plus_header *) {} | |||
904 | ||||
905 | void COFFDumper::printCodeViewDebugInfo() { | |||
906 | // Print types first to build CVUDTNames, then print symbols. | |||
907 | for (const SectionRef &S : Obj->sections()) { | |||
908 | StringRef SectionName; | |||
909 | error(S.getName(SectionName)); | |||
910 | // .debug$T is a standard CodeView type section, while .debug$P is the same | |||
911 | // format but used for MSVC precompiled header object files. | |||
912 | if (SectionName == ".debug$T" || SectionName == ".debug$P") | |||
913 | printCodeViewTypeSection(SectionName, S); | |||
914 | } | |||
915 | for (const SectionRef &S : Obj->sections()) { | |||
916 | StringRef SectionName; | |||
917 | error(S.getName(SectionName)); | |||
918 | if (SectionName == ".debug$S") | |||
919 | printCodeViewSymbolSection(SectionName, S); | |||
920 | } | |||
921 | } | |||
922 | ||||
923 | void COFFDumper::initializeFileAndStringTables(BinaryStreamReader &Reader) { | |||
924 | while (Reader.bytesRemaining() > 0 && | |||
925 | (!CVFileChecksumTable.valid() || !CVStringTable.valid())) { | |||
926 | // The section consists of a number of subsection in the following format: | |||
927 | // |SubSectionType|SubSectionSize|Contents...| | |||
928 | uint32_t SubType, SubSectionSize; | |||
929 | error(Reader.readInteger(SubType)); | |||
930 | error(Reader.readInteger(SubSectionSize)); | |||
931 | ||||
932 | StringRef Contents; | |||
933 | error(Reader.readFixedString(Contents, SubSectionSize)); | |||
934 | ||||
935 | BinaryStreamRef ST(Contents, support::little); | |||
936 | switch (DebugSubsectionKind(SubType)) { | |||
937 | case DebugSubsectionKind::FileChecksums: | |||
938 | error(CVFileChecksumTable.initialize(ST)); | |||
939 | break; | |||
940 | case DebugSubsectionKind::StringTable: | |||
941 | error(CVStringTable.initialize(ST)); | |||
942 | break; | |||
943 | default: | |||
944 | break; | |||
945 | } | |||
946 | ||||
947 | uint32_t PaddedSize = alignTo(SubSectionSize, 4); | |||
948 | error(Reader.skip(PaddedSize - SubSectionSize)); | |||
949 | } | |||
950 | } | |||
951 | ||||
952 | void COFFDumper::printCodeViewSymbolSection(StringRef SectionName, | |||
953 | const SectionRef &Section) { | |||
954 | StringRef SectionContents; | |||
955 | error(Section.getContents(SectionContents)); | |||
956 | StringRef Data = SectionContents; | |||
957 | ||||
958 | SmallVector<StringRef, 10> FunctionNames; | |||
959 | StringMap<StringRef> FunctionLineTables; | |||
960 | ||||
961 | ListScope D(W, "CodeViewDebugInfo"); | |||
962 | // Print the section to allow correlation with printSections. | |||
963 | W.printNumber("Section", SectionName, Obj->getSectionID(Section)); | |||
964 | ||||
965 | uint32_t Magic; | |||
966 | error(consume(Data, Magic)); | |||
967 | W.printHex("Magic", Magic); | |||
968 | if (Magic != COFF::DEBUG_SECTION_MAGIC) | |||
969 | return error(object_error::parse_failed); | |||
970 | ||||
971 | BinaryStreamReader FSReader(Data, support::little); | |||
972 | initializeFileAndStringTables(FSReader); | |||
973 | ||||
974 | // TODO: Convert this over to using ModuleSubstreamVisitor. | |||
975 | while (!Data.empty()) { | |||
976 | // The section consists of a number of subsection in the following format: | |||
977 | // |SubSectionType|SubSectionSize|Contents...| | |||
978 | uint32_t SubType, SubSectionSize; | |||
979 | error(consume(Data, SubType)); | |||
980 | error(consume(Data, SubSectionSize)); | |||
981 | ||||
982 | ListScope S(W, "Subsection"); | |||
983 | W.printEnum("SubSectionType", SubType, makeArrayRef(SubSectionTypes)); | |||
984 | W.printHex("SubSectionSize", SubSectionSize); | |||
985 | ||||
986 | // Get the contents of the subsection. | |||
987 | if (SubSectionSize > Data.size()) | |||
988 | return error(object_error::parse_failed); | |||
989 | StringRef Contents = Data.substr(0, SubSectionSize); | |||
990 | ||||
991 | // Add SubSectionSize to the current offset and align that offset to find | |||
992 | // the next subsection. | |||
993 | size_t SectionOffset = Data.data() - SectionContents.data(); | |||
994 | size_t NextOffset = SectionOffset + SubSectionSize; | |||
995 | NextOffset = alignTo(NextOffset, 4); | |||
996 | if (NextOffset > SectionContents.size()) | |||
997 | return error(object_error::parse_failed); | |||
998 | Data = SectionContents.drop_front(NextOffset); | |||
999 | ||||
1000 | // Optionally print the subsection bytes in case our parsing gets confused | |||
1001 | // later. | |||
1002 | if (opts::CodeViewSubsectionBytes) | |||
1003 | printBinaryBlockWithRelocs("SubSectionContents", Section, SectionContents, | |||
1004 | Contents); | |||
1005 | ||||
1006 | switch (DebugSubsectionKind(SubType)) { | |||
1007 | case DebugSubsectionKind::Symbols: | |||
1008 | printCodeViewSymbolsSubsection(Contents, Section, SectionContents); | |||
1009 | break; | |||
1010 | ||||
1011 | case DebugSubsectionKind::InlineeLines: | |||
1012 | printCodeViewInlineeLines(Contents); | |||
1013 | break; | |||
1014 | ||||
1015 | case DebugSubsectionKind::FileChecksums: | |||
1016 | printCodeViewFileChecksums(Contents); | |||
1017 | break; | |||
1018 | ||||
1019 | case DebugSubsectionKind::Lines: { | |||
1020 | // Holds a PC to file:line table. Some data to parse this subsection is | |||
1021 | // stored in the other subsections, so just check sanity and store the | |||
1022 | // pointers for deferred processing. | |||
1023 | ||||
1024 | if (SubSectionSize < 12) { | |||
1025 | // There should be at least three words to store two function | |||
1026 | // relocations and size of the code. | |||
1027 | error(object_error::parse_failed); | |||
1028 | return; | |||
1029 | } | |||
1030 | ||||
1031 | StringRef LinkageName; | |||
1032 | error(resolveSymbolName(Obj->getCOFFSection(Section), SectionOffset, | |||
1033 | LinkageName)); | |||
1034 | W.printString("LinkageName", LinkageName); | |||
1035 | if (FunctionLineTables.count(LinkageName) != 0) { | |||
1036 | // Saw debug info for this function already? | |||
1037 | error(object_error::parse_failed); | |||
1038 | return; | |||
1039 | } | |||
1040 | ||||
1041 | FunctionLineTables[LinkageName] = Contents; | |||
1042 | FunctionNames.push_back(LinkageName); | |||
1043 | break; | |||
1044 | } | |||
1045 | case DebugSubsectionKind::FrameData: { | |||
1046 | // First four bytes is a relocation against the function. | |||
1047 | BinaryStreamReader SR(Contents, llvm::support::little); | |||
1048 | ||||
1049 | DebugFrameDataSubsectionRef FrameData; | |||
1050 | error(FrameData.initialize(SR)); | |||
1051 | ||||
1052 | StringRef LinkageName; | |||
1053 | error(resolveSymbolName(Obj->getCOFFSection(Section), SectionContents, | |||
1054 | FrameData.getRelocPtr(), LinkageName)); | |||
1055 | W.printString("LinkageName", LinkageName); | |||
1056 | ||||
1057 | // To find the active frame description, search this array for the | |||
1058 | // smallest PC range that includes the current PC. | |||
1059 | for (const auto &FD : FrameData) { | |||
1060 | StringRef FrameFunc = error(CVStringTable.getString(FD.FrameFunc)); | |||
1061 | ||||
1062 | DictScope S(W, "FrameData"); | |||
1063 | W.printHex("RvaStart", FD.RvaStart); | |||
1064 | W.printHex("CodeSize", FD.CodeSize); | |||
1065 | W.printHex("LocalSize", FD.LocalSize); | |||
1066 | W.printHex("ParamsSize", FD.ParamsSize); | |||
1067 | W.printHex("MaxStackSize", FD.MaxStackSize); | |||
1068 | W.printHex("PrologSize", FD.PrologSize); | |||
1069 | W.printHex("SavedRegsSize", FD.SavedRegsSize); | |||
1070 | W.printFlags("Flags", FD.Flags, makeArrayRef(FrameDataFlags)); | |||
1071 | ||||
1072 | // The FrameFunc string is a small RPN program. It can be broken up into | |||
1073 | // statements that end in the '=' operator, which assigns the value on | |||
1074 | // the top of the stack to the previously pushed variable. Variables can | |||
1075 | // be temporary values ($T0) or physical registers ($esp). Print each | |||
1076 | // assignment on its own line to make these programs easier to read. | |||
1077 | { | |||
1078 | ListScope FFS(W, "FrameFunc"); | |||
1079 | while (!FrameFunc.empty()) { | |||
1080 | size_t EqOrEnd = FrameFunc.find('='); | |||
1081 | if (EqOrEnd == StringRef::npos) | |||
1082 | EqOrEnd = FrameFunc.size(); | |||
1083 | else | |||
1084 | ++EqOrEnd; | |||
1085 | StringRef Stmt = FrameFunc.substr(0, EqOrEnd); | |||
1086 | W.printString(Stmt); | |||
1087 | FrameFunc = FrameFunc.drop_front(EqOrEnd).trim(); | |||
1088 | } | |||
1089 | } | |||
1090 | } | |||
1091 | break; | |||
1092 | } | |||
1093 | ||||
1094 | // Do nothing for unrecognized subsections. | |||
1095 | default: | |||
1096 | break; | |||
1097 | } | |||
1098 | W.flush(); | |||
1099 | } | |||
1100 | ||||
1101 | // Dump the line tables now that we've read all the subsections and know all | |||
1102 | // the required information. | |||
1103 | for (unsigned I = 0, E = FunctionNames.size(); I != E; ++I) { | |||
1104 | StringRef Name = FunctionNames[I]; | |||
1105 | ListScope S(W, "FunctionLineTable"); | |||
1106 | W.printString("LinkageName", Name); | |||
1107 | ||||
1108 | BinaryStreamReader Reader(FunctionLineTables[Name], support::little); | |||
1109 | ||||
1110 | DebugLinesSubsectionRef LineInfo; | |||
1111 | error(LineInfo.initialize(Reader)); | |||
1112 | ||||
1113 | W.printHex("Flags", LineInfo.header()->Flags); | |||
1114 | W.printHex("CodeSize", LineInfo.header()->CodeSize); | |||
1115 | for (const auto &Entry : LineInfo) { | |||
1116 | ||||
1117 | ListScope S(W, "FilenameSegment"); | |||
1118 | printFileNameForOffset("Filename", Entry.NameIndex); | |||
1119 | uint32_t ColumnIndex = 0; | |||
1120 | for (const auto &Line : Entry.LineNumbers) { | |||
1121 | if (Line.Offset >= LineInfo.header()->CodeSize) { | |||
1122 | error(object_error::parse_failed); | |||
1123 | return; | |||
1124 | } | |||
1125 | ||||
1126 | std::string PC = formatv("+{0:X}", uint32_t(Line.Offset)); | |||
1127 | ListScope PCScope(W, PC); | |||
1128 | codeview::LineInfo LI(Line.Flags); | |||
1129 | ||||
1130 | if (LI.isAlwaysStepInto()) | |||
1131 | W.printString("StepInto", StringRef("Always")); | |||
1132 | else if (LI.isNeverStepInto()) | |||
1133 | W.printString("StepInto", StringRef("Never")); | |||
1134 | else | |||
1135 | W.printNumber("LineNumberStart", LI.getStartLine()); | |||
1136 | W.printNumber("LineNumberEndDelta", LI.getLineDelta()); | |||
1137 | W.printBoolean("IsStatement", LI.isStatement()); | |||
1138 | if (LineInfo.hasColumnInfo()) { | |||
1139 | W.printNumber("ColStart", Entry.Columns[ColumnIndex].StartColumn); | |||
1140 | W.printNumber("ColEnd", Entry.Columns[ColumnIndex].EndColumn); | |||
1141 | ++ColumnIndex; | |||
1142 | } | |||
1143 | } | |||
1144 | } | |||
1145 | } | |||
1146 | } | |||
1147 | ||||
1148 | void COFFDumper::printCodeViewSymbolsSubsection(StringRef Subsection, | |||
1149 | const SectionRef &Section, | |||
1150 | StringRef SectionContents) { | |||
1151 | ArrayRef<uint8_t> BinaryData(Subsection.bytes_begin(), | |||
1152 | Subsection.bytes_end()); | |||
1153 | auto CODD = llvm::make_unique<COFFObjectDumpDelegate>(*this, Section, Obj, | |||
1154 | SectionContents); | |||
1155 | CVSymbolDumper CVSD(W, Types, CodeViewContainer::ObjectFile, std::move(CODD), | |||
1156 | CompilationCPUType, opts::CodeViewSubsectionBytes); | |||
1157 | CVSymbolArray Symbols; | |||
1158 | BinaryStreamReader Reader(BinaryData, llvm::support::little); | |||
1159 | if (auto EC = Reader.readArray(Symbols, Reader.getLength())) { | |||
1160 | consumeError(std::move(EC)); | |||
1161 | W.flush(); | |||
1162 | error(object_error::parse_failed); | |||
1163 | } | |||
1164 | ||||
1165 | if (auto EC = CVSD.dump(Symbols)) { | |||
1166 | W.flush(); | |||
1167 | error(std::move(EC)); | |||
1168 | } | |||
1169 | CompilationCPUType = CVSD.getCompilationCPUType(); | |||
1170 | W.flush(); | |||
1171 | } | |||
1172 | ||||
1173 | void COFFDumper::printCodeViewFileChecksums(StringRef Subsection) { | |||
1174 | BinaryStreamRef Stream(Subsection, llvm::support::little); | |||
1175 | DebugChecksumsSubsectionRef Checksums; | |||
1176 | error(Checksums.initialize(Stream)); | |||
1177 | ||||
1178 | for (auto &FC : Checksums) { | |||
1179 | DictScope S(W, "FileChecksum"); | |||
1180 | ||||
1181 | StringRef Filename = error(CVStringTable.getString(FC.FileNameOffset)); | |||
1182 | W.printHex("Filename", Filename, FC.FileNameOffset); | |||
1183 | W.printHex("ChecksumSize", FC.Checksum.size()); | |||
1184 | W.printEnum("ChecksumKind", uint8_t(FC.Kind), | |||
1185 | makeArrayRef(FileChecksumKindNames)); | |||
1186 | ||||
1187 | W.printBinary("ChecksumBytes", FC.Checksum); | |||
1188 | } | |||
1189 | } | |||
1190 | ||||
1191 | void COFFDumper::printCodeViewInlineeLines(StringRef Subsection) { | |||
1192 | BinaryStreamReader SR(Subsection, llvm::support::little); | |||
1193 | DebugInlineeLinesSubsectionRef Lines; | |||
1194 | error(Lines.initialize(SR)); | |||
1195 | ||||
1196 | for (auto &Line : Lines) { | |||
1197 | DictScope S(W, "InlineeSourceLine"); | |||
1198 | printTypeIndex("Inlinee", Line.Header->Inlinee); | |||
1199 | printFileNameForOffset("FileID", Line.Header->FileID); | |||
1200 | W.printNumber("SourceLineNum", Line.Header->SourceLineNum); | |||
1201 | ||||
1202 | if (Lines.hasExtraFiles()) { | |||
1203 | W.printNumber("ExtraFileCount", Line.ExtraFiles.size()); | |||
1204 | ListScope ExtraFiles(W, "ExtraFiles"); | |||
1205 | for (const auto &FID : Line.ExtraFiles) { | |||
1206 | printFileNameForOffset("FileID", FID); | |||
1207 | } | |||
1208 | } | |||
1209 | } | |||
1210 | } | |||
1211 | ||||
1212 | StringRef COFFDumper::getFileNameForFileOffset(uint32_t FileOffset) { | |||
1213 | // The file checksum subsection should precede all references to it. | |||
1214 | if (!CVFileChecksumTable.valid() || !CVStringTable.valid()) | |||
1215 | error(object_error::parse_failed); | |||
1216 | ||||
1217 | auto Iter = CVFileChecksumTable.getArray().at(FileOffset); | |||
1218 | ||||
1219 | // Check if the file checksum table offset is valid. | |||
1220 | if (Iter == CVFileChecksumTable.end()) | |||
1221 | error(object_error::parse_failed); | |||
1222 | ||||
1223 | return error(CVStringTable.getString(Iter->FileNameOffset)); | |||
1224 | } | |||
1225 | ||||
1226 | void COFFDumper::printFileNameForOffset(StringRef Label, uint32_t FileOffset) { | |||
1227 | W.printHex(Label, getFileNameForFileOffset(FileOffset), FileOffset); | |||
1228 | } | |||
1229 | ||||
1230 | void COFFDumper::mergeCodeViewTypes(MergingTypeTableBuilder &CVIDs, | |||
1231 | MergingTypeTableBuilder &CVTypes) { | |||
1232 | for (const SectionRef &S : Obj->sections()) { | |||
1233 | StringRef SectionName; | |||
1234 | error(S.getName(SectionName)); | |||
1235 | if (SectionName == ".debug$T") { | |||
1236 | StringRef Data; | |||
1237 | error(S.getContents(Data)); | |||
1238 | uint32_t Magic; | |||
1239 | error(consume(Data, Magic)); | |||
1240 | if (Magic != 4) | |||
1241 | error(object_error::parse_failed); | |||
1242 | ||||
1243 | CVTypeArray Types; | |||
1244 | BinaryStreamReader Reader(Data, llvm::support::little); | |||
1245 | if (auto EC = Reader.readArray(Types, Reader.getLength())) { | |||
1246 | consumeError(std::move(EC)); | |||
1247 | W.flush(); | |||
1248 | error(object_error::parse_failed); | |||
1249 | } | |||
1250 | SmallVector<TypeIndex, 128> SourceToDest; | |||
1251 | if (auto EC = mergeTypeAndIdRecords(CVIDs, CVTypes, SourceToDest, Types)) | |||
1252 | return error(std::move(EC)); | |||
1253 | } | |||
1254 | } | |||
1255 | } | |||
1256 | ||||
1257 | void COFFDumper::printCodeViewTypeSection(StringRef SectionName, | |||
1258 | const SectionRef &Section) { | |||
1259 | ListScope D(W, "CodeViewTypes"); | |||
1260 | W.printNumber("Section", SectionName, Obj->getSectionID(Section)); | |||
1261 | ||||
1262 | StringRef Data; | |||
1263 | error(Section.getContents(Data)); | |||
1264 | if (opts::CodeViewSubsectionBytes) | |||
1265 | W.printBinaryBlock("Data", Data); | |||
1266 | ||||
1267 | uint32_t Magic; | |||
1268 | error(consume(Data, Magic)); | |||
1269 | W.printHex("Magic", Magic); | |||
1270 | if (Magic != COFF::DEBUG_SECTION_MAGIC) | |||
1271 | return error(object_error::parse_failed); | |||
1272 | ||||
1273 | Types.reset(Data, 100); | |||
1274 | ||||
1275 | TypeDumpVisitor TDV(Types, &W, opts::CodeViewSubsectionBytes); | |||
1276 | error(codeview::visitTypeStream(Types, TDV)); | |||
1277 | W.flush(); | |||
1278 | } | |||
1279 | ||||
1280 | void COFFDumper::printSections() { | |||
1281 | ListScope SectionsD(W, "Sections"); | |||
1282 | int SectionNumber = 0; | |||
1283 | for (const SectionRef &Sec : Obj->sections()) { | |||
1284 | ++SectionNumber; | |||
1285 | const coff_section *Section = Obj->getCOFFSection(Sec); | |||
1286 | ||||
1287 | StringRef Name; | |||
1288 | error(Sec.getName(Name)); | |||
1289 | ||||
1290 | DictScope D(W, "Section"); | |||
1291 | W.printNumber("Number", SectionNumber); | |||
1292 | W.printBinary("Name", Name, Section->Name); | |||
1293 | W.printHex ("VirtualSize", Section->VirtualSize); | |||
1294 | W.printHex ("VirtualAddress", Section->VirtualAddress); | |||
1295 | W.printNumber("RawDataSize", Section->SizeOfRawData); | |||
1296 | W.printHex ("PointerToRawData", Section->PointerToRawData); | |||
1297 | W.printHex ("PointerToRelocations", Section->PointerToRelocations); | |||
1298 | W.printHex ("PointerToLineNumbers", Section->PointerToLinenumbers); | |||
1299 | W.printNumber("RelocationCount", Section->NumberOfRelocations); | |||
1300 | W.printNumber("LineNumberCount", Section->NumberOfLinenumbers); | |||
1301 | W.printFlags ("Characteristics", Section->Characteristics, | |||
1302 | makeArrayRef(ImageSectionCharacteristics), | |||
1303 | COFF::SectionCharacteristics(0x00F00000)); | |||
1304 | ||||
1305 | if (opts::SectionRelocations) { | |||
1306 | ListScope D(W, "Relocations"); | |||
1307 | for (const RelocationRef &Reloc : Sec.relocations()) | |||
1308 | printRelocation(Sec, Reloc); | |||
1309 | } | |||
1310 | ||||
1311 | if (opts::SectionSymbols) { | |||
1312 | ListScope D(W, "Symbols"); | |||
1313 | for (const SymbolRef &Symbol : Obj->symbols()) { | |||
1314 | if (!Sec.containsSymbol(Symbol)) | |||
1315 | continue; | |||
1316 | ||||
1317 | printSymbol(Symbol); | |||
1318 | } | |||
1319 | } | |||
1320 | ||||
1321 | if (opts::SectionData && | |||
1322 | !(Section->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)) { | |||
1323 | StringRef Data; | |||
1324 | error(Sec.getContents(Data)); | |||
1325 | ||||
1326 | W.printBinaryBlock("SectionData", Data); | |||
1327 | } | |||
1328 | } | |||
1329 | } | |||
1330 | ||||
1331 | void COFFDumper::printRelocations() { | |||
1332 | ListScope D(W, "Relocations"); | |||
1333 | ||||
1334 | int SectionNumber = 0; | |||
1335 | for (const SectionRef &Section : Obj->sections()) { | |||
1336 | ++SectionNumber; | |||
1337 | StringRef Name; | |||
1338 | error(Section.getName(Name)); | |||
1339 | ||||
1340 | bool PrintedGroup = false; | |||
1341 | for (const RelocationRef &Reloc : Section.relocations()) { | |||
1342 | if (!PrintedGroup) { | |||
1343 | W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n"; | |||
1344 | W.indent(); | |||
1345 | PrintedGroup = true; | |||
1346 | } | |||
1347 | ||||
1348 | printRelocation(Section, Reloc); | |||
1349 | } | |||
1350 | ||||
1351 | if (PrintedGroup) { | |||
1352 | W.unindent(); | |||
1353 | W.startLine() << "}\n"; | |||
1354 | } | |||
1355 | } | |||
1356 | } | |||
1357 | ||||
1358 | void COFFDumper::printRelocation(const SectionRef &Section, | |||
1359 | const RelocationRef &Reloc, uint64_t Bias) { | |||
1360 | uint64_t Offset = Reloc.getOffset() - Bias; | |||
1361 | uint64_t RelocType = Reloc.getType(); | |||
1362 | SmallString<32> RelocName; | |||
1363 | StringRef SymbolName; | |||
1364 | Reloc.getTypeName(RelocName); | |||
1365 | symbol_iterator Symbol = Reloc.getSymbol(); | |||
1366 | if (Symbol != Obj->symbol_end()) { | |||
1367 | Expected<StringRef> SymbolNameOrErr = Symbol->getName(); | |||
1368 | error(errorToErrorCode(SymbolNameOrErr.takeError())); | |||
1369 | SymbolName = *SymbolNameOrErr; | |||
1370 | } | |||
1371 | ||||
1372 | if (opts::ExpandRelocs) { | |||
1373 | DictScope Group(W, "Relocation"); | |||
1374 | W.printHex("Offset", Offset); | |||
1375 | W.printNumber("Type", RelocName, RelocType); | |||
1376 | W.printString("Symbol", SymbolName.empty() ? "-" : SymbolName); | |||
1377 | } else { | |||
1378 | raw_ostream& OS = W.startLine(); | |||
1379 | OS << W.hex(Offset) | |||
1380 | << " " << RelocName | |||
1381 | << " " << (SymbolName.empty() ? "-" : SymbolName) | |||
1382 | << "\n"; | |||
1383 | } | |||
1384 | } | |||
1385 | ||||
1386 | void COFFDumper::printSymbols() { | |||
1387 | ListScope Group(W, "Symbols"); | |||
1388 | ||||
1389 | for (const SymbolRef &Symbol : Obj->symbols()) | |||
1390 | printSymbol(Symbol); | |||
1391 | } | |||
1392 | ||||
1393 | void COFFDumper::printDynamicSymbols() { ListScope Group(W, "DynamicSymbols"); } | |||
1394 | ||||
1395 | static ErrorOr<StringRef> | |||
1396 | getSectionName(const llvm::object::COFFObjectFile *Obj, int32_t SectionNumber, | |||
1397 | const coff_section *Section) { | |||
1398 | if (Section) { | |||
1399 | StringRef SectionName; | |||
1400 | if (std::error_code EC = Obj->getSectionName(Section, SectionName)) | |||
1401 | return EC; | |||
1402 | return SectionName; | |||
1403 | } | |||
1404 | if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG) | |||
1405 | return StringRef("IMAGE_SYM_DEBUG"); | |||
1406 | if (SectionNumber == llvm::COFF::IMAGE_SYM_ABSOLUTE) | |||
1407 | return StringRef("IMAGE_SYM_ABSOLUTE"); | |||
1408 | if (SectionNumber == llvm::COFF::IMAGE_SYM_UNDEFINED) | |||
1409 | return StringRef("IMAGE_SYM_UNDEFINED"); | |||
1410 | return StringRef(""); | |||
1411 | } | |||
1412 | ||||
1413 | void COFFDumper::printSymbol(const SymbolRef &Sym) { | |||
1414 | DictScope D(W, "Symbol"); | |||
1415 | ||||
1416 | COFFSymbolRef Symbol = Obj->getCOFFSymbol(Sym); | |||
1417 | const coff_section *Section; | |||
1418 | if (std::error_code EC = Obj->getSection(Symbol.getSectionNumber(), Section)) { | |||
1419 | W.startLine() << "Invalid section number: " << EC.message() << "\n"; | |||
1420 | W.flush(); | |||
1421 | return; | |||
1422 | } | |||
1423 | ||||
1424 | StringRef SymbolName; | |||
1425 | if (Obj->getSymbolName(Symbol, SymbolName)) | |||
1426 | SymbolName = ""; | |||
1427 | ||||
1428 | StringRef SectionName = ""; | |||
1429 | ErrorOr<StringRef> Res = | |||
1430 | getSectionName(Obj, Symbol.getSectionNumber(), Section); | |||
1431 | if (Res) | |||
1432 | SectionName = *Res; | |||
1433 | ||||
1434 | W.printString("Name", SymbolName); | |||
1435 | W.printNumber("Value", Symbol.getValue()); | |||
1436 | W.printNumber("Section", SectionName, Symbol.getSectionNumber()); | |||
1437 | W.printEnum ("BaseType", Symbol.getBaseType(), makeArrayRef(ImageSymType)); | |||
1438 | W.printEnum ("ComplexType", Symbol.getComplexType(), | |||
1439 | makeArrayRef(ImageSymDType)); | |||
1440 | W.printEnum ("StorageClass", Symbol.getStorageClass(), | |||
1441 | makeArrayRef(ImageSymClass)); | |||
1442 | W.printNumber("AuxSymbolCount", Symbol.getNumberOfAuxSymbols()); | |||
1443 | ||||
1444 | for (uint8_t I = 0; I < Symbol.getNumberOfAuxSymbols(); ++I) { | |||
1445 | if (Symbol.isFunctionDefinition()) { | |||
1446 | const coff_aux_function_definition *Aux; | |||
1447 | error(getSymbolAuxData(Obj, Symbol, I, Aux)); | |||
1448 | ||||
1449 | DictScope AS(W, "AuxFunctionDef"); | |||
1450 | W.printNumber("TagIndex", Aux->TagIndex); | |||
1451 | W.printNumber("TotalSize", Aux->TotalSize); | |||
1452 | W.printHex("PointerToLineNumber", Aux->PointerToLinenumber); | |||
1453 | W.printHex("PointerToNextFunction", Aux->PointerToNextFunction); | |||
1454 | ||||
1455 | } else if (Symbol.isAnyUndefined()) { | |||
1456 | const coff_aux_weak_external *Aux; | |||
1457 | error(getSymbolAuxData(Obj, Symbol, I, Aux)); | |||
1458 | ||||
1459 | Expected<COFFSymbolRef> Linked = Obj->getSymbol(Aux->TagIndex); | |||
1460 | StringRef LinkedName; | |||
1461 | std::error_code EC = errorToErrorCode(Linked.takeError()); | |||
1462 | if (EC || (EC = Obj->getSymbolName(*Linked, LinkedName))) { | |||
1463 | LinkedName = ""; | |||
1464 | error(EC); | |||
1465 | } | |||
1466 | ||||
1467 | DictScope AS(W, "AuxWeakExternal"); | |||
1468 | W.printNumber("Linked", LinkedName, Aux->TagIndex); | |||
1469 | W.printEnum ("Search", Aux->Characteristics, | |||
1470 | makeArrayRef(WeakExternalCharacteristics)); | |||
1471 | ||||
1472 | } else if (Symbol.isFileRecord()) { | |||
1473 | const char *FileName; | |||
1474 | error(getSymbolAuxData(Obj, Symbol, I, FileName)); | |||
1475 | ||||
1476 | DictScope AS(W, "AuxFileRecord"); | |||
1477 | ||||
1478 | StringRef Name(FileName, Symbol.getNumberOfAuxSymbols() * | |||
1479 | Obj->getSymbolTableEntrySize()); | |||
1480 | W.printString("FileName", Name.rtrim(StringRef("\0", 1))); | |||
1481 | break; | |||
1482 | } else if (Symbol.isSectionDefinition()) { | |||
1483 | const coff_aux_section_definition *Aux; | |||
1484 | error(getSymbolAuxData(Obj, Symbol, I, Aux)); | |||
1485 | ||||
1486 | int32_t AuxNumber = Aux->getNumber(Symbol.isBigObj()); | |||
1487 | ||||
1488 | DictScope AS(W, "AuxSectionDef"); | |||
1489 | W.printNumber("Length", Aux->Length); | |||
1490 | W.printNumber("RelocationCount", Aux->NumberOfRelocations); | |||
1491 | W.printNumber("LineNumberCount", Aux->NumberOfLinenumbers); | |||
1492 | W.printHex("Checksum", Aux->CheckSum); | |||
1493 | W.printNumber("Number", AuxNumber); | |||
1494 | W.printEnum("Selection", Aux->Selection, makeArrayRef(ImageCOMDATSelect)); | |||
1495 | ||||
1496 | if (Section && Section->Characteristics & COFF::IMAGE_SCN_LNK_COMDAT | |||
1497 | && Aux->Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) { | |||
1498 | const coff_section *Assoc; | |||
1499 | StringRef AssocName = ""; | |||
1500 | std::error_code EC = Obj->getSection(AuxNumber, Assoc); | |||
1501 | ErrorOr<StringRef> Res = getSectionName(Obj, AuxNumber, Assoc); | |||
1502 | if (Res) | |||
1503 | AssocName = *Res; | |||
1504 | if (!EC) | |||
1505 | EC = Res.getError(); | |||
1506 | if (EC) { | |||
1507 | AssocName = ""; | |||
1508 | error(EC); | |||
1509 | } | |||
1510 | ||||
1511 | W.printNumber("AssocSection", AssocName, AuxNumber); | |||
1512 | } | |||
1513 | } else if (Symbol.isCLRToken()) { | |||
1514 | const coff_aux_clr_token *Aux; | |||
1515 | error(getSymbolAuxData(Obj, Symbol, I, Aux)); | |||
1516 | ||||
1517 | Expected<COFFSymbolRef> ReferredSym = | |||
1518 | Obj->getSymbol(Aux->SymbolTableIndex); | |||
1519 | StringRef ReferredName; | |||
1520 | std::error_code EC = errorToErrorCode(ReferredSym.takeError()); | |||
1521 | if (EC || (EC = Obj->getSymbolName(*ReferredSym, ReferredName))) { | |||
1522 | ReferredName = ""; | |||
1523 | error(EC); | |||
1524 | } | |||
1525 | ||||
1526 | DictScope AS(W, "AuxCLRToken"); | |||
1527 | W.printNumber("AuxType", Aux->AuxType); | |||
1528 | W.printNumber("Reserved", Aux->Reserved); | |||
1529 | W.printNumber("SymbolTableIndex", ReferredName, Aux->SymbolTableIndex); | |||
1530 | ||||
1531 | } else { | |||
1532 | W.startLine() << "<unhandled auxiliary record>\n"; | |||
1533 | } | |||
1534 | } | |||
1535 | } | |||
1536 | ||||
1537 | void COFFDumper::printUnwindInfo() { | |||
1538 | ListScope D(W, "UnwindInformation"); | |||
1539 | switch (Obj->getMachine()) { | |||
1540 | case COFF::IMAGE_FILE_MACHINE_AMD64: { | |||
1541 | Win64EH::Dumper Dumper(W); | |||
1542 | Win64EH::Dumper::SymbolResolver | |||
1543 | Resolver = [](const object::coff_section *Section, uint64_t Offset, | |||
1544 | SymbolRef &Symbol, void *user_data) -> std::error_code { | |||
1545 | COFFDumper *Dumper = reinterpret_cast<COFFDumper *>(user_data); | |||
1546 | return Dumper->resolveSymbol(Section, Offset, Symbol); | |||
1547 | }; | |||
1548 | Win64EH::Dumper::Context Ctx(*Obj, Resolver, this); | |||
1549 | Dumper.printData(Ctx); | |||
1550 | break; | |||
1551 | } | |||
1552 | case COFF::IMAGE_FILE_MACHINE_ARM64: | |||
1553 | case COFF::IMAGE_FILE_MACHINE_ARMNT: { | |||
1554 | ARM::WinEH::Decoder Decoder(W, Obj->getMachine() == | |||
1555 | COFF::IMAGE_FILE_MACHINE_ARM64); | |||
1556 | Decoder.dumpProcedureData(*Obj); | |||
1557 | break; | |||
1558 | } | |||
1559 | default: | |||
1560 | W.printEnum("unsupported Image Machine", Obj->getMachine(), | |||
1561 | makeArrayRef(ImageFileMachineType)); | |||
1562 | break; | |||
1563 | } | |||
1564 | } | |||
1565 | ||||
1566 | void COFFDumper::printNeededLibraries() { | |||
1567 | ListScope D(W, "NeededLibraries"); | |||
1568 | ||||
1569 | using LibsTy = std::vector<StringRef>; | |||
1570 | LibsTy Libs; | |||
1571 | ||||
1572 | for (const ImportDirectoryEntryRef &DirRef : Obj->import_directories()) { | |||
1573 | StringRef Name; | |||
1574 | if (!DirRef.getName(Name)) | |||
1575 | Libs.push_back(Name); | |||
1576 | } | |||
1577 | ||||
1578 | std::stable_sort(Libs.begin(), Libs.end()); | |||
1579 | ||||
1580 | for (const auto &L : Libs) { | |||
1581 | outs() << " " << L << "\n"; | |||
1582 | } | |||
1583 | } | |||
1584 | ||||
1585 | void COFFDumper::printImportedSymbols( | |||
1586 | iterator_range<imported_symbol_iterator> Range) { | |||
1587 | for (const ImportedSymbolRef &I : Range) { | |||
1588 | StringRef Sym; | |||
1589 | error(I.getSymbolName(Sym)); | |||
1590 | uint16_t Ordinal; | |||
1591 | error(I.getOrdinal(Ordinal)); | |||
1592 | W.printNumber("Symbol", Sym, Ordinal); | |||
1593 | } | |||
1594 | } | |||
1595 | ||||
1596 | void COFFDumper::printDelayImportedSymbols( | |||
1597 | const DelayImportDirectoryEntryRef &I, | |||
1598 | iterator_range<imported_symbol_iterator> Range) { | |||
1599 | int Index = 0; | |||
1600 | for (const ImportedSymbolRef &S : Range) { | |||
1601 | DictScope Import(W, "Import"); | |||
1602 | StringRef Sym; | |||
1603 | error(S.getSymbolName(Sym)); | |||
1604 | uint16_t Ordinal; | |||
1605 | error(S.getOrdinal(Ordinal)); | |||
1606 | W.printNumber("Symbol", Sym, Ordinal); | |||
1607 | uint64_t Addr; | |||
1608 | error(I.getImportAddress(Index++, Addr)); | |||
1609 | W.printHex("Address", Addr); | |||
1610 | } | |||
1611 | } | |||
1612 | ||||
1613 | void COFFDumper::printCOFFImports() { | |||
1614 | // Regular imports | |||
1615 | for (const ImportDirectoryEntryRef &I : Obj->import_directories()) { | |||
1616 | DictScope Import(W, "Import"); | |||
1617 | StringRef Name; | |||
1618 | error(I.getName(Name)); | |||
1619 | W.printString("Name", Name); | |||
1620 | uint32_t ILTAddr; | |||
1621 | error(I.getImportLookupTableRVA(ILTAddr)); | |||
1622 | W.printHex("ImportLookupTableRVA", ILTAddr); | |||
1623 | uint32_t IATAddr; | |||
1624 | error(I.getImportAddressTableRVA(IATAddr)); | |||
1625 | W.printHex("ImportAddressTableRVA", IATAddr); | |||
1626 | // The import lookup table can be missing with certain older linkers, so | |||
1627 | // fall back to the import address table in that case. | |||
1628 | if (ILTAddr) | |||
1629 | printImportedSymbols(I.lookup_table_symbols()); | |||
1630 | else | |||
1631 | printImportedSymbols(I.imported_symbols()); | |||
1632 | } | |||
1633 | ||||
1634 | // Delay imports | |||
1635 | for (const DelayImportDirectoryEntryRef &I : Obj->delay_import_directories()) { | |||
1636 | DictScope Import(W, "DelayImport"); | |||
1637 | StringRef Name; | |||
1638 | error(I.getName(Name)); | |||
1639 | W.printString("Name", Name); | |||
1640 | const delay_import_directory_table_entry *Table; | |||
1641 | error(I.getDelayImportTable(Table)); | |||
1642 | W.printHex("Attributes", Table->Attributes); | |||
1643 | W.printHex("ModuleHandle", Table->ModuleHandle); | |||
1644 | W.printHex("ImportAddressTable", Table->DelayImportAddressTable); | |||
1645 | W.printHex("ImportNameTable", Table->DelayImportNameTable); | |||
1646 | W.printHex("BoundDelayImportTable", Table->BoundDelayImportTable); | |||
1647 | W.printHex("UnloadDelayImportTable", Table->UnloadDelayImportTable); | |||
1648 | printDelayImportedSymbols(I, I.imported_symbols()); | |||
1649 | } | |||
1650 | } | |||
1651 | ||||
1652 | void COFFDumper::printCOFFExports() { | |||
1653 | for (const ExportDirectoryEntryRef &E : Obj->export_directories()) { | |||
1654 | DictScope Export(W, "Export"); | |||
1655 | ||||
1656 | StringRef Name; | |||
1657 | uint32_t Ordinal, RVA; | |||
1658 | ||||
1659 | error(E.getSymbolName(Name)); | |||
1660 | error(E.getOrdinal(Ordinal)); | |||
1661 | error(E.getExportRVA(RVA)); | |||
1662 | ||||
1663 | W.printNumber("Ordinal", Ordinal); | |||
1664 | W.printString("Name", Name); | |||
1665 | W.printHex("RVA", RVA); | |||
1666 | } | |||
1667 | } | |||
1668 | ||||
1669 | void COFFDumper::printCOFFDirectives() { | |||
1670 | for (const SectionRef &Section : Obj->sections()) { | |||
1671 | StringRef Contents; | |||
1672 | StringRef Name; | |||
1673 | ||||
1674 | error(Section.getName(Name)); | |||
1675 | if (Name != ".drectve") | |||
1676 | continue; | |||
1677 | ||||
1678 | error(Section.getContents(Contents)); | |||
1679 | ||||
1680 | W.printString("Directive(s)", Contents); | |||
1681 | } | |||
1682 | } | |||
1683 | ||||
1684 | static std::string getBaseRelocTypeName(uint8_t Type) { | |||
1685 | switch (Type) { | |||
1686 | case COFF::IMAGE_REL_BASED_ABSOLUTE: return "ABSOLUTE"; | |||
1687 | case COFF::IMAGE_REL_BASED_HIGH: return "HIGH"; | |||
1688 | case COFF::IMAGE_REL_BASED_LOW: return "LOW"; | |||
1689 | case COFF::IMAGE_REL_BASED_HIGHLOW: return "HIGHLOW"; | |||
1690 | case COFF::IMAGE_REL_BASED_HIGHADJ: return "HIGHADJ"; | |||
1691 | case COFF::IMAGE_REL_BASED_ARM_MOV32T: return "ARM_MOV32(T)"; | |||
1692 | case COFF::IMAGE_REL_BASED_DIR64: return "DIR64"; | |||
1693 | default: return "unknown (" + llvm::utostr(Type) + ")"; | |||
1694 | } | |||
1695 | } | |||
1696 | ||||
1697 | void COFFDumper::printCOFFBaseReloc() { | |||
1698 | ListScope D(W, "BaseReloc"); | |||
1699 | for (const BaseRelocRef &I : Obj->base_relocs()) { | |||
1700 | uint8_t Type; | |||
1701 | uint32_t RVA; | |||
1702 | error(I.getRVA(RVA)); | |||
1703 | error(I.getType(Type)); | |||
1704 | DictScope Import(W, "Entry"); | |||
1705 | W.printString("Type", getBaseRelocTypeName(Type)); | |||
1706 | W.printHex("Address", RVA); | |||
1707 | } | |||
1708 | } | |||
1709 | ||||
1710 | void COFFDumper::printCOFFResources() { | |||
1711 | ListScope ResourcesD(W, "Resources"); | |||
1712 | for (const SectionRef &S : Obj->sections()) { | |||
1713 | StringRef Name; | |||
1714 | error(S.getName(Name)); | |||
1715 | if (!Name.startswith(".rsrc")) | |||
1716 | continue; | |||
1717 | ||||
1718 | StringRef Ref; | |||
1719 | error(S.getContents(Ref)); | |||
1720 | ||||
1721 | if ((Name == ".rsrc") || (Name == ".rsrc$01")) { | |||
1722 | ResourceSectionRef RSF(Ref); | |||
1723 | auto &BaseTable = unwrapOrError(RSF.getBaseTable()); | |||
1724 | W.printNumber("Total Number of Resources", | |||
1725 | countTotalTableEntries(RSF, BaseTable, "Type")); | |||
1726 | W.printHex("Base Table Address", | |||
1727 | Obj->getCOFFSection(S)->PointerToRawData); | |||
1728 | W.startLine() << "\n"; | |||
1729 | printResourceDirectoryTable(RSF, BaseTable, "Type"); | |||
1730 | } | |||
1731 | if (opts::SectionData) | |||
1732 | W.printBinaryBlock(Name.str() + " Data", Ref); | |||
1733 | } | |||
1734 | } | |||
1735 | ||||
1736 | uint32_t | |||
1737 | COFFDumper::countTotalTableEntries(ResourceSectionRef RSF, | |||
1738 | const coff_resource_dir_table &Table, | |||
1739 | StringRef Level) { | |||
1740 | uint32_t TotalEntries = 0; | |||
1741 | for (int i = 0; i < Table.NumberOfNameEntries + Table.NumberOfIDEntries; | |||
1742 | i++) { | |||
1743 | auto Entry = unwrapOrError(getResourceDirectoryTableEntry(Table, i)); | |||
1744 | if (Entry.Offset.isSubDir()) { | |||
1745 | StringRef NextLevel; | |||
1746 | if (Level == "Name") | |||
1747 | NextLevel = "Language"; | |||
1748 | else | |||
1749 | NextLevel = "Name"; | |||
1750 | auto &NextTable = unwrapOrError(RSF.getEntrySubDir(Entry)); | |||
1751 | TotalEntries += countTotalTableEntries(RSF, NextTable, NextLevel); | |||
1752 | } else { | |||
1753 | TotalEntries += 1; | |||
1754 | } | |||
1755 | } | |||
1756 | return TotalEntries; | |||
1757 | } | |||
1758 | ||||
1759 | void COFFDumper::printResourceDirectoryTable( | |||
1760 | ResourceSectionRef RSF, const coff_resource_dir_table &Table, | |||
1761 | StringRef Level) { | |||
1762 | ||||
1763 | W.printNumber("Number of String Entries", Table.NumberOfNameEntries); | |||
1764 | W.printNumber("Number of ID Entries", Table.NumberOfIDEntries); | |||
1765 | ||||
1766 | // Iterate through level in resource directory tree. | |||
1767 | for (int i = 0; i < Table.NumberOfNameEntries + Table.NumberOfIDEntries; | |||
1768 | i++) { | |||
1769 | auto Entry = unwrapOrError(getResourceDirectoryTableEntry(Table, i)); | |||
1770 | StringRef Name; | |||
1771 | SmallString<20> IDStr; | |||
1772 | raw_svector_ostream OS(IDStr); | |||
1773 | if (i < Table.NumberOfNameEntries) { | |||
1774 | ArrayRef<UTF16> RawEntryNameString = unwrapOrError(RSF.getEntryNameString(Entry)); | |||
1775 | std::vector<UTF16> EndianCorrectedNameString; | |||
1776 | if (llvm::sys::IsBigEndianHost) { | |||
1777 | EndianCorrectedNameString.resize(RawEntryNameString.size() + 1); | |||
1778 | std::copy(RawEntryNameString.begin(), RawEntryNameString.end(), | |||
1779 | EndianCorrectedNameString.begin() + 1); | |||
1780 | EndianCorrectedNameString[0] = UNI_UTF16_BYTE_ORDER_MARK_SWAPPED0xFFFE; | |||
1781 | RawEntryNameString = makeArrayRef(EndianCorrectedNameString); | |||
1782 | } | |||
1783 | std::string EntryNameString; | |||
1784 | if (!llvm::convertUTF16ToUTF8String(RawEntryNameString, EntryNameString)) | |||
1785 | error(object_error::parse_failed); | |||
1786 | OS << ": "; | |||
1787 | OS << EntryNameString; | |||
1788 | } else { | |||
1789 | if (Level == "Type") { | |||
1790 | ScopedPrinter Printer(OS); | |||
1791 | Printer.printEnum("", Entry.Identifier.ID, | |||
1792 | makeArrayRef(ResourceTypeNames)); | |||
1793 | IDStr = IDStr.slice(0, IDStr.find_first_of(")", 0) + 1); | |||
1794 | } else { | |||
1795 | OS << ": (ID " << Entry.Identifier.ID << ")"; | |||
1796 | } | |||
1797 | } | |||
1798 | Name = StringRef(IDStr); | |||
1799 | ListScope ResourceType(W, Level.str() + Name.str()); | |||
1800 | if (Entry.Offset.isSubDir()) { | |||
1801 | W.printHex("Table Offset", Entry.Offset.value()); | |||
1802 | StringRef NextLevel; | |||
1803 | if (Level == "Name") | |||
1804 | NextLevel = "Language"; | |||
1805 | else | |||
1806 | NextLevel = "Name"; | |||
1807 | auto &NextTable = unwrapOrError(RSF.getEntrySubDir(Entry)); | |||
1808 | printResourceDirectoryTable(RSF, NextTable, NextLevel); | |||
1809 | } else { | |||
1810 | W.printHex("Entry Offset", Entry.Offset.value()); | |||
1811 | char FormattedTime[20] = {}; | |||
1812 | time_t TDS = time_t(Table.TimeDateStamp); | |||
1813 | strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS)); | |||
1814 | W.printHex("Time/Date Stamp", FormattedTime, Table.TimeDateStamp); | |||
1815 | W.printNumber("Major Version", Table.MajorVersion); | |||
1816 | W.printNumber("Minor Version", Table.MinorVersion); | |||
1817 | W.printNumber("Characteristics", Table.Characteristics); | |||
1818 | } | |||
1819 | } | |||
1820 | } | |||
1821 | ||||
1822 | ErrorOr<const coff_resource_dir_entry &> | |||
1823 | COFFDumper::getResourceDirectoryTableEntry(const coff_resource_dir_table &Table, | |||
1824 | uint32_t Index) { | |||
1825 | if (Index >= (uint32_t)(Table.NumberOfNameEntries + Table.NumberOfIDEntries)) | |||
1826 | return object_error::parse_failed; | |||
1827 | auto TablePtr = reinterpret_cast<const coff_resource_dir_entry *>(&Table + 1); | |||
1828 | return TablePtr[Index]; | |||
1829 | } | |||
1830 | ||||
1831 | void COFFDumper::printStackMap() const { | |||
1832 | object::SectionRef StackMapSection; | |||
1833 | for (auto Sec : Obj->sections()) { | |||
1834 | StringRef Name; | |||
1835 | Sec.getName(Name); | |||
1836 | if (Name == ".llvm_stackmaps") { | |||
1837 | StackMapSection = Sec; | |||
1838 | break; | |||
1839 | } | |||
1840 | } | |||
1841 | ||||
1842 | if (StackMapSection == object::SectionRef()) | |||
1843 | return; | |||
1844 | ||||
1845 | StringRef StackMapContents; | |||
1846 | StackMapSection.getContents(StackMapContents); | |||
1847 | ArrayRef<uint8_t> StackMapContentsArray( | |||
1848 | reinterpret_cast<const uint8_t*>(StackMapContents.data()), | |||
1849 | StackMapContents.size()); | |||
1850 | ||||
1851 | if (Obj->isLittleEndian()) | |||
1852 | prettyPrintStackMap( | |||
1853 | W, StackMapV2Parser<support::little>(StackMapContentsArray)); | |||
1854 | else | |||
1855 | prettyPrintStackMap(W, | |||
1856 | StackMapV2Parser<support::big>(StackMapContentsArray)); | |||
1857 | } | |||
1858 | ||||
1859 | void COFFDumper::printAddrsig() { | |||
1860 | object::SectionRef AddrsigSection; | |||
| ||||
1861 | for (auto Sec : Obj->sections()) { | |||
1862 | StringRef Name; | |||
1863 | Sec.getName(Name); | |||
1864 | if (Name == ".llvm_addrsig") { | |||
1865 | AddrsigSection = Sec; | |||
1866 | break; | |||
1867 | } | |||
1868 | } | |||
1869 | ||||
1870 | if (AddrsigSection == object::SectionRef()) | |||
1871 | return; | |||
1872 | ||||
1873 | StringRef AddrsigContents; | |||
1874 | AddrsigSection.getContents(AddrsigContents); | |||
1875 | ArrayRef<uint8_t> AddrsigContentsArray( | |||
1876 | reinterpret_cast<const uint8_t*>(AddrsigContents.data()), | |||
1877 | AddrsigContents.size()); | |||
1878 | ||||
1879 | ListScope L(W, "Addrsig"); | |||
1880 | auto *Cur = reinterpret_cast<const uint8_t *>(AddrsigContents.begin()); | |||
1881 | auto *End = reinterpret_cast<const uint8_t *>(AddrsigContents.end()); | |||
1882 | while (Cur != End) { | |||
1883 | unsigned Size; | |||
1884 | const char *Err; | |||
1885 | uint64_t SymIndex = decodeULEB128(Cur, &Size, End, &Err); | |||
1886 | if (Err) | |||
1887 | reportError(Err); | |||
1888 | ||||
1889 | Expected<COFFSymbolRef> Sym = Obj->getSymbol(SymIndex); | |||
1890 | StringRef SymName; | |||
1891 | std::error_code EC = errorToErrorCode(Sym.takeError()); | |||
1892 | if (EC || (EC = Obj->getSymbolName(*Sym, SymName))) { | |||
1893 | SymName = ""; | |||
1894 | error(EC); | |||
1895 | } | |||
1896 | ||||
1897 | W.printNumber("Sym", SymName, SymIndex); | |||
1898 | Cur += Size; | |||
1899 | } | |||
1900 | } | |||
1901 | ||||
1902 | void llvm::dumpCodeViewMergedTypes( | |||
1903 | ScopedPrinter &Writer, llvm::codeview::MergingTypeTableBuilder &IDTable, | |||
1904 | llvm::codeview::MergingTypeTableBuilder &CVTypes) { | |||
1905 | // Flatten it first, then run our dumper on it. | |||
1906 | SmallString<0> TypeBuf; | |||
1907 | CVTypes.ForEachRecord([&](TypeIndex TI, const CVType &Record) { | |||
1908 | TypeBuf.append(Record.RecordData.begin(), Record.RecordData.end()); | |||
1909 | }); | |||
1910 | ||||
1911 | TypeTableCollection TpiTypes(CVTypes.records()); | |||
1912 | { | |||
1913 | ListScope S(Writer, "MergedTypeStream"); | |||
1914 | TypeDumpVisitor TDV(TpiTypes, &Writer, opts::CodeViewSubsectionBytes); | |||
1915 | error(codeview::visitTypeStream(TpiTypes, TDV)); | |||
1916 | Writer.flush(); | |||
1917 | } | |||
1918 | ||||
1919 | // Flatten the id stream and print it next. The ID stream refers to names from | |||
1920 | // the type stream. | |||
1921 | TypeTableCollection IpiTypes(IDTable.records()); | |||
1922 | { | |||
1923 | ListScope S(Writer, "MergedIDStream"); | |||
1924 | TypeDumpVisitor TDV(TpiTypes, &Writer, opts::CodeViewSubsectionBytes); | |||
1925 | TDV.setIpiTypes(IpiTypes); | |||
1926 | error(codeview::visitTypeStream(IpiTypes, TDV)); | |||
1927 | Writer.flush(); | |||
1928 | } | |||
1929 | } |
1 | //===- ObjectFile.h - File format independent object file -------*- C++ -*-===// | |||
2 | // | |||
3 | // The LLVM Compiler Infrastructure | |||
4 | // | |||
5 | // This file is distributed under the University of Illinois Open Source | |||
6 | // License. See LICENSE.TXT for details. | |||
7 | // | |||
8 | //===----------------------------------------------------------------------===// | |||
9 | // | |||
10 | // This file declares a file format independent ObjectFile class. | |||
11 | // | |||
12 | //===----------------------------------------------------------------------===// | |||
13 | ||||
14 | #ifndef LLVM_OBJECT_OBJECTFILE_H | |||
15 | #define LLVM_OBJECT_OBJECTFILE_H | |||
16 | ||||
17 | #include "llvm/ADT/StringRef.h" | |||
18 | #include "llvm/ADT/Triple.h" | |||
19 | #include "llvm/ADT/iterator_range.h" | |||
20 | #include "llvm/BinaryFormat/Magic.h" | |||
21 | #include "llvm/MC/SubtargetFeature.h" | |||
22 | #include "llvm/Object/Binary.h" | |||
23 | #include "llvm/Object/Error.h" | |||
24 | #include "llvm/Object/SymbolicFile.h" | |||
25 | #include "llvm/Support/Casting.h" | |||
26 | #include "llvm/Support/Error.h" | |||
27 | #include "llvm/Support/FileSystem.h" | |||
28 | #include "llvm/Support/MemoryBuffer.h" | |||
29 | #include <cassert> | |||
30 | #include <cstdint> | |||
31 | #include <memory> | |||
32 | #include <system_error> | |||
33 | ||||
34 | namespace llvm { | |||
35 | ||||
36 | class ARMAttributeParser; | |||
37 | ||||
38 | namespace object { | |||
39 | ||||
40 | class COFFObjectFile; | |||
41 | class MachOObjectFile; | |||
42 | class ObjectFile; | |||
43 | class SectionRef; | |||
44 | class SymbolRef; | |||
45 | class symbol_iterator; | |||
46 | class WasmObjectFile; | |||
47 | ||||
48 | using section_iterator = content_iterator<SectionRef>; | |||
49 | ||||
50 | /// This is a value type class that represents a single relocation in the list | |||
51 | /// of relocations in the object file. | |||
52 | class RelocationRef { | |||
53 | DataRefImpl RelocationPimpl; | |||
54 | const ObjectFile *OwningObject = nullptr; | |||
55 | ||||
56 | public: | |||
57 | RelocationRef() = default; | |||
58 | RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner); | |||
59 | ||||
60 | bool operator==(const RelocationRef &Other) const; | |||
61 | ||||
62 | void moveNext(); | |||
63 | ||||
64 | uint64_t getOffset() const; | |||
65 | symbol_iterator getSymbol() const; | |||
66 | uint64_t getType() const; | |||
67 | ||||
68 | /// Get a string that represents the type of this relocation. | |||
69 | /// | |||
70 | /// This is for display purposes only. | |||
71 | void getTypeName(SmallVectorImpl<char> &Result) const; | |||
72 | ||||
73 | DataRefImpl getRawDataRefImpl() const; | |||
74 | const ObjectFile *getObject() const; | |||
75 | }; | |||
76 | ||||
77 | using relocation_iterator = content_iterator<RelocationRef>; | |||
78 | ||||
79 | /// This is a value type class that represents a single section in the list of | |||
80 | /// sections in the object file. | |||
81 | class SectionRef { | |||
82 | friend class SymbolRef; | |||
83 | ||||
84 | DataRefImpl SectionPimpl; | |||
85 | const ObjectFile *OwningObject = nullptr; | |||
86 | ||||
87 | public: | |||
88 | SectionRef() = default; | |||
89 | SectionRef(DataRefImpl SectionP, const ObjectFile *Owner); | |||
90 | ||||
91 | bool operator==(const SectionRef &Other) const; | |||
92 | bool operator!=(const SectionRef &Other) const; | |||
93 | bool operator<(const SectionRef &Other) const; | |||
94 | ||||
95 | void moveNext(); | |||
96 | ||||
97 | std::error_code getName(StringRef &Result) const; | |||
98 | uint64_t getAddress() const; | |||
99 | uint64_t getIndex() const; | |||
100 | uint64_t getSize() const; | |||
101 | std::error_code getContents(StringRef &Result) const; | |||
102 | ||||
103 | /// Get the alignment of this section as the actual value (not log 2). | |||
104 | uint64_t getAlignment() const; | |||
105 | ||||
106 | bool isCompressed() const; | |||
107 | bool isText() const; | |||
108 | bool isData() const; | |||
109 | bool isBSS() const; | |||
110 | bool isVirtual() const; | |||
111 | bool isBitcode() const; | |||
112 | bool isStripped() const; | |||
113 | ||||
114 | bool containsSymbol(SymbolRef S) const; | |||
115 | ||||
116 | relocation_iterator relocation_begin() const; | |||
117 | relocation_iterator relocation_end() const; | |||
118 | iterator_range<relocation_iterator> relocations() const { | |||
119 | return make_range(relocation_begin(), relocation_end()); | |||
120 | } | |||
121 | section_iterator getRelocatedSection() const; | |||
122 | ||||
123 | DataRefImpl getRawDataRefImpl() const; | |||
124 | const ObjectFile *getObject() const; | |||
125 | }; | |||
126 | ||||
127 | /// This is a value type class that represents a single symbol in the list of | |||
128 | /// symbols in the object file. | |||
129 | class SymbolRef : public BasicSymbolRef { | |||
130 | friend class SectionRef; | |||
131 | ||||
132 | public: | |||
133 | enum Type { | |||
134 | ST_Unknown, // Type not specified | |||
135 | ST_Data, | |||
136 | ST_Debug, | |||
137 | ST_File, | |||
138 | ST_Function, | |||
139 | ST_Other | |||
140 | }; | |||
141 | ||||
142 | SymbolRef() = default; | |||
143 | SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner); | |||
144 | SymbolRef(const BasicSymbolRef &B) : BasicSymbolRef(B) { | |||
145 | assert(isa<ObjectFile>(BasicSymbolRef::getObject()))((isa<ObjectFile>(BasicSymbolRef::getObject())) ? static_cast <void> (0) : __assert_fail ("isa<ObjectFile>(BasicSymbolRef::getObject())" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Object/ObjectFile.h" , 145, __PRETTY_FUNCTION__)); | |||
146 | } | |||
147 | ||||
148 | Expected<StringRef> getName() const; | |||
149 | /// Returns the symbol virtual address (i.e. address at which it will be | |||
150 | /// mapped). | |||
151 | Expected<uint64_t> getAddress() const; | |||
152 | ||||
153 | /// Return the value of the symbol depending on the object this can be an | |||
154 | /// offset or a virtual address. | |||
155 | uint64_t getValue() const; | |||
156 | ||||
157 | /// Get the alignment of this symbol as the actual value (not log 2). | |||
158 | uint32_t getAlignment() const; | |||
159 | uint64_t getCommonSize() const; | |||
160 | Expected<SymbolRef::Type> getType() const; | |||
161 | ||||
162 | /// Get section this symbol is defined in reference to. Result is | |||
163 | /// end_sections() if it is undefined or is an absolute symbol. | |||
164 | Expected<section_iterator> getSection() const; | |||
165 | ||||
166 | const ObjectFile *getObject() const; | |||
167 | }; | |||
168 | ||||
169 | class symbol_iterator : public basic_symbol_iterator { | |||
170 | public: | |||
171 | symbol_iterator(SymbolRef Sym) : basic_symbol_iterator(Sym) {} | |||
172 | symbol_iterator(const basic_symbol_iterator &B) | |||
173 | : basic_symbol_iterator(SymbolRef(B->getRawDataRefImpl(), | |||
174 | cast<ObjectFile>(B->getObject()))) {} | |||
175 | ||||
176 | const SymbolRef *operator->() const { | |||
177 | const BasicSymbolRef &P = basic_symbol_iterator::operator *(); | |||
178 | return static_cast<const SymbolRef*>(&P); | |||
179 | } | |||
180 | ||||
181 | const SymbolRef &operator*() const { | |||
182 | const BasicSymbolRef &P = basic_symbol_iterator::operator *(); | |||
183 | return static_cast<const SymbolRef&>(P); | |||
184 | } | |||
185 | }; | |||
186 | ||||
187 | /// This class is the base class for all object file types. Concrete instances | |||
188 | /// of this object are created by createObjectFile, which figures out which type | |||
189 | /// to create. | |||
190 | class ObjectFile : public SymbolicFile { | |||
191 | virtual void anchor(); | |||
192 | ||||
193 | protected: | |||
194 | ObjectFile(unsigned int Type, MemoryBufferRef Source); | |||
195 | ||||
196 | const uint8_t *base() const { | |||
197 | return reinterpret_cast<const uint8_t *>(Data.getBufferStart()); | |||
198 | } | |||
199 | ||||
200 | // These functions are for SymbolRef to call internally. The main goal of | |||
201 | // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol | |||
202 | // entry in the memory mapped object file. SymbolPimpl cannot contain any | |||
203 | // virtual functions because then it could not point into the memory mapped | |||
204 | // file. | |||
205 | // | |||
206 | // Implementations assume that the DataRefImpl is valid and has not been | |||
207 | // modified externally. It's UB otherwise. | |||
208 | friend class SymbolRef; | |||
209 | ||||
210 | virtual Expected<StringRef> getSymbolName(DataRefImpl Symb) const = 0; | |||
211 | std::error_code printSymbolName(raw_ostream &OS, | |||
212 | DataRefImpl Symb) const override; | |||
213 | virtual Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const = 0; | |||
214 | virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const = 0; | |||
215 | virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const; | |||
216 | virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0; | |||
217 | virtual Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const = 0; | |||
218 | virtual Expected<section_iterator> | |||
219 | getSymbolSection(DataRefImpl Symb) const = 0; | |||
220 | ||||
221 | // Same as above for SectionRef. | |||
222 | friend class SectionRef; | |||
223 | ||||
224 | virtual void moveSectionNext(DataRefImpl &Sec) const = 0; | |||
225 | virtual std::error_code getSectionName(DataRefImpl Sec, | |||
226 | StringRef &Res) const = 0; | |||
227 | virtual uint64_t getSectionAddress(DataRefImpl Sec) const = 0; | |||
228 | virtual uint64_t getSectionIndex(DataRefImpl Sec) const = 0; | |||
229 | virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0; | |||
230 | virtual std::error_code getSectionContents(DataRefImpl Sec, | |||
231 | StringRef &Res) const = 0; | |||
232 | virtual uint64_t getSectionAlignment(DataRefImpl Sec) const = 0; | |||
233 | virtual bool isSectionCompressed(DataRefImpl Sec) const = 0; | |||
234 | virtual bool isSectionText(DataRefImpl Sec) const = 0; | |||
235 | virtual bool isSectionData(DataRefImpl Sec) const = 0; | |||
236 | virtual bool isSectionBSS(DataRefImpl Sec) const = 0; | |||
237 | // A section is 'virtual' if its contents aren't present in the object image. | |||
238 | virtual bool isSectionVirtual(DataRefImpl Sec) const = 0; | |||
239 | virtual bool isSectionBitcode(DataRefImpl Sec) const; | |||
240 | virtual bool isSectionStripped(DataRefImpl Sec) const; | |||
241 | virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const = 0; | |||
242 | virtual relocation_iterator section_rel_end(DataRefImpl Sec) const = 0; | |||
243 | virtual section_iterator getRelocatedSection(DataRefImpl Sec) const; | |||
244 | ||||
245 | // Same as above for RelocationRef. | |||
246 | friend class RelocationRef; | |||
247 | virtual void moveRelocationNext(DataRefImpl &Rel) const = 0; | |||
248 | virtual uint64_t getRelocationOffset(DataRefImpl Rel) const = 0; | |||
249 | virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const = 0; | |||
250 | virtual uint64_t getRelocationType(DataRefImpl Rel) const = 0; | |||
251 | virtual void getRelocationTypeName(DataRefImpl Rel, | |||
252 | SmallVectorImpl<char> &Result) const = 0; | |||
253 | ||||
254 | uint64_t getSymbolValue(DataRefImpl Symb) const; | |||
255 | ||||
256 | public: | |||
257 | ObjectFile() = delete; | |||
258 | ObjectFile(const ObjectFile &other) = delete; | |||
259 | ||||
260 | uint64_t getCommonSymbolSize(DataRefImpl Symb) const { | |||
261 | assert(getSymbolFlags(Symb) & SymbolRef::SF_Common)((getSymbolFlags(Symb) & SymbolRef::SF_Common) ? static_cast <void> (0) : __assert_fail ("getSymbolFlags(Symb) & SymbolRef::SF_Common" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Object/ObjectFile.h" , 261, __PRETTY_FUNCTION__)); | |||
262 | return getCommonSymbolSizeImpl(Symb); | |||
263 | } | |||
264 | ||||
265 | virtual std::vector<SectionRef> dynamic_relocation_sections() const { | |||
266 | return std::vector<SectionRef>(); | |||
267 | } | |||
268 | ||||
269 | using symbol_iterator_range = iterator_range<symbol_iterator>; | |||
270 | symbol_iterator_range symbols() const { | |||
271 | return symbol_iterator_range(symbol_begin(), symbol_end()); | |||
272 | } | |||
273 | ||||
274 | virtual section_iterator section_begin() const = 0; | |||
275 | virtual section_iterator section_end() const = 0; | |||
276 | ||||
277 | using section_iterator_range = iterator_range<section_iterator>; | |||
278 | section_iterator_range sections() const { | |||
279 | return section_iterator_range(section_begin(), section_end()); | |||
280 | } | |||
281 | ||||
282 | /// The number of bytes used to represent an address in this object | |||
283 | /// file format. | |||
284 | virtual uint8_t getBytesInAddress() const = 0; | |||
285 | ||||
286 | virtual StringRef getFileFormatName() const = 0; | |||
287 | virtual Triple::ArchType getArch() const = 0; | |||
288 | virtual SubtargetFeatures getFeatures() const = 0; | |||
289 | virtual void setARMSubArch(Triple &TheTriple) const { } | |||
290 | virtual Expected<uint64_t> getStartAddress() const { | |||
291 | return errorCodeToError(object_error::parse_failed); | |||
292 | }; | |||
293 | ||||
294 | /// Create a triple from the data in this object file. | |||
295 | Triple makeTriple() const; | |||
296 | ||||
297 | virtual std::error_code | |||
298 | getBuildAttributes(ARMAttributeParser &Attributes) const { | |||
299 | return std::error_code(); | |||
300 | } | |||
301 | ||||
302 | /// Maps a debug section name to a standard DWARF section name. | |||
303 | virtual StringRef mapDebugSectionName(StringRef Name) const { return Name; } | |||
304 | ||||
305 | /// True if this is a relocatable object (.o/.obj). | |||
306 | virtual bool isRelocatableObject() const = 0; | |||
307 | ||||
308 | /// @returns Pointer to ObjectFile subclass to handle this type of object. | |||
309 | /// @param ObjectPath The path to the object file. ObjectPath.isObject must | |||
310 | /// return true. | |||
311 | /// Create ObjectFile from path. | |||
312 | static Expected<OwningBinary<ObjectFile>> | |||
313 | createObjectFile(StringRef ObjectPath); | |||
314 | ||||
315 | static Expected<std::unique_ptr<ObjectFile>> | |||
316 | createObjectFile(MemoryBufferRef Object, llvm::file_magic Type); | |||
317 | static Expected<std::unique_ptr<ObjectFile>> | |||
318 | createObjectFile(MemoryBufferRef Object) { | |||
319 | return createObjectFile(Object, llvm::file_magic::unknown); | |||
320 | } | |||
321 | ||||
322 | static bool classof(const Binary *v) { | |||
323 | return v->isObject(); | |||
324 | } | |||
325 | ||||
326 | static Expected<std::unique_ptr<COFFObjectFile>> | |||
327 | createCOFFObjectFile(MemoryBufferRef Object); | |||
328 | ||||
329 | static Expected<std::unique_ptr<ObjectFile>> | |||
330 | createELFObjectFile(MemoryBufferRef Object); | |||
331 | ||||
332 | static Expected<std::unique_ptr<MachOObjectFile>> | |||
333 | createMachOObjectFile(MemoryBufferRef Object, | |||
334 | uint32_t UniversalCputype = 0, | |||
335 | uint32_t UniversalIndex = 0); | |||
336 | ||||
337 | static Expected<std::unique_ptr<WasmObjectFile>> | |||
338 | createWasmObjectFile(MemoryBufferRef Object); | |||
339 | }; | |||
340 | ||||
341 | // Inline function definitions. | |||
342 | inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner) | |||
343 | : BasicSymbolRef(SymbolP, Owner) {} | |||
344 | ||||
345 | inline Expected<StringRef> SymbolRef::getName() const { | |||
346 | return getObject()->getSymbolName(getRawDataRefImpl()); | |||
347 | } | |||
348 | ||||
349 | inline Expected<uint64_t> SymbolRef::getAddress() const { | |||
350 | return getObject()->getSymbolAddress(getRawDataRefImpl()); | |||
351 | } | |||
352 | ||||
353 | inline uint64_t SymbolRef::getValue() const { | |||
354 | return getObject()->getSymbolValue(getRawDataRefImpl()); | |||
355 | } | |||
356 | ||||
357 | inline uint32_t SymbolRef::getAlignment() const { | |||
358 | return getObject()->getSymbolAlignment(getRawDataRefImpl()); | |||
359 | } | |||
360 | ||||
361 | inline uint64_t SymbolRef::getCommonSize() const { | |||
362 | return getObject()->getCommonSymbolSize(getRawDataRefImpl()); | |||
363 | } | |||
364 | ||||
365 | inline Expected<section_iterator> SymbolRef::getSection() const { | |||
366 | return getObject()->getSymbolSection(getRawDataRefImpl()); | |||
367 | } | |||
368 | ||||
369 | inline Expected<SymbolRef::Type> SymbolRef::getType() const { | |||
370 | return getObject()->getSymbolType(getRawDataRefImpl()); | |||
371 | } | |||
372 | ||||
373 | inline const ObjectFile *SymbolRef::getObject() const { | |||
374 | const SymbolicFile *O = BasicSymbolRef::getObject(); | |||
375 | return cast<ObjectFile>(O); | |||
376 | } | |||
377 | ||||
378 | /// SectionRef | |||
379 | inline SectionRef::SectionRef(DataRefImpl SectionP, | |||
380 | const ObjectFile *Owner) | |||
381 | : SectionPimpl(SectionP) | |||
382 | , OwningObject(Owner) {} | |||
383 | ||||
384 | inline bool SectionRef::operator==(const SectionRef &Other) const { | |||
385 | return SectionPimpl == Other.SectionPimpl; | |||
386 | } | |||
387 | ||||
388 | inline bool SectionRef::operator!=(const SectionRef &Other) const { | |||
389 | return SectionPimpl != Other.SectionPimpl; | |||
390 | } | |||
391 | ||||
392 | inline bool SectionRef::operator<(const SectionRef &Other) const { | |||
393 | return SectionPimpl < Other.SectionPimpl; | |||
394 | } | |||
395 | ||||
396 | inline void SectionRef::moveNext() { | |||
397 | return OwningObject->moveSectionNext(SectionPimpl); | |||
398 | } | |||
399 | ||||
400 | inline std::error_code SectionRef::getName(StringRef &Result) const { | |||
401 | return OwningObject->getSectionName(SectionPimpl, Result); | |||
402 | } | |||
403 | ||||
404 | inline uint64_t SectionRef::getAddress() const { | |||
405 | return OwningObject->getSectionAddress(SectionPimpl); | |||
406 | } | |||
407 | ||||
408 | inline uint64_t SectionRef::getIndex() const { | |||
409 | return OwningObject->getSectionIndex(SectionPimpl); | |||
410 | } | |||
411 | ||||
412 | inline uint64_t SectionRef::getSize() const { | |||
413 | return OwningObject->getSectionSize(SectionPimpl); | |||
414 | } | |||
415 | ||||
416 | inline std::error_code SectionRef::getContents(StringRef &Result) const { | |||
417 | return OwningObject->getSectionContents(SectionPimpl, Result); | |||
| ||||
418 | } | |||
419 | ||||
420 | inline uint64_t SectionRef::getAlignment() const { | |||
421 | return OwningObject->getSectionAlignment(SectionPimpl); | |||
422 | } | |||
423 | ||||
424 | inline bool SectionRef::isCompressed() const { | |||
425 | return OwningObject->isSectionCompressed(SectionPimpl); | |||
426 | } | |||
427 | ||||
428 | inline bool SectionRef::isText() const { | |||
429 | return OwningObject->isSectionText(SectionPimpl); | |||
430 | } | |||
431 | ||||
432 | inline bool SectionRef::isData() const { | |||
433 | return OwningObject->isSectionData(SectionPimpl); | |||
434 | } | |||
435 | ||||
436 | inline bool SectionRef::isBSS() const { | |||
437 | return OwningObject->isSectionBSS(SectionPimpl); | |||
438 | } | |||
439 | ||||
440 | inline bool SectionRef::isVirtual() const { | |||
441 | return OwningObject->isSectionVirtual(SectionPimpl); | |||
442 | } | |||
443 | ||||
444 | inline bool SectionRef::isBitcode() const { | |||
445 | return OwningObject->isSectionBitcode(SectionPimpl); | |||
446 | } | |||
447 | ||||
448 | inline bool SectionRef::isStripped() const { | |||
449 | return OwningObject->isSectionStripped(SectionPimpl); | |||
450 | } | |||
451 | ||||
452 | inline relocation_iterator SectionRef::relocation_begin() const { | |||
453 | return OwningObject->section_rel_begin(SectionPimpl); | |||
454 | } | |||
455 | ||||
456 | inline relocation_iterator SectionRef::relocation_end() const { | |||
457 | return OwningObject->section_rel_end(SectionPimpl); | |||
458 | } | |||
459 | ||||
460 | inline section_iterator SectionRef::getRelocatedSection() const { | |||
461 | return OwningObject->getRelocatedSection(SectionPimpl); | |||
462 | } | |||
463 | ||||
464 | inline DataRefImpl SectionRef::getRawDataRefImpl() const { | |||
465 | return SectionPimpl; | |||
466 | } | |||
467 | ||||
468 | inline const ObjectFile *SectionRef::getObject() const { | |||
469 | return OwningObject; | |||
470 | } | |||
471 | ||||
472 | /// RelocationRef | |||
473 | inline RelocationRef::RelocationRef(DataRefImpl RelocationP, | |||
474 | const ObjectFile *Owner) | |||
475 | : RelocationPimpl(RelocationP) | |||
476 | , OwningObject(Owner) {} | |||
477 | ||||
478 | inline bool RelocationRef::operator==(const RelocationRef &Other) const { | |||
479 | return RelocationPimpl == Other.RelocationPimpl; | |||
480 | } | |||
481 | ||||
482 | inline void RelocationRef::moveNext() { | |||
483 | return OwningObject->moveRelocationNext(RelocationPimpl); | |||
484 | } | |||
485 | ||||
486 | inline uint64_t RelocationRef::getOffset() const { | |||
487 | return OwningObject->getRelocationOffset(RelocationPimpl); | |||
488 | } | |||
489 | ||||
490 | inline symbol_iterator RelocationRef::getSymbol() const { | |||
491 | return OwningObject->getRelocationSymbol(RelocationPimpl); | |||
492 | } | |||
493 | ||||
494 | inline uint64_t RelocationRef::getType() const { | |||
495 | return OwningObject->getRelocationType(RelocationPimpl); | |||
496 | } | |||
497 | ||||
498 | inline void RelocationRef::getTypeName(SmallVectorImpl<char> &Result) const { | |||
499 | return OwningObject->getRelocationTypeName(RelocationPimpl, Result); | |||
500 | } | |||
501 | ||||
502 | inline DataRefImpl RelocationRef::getRawDataRefImpl() const { | |||
503 | return RelocationPimpl; | |||
504 | } | |||
505 | ||||
506 | inline const ObjectFile *RelocationRef::getObject() const { | |||
507 | return OwningObject; | |||
508 | } | |||
509 | ||||
510 | } // end namespace object | |||
511 | ||||
512 | } // end namespace llvm | |||
513 | ||||
514 | #endif // LLVM_OBJECT_OBJECTFILE_H |