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