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