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