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