2012-12-06 04:12:35 +08:00
|
|
|
//===-- COFFDump.cpp - COFF-specific dumper ---------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief This file implements the COFF-specific dumper for llvm-objdump.
|
|
|
|
/// It outputs the Win64 EH data structures as plain text.
|
2014-01-25 01:20:08 +08:00
|
|
|
/// The encoding of the unwind codes is described in MSDN:
|
2012-12-06 04:12:35 +08:00
|
|
|
/// http://msdn.microsoft.com/en-us/library/ck9asaa9.aspx
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm-objdump.h"
|
|
|
|
#include "llvm/Object/COFF.h"
|
|
|
|
#include "llvm/Object/ObjectFile.h"
|
|
|
|
#include "llvm/Support/Format.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2013-01-02 18:26:28 +08:00
|
|
|
#include "llvm/Support/Win64EH.h"
|
2012-12-06 04:12:35 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstring>
|
2014-06-13 01:38:55 +08:00
|
|
|
#include <system_error>
|
2012-12-06 04:12:35 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace object;
|
|
|
|
using namespace llvm::Win64EH;
|
|
|
|
|
|
|
|
// Returns the name of the unwind code.
|
|
|
|
static StringRef getUnwindCodeTypeName(uint8_t Code) {
|
|
|
|
switch(Code) {
|
|
|
|
default: llvm_unreachable("Invalid unwind code");
|
|
|
|
case UOP_PushNonVol: return "UOP_PushNonVol";
|
|
|
|
case UOP_AllocLarge: return "UOP_AllocLarge";
|
|
|
|
case UOP_AllocSmall: return "UOP_AllocSmall";
|
|
|
|
case UOP_SetFPReg: return "UOP_SetFPReg";
|
|
|
|
case UOP_SaveNonVol: return "UOP_SaveNonVol";
|
|
|
|
case UOP_SaveNonVolBig: return "UOP_SaveNonVolBig";
|
|
|
|
case UOP_SaveXMM128: return "UOP_SaveXMM128";
|
|
|
|
case UOP_SaveXMM128Big: return "UOP_SaveXMM128Big";
|
|
|
|
case UOP_PushMachFrame: return "UOP_PushMachFrame";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the name of a referenced register.
|
|
|
|
static StringRef getUnwindRegisterName(uint8_t Reg) {
|
|
|
|
switch(Reg) {
|
|
|
|
default: llvm_unreachable("Invalid register");
|
|
|
|
case 0: return "RAX";
|
|
|
|
case 1: return "RCX";
|
|
|
|
case 2: return "RDX";
|
|
|
|
case 3: return "RBX";
|
|
|
|
case 4: return "RSP";
|
|
|
|
case 5: return "RBP";
|
|
|
|
case 6: return "RSI";
|
|
|
|
case 7: return "RDI";
|
|
|
|
case 8: return "R8";
|
|
|
|
case 9: return "R9";
|
|
|
|
case 10: return "R10";
|
|
|
|
case 11: return "R11";
|
|
|
|
case 12: return "R12";
|
|
|
|
case 13: return "R13";
|
|
|
|
case 14: return "R14";
|
|
|
|
case 15: return "R15";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculates the number of array slots required for the unwind code.
|
|
|
|
static unsigned getNumUsedSlots(const UnwindCode &UnwindCode) {
|
|
|
|
switch (UnwindCode.getUnwindOp()) {
|
|
|
|
default: llvm_unreachable("Invalid unwind code");
|
|
|
|
case UOP_PushNonVol:
|
|
|
|
case UOP_AllocSmall:
|
|
|
|
case UOP_SetFPReg:
|
|
|
|
case UOP_PushMachFrame:
|
|
|
|
return 1;
|
|
|
|
case UOP_SaveNonVol:
|
|
|
|
case UOP_SaveXMM128:
|
|
|
|
return 2;
|
|
|
|
case UOP_SaveNonVolBig:
|
|
|
|
case UOP_SaveXMM128Big:
|
|
|
|
return 3;
|
|
|
|
case UOP_AllocLarge:
|
|
|
|
return (UnwindCode.getOpInfo() == 0) ? 2 : 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prints one unwind code. Because an unwind code can occupy up to 3 slots in
|
|
|
|
// the unwind codes array, this function requires that the correct number of
|
|
|
|
// slots is provided.
|
|
|
|
static void printUnwindCode(ArrayRef<UnwindCode> UCs) {
|
|
|
|
assert(UCs.size() >= getNumUsedSlots(UCs[0]));
|
2014-03-05 03:23:56 +08:00
|
|
|
outs() << format(" 0x%02x: ", unsigned(UCs[0].u.CodeOffset))
|
2012-12-06 04:12:35 +08:00
|
|
|
<< getUnwindCodeTypeName(UCs[0].getUnwindOp());
|
|
|
|
switch (UCs[0].getUnwindOp()) {
|
|
|
|
case UOP_PushNonVol:
|
|
|
|
outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo());
|
|
|
|
break;
|
|
|
|
case UOP_AllocLarge:
|
|
|
|
if (UCs[0].getOpInfo() == 0) {
|
|
|
|
outs() << " " << UCs[1].FrameOffset;
|
|
|
|
} else {
|
|
|
|
outs() << " " << UCs[1].FrameOffset
|
|
|
|
+ (static_cast<uint32_t>(UCs[2].FrameOffset) << 16);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case UOP_AllocSmall:
|
|
|
|
outs() << " " << ((UCs[0].getOpInfo() + 1) * 8);
|
|
|
|
break;
|
|
|
|
case UOP_SetFPReg:
|
|
|
|
outs() << " ";
|
|
|
|
break;
|
|
|
|
case UOP_SaveNonVol:
|
|
|
|
outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo())
|
|
|
|
<< format(" [0x%04x]", 8 * UCs[1].FrameOffset);
|
|
|
|
break;
|
|
|
|
case UOP_SaveNonVolBig:
|
|
|
|
outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo())
|
|
|
|
<< format(" [0x%08x]", UCs[1].FrameOffset
|
|
|
|
+ (static_cast<uint32_t>(UCs[2].FrameOffset) << 16));
|
|
|
|
break;
|
|
|
|
case UOP_SaveXMM128:
|
|
|
|
outs() << " XMM" << static_cast<uint32_t>(UCs[0].getOpInfo())
|
|
|
|
<< format(" [0x%04x]", 16 * UCs[1].FrameOffset);
|
|
|
|
break;
|
|
|
|
case UOP_SaveXMM128Big:
|
|
|
|
outs() << " XMM" << UCs[0].getOpInfo()
|
|
|
|
<< format(" [0x%08x]", UCs[1].FrameOffset
|
|
|
|
+ (static_cast<uint32_t>(UCs[2].FrameOffset) << 16));
|
|
|
|
break;
|
|
|
|
case UOP_PushMachFrame:
|
|
|
|
outs() << " " << (UCs[0].getOpInfo() ? "w/o" : "w")
|
|
|
|
<< " error code";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
outs() << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printAllUnwindCodes(ArrayRef<UnwindCode> UCs) {
|
|
|
|
for (const UnwindCode *I = UCs.begin(), *E = UCs.end(); I < E; ) {
|
|
|
|
unsigned UsedSlots = getNumUsedSlots(*I);
|
|
|
|
if (UsedSlots > UCs.size()) {
|
|
|
|
outs() << "Unwind data corrupted: Encountered unwind op "
|
|
|
|
<< getUnwindCodeTypeName((*I).getUnwindOp())
|
|
|
|
<< " which requires " << UsedSlots
|
|
|
|
<< " slots, but only " << UCs.size()
|
|
|
|
<< " remaining in buffer";
|
|
|
|
return ;
|
|
|
|
}
|
2015-09-21 13:32:41 +08:00
|
|
|
printUnwindCode(makeArrayRef(I, E));
|
2012-12-06 04:12:35 +08:00
|
|
|
I += UsedSlots;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given a symbol sym this functions returns the address and section of it.
|
2014-06-13 11:07:50 +08:00
|
|
|
static std::error_code
|
|
|
|
resolveSectionAndAddress(const COFFObjectFile *Obj, const SymbolRef &Sym,
|
|
|
|
const coff_section *&ResolvedSection,
|
|
|
|
uint64_t &ResolvedAddr) {
|
2015-07-04 02:19:00 +08:00
|
|
|
ErrorOr<uint64_t> ResolvedAddrOrErr = Sym.getAddress();
|
|
|
|
if (std::error_code EC = ResolvedAddrOrErr.getError())
|
2014-01-17 04:57:55 +08:00
|
|
|
return EC;
|
2015-07-04 02:19:00 +08:00
|
|
|
ResolvedAddr = *ResolvedAddrOrErr;
|
2016-05-03 04:28:12 +08:00
|
|
|
Expected<section_iterator> Iter = Sym.getSection();
|
|
|
|
if (!Iter)
|
|
|
|
return errorToErrorCode(Iter.takeError());
|
2015-08-08 07:27:14 +08:00
|
|
|
ResolvedSection = Obj->getCOFFSection(**Iter);
|
2015-06-09 23:20:42 +08:00
|
|
|
return std::error_code();
|
2012-12-06 04:12:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Given a vector of relocations for a section and an offset into this section
|
|
|
|
// the function returns the symbol used for the relocation at the offset.
|
2014-06-13 11:07:50 +08:00
|
|
|
static std::error_code resolveSymbol(const std::vector<RelocationRef> &Rels,
|
|
|
|
uint64_t Offset, SymbolRef &Sym) {
|
2012-12-06 04:12:35 +08:00
|
|
|
for (std::vector<RelocationRef>::const_iterator I = Rels.begin(),
|
|
|
|
E = Rels.end();
|
|
|
|
I != E; ++I) {
|
2015-06-30 07:29:12 +08:00
|
|
|
uint64_t Ofs = I->getOffset();
|
2012-12-06 04:12:35 +08:00
|
|
|
if (Ofs == Offset) {
|
2013-06-05 09:33:53 +08:00
|
|
|
Sym = *I->getSymbol();
|
2015-06-09 23:20:42 +08:00
|
|
|
return std::error_code();
|
2012-12-06 04:12:35 +08:00
|
|
|
}
|
|
|
|
}
|
2014-02-28 13:21:29 +08:00
|
|
|
return object_error::parse_failed;
|
2012-12-06 04:12:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Given a vector of relocations for a section and an offset into this section
|
|
|
|
// the function resolves the symbol used for the relocation at the offset and
|
|
|
|
// returns the section content and the address inside the content pointed to
|
|
|
|
// by the symbol.
|
2014-06-13 11:07:50 +08:00
|
|
|
static std::error_code
|
|
|
|
getSectionContents(const COFFObjectFile *Obj,
|
|
|
|
const std::vector<RelocationRef> &Rels, uint64_t Offset,
|
|
|
|
ArrayRef<uint8_t> &Contents, uint64_t &Addr) {
|
2012-12-06 04:12:35 +08:00
|
|
|
SymbolRef Sym;
|
2014-06-13 11:07:50 +08:00
|
|
|
if (std::error_code EC = resolveSymbol(Rels, Offset, Sym))
|
2014-02-28 13:21:26 +08:00
|
|
|
return EC;
|
2012-12-06 04:12:35 +08:00
|
|
|
const coff_section *Section;
|
2014-06-13 11:07:50 +08:00
|
|
|
if (std::error_code EC = resolveSectionAndAddress(Obj, Sym, Section, Addr))
|
2014-01-17 04:57:55 +08:00
|
|
|
return EC;
|
2014-06-13 11:07:50 +08:00
|
|
|
if (std::error_code EC = Obj->getSectionContents(Section, Contents))
|
2014-01-17 04:57:55 +08:00
|
|
|
return EC;
|
2015-06-09 23:20:42 +08:00
|
|
|
return std::error_code();
|
2012-12-06 04:12:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Given a vector of relocations for a section and an offset into this section
|
|
|
|
// the function returns the name of the symbol used for the relocation at the
|
|
|
|
// offset.
|
2014-06-13 11:07:50 +08:00
|
|
|
static std::error_code resolveSymbolName(const std::vector<RelocationRef> &Rels,
|
|
|
|
uint64_t Offset, StringRef &Name) {
|
2012-12-06 04:12:35 +08:00
|
|
|
SymbolRef Sym;
|
2014-06-13 11:07:50 +08:00
|
|
|
if (std::error_code EC = resolveSymbol(Rels, Offset, Sym))
|
2014-01-17 04:57:55 +08:00
|
|
|
return EC;
|
2016-04-21 05:24:34 +08:00
|
|
|
Expected<StringRef> NameOrErr = Sym.getName();
|
|
|
|
if (!NameOrErr)
|
|
|
|
return errorToErrorCode(NameOrErr.takeError());
|
2015-07-03 04:55:21 +08:00
|
|
|
Name = *NameOrErr;
|
2015-06-09 23:20:42 +08:00
|
|
|
return std::error_code();
|
2012-12-06 04:12:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void printCOFFSymbolAddress(llvm::raw_ostream &Out,
|
|
|
|
const std::vector<RelocationRef> &Rels,
|
|
|
|
uint64_t Offset, uint32_t Disp) {
|
|
|
|
StringRef Sym;
|
2014-02-28 13:21:29 +08:00
|
|
|
if (!resolveSymbolName(Rels, Offset, Sym)) {
|
|
|
|
Out << Sym;
|
|
|
|
if (Disp > 0)
|
|
|
|
Out << format(" + 0x%04x", Disp);
|
|
|
|
} else {
|
|
|
|
Out << format("0x%04x", Disp);
|
|
|
|
}
|
2012-12-06 04:12:35 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 14:51:07 +08:00
|
|
|
static void
|
|
|
|
printSEHTable(const COFFObjectFile *Obj, uint32_t TableVA, int Count) {
|
|
|
|
if (Count == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const pe32_header *PE32Header;
|
2015-08-05 15:18:31 +08:00
|
|
|
error(Obj->getPE32Header(PE32Header));
|
2014-02-20 14:51:07 +08:00
|
|
|
uint32_t ImageBase = PE32Header->ImageBase;
|
|
|
|
uintptr_t IntPtr = 0;
|
2015-08-05 15:18:31 +08:00
|
|
|
error(Obj->getVaPtr(TableVA, IntPtr));
|
2014-02-20 14:51:07 +08:00
|
|
|
const support::ulittle32_t *P = (const support::ulittle32_t *)IntPtr;
|
|
|
|
outs() << "SEH Table:";
|
|
|
|
for (int I = 0; I < Count; ++I)
|
|
|
|
outs() << format(" 0x%x", P[I] + ImageBase);
|
|
|
|
outs() << "\n\n";
|
|
|
|
}
|
|
|
|
|
2016-03-15 14:14:01 +08:00
|
|
|
template <typename T>
|
|
|
|
static void printTLSDirectoryT(const coff_tls_directory<T> *TLSDir) {
|
|
|
|
size_t FormatWidth = sizeof(T) * 2;
|
|
|
|
outs() << "TLS directory:"
|
|
|
|
<< "\n StartAddressOfRawData: "
|
|
|
|
<< format_hex(TLSDir->StartAddressOfRawData, FormatWidth)
|
|
|
|
<< "\n EndAddressOfRawData: "
|
|
|
|
<< format_hex(TLSDir->EndAddressOfRawData, FormatWidth)
|
|
|
|
<< "\n AddressOfIndex: "
|
|
|
|
<< format_hex(TLSDir->AddressOfIndex, FormatWidth)
|
|
|
|
<< "\n AddressOfCallBacks: "
|
|
|
|
<< format_hex(TLSDir->AddressOfCallBacks, FormatWidth)
|
|
|
|
<< "\n SizeOfZeroFill: "
|
|
|
|
<< TLSDir->SizeOfZeroFill
|
|
|
|
<< "\n Characteristics: "
|
|
|
|
<< TLSDir->Characteristics
|
|
|
|
<< "\n Alignment: "
|
|
|
|
<< TLSDir->getAlignment()
|
|
|
|
<< "\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printTLSDirectory(const COFFObjectFile *Obj) {
|
|
|
|
const pe32_header *PE32Header;
|
|
|
|
error(Obj->getPE32Header(PE32Header));
|
|
|
|
|
|
|
|
const pe32plus_header *PE32PlusHeader;
|
|
|
|
error(Obj->getPE32PlusHeader(PE32PlusHeader));
|
|
|
|
|
|
|
|
// Skip if it's not executable.
|
|
|
|
if (!PE32Header && !PE32PlusHeader)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const data_directory *DataDir;
|
|
|
|
error(Obj->getDataDirectory(COFF::TLS_TABLE, DataDir));
|
|
|
|
uintptr_t IntPtr = 0;
|
|
|
|
if (DataDir->RelativeVirtualAddress == 0)
|
|
|
|
return;
|
|
|
|
error(Obj->getRvaPtr(DataDir->RelativeVirtualAddress, IntPtr));
|
|
|
|
|
|
|
|
if (PE32Header) {
|
|
|
|
auto *TLSDir = reinterpret_cast<const coff_tls_directory32 *>(IntPtr);
|
|
|
|
printTLSDirectoryT(TLSDir);
|
|
|
|
} else {
|
|
|
|
auto *TLSDir = reinterpret_cast<const coff_tls_directory64 *>(IntPtr);
|
|
|
|
printTLSDirectoryT(TLSDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
outs() << "\n";
|
|
|
|
}
|
|
|
|
|
2014-02-19 11:53:11 +08:00
|
|
|
static void printLoadConfiguration(const COFFObjectFile *Obj) {
|
2014-02-22 04:27:15 +08:00
|
|
|
// Skip if it's not executable.
|
|
|
|
const pe32_header *PE32Header;
|
2015-08-05 15:18:31 +08:00
|
|
|
error(Obj->getPE32Header(PE32Header));
|
2014-02-22 04:27:15 +08:00
|
|
|
if (!PE32Header)
|
|
|
|
return;
|
|
|
|
|
2014-02-19 11:53:11 +08:00
|
|
|
// Currently only x86 is supported
|
2014-09-10 20:51:52 +08:00
|
|
|
if (Obj->getMachine() != COFF::IMAGE_FILE_MACHINE_I386)
|
2014-02-19 11:53:11 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
const data_directory *DataDir;
|
2015-08-05 15:18:31 +08:00
|
|
|
error(Obj->getDataDirectory(COFF::LOAD_CONFIG_TABLE, DataDir));
|
2014-02-19 11:53:11 +08:00
|
|
|
uintptr_t IntPtr = 0;
|
|
|
|
if (DataDir->RelativeVirtualAddress == 0)
|
|
|
|
return;
|
2015-08-05 15:18:31 +08:00
|
|
|
error(Obj->getRvaPtr(DataDir->RelativeVirtualAddress, IntPtr));
|
2014-02-20 14:51:07 +08:00
|
|
|
|
2014-03-04 12:15:59 +08:00
|
|
|
auto *LoadConf = reinterpret_cast<const coff_load_configuration32 *>(IntPtr);
|
2014-02-19 11:53:11 +08:00
|
|
|
outs() << "Load configuration:"
|
|
|
|
<< "\n Timestamp: " << LoadConf->TimeDateStamp
|
|
|
|
<< "\n Major Version: " << LoadConf->MajorVersion
|
|
|
|
<< "\n Minor Version: " << LoadConf->MinorVersion
|
|
|
|
<< "\n GlobalFlags Clear: " << LoadConf->GlobalFlagsClear
|
|
|
|
<< "\n GlobalFlags Set: " << LoadConf->GlobalFlagsSet
|
|
|
|
<< "\n Critical Section Default Timeout: " << LoadConf->CriticalSectionDefaultTimeout
|
|
|
|
<< "\n Decommit Free Block Threshold: " << LoadConf->DeCommitFreeBlockThreshold
|
|
|
|
<< "\n Decommit Total Free Threshold: " << LoadConf->DeCommitTotalFreeThreshold
|
|
|
|
<< "\n Lock Prefix Table: " << LoadConf->LockPrefixTable
|
|
|
|
<< "\n Maximum Allocation Size: " << LoadConf->MaximumAllocationSize
|
|
|
|
<< "\n Virtual Memory Threshold: " << LoadConf->VirtualMemoryThreshold
|
|
|
|
<< "\n Process Affinity Mask: " << LoadConf->ProcessAffinityMask
|
|
|
|
<< "\n Process Heap Flags: " << LoadConf->ProcessHeapFlags
|
|
|
|
<< "\n CSD Version: " << LoadConf->CSDVersion
|
|
|
|
<< "\n Security Cookie: " << LoadConf->SecurityCookie
|
|
|
|
<< "\n SEH Table: " << LoadConf->SEHandlerTable
|
|
|
|
<< "\n SEH Count: " << LoadConf->SEHandlerCount
|
|
|
|
<< "\n\n";
|
2014-02-20 14:51:07 +08:00
|
|
|
printSEHTable(Obj, LoadConf->SEHandlerTable, LoadConf->SEHandlerCount);
|
|
|
|
outs() << "\n";
|
2014-02-19 11:53:11 +08:00
|
|
|
}
|
|
|
|
|
2013-09-28 05:04:00 +08:00
|
|
|
// Prints import tables. The import table is a table containing the list of
|
|
|
|
// DLL name and symbol names which will be linked by the loader.
|
|
|
|
static void printImportTables(const COFFObjectFile *Obj) {
|
2014-01-17 04:57:55 +08:00
|
|
|
import_directory_iterator I = Obj->import_directory_begin();
|
|
|
|
import_directory_iterator E = Obj->import_directory_end();
|
|
|
|
if (I == E)
|
2014-01-16 07:46:18 +08:00
|
|
|
return;
|
2013-09-28 05:04:00 +08:00
|
|
|
outs() << "The Import Tables:\n";
|
2014-01-30 10:49:50 +08:00
|
|
|
for (; I != E; I = ++I) {
|
2013-09-28 05:04:00 +08:00
|
|
|
const import_directory_table_entry *Dir;
|
|
|
|
StringRef Name;
|
2014-01-17 04:57:55 +08:00
|
|
|
if (I->getImportTableEntry(Dir)) return;
|
|
|
|
if (I->getName(Name)) return;
|
2013-09-28 05:04:00 +08:00
|
|
|
|
|
|
|
outs() << format(" lookup %08x time %08x fwd %08x name %08x addr %08x\n\n",
|
|
|
|
static_cast<uint32_t>(Dir->ImportLookupTableRVA),
|
|
|
|
static_cast<uint32_t>(Dir->TimeDateStamp),
|
|
|
|
static_cast<uint32_t>(Dir->ForwarderChain),
|
|
|
|
static_cast<uint32_t>(Dir->NameRVA),
|
|
|
|
static_cast<uint32_t>(Dir->ImportAddressTableRVA));
|
|
|
|
outs() << " DLL Name: " << Name << "\n";
|
|
|
|
outs() << " Hint/Ord Name\n";
|
|
|
|
const import_lookup_table_entry32 *entry;
|
2014-01-17 04:57:55 +08:00
|
|
|
if (I->getImportLookupEntry(entry))
|
2013-09-28 05:04:00 +08:00
|
|
|
return;
|
2014-10-03 06:13:44 +08:00
|
|
|
for (; entry->Data; ++entry) {
|
2013-09-28 05:04:00 +08:00
|
|
|
if (entry->isOrdinal()) {
|
|
|
|
outs() << format(" % 6d\n", entry->getOrdinal());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uint16_t Hint;
|
|
|
|
StringRef Name;
|
|
|
|
if (Obj->getHintName(entry->getHintNameRVA(), Hint, Name))
|
|
|
|
return;
|
|
|
|
outs() << format(" % 6d ", Hint) << Name << "\n";
|
|
|
|
}
|
|
|
|
outs() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-16 15:05:49 +08:00
|
|
|
// Prints export tables. The export table is a table containing the list of
|
|
|
|
// exported symbol from the DLL.
|
|
|
|
static void printExportTable(const COFFObjectFile *Obj) {
|
|
|
|
outs() << "Export Table:\n";
|
|
|
|
export_directory_iterator I = Obj->export_directory_begin();
|
|
|
|
export_directory_iterator E = Obj->export_directory_end();
|
|
|
|
if (I == E)
|
|
|
|
return;
|
2014-01-17 04:50:34 +08:00
|
|
|
StringRef DllName;
|
2014-01-18 06:02:24 +08:00
|
|
|
uint32_t OrdinalBase;
|
2014-01-17 04:50:34 +08:00
|
|
|
if (I->getDllName(DllName))
|
|
|
|
return;
|
2014-01-18 06:02:24 +08:00
|
|
|
if (I->getOrdinalBase(OrdinalBase))
|
|
|
|
return;
|
2014-01-17 04:50:34 +08:00
|
|
|
outs() << " DLL name: " << DllName << "\n";
|
2014-01-18 06:02:24 +08:00
|
|
|
outs() << " Ordinal base: " << OrdinalBase << "\n";
|
2014-01-16 15:05:49 +08:00
|
|
|
outs() << " Ordinal RVA Name\n";
|
2014-01-30 10:49:50 +08:00
|
|
|
for (; I != E; I = ++I) {
|
2014-01-16 15:05:49 +08:00
|
|
|
uint32_t Ordinal;
|
|
|
|
if (I->getOrdinal(Ordinal))
|
|
|
|
return;
|
|
|
|
uint32_t RVA;
|
|
|
|
if (I->getExportRVA(RVA))
|
|
|
|
return;
|
2016-01-13 07:28:42 +08:00
|
|
|
bool IsForwarder;
|
|
|
|
if (I->isForwarder(IsForwarder))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (IsForwarder) {
|
|
|
|
// Export table entries can be used to re-export symbols that
|
|
|
|
// this COFF file is imported from some DLLs. This is rare.
|
|
|
|
// In most cases IsForwarder is false.
|
|
|
|
outs() << format(" % 4d ", Ordinal);
|
|
|
|
} else {
|
|
|
|
outs() << format(" % 4d %# 8x", Ordinal, RVA);
|
|
|
|
}
|
2014-01-16 15:05:49 +08:00
|
|
|
|
|
|
|
StringRef Name;
|
2014-01-17 04:50:34 +08:00
|
|
|
if (I->getSymbolName(Name))
|
2014-01-16 15:05:49 +08:00
|
|
|
continue;
|
|
|
|
if (!Name.empty())
|
|
|
|
outs() << " " << Name;
|
2016-01-13 07:28:42 +08:00
|
|
|
if (IsForwarder) {
|
|
|
|
StringRef S;
|
|
|
|
if (I->getForwardTo(S))
|
|
|
|
return;
|
|
|
|
outs() << " (forwarded to " << S << ")";
|
|
|
|
}
|
2014-01-16 15:05:49 +08:00
|
|
|
outs() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-04 08:31:48 +08:00
|
|
|
// Given the COFF object file, this function returns the relocations for .pdata
|
|
|
|
// and the pointer to "runtime function" structs.
|
|
|
|
static bool getPDataSection(const COFFObjectFile *Obj,
|
|
|
|
std::vector<RelocationRef> &Rels,
|
|
|
|
const RuntimeFunction *&RFStart, int &NumRFs) {
|
2014-03-18 14:53:02 +08:00
|
|
|
for (const SectionRef &Section : Obj->sections()) {
|
2012-12-06 04:12:35 +08:00
|
|
|
StringRef Name;
|
2015-08-05 15:18:31 +08:00
|
|
|
error(Section.getName(Name));
|
2014-03-04 08:31:48 +08:00
|
|
|
if (Name != ".pdata")
|
|
|
|
continue;
|
2012-12-06 04:12:35 +08:00
|
|
|
|
2014-03-18 14:53:02 +08:00
|
|
|
const coff_section *Pdata = Obj->getCOFFSection(Section);
|
|
|
|
for (const RelocationRef &Reloc : Section.relocations())
|
2014-03-14 22:22:49 +08:00
|
|
|
Rels.push_back(Reloc);
|
2012-12-06 04:12:35 +08:00
|
|
|
|
|
|
|
// Sort relocations by address.
|
|
|
|
std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
|
|
|
|
|
|
|
|
ArrayRef<uint8_t> Contents;
|
2015-08-05 15:18:31 +08:00
|
|
|
error(Obj->getSectionContents(Pdata, Contents));
|
2014-03-04 08:31:48 +08:00
|
|
|
if (Contents.empty())
|
|
|
|
continue;
|
2012-12-06 04:12:35 +08:00
|
|
|
|
2014-03-04 08:31:48 +08:00
|
|
|
RFStart = reinterpret_cast<const RuntimeFunction *>(Contents.data());
|
|
|
|
NumRFs = Contents.size() / sizeof(RuntimeFunction);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-06 04:12:35 +08:00
|
|
|
|
2014-03-04 11:08:45 +08:00
|
|
|
static void printWin64EHUnwindInfo(const Win64EH::UnwindInfo *UI) {
|
|
|
|
// The casts to int are required in order to output the value as number.
|
|
|
|
// Without the casts the value would be interpreted as char data (which
|
|
|
|
// results in garbage output).
|
2014-03-05 03:23:56 +08:00
|
|
|
outs() << " Version: " << static_cast<int>(UI->getVersion()) << "\n";
|
|
|
|
outs() << " Flags: " << static_cast<int>(UI->getFlags());
|
2014-03-04 11:08:45 +08:00
|
|
|
if (UI->getFlags()) {
|
|
|
|
if (UI->getFlags() & UNW_ExceptionHandler)
|
|
|
|
outs() << " UNW_ExceptionHandler";
|
|
|
|
if (UI->getFlags() & UNW_TerminateHandler)
|
|
|
|
outs() << " UNW_TerminateHandler";
|
|
|
|
if (UI->getFlags() & UNW_ChainInfo)
|
|
|
|
outs() << " UNW_ChainInfo";
|
|
|
|
}
|
|
|
|
outs() << "\n";
|
2014-03-05 03:23:56 +08:00
|
|
|
outs() << " Size of prolog: " << static_cast<int>(UI->PrologSize) << "\n";
|
|
|
|
outs() << " Number of Codes: " << static_cast<int>(UI->NumCodes) << "\n";
|
2014-03-04 11:08:45 +08:00
|
|
|
// Maybe this should move to output of UOP_SetFPReg?
|
|
|
|
if (UI->getFrameRegister()) {
|
2014-03-05 03:23:56 +08:00
|
|
|
outs() << " Frame register: "
|
2014-03-04 11:08:45 +08:00
|
|
|
<< getUnwindRegisterName(UI->getFrameRegister()) << "\n";
|
2014-03-05 03:23:56 +08:00
|
|
|
outs() << " Frame offset: " << 16 * UI->getFrameOffset() << "\n";
|
2014-03-04 11:08:45 +08:00
|
|
|
} else {
|
2014-03-05 03:23:56 +08:00
|
|
|
outs() << " No frame pointer used\n";
|
2014-03-04 11:08:45 +08:00
|
|
|
}
|
|
|
|
if (UI->getFlags() & (UNW_ExceptionHandler | UNW_TerminateHandler)) {
|
|
|
|
// FIXME: Output exception handler data
|
|
|
|
} else if (UI->getFlags() & UNW_ChainInfo) {
|
|
|
|
// FIXME: Output chained unwind info
|
|
|
|
}
|
|
|
|
|
|
|
|
if (UI->NumCodes)
|
2014-03-05 03:23:56 +08:00
|
|
|
outs() << " Unwind Codes:\n";
|
2014-03-04 11:08:45 +08:00
|
|
|
|
2015-09-21 13:32:41 +08:00
|
|
|
printAllUnwindCodes(makeArrayRef(&UI->UnwindCodes[0], UI->NumCodes));
|
2014-03-04 11:08:45 +08:00
|
|
|
|
2014-03-05 03:23:56 +08:00
|
|
|
outs() << "\n";
|
2014-03-04 11:08:45 +08:00
|
|
|
outs().flush();
|
|
|
|
}
|
|
|
|
|
2014-03-04 12:22:41 +08:00
|
|
|
/// Prints out the given RuntimeFunction struct for x64, assuming that Obj is
|
2014-03-04 12:00:55 +08:00
|
|
|
/// pointing to an executable file.
|
2014-03-04 08:31:48 +08:00
|
|
|
static void printRuntimeFunction(const COFFObjectFile *Obj,
|
2014-03-04 12:00:55 +08:00
|
|
|
const RuntimeFunction &RF) {
|
|
|
|
if (!RF.StartAddress)
|
|
|
|
return;
|
|
|
|
outs() << "Function Table:\n"
|
2014-03-06 07:03:37 +08:00
|
|
|
<< format(" Start Address: 0x%04x\n",
|
|
|
|
static_cast<uint32_t>(RF.StartAddress))
|
|
|
|
<< format(" End Address: 0x%04x\n",
|
|
|
|
static_cast<uint32_t>(RF.EndAddress))
|
|
|
|
<< format(" Unwind Info Address: 0x%04x\n",
|
|
|
|
static_cast<uint32_t>(RF.UnwindInfoOffset));
|
2014-03-04 12:00:55 +08:00
|
|
|
uintptr_t addr;
|
|
|
|
if (Obj->getRvaPtr(RF.UnwindInfoOffset, addr))
|
|
|
|
return;
|
|
|
|
printWin64EHUnwindInfo(reinterpret_cast<const Win64EH::UnwindInfo *>(addr));
|
|
|
|
}
|
|
|
|
|
2014-03-04 12:22:41 +08:00
|
|
|
/// Prints out the given RuntimeFunction struct for x64, assuming that Obj is
|
|
|
|
/// pointing to an object file. Unlike executable, fields in RuntimeFunction
|
2014-03-04 12:00:55 +08:00
|
|
|
/// struct are filled with zeros, but instead there are relocations pointing to
|
|
|
|
/// them so that the linker will fill targets' RVAs to the fields at link
|
|
|
|
/// time. This function interprets the relocations to find the data to be used
|
|
|
|
/// in the resulting executable.
|
|
|
|
static void printRuntimeFunctionRels(const COFFObjectFile *Obj,
|
|
|
|
const RuntimeFunction &RF,
|
|
|
|
uint64_t SectionOffset,
|
|
|
|
const std::vector<RelocationRef> &Rels) {
|
2014-03-04 08:31:48 +08:00
|
|
|
outs() << "Function Table:\n";
|
|
|
|
outs() << " Start Address: ";
|
|
|
|
printCOFFSymbolAddress(outs(), Rels,
|
|
|
|
SectionOffset +
|
|
|
|
/*offsetof(RuntimeFunction, StartAddress)*/ 0,
|
|
|
|
RF.StartAddress);
|
|
|
|
outs() << "\n";
|
2012-12-06 04:12:35 +08:00
|
|
|
|
2014-03-04 08:31:48 +08:00
|
|
|
outs() << " End Address: ";
|
|
|
|
printCOFFSymbolAddress(outs(), Rels,
|
|
|
|
SectionOffset +
|
|
|
|
/*offsetof(RuntimeFunction, EndAddress)*/ 4,
|
|
|
|
RF.EndAddress);
|
|
|
|
outs() << "\n";
|
2012-12-06 04:12:35 +08:00
|
|
|
|
2014-03-04 08:31:48 +08:00
|
|
|
outs() << " Unwind Info Address: ";
|
|
|
|
printCOFFSymbolAddress(outs(), Rels,
|
|
|
|
SectionOffset +
|
|
|
|
/*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8,
|
|
|
|
RF.UnwindInfoOffset);
|
|
|
|
outs() << "\n";
|
|
|
|
|
|
|
|
ArrayRef<uint8_t> XContents;
|
|
|
|
uint64_t UnwindInfoOffset = 0;
|
2015-08-05 15:18:31 +08:00
|
|
|
error(getSectionContents(
|
2014-03-04 08:31:48 +08:00
|
|
|
Obj, Rels, SectionOffset +
|
|
|
|
/*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8,
|
2015-08-05 15:18:31 +08:00
|
|
|
XContents, UnwindInfoOffset));
|
2014-03-04 08:31:48 +08:00
|
|
|
if (XContents.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
UnwindInfoOffset += RF.UnwindInfoOffset;
|
|
|
|
if (UnwindInfoOffset > XContents.size())
|
|
|
|
return;
|
|
|
|
|
2014-03-04 12:15:59 +08:00
|
|
|
auto *UI = reinterpret_cast<const Win64EH::UnwindInfo *>(XContents.data() +
|
|
|
|
UnwindInfoOffset);
|
2014-03-04 11:08:45 +08:00
|
|
|
printWin64EHUnwindInfo(UI);
|
2014-03-04 08:31:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void llvm::printCOFFUnwindInfo(const COFFObjectFile *Obj) {
|
2014-09-10 20:51:52 +08:00
|
|
|
if (Obj->getMachine() != COFF::IMAGE_FILE_MACHINE_AMD64) {
|
2014-03-04 08:31:48 +08:00
|
|
|
errs() << "Unsupported image machine type "
|
|
|
|
"(currently only AMD64 is supported).\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<RelocationRef> Rels;
|
|
|
|
const RuntimeFunction *RFStart;
|
|
|
|
int NumRFs;
|
|
|
|
if (!getPDataSection(Obj, Rels, RFStart, NumRFs))
|
|
|
|
return;
|
|
|
|
ArrayRef<RuntimeFunction> RFs(RFStart, NumRFs);
|
|
|
|
|
2014-03-04 12:00:55 +08:00
|
|
|
bool IsExecutable = Rels.empty();
|
|
|
|
if (IsExecutable) {
|
|
|
|
for (const RuntimeFunction &RF : RFs)
|
|
|
|
printRuntimeFunction(Obj, RF);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-04 08:31:48 +08:00
|
|
|
for (const RuntimeFunction &RF : RFs) {
|
|
|
|
uint64_t SectionOffset =
|
|
|
|
std::distance(RFs.begin(), &RF) * sizeof(RuntimeFunction);
|
2014-03-04 12:00:55 +08:00
|
|
|
printRuntimeFunctionRels(Obj, RF, SectionOffset, Rels);
|
2012-12-06 04:12:35 +08:00
|
|
|
}
|
|
|
|
}
|
2013-09-28 05:04:00 +08:00
|
|
|
|
|
|
|
void llvm::printCOFFFileHeader(const object::ObjectFile *Obj) {
|
2014-01-16 15:05:49 +08:00
|
|
|
const COFFObjectFile *file = dyn_cast<const COFFObjectFile>(Obj);
|
2016-03-15 14:14:01 +08:00
|
|
|
printTLSDirectory(file);
|
2014-02-19 11:53:11 +08:00
|
|
|
printLoadConfiguration(file);
|
2014-01-16 15:05:49 +08:00
|
|
|
printImportTables(file);
|
|
|
|
printExportTable(file);
|
2013-09-28 05:04:00 +08:00
|
|
|
}
|
2015-12-20 17:54:34 +08:00
|
|
|
|
|
|
|
void llvm::printCOFFSymbolTable(const COFFObjectFile *coff) {
|
|
|
|
for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) {
|
|
|
|
ErrorOr<COFFSymbolRef> Symbol = coff->getSymbol(SI);
|
|
|
|
StringRef Name;
|
|
|
|
error(Symbol.getError());
|
|
|
|
error(coff->getSymbolName(*Symbol, Name));
|
|
|
|
|
|
|
|
outs() << "[" << format("%2d", SI) << "]"
|
|
|
|
<< "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")"
|
|
|
|
<< "(fl 0x00)" // Flag bits, which COFF doesn't have.
|
|
|
|
<< "(ty " << format("%3x", unsigned(Symbol->getType())) << ")"
|
|
|
|
<< "(scl " << format("%3x", unsigned(Symbol->getStorageClass())) << ") "
|
|
|
|
<< "(nx " << unsigned(Symbol->getNumberOfAuxSymbols()) << ") "
|
|
|
|
<< "0x" << format("%08x", unsigned(Symbol->getValue())) << " "
|
|
|
|
<< Name << "\n";
|
|
|
|
|
|
|
|
for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) {
|
|
|
|
if (Symbol->isSectionDefinition()) {
|
|
|
|
const coff_aux_section_definition *asd;
|
|
|
|
error(coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd));
|
|
|
|
|
|
|
|
int32_t AuxNumber = asd->getNumber(Symbol->isBigObj());
|
|
|
|
|
|
|
|
outs() << "AUX "
|
|
|
|
<< format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
|
|
|
|
, unsigned(asd->Length)
|
|
|
|
, unsigned(asd->NumberOfRelocations)
|
|
|
|
, unsigned(asd->NumberOfLinenumbers)
|
|
|
|
, unsigned(asd->CheckSum))
|
|
|
|
<< format("assoc %d comdat %d\n"
|
|
|
|
, unsigned(AuxNumber)
|
|
|
|
, unsigned(asd->Selection));
|
|
|
|
} else if (Symbol->isFileRecord()) {
|
|
|
|
const char *FileName;
|
|
|
|
error(coff->getAuxSymbol<char>(SI + 1, FileName));
|
|
|
|
|
|
|
|
StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() *
|
|
|
|
coff->getSymbolTableEntrySize());
|
|
|
|
outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n';
|
|
|
|
|
|
|
|
SI = SI + Symbol->getNumberOfAuxSymbols();
|
|
|
|
break;
|
2016-05-26 09:45:12 +08:00
|
|
|
} else if (Symbol->isWeakExternal()) {
|
|
|
|
const coff_aux_weak_external *awe;
|
|
|
|
error(coff->getAuxSymbol<coff_aux_weak_external>(SI + 1, awe));
|
|
|
|
|
|
|
|
outs() << "AUX " << format("indx %d srch %d\n",
|
|
|
|
static_cast<uint32_t>(awe->TagIndex),
|
|
|
|
static_cast<uint32_t>(awe->Characteristics));
|
2015-12-20 17:54:34 +08:00
|
|
|
} else {
|
|
|
|
outs() << "AUX Unknown\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|