Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 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 "llvm-readobj.h" |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 16 | #include "ARMWinEHPrinter.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 17 | #include "Error.h" |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 18 | #include "ObjDumper.h" |
Lang Hames | 0000afd | 2015-06-26 23:56:53 +0000 | [diff] [blame] | 19 | #include "StackMapPrinter.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 20 | #include "StreamWriter.h" |
Saleem Abdulrasool | e8839a7 | 2014-05-25 20:26:45 +0000 | [diff] [blame] | 21 | #include "Win64EHDumper.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/DenseMap.h" |
| 23 | #include "llvm/ADT/SmallString.h" |
Colin LeMahieu | 9fbffee | 2014-11-19 17:10:39 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 25 | #include "llvm/Object/COFF.h" |
| 26 | #include "llvm/Object/ObjectFile.h" |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 27 | #include "llvm/Support/COFF.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Casting.h" |
| 29 | #include "llvm/Support/Compiler.h" |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 30 | #include "llvm/Support/DataExtractor.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Format.h" |
| 32 | #include "llvm/Support/SourceMgr.h" |
| 33 | #include "llvm/Support/Win64EH.h" |
| 34 | #include "llvm/Support/raw_ostream.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 35 | #include <algorithm> |
| 36 | #include <cstring> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 37 | #include <system_error> |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 38 | #include <time.h> |
| 39 | |
| 40 | using namespace llvm; |
| 41 | using namespace llvm::object; |
| 42 | using namespace llvm::Win64EH; |
| 43 | |
| 44 | namespace { |
| 45 | |
| 46 | class COFFDumper : public ObjDumper { |
| 47 | public: |
| 48 | COFFDumper(const llvm::object::COFFObjectFile *Obj, StreamWriter& Writer) |
| 49 | : ObjDumper(Writer) |
| 50 | , Obj(Obj) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Craig Topper | fd38cbe | 2014-08-30 16:48:34 +0000 | [diff] [blame] | 53 | void printFileHeaders() override; |
| 54 | void printSections() override; |
| 55 | void printRelocations() override; |
| 56 | void printSymbols() override; |
| 57 | void printDynamicSymbols() override; |
| 58 | void printUnwindInfo() override; |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 59 | void printCOFFImports() override; |
Saleem Abdulrasool | ddd9264 | 2015-01-03 21:35:09 +0000 | [diff] [blame] | 60 | void printCOFFExports() override; |
Saleem Abdulrasool | f957863 | 2014-10-07 19:37:52 +0000 | [diff] [blame] | 61 | void printCOFFDirectives() override; |
Rui Ueyama | 74e8513 | 2014-11-19 00:18:07 +0000 | [diff] [blame] | 62 | void printCOFFBaseReloc() override; |
Lang Hames | 0000afd | 2015-06-26 23:56:53 +0000 | [diff] [blame] | 63 | void printStackMap() const override; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 64 | private: |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 65 | void printSymbol(const SymbolRef &Sym); |
| 66 | void printRelocation(const SectionRef &Section, const RelocationRef &Reloc); |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 67 | void printDataDirectory(uint32_t Index, const std::string &FieldName); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 68 | |
David Majnemer | 5026722 | 2014-11-05 06:24:35 +0000 | [diff] [blame] | 69 | void printDOSHeader(const dos_header *DH); |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 70 | template <class PEHeader> void printPEHeader(const PEHeader *Hdr); |
| 71 | void printBaseOfDataField(const pe32_header *Hdr); |
| 72 | void printBaseOfDataField(const pe32plus_header *Hdr); |
| 73 | |
Zachary Turner | 99f0215 | 2015-02-18 19:32:05 +0000 | [diff] [blame] | 74 | void printCodeViewDebugInfo(const SectionRef &Section); |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 75 | |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 76 | void printCodeViewSymbolsSubsection(StringRef Subsection, |
| 77 | const SectionRef &Section, |
| 78 | uint32_t Offset); |
| 79 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 80 | void cacheRelocations(); |
| 81 | |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 82 | std::error_code resolveSymbol(const coff_section *Section, uint64_t Offset, |
| 83 | SymbolRef &Sym); |
| 84 | std::error_code resolveSymbolName(const coff_section *Section, |
| 85 | uint64_t Offset, StringRef &Name); |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 86 | void printImportedSymbols(iterator_range<imported_symbol_iterator> Range); |
Rui Ueyama | ffa4ceb | 2014-11-13 03:22:54 +0000 | [diff] [blame] | 87 | void printDelayImportedSymbols( |
| 88 | const DelayImportDirectoryEntryRef &I, |
| 89 | iterator_range<imported_symbol_iterator> Range); |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 90 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 91 | typedef DenseMap<const coff_section*, std::vector<RelocationRef> > RelocMapTy; |
| 92 | |
| 93 | const llvm::object::COFFObjectFile *Obj; |
Rafael Espindola | 76d650e | 2015-07-06 14:26:07 +0000 | [diff] [blame] | 94 | bool RelocCached = false; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 95 | RelocMapTy RelocMap; |
Timur Iskhodzhanov | 1160333 | 2014-10-06 16:59:44 +0000 | [diff] [blame] | 96 | StringRef CVFileIndexToStringOffsetTable; |
| 97 | StringRef CVStringTable; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | } // namespace |
| 101 | |
| 102 | |
| 103 | namespace llvm { |
| 104 | |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 105 | std::error_code createCOFFDumper(const object::ObjectFile *Obj, |
| 106 | StreamWriter &Writer, |
| 107 | std::unique_ptr<ObjDumper> &Result) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 108 | const COFFObjectFile *COFFObj = dyn_cast<COFFObjectFile>(Obj); |
| 109 | if (!COFFObj) |
| 110 | return readobj_error::unsupported_obj_file_format; |
| 111 | |
| 112 | Result.reset(new COFFDumper(COFFObj, Writer)); |
| 113 | return readobj_error::success; |
| 114 | } |
| 115 | |
| 116 | } // namespace llvm |
| 117 | |
Saleem Abdulrasool | 5dd27f4 | 2014-05-25 20:26:37 +0000 | [diff] [blame] | 118 | // Given a a section and an offset into this section the function returns the |
| 119 | // symbol used for the relocation at the offset. |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 120 | std::error_code COFFDumper::resolveSymbol(const coff_section *Section, |
| 121 | uint64_t Offset, SymbolRef &Sym) { |
Rafael Espindola | 76d650e | 2015-07-06 14:26:07 +0000 | [diff] [blame] | 122 | cacheRelocations(); |
Saleem Abdulrasool | 5dd27f4 | 2014-05-25 20:26:37 +0000 | [diff] [blame] | 123 | const auto &Relocations = RelocMap[Section]; |
| 124 | for (const auto &Relocation : Relocations) { |
Rafael Espindola | 96d071c | 2015-06-29 23:29:12 +0000 | [diff] [blame] | 125 | uint64_t RelocationOffset = Relocation.getOffset(); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 126 | |
Saleem Abdulrasool | 5dd27f4 | 2014-05-25 20:26:37 +0000 | [diff] [blame] | 127 | if (RelocationOffset == Offset) { |
Saleem Abdulrasool | 4a6f583 | 2014-05-20 05:18:06 +0000 | [diff] [blame] | 128 | Sym = *Relocation.getSymbol(); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 129 | return readobj_error::success; |
| 130 | } |
| 131 | } |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 132 | return readobj_error::unknown_symbol; |
| 133 | } |
| 134 | |
Saleem Abdulrasool | 5dd27f4 | 2014-05-25 20:26:37 +0000 | [diff] [blame] | 135 | // Given a section and an offset into this section the function returns the name |
| 136 | // of the symbol used for the relocation at the offset. |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 137 | std::error_code COFFDumper::resolveSymbolName(const coff_section *Section, |
| 138 | uint64_t Offset, |
| 139 | StringRef &Name) { |
Saleem Abdulrasool | 5dd27f4 | 2014-05-25 20:26:37 +0000 | [diff] [blame] | 140 | SymbolRef Symbol; |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 141 | if (std::error_code EC = resolveSymbol(Section, Offset, Symbol)) |
Saleem Abdulrasool | 5dd27f4 | 2014-05-25 20:26:37 +0000 | [diff] [blame] | 142 | return EC; |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 143 | ErrorOr<StringRef> NameOrErr = Symbol.getName(); |
| 144 | if (std::error_code EC = NameOrErr.getError()) |
Saleem Abdulrasool | 5dd27f4 | 2014-05-25 20:26:37 +0000 | [diff] [blame] | 145 | return EC; |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 146 | Name = *NameOrErr; |
Rui Ueyama | 7d09919 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 147 | return std::error_code(); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static const EnumEntry<COFF::MachineTypes> ImageFileMachineType[] = { |
| 151 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_UNKNOWN ), |
| 152 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AM33 ), |
| 153 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AMD64 ), |
| 154 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARM ), |
Saleem Abdulrasool | 5e1780e | 2014-03-11 03:08:37 +0000 | [diff] [blame] | 155 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARMNT ), |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 156 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_EBC ), |
| 157 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_I386 ), |
| 158 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_IA64 ), |
| 159 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_M32R ), |
| 160 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPS16 ), |
| 161 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU ), |
| 162 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU16), |
| 163 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPC ), |
| 164 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPCFP), |
| 165 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_R4000 ), |
| 166 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3 ), |
| 167 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3DSP ), |
| 168 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH4 ), |
| 169 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH5 ), |
| 170 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_THUMB ), |
| 171 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_WCEMIPSV2) |
| 172 | }; |
| 173 | |
| 174 | static const EnumEntry<COFF::Characteristics> ImageFileCharacteristics[] = { |
| 175 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_RELOCS_STRIPPED ), |
| 176 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_EXECUTABLE_IMAGE ), |
| 177 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LINE_NUMS_STRIPPED ), |
| 178 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LOCAL_SYMS_STRIPPED ), |
| 179 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_AGGRESSIVE_WS_TRIM ), |
| 180 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LARGE_ADDRESS_AWARE ), |
| 181 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_LO ), |
| 182 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_32BIT_MACHINE ), |
| 183 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DEBUG_STRIPPED ), |
| 184 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP), |
| 185 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_NET_RUN_FROM_SWAP ), |
| 186 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_SYSTEM ), |
| 187 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DLL ), |
| 188 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_UP_SYSTEM_ONLY ), |
| 189 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_HI ) |
| 190 | }; |
| 191 | |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 192 | static const EnumEntry<COFF::WindowsSubsystem> PEWindowsSubsystem[] = { |
| 193 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_UNKNOWN ), |
| 194 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_NATIVE ), |
| 195 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_GUI ), |
| 196 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CUI ), |
| 197 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_POSIX_CUI ), |
| 198 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI ), |
| 199 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_APPLICATION ), |
| 200 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER), |
| 201 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER ), |
| 202 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_ROM ), |
| 203 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_XBOX ), |
| 204 | }; |
| 205 | |
| 206 | static const EnumEntry<COFF::DLLCharacteristics> PEDLLCharacteristics[] = { |
Rui Ueyama | 06dc5e7 | 2014-01-27 04:22:24 +0000 | [diff] [blame] | 207 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA ), |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 208 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE ), |
| 209 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY ), |
| 210 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NX_COMPAT ), |
| 211 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION ), |
| 212 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_SEH ), |
| 213 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_BIND ), |
Saleem Abdulrasool | d90f86d | 2014-06-27 03:11:18 +0000 | [diff] [blame] | 214 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_APPCONTAINER ), |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 215 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER ), |
Saleem Abdulrasool | d90f86d | 2014-06-27 03:11:18 +0000 | [diff] [blame] | 216 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_GUARD_CF ), |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 217 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE), |
| 218 | }; |
| 219 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 220 | static const EnumEntry<COFF::SectionCharacteristics> |
| 221 | ImageSectionCharacteristics[] = { |
| 222 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_TYPE_NO_PAD ), |
| 223 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_CODE ), |
| 224 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_INITIALIZED_DATA ), |
| 225 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_UNINITIALIZED_DATA), |
| 226 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_OTHER ), |
| 227 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_INFO ), |
| 228 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_REMOVE ), |
| 229 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_COMDAT ), |
| 230 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_GPREL ), |
| 231 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PURGEABLE ), |
| 232 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_16BIT ), |
| 233 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_LOCKED ), |
| 234 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PRELOAD ), |
| 235 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1BYTES ), |
| 236 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2BYTES ), |
| 237 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4BYTES ), |
| 238 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8BYTES ), |
| 239 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_16BYTES ), |
| 240 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_32BYTES ), |
| 241 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_64BYTES ), |
| 242 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_128BYTES ), |
| 243 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_256BYTES ), |
| 244 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_512BYTES ), |
| 245 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1024BYTES ), |
| 246 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2048BYTES ), |
| 247 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4096BYTES ), |
| 248 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8192BYTES ), |
| 249 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_NRELOC_OVFL ), |
| 250 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_DISCARDABLE ), |
| 251 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_CACHED ), |
| 252 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_PAGED ), |
| 253 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_SHARED ), |
| 254 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_EXECUTE ), |
| 255 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_READ ), |
| 256 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_WRITE ) |
| 257 | }; |
| 258 | |
| 259 | static const EnumEntry<COFF::SymbolBaseType> ImageSymType[] = { |
| 260 | { "Null" , COFF::IMAGE_SYM_TYPE_NULL }, |
| 261 | { "Void" , COFF::IMAGE_SYM_TYPE_VOID }, |
| 262 | { "Char" , COFF::IMAGE_SYM_TYPE_CHAR }, |
| 263 | { "Short" , COFF::IMAGE_SYM_TYPE_SHORT }, |
| 264 | { "Int" , COFF::IMAGE_SYM_TYPE_INT }, |
| 265 | { "Long" , COFF::IMAGE_SYM_TYPE_LONG }, |
| 266 | { "Float" , COFF::IMAGE_SYM_TYPE_FLOAT }, |
| 267 | { "Double", COFF::IMAGE_SYM_TYPE_DOUBLE }, |
| 268 | { "Struct", COFF::IMAGE_SYM_TYPE_STRUCT }, |
| 269 | { "Union" , COFF::IMAGE_SYM_TYPE_UNION }, |
| 270 | { "Enum" , COFF::IMAGE_SYM_TYPE_ENUM }, |
| 271 | { "MOE" , COFF::IMAGE_SYM_TYPE_MOE }, |
| 272 | { "Byte" , COFF::IMAGE_SYM_TYPE_BYTE }, |
| 273 | { "Word" , COFF::IMAGE_SYM_TYPE_WORD }, |
| 274 | { "UInt" , COFF::IMAGE_SYM_TYPE_UINT }, |
| 275 | { "DWord" , COFF::IMAGE_SYM_TYPE_DWORD } |
| 276 | }; |
| 277 | |
| 278 | static const EnumEntry<COFF::SymbolComplexType> ImageSymDType[] = { |
| 279 | { "Null" , COFF::IMAGE_SYM_DTYPE_NULL }, |
| 280 | { "Pointer" , COFF::IMAGE_SYM_DTYPE_POINTER }, |
| 281 | { "Function", COFF::IMAGE_SYM_DTYPE_FUNCTION }, |
| 282 | { "Array" , COFF::IMAGE_SYM_DTYPE_ARRAY } |
| 283 | }; |
| 284 | |
| 285 | static const EnumEntry<COFF::SymbolStorageClass> ImageSymClass[] = { |
| 286 | { "EndOfFunction" , COFF::IMAGE_SYM_CLASS_END_OF_FUNCTION }, |
| 287 | { "Null" , COFF::IMAGE_SYM_CLASS_NULL }, |
| 288 | { "Automatic" , COFF::IMAGE_SYM_CLASS_AUTOMATIC }, |
| 289 | { "External" , COFF::IMAGE_SYM_CLASS_EXTERNAL }, |
| 290 | { "Static" , COFF::IMAGE_SYM_CLASS_STATIC }, |
| 291 | { "Register" , COFF::IMAGE_SYM_CLASS_REGISTER }, |
| 292 | { "ExternalDef" , COFF::IMAGE_SYM_CLASS_EXTERNAL_DEF }, |
| 293 | { "Label" , COFF::IMAGE_SYM_CLASS_LABEL }, |
| 294 | { "UndefinedLabel" , COFF::IMAGE_SYM_CLASS_UNDEFINED_LABEL }, |
| 295 | { "MemberOfStruct" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_STRUCT }, |
| 296 | { "Argument" , COFF::IMAGE_SYM_CLASS_ARGUMENT }, |
| 297 | { "StructTag" , COFF::IMAGE_SYM_CLASS_STRUCT_TAG }, |
| 298 | { "MemberOfUnion" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_UNION }, |
| 299 | { "UnionTag" , COFF::IMAGE_SYM_CLASS_UNION_TAG }, |
| 300 | { "TypeDefinition" , COFF::IMAGE_SYM_CLASS_TYPE_DEFINITION }, |
| 301 | { "UndefinedStatic", COFF::IMAGE_SYM_CLASS_UNDEFINED_STATIC }, |
| 302 | { "EnumTag" , COFF::IMAGE_SYM_CLASS_ENUM_TAG }, |
| 303 | { "MemberOfEnum" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_ENUM }, |
| 304 | { "RegisterParam" , COFF::IMAGE_SYM_CLASS_REGISTER_PARAM }, |
| 305 | { "BitField" , COFF::IMAGE_SYM_CLASS_BIT_FIELD }, |
| 306 | { "Block" , COFF::IMAGE_SYM_CLASS_BLOCK }, |
| 307 | { "Function" , COFF::IMAGE_SYM_CLASS_FUNCTION }, |
| 308 | { "EndOfStruct" , COFF::IMAGE_SYM_CLASS_END_OF_STRUCT }, |
| 309 | { "File" , COFF::IMAGE_SYM_CLASS_FILE }, |
| 310 | { "Section" , COFF::IMAGE_SYM_CLASS_SECTION }, |
| 311 | { "WeakExternal" , COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL }, |
| 312 | { "CLRToken" , COFF::IMAGE_SYM_CLASS_CLR_TOKEN } |
| 313 | }; |
| 314 | |
| 315 | static const EnumEntry<COFF::COMDATType> ImageCOMDATSelect[] = { |
| 316 | { "NoDuplicates", COFF::IMAGE_COMDAT_SELECT_NODUPLICATES }, |
| 317 | { "Any" , COFF::IMAGE_COMDAT_SELECT_ANY }, |
| 318 | { "SameSize" , COFF::IMAGE_COMDAT_SELECT_SAME_SIZE }, |
| 319 | { "ExactMatch" , COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH }, |
| 320 | { "Associative" , COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE }, |
| 321 | { "Largest" , COFF::IMAGE_COMDAT_SELECT_LARGEST }, |
| 322 | { "Newest" , COFF::IMAGE_COMDAT_SELECT_NEWEST } |
| 323 | }; |
| 324 | |
| 325 | static const EnumEntry<COFF::WeakExternalCharacteristics> |
| 326 | WeakExternalCharacteristics[] = { |
| 327 | { "NoLibrary", COFF::IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY }, |
| 328 | { "Library" , COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY }, |
| 329 | { "Alias" , COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS } |
| 330 | }; |
| 331 | |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 332 | template <typename T> |
| 333 | static std::error_code getSymbolAuxData(const COFFObjectFile *Obj, |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 334 | COFFSymbolRef Symbol, |
| 335 | uint8_t AuxSymbolIdx, const T *&Aux) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 336 | ArrayRef<uint8_t> AuxData = Obj->getSymbolAuxData(Symbol); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 337 | AuxData = AuxData.slice(AuxSymbolIdx * Obj->getSymbolTableEntrySize()); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 338 | Aux = reinterpret_cast<const T*>(AuxData.data()); |
| 339 | return readobj_error::success; |
| 340 | } |
| 341 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 342 | void COFFDumper::cacheRelocations() { |
Rafael Espindola | 76d650e | 2015-07-06 14:26:07 +0000 | [diff] [blame] | 343 | if (RelocCached) |
| 344 | return; |
| 345 | RelocCached = true; |
| 346 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 347 | for (const SectionRef &S : Obj->sections()) { |
| 348 | const coff_section *Section = Obj->getCOFFSection(S); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 349 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 350 | for (const RelocationRef &Reloc : S.relocations()) |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 351 | RelocMap[Section].push_back(Reloc); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 352 | |
| 353 | // Sort relocations by address. |
| 354 | std::sort(RelocMap[Section].begin(), RelocMap[Section].end(), |
| 355 | relocAddressLess); |
| 356 | } |
| 357 | } |
| 358 | |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 359 | void COFFDumper::printDataDirectory(uint32_t Index, const std::string &FieldName) { |
| 360 | const data_directory *Data; |
| 361 | if (Obj->getDataDirectory(Index, Data)) |
| 362 | return; |
| 363 | W.printHex(FieldName + "RVA", Data->RelativeVirtualAddress); |
| 364 | W.printHex(FieldName + "Size", Data->Size); |
| 365 | } |
| 366 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 367 | void COFFDumper::printFileHeaders() { |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 368 | time_t TDS = Obj->getTimeDateStamp(); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 369 | char FormattedTime[20] = { }; |
| 370 | strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS)); |
| 371 | |
| 372 | { |
| 373 | DictScope D(W, "ImageFileHeader"); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 374 | W.printEnum ("Machine", Obj->getMachine(), |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 375 | makeArrayRef(ImageFileMachineType)); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 376 | W.printNumber("SectionCount", Obj->getNumberOfSections()); |
| 377 | W.printHex ("TimeDateStamp", FormattedTime, Obj->getTimeDateStamp()); |
| 378 | W.printHex ("PointerToSymbolTable", Obj->getPointerToSymbolTable()); |
| 379 | W.printNumber("SymbolCount", Obj->getNumberOfSymbols()); |
| 380 | W.printNumber("OptionalHeaderSize", Obj->getSizeOfOptionalHeader()); |
| 381 | W.printFlags ("Characteristics", Obj->getCharacteristics(), |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 382 | makeArrayRef(ImageFileCharacteristics)); |
| 383 | } |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 384 | |
| 385 | // Print PE header. This header does not exist if this is an object file and |
| 386 | // not an executable. |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 387 | const pe32_header *PEHeader = nullptr; |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 388 | if (error(Obj->getPE32Header(PEHeader))) |
| 389 | return; |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 390 | if (PEHeader) |
| 391 | printPEHeader<pe32_header>(PEHeader); |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 392 | |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 393 | const pe32plus_header *PEPlusHeader = nullptr; |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 394 | if (error(Obj->getPE32PlusHeader(PEPlusHeader))) |
| 395 | return; |
| 396 | if (PEPlusHeader) |
| 397 | printPEHeader<pe32plus_header>(PEPlusHeader); |
David Majnemer | 5026722 | 2014-11-05 06:24:35 +0000 | [diff] [blame] | 398 | |
| 399 | if (const dos_header *DH = Obj->getDOSHeader()) |
| 400 | printDOSHeader(DH); |
| 401 | } |
| 402 | |
| 403 | void COFFDumper::printDOSHeader(const dos_header *DH) { |
| 404 | DictScope D(W, "DOSHeader"); |
| 405 | W.printString("Magic", StringRef(DH->Magic, sizeof(DH->Magic))); |
| 406 | W.printNumber("UsedBytesInTheLastPage", DH->UsedBytesInTheLastPage); |
| 407 | W.printNumber("FileSizeInPages", DH->FileSizeInPages); |
| 408 | W.printNumber("NumberOfRelocationItems", DH->NumberOfRelocationItems); |
| 409 | W.printNumber("HeaderSizeInParagraphs", DH->HeaderSizeInParagraphs); |
| 410 | W.printNumber("MinimumExtraParagraphs", DH->MinimumExtraParagraphs); |
| 411 | W.printNumber("MaximumExtraParagraphs", DH->MaximumExtraParagraphs); |
| 412 | W.printNumber("InitialRelativeSS", DH->InitialRelativeSS); |
| 413 | W.printNumber("InitialSP", DH->InitialSP); |
| 414 | W.printNumber("Checksum", DH->Checksum); |
| 415 | W.printNumber("InitialIP", DH->InitialIP); |
| 416 | W.printNumber("InitialRelativeCS", DH->InitialRelativeCS); |
| 417 | W.printNumber("AddressOfRelocationTable", DH->AddressOfRelocationTable); |
| 418 | W.printNumber("OverlayNumber", DH->OverlayNumber); |
| 419 | W.printNumber("OEMid", DH->OEMid); |
| 420 | W.printNumber("OEMinfo", DH->OEMinfo); |
| 421 | W.printNumber("AddressOfNewExeHeader", DH->AddressOfNewExeHeader); |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 422 | } |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 423 | |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 424 | template <class PEHeader> |
| 425 | void COFFDumper::printPEHeader(const PEHeader *Hdr) { |
| 426 | DictScope D(W, "ImageOptionalHeader"); |
| 427 | W.printNumber("MajorLinkerVersion", Hdr->MajorLinkerVersion); |
| 428 | W.printNumber("MinorLinkerVersion", Hdr->MinorLinkerVersion); |
| 429 | W.printNumber("SizeOfCode", Hdr->SizeOfCode); |
| 430 | W.printNumber("SizeOfInitializedData", Hdr->SizeOfInitializedData); |
| 431 | W.printNumber("SizeOfUninitializedData", Hdr->SizeOfUninitializedData); |
| 432 | W.printHex ("AddressOfEntryPoint", Hdr->AddressOfEntryPoint); |
| 433 | W.printHex ("BaseOfCode", Hdr->BaseOfCode); |
| 434 | printBaseOfDataField(Hdr); |
| 435 | W.printHex ("ImageBase", Hdr->ImageBase); |
| 436 | W.printNumber("SectionAlignment", Hdr->SectionAlignment); |
| 437 | W.printNumber("FileAlignment", Hdr->FileAlignment); |
| 438 | W.printNumber("MajorOperatingSystemVersion", |
| 439 | Hdr->MajorOperatingSystemVersion); |
| 440 | W.printNumber("MinorOperatingSystemVersion", |
| 441 | Hdr->MinorOperatingSystemVersion); |
| 442 | W.printNumber("MajorImageVersion", Hdr->MajorImageVersion); |
| 443 | W.printNumber("MinorImageVersion", Hdr->MinorImageVersion); |
| 444 | W.printNumber("MajorSubsystemVersion", Hdr->MajorSubsystemVersion); |
| 445 | W.printNumber("MinorSubsystemVersion", Hdr->MinorSubsystemVersion); |
| 446 | W.printNumber("SizeOfImage", Hdr->SizeOfImage); |
| 447 | W.printNumber("SizeOfHeaders", Hdr->SizeOfHeaders); |
| 448 | W.printEnum ("Subsystem", Hdr->Subsystem, makeArrayRef(PEWindowsSubsystem)); |
David Majnemer | 774aadf | 2014-11-18 02:45:28 +0000 | [diff] [blame] | 449 | W.printFlags ("Characteristics", Hdr->DLLCharacteristics, |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 450 | makeArrayRef(PEDLLCharacteristics)); |
| 451 | W.printNumber("SizeOfStackReserve", Hdr->SizeOfStackReserve); |
| 452 | W.printNumber("SizeOfStackCommit", Hdr->SizeOfStackCommit); |
| 453 | W.printNumber("SizeOfHeapReserve", Hdr->SizeOfHeapReserve); |
| 454 | W.printNumber("SizeOfHeapCommit", Hdr->SizeOfHeapCommit); |
| 455 | W.printNumber("NumberOfRvaAndSize", Hdr->NumberOfRvaAndSize); |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 456 | |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 457 | if (Hdr->NumberOfRvaAndSize > 0) { |
| 458 | DictScope D(W, "DataDirectory"); |
| 459 | static const char * const directory[] = { |
| 460 | "ExportTable", "ImportTable", "ResourceTable", "ExceptionTable", |
| 461 | "CertificateTable", "BaseRelocationTable", "Debug", "Architecture", |
| 462 | "GlobalPtr", "TLSTable", "LoadConfigTable", "BoundImport", "IAT", |
| 463 | "DelayImportDescriptor", "CLRRuntimeHeader", "Reserved" |
| 464 | }; |
| 465 | |
| 466 | for (uint32_t i = 0; i < Hdr->NumberOfRvaAndSize; ++i) { |
| 467 | printDataDirectory(i, directory[i]); |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 468 | } |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 469 | } |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 472 | void COFFDumper::printBaseOfDataField(const pe32_header *Hdr) { |
| 473 | W.printHex("BaseOfData", Hdr->BaseOfData); |
| 474 | } |
| 475 | |
| 476 | void COFFDumper::printBaseOfDataField(const pe32plus_header *) {} |
| 477 | |
Zachary Turner | 99f0215 | 2015-02-18 19:32:05 +0000 | [diff] [blame] | 478 | void COFFDumper::printCodeViewDebugInfo(const SectionRef &Section) { |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 479 | StringRef Data; |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 480 | if (error(Section.getContents(Data))) |
| 481 | return; |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 482 | |
| 483 | SmallVector<StringRef, 10> FunctionNames; |
| 484 | StringMap<StringRef> FunctionLineTables; |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 485 | |
Zachary Turner | 99f0215 | 2015-02-18 19:32:05 +0000 | [diff] [blame] | 486 | ListScope D(W, "CodeViewDebugInfo"); |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 487 | { |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 488 | // FIXME: Add more offset correctness checks. |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 489 | DataExtractor DE(Data, true, 4); |
| 490 | uint32_t Offset = 0, |
| 491 | Magic = DE.getU32(&Offset); |
| 492 | W.printHex("Magic", Magic); |
| 493 | if (Magic != COFF::DEBUG_SECTION_MAGIC) { |
| 494 | error(object_error::parse_failed); |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | bool Finished = false; |
| 499 | while (DE.isValidOffset(Offset) && !Finished) { |
| 500 | // The section consists of a number of subsection in the following format: |
| 501 | // |Type|PayloadSize|Payload...| |
| 502 | uint32_t SubSectionType = DE.getU32(&Offset), |
| 503 | PayloadSize = DE.getU32(&Offset); |
| 504 | ListScope S(W, "Subsection"); |
| 505 | W.printHex("Type", SubSectionType); |
| 506 | W.printHex("PayloadSize", PayloadSize); |
| 507 | if (PayloadSize > Data.size() - Offset) { |
| 508 | error(object_error::parse_failed); |
| 509 | return; |
| 510 | } |
| 511 | |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 512 | StringRef Contents = Data.substr(Offset, PayloadSize); |
Zachary Turner | 99f0215 | 2015-02-18 19:32:05 +0000 | [diff] [blame] | 513 | if (opts::CodeViewSubsectionBytes) { |
| 514 | // Print the raw contents to simplify debugging if anything goes wrong |
| 515 | // afterwards. |
| 516 | W.printBinaryBlock("Contents", Contents); |
| 517 | } |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 518 | |
| 519 | switch (SubSectionType) { |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 520 | case COFF::DEBUG_SYMBOL_SUBSECTION: |
Zachary Turner | 99f0215 | 2015-02-18 19:32:05 +0000 | [diff] [blame] | 521 | if (opts::SectionSymbols) |
| 522 | printCodeViewSymbolsSubsection(Contents, Section, Offset); |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 523 | break; |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 524 | case COFF::DEBUG_LINE_TABLE_SUBSECTION: { |
| 525 | // Holds a PC to file:line table. Some data to parse this subsection is |
| 526 | // stored in the other subsections, so just check sanity and store the |
| 527 | // pointers for deferred processing. |
| 528 | |
| 529 | if (PayloadSize < 12) { |
| 530 | // There should be at least three words to store two function |
| 531 | // relocations and size of the code. |
| 532 | error(object_error::parse_failed); |
| 533 | return; |
| 534 | } |
| 535 | |
| 536 | StringRef FunctionName; |
Saleem Abdulrasool | 5dd27f4 | 2014-05-25 20:26:37 +0000 | [diff] [blame] | 537 | if (error(resolveSymbolName(Obj->getCOFFSection(Section), Offset, |
| 538 | FunctionName))) |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 539 | return; |
| 540 | W.printString("FunctionName", FunctionName); |
| 541 | if (FunctionLineTables.count(FunctionName) != 0) { |
| 542 | // Saw debug info for this function already? |
| 543 | error(object_error::parse_failed); |
| 544 | return; |
| 545 | } |
| 546 | |
| 547 | FunctionLineTables[FunctionName] = Contents; |
| 548 | FunctionNames.push_back(FunctionName); |
| 549 | break; |
| 550 | } |
| 551 | case COFF::DEBUG_STRING_TABLE_SUBSECTION: |
Timur Iskhodzhanov | 1160333 | 2014-10-06 16:59:44 +0000 | [diff] [blame] | 552 | if (PayloadSize == 0 || CVStringTable.data() != nullptr || |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 553 | Contents.back() != '\0') { |
| 554 | // Empty or duplicate or non-null-terminated subsection. |
| 555 | error(object_error::parse_failed); |
| 556 | return; |
| 557 | } |
Timur Iskhodzhanov | 1160333 | 2014-10-06 16:59:44 +0000 | [diff] [blame] | 558 | CVStringTable = Contents; |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 559 | break; |
| 560 | case COFF::DEBUG_INDEX_SUBSECTION: |
| 561 | // Holds the translation table from file indices |
| 562 | // to offsets in the string table. |
| 563 | |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 564 | if (PayloadSize == 0 || |
Timur Iskhodzhanov | 1160333 | 2014-10-06 16:59:44 +0000 | [diff] [blame] | 565 | CVFileIndexToStringOffsetTable.data() != nullptr) { |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 566 | // Empty or duplicate subsection. |
| 567 | error(object_error::parse_failed); |
| 568 | return; |
| 569 | } |
Timur Iskhodzhanov | 1160333 | 2014-10-06 16:59:44 +0000 | [diff] [blame] | 570 | CVFileIndexToStringOffsetTable = Contents; |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 571 | break; |
| 572 | } |
| 573 | Offset += PayloadSize; |
| 574 | |
| 575 | // Align the reading pointer by 4. |
| 576 | Offset += (-Offset) % 4; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | // Dump the line tables now that we've read all the subsections and know all |
| 581 | // the required information. |
| 582 | for (unsigned I = 0, E = FunctionNames.size(); I != E; ++I) { |
| 583 | StringRef Name = FunctionNames[I]; |
| 584 | ListScope S(W, "FunctionLineTable"); |
| 585 | W.printString("FunctionName", Name); |
| 586 | |
| 587 | DataExtractor DE(FunctionLineTables[Name], true, 4); |
| 588 | uint32_t Offset = 8; // Skip relocations. |
| 589 | uint32_t FunctionSize = DE.getU32(&Offset); |
| 590 | W.printHex("CodeSize", FunctionSize); |
| 591 | while (DE.isValidOffset(Offset)) { |
| 592 | // For each range of lines with the same filename, we have a segment |
| 593 | // in the line table. The filename string is accessed using double |
| 594 | // indirection to the string table subsection using the index subsection. |
| 595 | uint32_t OffsetInIndex = DE.getU32(&Offset), |
| 596 | SegmentLength = DE.getU32(&Offset), |
| 597 | FullSegmentSize = DE.getU32(&Offset); |
| 598 | if (FullSegmentSize != 12 + 8 * SegmentLength) { |
| 599 | error(object_error::parse_failed); |
| 600 | return; |
| 601 | } |
| 602 | |
| 603 | uint32_t FilenameOffset; |
| 604 | { |
Timur Iskhodzhanov | 1160333 | 2014-10-06 16:59:44 +0000 | [diff] [blame] | 605 | DataExtractor SDE(CVFileIndexToStringOffsetTable, true, 4); |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 606 | uint32_t OffsetInSDE = OffsetInIndex; |
| 607 | if (!SDE.isValidOffset(OffsetInSDE)) { |
| 608 | error(object_error::parse_failed); |
| 609 | return; |
| 610 | } |
| 611 | FilenameOffset = SDE.getU32(&OffsetInSDE); |
| 612 | } |
| 613 | |
Timur Iskhodzhanov | 1160333 | 2014-10-06 16:59:44 +0000 | [diff] [blame] | 614 | if (FilenameOffset == 0 || FilenameOffset + 1 >= CVStringTable.size() || |
| 615 | CVStringTable.data()[FilenameOffset - 1] != '\0') { |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 616 | // Each string in an F3 subsection should be preceded by a null |
| 617 | // character. |
| 618 | error(object_error::parse_failed); |
| 619 | return; |
| 620 | } |
| 621 | |
Timur Iskhodzhanov | 1160333 | 2014-10-06 16:59:44 +0000 | [diff] [blame] | 622 | StringRef Filename(CVStringTable.data() + FilenameOffset); |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 623 | ListScope S(W, "FilenameSegment"); |
| 624 | W.printString("Filename", Filename); |
| 625 | for (unsigned J = 0; J != SegmentLength && DE.isValidOffset(Offset); |
| 626 | ++J) { |
| 627 | // Then go the (PC, LineNumber) pairs. The line number is stored in the |
| 628 | // least significant 31 bits of the respective word in the table. |
| 629 | uint32_t PC = DE.getU32(&Offset), |
| 630 | LineNumber = DE.getU32(&Offset) & 0x7fffffff; |
| 631 | if (PC >= FunctionSize) { |
| 632 | error(object_error::parse_failed); |
| 633 | return; |
| 634 | } |
| 635 | char Buffer[32]; |
| 636 | format("+0x%X", PC).snprint(Buffer, 32); |
| 637 | W.printNumber(Buffer, LineNumber); |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | } |
| 642 | |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 643 | void COFFDumper::printCodeViewSymbolsSubsection(StringRef Subsection, |
| 644 | const SectionRef &Section, |
| 645 | uint32_t OffsetInSection) { |
| 646 | if (Subsection.size() == 0) { |
| 647 | error(object_error::parse_failed); |
| 648 | return; |
| 649 | } |
| 650 | DataExtractor DE(Subsection, true, 4); |
| 651 | uint32_t Offset = 0; |
| 652 | |
| 653 | // Function-level subsections have "procedure start" and "procedure end" |
| 654 | // commands that should come in pairs and surround relevant info. |
| 655 | bool InFunctionScope = false; |
| 656 | while (DE.isValidOffset(Offset)) { |
| 657 | // Read subsection segments one by one. |
| 658 | uint16_t Size = DE.getU16(&Offset); |
| 659 | // The section size includes the size of the type identifier. |
| 660 | if (Size < 2 || !DE.isValidOffsetForDataOfSize(Offset, Size)) { |
| 661 | error(object_error::parse_failed); |
| 662 | return; |
| 663 | } |
| 664 | Size -= 2; |
| 665 | uint16_t Type = DE.getU16(&Offset); |
| 666 | switch (Type) { |
| 667 | case COFF::DEBUG_SYMBOL_TYPE_PROC_START: { |
| 668 | DictScope S(W, "ProcStart"); |
| 669 | if (InFunctionScope || Size < 36) { |
| 670 | error(object_error::parse_failed); |
| 671 | return; |
| 672 | } |
| 673 | InFunctionScope = true; |
| 674 | |
| 675 | // We're currently interested in a limited subset of fields in this |
| 676 | // segment, just ignore the rest of the fields for now. |
| 677 | uint8_t Unused[12]; |
| 678 | DE.getU8(&Offset, Unused, 12); |
| 679 | uint32_t CodeSize = DE.getU32(&Offset); |
| 680 | DE.getU8(&Offset, Unused, 12); |
| 681 | StringRef SectionName; |
| 682 | if (error(resolveSymbolName(Obj->getCOFFSection(Section), |
| 683 | OffsetInSection + Offset, SectionName))) |
| 684 | return; |
| 685 | Offset += 4; |
| 686 | DE.getU8(&Offset, Unused, 3); |
Timur Iskhodzhanov | a11b32b | 2014-11-12 20:10:09 +0000 | [diff] [blame] | 687 | StringRef DisplayName = DE.getCStr(&Offset); |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 688 | if (!DE.isValidOffset(Offset)) { |
| 689 | error(object_error::parse_failed); |
| 690 | return; |
| 691 | } |
Timur Iskhodzhanov | a11b32b | 2014-11-12 20:10:09 +0000 | [diff] [blame] | 692 | W.printString("DisplayName", DisplayName); |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 693 | W.printString("Section", SectionName); |
| 694 | W.printHex("CodeSize", CodeSize); |
| 695 | |
| 696 | break; |
| 697 | } |
| 698 | case COFF::DEBUG_SYMBOL_TYPE_PROC_END: { |
| 699 | W.startLine() << "ProcEnd\n"; |
| 700 | if (!InFunctionScope || Size > 0) { |
| 701 | error(object_error::parse_failed); |
| 702 | return; |
| 703 | } |
| 704 | InFunctionScope = false; |
| 705 | break; |
| 706 | } |
Zachary Turner | 99f0215 | 2015-02-18 19:32:05 +0000 | [diff] [blame] | 707 | default: { |
| 708 | if (opts::CodeViewSubsectionBytes) { |
| 709 | ListScope S(W, "Record"); |
| 710 | W.printHex("Size", Size); |
| 711 | W.printHex("Type", Type); |
| 712 | |
| 713 | StringRef Contents = DE.getData().substr(Offset, Size); |
| 714 | W.printBinaryBlock("Contents", Contents); |
| 715 | } |
| 716 | |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 717 | Offset += Size; |
| 718 | break; |
| 719 | } |
Zachary Turner | 99f0215 | 2015-02-18 19:32:05 +0000 | [diff] [blame] | 720 | } |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | if (InFunctionScope) |
| 724 | error(object_error::parse_failed); |
| 725 | } |
| 726 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 727 | void COFFDumper::printSections() { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 728 | ListScope SectionsD(W, "Sections"); |
| 729 | int SectionNumber = 0; |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 730 | for (const SectionRef &Sec : Obj->sections()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 731 | ++SectionNumber; |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 732 | const coff_section *Section = Obj->getCOFFSection(Sec); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 733 | |
| 734 | StringRef Name; |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 735 | if (error(Sec.getName(Name))) |
| 736 | Name = ""; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 737 | |
| 738 | DictScope D(W, "Section"); |
| 739 | W.printNumber("Number", SectionNumber); |
| 740 | W.printBinary("Name", Name, Section->Name); |
| 741 | W.printHex ("VirtualSize", Section->VirtualSize); |
| 742 | W.printHex ("VirtualAddress", Section->VirtualAddress); |
| 743 | W.printNumber("RawDataSize", Section->SizeOfRawData); |
| 744 | W.printHex ("PointerToRawData", Section->PointerToRawData); |
| 745 | W.printHex ("PointerToRelocations", Section->PointerToRelocations); |
| 746 | W.printHex ("PointerToLineNumbers", Section->PointerToLinenumbers); |
| 747 | W.printNumber("RelocationCount", Section->NumberOfRelocations); |
| 748 | W.printNumber("LineNumberCount", Section->NumberOfLinenumbers); |
| 749 | W.printFlags ("Characteristics", Section->Characteristics, |
| 750 | makeArrayRef(ImageSectionCharacteristics), |
| 751 | COFF::SectionCharacteristics(0x00F00000)); |
| 752 | |
| 753 | if (opts::SectionRelocations) { |
| 754 | ListScope D(W, "Relocations"); |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 755 | for (const RelocationRef &Reloc : Sec.relocations()) |
| 756 | printRelocation(Sec, Reloc); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | if (opts::SectionSymbols) { |
| 760 | ListScope D(W, "Symbols"); |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 761 | for (const SymbolRef &Symbol : Obj->symbols()) { |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 762 | if (!Sec.containsSymbol(Symbol)) |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 763 | continue; |
| 764 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 765 | printSymbol(Symbol); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | |
Zachary Turner | 99f0215 | 2015-02-18 19:32:05 +0000 | [diff] [blame] | 769 | if (Name == ".debug$S" && opts::CodeView) |
| 770 | printCodeViewDebugInfo(Sec); |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 771 | |
David Majnemer | dac3985 | 2014-09-26 22:32:16 +0000 | [diff] [blame] | 772 | if (opts::SectionData && |
| 773 | !(Section->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 774 | StringRef Data; |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 775 | if (error(Sec.getContents(Data))) |
| 776 | break; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 777 | |
| 778 | W.printBinaryBlock("SectionData", Data); |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | void COFFDumper::printRelocations() { |
| 784 | ListScope D(W, "Relocations"); |
| 785 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 786 | int SectionNumber = 0; |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 787 | for (const SectionRef &Section : Obj->sections()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 788 | ++SectionNumber; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 789 | StringRef Name; |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 790 | if (error(Section.getName(Name))) |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 791 | continue; |
| 792 | |
| 793 | bool PrintedGroup = false; |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 794 | for (const RelocationRef &Reloc : Section.relocations()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 795 | if (!PrintedGroup) { |
| 796 | W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n"; |
| 797 | W.indent(); |
| 798 | PrintedGroup = true; |
| 799 | } |
| 800 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 801 | printRelocation(Section, Reloc); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | if (PrintedGroup) { |
| 805 | W.unindent(); |
| 806 | W.startLine() << "}\n"; |
| 807 | } |
| 808 | } |
| 809 | } |
| 810 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 811 | void COFFDumper::printRelocation(const SectionRef &Section, |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 812 | const RelocationRef &Reloc) { |
Rafael Espindola | 96d071c | 2015-06-29 23:29:12 +0000 | [diff] [blame] | 813 | uint64_t Offset = Reloc.getOffset(); |
Rafael Espindola | 99c041b | 2015-06-30 01:53:01 +0000 | [diff] [blame] | 814 | uint64_t RelocType = Reloc.getType(); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 815 | SmallString<32> RelocName; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 816 | StringRef SymbolName; |
Rafael Espindola | 41bb432 | 2015-06-30 04:08:37 +0000 | [diff] [blame] | 817 | Reloc.getTypeName(RelocName); |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 818 | symbol_iterator Symbol = Reloc.getSymbol(); |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 819 | if (Symbol != Obj->symbol_end()) { |
| 820 | ErrorOr<StringRef> SymbolNameOrErr = Symbol->getName(); |
| 821 | if (error(SymbolNameOrErr.getError())) |
| 822 | return; |
| 823 | SymbolName = *SymbolNameOrErr; |
| 824 | } |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 825 | |
Nico Rieck | f3f0b79 | 2013-04-12 04:01:52 +0000 | [diff] [blame] | 826 | if (opts::ExpandRelocs) { |
| 827 | DictScope Group(W, "Relocation"); |
| 828 | W.printHex("Offset", Offset); |
| 829 | W.printNumber("Type", RelocName, RelocType); |
David Majnemer | 1f80b0a | 2014-11-13 07:42:11 +0000 | [diff] [blame] | 830 | W.printString("Symbol", SymbolName.empty() ? "-" : SymbolName); |
Nico Rieck | f3f0b79 | 2013-04-12 04:01:52 +0000 | [diff] [blame] | 831 | } else { |
| 832 | raw_ostream& OS = W.startLine(); |
| 833 | OS << W.hex(Offset) |
| 834 | << " " << RelocName |
David Majnemer | 1f80b0a | 2014-11-13 07:42:11 +0000 | [diff] [blame] | 835 | << " " << (SymbolName.empty() ? "-" : SymbolName) |
Nico Rieck | f3f0b79 | 2013-04-12 04:01:52 +0000 | [diff] [blame] | 836 | << "\n"; |
| 837 | } |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | void COFFDumper::printSymbols() { |
| 841 | ListScope Group(W, "Symbols"); |
| 842 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 843 | for (const SymbolRef &Symbol : Obj->symbols()) |
| 844 | printSymbol(Symbol); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 847 | void COFFDumper::printDynamicSymbols() { ListScope Group(W, "DynamicSymbols"); } |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 848 | |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 849 | static ErrorOr<StringRef> |
| 850 | getSectionName(const llvm::object::COFFObjectFile *Obj, int32_t SectionNumber, |
| 851 | const coff_section *Section) { |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 852 | if (Section) { |
| 853 | StringRef SectionName; |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 854 | if (std::error_code EC = Obj->getSectionName(Section, SectionName)) |
| 855 | return EC; |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 856 | return SectionName; |
| 857 | } |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 858 | if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG) |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 859 | return StringRef("IMAGE_SYM_DEBUG"); |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 860 | if (SectionNumber == llvm::COFF::IMAGE_SYM_ABSOLUTE) |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 861 | return StringRef("IMAGE_SYM_ABSOLUTE"); |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 862 | if (SectionNumber == llvm::COFF::IMAGE_SYM_UNDEFINED) |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 863 | return StringRef("IMAGE_SYM_UNDEFINED"); |
| 864 | return StringRef(""); |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 867 | void COFFDumper::printSymbol(const SymbolRef &Sym) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 868 | DictScope D(W, "Symbol"); |
| 869 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 870 | COFFSymbolRef Symbol = Obj->getCOFFSymbol(Sym); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 871 | const coff_section *Section; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 872 | if (std::error_code EC = Obj->getSection(Symbol.getSectionNumber(), Section)) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 873 | W.startLine() << "Invalid section number: " << EC.message() << "\n"; |
| 874 | W.flush(); |
| 875 | return; |
| 876 | } |
| 877 | |
| 878 | StringRef SymbolName; |
| 879 | if (Obj->getSymbolName(Symbol, SymbolName)) |
| 880 | SymbolName = ""; |
| 881 | |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 882 | StringRef SectionName = ""; |
| 883 | ErrorOr<StringRef> Res = |
| 884 | getSectionName(Obj, Symbol.getSectionNumber(), Section); |
| 885 | if (Res) |
| 886 | SectionName = *Res; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 887 | |
| 888 | W.printString("Name", SymbolName); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 889 | W.printNumber("Value", Symbol.getValue()); |
| 890 | W.printNumber("Section", SectionName, Symbol.getSectionNumber()); |
| 891 | W.printEnum ("BaseType", Symbol.getBaseType(), makeArrayRef(ImageSymType)); |
| 892 | W.printEnum ("ComplexType", Symbol.getComplexType(), |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 893 | makeArrayRef(ImageSymDType)); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 894 | W.printEnum ("StorageClass", Symbol.getStorageClass(), |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 895 | makeArrayRef(ImageSymClass)); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 896 | W.printNumber("AuxSymbolCount", Symbol.getNumberOfAuxSymbols()); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 897 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 898 | for (uint8_t I = 0; I < Symbol.getNumberOfAuxSymbols(); ++I) { |
| 899 | if (Symbol.isFunctionDefinition()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 900 | const coff_aux_function_definition *Aux; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 901 | if (error(getSymbolAuxData(Obj, Symbol, I, Aux))) |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 902 | break; |
| 903 | |
| 904 | DictScope AS(W, "AuxFunctionDef"); |
| 905 | W.printNumber("TagIndex", Aux->TagIndex); |
| 906 | W.printNumber("TotalSize", Aux->TotalSize); |
David Majnemer | f3a2af5 | 2014-03-19 04:33:27 +0000 | [diff] [blame] | 907 | W.printHex("PointerToLineNumber", Aux->PointerToLinenumber); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 908 | W.printHex("PointerToNextFunction", Aux->PointerToNextFunction); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 909 | |
David Majnemer | c7d7c6f | 2014-10-31 05:07:00 +0000 | [diff] [blame] | 910 | } else if (Symbol.isAnyUndefined()) { |
David Majnemer | f3a2af5 | 2014-03-19 04:33:27 +0000 | [diff] [blame] | 911 | const coff_aux_weak_external *Aux; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 912 | if (error(getSymbolAuxData(Obj, Symbol, I, Aux))) |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 913 | break; |
| 914 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 915 | ErrorOr<COFFSymbolRef> Linked = Obj->getSymbol(Aux->TagIndex); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 916 | StringRef LinkedName; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 917 | std::error_code EC = Linked.getError(); |
| 918 | if (EC || (EC = Obj->getSymbolName(*Linked, LinkedName))) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 919 | LinkedName = ""; |
| 920 | error(EC); |
| 921 | } |
| 922 | |
| 923 | DictScope AS(W, "AuxWeakExternal"); |
| 924 | W.printNumber("Linked", LinkedName, Aux->TagIndex); |
| 925 | W.printEnum ("Search", Aux->Characteristics, |
| 926 | makeArrayRef(WeakExternalCharacteristics)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 927 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 928 | } else if (Symbol.isFileRecord()) { |
| 929 | const char *FileName; |
| 930 | if (error(getSymbolAuxData(Obj, Symbol, I, FileName))) |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 931 | break; |
| 932 | |
Nico Rieck | 0ab8e60 | 2013-04-22 08:35:11 +0000 | [diff] [blame] | 933 | DictScope AS(W, "AuxFileRecord"); |
Saleem Abdulrasool | d38c6b1 | 2014-04-14 02:37:23 +0000 | [diff] [blame] | 934 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 935 | StringRef Name(FileName, Symbol.getNumberOfAuxSymbols() * |
| 936 | Obj->getSymbolTableEntrySize()); |
Saleem Abdulrasool | d38c6b1 | 2014-04-14 02:37:23 +0000 | [diff] [blame] | 937 | W.printString("FileName", Name.rtrim(StringRef("\0", 1))); |
Saleem Abdulrasool | 3b5e001 | 2014-04-16 04:15:29 +0000 | [diff] [blame] | 938 | break; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 939 | } else if (Symbol.isSectionDefinition()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 940 | const coff_aux_section_definition *Aux; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 941 | if (error(getSymbolAuxData(Obj, Symbol, I, Aux))) |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 942 | break; |
| 943 | |
David Majnemer | 4d57159 | 2014-09-15 19:42:42 +0000 | [diff] [blame] | 944 | int32_t AuxNumber = Aux->getNumber(Symbol.isBigObj()); |
| 945 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 946 | DictScope AS(W, "AuxSectionDef"); |
| 947 | W.printNumber("Length", Aux->Length); |
| 948 | W.printNumber("RelocationCount", Aux->NumberOfRelocations); |
| 949 | W.printNumber("LineNumberCount", Aux->NumberOfLinenumbers); |
| 950 | W.printHex("Checksum", Aux->CheckSum); |
David Majnemer | 4d57159 | 2014-09-15 19:42:42 +0000 | [diff] [blame] | 951 | W.printNumber("Number", AuxNumber); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 952 | W.printEnum("Selection", Aux->Selection, makeArrayRef(ImageCOMDATSelect)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 953 | |
Nico Rieck | a711dee | 2013-04-22 08:34:59 +0000 | [diff] [blame] | 954 | if (Section && Section->Characteristics & COFF::IMAGE_SCN_LNK_COMDAT |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 955 | && Aux->Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) { |
| 956 | const coff_section *Assoc; |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 957 | StringRef AssocName = ""; |
| 958 | std::error_code EC = Obj->getSection(AuxNumber, Assoc); |
| 959 | ErrorOr<StringRef> Res = getSectionName(Obj, AuxNumber, Assoc); |
| 960 | if (Res) |
| 961 | AssocName = *Res; |
| 962 | if (!EC) |
| 963 | EC = Res.getError(); |
| 964 | if (EC) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 965 | AssocName = ""; |
| 966 | error(EC); |
| 967 | } |
| 968 | |
David Majnemer | 4d57159 | 2014-09-15 19:42:42 +0000 | [diff] [blame] | 969 | W.printNumber("AssocSection", AssocName, AuxNumber); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 970 | } |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 971 | } else if (Symbol.isCLRToken()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 972 | const coff_aux_clr_token *Aux; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 973 | if (error(getSymbolAuxData(Obj, Symbol, I, Aux))) |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 974 | break; |
| 975 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 976 | ErrorOr<COFFSymbolRef> ReferredSym = |
| 977 | Obj->getSymbol(Aux->SymbolTableIndex); |
Nico Rieck | 8678acd | 2014-03-17 01:46:52 +0000 | [diff] [blame] | 978 | StringRef ReferredName; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 979 | std::error_code EC = ReferredSym.getError(); |
| 980 | if (EC || (EC = Obj->getSymbolName(*ReferredSym, ReferredName))) { |
Nico Rieck | 8678acd | 2014-03-17 01:46:52 +0000 | [diff] [blame] | 981 | ReferredName = ""; |
| 982 | error(EC); |
| 983 | } |
| 984 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 985 | DictScope AS(W, "AuxCLRToken"); |
| 986 | W.printNumber("AuxType", Aux->AuxType); |
| 987 | W.printNumber("Reserved", Aux->Reserved); |
Nico Rieck | 8678acd | 2014-03-17 01:46:52 +0000 | [diff] [blame] | 988 | W.printNumber("SymbolTableIndex", ReferredName, Aux->SymbolTableIndex); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 989 | |
| 990 | } else { |
| 991 | W.startLine() << "<unhandled auxiliary record>\n"; |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | void COFFDumper::printUnwindInfo() { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 997 | ListScope D(W, "UnwindInformation"); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 998 | switch (Obj->getMachine()) { |
Saleem Abdulrasool | e8839a7 | 2014-05-25 20:26:45 +0000 | [diff] [blame] | 999 | case COFF::IMAGE_FILE_MACHINE_AMD64: { |
| 1000 | Win64EH::Dumper Dumper(W); |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 1001 | Win64EH::Dumper::SymbolResolver |
| 1002 | Resolver = [](const object::coff_section *Section, uint64_t Offset, |
| 1003 | SymbolRef &Symbol, void *user_data) -> std::error_code { |
| 1004 | COFFDumper *Dumper = reinterpret_cast<COFFDumper *>(user_data); |
| 1005 | return Dumper->resolveSymbol(Section, Offset, Symbol); |
| 1006 | }; |
Saleem Abdulrasool | 65dbbb5 | 2014-05-25 21:37:59 +0000 | [diff] [blame] | 1007 | Win64EH::Dumper::Context Ctx(*Obj, Resolver, this); |
Saleem Abdulrasool | e8839a7 | 2014-05-25 20:26:45 +0000 | [diff] [blame] | 1008 | Dumper.printData(Ctx); |
| 1009 | break; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1010 | } |
Saleem Abdulrasool | e6971ca | 2014-06-04 15:47:15 +0000 | [diff] [blame] | 1011 | case COFF::IMAGE_FILE_MACHINE_ARMNT: { |
| 1012 | ARM::WinEH::Decoder Decoder(W); |
| 1013 | Decoder.dumpProcedureData(*Obj); |
| 1014 | break; |
| 1015 | } |
Saleem Abdulrasool | e8839a7 | 2014-05-25 20:26:45 +0000 | [diff] [blame] | 1016 | default: |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1017 | W.printEnum("unsupported Image Machine", Obj->getMachine(), |
Saleem Abdulrasool | e8839a7 | 2014-05-25 20:26:45 +0000 | [diff] [blame] | 1018 | makeArrayRef(ImageFileMachineType)); |
| 1019 | break; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1020 | } |
| 1021 | } |
| 1022 | |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 1023 | void COFFDumper::printImportedSymbols( |
| 1024 | iterator_range<imported_symbol_iterator> Range) { |
| 1025 | for (const ImportedSymbolRef &I : Range) { |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1026 | StringRef Sym; |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 1027 | if (error(I.getSymbolName(Sym))) return; |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1028 | uint16_t Ordinal; |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 1029 | if (error(I.getOrdinal(Ordinal))) return; |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1030 | W.printNumber("Symbol", Sym, Ordinal); |
| 1031 | } |
| 1032 | } |
| 1033 | |
Rui Ueyama | ffa4ceb | 2014-11-13 03:22:54 +0000 | [diff] [blame] | 1034 | void COFFDumper::printDelayImportedSymbols( |
| 1035 | const DelayImportDirectoryEntryRef &I, |
| 1036 | iterator_range<imported_symbol_iterator> Range) { |
| 1037 | int Index = 0; |
| 1038 | for (const ImportedSymbolRef &S : Range) { |
| 1039 | DictScope Import(W, "Import"); |
| 1040 | StringRef Sym; |
| 1041 | if (error(S.getSymbolName(Sym))) return; |
| 1042 | uint16_t Ordinal; |
| 1043 | if (error(S.getOrdinal(Ordinal))) return; |
| 1044 | W.printNumber("Symbol", Sym, Ordinal); |
| 1045 | uint64_t Addr; |
| 1046 | if (error(I.getImportAddress(Index++, Addr))) return; |
| 1047 | W.printHex("Address", Addr); |
| 1048 | } |
| 1049 | } |
| 1050 | |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1051 | void COFFDumper::printCOFFImports() { |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1052 | // Regular imports |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 1053 | for (const ImportDirectoryEntryRef &I : Obj->import_directories()) { |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1054 | DictScope Import(W, "Import"); |
| 1055 | StringRef Name; |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 1056 | if (error(I.getName(Name))) return; |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1057 | W.printString("Name", Name); |
| 1058 | uint32_t Addr; |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 1059 | if (error(I.getImportLookupTableRVA(Addr))) return; |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1060 | W.printHex("ImportLookupTableRVA", Addr); |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 1061 | if (error(I.getImportAddressTableRVA(Addr))) return; |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1062 | W.printHex("ImportAddressTableRVA", Addr); |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 1063 | printImportedSymbols(I.imported_symbols()); |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | // Delay imports |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 1067 | for (const DelayImportDirectoryEntryRef &I : Obj->delay_import_directories()) { |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1068 | DictScope Import(W, "DelayImport"); |
| 1069 | StringRef Name; |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 1070 | if (error(I.getName(Name))) return; |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1071 | W.printString("Name", Name); |
Rui Ueyama | 1af0865 | 2014-10-03 18:07:18 +0000 | [diff] [blame] | 1072 | const delay_import_directory_table_entry *Table; |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 1073 | if (error(I.getDelayImportTable(Table))) return; |
Rui Ueyama | 1af0865 | 2014-10-03 18:07:18 +0000 | [diff] [blame] | 1074 | W.printHex("Attributes", Table->Attributes); |
| 1075 | W.printHex("ModuleHandle", Table->ModuleHandle); |
| 1076 | W.printHex("ImportAddressTable", Table->DelayImportAddressTable); |
| 1077 | W.printHex("ImportNameTable", Table->DelayImportNameTable); |
| 1078 | W.printHex("BoundDelayImportTable", Table->BoundDelayImportTable); |
| 1079 | W.printHex("UnloadDelayImportTable", Table->UnloadDelayImportTable); |
Rui Ueyama | ffa4ceb | 2014-11-13 03:22:54 +0000 | [diff] [blame] | 1080 | printDelayImportedSymbols(I, I.imported_symbols()); |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1081 | } |
| 1082 | } |
Saleem Abdulrasool | f957863 | 2014-10-07 19:37:52 +0000 | [diff] [blame] | 1083 | |
Saleem Abdulrasool | ddd9264 | 2015-01-03 21:35:09 +0000 | [diff] [blame] | 1084 | void COFFDumper::printCOFFExports() { |
| 1085 | for (const ExportDirectoryEntryRef &E : Obj->export_directories()) { |
| 1086 | DictScope Export(W, "Export"); |
| 1087 | |
| 1088 | StringRef Name; |
| 1089 | uint32_t Ordinal, RVA; |
| 1090 | |
| 1091 | if (error(E.getSymbolName(Name))) |
| 1092 | continue; |
| 1093 | if (error(E.getOrdinal(Ordinal))) |
| 1094 | continue; |
| 1095 | if (error(E.getExportRVA(RVA))) |
| 1096 | continue; |
| 1097 | |
| 1098 | W.printNumber("Ordinal", Ordinal); |
| 1099 | W.printString("Name", Name); |
| 1100 | W.printHex("RVA", RVA); |
| 1101 | } |
| 1102 | } |
| 1103 | |
Saleem Abdulrasool | f957863 | 2014-10-07 19:37:52 +0000 | [diff] [blame] | 1104 | void COFFDumper::printCOFFDirectives() { |
| 1105 | for (const SectionRef &Section : Obj->sections()) { |
| 1106 | StringRef Contents; |
| 1107 | StringRef Name; |
| 1108 | |
| 1109 | if (error(Section.getName(Name))) |
| 1110 | continue; |
| 1111 | if (Name != ".drectve") |
| 1112 | continue; |
| 1113 | |
| 1114 | if (error(Section.getContents(Contents))) |
| 1115 | return; |
| 1116 | |
| 1117 | W.printString("Directive(s)", Contents); |
| 1118 | } |
| 1119 | } |
Rui Ueyama | 74e8513 | 2014-11-19 00:18:07 +0000 | [diff] [blame] | 1120 | |
| 1121 | static StringRef getBaseRelocTypeName(uint8_t Type) { |
| 1122 | switch (Type) { |
| 1123 | case COFF::IMAGE_REL_BASED_ABSOLUTE: return "ABSOLUTE"; |
| 1124 | case COFF::IMAGE_REL_BASED_HIGH: return "HIGH"; |
| 1125 | case COFF::IMAGE_REL_BASED_LOW: return "LOW"; |
| 1126 | case COFF::IMAGE_REL_BASED_HIGHLOW: return "HIGHLOW"; |
| 1127 | case COFF::IMAGE_REL_BASED_HIGHADJ: return "HIGHADJ"; |
Saleem Abdulrasool | 5a41c37 | 2015-01-16 20:16:09 +0000 | [diff] [blame] | 1128 | case COFF::IMAGE_REL_BASED_ARM_MOV32T: return "ARM_MOV32(T)"; |
Rui Ueyama | 74e8513 | 2014-11-19 00:18:07 +0000 | [diff] [blame] | 1129 | case COFF::IMAGE_REL_BASED_DIR64: return "DIR64"; |
Colin LeMahieu | 9fbffee | 2014-11-19 17:10:39 +0000 | [diff] [blame] | 1130 | default: return "unknown (" + llvm::utostr(Type) + ")"; |
Rui Ueyama | 74e8513 | 2014-11-19 00:18:07 +0000 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | void COFFDumper::printCOFFBaseReloc() { |
| 1135 | ListScope D(W, "BaseReloc"); |
| 1136 | for (const BaseRelocRef &I : Obj->base_relocs()) { |
| 1137 | uint8_t Type; |
| 1138 | uint32_t RVA; |
| 1139 | if (error(I.getRVA(RVA))) |
| 1140 | continue; |
| 1141 | if (error(I.getType(Type))) |
| 1142 | continue; |
Rui Ueyama | 74e8513 | 2014-11-19 00:18:07 +0000 | [diff] [blame] | 1143 | DictScope Import(W, "Entry"); |
| 1144 | W.printString("Type", getBaseRelocTypeName(Type)); |
| 1145 | W.printHex("Address", RVA); |
| 1146 | } |
| 1147 | } |
Lang Hames | 0000afd | 2015-06-26 23:56:53 +0000 | [diff] [blame] | 1148 | |
| 1149 | void COFFDumper::printStackMap() const { |
| 1150 | object::SectionRef StackMapSection; |
| 1151 | for (auto Sec : Obj->sections()) { |
| 1152 | StringRef Name; |
| 1153 | Sec.getName(Name); |
| 1154 | if (Name == ".llvm_stackmaps") { |
| 1155 | StackMapSection = Sec; |
| 1156 | break; |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | if (StackMapSection == object::SectionRef()) |
| 1161 | return; |
| 1162 | |
| 1163 | StringRef StackMapContents; |
| 1164 | StackMapSection.getContents(StackMapContents); |
| 1165 | ArrayRef<uint8_t> StackMapContentsArray( |
| 1166 | reinterpret_cast<const uint8_t*>(StackMapContents.data()), |
| 1167 | StackMapContents.size()); |
| 1168 | |
| 1169 | if (Obj->isLittleEndian()) |
| 1170 | prettyPrintStackMap( |
| 1171 | llvm::outs(), |
| 1172 | StackMapV1Parser<support::little>(StackMapContentsArray)); |
| 1173 | else |
| 1174 | prettyPrintStackMap(llvm::outs(), |
| 1175 | StackMapV1Parser<support::big>(StackMapContentsArray)); |
| 1176 | } |