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