1 //===-- COFFDumper.cpp - COFF-specific dumper -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file implements the COFF-specific dumper for llvm-readobj.
13 //===----------------------------------------------------------------------===//
15 #include "llvm-readobj.h"
16 #include "ARMWinEHPrinter.h"
18 #include "ObjDumper.h"
19 #include "StackMapPrinter.h"
20 #include "StreamWriter.h"
21 #include "Win64EHDumper.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/Object/COFF.h"
26 #include "llvm/Object/ObjectFile.h"
27 #include "llvm/Support/COFF.h"
28 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/Compiler.h"
30 #include "llvm/Support/DataExtractor.h"
31 #include "llvm/Support/Format.h"
32 #include "llvm/Support/SourceMgr.h"
33 #include "llvm/Support/Win64EH.h"
34 #include "llvm/Support/raw_ostream.h"
37 #include <system_error>
41 using namespace llvm::object;
42 using namespace llvm::Win64EH;
46 class COFFDumper : public ObjDumper {
48 COFFDumper(const llvm::object::COFFObjectFile *Obj, StreamWriter& Writer)
53 void printFileHeaders() override;
54 void printSections() override;
55 void printRelocations() override;
56 void printSymbols() override;
57 void printDynamicSymbols() override;
58 void printUnwindInfo() override;
59 void printCOFFImports() override;
60 void printCOFFExports() override;
61 void printCOFFDirectives() override;
62 void printCOFFBaseReloc() override;
63 void printStackMap() const override;
65 void printSymbol(const SymbolRef &Sym);
66 void printRelocation(const SectionRef &Section, const RelocationRef &Reloc);
67 void printDataDirectory(uint32_t Index, const std::string &FieldName);
69 void printDOSHeader(const dos_header *DH);
70 template <class PEHeader> void printPEHeader(const PEHeader *Hdr);
71 void printBaseOfDataField(const pe32_header *Hdr);
72 void printBaseOfDataField(const pe32plus_header *Hdr);
74 void printCodeViewDebugInfo(const SectionRef &Section);
76 void printCodeViewSymbolsSubsection(StringRef Subsection,
77 const SectionRef &Section,
80 void cacheRelocations();
82 std::error_code resolveSymbol(const coff_section *Section, uint64_t Offset,
84 std::error_code resolveSymbolName(const coff_section *Section,
85 uint64_t Offset, StringRef &Name);
86 void printImportedSymbols(iterator_range<imported_symbol_iterator> Range);
87 void printDelayImportedSymbols(
88 const DelayImportDirectoryEntryRef &I,
89 iterator_range<imported_symbol_iterator> Range);
91 typedef DenseMap<const coff_section*, std::vector<RelocationRef> > RelocMapTy;
93 const llvm::object::COFFObjectFile *Obj;
94 bool RelocCached = false;
96 StringRef CVFileIndexToStringOffsetTable;
97 StringRef CVStringTable;
105 std::error_code createCOFFDumper(const object::ObjectFile *Obj,
106 StreamWriter &Writer,
107 std::unique_ptr<ObjDumper> &Result) {
108 const COFFObjectFile *COFFObj = dyn_cast<COFFObjectFile>(Obj);
110 return readobj_error::unsupported_obj_file_format;
112 Result.reset(new COFFDumper(COFFObj, Writer));
113 return readobj_error::success;
118 // Given a a section and an offset into this section the function returns the
119 // symbol used for the relocation at the offset.
120 std::error_code COFFDumper::resolveSymbol(const coff_section *Section,
121 uint64_t Offset, SymbolRef &Sym) {
123 const auto &Relocations = RelocMap[Section];
124 for (const auto &Relocation : Relocations) {
125 uint64_t RelocationOffset = Relocation.getOffset();
127 if (RelocationOffset == Offset) {
128 Sym = *Relocation.getSymbol();
129 return readobj_error::success;
132 return readobj_error::unknown_symbol;
135 // Given a section and an offset into this section the function returns the name
136 // of the symbol used for the relocation at the offset.
137 std::error_code COFFDumper::resolveSymbolName(const coff_section *Section,
141 if (std::error_code EC = resolveSymbol(Section, Offset, Symbol))
143 ErrorOr<StringRef> NameOrErr = Symbol.getName();
144 if (std::error_code EC = NameOrErr.getError())
147 return std::error_code();
150 static const EnumEntry<COFF::MachineTypes> ImageFileMachineType[] = {
151 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_UNKNOWN ),
152 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AM33 ),
153 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AMD64 ),
154 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARM ),
155 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARMNT ),
156 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_EBC ),
157 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_I386 ),
158 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_IA64 ),
159 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_M32R ),
160 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPS16 ),
161 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU ),
162 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU16),
163 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPC ),
164 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPCFP),
165 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_R4000 ),
166 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3 ),
167 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3DSP ),
168 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH4 ),
169 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH5 ),
170 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_THUMB ),
171 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_WCEMIPSV2)
174 static const EnumEntry<COFF::Characteristics> ImageFileCharacteristics[] = {
175 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_RELOCS_STRIPPED ),
176 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_EXECUTABLE_IMAGE ),
177 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LINE_NUMS_STRIPPED ),
178 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LOCAL_SYMS_STRIPPED ),
179 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_AGGRESSIVE_WS_TRIM ),
180 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LARGE_ADDRESS_AWARE ),
181 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_LO ),
182 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_32BIT_MACHINE ),
183 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DEBUG_STRIPPED ),
184 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP),
185 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_NET_RUN_FROM_SWAP ),
186 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_SYSTEM ),
187 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DLL ),
188 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_UP_SYSTEM_ONLY ),
189 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_HI )
192 static const EnumEntry<COFF::WindowsSubsystem> PEWindowsSubsystem[] = {
193 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_UNKNOWN ),
194 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_NATIVE ),
195 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_GUI ),
196 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CUI ),
197 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_POSIX_CUI ),
198 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI ),
199 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_APPLICATION ),
200 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER),
201 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER ),
202 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_ROM ),
203 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_XBOX ),
206 static const EnumEntry<COFF::DLLCharacteristics> PEDLLCharacteristics[] = {
207 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA ),
208 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE ),
209 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY ),
210 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NX_COMPAT ),
211 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION ),
212 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_SEH ),
213 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_BIND ),
214 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_APPCONTAINER ),
215 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER ),
216 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_GUARD_CF ),
217 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE),
220 static const EnumEntry<COFF::SectionCharacteristics>
221 ImageSectionCharacteristics[] = {
222 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_TYPE_NO_PAD ),
223 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_CODE ),
224 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_INITIALIZED_DATA ),
225 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_UNINITIALIZED_DATA),
226 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_OTHER ),
227 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_INFO ),
228 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_REMOVE ),
229 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_COMDAT ),
230 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_GPREL ),
231 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PURGEABLE ),
232 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_16BIT ),
233 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_LOCKED ),
234 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PRELOAD ),
235 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1BYTES ),
236 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2BYTES ),
237 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4BYTES ),
238 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8BYTES ),
239 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_16BYTES ),
240 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_32BYTES ),
241 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_64BYTES ),
242 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_128BYTES ),
243 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_256BYTES ),
244 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_512BYTES ),
245 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1024BYTES ),
246 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2048BYTES ),
247 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4096BYTES ),
248 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8192BYTES ),
249 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_NRELOC_OVFL ),
250 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_DISCARDABLE ),
251 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_CACHED ),
252 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_PAGED ),
253 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_SHARED ),
254 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_EXECUTE ),
255 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_READ ),
256 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_WRITE )
259 static const EnumEntry<COFF::SymbolBaseType> ImageSymType[] = {
260 { "Null" , COFF::IMAGE_SYM_TYPE_NULL },
261 { "Void" , COFF::IMAGE_SYM_TYPE_VOID },
262 { "Char" , COFF::IMAGE_SYM_TYPE_CHAR },
263 { "Short" , COFF::IMAGE_SYM_TYPE_SHORT },
264 { "Int" , COFF::IMAGE_SYM_TYPE_INT },
265 { "Long" , COFF::IMAGE_SYM_TYPE_LONG },
266 { "Float" , COFF::IMAGE_SYM_TYPE_FLOAT },
267 { "Double", COFF::IMAGE_SYM_TYPE_DOUBLE },
268 { "Struct", COFF::IMAGE_SYM_TYPE_STRUCT },
269 { "Union" , COFF::IMAGE_SYM_TYPE_UNION },
270 { "Enum" , COFF::IMAGE_SYM_TYPE_ENUM },
271 { "MOE" , COFF::IMAGE_SYM_TYPE_MOE },
272 { "Byte" , COFF::IMAGE_SYM_TYPE_BYTE },
273 { "Word" , COFF::IMAGE_SYM_TYPE_WORD },
274 { "UInt" , COFF::IMAGE_SYM_TYPE_UINT },
275 { "DWord" , COFF::IMAGE_SYM_TYPE_DWORD }
278 static const EnumEntry<COFF::SymbolComplexType> ImageSymDType[] = {
279 { "Null" , COFF::IMAGE_SYM_DTYPE_NULL },
280 { "Pointer" , COFF::IMAGE_SYM_DTYPE_POINTER },
281 { "Function", COFF::IMAGE_SYM_DTYPE_FUNCTION },
282 { "Array" , COFF::IMAGE_SYM_DTYPE_ARRAY }
285 static const EnumEntry<COFF::SymbolStorageClass> ImageSymClass[] = {
286 { "EndOfFunction" , COFF::IMAGE_SYM_CLASS_END_OF_FUNCTION },
287 { "Null" , COFF::IMAGE_SYM_CLASS_NULL },
288 { "Automatic" , COFF::IMAGE_SYM_CLASS_AUTOMATIC },
289 { "External" , COFF::IMAGE_SYM_CLASS_EXTERNAL },
290 { "Static" , COFF::IMAGE_SYM_CLASS_STATIC },
291 { "Register" , COFF::IMAGE_SYM_CLASS_REGISTER },
292 { "ExternalDef" , COFF::IMAGE_SYM_CLASS_EXTERNAL_DEF },
293 { "Label" , COFF::IMAGE_SYM_CLASS_LABEL },
294 { "UndefinedLabel" , COFF::IMAGE_SYM_CLASS_UNDEFINED_LABEL },
295 { "MemberOfStruct" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_STRUCT },
296 { "Argument" , COFF::IMAGE_SYM_CLASS_ARGUMENT },
297 { "StructTag" , COFF::IMAGE_SYM_CLASS_STRUCT_TAG },
298 { "MemberOfUnion" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_UNION },
299 { "UnionTag" , COFF::IMAGE_SYM_CLASS_UNION_TAG },
300 { "TypeDefinition" , COFF::IMAGE_SYM_CLASS_TYPE_DEFINITION },
301 { "UndefinedStatic", COFF::IMAGE_SYM_CLASS_UNDEFINED_STATIC },
302 { "EnumTag" , COFF::IMAGE_SYM_CLASS_ENUM_TAG },
303 { "MemberOfEnum" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_ENUM },
304 { "RegisterParam" , COFF::IMAGE_SYM_CLASS_REGISTER_PARAM },
305 { "BitField" , COFF::IMAGE_SYM_CLASS_BIT_FIELD },
306 { "Block" , COFF::IMAGE_SYM_CLASS_BLOCK },
307 { "Function" , COFF::IMAGE_SYM_CLASS_FUNCTION },
308 { "EndOfStruct" , COFF::IMAGE_SYM_CLASS_END_OF_STRUCT },
309 { "File" , COFF::IMAGE_SYM_CLASS_FILE },
310 { "Section" , COFF::IMAGE_SYM_CLASS_SECTION },
311 { "WeakExternal" , COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL },
312 { "CLRToken" , COFF::IMAGE_SYM_CLASS_CLR_TOKEN }
315 static const EnumEntry<COFF::COMDATType> ImageCOMDATSelect[] = {
316 { "NoDuplicates", COFF::IMAGE_COMDAT_SELECT_NODUPLICATES },
317 { "Any" , COFF::IMAGE_COMDAT_SELECT_ANY },
318 { "SameSize" , COFF::IMAGE_COMDAT_SELECT_SAME_SIZE },
319 { "ExactMatch" , COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH },
320 { "Associative" , COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE },
321 { "Largest" , COFF::IMAGE_COMDAT_SELECT_LARGEST },
322 { "Newest" , COFF::IMAGE_COMDAT_SELECT_NEWEST }
325 static const EnumEntry<COFF::WeakExternalCharacteristics>
326 WeakExternalCharacteristics[] = {
327 { "NoLibrary", COFF::IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY },
328 { "Library" , COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY },
329 { "Alias" , COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS }
332 template <typename T>
333 static std::error_code getSymbolAuxData(const COFFObjectFile *Obj,
334 COFFSymbolRef Symbol,
335 uint8_t AuxSymbolIdx, const T *&Aux) {
336 ArrayRef<uint8_t> AuxData = Obj->getSymbolAuxData(Symbol);
337 AuxData = AuxData.slice(AuxSymbolIdx * Obj->getSymbolTableEntrySize());
338 Aux = reinterpret_cast<const T*>(AuxData.data());
339 return readobj_error::success;
342 void COFFDumper::cacheRelocations() {
347 for (const SectionRef &S : Obj->sections()) {
348 const coff_section *Section = Obj->getCOFFSection(S);
350 for (const RelocationRef &Reloc : S.relocations())
351 RelocMap[Section].push_back(Reloc);
353 // Sort relocations by address.
354 std::sort(RelocMap[Section].begin(), RelocMap[Section].end(),
359 void COFFDumper::printDataDirectory(uint32_t Index, const std::string &FieldName) {
360 const data_directory *Data;
361 if (Obj->getDataDirectory(Index, Data))
363 W.printHex(FieldName + "RVA", Data->RelativeVirtualAddress);
364 W.printHex(FieldName + "Size", Data->Size);
367 void COFFDumper::printFileHeaders() {
368 time_t TDS = Obj->getTimeDateStamp();
369 char FormattedTime[20] = { };
370 strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS));
373 DictScope D(W, "ImageFileHeader");
374 W.printEnum ("Machine", Obj->getMachine(),
375 makeArrayRef(ImageFileMachineType));
376 W.printNumber("SectionCount", Obj->getNumberOfSections());
377 W.printHex ("TimeDateStamp", FormattedTime, Obj->getTimeDateStamp());
378 W.printHex ("PointerToSymbolTable", Obj->getPointerToSymbolTable());
379 W.printNumber("SymbolCount", Obj->getNumberOfSymbols());
380 W.printNumber("OptionalHeaderSize", Obj->getSizeOfOptionalHeader());
381 W.printFlags ("Characteristics", Obj->getCharacteristics(),
382 makeArrayRef(ImageFileCharacteristics));
385 // Print PE header. This header does not exist if this is an object file and
386 // not an executable.
387 const pe32_header *PEHeader = nullptr;
388 if (error(Obj->getPE32Header(PEHeader)))
391 printPEHeader<pe32_header>(PEHeader);
393 const pe32plus_header *PEPlusHeader = nullptr;
394 if (error(Obj->getPE32PlusHeader(PEPlusHeader)))
397 printPEHeader<pe32plus_header>(PEPlusHeader);
399 if (const dos_header *DH = Obj->getDOSHeader())
403 void COFFDumper::printDOSHeader(const dos_header *DH) {
404 DictScope D(W, "DOSHeader");
405 W.printString("Magic", StringRef(DH->Magic, sizeof(DH->Magic)));
406 W.printNumber("UsedBytesInTheLastPage", DH->UsedBytesInTheLastPage);
407 W.printNumber("FileSizeInPages", DH->FileSizeInPages);
408 W.printNumber("NumberOfRelocationItems", DH->NumberOfRelocationItems);
409 W.printNumber("HeaderSizeInParagraphs", DH->HeaderSizeInParagraphs);
410 W.printNumber("MinimumExtraParagraphs", DH->MinimumExtraParagraphs);
411 W.printNumber("MaximumExtraParagraphs", DH->MaximumExtraParagraphs);
412 W.printNumber("InitialRelativeSS", DH->InitialRelativeSS);
413 W.printNumber("InitialSP", DH->InitialSP);
414 W.printNumber("Checksum", DH->Checksum);
415 W.printNumber("InitialIP", DH->InitialIP);
416 W.printNumber("InitialRelativeCS", DH->InitialRelativeCS);
417 W.printNumber("AddressOfRelocationTable", DH->AddressOfRelocationTable);
418 W.printNumber("OverlayNumber", DH->OverlayNumber);
419 W.printNumber("OEMid", DH->OEMid);
420 W.printNumber("OEMinfo", DH->OEMinfo);
421 W.printNumber("AddressOfNewExeHeader", DH->AddressOfNewExeHeader);
424 template <class PEHeader>
425 void COFFDumper::printPEHeader(const PEHeader *Hdr) {
426 DictScope D(W, "ImageOptionalHeader");
427 W.printNumber("MajorLinkerVersion", Hdr->MajorLinkerVersion);
428 W.printNumber("MinorLinkerVersion", Hdr->MinorLinkerVersion);
429 W.printNumber("SizeOfCode", Hdr->SizeOfCode);
430 W.printNumber("SizeOfInitializedData", Hdr->SizeOfInitializedData);
431 W.printNumber("SizeOfUninitializedData", Hdr->SizeOfUninitializedData);
432 W.printHex ("AddressOfEntryPoint", Hdr->AddressOfEntryPoint);
433 W.printHex ("BaseOfCode", Hdr->BaseOfCode);
434 printBaseOfDataField(Hdr);
435 W.printHex ("ImageBase", Hdr->ImageBase);
436 W.printNumber("SectionAlignment", Hdr->SectionAlignment);
437 W.printNumber("FileAlignment", Hdr->FileAlignment);
438 W.printNumber("MajorOperatingSystemVersion",
439 Hdr->MajorOperatingSystemVersion);
440 W.printNumber("MinorOperatingSystemVersion",
441 Hdr->MinorOperatingSystemVersion);
442 W.printNumber("MajorImageVersion", Hdr->MajorImageVersion);
443 W.printNumber("MinorImageVersion", Hdr->MinorImageVersion);
444 W.printNumber("MajorSubsystemVersion", Hdr->MajorSubsystemVersion);
445 W.printNumber("MinorSubsystemVersion", Hdr->MinorSubsystemVersion);
446 W.printNumber("SizeOfImage", Hdr->SizeOfImage);
447 W.printNumber("SizeOfHeaders", Hdr->SizeOfHeaders);
448 W.printEnum ("Subsystem", Hdr->Subsystem, makeArrayRef(PEWindowsSubsystem));
449 W.printFlags ("Characteristics", Hdr->DLLCharacteristics,
450 makeArrayRef(PEDLLCharacteristics));
451 W.printNumber("SizeOfStackReserve", Hdr->SizeOfStackReserve);
452 W.printNumber("SizeOfStackCommit", Hdr->SizeOfStackCommit);
453 W.printNumber("SizeOfHeapReserve", Hdr->SizeOfHeapReserve);
454 W.printNumber("SizeOfHeapCommit", Hdr->SizeOfHeapCommit);
455 W.printNumber("NumberOfRvaAndSize", Hdr->NumberOfRvaAndSize);
457 if (Hdr->NumberOfRvaAndSize > 0) {
458 DictScope D(W, "DataDirectory");
459 static const char * const directory[] = {
460 "ExportTable", "ImportTable", "ResourceTable", "ExceptionTable",
461 "CertificateTable", "BaseRelocationTable", "Debug", "Architecture",
462 "GlobalPtr", "TLSTable", "LoadConfigTable", "BoundImport", "IAT",
463 "DelayImportDescriptor", "CLRRuntimeHeader", "Reserved"
466 for (uint32_t i = 0; i < Hdr->NumberOfRvaAndSize; ++i) {
467 printDataDirectory(i, directory[i]);
472 void COFFDumper::printBaseOfDataField(const pe32_header *Hdr) {
473 W.printHex("BaseOfData", Hdr->BaseOfData);
476 void COFFDumper::printBaseOfDataField(const pe32plus_header *) {}
478 void COFFDumper::printCodeViewDebugInfo(const SectionRef &Section) {
480 if (error(Section.getContents(Data)))
483 SmallVector<StringRef, 10> FunctionNames;
484 StringMap<StringRef> FunctionLineTables;
486 ListScope D(W, "CodeViewDebugInfo");
488 // FIXME: Add more offset correctness checks.
489 DataExtractor DE(Data, true, 4);
491 Magic = DE.getU32(&Offset);
492 W.printHex("Magic", Magic);
493 if (Magic != COFF::DEBUG_SECTION_MAGIC) {
494 error(object_error::parse_failed);
498 bool Finished = false;
499 while (DE.isValidOffset(Offset) && !Finished) {
500 // The section consists of a number of subsection in the following format:
501 // |Type|PayloadSize|Payload...|
502 uint32_t SubSectionType = DE.getU32(&Offset),
503 PayloadSize = DE.getU32(&Offset);
504 ListScope S(W, "Subsection");
505 W.printHex("Type", SubSectionType);
506 W.printHex("PayloadSize", PayloadSize);
507 if (PayloadSize > Data.size() - Offset) {
508 error(object_error::parse_failed);
512 StringRef Contents = Data.substr(Offset, PayloadSize);
513 if (opts::CodeViewSubsectionBytes) {
514 // Print the raw contents to simplify debugging if anything goes wrong
516 W.printBinaryBlock("Contents", Contents);
519 switch (SubSectionType) {
520 case COFF::DEBUG_SYMBOL_SUBSECTION:
521 if (opts::SectionSymbols)
522 printCodeViewSymbolsSubsection(Contents, Section, Offset);
524 case COFF::DEBUG_LINE_TABLE_SUBSECTION: {
525 // Holds a PC to file:line table. Some data to parse this subsection is
526 // stored in the other subsections, so just check sanity and store the
527 // pointers for deferred processing.
529 if (PayloadSize < 12) {
530 // There should be at least three words to store two function
531 // relocations and size of the code.
532 error(object_error::parse_failed);
536 StringRef FunctionName;
537 if (error(resolveSymbolName(Obj->getCOFFSection(Section), Offset,
540 W.printString("FunctionName", FunctionName);
541 if (FunctionLineTables.count(FunctionName) != 0) {
542 // Saw debug info for this function already?
543 error(object_error::parse_failed);
547 FunctionLineTables[FunctionName] = Contents;
548 FunctionNames.push_back(FunctionName);
551 case COFF::DEBUG_STRING_TABLE_SUBSECTION:
552 if (PayloadSize == 0 || CVStringTable.data() != nullptr ||
553 Contents.back() != '\0') {
554 // Empty or duplicate or non-null-terminated subsection.
555 error(object_error::parse_failed);
558 CVStringTable = Contents;
560 case COFF::DEBUG_INDEX_SUBSECTION:
561 // Holds the translation table from file indices
562 // to offsets in the string table.
564 if (PayloadSize == 0 ||
565 CVFileIndexToStringOffsetTable.data() != nullptr) {
566 // Empty or duplicate subsection.
567 error(object_error::parse_failed);
570 CVFileIndexToStringOffsetTable = Contents;
573 Offset += PayloadSize;
575 // Align the reading pointer by 4.
576 Offset += (-Offset) % 4;
580 // Dump the line tables now that we've read all the subsections and know all
581 // the required information.
582 for (unsigned I = 0, E = FunctionNames.size(); I != E; ++I) {
583 StringRef Name = FunctionNames[I];
584 ListScope S(W, "FunctionLineTable");
585 W.printString("FunctionName", Name);
587 DataExtractor DE(FunctionLineTables[Name], true, 4);
588 uint32_t Offset = 8; // Skip relocations.
589 uint32_t FunctionSize = DE.getU32(&Offset);
590 W.printHex("CodeSize", FunctionSize);
591 while (DE.isValidOffset(Offset)) {
592 // For each range of lines with the same filename, we have a segment
593 // in the line table. The filename string is accessed using double
594 // indirection to the string table subsection using the index subsection.
595 uint32_t OffsetInIndex = DE.getU32(&Offset),
596 SegmentLength = DE.getU32(&Offset),
597 FullSegmentSize = DE.getU32(&Offset);
598 if (FullSegmentSize != 12 + 8 * SegmentLength) {
599 error(object_error::parse_failed);
603 uint32_t FilenameOffset;
605 DataExtractor SDE(CVFileIndexToStringOffsetTable, true, 4);
606 uint32_t OffsetInSDE = OffsetInIndex;
607 if (!SDE.isValidOffset(OffsetInSDE)) {
608 error(object_error::parse_failed);
611 FilenameOffset = SDE.getU32(&OffsetInSDE);
614 if (FilenameOffset == 0 || FilenameOffset + 1 >= CVStringTable.size() ||
615 CVStringTable.data()[FilenameOffset - 1] != '\0') {
616 // Each string in an F3 subsection should be preceded by a null
618 error(object_error::parse_failed);
622 StringRef Filename(CVStringTable.data() + FilenameOffset);
623 ListScope S(W, "FilenameSegment");
624 W.printString("Filename", Filename);
625 for (unsigned J = 0; J != SegmentLength && DE.isValidOffset(Offset);
627 // Then go the (PC, LineNumber) pairs. The line number is stored in the
628 // least significant 31 bits of the respective word in the table.
629 uint32_t PC = DE.getU32(&Offset),
630 LineNumber = DE.getU32(&Offset) & 0x7fffffff;
631 if (PC >= FunctionSize) {
632 error(object_error::parse_failed);
636 format("+0x%X", PC).snprint(Buffer, 32);
637 W.printNumber(Buffer, LineNumber);
643 void COFFDumper::printCodeViewSymbolsSubsection(StringRef Subsection,
644 const SectionRef &Section,
645 uint32_t OffsetInSection) {
646 if (Subsection.size() == 0) {
647 error(object_error::parse_failed);
650 DataExtractor DE(Subsection, true, 4);
653 // Function-level subsections have "procedure start" and "procedure end"
654 // commands that should come in pairs and surround relevant info.
655 bool InFunctionScope = false;
656 while (DE.isValidOffset(Offset)) {
657 // Read subsection segments one by one.
658 uint16_t Size = DE.getU16(&Offset);
659 // The section size includes the size of the type identifier.
660 if (Size < 2 || !DE.isValidOffsetForDataOfSize(Offset, Size)) {
661 error(object_error::parse_failed);
665 uint16_t Type = DE.getU16(&Offset);
667 case COFF::DEBUG_SYMBOL_TYPE_PROC_START: {
668 DictScope S(W, "ProcStart");
669 if (InFunctionScope || Size < 36) {
670 error(object_error::parse_failed);
673 InFunctionScope = true;
675 // We're currently interested in a limited subset of fields in this
676 // segment, just ignore the rest of the fields for now.
678 DE.getU8(&Offset, Unused, 12);
679 uint32_t CodeSize = DE.getU32(&Offset);
680 DE.getU8(&Offset, Unused, 12);
681 StringRef SectionName;
682 if (error(resolveSymbolName(Obj->getCOFFSection(Section),
683 OffsetInSection + Offset, SectionName)))
686 DE.getU8(&Offset, Unused, 3);
687 StringRef DisplayName = DE.getCStr(&Offset);
688 if (!DE.isValidOffset(Offset)) {
689 error(object_error::parse_failed);
692 W.printString("DisplayName", DisplayName);
693 W.printString("Section", SectionName);
694 W.printHex("CodeSize", CodeSize);
698 case COFF::DEBUG_SYMBOL_TYPE_PROC_END: {
699 W.startLine() << "ProcEnd\n";
700 if (!InFunctionScope || Size > 0) {
701 error(object_error::parse_failed);
704 InFunctionScope = false;
708 if (opts::CodeViewSubsectionBytes) {
709 ListScope S(W, "Record");
710 W.printHex("Size", Size);
711 W.printHex("Type", Type);
713 StringRef Contents = DE.getData().substr(Offset, Size);
714 W.printBinaryBlock("Contents", Contents);
724 error(object_error::parse_failed);
727 void COFFDumper::printSections() {
728 ListScope SectionsD(W, "Sections");
729 int SectionNumber = 0;
730 for (const SectionRef &Sec : Obj->sections()) {
732 const coff_section *Section = Obj->getCOFFSection(Sec);
735 if (error(Sec.getName(Name)))
738 DictScope D(W, "Section");
739 W.printNumber("Number", SectionNumber);
740 W.printBinary("Name", Name, Section->Name);
741 W.printHex ("VirtualSize", Section->VirtualSize);
742 W.printHex ("VirtualAddress", Section->VirtualAddress);
743 W.printNumber("RawDataSize", Section->SizeOfRawData);
744 W.printHex ("PointerToRawData", Section->PointerToRawData);
745 W.printHex ("PointerToRelocations", Section->PointerToRelocations);
746 W.printHex ("PointerToLineNumbers", Section->PointerToLinenumbers);
747 W.printNumber("RelocationCount", Section->NumberOfRelocations);
748 W.printNumber("LineNumberCount", Section->NumberOfLinenumbers);
749 W.printFlags ("Characteristics", Section->Characteristics,
750 makeArrayRef(ImageSectionCharacteristics),
751 COFF::SectionCharacteristics(0x00F00000));
753 if (opts::SectionRelocations) {
754 ListScope D(W, "Relocations");
755 for (const RelocationRef &Reloc : Sec.relocations())
756 printRelocation(Sec, Reloc);
759 if (opts::SectionSymbols) {
760 ListScope D(W, "Symbols");
761 for (const SymbolRef &Symbol : Obj->symbols()) {
762 if (!Sec.containsSymbol(Symbol))
769 if (Name == ".debug$S" && opts::CodeView)
770 printCodeViewDebugInfo(Sec);
772 if (opts::SectionData &&
773 !(Section->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)) {
775 if (error(Sec.getContents(Data)))
778 W.printBinaryBlock("SectionData", Data);
783 void COFFDumper::printRelocations() {
784 ListScope D(W, "Relocations");
786 int SectionNumber = 0;
787 for (const SectionRef &Section : Obj->sections()) {
790 if (error(Section.getName(Name)))
793 bool PrintedGroup = false;
794 for (const RelocationRef &Reloc : Section.relocations()) {
796 W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
801 printRelocation(Section, Reloc);
806 W.startLine() << "}\n";
811 void COFFDumper::printRelocation(const SectionRef &Section,
812 const RelocationRef &Reloc) {
813 uint64_t Offset = Reloc.getOffset();
814 uint64_t RelocType = Reloc.getType();
815 SmallString<32> RelocName;
816 StringRef SymbolName;
817 Reloc.getTypeName(RelocName);
818 symbol_iterator Symbol = Reloc.getSymbol();
819 if (Symbol != Obj->symbol_end()) {
820 ErrorOr<StringRef> SymbolNameOrErr = Symbol->getName();
821 if (error(SymbolNameOrErr.getError()))
823 SymbolName = *SymbolNameOrErr;
826 if (opts::ExpandRelocs) {
827 DictScope Group(W, "Relocation");
828 W.printHex("Offset", Offset);
829 W.printNumber("Type", RelocName, RelocType);
830 W.printString("Symbol", SymbolName.empty() ? "-" : SymbolName);
832 raw_ostream& OS = W.startLine();
835 << " " << (SymbolName.empty() ? "-" : SymbolName)
840 void COFFDumper::printSymbols() {
841 ListScope Group(W, "Symbols");
843 for (const SymbolRef &Symbol : Obj->symbols())
847 void COFFDumper::printDynamicSymbols() { ListScope Group(W, "DynamicSymbols"); }
849 static ErrorOr<StringRef>
850 getSectionName(const llvm::object::COFFObjectFile *Obj, int32_t SectionNumber,
851 const coff_section *Section) {
853 StringRef SectionName;
854 if (std::error_code EC = Obj->getSectionName(Section, SectionName))
858 if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
859 return StringRef("IMAGE_SYM_DEBUG");
860 if (SectionNumber == llvm::COFF::IMAGE_SYM_ABSOLUTE)
861 return StringRef("IMAGE_SYM_ABSOLUTE");
862 if (SectionNumber == llvm::COFF::IMAGE_SYM_UNDEFINED)
863 return StringRef("IMAGE_SYM_UNDEFINED");
864 return StringRef("");
867 void COFFDumper::printSymbol(const SymbolRef &Sym) {
868 DictScope D(W, "Symbol");
870 COFFSymbolRef Symbol = Obj->getCOFFSymbol(Sym);
871 const coff_section *Section;
872 if (std::error_code EC = Obj->getSection(Symbol.getSectionNumber(), Section)) {
873 W.startLine() << "Invalid section number: " << EC.message() << "\n";
878 StringRef SymbolName;
879 if (Obj->getSymbolName(Symbol, SymbolName))
882 StringRef SectionName = "";
883 ErrorOr<StringRef> Res =
884 getSectionName(Obj, Symbol.getSectionNumber(), Section);
888 W.printString("Name", SymbolName);
889 W.printNumber("Value", Symbol.getValue());
890 W.printNumber("Section", SectionName, Symbol.getSectionNumber());
891 W.printEnum ("BaseType", Symbol.getBaseType(), makeArrayRef(ImageSymType));
892 W.printEnum ("ComplexType", Symbol.getComplexType(),
893 makeArrayRef(ImageSymDType));
894 W.printEnum ("StorageClass", Symbol.getStorageClass(),
895 makeArrayRef(ImageSymClass));
896 W.printNumber("AuxSymbolCount", Symbol.getNumberOfAuxSymbols());
898 for (uint8_t I = 0; I < Symbol.getNumberOfAuxSymbols(); ++I) {
899 if (Symbol.isFunctionDefinition()) {
900 const coff_aux_function_definition *Aux;
901 if (error(getSymbolAuxData(Obj, Symbol, I, Aux)))
904 DictScope AS(W, "AuxFunctionDef");
905 W.printNumber("TagIndex", Aux->TagIndex);
906 W.printNumber("TotalSize", Aux->TotalSize);
907 W.printHex("PointerToLineNumber", Aux->PointerToLinenumber);
908 W.printHex("PointerToNextFunction", Aux->PointerToNextFunction);
910 } else if (Symbol.isAnyUndefined()) {
911 const coff_aux_weak_external *Aux;
912 if (error(getSymbolAuxData(Obj, Symbol, I, Aux)))
915 ErrorOr<COFFSymbolRef> Linked = Obj->getSymbol(Aux->TagIndex);
916 StringRef LinkedName;
917 std::error_code EC = Linked.getError();
918 if (EC || (EC = Obj->getSymbolName(*Linked, LinkedName))) {
923 DictScope AS(W, "AuxWeakExternal");
924 W.printNumber("Linked", LinkedName, Aux->TagIndex);
925 W.printEnum ("Search", Aux->Characteristics,
926 makeArrayRef(WeakExternalCharacteristics));
928 } else if (Symbol.isFileRecord()) {
929 const char *FileName;
930 if (error(getSymbolAuxData(Obj, Symbol, I, FileName)))
933 DictScope AS(W, "AuxFileRecord");
935 StringRef Name(FileName, Symbol.getNumberOfAuxSymbols() *
936 Obj->getSymbolTableEntrySize());
937 W.printString("FileName", Name.rtrim(StringRef("\0", 1)));
939 } else if (Symbol.isSectionDefinition()) {
940 const coff_aux_section_definition *Aux;
941 if (error(getSymbolAuxData(Obj, Symbol, I, Aux)))
944 int32_t AuxNumber = Aux->getNumber(Symbol.isBigObj());
946 DictScope AS(W, "AuxSectionDef");
947 W.printNumber("Length", Aux->Length);
948 W.printNumber("RelocationCount", Aux->NumberOfRelocations);
949 W.printNumber("LineNumberCount", Aux->NumberOfLinenumbers);
950 W.printHex("Checksum", Aux->CheckSum);
951 W.printNumber("Number", AuxNumber);
952 W.printEnum("Selection", Aux->Selection, makeArrayRef(ImageCOMDATSelect));
954 if (Section && Section->Characteristics & COFF::IMAGE_SCN_LNK_COMDAT
955 && Aux->Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
956 const coff_section *Assoc;
957 StringRef AssocName = "";
958 std::error_code EC = Obj->getSection(AuxNumber, Assoc);
959 ErrorOr<StringRef> Res = getSectionName(Obj, AuxNumber, Assoc);
969 W.printNumber("AssocSection", AssocName, AuxNumber);
971 } else if (Symbol.isCLRToken()) {
972 const coff_aux_clr_token *Aux;
973 if (error(getSymbolAuxData(Obj, Symbol, I, Aux)))
976 ErrorOr<COFFSymbolRef> ReferredSym =
977 Obj->getSymbol(Aux->SymbolTableIndex);
978 StringRef ReferredName;
979 std::error_code EC = ReferredSym.getError();
980 if (EC || (EC = Obj->getSymbolName(*ReferredSym, ReferredName))) {
985 DictScope AS(W, "AuxCLRToken");
986 W.printNumber("AuxType", Aux->AuxType);
987 W.printNumber("Reserved", Aux->Reserved);
988 W.printNumber("SymbolTableIndex", ReferredName, Aux->SymbolTableIndex);
991 W.startLine() << "<unhandled auxiliary record>\n";
996 void COFFDumper::printUnwindInfo() {
997 ListScope D(W, "UnwindInformation");
998 switch (Obj->getMachine()) {
999 case COFF::IMAGE_FILE_MACHINE_AMD64: {
1000 Win64EH::Dumper Dumper(W);
1001 Win64EH::Dumper::SymbolResolver
1002 Resolver = [](const object::coff_section *Section, uint64_t Offset,
1003 SymbolRef &Symbol, void *user_data) -> std::error_code {
1004 COFFDumper *Dumper = reinterpret_cast<COFFDumper *>(user_data);
1005 return Dumper->resolveSymbol(Section, Offset, Symbol);
1007 Win64EH::Dumper::Context Ctx(*Obj, Resolver, this);
1008 Dumper.printData(Ctx);
1011 case COFF::IMAGE_FILE_MACHINE_ARMNT: {
1012 ARM::WinEH::Decoder Decoder(W);
1013 Decoder.dumpProcedureData(*Obj);
1017 W.printEnum("unsupported Image Machine", Obj->getMachine(),
1018 makeArrayRef(ImageFileMachineType));
1023 void COFFDumper::printImportedSymbols(
1024 iterator_range<imported_symbol_iterator> Range) {
1025 for (const ImportedSymbolRef &I : Range) {
1027 if (error(I.getSymbolName(Sym))) return;
1029 if (error(I.getOrdinal(Ordinal))) return;
1030 W.printNumber("Symbol", Sym, Ordinal);
1034 void COFFDumper::printDelayImportedSymbols(
1035 const DelayImportDirectoryEntryRef &I,
1036 iterator_range<imported_symbol_iterator> Range) {
1038 for (const ImportedSymbolRef &S : Range) {
1039 DictScope Import(W, "Import");
1041 if (error(S.getSymbolName(Sym))) return;
1043 if (error(S.getOrdinal(Ordinal))) return;
1044 W.printNumber("Symbol", Sym, Ordinal);
1046 if (error(I.getImportAddress(Index++, Addr))) return;
1047 W.printHex("Address", Addr);
1051 void COFFDumper::printCOFFImports() {
1053 for (const ImportDirectoryEntryRef &I : Obj->import_directories()) {
1054 DictScope Import(W, "Import");
1056 if (error(I.getName(Name))) return;
1057 W.printString("Name", Name);
1059 if (error(I.getImportLookupTableRVA(Addr))) return;
1060 W.printHex("ImportLookupTableRVA", Addr);
1061 if (error(I.getImportAddressTableRVA(Addr))) return;
1062 W.printHex("ImportAddressTableRVA", Addr);
1063 printImportedSymbols(I.imported_symbols());
1067 for (const DelayImportDirectoryEntryRef &I : Obj->delay_import_directories()) {
1068 DictScope Import(W, "DelayImport");
1070 if (error(I.getName(Name))) return;
1071 W.printString("Name", Name);
1072 const delay_import_directory_table_entry *Table;
1073 if (error(I.getDelayImportTable(Table))) return;
1074 W.printHex("Attributes", Table->Attributes);
1075 W.printHex("ModuleHandle", Table->ModuleHandle);
1076 W.printHex("ImportAddressTable", Table->DelayImportAddressTable);
1077 W.printHex("ImportNameTable", Table->DelayImportNameTable);
1078 W.printHex("BoundDelayImportTable", Table->BoundDelayImportTable);
1079 W.printHex("UnloadDelayImportTable", Table->UnloadDelayImportTable);
1080 printDelayImportedSymbols(I, I.imported_symbols());
1084 void COFFDumper::printCOFFExports() {
1085 for (const ExportDirectoryEntryRef &E : Obj->export_directories()) {
1086 DictScope Export(W, "Export");
1089 uint32_t Ordinal, RVA;
1091 if (error(E.getSymbolName(Name)))
1093 if (error(E.getOrdinal(Ordinal)))
1095 if (error(E.getExportRVA(RVA)))
1098 W.printNumber("Ordinal", Ordinal);
1099 W.printString("Name", Name);
1100 W.printHex("RVA", RVA);
1104 void COFFDumper::printCOFFDirectives() {
1105 for (const SectionRef &Section : Obj->sections()) {
1109 if (error(Section.getName(Name)))
1111 if (Name != ".drectve")
1114 if (error(Section.getContents(Contents)))
1117 W.printString("Directive(s)", Contents);
1121 static StringRef getBaseRelocTypeName(uint8_t Type) {
1123 case COFF::IMAGE_REL_BASED_ABSOLUTE: return "ABSOLUTE";
1124 case COFF::IMAGE_REL_BASED_HIGH: return "HIGH";
1125 case COFF::IMAGE_REL_BASED_LOW: return "LOW";
1126 case COFF::IMAGE_REL_BASED_HIGHLOW: return "HIGHLOW";
1127 case COFF::IMAGE_REL_BASED_HIGHADJ: return "HIGHADJ";
1128 case COFF::IMAGE_REL_BASED_ARM_MOV32T: return "ARM_MOV32(T)";
1129 case COFF::IMAGE_REL_BASED_DIR64: return "DIR64";
1130 default: return "unknown (" + llvm::utostr(Type) + ")";
1134 void COFFDumper::printCOFFBaseReloc() {
1135 ListScope D(W, "BaseReloc");
1136 for (const BaseRelocRef &I : Obj->base_relocs()) {
1139 if (error(I.getRVA(RVA)))
1141 if (error(I.getType(Type)))
1143 DictScope Import(W, "Entry");
1144 W.printString("Type", getBaseRelocTypeName(Type));
1145 W.printHex("Address", RVA);
1149 void COFFDumper::printStackMap() const {
1150 object::SectionRef StackMapSection;
1151 for (auto Sec : Obj->sections()) {
1154 if (Name == ".llvm_stackmaps") {
1155 StackMapSection = Sec;
1160 if (StackMapSection == object::SectionRef())
1163 StringRef StackMapContents;
1164 StackMapSection.getContents(StackMapContents);
1165 ArrayRef<uint8_t> StackMapContentsArray(
1166 reinterpret_cast<const uint8_t*>(StackMapContents.data()),
1167 StackMapContents.size());
1169 if (Obj->isLittleEndian())
1170 prettyPrintStackMap(
1172 StackMapV1Parser<support::little>(StackMapContentsArray));
1174 prettyPrintStackMap(llvm::outs(),
1175 StackMapV1Parser<support::big>(StackMapContentsArray));