Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 1 | //===-- COFFDump.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 |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 11 | /// This file implements the COFF-specific dumper for llvm-objdump. |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 12 | /// It outputs the Win64 EH data structures as plain text. |
Alp Toker | ae43cab6 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 13 | /// The encoding of the unwind codes is described in MSDN: |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 14 | /// http://msdn.microsoft.com/en-us/library/ck9asaa9.aspx |
| 15 | /// |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "llvm-objdump.h" |
Zachary Turner | c04ae04 | 2018-08-20 22:18:21 +0000 | [diff] [blame] | 19 | #include "llvm/Demangle/Demangle.h" |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 20 | #include "llvm/Object/COFF.h" |
Saleem Abdulrasool | 8a83602 | 2016-08-18 16:39:19 +0000 | [diff] [blame] | 21 | #include "llvm/Object/COFFImportFile.h" |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 22 | #include "llvm/Object/ObjectFile.h" |
| 23 | #include "llvm/Support/Format.h" |
Chandler Carruth | 7f00f87 | 2013-01-02 10:26:28 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Win64EH.h" |
Jonas Devlieghere | 8e8485e | 2018-11-11 22:12:04 +0000 | [diff] [blame] | 25 | #include "llvm/Support/WithColor.h" |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace llvm; |
| 29 | using namespace object; |
| 30 | using namespace llvm::Win64EH; |
| 31 | |
| 32 | // Returns the name of the unwind code. |
| 33 | static StringRef getUnwindCodeTypeName(uint8_t Code) { |
| 34 | switch(Code) { |
| 35 | default: llvm_unreachable("Invalid unwind code"); |
| 36 | case UOP_PushNonVol: return "UOP_PushNonVol"; |
| 37 | case UOP_AllocLarge: return "UOP_AllocLarge"; |
| 38 | case UOP_AllocSmall: return "UOP_AllocSmall"; |
| 39 | case UOP_SetFPReg: return "UOP_SetFPReg"; |
| 40 | case UOP_SaveNonVol: return "UOP_SaveNonVol"; |
| 41 | case UOP_SaveNonVolBig: return "UOP_SaveNonVolBig"; |
| 42 | case UOP_SaveXMM128: return "UOP_SaveXMM128"; |
| 43 | case UOP_SaveXMM128Big: return "UOP_SaveXMM128Big"; |
| 44 | case UOP_PushMachFrame: return "UOP_PushMachFrame"; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // Returns the name of a referenced register. |
| 49 | static StringRef getUnwindRegisterName(uint8_t Reg) { |
| 50 | switch(Reg) { |
| 51 | default: llvm_unreachable("Invalid register"); |
| 52 | case 0: return "RAX"; |
| 53 | case 1: return "RCX"; |
| 54 | case 2: return "RDX"; |
| 55 | case 3: return "RBX"; |
| 56 | case 4: return "RSP"; |
| 57 | case 5: return "RBP"; |
| 58 | case 6: return "RSI"; |
| 59 | case 7: return "RDI"; |
| 60 | case 8: return "R8"; |
| 61 | case 9: return "R9"; |
| 62 | case 10: return "R10"; |
| 63 | case 11: return "R11"; |
| 64 | case 12: return "R12"; |
| 65 | case 13: return "R13"; |
| 66 | case 14: return "R14"; |
| 67 | case 15: return "R15"; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Calculates the number of array slots required for the unwind code. |
| 72 | static unsigned getNumUsedSlots(const UnwindCode &UnwindCode) { |
| 73 | switch (UnwindCode.getUnwindOp()) { |
| 74 | default: llvm_unreachable("Invalid unwind code"); |
| 75 | case UOP_PushNonVol: |
| 76 | case UOP_AllocSmall: |
| 77 | case UOP_SetFPReg: |
| 78 | case UOP_PushMachFrame: |
| 79 | return 1; |
| 80 | case UOP_SaveNonVol: |
| 81 | case UOP_SaveXMM128: |
| 82 | return 2; |
| 83 | case UOP_SaveNonVolBig: |
| 84 | case UOP_SaveXMM128Big: |
| 85 | return 3; |
| 86 | case UOP_AllocLarge: |
| 87 | return (UnwindCode.getOpInfo() == 0) ? 2 : 3; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // Prints one unwind code. Because an unwind code can occupy up to 3 slots in |
| 92 | // the unwind codes array, this function requires that the correct number of |
| 93 | // slots is provided. |
| 94 | static void printUnwindCode(ArrayRef<UnwindCode> UCs) { |
| 95 | assert(UCs.size() >= getNumUsedSlots(UCs[0])); |
Rui Ueyama | afee151 | 2014-03-04 19:23:56 +0000 | [diff] [blame] | 96 | outs() << format(" 0x%02x: ", unsigned(UCs[0].u.CodeOffset)) |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 97 | << getUnwindCodeTypeName(UCs[0].getUnwindOp()); |
| 98 | switch (UCs[0].getUnwindOp()) { |
| 99 | case UOP_PushNonVol: |
| 100 | outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo()); |
| 101 | break; |
| 102 | case UOP_AllocLarge: |
| 103 | if (UCs[0].getOpInfo() == 0) { |
| 104 | outs() << " " << UCs[1].FrameOffset; |
| 105 | } else { |
| 106 | outs() << " " << UCs[1].FrameOffset |
| 107 | + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16); |
| 108 | } |
| 109 | break; |
| 110 | case UOP_AllocSmall: |
| 111 | outs() << " " << ((UCs[0].getOpInfo() + 1) * 8); |
| 112 | break; |
| 113 | case UOP_SetFPReg: |
| 114 | outs() << " "; |
| 115 | break; |
| 116 | case UOP_SaveNonVol: |
| 117 | outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo()) |
| 118 | << format(" [0x%04x]", 8 * UCs[1].FrameOffset); |
| 119 | break; |
| 120 | case UOP_SaveNonVolBig: |
| 121 | outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo()) |
| 122 | << format(" [0x%08x]", UCs[1].FrameOffset |
| 123 | + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16)); |
| 124 | break; |
| 125 | case UOP_SaveXMM128: |
| 126 | outs() << " XMM" << static_cast<uint32_t>(UCs[0].getOpInfo()) |
| 127 | << format(" [0x%04x]", 16 * UCs[1].FrameOffset); |
| 128 | break; |
| 129 | case UOP_SaveXMM128Big: |
| 130 | outs() << " XMM" << UCs[0].getOpInfo() |
| 131 | << format(" [0x%08x]", UCs[1].FrameOffset |
| 132 | + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16)); |
| 133 | break; |
| 134 | case UOP_PushMachFrame: |
| 135 | outs() << " " << (UCs[0].getOpInfo() ? "w/o" : "w") |
| 136 | << " error code"; |
| 137 | break; |
| 138 | } |
| 139 | outs() << "\n"; |
| 140 | } |
| 141 | |
| 142 | static void printAllUnwindCodes(ArrayRef<UnwindCode> UCs) { |
| 143 | for (const UnwindCode *I = UCs.begin(), *E = UCs.end(); I < E; ) { |
| 144 | unsigned UsedSlots = getNumUsedSlots(*I); |
| 145 | if (UsedSlots > UCs.size()) { |
| 146 | outs() << "Unwind data corrupted: Encountered unwind op " |
| 147 | << getUnwindCodeTypeName((*I).getUnwindOp()) |
| 148 | << " which requires " << UsedSlots |
| 149 | << " slots, but only " << UCs.size() |
| 150 | << " remaining in buffer"; |
| 151 | return ; |
| 152 | } |
Craig Topper | 795a06a | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 153 | printUnwindCode(makeArrayRef(I, E)); |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 154 | I += UsedSlots; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Given a symbol sym this functions returns the address and section of it. |
Rafael Espindola | 1ad4502 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 159 | static std::error_code |
| 160 | resolveSectionAndAddress(const COFFObjectFile *Obj, const SymbolRef &Sym, |
| 161 | const coff_section *&ResolvedSection, |
| 162 | uint64_t &ResolvedAddr) { |
Kevin Enderby | 317de7c | 2016-06-24 18:24:42 +0000 | [diff] [blame] | 163 | Expected<uint64_t> ResolvedAddrOrErr = Sym.getAddress(); |
| 164 | if (!ResolvedAddrOrErr) |
| 165 | return errorToErrorCode(ResolvedAddrOrErr.takeError()); |
Rafael Espindola | 5954faa | 2015-07-03 18:19:00 +0000 | [diff] [blame] | 166 | ResolvedAddr = *ResolvedAddrOrErr; |
Kevin Enderby | a486dca | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 167 | Expected<section_iterator> Iter = Sym.getSection(); |
| 168 | if (!Iter) |
| 169 | return errorToErrorCode(Iter.takeError()); |
Rafael Espindola | e84d8c1 | 2015-08-07 23:27:14 +0000 | [diff] [blame] | 170 | ResolvedSection = Obj->getCOFFSection(**Iter); |
Rui Ueyama | eae4673 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 171 | return std::error_code(); |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // Given a vector of relocations for a section and an offset into this section |
| 175 | // the function returns the symbol used for the relocation at the offset. |
Rafael Espindola | 1ad4502 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 176 | static std::error_code resolveSymbol(const std::vector<RelocationRef> &Rels, |
| 177 | uint64_t Offset, SymbolRef &Sym) { |
Davide Italiano | c5efc2c | 2016-09-30 23:22:42 +0000 | [diff] [blame] | 178 | for (auto &R : Rels) { |
| 179 | uint64_t Ofs = R.getOffset(); |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 180 | if (Ofs == Offset) { |
Davide Italiano | c5efc2c | 2016-09-30 23:22:42 +0000 | [diff] [blame] | 181 | Sym = *R.getSymbol(); |
Rui Ueyama | eae4673 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 182 | return std::error_code(); |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
Rui Ueyama | 49c7656 | 2014-02-28 05:21:29 +0000 | [diff] [blame] | 185 | return object_error::parse_failed; |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // Given a vector of relocations for a section and an offset into this section |
| 189 | // the function resolves the symbol used for the relocation at the offset and |
| 190 | // returns the section content and the address inside the content pointed to |
| 191 | // by the symbol. |
Rafael Espindola | 1ad4502 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 192 | static std::error_code |
| 193 | getSectionContents(const COFFObjectFile *Obj, |
| 194 | const std::vector<RelocationRef> &Rels, uint64_t Offset, |
| 195 | ArrayRef<uint8_t> &Contents, uint64_t &Addr) { |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 196 | SymbolRef Sym; |
Rafael Espindola | 1ad4502 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 197 | if (std::error_code EC = resolveSymbol(Rels, Offset, Sym)) |
Rui Ueyama | 6738a1b | 2014-02-28 05:21:26 +0000 | [diff] [blame] | 198 | return EC; |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 199 | const coff_section *Section; |
Rafael Espindola | 1ad4502 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 200 | if (std::error_code EC = resolveSectionAndAddress(Obj, Sym, Section, Addr)) |
Rui Ueyama | 4b81ee4 | 2014-01-16 20:57:55 +0000 | [diff] [blame] | 201 | return EC; |
Rafael Espindola | 1ad4502 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 202 | if (std::error_code EC = Obj->getSectionContents(Section, Contents)) |
Rui Ueyama | 4b81ee4 | 2014-01-16 20:57:55 +0000 | [diff] [blame] | 203 | return EC; |
Rui Ueyama | eae4673 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 204 | return std::error_code(); |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // Given a vector of relocations for a section and an offset into this section |
| 208 | // the function returns the name of the symbol used for the relocation at the |
| 209 | // offset. |
Rafael Espindola | 1ad4502 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 210 | static std::error_code resolveSymbolName(const std::vector<RelocationRef> &Rels, |
| 211 | uint64_t Offset, StringRef &Name) { |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 212 | SymbolRef Sym; |
Rafael Espindola | 1ad4502 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 213 | if (std::error_code EC = resolveSymbol(Rels, Offset, Sym)) |
Rui Ueyama | 4b81ee4 | 2014-01-16 20:57:55 +0000 | [diff] [blame] | 214 | return EC; |
Kevin Enderby | 813e0cf | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 215 | Expected<StringRef> NameOrErr = Sym.getName(); |
| 216 | if (!NameOrErr) |
| 217 | return errorToErrorCode(NameOrErr.takeError()); |
Rafael Espindola | 8a80641 | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 218 | Name = *NameOrErr; |
Rui Ueyama | eae4673 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 219 | return std::error_code(); |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static void printCOFFSymbolAddress(llvm::raw_ostream &Out, |
| 223 | const std::vector<RelocationRef> &Rels, |
| 224 | uint64_t Offset, uint32_t Disp) { |
| 225 | StringRef Sym; |
Rui Ueyama | 49c7656 | 2014-02-28 05:21:29 +0000 | [diff] [blame] | 226 | if (!resolveSymbolName(Rels, Offset, Sym)) { |
| 227 | Out << Sym; |
| 228 | if (Disp > 0) |
| 229 | Out << format(" + 0x%04x", Disp); |
| 230 | } else { |
| 231 | Out << format("0x%04x", Disp); |
| 232 | } |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Rui Ueyama | 6fac32b | 2014-02-20 06:51:07 +0000 | [diff] [blame] | 235 | static void |
| 236 | printSEHTable(const COFFObjectFile *Obj, uint32_t TableVA, int Count) { |
| 237 | if (Count == 0) |
| 238 | return; |
| 239 | |
| 240 | const pe32_header *PE32Header; |
Davide Italiano | 281c1b0 | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 241 | error(Obj->getPE32Header(PE32Header)); |
Rui Ueyama | 6fac32b | 2014-02-20 06:51:07 +0000 | [diff] [blame] | 242 | uint32_t ImageBase = PE32Header->ImageBase; |
| 243 | uintptr_t IntPtr = 0; |
Davide Italiano | 281c1b0 | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 244 | error(Obj->getVaPtr(TableVA, IntPtr)); |
Rui Ueyama | 6fac32b | 2014-02-20 06:51:07 +0000 | [diff] [blame] | 245 | const support::ulittle32_t *P = (const support::ulittle32_t *)IntPtr; |
| 246 | outs() << "SEH Table:"; |
| 247 | for (int I = 0; I < Count; ++I) |
| 248 | outs() << format(" 0x%x", P[I] + ImageBase); |
| 249 | outs() << "\n\n"; |
| 250 | } |
| 251 | |
David Majnemer | c533548 | 2016-03-15 06:14:01 +0000 | [diff] [blame] | 252 | template <typename T> |
| 253 | static void printTLSDirectoryT(const coff_tls_directory<T> *TLSDir) { |
| 254 | size_t FormatWidth = sizeof(T) * 2; |
| 255 | outs() << "TLS directory:" |
| 256 | << "\n StartAddressOfRawData: " |
| 257 | << format_hex(TLSDir->StartAddressOfRawData, FormatWidth) |
| 258 | << "\n EndAddressOfRawData: " |
| 259 | << format_hex(TLSDir->EndAddressOfRawData, FormatWidth) |
| 260 | << "\n AddressOfIndex: " |
| 261 | << format_hex(TLSDir->AddressOfIndex, FormatWidth) |
| 262 | << "\n AddressOfCallBacks: " |
| 263 | << format_hex(TLSDir->AddressOfCallBacks, FormatWidth) |
| 264 | << "\n SizeOfZeroFill: " |
| 265 | << TLSDir->SizeOfZeroFill |
| 266 | << "\n Characteristics: " |
| 267 | << TLSDir->Characteristics |
| 268 | << "\n Alignment: " |
| 269 | << TLSDir->getAlignment() |
| 270 | << "\n\n"; |
| 271 | } |
| 272 | |
| 273 | static void printTLSDirectory(const COFFObjectFile *Obj) { |
| 274 | const pe32_header *PE32Header; |
| 275 | error(Obj->getPE32Header(PE32Header)); |
| 276 | |
| 277 | const pe32plus_header *PE32PlusHeader; |
| 278 | error(Obj->getPE32PlusHeader(PE32PlusHeader)); |
| 279 | |
| 280 | // Skip if it's not executable. |
| 281 | if (!PE32Header && !PE32PlusHeader) |
| 282 | return; |
| 283 | |
| 284 | const data_directory *DataDir; |
| 285 | error(Obj->getDataDirectory(COFF::TLS_TABLE, DataDir)); |
| 286 | uintptr_t IntPtr = 0; |
| 287 | if (DataDir->RelativeVirtualAddress == 0) |
| 288 | return; |
| 289 | error(Obj->getRvaPtr(DataDir->RelativeVirtualAddress, IntPtr)); |
| 290 | |
| 291 | if (PE32Header) { |
| 292 | auto *TLSDir = reinterpret_cast<const coff_tls_directory32 *>(IntPtr); |
| 293 | printTLSDirectoryT(TLSDir); |
| 294 | } else { |
| 295 | auto *TLSDir = reinterpret_cast<const coff_tls_directory64 *>(IntPtr); |
| 296 | printTLSDirectoryT(TLSDir); |
| 297 | } |
| 298 | |
| 299 | outs() << "\n"; |
| 300 | } |
| 301 | |
Rui Ueyama | b7e1ab7 | 2014-02-19 03:53:11 +0000 | [diff] [blame] | 302 | static void printLoadConfiguration(const COFFObjectFile *Obj) { |
Rui Ueyama | 6700168 | 2014-02-21 20:27:15 +0000 | [diff] [blame] | 303 | // Skip if it's not executable. |
| 304 | const pe32_header *PE32Header; |
Davide Italiano | 281c1b0 | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 305 | error(Obj->getPE32Header(PE32Header)); |
Rui Ueyama | 6700168 | 2014-02-21 20:27:15 +0000 | [diff] [blame] | 306 | if (!PE32Header) |
| 307 | return; |
| 308 | |
Rui Ueyama | b7e1ab7 | 2014-02-19 03:53:11 +0000 | [diff] [blame] | 309 | // Currently only x86 is supported |
David Majnemer | b3a86a0 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 310 | if (Obj->getMachine() != COFF::IMAGE_FILE_MACHINE_I386) |
Rui Ueyama | b7e1ab7 | 2014-02-19 03:53:11 +0000 | [diff] [blame] | 311 | return; |
| 312 | |
| 313 | const data_directory *DataDir; |
Davide Italiano | 281c1b0 | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 314 | error(Obj->getDataDirectory(COFF::LOAD_CONFIG_TABLE, DataDir)); |
Rui Ueyama | b7e1ab7 | 2014-02-19 03:53:11 +0000 | [diff] [blame] | 315 | uintptr_t IntPtr = 0; |
| 316 | if (DataDir->RelativeVirtualAddress == 0) |
| 317 | return; |
Davide Italiano | 281c1b0 | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 318 | error(Obj->getRvaPtr(DataDir->RelativeVirtualAddress, IntPtr)); |
Rui Ueyama | 6fac32b | 2014-02-20 06:51:07 +0000 | [diff] [blame] | 319 | |
Rui Ueyama | 80f2397 | 2014-03-04 04:15:59 +0000 | [diff] [blame] | 320 | auto *LoadConf = reinterpret_cast<const coff_load_configuration32 *>(IntPtr); |
Rui Ueyama | b7e1ab7 | 2014-02-19 03:53:11 +0000 | [diff] [blame] | 321 | outs() << "Load configuration:" |
| 322 | << "\n Timestamp: " << LoadConf->TimeDateStamp |
| 323 | << "\n Major Version: " << LoadConf->MajorVersion |
| 324 | << "\n Minor Version: " << LoadConf->MinorVersion |
| 325 | << "\n GlobalFlags Clear: " << LoadConf->GlobalFlagsClear |
| 326 | << "\n GlobalFlags Set: " << LoadConf->GlobalFlagsSet |
| 327 | << "\n Critical Section Default Timeout: " << LoadConf->CriticalSectionDefaultTimeout |
| 328 | << "\n Decommit Free Block Threshold: " << LoadConf->DeCommitFreeBlockThreshold |
| 329 | << "\n Decommit Total Free Threshold: " << LoadConf->DeCommitTotalFreeThreshold |
| 330 | << "\n Lock Prefix Table: " << LoadConf->LockPrefixTable |
| 331 | << "\n Maximum Allocation Size: " << LoadConf->MaximumAllocationSize |
| 332 | << "\n Virtual Memory Threshold: " << LoadConf->VirtualMemoryThreshold |
| 333 | << "\n Process Affinity Mask: " << LoadConf->ProcessAffinityMask |
| 334 | << "\n Process Heap Flags: " << LoadConf->ProcessHeapFlags |
| 335 | << "\n CSD Version: " << LoadConf->CSDVersion |
| 336 | << "\n Security Cookie: " << LoadConf->SecurityCookie |
| 337 | << "\n SEH Table: " << LoadConf->SEHandlerTable |
| 338 | << "\n SEH Count: " << LoadConf->SEHandlerCount |
| 339 | << "\n\n"; |
Rui Ueyama | 6fac32b | 2014-02-20 06:51:07 +0000 | [diff] [blame] | 340 | printSEHTable(Obj, LoadConf->SEHandlerTable, LoadConf->SEHandlerCount); |
| 341 | outs() << "\n"; |
Rui Ueyama | b7e1ab7 | 2014-02-19 03:53:11 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Rui Ueyama | a6610ee | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 344 | // Prints import tables. The import table is a table containing the list of |
| 345 | // DLL name and symbol names which will be linked by the loader. |
| 346 | static void printImportTables(const COFFObjectFile *Obj) { |
Rui Ueyama | 4b81ee4 | 2014-01-16 20:57:55 +0000 | [diff] [blame] | 347 | import_directory_iterator I = Obj->import_directory_begin(); |
| 348 | import_directory_iterator E = Obj->import_directory_end(); |
| 349 | if (I == E) |
Rui Ueyama | d12cef8 | 2014-01-15 23:46:18 +0000 | [diff] [blame] | 350 | return; |
Rui Ueyama | a6610ee | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 351 | outs() << "The Import Tables:\n"; |
David Majnemer | 6ea11a4 | 2016-06-26 04:36:32 +0000 | [diff] [blame] | 352 | for (const ImportDirectoryEntryRef &DirRef : Obj->import_directories()) { |
David Majnemer | e2fac43 | 2016-07-31 19:25:21 +0000 | [diff] [blame] | 353 | const coff_import_directory_table_entry *Dir; |
Rui Ueyama | a6610ee | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 354 | StringRef Name; |
David Majnemer | 6ea11a4 | 2016-06-26 04:36:32 +0000 | [diff] [blame] | 355 | if (DirRef.getImportTableEntry(Dir)) return; |
| 356 | if (DirRef.getName(Name)) return; |
Rui Ueyama | a6610ee | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 357 | |
| 358 | outs() << format(" lookup %08x time %08x fwd %08x name %08x addr %08x\n\n", |
| 359 | static_cast<uint32_t>(Dir->ImportLookupTableRVA), |
| 360 | static_cast<uint32_t>(Dir->TimeDateStamp), |
| 361 | static_cast<uint32_t>(Dir->ForwarderChain), |
| 362 | static_cast<uint32_t>(Dir->NameRVA), |
| 363 | static_cast<uint32_t>(Dir->ImportAddressTableRVA)); |
| 364 | outs() << " DLL Name: " << Name << "\n"; |
| 365 | outs() << " Hint/Ord Name\n"; |
David Majnemer | 6ea11a4 | 2016-06-26 04:36:32 +0000 | [diff] [blame] | 366 | for (const ImportedSymbolRef &Entry : DirRef.imported_symbols()) { |
| 367 | bool IsOrdinal; |
| 368 | if (Entry.isOrdinal(IsOrdinal)) |
| 369 | return; |
| 370 | if (IsOrdinal) { |
| 371 | uint16_t Ordinal; |
| 372 | if (Entry.getOrdinal(Ordinal)) |
| 373 | return; |
| 374 | outs() << format(" % 6d\n", Ordinal); |
Rui Ueyama | a6610ee | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 375 | continue; |
| 376 | } |
David Majnemer | 6ea11a4 | 2016-06-26 04:36:32 +0000 | [diff] [blame] | 377 | uint32_t HintNameRVA; |
| 378 | if (Entry.getHintNameRVA(HintNameRVA)) |
| 379 | return; |
Rui Ueyama | a6610ee | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 380 | uint16_t Hint; |
| 381 | StringRef Name; |
David Majnemer | 6ea11a4 | 2016-06-26 04:36:32 +0000 | [diff] [blame] | 382 | if (Obj->getHintName(HintNameRVA, Hint, Name)) |
Rui Ueyama | a6610ee | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 383 | return; |
| 384 | outs() << format(" % 6d ", Hint) << Name << "\n"; |
| 385 | } |
| 386 | outs() << "\n"; |
| 387 | } |
| 388 | } |
| 389 | |
Rui Ueyama | fb432ac | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 390 | // Prints export tables. The export table is a table containing the list of |
| 391 | // exported symbol from the DLL. |
| 392 | static void printExportTable(const COFFObjectFile *Obj) { |
| 393 | outs() << "Export Table:\n"; |
| 394 | export_directory_iterator I = Obj->export_directory_begin(); |
| 395 | export_directory_iterator E = Obj->export_directory_end(); |
| 396 | if (I == E) |
| 397 | return; |
Rui Ueyama | 6010e5d | 2014-01-16 20:50:34 +0000 | [diff] [blame] | 398 | StringRef DllName; |
Rui Ueyama | 9106d36 | 2014-01-17 22:02:24 +0000 | [diff] [blame] | 399 | uint32_t OrdinalBase; |
Rui Ueyama | 6010e5d | 2014-01-16 20:50:34 +0000 | [diff] [blame] | 400 | if (I->getDllName(DllName)) |
| 401 | return; |
Rui Ueyama | 9106d36 | 2014-01-17 22:02:24 +0000 | [diff] [blame] | 402 | if (I->getOrdinalBase(OrdinalBase)) |
| 403 | return; |
Rui Ueyama | 6010e5d | 2014-01-16 20:50:34 +0000 | [diff] [blame] | 404 | outs() << " DLL name: " << DllName << "\n"; |
Rui Ueyama | 9106d36 | 2014-01-17 22:02:24 +0000 | [diff] [blame] | 405 | outs() << " Ordinal base: " << OrdinalBase << "\n"; |
Rui Ueyama | fb432ac | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 406 | outs() << " Ordinal RVA Name\n"; |
Rafael Espindola | efdbec8 | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 407 | for (; I != E; I = ++I) { |
Rui Ueyama | fb432ac | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 408 | uint32_t Ordinal; |
| 409 | if (I->getOrdinal(Ordinal)) |
| 410 | return; |
| 411 | uint32_t RVA; |
| 412 | if (I->getExportRVA(RVA)) |
| 413 | return; |
Rui Ueyama | 68e634a | 2016-01-12 23:28:42 +0000 | [diff] [blame] | 414 | bool IsForwarder; |
| 415 | if (I->isForwarder(IsForwarder)) |
| 416 | return; |
| 417 | |
| 418 | if (IsForwarder) { |
| 419 | // Export table entries can be used to re-export symbols that |
| 420 | // this COFF file is imported from some DLLs. This is rare. |
| 421 | // In most cases IsForwarder is false. |
| 422 | outs() << format(" % 4d ", Ordinal); |
| 423 | } else { |
| 424 | outs() << format(" % 4d %# 8x", Ordinal, RVA); |
| 425 | } |
Rui Ueyama | fb432ac | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 426 | |
| 427 | StringRef Name; |
Rui Ueyama | 6010e5d | 2014-01-16 20:50:34 +0000 | [diff] [blame] | 428 | if (I->getSymbolName(Name)) |
Rui Ueyama | fb432ac | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 429 | continue; |
| 430 | if (!Name.empty()) |
| 431 | outs() << " " << Name; |
Rui Ueyama | 68e634a | 2016-01-12 23:28:42 +0000 | [diff] [blame] | 432 | if (IsForwarder) { |
| 433 | StringRef S; |
| 434 | if (I->getForwardTo(S)) |
| 435 | return; |
| 436 | outs() << " (forwarded to " << S << ")"; |
| 437 | } |
Rui Ueyama | fb432ac | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 438 | outs() << "\n"; |
| 439 | } |
| 440 | } |
| 441 | |
Rui Ueyama | d1a0a58 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 442 | // Given the COFF object file, this function returns the relocations for .pdata |
| 443 | // and the pointer to "runtime function" structs. |
| 444 | static bool getPDataSection(const COFFObjectFile *Obj, |
| 445 | std::vector<RelocationRef> &Rels, |
| 446 | const RuntimeFunction *&RFStart, int &NumRFs) { |
Alexey Samsonov | 5b64579 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 447 | for (const SectionRef &Section : Obj->sections()) { |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 448 | StringRef Name; |
Davide Italiano | 281c1b0 | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 449 | error(Section.getName(Name)); |
Rui Ueyama | d1a0a58 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 450 | if (Name != ".pdata") |
| 451 | continue; |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 452 | |
Alexey Samsonov | 5b64579 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 453 | const coff_section *Pdata = Obj->getCOFFSection(Section); |
| 454 | for (const RelocationRef &Reloc : Section.relocations()) |
Alexey Samsonov | 6f07b35 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 455 | Rels.push_back(Reloc); |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 456 | |
| 457 | // Sort relocations by address. |
George Rimar | 45d116d | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 458 | llvm::sort(Rels, isRelocAddressLess); |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 459 | |
| 460 | ArrayRef<uint8_t> Contents; |
Davide Italiano | 281c1b0 | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 461 | error(Obj->getSectionContents(Pdata, Contents)); |
Rui Ueyama | d1a0a58 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 462 | if (Contents.empty()) |
| 463 | continue; |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 464 | |
Rui Ueyama | d1a0a58 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 465 | RFStart = reinterpret_cast<const RuntimeFunction *>(Contents.data()); |
| 466 | NumRFs = Contents.size() / sizeof(RuntimeFunction); |
| 467 | return true; |
| 468 | } |
| 469 | return false; |
| 470 | } |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 471 | |
Rui Ueyama | b1347c6 | 2014-03-04 03:08:45 +0000 | [diff] [blame] | 472 | static void printWin64EHUnwindInfo(const Win64EH::UnwindInfo *UI) { |
Rui Ueyama | d1a0a58 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 473 | // The casts to int are required in order to output the value as number. |
| 474 | // Without the casts the value would be interpreted as char data (which |
| 475 | // results in garbage output). |
Rui Ueyama | afee151 | 2014-03-04 19:23:56 +0000 | [diff] [blame] | 476 | outs() << " Version: " << static_cast<int>(UI->getVersion()) << "\n"; |
| 477 | outs() << " Flags: " << static_cast<int>(UI->getFlags()); |
Rui Ueyama | d1a0a58 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 478 | if (UI->getFlags()) { |
| 479 | if (UI->getFlags() & UNW_ExceptionHandler) |
| 480 | outs() << " UNW_ExceptionHandler"; |
| 481 | if (UI->getFlags() & UNW_TerminateHandler) |
| 482 | outs() << " UNW_TerminateHandler"; |
| 483 | if (UI->getFlags() & UNW_ChainInfo) |
| 484 | outs() << " UNW_ChainInfo"; |
| 485 | } |
| 486 | outs() << "\n"; |
Rui Ueyama | afee151 | 2014-03-04 19:23:56 +0000 | [diff] [blame] | 487 | outs() << " Size of prolog: " << static_cast<int>(UI->PrologSize) << "\n"; |
| 488 | outs() << " Number of Codes: " << static_cast<int>(UI->NumCodes) << "\n"; |
Rui Ueyama | d1a0a58 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 489 | // Maybe this should move to output of UOP_SetFPReg? |
| 490 | if (UI->getFrameRegister()) { |
Rui Ueyama | afee151 | 2014-03-04 19:23:56 +0000 | [diff] [blame] | 491 | outs() << " Frame register: " |
Rui Ueyama | d1a0a58 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 492 | << getUnwindRegisterName(UI->getFrameRegister()) << "\n"; |
Rui Ueyama | afee151 | 2014-03-04 19:23:56 +0000 | [diff] [blame] | 493 | outs() << " Frame offset: " << 16 * UI->getFrameOffset() << "\n"; |
Rui Ueyama | d1a0a58 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 494 | } else { |
Rui Ueyama | afee151 | 2014-03-04 19:23:56 +0000 | [diff] [blame] | 495 | outs() << " No frame pointer used\n"; |
Rui Ueyama | d1a0a58 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 496 | } |
| 497 | if (UI->getFlags() & (UNW_ExceptionHandler | UNW_TerminateHandler)) { |
| 498 | // FIXME: Output exception handler data |
| 499 | } else if (UI->getFlags() & UNW_ChainInfo) { |
| 500 | // FIXME: Output chained unwind info |
| 501 | } |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 502 | |
Rui Ueyama | d1a0a58 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 503 | if (UI->NumCodes) |
Rui Ueyama | afee151 | 2014-03-04 19:23:56 +0000 | [diff] [blame] | 504 | outs() << " Unwind Codes:\n"; |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 505 | |
Craig Topper | 795a06a | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 506 | printAllUnwindCodes(makeArrayRef(&UI->UnwindCodes[0], UI->NumCodes)); |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 507 | |
Rui Ueyama | afee151 | 2014-03-04 19:23:56 +0000 | [diff] [blame] | 508 | outs() << "\n"; |
Rui Ueyama | d1a0a58 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 509 | outs().flush(); |
| 510 | } |
| 511 | |
Rui Ueyama | cfb73ab | 2014-03-04 04:22:41 +0000 | [diff] [blame] | 512 | /// Prints out the given RuntimeFunction struct for x64, assuming that Obj is |
Rui Ueyama | 3ab0ce0 | 2014-03-04 04:00:55 +0000 | [diff] [blame] | 513 | /// pointing to an executable file. |
Rui Ueyama | b1347c6 | 2014-03-04 03:08:45 +0000 | [diff] [blame] | 514 | static void printRuntimeFunction(const COFFObjectFile *Obj, |
Rui Ueyama | 3ab0ce0 | 2014-03-04 04:00:55 +0000 | [diff] [blame] | 515 | const RuntimeFunction &RF) { |
| 516 | if (!RF.StartAddress) |
| 517 | return; |
| 518 | outs() << "Function Table:\n" |
Rui Ueyama | 1da08cb | 2014-03-05 23:03:37 +0000 | [diff] [blame] | 519 | << format(" Start Address: 0x%04x\n", |
| 520 | static_cast<uint32_t>(RF.StartAddress)) |
| 521 | << format(" End Address: 0x%04x\n", |
| 522 | static_cast<uint32_t>(RF.EndAddress)) |
| 523 | << format(" Unwind Info Address: 0x%04x\n", |
| 524 | static_cast<uint32_t>(RF.UnwindInfoOffset)); |
Rui Ueyama | 3ab0ce0 | 2014-03-04 04:00:55 +0000 | [diff] [blame] | 525 | uintptr_t addr; |
| 526 | if (Obj->getRvaPtr(RF.UnwindInfoOffset, addr)) |
| 527 | return; |
| 528 | printWin64EHUnwindInfo(reinterpret_cast<const Win64EH::UnwindInfo *>(addr)); |
| 529 | } |
| 530 | |
Rui Ueyama | cfb73ab | 2014-03-04 04:22:41 +0000 | [diff] [blame] | 531 | /// Prints out the given RuntimeFunction struct for x64, assuming that Obj is |
| 532 | /// pointing to an object file. Unlike executable, fields in RuntimeFunction |
Rui Ueyama | 3ab0ce0 | 2014-03-04 04:00:55 +0000 | [diff] [blame] | 533 | /// struct are filled with zeros, but instead there are relocations pointing to |
| 534 | /// them so that the linker will fill targets' RVAs to the fields at link |
| 535 | /// time. This function interprets the relocations to find the data to be used |
| 536 | /// in the resulting executable. |
| 537 | static void printRuntimeFunctionRels(const COFFObjectFile *Obj, |
| 538 | const RuntimeFunction &RF, |
| 539 | uint64_t SectionOffset, |
| 540 | const std::vector<RelocationRef> &Rels) { |
Rui Ueyama | b1347c6 | 2014-03-04 03:08:45 +0000 | [diff] [blame] | 541 | outs() << "Function Table:\n"; |
| 542 | outs() << " Start Address: "; |
| 543 | printCOFFSymbolAddress(outs(), Rels, |
| 544 | SectionOffset + |
| 545 | /*offsetof(RuntimeFunction, StartAddress)*/ 0, |
| 546 | RF.StartAddress); |
| 547 | outs() << "\n"; |
| 548 | |
| 549 | outs() << " End Address: "; |
| 550 | printCOFFSymbolAddress(outs(), Rels, |
| 551 | SectionOffset + |
| 552 | /*offsetof(RuntimeFunction, EndAddress)*/ 4, |
| 553 | RF.EndAddress); |
| 554 | outs() << "\n"; |
| 555 | |
| 556 | outs() << " Unwind Info Address: "; |
| 557 | printCOFFSymbolAddress(outs(), Rels, |
| 558 | SectionOffset + |
| 559 | /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8, |
| 560 | RF.UnwindInfoOffset); |
| 561 | outs() << "\n"; |
| 562 | |
| 563 | ArrayRef<uint8_t> XContents; |
| 564 | uint64_t UnwindInfoOffset = 0; |
Davide Italiano | 281c1b0 | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 565 | error(getSectionContents( |
Rui Ueyama | b1347c6 | 2014-03-04 03:08:45 +0000 | [diff] [blame] | 566 | Obj, Rels, SectionOffset + |
| 567 | /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8, |
Davide Italiano | 281c1b0 | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 568 | XContents, UnwindInfoOffset)); |
Rui Ueyama | b1347c6 | 2014-03-04 03:08:45 +0000 | [diff] [blame] | 569 | if (XContents.empty()) |
| 570 | return; |
| 571 | |
| 572 | UnwindInfoOffset += RF.UnwindInfoOffset; |
| 573 | if (UnwindInfoOffset > XContents.size()) |
| 574 | return; |
| 575 | |
Rui Ueyama | 80f2397 | 2014-03-04 04:15:59 +0000 | [diff] [blame] | 576 | auto *UI = reinterpret_cast<const Win64EH::UnwindInfo *>(XContents.data() + |
| 577 | UnwindInfoOffset); |
Rui Ueyama | b1347c6 | 2014-03-04 03:08:45 +0000 | [diff] [blame] | 578 | printWin64EHUnwindInfo(UI); |
| 579 | } |
| 580 | |
Rui Ueyama | d1a0a58 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 581 | void llvm::printCOFFUnwindInfo(const COFFObjectFile *Obj) { |
David Majnemer | b3a86a0 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 582 | if (Obj->getMachine() != COFF::IMAGE_FILE_MACHINE_AMD64) { |
Jonas Devlieghere | 8e8485e | 2018-11-11 22:12:04 +0000 | [diff] [blame] | 583 | WithColor::error(errs(), "llvm-objdump") |
| 584 | << "unsupported image machine type " |
| 585 | "(currently only AMD64 is supported).\n"; |
Rui Ueyama | d1a0a58 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 586 | return; |
| 587 | } |
| 588 | |
| 589 | std::vector<RelocationRef> Rels; |
| 590 | const RuntimeFunction *RFStart; |
| 591 | int NumRFs; |
| 592 | if (!getPDataSection(Obj, Rels, RFStart, NumRFs)) |
| 593 | return; |
| 594 | ArrayRef<RuntimeFunction> RFs(RFStart, NumRFs); |
| 595 | |
Rui Ueyama | 3ab0ce0 | 2014-03-04 04:00:55 +0000 | [diff] [blame] | 596 | bool IsExecutable = Rels.empty(); |
| 597 | if (IsExecutable) { |
| 598 | for (const RuntimeFunction &RF : RFs) |
| 599 | printRuntimeFunction(Obj, RF); |
| 600 | return; |
| 601 | } |
| 602 | |
Rui Ueyama | d1a0a58 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 603 | for (const RuntimeFunction &RF : RFs) { |
| 604 | uint64_t SectionOffset = |
| 605 | std::distance(RFs.begin(), &RF) * sizeof(RuntimeFunction); |
Rui Ueyama | 3ab0ce0 | 2014-03-04 04:00:55 +0000 | [diff] [blame] | 606 | printRuntimeFunctionRels(Obj, RF, SectionOffset, Rels); |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 607 | } |
| 608 | } |
Rui Ueyama | a6610ee | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 609 | |
| 610 | void llvm::printCOFFFileHeader(const object::ObjectFile *Obj) { |
Rui Ueyama | fb432ac | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 611 | const COFFObjectFile *file = dyn_cast<const COFFObjectFile>(Obj); |
David Majnemer | c533548 | 2016-03-15 06:14:01 +0000 | [diff] [blame] | 612 | printTLSDirectory(file); |
Rui Ueyama | b7e1ab7 | 2014-02-19 03:53:11 +0000 | [diff] [blame] | 613 | printLoadConfiguration(file); |
Rui Ueyama | fb432ac | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 614 | printImportTables(file); |
| 615 | printExportTable(file); |
Rui Ueyama | a6610ee | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 616 | } |
Davide Italiano | abe8418 | 2015-12-20 09:54:34 +0000 | [diff] [blame] | 617 | |
Saleem Abdulrasool | 8a83602 | 2016-08-18 16:39:19 +0000 | [diff] [blame] | 618 | void llvm::printCOFFSymbolTable(const object::COFFImportFile *i) { |
| 619 | unsigned Index = 0; |
| 620 | bool IsCode = i->getCOFFImportHeader()->getType() == COFF::IMPORT_CODE; |
| 621 | |
| 622 | for (const object::BasicSymbolRef &Sym : i->symbols()) { |
| 623 | std::string Name; |
| 624 | raw_string_ostream NS(Name); |
| 625 | |
| 626 | Sym.printName(NS); |
| 627 | NS.flush(); |
| 628 | |
| 629 | outs() << "[" << format("%2d", Index) << "]" |
| 630 | << "(sec " << format("%2d", 0) << ")" |
| 631 | << "(fl 0x00)" // Flag bits, which COFF doesn't have. |
| 632 | << "(ty " << format("%3x", (IsCode && Index) ? 32 : 0) << ")" |
| 633 | << "(scl " << format("%3x", 0) << ") " |
| 634 | << "(nx " << 0 << ") " |
| 635 | << "0x" << format("%08x", 0) << " " << Name << '\n'; |
| 636 | |
| 637 | ++Index; |
| 638 | } |
| 639 | } |
| 640 | |
Davide Italiano | abe8418 | 2015-12-20 09:54:34 +0000 | [diff] [blame] | 641 | void llvm::printCOFFSymbolTable(const COFFObjectFile *coff) { |
| 642 | for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) { |
Rafael Espindola | a8a175e | 2017-10-11 17:23:15 +0000 | [diff] [blame] | 643 | Expected<COFFSymbolRef> Symbol = coff->getSymbol(SI); |
Davide Italiano | abe8418 | 2015-12-20 09:54:34 +0000 | [diff] [blame] | 644 | StringRef Name; |
Rafael Espindola | a8a175e | 2017-10-11 17:23:15 +0000 | [diff] [blame] | 645 | error(errorToErrorCode(Symbol.takeError())); |
Davide Italiano | abe8418 | 2015-12-20 09:54:34 +0000 | [diff] [blame] | 646 | error(coff->getSymbolName(*Symbol, Name)); |
| 647 | |
| 648 | outs() << "[" << format("%2d", SI) << "]" |
| 649 | << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")" |
| 650 | << "(fl 0x00)" // Flag bits, which COFF doesn't have. |
| 651 | << "(ty " << format("%3x", unsigned(Symbol->getType())) << ")" |
Zachary Turner | c04ae04 | 2018-08-20 22:18:21 +0000 | [diff] [blame] | 652 | << "(scl " << format("%3x", unsigned(Symbol->getStorageClass())) |
| 653 | << ") " |
Davide Italiano | abe8418 | 2015-12-20 09:54:34 +0000 | [diff] [blame] | 654 | << "(nx " << unsigned(Symbol->getNumberOfAuxSymbols()) << ") " |
| 655 | << "0x" << format("%08x", unsigned(Symbol->getValue())) << " " |
Zachary Turner | c04ae04 | 2018-08-20 22:18:21 +0000 | [diff] [blame] | 656 | << Name; |
| 657 | if (Demangle && Name.startswith("?")) { |
| 658 | char *DemangledSymbol = nullptr; |
| 659 | size_t Size = 0; |
| 660 | int Status = -1; |
| 661 | DemangledSymbol = |
| 662 | microsoftDemangle(Name.data(), DemangledSymbol, &Size, &Status); |
| 663 | |
| 664 | if (Status == 0 && DemangledSymbol) { |
| 665 | outs() << " (" << StringRef(DemangledSymbol) << ")"; |
| 666 | std::free(DemangledSymbol); |
Zachary Turner | ebae6b8 | 2018-08-21 21:23:29 +0000 | [diff] [blame] | 667 | } else { |
| 668 | outs() << " (invalid mangled name)"; |
Zachary Turner | c04ae04 | 2018-08-20 22:18:21 +0000 | [diff] [blame] | 669 | } |
| 670 | } |
| 671 | outs() << "\n"; |
Davide Italiano | abe8418 | 2015-12-20 09:54:34 +0000 | [diff] [blame] | 672 | |
| 673 | for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) { |
| 674 | if (Symbol->isSectionDefinition()) { |
| 675 | const coff_aux_section_definition *asd; |
| 676 | error(coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd)); |
| 677 | |
| 678 | int32_t AuxNumber = asd->getNumber(Symbol->isBigObj()); |
| 679 | |
| 680 | outs() << "AUX " |
| 681 | << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x " |
| 682 | , unsigned(asd->Length) |
| 683 | , unsigned(asd->NumberOfRelocations) |
| 684 | , unsigned(asd->NumberOfLinenumbers) |
| 685 | , unsigned(asd->CheckSum)) |
| 686 | << format("assoc %d comdat %d\n" |
| 687 | , unsigned(AuxNumber) |
| 688 | , unsigned(asd->Selection)); |
| 689 | } else if (Symbol->isFileRecord()) { |
| 690 | const char *FileName; |
| 691 | error(coff->getAuxSymbol<char>(SI + 1, FileName)); |
| 692 | |
| 693 | StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() * |
| 694 | coff->getSymbolTableEntrySize()); |
| 695 | outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n'; |
| 696 | |
| 697 | SI = SI + Symbol->getNumberOfAuxSymbols(); |
| 698 | break; |
Saleem Abdulrasool | 3eda57b | 2016-05-26 01:45:12 +0000 | [diff] [blame] | 699 | } else if (Symbol->isWeakExternal()) { |
| 700 | const coff_aux_weak_external *awe; |
| 701 | error(coff->getAuxSymbol<coff_aux_weak_external>(SI + 1, awe)); |
| 702 | |
| 703 | outs() << "AUX " << format("indx %d srch %d\n", |
| 704 | static_cast<uint32_t>(awe->TagIndex), |
| 705 | static_cast<uint32_t>(awe->Characteristics)); |
Davide Italiano | abe8418 | 2015-12-20 09:54:34 +0000 | [diff] [blame] | 706 | } else { |
| 707 | outs() << "AUX Unknown\n"; |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | } |