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 /// \brief This file implements the COFF-specific dumper for llvm-readobj. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #include "ARMWinEHPrinter.h" 16 #include "CodeView.h" 17 #include "Error.h" 18 #include "ObjDumper.h" 19 #include "StackMapPrinter.h" 20 #include "Win64EHDumper.h" 21 #include "llvm-readobj.h" 22 #include "llvm/ADT/DenseMap.h" 23 #include "llvm/ADT/SmallString.h" 24 #include "llvm/ADT/StringExtras.h" 25 #include "llvm/DebugInfo/CodeView/CVTypeDumper.h" 26 #include "llvm/DebugInfo/CodeView/CodeView.h" 27 #include "llvm/DebugInfo/CodeView/Line.h" 28 #include "llvm/DebugInfo/CodeView/RecordSerialization.h" 29 #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h" 30 #include "llvm/DebugInfo/CodeView/SymbolDumpDelegate.h" 31 #include "llvm/DebugInfo/CodeView/SymbolDumper.h" 32 #include "llvm/DebugInfo/CodeView/SymbolRecord.h" 33 #include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h" 34 #include "llvm/DebugInfo/CodeView/TypeIndex.h" 35 #include "llvm/DebugInfo/CodeView/TypeRecord.h" 36 #include "llvm/DebugInfo/CodeView/TypeStreamMerger.h" 37 #include "llvm/DebugInfo/CodeView/TypeTableBuilder.h" 38 #include "llvm/DebugInfo/MSF/ByteStream.h" 39 #include "llvm/Object/COFF.h" 40 #include "llvm/Object/ObjectFile.h" 41 #include "llvm/Support/COFF.h" 42 #include "llvm/Support/Casting.h" 43 #include "llvm/Support/Compiler.h" 44 #include "llvm/Support/DataExtractor.h" 45 #include "llvm/Support/Format.h" 46 #include "llvm/Support/ScopedPrinter.h" 47 #include "llvm/Support/SourceMgr.h" 48 #include "llvm/Support/Win64EH.h" 49 #include "llvm/Support/raw_ostream.h" 50 #include <algorithm> 51 #include <cstring> 52 #include <system_error> 53 #include <time.h> 54 55 using namespace llvm; 56 using namespace llvm::object; 57 using namespace llvm::codeview; 58 using namespace llvm::msf; 59 using namespace llvm::support; 60 using namespace llvm::Win64EH; 61 62 namespace { 63 64 class COFFDumper : public ObjDumper { 65 public: 66 friend class COFFObjectDumpDelegate; 67 COFFDumper(const llvm::object::COFFObjectFile *Obj, ScopedPrinter &Writer) 68 : ObjDumper(Writer), Obj(Obj), Writer(Writer) {} 69 70 void printFileHeaders() override; 71 void printSections() override; 72 void printRelocations() override; 73 void printSymbols() override; 74 void printDynamicSymbols() override; 75 void printUnwindInfo() override; 76 void printCOFFImports() override; 77 void printCOFFExports() override; 78 void printCOFFDirectives() override; 79 void printCOFFBaseReloc() override; 80 void printCOFFDebugDirectory() override; 81 void printCodeViewDebugInfo() override; 82 void mergeCodeViewTypes(llvm::codeview::TypeTableBuilder &CVTypes) override; 83 void printStackMap() const override; 84 private: 85 void printSymbol(const SymbolRef &Sym); 86 void printRelocation(const SectionRef &Section, const RelocationRef &Reloc, 87 uint64_t Bias = 0); 88 void printDataDirectory(uint32_t Index, const std::string &FieldName); 89 90 void printDOSHeader(const dos_header *DH); 91 template <class PEHeader> void printPEHeader(const PEHeader *Hdr); 92 void printBaseOfDataField(const pe32_header *Hdr); 93 void printBaseOfDataField(const pe32plus_header *Hdr); 94 95 void printCodeViewSymbolSection(StringRef SectionName, const SectionRef &Section); 96 void printCodeViewTypeSection(StringRef SectionName, const SectionRef &Section); 97 StringRef getTypeName(TypeIndex Ty); 98 StringRef getFileNameForFileOffset(uint32_t FileOffset); 99 void printFileNameForOffset(StringRef Label, uint32_t FileOffset); 100 void printTypeIndex(StringRef FieldName, TypeIndex TI) { 101 // Forward to CVTypeDumper for simplicity. 102 CVTypeDumper::printTypeIndex(Writer, FieldName, TI, TypeDB); 103 } 104 105 void printCodeViewSymbolsSubsection(StringRef Subsection, 106 const SectionRef &Section, 107 StringRef SectionContents); 108 109 void printCodeViewFileChecksums(StringRef Subsection); 110 111 void printCodeViewInlineeLines(StringRef Subsection); 112 113 void printRelocatedField(StringRef Label, const coff_section *Sec, 114 uint32_t RelocOffset, uint32_t Offset, 115 StringRef *RelocSym = nullptr); 116 117 void printBinaryBlockWithRelocs(StringRef Label, const SectionRef &Sec, 118 StringRef SectionContents, StringRef Block); 119 120 /// Given a .debug$S section, find the string table and file checksum table. 121 void initializeFileAndStringTables(StringRef Data); 122 123 void cacheRelocations(); 124 125 std::error_code resolveSymbol(const coff_section *Section, uint64_t Offset, 126 SymbolRef &Sym); 127 std::error_code resolveSymbolName(const coff_section *Section, 128 uint64_t Offset, StringRef &Name); 129 std::error_code resolveSymbolName(const coff_section *Section, 130 StringRef SectionContents, 131 const void *RelocPtr, StringRef &Name); 132 void printImportedSymbols(iterator_range<imported_symbol_iterator> Range); 133 void printDelayImportedSymbols( 134 const DelayImportDirectoryEntryRef &I, 135 iterator_range<imported_symbol_iterator> Range); 136 137 typedef DenseMap<const coff_section*, std::vector<RelocationRef> > RelocMapTy; 138 139 const llvm::object::COFFObjectFile *Obj; 140 bool RelocCached = false; 141 RelocMapTy RelocMap; 142 StringRef CVFileChecksumTable; 143 StringRef CVStringTable; 144 145 ScopedPrinter &Writer; 146 TypeDatabase TypeDB; 147 }; 148 149 class COFFObjectDumpDelegate : public SymbolDumpDelegate { 150 public: 151 COFFObjectDumpDelegate(COFFDumper &CD, const SectionRef &SR, 152 const COFFObjectFile *Obj, StringRef SectionContents) 153 : CD(CD), SR(SR), SectionContents(SectionContents) { 154 Sec = Obj->getCOFFSection(SR); 155 } 156 157 uint32_t getRecordOffset(msf::StreamReader Reader) override { 158 ArrayRef<uint8_t> Data; 159 if (auto EC = Reader.readLongestContiguousChunk(Data)) { 160 llvm::consumeError(std::move(EC)); 161 return 0; 162 } 163 return Data.data() - SectionContents.bytes_begin(); 164 } 165 166 void printRelocatedField(StringRef Label, uint32_t RelocOffset, 167 uint32_t Offset, StringRef *RelocSym) override { 168 CD.printRelocatedField(Label, Sec, RelocOffset, Offset, RelocSym); 169 } 170 171 void printBinaryBlockWithRelocs(StringRef Label, 172 ArrayRef<uint8_t> Block) override { 173 StringRef SBlock(reinterpret_cast<const char *>(Block.data()), 174 Block.size()); 175 if (opts::CodeViewSubsectionBytes) 176 CD.printBinaryBlockWithRelocs(Label, SR, SectionContents, SBlock); 177 } 178 179 StringRef getFileNameForFileOffset(uint32_t FileOffset) override { 180 return CD.getFileNameForFileOffset(FileOffset); 181 } 182 183 StringRef getStringTable() override { return CD.CVStringTable; } 184 185 private: 186 COFFDumper &CD; 187 const SectionRef &SR; 188 const coff_section *Sec; 189 StringRef SectionContents; 190 }; 191 192 } // end namespace 193 194 namespace llvm { 195 196 std::error_code createCOFFDumper(const object::ObjectFile *Obj, 197 ScopedPrinter &Writer, 198 std::unique_ptr<ObjDumper> &Result) { 199 const COFFObjectFile *COFFObj = dyn_cast<COFFObjectFile>(Obj); 200 if (!COFFObj) 201 return readobj_error::unsupported_obj_file_format; 202 203 Result.reset(new COFFDumper(COFFObj, Writer)); 204 return readobj_error::success; 205 } 206 207 } // namespace llvm 208 209 // Given a a section and an offset into this section the function returns the 210 // symbol used for the relocation at the offset. 211 std::error_code COFFDumper::resolveSymbol(const coff_section *Section, 212 uint64_t Offset, SymbolRef &Sym) { 213 cacheRelocations(); 214 const auto &Relocations = RelocMap[Section]; 215 auto SymI = Obj->symbol_end(); 216 for (const auto &Relocation : Relocations) { 217 uint64_t RelocationOffset = Relocation.getOffset(); 218 219 if (RelocationOffset == Offset) { 220 SymI = Relocation.getSymbol(); 221 break; 222 } 223 } 224 if (SymI == Obj->symbol_end()) 225 return readobj_error::unknown_symbol; 226 Sym = *SymI; 227 return readobj_error::success; 228 } 229 230 // Given a section and an offset into this section the function returns the name 231 // of the symbol used for the relocation at the offset. 232 std::error_code COFFDumper::resolveSymbolName(const coff_section *Section, 233 uint64_t Offset, 234 StringRef &Name) { 235 SymbolRef Symbol; 236 if (std::error_code EC = resolveSymbol(Section, Offset, Symbol)) 237 return EC; 238 Expected<StringRef> NameOrErr = Symbol.getName(); 239 if (!NameOrErr) 240 return errorToErrorCode(NameOrErr.takeError()); 241 Name = *NameOrErr; 242 return std::error_code(); 243 } 244 245 // Helper for when you have a pointer to real data and you want to know about 246 // relocations against it. 247 std::error_code COFFDumper::resolveSymbolName(const coff_section *Section, 248 StringRef SectionContents, 249 const void *RelocPtr, 250 StringRef &Name) { 251 assert(SectionContents.data() < RelocPtr && 252 RelocPtr < SectionContents.data() + SectionContents.size() && 253 "pointer to relocated object is not in section"); 254 uint64_t Offset = ptrdiff_t(reinterpret_cast<const char *>(RelocPtr) - 255 SectionContents.data()); 256 return resolveSymbolName(Section, Offset, Name); 257 } 258 259 void COFFDumper::printRelocatedField(StringRef Label, const coff_section *Sec, 260 uint32_t RelocOffset, uint32_t Offset, 261 StringRef *RelocSym) { 262 StringRef SymStorage; 263 StringRef &Symbol = RelocSym ? *RelocSym : SymStorage; 264 if (!resolveSymbolName(Sec, RelocOffset, Symbol)) 265 W.printSymbolOffset(Label, Symbol, Offset); 266 else 267 W.printHex(Label, RelocOffset); 268 } 269 270 void COFFDumper::printBinaryBlockWithRelocs(StringRef Label, 271 const SectionRef &Sec, 272 StringRef SectionContents, 273 StringRef Block) { 274 W.printBinaryBlock(Label, Block); 275 276 assert(SectionContents.begin() < Block.begin() && 277 SectionContents.end() >= Block.end() && 278 "Block is not contained in SectionContents"); 279 uint64_t OffsetStart = Block.data() - SectionContents.data(); 280 uint64_t OffsetEnd = OffsetStart + Block.size(); 281 282 W.flush(); 283 cacheRelocations(); 284 ListScope D(W, "BlockRelocations"); 285 const coff_section *Section = Obj->getCOFFSection(Sec); 286 const auto &Relocations = RelocMap[Section]; 287 for (const auto &Relocation : Relocations) { 288 uint64_t RelocationOffset = Relocation.getOffset(); 289 if (OffsetStart <= RelocationOffset && RelocationOffset < OffsetEnd) 290 printRelocation(Sec, Relocation, OffsetStart); 291 } 292 } 293 294 static const EnumEntry<COFF::MachineTypes> ImageFileMachineType[] = { 295 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_UNKNOWN ), 296 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AM33 ), 297 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AMD64 ), 298 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARM ), 299 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARMNT ), 300 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_EBC ), 301 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_I386 ), 302 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_IA64 ), 303 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_M32R ), 304 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPS16 ), 305 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU ), 306 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU16), 307 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPC ), 308 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPCFP), 309 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_R4000 ), 310 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3 ), 311 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3DSP ), 312 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH4 ), 313 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH5 ), 314 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_THUMB ), 315 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_WCEMIPSV2) 316 }; 317 318 static const EnumEntry<COFF::Characteristics> ImageFileCharacteristics[] = { 319 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_RELOCS_STRIPPED ), 320 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_EXECUTABLE_IMAGE ), 321 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LINE_NUMS_STRIPPED ), 322 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LOCAL_SYMS_STRIPPED ), 323 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_AGGRESSIVE_WS_TRIM ), 324 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LARGE_ADDRESS_AWARE ), 325 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_LO ), 326 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_32BIT_MACHINE ), 327 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DEBUG_STRIPPED ), 328 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP), 329 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_NET_RUN_FROM_SWAP ), 330 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_SYSTEM ), 331 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DLL ), 332 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_UP_SYSTEM_ONLY ), 333 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_HI ) 334 }; 335 336 static const EnumEntry<COFF::WindowsSubsystem> PEWindowsSubsystem[] = { 337 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_UNKNOWN ), 338 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_NATIVE ), 339 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_GUI ), 340 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CUI ), 341 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_POSIX_CUI ), 342 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI ), 343 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_APPLICATION ), 344 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER), 345 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER ), 346 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_ROM ), 347 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_XBOX ), 348 }; 349 350 static const EnumEntry<COFF::DLLCharacteristics> PEDLLCharacteristics[] = { 351 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA ), 352 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE ), 353 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY ), 354 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NX_COMPAT ), 355 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION ), 356 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_SEH ), 357 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_BIND ), 358 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_APPCONTAINER ), 359 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER ), 360 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_GUARD_CF ), 361 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE), 362 }; 363 364 static const EnumEntry<COFF::SectionCharacteristics> 365 ImageSectionCharacteristics[] = { 366 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_TYPE_NOLOAD ), 367 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_TYPE_NO_PAD ), 368 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_CODE ), 369 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_INITIALIZED_DATA ), 370 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_UNINITIALIZED_DATA), 371 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_OTHER ), 372 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_INFO ), 373 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_REMOVE ), 374 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_COMDAT ), 375 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_GPREL ), 376 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PURGEABLE ), 377 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_16BIT ), 378 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_LOCKED ), 379 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PRELOAD ), 380 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1BYTES ), 381 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2BYTES ), 382 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4BYTES ), 383 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8BYTES ), 384 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_16BYTES ), 385 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_32BYTES ), 386 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_64BYTES ), 387 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_128BYTES ), 388 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_256BYTES ), 389 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_512BYTES ), 390 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1024BYTES ), 391 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2048BYTES ), 392 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4096BYTES ), 393 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8192BYTES ), 394 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_NRELOC_OVFL ), 395 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_DISCARDABLE ), 396 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_CACHED ), 397 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_PAGED ), 398 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_SHARED ), 399 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_EXECUTE ), 400 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_READ ), 401 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_WRITE ) 402 }; 403 404 static const EnumEntry<COFF::SymbolBaseType> ImageSymType[] = { 405 { "Null" , COFF::IMAGE_SYM_TYPE_NULL }, 406 { "Void" , COFF::IMAGE_SYM_TYPE_VOID }, 407 { "Char" , COFF::IMAGE_SYM_TYPE_CHAR }, 408 { "Short" , COFF::IMAGE_SYM_TYPE_SHORT }, 409 { "Int" , COFF::IMAGE_SYM_TYPE_INT }, 410 { "Long" , COFF::IMAGE_SYM_TYPE_LONG }, 411 { "Float" , COFF::IMAGE_SYM_TYPE_FLOAT }, 412 { "Double", COFF::IMAGE_SYM_TYPE_DOUBLE }, 413 { "Struct", COFF::IMAGE_SYM_TYPE_STRUCT }, 414 { "Union" , COFF::IMAGE_SYM_TYPE_UNION }, 415 { "Enum" , COFF::IMAGE_SYM_TYPE_ENUM }, 416 { "MOE" , COFF::IMAGE_SYM_TYPE_MOE }, 417 { "Byte" , COFF::IMAGE_SYM_TYPE_BYTE }, 418 { "Word" , COFF::IMAGE_SYM_TYPE_WORD }, 419 { "UInt" , COFF::IMAGE_SYM_TYPE_UINT }, 420 { "DWord" , COFF::IMAGE_SYM_TYPE_DWORD } 421 }; 422 423 static const EnumEntry<COFF::SymbolComplexType> ImageSymDType[] = { 424 { "Null" , COFF::IMAGE_SYM_DTYPE_NULL }, 425 { "Pointer" , COFF::IMAGE_SYM_DTYPE_POINTER }, 426 { "Function", COFF::IMAGE_SYM_DTYPE_FUNCTION }, 427 { "Array" , COFF::IMAGE_SYM_DTYPE_ARRAY } 428 }; 429 430 static const EnumEntry<COFF::SymbolStorageClass> ImageSymClass[] = { 431 { "EndOfFunction" , COFF::IMAGE_SYM_CLASS_END_OF_FUNCTION }, 432 { "Null" , COFF::IMAGE_SYM_CLASS_NULL }, 433 { "Automatic" , COFF::IMAGE_SYM_CLASS_AUTOMATIC }, 434 { "External" , COFF::IMAGE_SYM_CLASS_EXTERNAL }, 435 { "Static" , COFF::IMAGE_SYM_CLASS_STATIC }, 436 { "Register" , COFF::IMAGE_SYM_CLASS_REGISTER }, 437 { "ExternalDef" , COFF::IMAGE_SYM_CLASS_EXTERNAL_DEF }, 438 { "Label" , COFF::IMAGE_SYM_CLASS_LABEL }, 439 { "UndefinedLabel" , COFF::IMAGE_SYM_CLASS_UNDEFINED_LABEL }, 440 { "MemberOfStruct" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_STRUCT }, 441 { "Argument" , COFF::IMAGE_SYM_CLASS_ARGUMENT }, 442 { "StructTag" , COFF::IMAGE_SYM_CLASS_STRUCT_TAG }, 443 { "MemberOfUnion" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_UNION }, 444 { "UnionTag" , COFF::IMAGE_SYM_CLASS_UNION_TAG }, 445 { "TypeDefinition" , COFF::IMAGE_SYM_CLASS_TYPE_DEFINITION }, 446 { "UndefinedStatic", COFF::IMAGE_SYM_CLASS_UNDEFINED_STATIC }, 447 { "EnumTag" , COFF::IMAGE_SYM_CLASS_ENUM_TAG }, 448 { "MemberOfEnum" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_ENUM }, 449 { "RegisterParam" , COFF::IMAGE_SYM_CLASS_REGISTER_PARAM }, 450 { "BitField" , COFF::IMAGE_SYM_CLASS_BIT_FIELD }, 451 { "Block" , COFF::IMAGE_SYM_CLASS_BLOCK }, 452 { "Function" , COFF::IMAGE_SYM_CLASS_FUNCTION }, 453 { "EndOfStruct" , COFF::IMAGE_SYM_CLASS_END_OF_STRUCT }, 454 { "File" , COFF::IMAGE_SYM_CLASS_FILE }, 455 { "Section" , COFF::IMAGE_SYM_CLASS_SECTION }, 456 { "WeakExternal" , COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL }, 457 { "CLRToken" , COFF::IMAGE_SYM_CLASS_CLR_TOKEN } 458 }; 459 460 static const EnumEntry<COFF::COMDATType> ImageCOMDATSelect[] = { 461 { "NoDuplicates", COFF::IMAGE_COMDAT_SELECT_NODUPLICATES }, 462 { "Any" , COFF::IMAGE_COMDAT_SELECT_ANY }, 463 { "SameSize" , COFF::IMAGE_COMDAT_SELECT_SAME_SIZE }, 464 { "ExactMatch" , COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH }, 465 { "Associative" , COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE }, 466 { "Largest" , COFF::IMAGE_COMDAT_SELECT_LARGEST }, 467 { "Newest" , COFF::IMAGE_COMDAT_SELECT_NEWEST } 468 }; 469 470 static const EnumEntry<COFF::DebugType> ImageDebugType[] = { 471 { "Unknown" , COFF::IMAGE_DEBUG_TYPE_UNKNOWN }, 472 { "COFF" , COFF::IMAGE_DEBUG_TYPE_COFF }, 473 { "CodeView" , COFF::IMAGE_DEBUG_TYPE_CODEVIEW }, 474 { "FPO" , COFF::IMAGE_DEBUG_TYPE_FPO }, 475 { "Misc" , COFF::IMAGE_DEBUG_TYPE_MISC }, 476 { "Exception" , COFF::IMAGE_DEBUG_TYPE_EXCEPTION }, 477 { "Fixup" , COFF::IMAGE_DEBUG_TYPE_FIXUP }, 478 { "OmapToSrc" , COFF::IMAGE_DEBUG_TYPE_OMAP_TO_SRC }, 479 { "OmapFromSrc", COFF::IMAGE_DEBUG_TYPE_OMAP_FROM_SRC }, 480 { "Borland" , COFF::IMAGE_DEBUG_TYPE_BORLAND }, 481 { "Reserved10" , COFF::IMAGE_DEBUG_TYPE_RESERVED10 }, 482 { "CLSID" , COFF::IMAGE_DEBUG_TYPE_CLSID }, 483 { "VCFeature" , COFF::IMAGE_DEBUG_TYPE_VC_FEATURE }, 484 { "POGO" , COFF::IMAGE_DEBUG_TYPE_POGO }, 485 { "ILTCG" , COFF::IMAGE_DEBUG_TYPE_ILTCG }, 486 { "MPX" , COFF::IMAGE_DEBUG_TYPE_MPX }, 487 { "Repro" , COFF::IMAGE_DEBUG_TYPE_REPRO }, 488 }; 489 490 static const EnumEntry<COFF::WeakExternalCharacteristics> 491 WeakExternalCharacteristics[] = { 492 { "NoLibrary", COFF::IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY }, 493 { "Library" , COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY }, 494 { "Alias" , COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS } 495 }; 496 497 static const EnumEntry<uint32_t> SubSectionTypes[] = { 498 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, Symbols), 499 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, Lines), 500 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, StringTable), 501 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, FileChecksums), 502 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, FrameData), 503 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, InlineeLines), 504 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, CrossScopeImports), 505 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, CrossScopeExports), 506 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, ILLines), 507 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, FuncMDTokenMap), 508 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, TypeMDTokenMap), 509 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, MergedAssemblyInput), 510 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, CoffSymbolRVA), 511 }; 512 513 static const EnumEntry<uint32_t> FrameDataFlags[] = { 514 LLVM_READOBJ_ENUM_ENT(FrameData, HasSEH), 515 LLVM_READOBJ_ENUM_ENT(FrameData, HasEH), 516 LLVM_READOBJ_ENUM_ENT(FrameData, IsFunctionStart), 517 }; 518 519 static const EnumEntry<uint8_t> FileChecksumKindNames[] = { 520 LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, None), 521 LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, MD5), 522 LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, SHA1), 523 LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, SHA256), 524 }; 525 526 template <typename T> 527 static std::error_code getSymbolAuxData(const COFFObjectFile *Obj, 528 COFFSymbolRef Symbol, 529 uint8_t AuxSymbolIdx, const T *&Aux) { 530 ArrayRef<uint8_t> AuxData = Obj->getSymbolAuxData(Symbol); 531 AuxData = AuxData.slice(AuxSymbolIdx * Obj->getSymbolTableEntrySize()); 532 Aux = reinterpret_cast<const T*>(AuxData.data()); 533 return readobj_error::success; 534 } 535 536 void COFFDumper::cacheRelocations() { 537 if (RelocCached) 538 return; 539 RelocCached = true; 540 541 for (const SectionRef &S : Obj->sections()) { 542 const coff_section *Section = Obj->getCOFFSection(S); 543 544 for (const RelocationRef &Reloc : S.relocations()) 545 RelocMap[Section].push_back(Reloc); 546 547 // Sort relocations by address. 548 std::sort(RelocMap[Section].begin(), RelocMap[Section].end(), 549 relocAddressLess); 550 } 551 } 552 553 void COFFDumper::printDataDirectory(uint32_t Index, const std::string &FieldName) { 554 const data_directory *Data; 555 if (Obj->getDataDirectory(Index, Data)) 556 return; 557 W.printHex(FieldName + "RVA", Data->RelativeVirtualAddress); 558 W.printHex(FieldName + "Size", Data->Size); 559 } 560 561 void COFFDumper::printFileHeaders() { 562 time_t TDS = Obj->getTimeDateStamp(); 563 char FormattedTime[20] = { }; 564 strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS)); 565 566 { 567 DictScope D(W, "ImageFileHeader"); 568 W.printEnum ("Machine", Obj->getMachine(), 569 makeArrayRef(ImageFileMachineType)); 570 W.printNumber("SectionCount", Obj->getNumberOfSections()); 571 W.printHex ("TimeDateStamp", FormattedTime, Obj->getTimeDateStamp()); 572 W.printHex ("PointerToSymbolTable", Obj->getPointerToSymbolTable()); 573 W.printNumber("SymbolCount", Obj->getNumberOfSymbols()); 574 W.printNumber("OptionalHeaderSize", Obj->getSizeOfOptionalHeader()); 575 W.printFlags ("Characteristics", Obj->getCharacteristics(), 576 makeArrayRef(ImageFileCharacteristics)); 577 } 578 579 // Print PE header. This header does not exist if this is an object file and 580 // not an executable. 581 const pe32_header *PEHeader = nullptr; 582 error(Obj->getPE32Header(PEHeader)); 583 if (PEHeader) 584 printPEHeader<pe32_header>(PEHeader); 585 586 const pe32plus_header *PEPlusHeader = nullptr; 587 error(Obj->getPE32PlusHeader(PEPlusHeader)); 588 if (PEPlusHeader) 589 printPEHeader<pe32plus_header>(PEPlusHeader); 590 591 if (const dos_header *DH = Obj->getDOSHeader()) 592 printDOSHeader(DH); 593 } 594 595 void COFFDumper::printDOSHeader(const dos_header *DH) { 596 DictScope D(W, "DOSHeader"); 597 W.printString("Magic", StringRef(DH->Magic, sizeof(DH->Magic))); 598 W.printNumber("UsedBytesInTheLastPage", DH->UsedBytesInTheLastPage); 599 W.printNumber("FileSizeInPages", DH->FileSizeInPages); 600 W.printNumber("NumberOfRelocationItems", DH->NumberOfRelocationItems); 601 W.printNumber("HeaderSizeInParagraphs", DH->HeaderSizeInParagraphs); 602 W.printNumber("MinimumExtraParagraphs", DH->MinimumExtraParagraphs); 603 W.printNumber("MaximumExtraParagraphs", DH->MaximumExtraParagraphs); 604 W.printNumber("InitialRelativeSS", DH->InitialRelativeSS); 605 W.printNumber("InitialSP", DH->InitialSP); 606 W.printNumber("Checksum", DH->Checksum); 607 W.printNumber("InitialIP", DH->InitialIP); 608 W.printNumber("InitialRelativeCS", DH->InitialRelativeCS); 609 W.printNumber("AddressOfRelocationTable", DH->AddressOfRelocationTable); 610 W.printNumber("OverlayNumber", DH->OverlayNumber); 611 W.printNumber("OEMid", DH->OEMid); 612 W.printNumber("OEMinfo", DH->OEMinfo); 613 W.printNumber("AddressOfNewExeHeader", DH->AddressOfNewExeHeader); 614 } 615 616 template <class PEHeader> 617 void COFFDumper::printPEHeader(const PEHeader *Hdr) { 618 DictScope D(W, "ImageOptionalHeader"); 619 W.printNumber("MajorLinkerVersion", Hdr->MajorLinkerVersion); 620 W.printNumber("MinorLinkerVersion", Hdr->MinorLinkerVersion); 621 W.printNumber("SizeOfCode", Hdr->SizeOfCode); 622 W.printNumber("SizeOfInitializedData", Hdr->SizeOfInitializedData); 623 W.printNumber("SizeOfUninitializedData", Hdr->SizeOfUninitializedData); 624 W.printHex ("AddressOfEntryPoint", Hdr->AddressOfEntryPoint); 625 W.printHex ("BaseOfCode", Hdr->BaseOfCode); 626 printBaseOfDataField(Hdr); 627 W.printHex ("ImageBase", Hdr->ImageBase); 628 W.printNumber("SectionAlignment", Hdr->SectionAlignment); 629 W.printNumber("FileAlignment", Hdr->FileAlignment); 630 W.printNumber("MajorOperatingSystemVersion", 631 Hdr->MajorOperatingSystemVersion); 632 W.printNumber("MinorOperatingSystemVersion", 633 Hdr->MinorOperatingSystemVersion); 634 W.printNumber("MajorImageVersion", Hdr->MajorImageVersion); 635 W.printNumber("MinorImageVersion", Hdr->MinorImageVersion); 636 W.printNumber("MajorSubsystemVersion", Hdr->MajorSubsystemVersion); 637 W.printNumber("MinorSubsystemVersion", Hdr->MinorSubsystemVersion); 638 W.printNumber("SizeOfImage", Hdr->SizeOfImage); 639 W.printNumber("SizeOfHeaders", Hdr->SizeOfHeaders); 640 W.printEnum ("Subsystem", Hdr->Subsystem, makeArrayRef(PEWindowsSubsystem)); 641 W.printFlags ("Characteristics", Hdr->DLLCharacteristics, 642 makeArrayRef(PEDLLCharacteristics)); 643 W.printNumber("SizeOfStackReserve", Hdr->SizeOfStackReserve); 644 W.printNumber("SizeOfStackCommit", Hdr->SizeOfStackCommit); 645 W.printNumber("SizeOfHeapReserve", Hdr->SizeOfHeapReserve); 646 W.printNumber("SizeOfHeapCommit", Hdr->SizeOfHeapCommit); 647 W.printNumber("NumberOfRvaAndSize", Hdr->NumberOfRvaAndSize); 648 649 if (Hdr->NumberOfRvaAndSize > 0) { 650 DictScope D(W, "DataDirectory"); 651 static const char * const directory[] = { 652 "ExportTable", "ImportTable", "ResourceTable", "ExceptionTable", 653 "CertificateTable", "BaseRelocationTable", "Debug", "Architecture", 654 "GlobalPtr", "TLSTable", "LoadConfigTable", "BoundImport", "IAT", 655 "DelayImportDescriptor", "CLRRuntimeHeader", "Reserved" 656 }; 657 658 for (uint32_t i = 0; i < Hdr->NumberOfRvaAndSize; ++i) 659 printDataDirectory(i, directory[i]); 660 } 661 } 662 663 void COFFDumper::printCOFFDebugDirectory() { 664 ListScope LS(W, "DebugDirectory"); 665 for (const debug_directory &D : Obj->debug_directories()) { 666 char FormattedTime[20] = {}; 667 time_t TDS = D.TimeDateStamp; 668 strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS)); 669 DictScope S(W, "DebugEntry"); 670 W.printHex("Characteristics", D.Characteristics); 671 W.printHex("TimeDateStamp", FormattedTime, D.TimeDateStamp); 672 W.printHex("MajorVersion", D.MajorVersion); 673 W.printHex("MinorVersion", D.MinorVersion); 674 W.printEnum("Type", D.Type, makeArrayRef(ImageDebugType)); 675 W.printHex("SizeOfData", D.SizeOfData); 676 W.printHex("AddressOfRawData", D.AddressOfRawData); 677 W.printHex("PointerToRawData", D.PointerToRawData); 678 if (D.Type == COFF::IMAGE_DEBUG_TYPE_CODEVIEW) { 679 const codeview::DebugInfo *DebugInfo; 680 StringRef PDBFileName; 681 error(Obj->getDebugPDBInfo(&D, DebugInfo, PDBFileName)); 682 DictScope PDBScope(W, "PDBInfo"); 683 W.printHex("PDBSignature", DebugInfo->Signature.CVSignature); 684 if (DebugInfo->Signature.CVSignature == OMF::Signature::PDB70) { 685 W.printBinary("PDBGUID", makeArrayRef(DebugInfo->PDB70.Signature)); 686 W.printNumber("PDBAge", DebugInfo->PDB70.Age); 687 W.printString("PDBFileName", PDBFileName); 688 } 689 } else { 690 // FIXME: Type values of 12 and 13 are commonly observed but are not in 691 // the documented type enum. Figure out what they mean. 692 ArrayRef<uint8_t> RawData; 693 error( 694 Obj->getRvaAndSizeAsBytes(D.AddressOfRawData, D.SizeOfData, RawData)); 695 W.printBinaryBlock("RawData", RawData); 696 } 697 } 698 } 699 700 void COFFDumper::printBaseOfDataField(const pe32_header *Hdr) { 701 W.printHex("BaseOfData", Hdr->BaseOfData); 702 } 703 704 void COFFDumper::printBaseOfDataField(const pe32plus_header *) {} 705 706 void COFFDumper::printCodeViewDebugInfo() { 707 // Print types first to build CVUDTNames, then print symbols. 708 for (const SectionRef &S : Obj->sections()) { 709 StringRef SectionName; 710 error(S.getName(SectionName)); 711 if (SectionName == ".debug$T") 712 printCodeViewTypeSection(SectionName, S); 713 } 714 for (const SectionRef &S : Obj->sections()) { 715 StringRef SectionName; 716 error(S.getName(SectionName)); 717 if (SectionName == ".debug$S") 718 printCodeViewSymbolSection(SectionName, S); 719 } 720 } 721 722 void COFFDumper::initializeFileAndStringTables(StringRef Data) { 723 while (!Data.empty() && (CVFileChecksumTable.data() == nullptr || 724 CVStringTable.data() == nullptr)) { 725 // The section consists of a number of subsection in the following format: 726 // |SubSectionType|SubSectionSize|Contents...| 727 uint32_t SubType, SubSectionSize; 728 error(consume(Data, SubType)); 729 error(consume(Data, SubSectionSize)); 730 if (SubSectionSize > Data.size()) 731 return error(object_error::parse_failed); 732 switch (ModuleSubstreamKind(SubType)) { 733 case ModuleSubstreamKind::FileChecksums: 734 CVFileChecksumTable = Data.substr(0, SubSectionSize); 735 break; 736 case ModuleSubstreamKind::StringTable: 737 CVStringTable = Data.substr(0, SubSectionSize); 738 break; 739 default: 740 break; 741 } 742 uint32_t PaddedSize = alignTo(SubSectionSize, 4); 743 if (PaddedSize > Data.size()) 744 error(object_error::parse_failed); 745 Data = Data.drop_front(PaddedSize); 746 } 747 } 748 749 void COFFDumper::printCodeViewSymbolSection(StringRef SectionName, 750 const SectionRef &Section) { 751 StringRef SectionContents; 752 error(Section.getContents(SectionContents)); 753 StringRef Data = SectionContents; 754 755 SmallVector<StringRef, 10> FunctionNames; 756 StringMap<StringRef> FunctionLineTables; 757 758 ListScope D(W, "CodeViewDebugInfo"); 759 // Print the section to allow correlation with printSections. 760 W.printNumber("Section", SectionName, Obj->getSectionID(Section)); 761 762 uint32_t Magic; 763 error(consume(Data, Magic)); 764 W.printHex("Magic", Magic); 765 if (Magic != COFF::DEBUG_SECTION_MAGIC) 766 return error(object_error::parse_failed); 767 768 initializeFileAndStringTables(Data); 769 770 // TODO: Convert this over to using ModuleSubstreamVisitor. 771 while (!Data.empty()) { 772 // The section consists of a number of subsection in the following format: 773 // |SubSectionType|SubSectionSize|Contents...| 774 uint32_t SubType, SubSectionSize; 775 error(consume(Data, SubType)); 776 error(consume(Data, SubSectionSize)); 777 778 ListScope S(W, "Subsection"); 779 W.printEnum("SubSectionType", SubType, makeArrayRef(SubSectionTypes)); 780 W.printHex("SubSectionSize", SubSectionSize); 781 782 // Get the contents of the subsection. 783 if (SubSectionSize > Data.size()) 784 return error(object_error::parse_failed); 785 StringRef Contents = Data.substr(0, SubSectionSize); 786 787 // Add SubSectionSize to the current offset and align that offset to find 788 // the next subsection. 789 size_t SectionOffset = Data.data() - SectionContents.data(); 790 size_t NextOffset = SectionOffset + SubSectionSize; 791 NextOffset = alignTo(NextOffset, 4); 792 if (NextOffset > SectionContents.size()) 793 return error(object_error::parse_failed); 794 Data = SectionContents.drop_front(NextOffset); 795 796 // Optionally print the subsection bytes in case our parsing gets confused 797 // later. 798 if (opts::CodeViewSubsectionBytes) 799 printBinaryBlockWithRelocs("SubSectionContents", Section, SectionContents, 800 Contents); 801 802 switch (ModuleSubstreamKind(SubType)) { 803 case ModuleSubstreamKind::Symbols: 804 printCodeViewSymbolsSubsection(Contents, Section, SectionContents); 805 break; 806 807 case ModuleSubstreamKind::InlineeLines: 808 printCodeViewInlineeLines(Contents); 809 break; 810 811 case ModuleSubstreamKind::FileChecksums: 812 printCodeViewFileChecksums(Contents); 813 break; 814 815 case ModuleSubstreamKind::Lines: { 816 // Holds a PC to file:line table. Some data to parse this subsection is 817 // stored in the other subsections, so just check sanity and store the 818 // pointers for deferred processing. 819 820 if (SubSectionSize < 12) { 821 // There should be at least three words to store two function 822 // relocations and size of the code. 823 error(object_error::parse_failed); 824 return; 825 } 826 827 StringRef LinkageName; 828 error(resolveSymbolName(Obj->getCOFFSection(Section), SectionOffset, 829 LinkageName)); 830 W.printString("LinkageName", LinkageName); 831 if (FunctionLineTables.count(LinkageName) != 0) { 832 // Saw debug info for this function already? 833 error(object_error::parse_failed); 834 return; 835 } 836 837 FunctionLineTables[LinkageName] = Contents; 838 FunctionNames.push_back(LinkageName); 839 break; 840 } 841 case ModuleSubstreamKind::FrameData: { 842 // First four bytes is a relocation against the function. 843 msf::ByteStream S(Contents); 844 msf::StreamReader SR(S); 845 const uint32_t *CodePtr; 846 error(SR.readObject(CodePtr)); 847 StringRef LinkageName; 848 error(resolveSymbolName(Obj->getCOFFSection(Section), SectionContents, 849 CodePtr, LinkageName)); 850 W.printString("LinkageName", LinkageName); 851 852 // To find the active frame description, search this array for the 853 // smallest PC range that includes the current PC. 854 while (!SR.empty()) { 855 const FrameData *FD; 856 error(SR.readObject(FD)); 857 858 if (FD->FrameFunc >= CVStringTable.size()) 859 error(object_error::parse_failed); 860 861 StringRef FrameFunc = 862 CVStringTable.drop_front(FD->FrameFunc).split('\0').first; 863 864 DictScope S(W, "FrameData"); 865 W.printHex("RvaStart", FD->RvaStart); 866 W.printHex("CodeSize", FD->CodeSize); 867 W.printHex("LocalSize", FD->LocalSize); 868 W.printHex("ParamsSize", FD->ParamsSize); 869 W.printHex("MaxStackSize", FD->MaxStackSize); 870 W.printString("FrameFunc", FrameFunc); 871 W.printHex("PrologSize", FD->PrologSize); 872 W.printHex("SavedRegsSize", FD->SavedRegsSize); 873 W.printFlags("Flags", FD->Flags, makeArrayRef(FrameDataFlags)); 874 } 875 break; 876 } 877 878 // Do nothing for unrecognized subsections. 879 default: 880 break; 881 } 882 W.flush(); 883 } 884 885 // Dump the line tables now that we've read all the subsections and know all 886 // the required information. 887 for (unsigned I = 0, E = FunctionNames.size(); I != E; ++I) { 888 StringRef Name = FunctionNames[I]; 889 ListScope S(W, "FunctionLineTable"); 890 W.printString("LinkageName", Name); 891 892 DataExtractor DE(FunctionLineTables[Name], true, 4); 893 uint32_t Offset = 6; // Skip relocations. 894 uint16_t Flags = DE.getU16(&Offset); 895 W.printHex("Flags", Flags); 896 bool HasColumnInformation = Flags & codeview::LineFlags::HaveColumns; 897 uint32_t FunctionSize = DE.getU32(&Offset); 898 W.printHex("CodeSize", FunctionSize); 899 while (DE.isValidOffset(Offset)) { 900 // For each range of lines with the same filename, we have a segment 901 // in the line table. The filename string is accessed using double 902 // indirection to the string table subsection using the index subsection. 903 uint32_t OffsetInIndex = DE.getU32(&Offset), 904 NumLines = DE.getU32(&Offset), 905 FullSegmentSize = DE.getU32(&Offset); 906 907 uint32_t ColumnOffset = Offset + 8 * NumLines; 908 DataExtractor ColumnDE(DE.getData(), true, 4); 909 910 if (FullSegmentSize != 911 12 + 8 * NumLines + (HasColumnInformation ? 4 * NumLines : 0)) { 912 error(object_error::parse_failed); 913 return; 914 } 915 916 ListScope S(W, "FilenameSegment"); 917 printFileNameForOffset("Filename", OffsetInIndex); 918 for (unsigned LineIdx = 0; 919 LineIdx != NumLines && DE.isValidOffset(Offset); ++LineIdx) { 920 // Then go the (PC, LineNumber) pairs. The line number is stored in the 921 // least significant 31 bits of the respective word in the table. 922 uint32_t PC = DE.getU32(&Offset), LineData = DE.getU32(&Offset); 923 if (PC >= FunctionSize) { 924 error(object_error::parse_failed); 925 return; 926 } 927 char Buffer[32]; 928 format("+0x%X", PC).snprint(Buffer, 32); 929 ListScope PCScope(W, Buffer); 930 LineInfo LI(LineData); 931 if (LI.isAlwaysStepInto()) 932 W.printString("StepInto", StringRef("Always")); 933 else if (LI.isNeverStepInto()) 934 W.printString("StepInto", StringRef("Never")); 935 else 936 W.printNumber("LineNumberStart", LI.getStartLine()); 937 W.printNumber("LineNumberEndDelta", LI.getLineDelta()); 938 W.printBoolean("IsStatement", LI.isStatement()); 939 if (HasColumnInformation && 940 ColumnDE.isValidOffsetForDataOfSize(ColumnOffset, 4)) { 941 uint16_t ColStart = ColumnDE.getU16(&ColumnOffset); 942 W.printNumber("ColStart", ColStart); 943 uint16_t ColEnd = ColumnDE.getU16(&ColumnOffset); 944 W.printNumber("ColEnd", ColEnd); 945 } 946 } 947 // Skip over the column data. 948 if (HasColumnInformation) { 949 for (unsigned LineIdx = 0; 950 LineIdx != NumLines && DE.isValidOffset(Offset); ++LineIdx) { 951 DE.getU32(&Offset); 952 } 953 } 954 } 955 } 956 } 957 958 void COFFDumper::printCodeViewSymbolsSubsection(StringRef Subsection, 959 const SectionRef &Section, 960 StringRef SectionContents) { 961 ArrayRef<uint8_t> BinaryData(Subsection.bytes_begin(), 962 Subsection.bytes_end()); 963 auto CODD = llvm::make_unique<COFFObjectDumpDelegate>(*this, Section, Obj, 964 SectionContents); 965 966 CVSymbolDumper CVSD(W, TypeDB, std::move(CODD), 967 opts::CodeViewSubsectionBytes); 968 ByteStream Stream(BinaryData); 969 CVSymbolArray Symbols; 970 StreamReader Reader(Stream); 971 if (auto EC = Reader.readArray(Symbols, Reader.getLength())) { 972 consumeError(std::move(EC)); 973 W.flush(); 974 error(object_error::parse_failed); 975 } 976 977 if (auto EC = CVSD.dump(Symbols)) { 978 W.flush(); 979 error(std::move(EC)); 980 } 981 W.flush(); 982 } 983 984 void COFFDumper::printCodeViewFileChecksums(StringRef Subsection) { 985 msf::ByteStream S(Subsection); 986 msf::StreamReader SR(S); 987 while (!SR.empty()) { 988 DictScope S(W, "FileChecksum"); 989 const FileChecksum *FC; 990 error(SR.readObject(FC)); 991 if (FC->FileNameOffset >= CVStringTable.size()) 992 error(object_error::parse_failed); 993 StringRef Filename = 994 CVStringTable.drop_front(FC->FileNameOffset).split('\0').first; 995 W.printHex("Filename", Filename, FC->FileNameOffset); 996 W.printHex("ChecksumSize", FC->ChecksumSize); 997 W.printEnum("ChecksumKind", uint8_t(FC->ChecksumKind), 998 makeArrayRef(FileChecksumKindNames)); 999 if (FC->ChecksumSize >= SR.bytesRemaining()) 1000 error(object_error::parse_failed); 1001 ArrayRef<uint8_t> ChecksumBytes; 1002 error(SR.readBytes(ChecksumBytes, FC->ChecksumSize)); 1003 W.printBinary("ChecksumBytes", ChecksumBytes); 1004 unsigned PaddedSize = alignTo(FC->ChecksumSize + sizeof(FileChecksum), 4) - 1005 sizeof(FileChecksum); 1006 PaddedSize -= ChecksumBytes.size(); 1007 if (PaddedSize > SR.bytesRemaining()) 1008 error(object_error::parse_failed); 1009 error(SR.skip(PaddedSize)); 1010 } 1011 } 1012 1013 void COFFDumper::printCodeViewInlineeLines(StringRef Subsection) { 1014 msf::ByteStream S(Subsection); 1015 msf::StreamReader SR(S); 1016 uint32_t Signature; 1017 error(SR.readInteger(Signature)); 1018 bool HasExtraFiles = Signature == unsigned(InlineeLinesSignature::ExtraFiles); 1019 1020 while (!SR.empty()) { 1021 const InlineeSourceLine *ISL; 1022 error(SR.readObject(ISL)); 1023 DictScope S(W, "InlineeSourceLine"); 1024 printTypeIndex("Inlinee", ISL->Inlinee); 1025 printFileNameForOffset("FileID", ISL->FileID); 1026 W.printNumber("SourceLineNum", ISL->SourceLineNum); 1027 1028 if (HasExtraFiles) { 1029 uint32_t ExtraFileCount; 1030 error(SR.readInteger(ExtraFileCount)); 1031 W.printNumber("ExtraFileCount", ExtraFileCount); 1032 ListScope ExtraFiles(W, "ExtraFiles"); 1033 for (unsigned I = 0; I < ExtraFileCount; ++I) { 1034 uint32_t FileID; 1035 error(SR.readInteger(FileID)); 1036 printFileNameForOffset("FileID", FileID); 1037 } 1038 } 1039 } 1040 } 1041 1042 StringRef COFFDumper::getFileNameForFileOffset(uint32_t FileOffset) { 1043 // The file checksum subsection should precede all references to it. 1044 if (!CVFileChecksumTable.data() || !CVStringTable.data()) 1045 error(object_error::parse_failed); 1046 // Check if the file checksum table offset is valid. 1047 if (FileOffset >= CVFileChecksumTable.size()) 1048 error(object_error::parse_failed); 1049 1050 // The string table offset comes first before the file checksum. 1051 StringRef Data = CVFileChecksumTable.drop_front(FileOffset); 1052 uint32_t StringOffset; 1053 error(consume(Data, StringOffset)); 1054 1055 // Check if the string table offset is valid. 1056 if (StringOffset >= CVStringTable.size()) 1057 error(object_error::parse_failed); 1058 1059 // Return the null-terminated string. 1060 return CVStringTable.drop_front(StringOffset).split('\0').first; 1061 } 1062 1063 void COFFDumper::printFileNameForOffset(StringRef Label, uint32_t FileOffset) { 1064 W.printHex(Label, getFileNameForFileOffset(FileOffset), FileOffset); 1065 } 1066 1067 void COFFDumper::mergeCodeViewTypes(TypeTableBuilder &CVTypes) { 1068 for (const SectionRef &S : Obj->sections()) { 1069 StringRef SectionName; 1070 error(S.getName(SectionName)); 1071 if (SectionName == ".debug$T") { 1072 StringRef Data; 1073 error(S.getContents(Data)); 1074 uint32_t Magic; 1075 error(consume(Data, Magic)); 1076 if (Magic != 4) 1077 error(object_error::parse_failed); 1078 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(Data.data()), 1079 Data.size()); 1080 ByteStream Stream(Bytes); 1081 CVTypeArray Types; 1082 StreamReader Reader(Stream); 1083 if (auto EC = Reader.readArray(Types, Reader.getLength())) { 1084 consumeError(std::move(EC)); 1085 W.flush(); 1086 error(object_error::parse_failed); 1087 } 1088 1089 if (!mergeTypeStreams(CVTypes, Types)) 1090 return error(object_error::parse_failed); 1091 } 1092 } 1093 } 1094 1095 void COFFDumper::printCodeViewTypeSection(StringRef SectionName, 1096 const SectionRef &Section) { 1097 ListScope D(W, "CodeViewTypes"); 1098 W.printNumber("Section", SectionName, Obj->getSectionID(Section)); 1099 1100 StringRef Data; 1101 error(Section.getContents(Data)); 1102 if (opts::CodeViewSubsectionBytes) 1103 W.printBinaryBlock("Data", Data); 1104 1105 uint32_t Magic; 1106 error(consume(Data, Magic)); 1107 W.printHex("Magic", Magic); 1108 if (Magic != COFF::DEBUG_SECTION_MAGIC) 1109 return error(object_error::parse_failed); 1110 1111 CVTypeDumper CVTD(TypeDB); 1112 TypeDumpVisitor TDV(TypeDB, &W, opts::CodeViewSubsectionBytes); 1113 if (auto EC = CVTD.dump({Data.bytes_begin(), Data.bytes_end()}, TDV)) { 1114 W.flush(); 1115 error(llvm::errorToErrorCode(std::move(EC))); 1116 } 1117 } 1118 1119 void COFFDumper::printSections() { 1120 ListScope SectionsD(W, "Sections"); 1121 int SectionNumber = 0; 1122 for (const SectionRef &Sec : Obj->sections()) { 1123 ++SectionNumber; 1124 const coff_section *Section = Obj->getCOFFSection(Sec); 1125 1126 StringRef Name; 1127 error(Sec.getName(Name)); 1128 1129 DictScope D(W, "Section"); 1130 W.printNumber("Number", SectionNumber); 1131 W.printBinary("Name", Name, Section->Name); 1132 W.printHex ("VirtualSize", Section->VirtualSize); 1133 W.printHex ("VirtualAddress", Section->VirtualAddress); 1134 W.printNumber("RawDataSize", Section->SizeOfRawData); 1135 W.printHex ("PointerToRawData", Section->PointerToRawData); 1136 W.printHex ("PointerToRelocations", Section->PointerToRelocations); 1137 W.printHex ("PointerToLineNumbers", Section->PointerToLinenumbers); 1138 W.printNumber("RelocationCount", Section->NumberOfRelocations); 1139 W.printNumber("LineNumberCount", Section->NumberOfLinenumbers); 1140 W.printFlags ("Characteristics", Section->Characteristics, 1141 makeArrayRef(ImageSectionCharacteristics), 1142 COFF::SectionCharacteristics(0x00F00000)); 1143 1144 if (opts::SectionRelocations) { 1145 ListScope D(W, "Relocations"); 1146 for (const RelocationRef &Reloc : Sec.relocations()) 1147 printRelocation(Sec, Reloc); 1148 } 1149 1150 if (opts::SectionSymbols) { 1151 ListScope D(W, "Symbols"); 1152 for (const SymbolRef &Symbol : Obj->symbols()) { 1153 if (!Sec.containsSymbol(Symbol)) 1154 continue; 1155 1156 printSymbol(Symbol); 1157 } 1158 } 1159 1160 if (opts::SectionData && 1161 !(Section->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)) { 1162 StringRef Data; 1163 error(Sec.getContents(Data)); 1164 1165 W.printBinaryBlock("SectionData", Data); 1166 } 1167 } 1168 } 1169 1170 void COFFDumper::printRelocations() { 1171 ListScope D(W, "Relocations"); 1172 1173 int SectionNumber = 0; 1174 for (const SectionRef &Section : Obj->sections()) { 1175 ++SectionNumber; 1176 StringRef Name; 1177 error(Section.getName(Name)); 1178 1179 bool PrintedGroup = false; 1180 for (const RelocationRef &Reloc : Section.relocations()) { 1181 if (!PrintedGroup) { 1182 W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n"; 1183 W.indent(); 1184 PrintedGroup = true; 1185 } 1186 1187 printRelocation(Section, Reloc); 1188 } 1189 1190 if (PrintedGroup) { 1191 W.unindent(); 1192 W.startLine() << "}\n"; 1193 } 1194 } 1195 } 1196 1197 void COFFDumper::printRelocation(const SectionRef &Section, 1198 const RelocationRef &Reloc, uint64_t Bias) { 1199 uint64_t Offset = Reloc.getOffset() - Bias; 1200 uint64_t RelocType = Reloc.getType(); 1201 SmallString<32> RelocName; 1202 StringRef SymbolName; 1203 Reloc.getTypeName(RelocName); 1204 symbol_iterator Symbol = Reloc.getSymbol(); 1205 if (Symbol != Obj->symbol_end()) { 1206 Expected<StringRef> SymbolNameOrErr = Symbol->getName(); 1207 error(errorToErrorCode(SymbolNameOrErr.takeError())); 1208 SymbolName = *SymbolNameOrErr; 1209 } 1210 1211 if (opts::ExpandRelocs) { 1212 DictScope Group(W, "Relocation"); 1213 W.printHex("Offset", Offset); 1214 W.printNumber("Type", RelocName, RelocType); 1215 W.printString("Symbol", SymbolName.empty() ? "-" : SymbolName); 1216 } else { 1217 raw_ostream& OS = W.startLine(); 1218 OS << W.hex(Offset) 1219 << " " << RelocName 1220 << " " << (SymbolName.empty() ? "-" : SymbolName) 1221 << "\n"; 1222 } 1223 } 1224 1225 void COFFDumper::printSymbols() { 1226 ListScope Group(W, "Symbols"); 1227 1228 for (const SymbolRef &Symbol : Obj->symbols()) 1229 printSymbol(Symbol); 1230 } 1231 1232 void COFFDumper::printDynamicSymbols() { ListScope Group(W, "DynamicSymbols"); } 1233 1234 static ErrorOr<StringRef> 1235 getSectionName(const llvm::object::COFFObjectFile *Obj, int32_t SectionNumber, 1236 const coff_section *Section) { 1237 if (Section) { 1238 StringRef SectionName; 1239 if (std::error_code EC = Obj->getSectionName(Section, SectionName)) 1240 return EC; 1241 return SectionName; 1242 } 1243 if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG) 1244 return StringRef("IMAGE_SYM_DEBUG"); 1245 if (SectionNumber == llvm::COFF::IMAGE_SYM_ABSOLUTE) 1246 return StringRef("IMAGE_SYM_ABSOLUTE"); 1247 if (SectionNumber == llvm::COFF::IMAGE_SYM_UNDEFINED) 1248 return StringRef("IMAGE_SYM_UNDEFINED"); 1249 return StringRef(""); 1250 } 1251 1252 void COFFDumper::printSymbol(const SymbolRef &Sym) { 1253 DictScope D(W, "Symbol"); 1254 1255 COFFSymbolRef Symbol = Obj->getCOFFSymbol(Sym); 1256 const coff_section *Section; 1257 if (std::error_code EC = Obj->getSection(Symbol.getSectionNumber(), Section)) { 1258 W.startLine() << "Invalid section number: " << EC.message() << "\n"; 1259 W.flush(); 1260 return; 1261 } 1262 1263 StringRef SymbolName; 1264 if (Obj->getSymbolName(Symbol, SymbolName)) 1265 SymbolName = ""; 1266 1267 StringRef SectionName = ""; 1268 ErrorOr<StringRef> Res = 1269 getSectionName(Obj, Symbol.getSectionNumber(), Section); 1270 if (Res) 1271 SectionName = *Res; 1272 1273 W.printString("Name", SymbolName); 1274 W.printNumber("Value", Symbol.getValue()); 1275 W.printNumber("Section", SectionName, Symbol.getSectionNumber()); 1276 W.printEnum ("BaseType", Symbol.getBaseType(), makeArrayRef(ImageSymType)); 1277 W.printEnum ("ComplexType", Symbol.getComplexType(), 1278 makeArrayRef(ImageSymDType)); 1279 W.printEnum ("StorageClass", Symbol.getStorageClass(), 1280 makeArrayRef(ImageSymClass)); 1281 W.printNumber("AuxSymbolCount", Symbol.getNumberOfAuxSymbols()); 1282 1283 for (uint8_t I = 0; I < Symbol.getNumberOfAuxSymbols(); ++I) { 1284 if (Symbol.isFunctionDefinition()) { 1285 const coff_aux_function_definition *Aux; 1286 error(getSymbolAuxData(Obj, Symbol, I, Aux)); 1287 1288 DictScope AS(W, "AuxFunctionDef"); 1289 W.printNumber("TagIndex", Aux->TagIndex); 1290 W.printNumber("TotalSize", Aux->TotalSize); 1291 W.printHex("PointerToLineNumber", Aux->PointerToLinenumber); 1292 W.printHex("PointerToNextFunction", Aux->PointerToNextFunction); 1293 1294 } else if (Symbol.isAnyUndefined()) { 1295 const coff_aux_weak_external *Aux; 1296 error(getSymbolAuxData(Obj, Symbol, I, Aux)); 1297 1298 ErrorOr<COFFSymbolRef> Linked = Obj->getSymbol(Aux->TagIndex); 1299 StringRef LinkedName; 1300 std::error_code EC = Linked.getError(); 1301 if (EC || (EC = Obj->getSymbolName(*Linked, LinkedName))) { 1302 LinkedName = ""; 1303 error(EC); 1304 } 1305 1306 DictScope AS(W, "AuxWeakExternal"); 1307 W.printNumber("Linked", LinkedName, Aux->TagIndex); 1308 W.printEnum ("Search", Aux->Characteristics, 1309 makeArrayRef(WeakExternalCharacteristics)); 1310 1311 } else if (Symbol.isFileRecord()) { 1312 const char *FileName; 1313 error(getSymbolAuxData(Obj, Symbol, I, FileName)); 1314 1315 DictScope AS(W, "AuxFileRecord"); 1316 1317 StringRef Name(FileName, Symbol.getNumberOfAuxSymbols() * 1318 Obj->getSymbolTableEntrySize()); 1319 W.printString("FileName", Name.rtrim(StringRef("\0", 1))); 1320 break; 1321 } else if (Symbol.isSectionDefinition()) { 1322 const coff_aux_section_definition *Aux; 1323 error(getSymbolAuxData(Obj, Symbol, I, Aux)); 1324 1325 int32_t AuxNumber = Aux->getNumber(Symbol.isBigObj()); 1326 1327 DictScope AS(W, "AuxSectionDef"); 1328 W.printNumber("Length", Aux->Length); 1329 W.printNumber("RelocationCount", Aux->NumberOfRelocations); 1330 W.printNumber("LineNumberCount", Aux->NumberOfLinenumbers); 1331 W.printHex("Checksum", Aux->CheckSum); 1332 W.printNumber("Number", AuxNumber); 1333 W.printEnum("Selection", Aux->Selection, makeArrayRef(ImageCOMDATSelect)); 1334 1335 if (Section && Section->Characteristics & COFF::IMAGE_SCN_LNK_COMDAT 1336 && Aux->Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) { 1337 const coff_section *Assoc; 1338 StringRef AssocName = ""; 1339 std::error_code EC = Obj->getSection(AuxNumber, Assoc); 1340 ErrorOr<StringRef> Res = getSectionName(Obj, AuxNumber, Assoc); 1341 if (Res) 1342 AssocName = *Res; 1343 if (!EC) 1344 EC = Res.getError(); 1345 if (EC) { 1346 AssocName = ""; 1347 error(EC); 1348 } 1349 1350 W.printNumber("AssocSection", AssocName, AuxNumber); 1351 } 1352 } else if (Symbol.isCLRToken()) { 1353 const coff_aux_clr_token *Aux; 1354 error(getSymbolAuxData(Obj, Symbol, I, Aux)); 1355 1356 ErrorOr<COFFSymbolRef> ReferredSym = 1357 Obj->getSymbol(Aux->SymbolTableIndex); 1358 StringRef ReferredName; 1359 std::error_code EC = ReferredSym.getError(); 1360 if (EC || (EC = Obj->getSymbolName(*ReferredSym, ReferredName))) { 1361 ReferredName = ""; 1362 error(EC); 1363 } 1364 1365 DictScope AS(W, "AuxCLRToken"); 1366 W.printNumber("AuxType", Aux->AuxType); 1367 W.printNumber("Reserved", Aux->Reserved); 1368 W.printNumber("SymbolTableIndex", ReferredName, Aux->SymbolTableIndex); 1369 1370 } else { 1371 W.startLine() << "<unhandled auxiliary record>\n"; 1372 } 1373 } 1374 } 1375 1376 void COFFDumper::printUnwindInfo() { 1377 ListScope D(W, "UnwindInformation"); 1378 switch (Obj->getMachine()) { 1379 case COFF::IMAGE_FILE_MACHINE_AMD64: { 1380 Win64EH::Dumper Dumper(W); 1381 Win64EH::Dumper::SymbolResolver 1382 Resolver = [](const object::coff_section *Section, uint64_t Offset, 1383 SymbolRef &Symbol, void *user_data) -> std::error_code { 1384 COFFDumper *Dumper = reinterpret_cast<COFFDumper *>(user_data); 1385 return Dumper->resolveSymbol(Section, Offset, Symbol); 1386 }; 1387 Win64EH::Dumper::Context Ctx(*Obj, Resolver, this); 1388 Dumper.printData(Ctx); 1389 break; 1390 } 1391 case COFF::IMAGE_FILE_MACHINE_ARMNT: { 1392 ARM::WinEH::Decoder Decoder(W); 1393 Decoder.dumpProcedureData(*Obj); 1394 break; 1395 } 1396 default: 1397 W.printEnum("unsupported Image Machine", Obj->getMachine(), 1398 makeArrayRef(ImageFileMachineType)); 1399 break; 1400 } 1401 } 1402 1403 void COFFDumper::printImportedSymbols( 1404 iterator_range<imported_symbol_iterator> Range) { 1405 for (const ImportedSymbolRef &I : Range) { 1406 StringRef Sym; 1407 error(I.getSymbolName(Sym)); 1408 uint16_t Ordinal; 1409 error(I.getOrdinal(Ordinal)); 1410 W.printNumber("Symbol", Sym, Ordinal); 1411 } 1412 } 1413 1414 void COFFDumper::printDelayImportedSymbols( 1415 const DelayImportDirectoryEntryRef &I, 1416 iterator_range<imported_symbol_iterator> Range) { 1417 int Index = 0; 1418 for (const ImportedSymbolRef &S : Range) { 1419 DictScope Import(W, "Import"); 1420 StringRef Sym; 1421 error(S.getSymbolName(Sym)); 1422 uint16_t Ordinal; 1423 error(S.getOrdinal(Ordinal)); 1424 W.printNumber("Symbol", Sym, Ordinal); 1425 uint64_t Addr; 1426 error(I.getImportAddress(Index++, Addr)); 1427 W.printHex("Address", Addr); 1428 } 1429 } 1430 1431 void COFFDumper::printCOFFImports() { 1432 // Regular imports 1433 for (const ImportDirectoryEntryRef &I : Obj->import_directories()) { 1434 DictScope Import(W, "Import"); 1435 StringRef Name; 1436 error(I.getName(Name)); 1437 W.printString("Name", Name); 1438 uint32_t Addr; 1439 error(I.getImportLookupTableRVA(Addr)); 1440 W.printHex("ImportLookupTableRVA", Addr); 1441 error(I.getImportAddressTableRVA(Addr)); 1442 W.printHex("ImportAddressTableRVA", Addr); 1443 printImportedSymbols(I.imported_symbols()); 1444 } 1445 1446 // Delay imports 1447 for (const DelayImportDirectoryEntryRef &I : Obj->delay_import_directories()) { 1448 DictScope Import(W, "DelayImport"); 1449 StringRef Name; 1450 error(I.getName(Name)); 1451 W.printString("Name", Name); 1452 const delay_import_directory_table_entry *Table; 1453 error(I.getDelayImportTable(Table)); 1454 W.printHex("Attributes", Table->Attributes); 1455 W.printHex("ModuleHandle", Table->ModuleHandle); 1456 W.printHex("ImportAddressTable", Table->DelayImportAddressTable); 1457 W.printHex("ImportNameTable", Table->DelayImportNameTable); 1458 W.printHex("BoundDelayImportTable", Table->BoundDelayImportTable); 1459 W.printHex("UnloadDelayImportTable", Table->UnloadDelayImportTable); 1460 printDelayImportedSymbols(I, I.imported_symbols()); 1461 } 1462 } 1463 1464 void COFFDumper::printCOFFExports() { 1465 for (const ExportDirectoryEntryRef &E : Obj->export_directories()) { 1466 DictScope Export(W, "Export"); 1467 1468 StringRef Name; 1469 uint32_t Ordinal, RVA; 1470 1471 error(E.getSymbolName(Name)); 1472 error(E.getOrdinal(Ordinal)); 1473 error(E.getExportRVA(RVA)); 1474 1475 W.printNumber("Ordinal", Ordinal); 1476 W.printString("Name", Name); 1477 W.printHex("RVA", RVA); 1478 } 1479 } 1480 1481 void COFFDumper::printCOFFDirectives() { 1482 for (const SectionRef &Section : Obj->sections()) { 1483 StringRef Contents; 1484 StringRef Name; 1485 1486 error(Section.getName(Name)); 1487 if (Name != ".drectve") 1488 continue; 1489 1490 error(Section.getContents(Contents)); 1491 1492 W.printString("Directive(s)", Contents); 1493 } 1494 } 1495 1496 static StringRef getBaseRelocTypeName(uint8_t Type) { 1497 switch (Type) { 1498 case COFF::IMAGE_REL_BASED_ABSOLUTE: return "ABSOLUTE"; 1499 case COFF::IMAGE_REL_BASED_HIGH: return "HIGH"; 1500 case COFF::IMAGE_REL_BASED_LOW: return "LOW"; 1501 case COFF::IMAGE_REL_BASED_HIGHLOW: return "HIGHLOW"; 1502 case COFF::IMAGE_REL_BASED_HIGHADJ: return "HIGHADJ"; 1503 case COFF::IMAGE_REL_BASED_ARM_MOV32T: return "ARM_MOV32(T)"; 1504 case COFF::IMAGE_REL_BASED_DIR64: return "DIR64"; 1505 default: return "unknown (" + llvm::utostr(Type) + ")"; 1506 } 1507 } 1508 1509 void COFFDumper::printCOFFBaseReloc() { 1510 ListScope D(W, "BaseReloc"); 1511 for (const BaseRelocRef &I : Obj->base_relocs()) { 1512 uint8_t Type; 1513 uint32_t RVA; 1514 error(I.getRVA(RVA)); 1515 error(I.getType(Type)); 1516 DictScope Import(W, "Entry"); 1517 W.printString("Type", getBaseRelocTypeName(Type)); 1518 W.printHex("Address", RVA); 1519 } 1520 } 1521 1522 void COFFDumper::printStackMap() const { 1523 object::SectionRef StackMapSection; 1524 for (auto Sec : Obj->sections()) { 1525 StringRef Name; 1526 Sec.getName(Name); 1527 if (Name == ".llvm_stackmaps") { 1528 StackMapSection = Sec; 1529 break; 1530 } 1531 } 1532 1533 if (StackMapSection == object::SectionRef()) 1534 return; 1535 1536 StringRef StackMapContents; 1537 StackMapSection.getContents(StackMapContents); 1538 ArrayRef<uint8_t> StackMapContentsArray( 1539 reinterpret_cast<const uint8_t*>(StackMapContents.data()), 1540 StackMapContents.size()); 1541 1542 if (Obj->isLittleEndian()) 1543 prettyPrintStackMap( 1544 llvm::outs(), 1545 StackMapV2Parser<support::little>(StackMapContentsArray)); 1546 else 1547 prettyPrintStackMap(llvm::outs(), 1548 StackMapV2Parser<support::big>(StackMapContentsArray)); 1549 } 1550 1551 void llvm::dumpCodeViewMergedTypes(ScopedPrinter &Writer, 1552 llvm::codeview::TypeTableBuilder &CVTypes) { 1553 // Flatten it first, then run our dumper on it. 1554 ListScope S(Writer, "MergedTypeStream"); 1555 SmallString<0> Buf; 1556 CVTypes.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Record) { 1557 Buf.append(Record.begin(), Record.end()); 1558 }); 1559 1560 TypeDatabase TypeDB; 1561 CVTypeDumper CVTD(TypeDB); 1562 TypeDumpVisitor TDV(TypeDB, &Writer, opts::CodeViewSubsectionBytes); 1563 if (auto EC = 1564 CVTD.dump({Buf.str().bytes_begin(), Buf.str().bytes_end()}, TDV)) { 1565 Writer.flush(); 1566 error(llvm::errorToErrorCode(std::move(EC))); 1567 } 1568 } 1569