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 "ObjDumper.h"
19 #include "StreamWriter.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/Object/COFF.h"
24 #include "llvm/Object/ObjectFile.h"
25 #include "llvm/Support/Casting.h"
26 #include "llvm/Support/Compiler.h"
27 #include "llvm/Support/Format.h"
28 #include "llvm/Support/SourceMgr.h"
29 #include "llvm/Support/Win64EH.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/Support/system_error.h"
38 using namespace llvm::object;
39 using namespace llvm::Win64EH;
43 class COFFDumper : public ObjDumper {
45 COFFDumper(const llvm::object::COFFObjectFile *Obj, StreamWriter& Writer)
51 virtual void printFileHeaders() LLVM_OVERRIDE;
52 virtual void printSections() LLVM_OVERRIDE;
53 virtual void printRelocations() LLVM_OVERRIDE;
54 virtual void printSymbols() LLVM_OVERRIDE;
55 virtual void printDynamicSymbols() LLVM_OVERRIDE;
56 virtual void printUnwindInfo() LLVM_OVERRIDE;
59 void printSymbol(symbol_iterator SymI);
61 void printRelocation(section_iterator SecI, relocation_iterator RelI);
63 void printX64UnwindInfo();
65 void printRuntimeFunction(
66 const RuntimeFunction& RTF,
67 uint64_t OffsetInSection,
68 const std::vector<RelocationRef> &Rels);
71 const Win64EH::UnwindInfo& UI,
72 uint64_t OffsetInSection,
73 const std::vector<RelocationRef> &Rels);
75 void printUnwindCode(const Win64EH::UnwindInfo& UI, ArrayRef<UnwindCode> UCs);
77 void cacheRelocations();
79 error_code getSectionContents(
80 const std::vector<RelocationRef> &Rels,
82 ArrayRef<uint8_t> &Contents,
85 error_code getSection(
86 const std::vector<RelocationRef> &Rels,
88 const coff_section **Section,
91 typedef DenseMap<const coff_section*, std::vector<RelocationRef> > RelocMapTy;
93 const llvm::object::COFFObjectFile *Obj;
95 std::vector<RelocationRef> EmptyRelocs;
103 error_code createCOFFDumper(const object::ObjectFile *Obj,
104 StreamWriter& Writer,
105 OwningPtr<ObjDumper> &Result) {
106 const COFFObjectFile *COFFObj = dyn_cast<COFFObjectFile>(Obj);
108 return readobj_error::unsupported_obj_file_format;
110 Result.reset(new COFFDumper(COFFObj, Writer));
111 return readobj_error::success;
117 // Returns the name of the unwind code.
118 static StringRef getUnwindCodeTypeName(uint8_t Code) {
120 default: llvm_unreachable("Invalid unwind code");
121 case UOP_PushNonVol: return "PUSH_NONVOL";
122 case UOP_AllocLarge: return "ALLOC_LARGE";
123 case UOP_AllocSmall: return "ALLOC_SMALL";
124 case UOP_SetFPReg: return "SET_FPREG";
125 case UOP_SaveNonVol: return "SAVE_NONVOL";
126 case UOP_SaveNonVolBig: return "SAVE_NONVOL_FAR";
127 case UOP_SaveXMM128: return "SAVE_XMM128";
128 case UOP_SaveXMM128Big: return "SAVE_XMM128_FAR";
129 case UOP_PushMachFrame: return "PUSH_MACHFRAME";
133 // Returns the name of a referenced register.
134 static StringRef getUnwindRegisterName(uint8_t Reg) {
136 default: llvm_unreachable("Invalid register");
137 case 0: return "RAX";
138 case 1: return "RCX";
139 case 2: return "RDX";
140 case 3: return "RBX";
141 case 4: return "RSP";
142 case 5: return "RBP";
143 case 6: return "RSI";
144 case 7: return "RDI";
147 case 10: return "R10";
148 case 11: return "R11";
149 case 12: return "R12";
150 case 13: return "R13";
151 case 14: return "R14";
152 case 15: return "R15";
156 // Calculates the number of array slots required for the unwind code.
157 static unsigned getNumUsedSlots(const UnwindCode &UnwindCode) {
158 switch (UnwindCode.getUnwindOp()) {
159 default: llvm_unreachable("Invalid unwind code");
163 case UOP_PushMachFrame:
168 case UOP_SaveNonVolBig:
169 case UOP_SaveXMM128Big:
172 return (UnwindCode.getOpInfo() == 0) ? 2 : 3;
176 // Given a symbol sym this functions returns the address and section of it.
177 static error_code resolveSectionAndAddress(const COFFObjectFile *Obj,
178 const SymbolRef &Sym,
179 const coff_section *&ResolvedSection,
180 uint64_t &ResolvedAddr) {
181 if (error_code EC = Sym.getAddress(ResolvedAddr))
184 section_iterator iter(Obj->begin_sections());
185 if (error_code EC = Sym.getSection(iter))
188 ResolvedSection = Obj->getCOFFSection(iter);
189 return object_error::success;
192 // Given a vector of relocations for a section and an offset into this section
193 // the function returns the symbol used for the relocation at the offset.
194 static error_code resolveSymbol(const std::vector<RelocationRef> &Rels,
195 uint64_t Offset, SymbolRef &Sym) {
196 for (std::vector<RelocationRef>::const_iterator RelI = Rels.begin(),
198 RelI != RelE; ++RelI) {
200 if (error_code EC = RelI->getOffset(Ofs))
204 if (error_code EC = RelI->getSymbol(Sym))
206 return readobj_error::success;
210 return readobj_error::unknown_symbol;
213 // Given a vector of relocations for a section and an offset into this section
214 // the function returns the name of the symbol used for the relocation at the
216 static error_code resolveSymbolName(const std::vector<RelocationRef> &Rels,
217 uint64_t Offset, StringRef &Name) {
219 if (error_code EC = resolveSymbol(Rels, Offset, Sym)) return EC;
220 if (error_code EC = Sym.getName(Name)) return EC;
221 return object_error::success;
224 static const EnumEntry<COFF::MachineTypes> ImageFileMachineType[] = {
225 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_UNKNOWN ),
226 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AM33 ),
227 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AMD64 ),
228 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARM ),
229 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARMV7 ),
230 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_EBC ),
231 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_I386 ),
232 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_IA64 ),
233 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_M32R ),
234 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPS16 ),
235 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU ),
236 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU16),
237 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPC ),
238 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPCFP),
239 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_R4000 ),
240 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3 ),
241 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3DSP ),
242 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH4 ),
243 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH5 ),
244 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_THUMB ),
245 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_WCEMIPSV2)
248 static const EnumEntry<COFF::Characteristics> ImageFileCharacteristics[] = {
249 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_RELOCS_STRIPPED ),
250 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_EXECUTABLE_IMAGE ),
251 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LINE_NUMS_STRIPPED ),
252 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LOCAL_SYMS_STRIPPED ),
253 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_AGGRESSIVE_WS_TRIM ),
254 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LARGE_ADDRESS_AWARE ),
255 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_LO ),
256 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_32BIT_MACHINE ),
257 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DEBUG_STRIPPED ),
258 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP),
259 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_NET_RUN_FROM_SWAP ),
260 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_SYSTEM ),
261 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DLL ),
262 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_UP_SYSTEM_ONLY ),
263 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_HI )
266 static const EnumEntry<COFF::SectionCharacteristics>
267 ImageSectionCharacteristics[] = {
268 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_TYPE_NO_PAD ),
269 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_CODE ),
270 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_INITIALIZED_DATA ),
271 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_UNINITIALIZED_DATA),
272 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_OTHER ),
273 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_INFO ),
274 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_REMOVE ),
275 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_COMDAT ),
276 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_GPREL ),
277 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PURGEABLE ),
278 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_16BIT ),
279 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_LOCKED ),
280 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PRELOAD ),
281 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1BYTES ),
282 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2BYTES ),
283 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4BYTES ),
284 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8BYTES ),
285 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_16BYTES ),
286 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_32BYTES ),
287 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_64BYTES ),
288 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_128BYTES ),
289 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_256BYTES ),
290 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_512BYTES ),
291 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1024BYTES ),
292 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2048BYTES ),
293 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4096BYTES ),
294 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8192BYTES ),
295 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_NRELOC_OVFL ),
296 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_DISCARDABLE ),
297 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_CACHED ),
298 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_PAGED ),
299 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_SHARED ),
300 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_EXECUTE ),
301 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_READ ),
302 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_WRITE )
305 static const EnumEntry<COFF::SymbolBaseType> ImageSymType[] = {
306 { "Null" , COFF::IMAGE_SYM_TYPE_NULL },
307 { "Void" , COFF::IMAGE_SYM_TYPE_VOID },
308 { "Char" , COFF::IMAGE_SYM_TYPE_CHAR },
309 { "Short" , COFF::IMAGE_SYM_TYPE_SHORT },
310 { "Int" , COFF::IMAGE_SYM_TYPE_INT },
311 { "Long" , COFF::IMAGE_SYM_TYPE_LONG },
312 { "Float" , COFF::IMAGE_SYM_TYPE_FLOAT },
313 { "Double", COFF::IMAGE_SYM_TYPE_DOUBLE },
314 { "Struct", COFF::IMAGE_SYM_TYPE_STRUCT },
315 { "Union" , COFF::IMAGE_SYM_TYPE_UNION },
316 { "Enum" , COFF::IMAGE_SYM_TYPE_ENUM },
317 { "MOE" , COFF::IMAGE_SYM_TYPE_MOE },
318 { "Byte" , COFF::IMAGE_SYM_TYPE_BYTE },
319 { "Word" , COFF::IMAGE_SYM_TYPE_WORD },
320 { "UInt" , COFF::IMAGE_SYM_TYPE_UINT },
321 { "DWord" , COFF::IMAGE_SYM_TYPE_DWORD }
324 static const EnumEntry<COFF::SymbolComplexType> ImageSymDType[] = {
325 { "Null" , COFF::IMAGE_SYM_DTYPE_NULL },
326 { "Pointer" , COFF::IMAGE_SYM_DTYPE_POINTER },
327 { "Function", COFF::IMAGE_SYM_DTYPE_FUNCTION },
328 { "Array" , COFF::IMAGE_SYM_DTYPE_ARRAY }
331 static const EnumEntry<COFF::SymbolStorageClass> ImageSymClass[] = {
332 { "EndOfFunction" , COFF::IMAGE_SYM_CLASS_END_OF_FUNCTION },
333 { "Null" , COFF::IMAGE_SYM_CLASS_NULL },
334 { "Automatic" , COFF::IMAGE_SYM_CLASS_AUTOMATIC },
335 { "External" , COFF::IMAGE_SYM_CLASS_EXTERNAL },
336 { "Static" , COFF::IMAGE_SYM_CLASS_STATIC },
337 { "Register" , COFF::IMAGE_SYM_CLASS_REGISTER },
338 { "ExternalDef" , COFF::IMAGE_SYM_CLASS_EXTERNAL_DEF },
339 { "Label" , COFF::IMAGE_SYM_CLASS_LABEL },
340 { "UndefinedLabel" , COFF::IMAGE_SYM_CLASS_UNDEFINED_LABEL },
341 { "MemberOfStruct" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_STRUCT },
342 { "Argument" , COFF::IMAGE_SYM_CLASS_ARGUMENT },
343 { "StructTag" , COFF::IMAGE_SYM_CLASS_STRUCT_TAG },
344 { "MemberOfUnion" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_UNION },
345 { "UnionTag" , COFF::IMAGE_SYM_CLASS_UNION_TAG },
346 { "TypeDefinition" , COFF::IMAGE_SYM_CLASS_TYPE_DEFINITION },
347 { "UndefinedStatic", COFF::IMAGE_SYM_CLASS_UNDEFINED_STATIC },
348 { "EnumTag" , COFF::IMAGE_SYM_CLASS_ENUM_TAG },
349 { "MemberOfEnum" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_ENUM },
350 { "RegisterParam" , COFF::IMAGE_SYM_CLASS_REGISTER_PARAM },
351 { "BitField" , COFF::IMAGE_SYM_CLASS_BIT_FIELD },
352 { "Block" , COFF::IMAGE_SYM_CLASS_BLOCK },
353 { "Function" , COFF::IMAGE_SYM_CLASS_FUNCTION },
354 { "EndOfStruct" , COFF::IMAGE_SYM_CLASS_END_OF_STRUCT },
355 { "File" , COFF::IMAGE_SYM_CLASS_FILE },
356 { "Section" , COFF::IMAGE_SYM_CLASS_SECTION },
357 { "WeakExternal" , COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL },
358 { "CLRToken" , COFF::IMAGE_SYM_CLASS_CLR_TOKEN }
361 static const EnumEntry<COFF::COMDATType> ImageCOMDATSelect[] = {
362 { "NoDuplicates", COFF::IMAGE_COMDAT_SELECT_NODUPLICATES },
363 { "Any" , COFF::IMAGE_COMDAT_SELECT_ANY },
364 { "SameSize" , COFF::IMAGE_COMDAT_SELECT_SAME_SIZE },
365 { "ExactMatch" , COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH },
366 { "Associative" , COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE },
367 { "Largest" , COFF::IMAGE_COMDAT_SELECT_LARGEST },
368 { "Newest" , COFF::IMAGE_COMDAT_SELECT_NEWEST }
371 static const EnumEntry<COFF::WeakExternalCharacteristics>
372 WeakExternalCharacteristics[] = {
373 { "NoLibrary", COFF::IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY },
374 { "Library" , COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY },
375 { "Alias" , COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS }
378 static const EnumEntry<unsigned> UnwindFlags[] = {
379 { "ExceptionHandler", Win64EH::UNW_ExceptionHandler },
380 { "TerminateHandler", Win64EH::UNW_TerminateHandler },
381 { "ChainInfo" , Win64EH::UNW_ChainInfo }
384 static const EnumEntry<unsigned> UnwindOpInfo[] = {
403 // Some additional COFF structures not defined by llvm::object.
405 struct coff_aux_function_definition {
406 support::ulittle32_t TagIndex;
407 support::ulittle32_t TotalSize;
408 support::ulittle32_t PointerToLineNumber;
409 support::ulittle32_t PointerToNextFunction;
413 struct coff_aux_weak_external_definition {
414 support::ulittle32_t TagIndex;
415 support::ulittle32_t Characteristics;
419 struct coff_aux_file_record {
423 struct coff_aux_clr_token {
424 support::ulittle8_t AuxType;
425 support::ulittle8_t Reserved;
426 support::ulittle32_t SymbolTableIndex;
431 static uint64_t getOffsetOfLSDA(const Win64EH::UnwindInfo& UI) {
432 return static_cast<const char*>(UI.getLanguageSpecificData())
433 - reinterpret_cast<const char*>(&UI);
436 static uint32_t getLargeSlotValue(ArrayRef<UnwindCode> UCs) {
440 return UCs[1].FrameOffset + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16);
444 static error_code getSymbolAuxData(const COFFObjectFile *Obj,
445 const coff_symbol *Symbol, const T* &Aux) {
446 ArrayRef<uint8_t> AuxData = Obj->getSymbolAuxData(Symbol);
447 Aux = reinterpret_cast<const T*>(AuxData.data());
448 return readobj_error::success;
451 static std::string formatSymbol(const std::vector<RelocationRef> &Rels,
452 uint64_t Offset, uint32_t Disp) {
454 raw_string_ostream Str(Buffer);
457 if (resolveSymbolName(Rels, Offset, Sym)) {
458 Str << format(" (0x%X)", Offset);
464 Str << format(" +0x%X (0x%X)", Disp, Offset);
466 Str << format(" (0x%X)", Offset);
472 // Given a vector of relocations for a section and an offset into this section
473 // the function resolves the symbol used for the relocation at the offset and
474 // returns the section content and the address inside the content pointed to
476 error_code COFFDumper::getSectionContents(
477 const std::vector<RelocationRef> &Rels, uint64_t Offset,
478 ArrayRef<uint8_t> &Contents, uint64_t &Addr) {
481 const coff_section *Section;
483 if (error_code EC = resolveSymbol(Rels, Offset, Sym))
485 if (error_code EC = resolveSectionAndAddress(Obj, Sym, Section, Addr))
487 if (error_code EC = Obj->getSectionContents(Section, Contents))
490 return object_error::success;
493 error_code COFFDumper::getSection(
494 const std::vector<RelocationRef> &Rels, uint64_t Offset,
495 const coff_section **SectionPtr, uint64_t *AddrPtr) {
498 if (error_code EC = resolveSymbol(Rels, Offset, Sym))
501 const coff_section *Section;
503 if (error_code EC = resolveSectionAndAddress(Obj, Sym, Section, Addr))
507 *SectionPtr = Section;
511 return object_error::success;
514 void COFFDumper::cacheRelocations() {
516 for (section_iterator SecI = Obj->begin_sections(),
517 SecE = Obj->end_sections();
518 SecI != SecE; SecI.increment(EC)) {
522 const coff_section *Section = Obj->getCOFFSection(SecI);
524 for (relocation_iterator RelI = SecI->begin_relocations(),
525 RelE = SecI->end_relocations();
526 RelI != RelE; RelI.increment(EC)) {
530 RelocMap[Section].push_back(*RelI);
533 // Sort relocations by address.
534 std::sort(RelocMap[Section].begin(), RelocMap[Section].end(),
539 void COFFDumper::printFileHeaders() {
540 const coff_file_header *Header = 0;
541 if (error(Obj->getHeader(Header)))
544 time_t TDS = Header->TimeDateStamp;
545 char FormattedTime[20] = { };
546 strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS));
549 DictScope D(W, "ImageFileHeader");
550 W.printEnum ("Machine", Header->Machine,
551 makeArrayRef(ImageFileMachineType));
552 W.printNumber("SectionCount", Header->NumberOfSections);
553 W.printHex ("TimeDateStamp", FormattedTime, Header->TimeDateStamp);
554 W.printHex ("PointerToSymbolTable", Header->PointerToSymbolTable);
555 W.printNumber("SymbolCount", Header->NumberOfSymbols);
556 W.printNumber("OptionalHeaderSize", Header->SizeOfOptionalHeader);
557 W.printFlags ("Characteristics", Header->Characteristics,
558 makeArrayRef(ImageFileCharacteristics));
562 void COFFDumper::printSections() {
565 ListScope SectionsD(W, "Sections");
566 int SectionNumber = 0;
567 for (section_iterator SecI = Obj->begin_sections(),
568 SecE = Obj->end_sections();
569 SecI != SecE; SecI.increment(EC)) {
574 const coff_section *Section = Obj->getCOFFSection(SecI);
577 if (error(SecI->getName(Name)))
580 DictScope D(W, "Section");
581 W.printNumber("Number", SectionNumber);
582 W.printBinary("Name", Name, Section->Name);
583 W.printHex ("VirtualSize", Section->VirtualSize);
584 W.printHex ("VirtualAddress", Section->VirtualAddress);
585 W.printNumber("RawDataSize", Section->SizeOfRawData);
586 W.printHex ("PointerToRawData", Section->PointerToRawData);
587 W.printHex ("PointerToRelocations", Section->PointerToRelocations);
588 W.printHex ("PointerToLineNumbers", Section->PointerToLinenumbers);
589 W.printNumber("RelocationCount", Section->NumberOfRelocations);
590 W.printNumber("LineNumberCount", Section->NumberOfLinenumbers);
591 W.printFlags ("Characteristics", Section->Characteristics,
592 makeArrayRef(ImageSectionCharacteristics),
593 COFF::SectionCharacteristics(0x00F00000));
595 if (opts::SectionRelocations) {
596 ListScope D(W, "Relocations");
597 for (relocation_iterator RelI = SecI->begin_relocations(),
598 RelE = SecI->end_relocations();
599 RelI != RelE; RelI.increment(EC)) {
600 if (error(EC)) break;
602 printRelocation(SecI, RelI);
606 if (opts::SectionSymbols) {
607 ListScope D(W, "Symbols");
608 for (symbol_iterator SymI = Obj->begin_symbols(),
609 SymE = Obj->end_symbols();
610 SymI != SymE; SymI.increment(EC)) {
611 if (error(EC)) break;
613 bool Contained = false;
614 if (SecI->containsSymbol(*SymI, Contained) || !Contained)
621 if (opts::SectionData) {
623 if (error(SecI->getContents(Data))) break;
625 W.printBinaryBlock("SectionData", Data);
630 void COFFDumper::printRelocations() {
631 ListScope D(W, "Relocations");
634 int SectionNumber = 0;
635 for (section_iterator SecI = Obj->begin_sections(),
636 SecE = Obj->end_sections();
637 SecI != SecE; SecI.increment(EC)) {
643 if (error(SecI->getName(Name)))
646 bool PrintedGroup = false;
647 for (relocation_iterator RelI = SecI->begin_relocations(),
648 RelE = SecI->end_relocations();
649 RelI != RelE; RelI.increment(EC)) {
650 if (error(EC)) break;
653 W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
658 printRelocation(SecI, RelI);
663 W.startLine() << "}\n";
668 void COFFDumper::printRelocation(section_iterator SecI,
669 relocation_iterator RelI) {
672 SmallString<32> RelocName;
674 StringRef SymbolName;
676 if (error(RelI->getOffset(Offset))) return;
677 if (error(RelI->getType(RelocType))) return;
678 if (error(RelI->getTypeName(RelocName))) return;
679 if (error(RelI->getSymbol(Symbol))) return;
680 if (error(Symbol.getName(SymbolName))) return;
681 if (error(SecI->getContents(Contents))) return;
683 if (opts::ExpandRelocs) {
684 DictScope Group(W, "Relocation");
685 W.printHex("Offset", Offset);
686 W.printNumber("Type", RelocName, RelocType);
687 W.printString("Symbol", SymbolName.size() > 0 ? SymbolName : "-");
689 raw_ostream& OS = W.startLine();
692 << " " << (SymbolName.size() > 0 ? SymbolName : "-")
697 void COFFDumper::printSymbols() {
698 ListScope Group(W, "Symbols");
701 for (symbol_iterator SymI = Obj->begin_symbols(),
702 SymE = Obj->end_symbols();
703 SymI != SymE; SymI.increment(EC)) {
704 if (error(EC)) break;
710 void COFFDumper::printDynamicSymbols() {
711 ListScope Group(W, "DynamicSymbols");
714 void COFFDumper::printSymbol(symbol_iterator SymI) {
715 DictScope D(W, "Symbol");
717 const coff_symbol *Symbol = Obj->getCOFFSymbol(SymI);
718 const coff_section *Section;
719 if (error_code EC = Obj->getSection(Symbol->SectionNumber, Section)) {
720 W.startLine() << "Invalid section number: " << EC.message() << "\n";
725 StringRef SymbolName;
726 if (Obj->getSymbolName(Symbol, SymbolName))
729 StringRef SectionName = "";
731 Obj->getSectionName(Section, SectionName);
733 W.printString("Name", SymbolName);
734 W.printNumber("Value", Symbol->Value);
735 W.printNumber("Section", SectionName, Symbol->SectionNumber);
736 W.printEnum ("BaseType", Symbol->getBaseType(), makeArrayRef(ImageSymType));
737 W.printEnum ("ComplexType", Symbol->getComplexType(),
738 makeArrayRef(ImageSymDType));
739 W.printEnum ("StorageClass", Symbol->StorageClass,
740 makeArrayRef(ImageSymClass));
741 W.printNumber("AuxSymbolCount", Symbol->NumberOfAuxSymbols);
743 for (unsigned I = 0; I < Symbol->NumberOfAuxSymbols; ++I) {
744 if (Symbol->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
745 Symbol->getBaseType() == COFF::IMAGE_SYM_TYPE_NULL &&
746 Symbol->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION &&
747 Symbol->SectionNumber > 0) {
748 const coff_aux_function_definition *Aux;
749 if (error(getSymbolAuxData(Obj, Symbol + I, Aux)))
752 DictScope AS(W, "AuxFunctionDef");
753 W.printNumber("TagIndex", Aux->TagIndex);
754 W.printNumber("TotalSize", Aux->TotalSize);
755 W.printHex("PointerToLineNumber", Aux->PointerToLineNumber);
756 W.printHex("PointerToNextFunction", Aux->PointerToNextFunction);
757 W.printBinary("Unused", makeArrayRef(Aux->Unused));
760 Symbol->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL ||
761 (Symbol->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
762 Symbol->SectionNumber == 0 &&
763 Symbol->Value == 0)) {
764 const coff_aux_weak_external_definition *Aux;
765 if (error(getSymbolAuxData(Obj, Symbol + I, Aux)))
768 const coff_symbol *Linked;
769 StringRef LinkedName;
771 if ((EC = Obj->getSymbol(Aux->TagIndex, Linked)) ||
772 (EC = Obj->getSymbolName(Linked, LinkedName))) {
777 DictScope AS(W, "AuxWeakExternal");
778 W.printNumber("Linked", LinkedName, Aux->TagIndex);
779 W.printEnum ("Search", Aux->Characteristics,
780 makeArrayRef(WeakExternalCharacteristics));
781 W.printBinary("Unused", Aux->Unused);
783 } else if (Symbol->StorageClass == COFF::IMAGE_SYM_CLASS_FILE) {
784 const coff_aux_file_record *Aux;
785 if (error(getSymbolAuxData(Obj, Symbol + I, Aux)))
788 DictScope AS(W, "AuxFileRecord");
789 W.printString("FileName", StringRef(Aux->FileName));
791 } else if (Symbol->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC ||
792 (Symbol->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
793 Symbol->SectionNumber != COFF::IMAGE_SYM_UNDEFINED)) {
794 const coff_aux_section_definition *Aux;
795 if (error(getSymbolAuxData(Obj, Symbol + I, Aux)))
798 DictScope AS(W, "AuxSectionDef");
799 W.printNumber("Length", Aux->Length);
800 W.printNumber("RelocationCount", Aux->NumberOfRelocations);
801 W.printNumber("LineNumberCount", Aux->NumberOfLinenumbers);
802 W.printHex("Checksum", Aux->CheckSum);
803 W.printNumber("Number", Aux->Number);
804 W.printEnum("Selection", Aux->Selection, makeArrayRef(ImageCOMDATSelect));
805 W.printBinary("Unused", makeArrayRef(Aux->Unused));
807 if (Section && Section->Characteristics & COFF::IMAGE_SCN_LNK_COMDAT
808 && Aux->Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
809 const coff_section *Assoc;
812 if ((EC = Obj->getSection(Aux->Number, Assoc)) ||
813 (EC = Obj->getSectionName(Assoc, AssocName))) {
818 W.printNumber("AssocSection", AssocName, Aux->Number);
820 } else if (Symbol->StorageClass == COFF::IMAGE_SYM_CLASS_CLR_TOKEN) {
821 const coff_aux_clr_token *Aux;
822 if (error(getSymbolAuxData(Obj, Symbol + I, Aux)))
825 DictScope AS(W, "AuxCLRToken");
826 W.printNumber("AuxType", Aux->AuxType);
827 W.printNumber("Reserved", Aux->Reserved);
828 W.printNumber("SymbolTableIndex", Aux->SymbolTableIndex);
829 W.printBinary("Unused", Aux->Unused);
832 W.startLine() << "<unhandled auxiliary record>\n";
837 void COFFDumper::printUnwindInfo() {
838 const coff_file_header *Header;
839 if (error(Obj->getHeader(Header)))
842 ListScope D(W, "UnwindInformation");
843 if (Header->Machine != COFF::IMAGE_FILE_MACHINE_AMD64) {
844 W.startLine() << "Unsupported image machine type "
845 "(currently only AMD64 is supported).\n";
849 printX64UnwindInfo();
852 void COFFDumper::printX64UnwindInfo() {
854 for (section_iterator SecI = Obj->begin_sections(),
855 SecE = Obj->end_sections();
856 SecI != SecE; SecI.increment(EC)) {
857 if (error(EC)) break;
860 if (error(SecI->getName(Name)))
862 if (Name != ".pdata" && !Name.startswith(".pdata$"))
865 const coff_section *PData = Obj->getCOFFSection(SecI);
867 ArrayRef<uint8_t> Contents;
868 if (error(Obj->getSectionContents(PData, Contents)) ||
872 ArrayRef<RuntimeFunction> RFs(
873 reinterpret_cast<const RuntimeFunction *>(Contents.data()),
874 Contents.size() / sizeof(RuntimeFunction));
876 for (const RuntimeFunction *I = RFs.begin(), *E = RFs.end(); I < E; ++I) {
877 const uint64_t OffsetInSection = std::distance(RFs.begin(), I)
878 * sizeof(RuntimeFunction);
880 printRuntimeFunction(*I, OffsetInSection, RelocMap[PData]);
885 void COFFDumper::printRuntimeFunction(
886 const RuntimeFunction& RTF,
887 uint64_t OffsetInSection,
888 const std::vector<RelocationRef> &Rels) {
890 DictScope D(W, "RuntimeFunction");
891 W.printString("StartAddress",
892 formatSymbol(Rels, OffsetInSection + 0, RTF.StartAddress));
893 W.printString("EndAddress",
894 formatSymbol(Rels, OffsetInSection + 4, RTF.EndAddress));
895 W.printString("UnwindInfoAddress",
896 formatSymbol(Rels, OffsetInSection + 8, RTF.UnwindInfoOffset));
898 const coff_section* XData = 0;
899 uint64_t UnwindInfoOffset = 0;
900 if (error(getSection(Rels, OffsetInSection + 8, &XData, &UnwindInfoOffset)))
903 ArrayRef<uint8_t> XContents;
904 if (error(Obj->getSectionContents(XData, XContents)) || XContents.empty())
907 UnwindInfoOffset += RTF.UnwindInfoOffset;
908 if (UnwindInfoOffset > XContents.size())
911 const Win64EH::UnwindInfo *UI =
912 reinterpret_cast<const Win64EH::UnwindInfo *>(
913 XContents.data() + UnwindInfoOffset);
915 printUnwindInfo(*UI, UnwindInfoOffset, RelocMap[XData]);
918 void COFFDumper::printUnwindInfo(
919 const Win64EH::UnwindInfo& UI,
920 uint64_t OffsetInSection,
921 const std::vector<RelocationRef> &Rels) {
922 DictScope D(W, "UnwindInfo");
923 W.printNumber("Version", UI.getVersion());
924 W.printFlags("Flags", UI.getFlags(), makeArrayRef(UnwindFlags));
925 W.printNumber("PrologSize", UI.PrologSize);
926 if (UI.getFrameRegister() != 0) {
927 W.printEnum("FrameRegister", UI.getFrameRegister(),
928 makeArrayRef(UnwindOpInfo));
929 W.printHex("FrameOffset", UI.getFrameOffset());
931 W.printString("FrameRegister", StringRef("-"));
932 W.printString("FrameOffset", StringRef("-"));
935 W.printNumber("UnwindCodeCount", UI.NumCodes);
937 ListScope CodesD(W, "UnwindCodes");
938 ArrayRef<UnwindCode> UCs(&UI.UnwindCodes[0], UI.NumCodes);
939 for (const UnwindCode *I = UCs.begin(), *E = UCs.end(); I < E; ++I) {
940 unsigned UsedSlots = getNumUsedSlots(*I);
941 if (UsedSlots > UCs.size()) {
942 errs() << "Corrupt unwind data";
945 printUnwindCode(UI, ArrayRef<UnwindCode>(I, E));
950 uint64_t LSDAOffset = OffsetInSection + getOffsetOfLSDA(UI);
951 if (UI.getFlags() & (UNW_ExceptionHandler | UNW_TerminateHandler)) {
952 W.printString("Handler", formatSymbol(Rels, LSDAOffset,
953 UI.getLanguageSpecificHandlerOffset()));
954 } else if (UI.getFlags() & UNW_ChainInfo) {
955 const RuntimeFunction *Chained = UI.getChainedFunctionEntry();
957 DictScope D(W, "Chained");
958 W.printString("StartAddress", formatSymbol(Rels, LSDAOffset + 0,
959 Chained->StartAddress));
960 W.printString("EndAddress", formatSymbol(Rels, LSDAOffset + 4,
961 Chained->EndAddress));
962 W.printString("UnwindInfoAddress", formatSymbol(Rels, LSDAOffset + 8,
963 Chained->UnwindInfoOffset));
968 // Prints one unwind code. Because an unwind code can occupy up to 3 slots in
969 // the unwind codes array, this function requires that the correct number of
970 // slots is provided.
971 void COFFDumper::printUnwindCode(const Win64EH::UnwindInfo& UI,
972 ArrayRef<UnwindCode> UCs) {
973 assert(UCs.size() >= getNumUsedSlots(UCs[0]));
975 W.startLine() << format("0x%02X: ", unsigned(UCs[0].u.CodeOffset))
976 << getUnwindCodeTypeName(UCs[0].getUnwindOp());
978 uint32_t AllocSize = 0;
980 switch (UCs[0].getUnwindOp()) {
982 outs() << " reg=" << getUnwindRegisterName(UCs[0].getOpInfo());
986 if (UCs[0].getOpInfo() == 0) {
987 AllocSize = UCs[1].FrameOffset * 8;
989 AllocSize = getLargeSlotValue(UCs);
991 outs() << " size=" << AllocSize;
994 outs() << " size=" << ((UCs[0].getOpInfo() + 1) * 8);
997 if (UI.getFrameRegister() == 0) {
998 outs() << " reg=<invalid>";
1000 outs() << " reg=" << getUnwindRegisterName(UI.getFrameRegister())
1001 << format(", offset=0x%X", UI.getFrameOffset() * 16);
1004 case UOP_SaveNonVol:
1005 outs() << " reg=" << getUnwindRegisterName(UCs[0].getOpInfo())
1006 << format(", offset=0x%X", UCs[1].FrameOffset * 8);
1008 case UOP_SaveNonVolBig:
1009 outs() << " reg=" << getUnwindRegisterName(UCs[0].getOpInfo())
1010 << format(", offset=0x%X", getLargeSlotValue(UCs));
1012 case UOP_SaveXMM128:
1013 outs() << " reg=XMM" << static_cast<uint32_t>(UCs[0].getOpInfo())
1014 << format(", offset=0x%X", UCs[1].FrameOffset * 16);
1016 case UOP_SaveXMM128Big:
1017 outs() << " reg=XMM" << static_cast<uint32_t>(UCs[0].getOpInfo())
1018 << format(", offset=0x%X", getLargeSlotValue(UCs));
1020 case UOP_PushMachFrame:
1021 outs() << " errcode=" << (UCs[0].getOpInfo() == 0 ? "no" : "yes");