[llvm] r218891 - This patch adds a new flag "-coff-imports" to llvm-readobj.
Rui Ueyama
ruiu at google.com
Thu Oct 2 10:02:18 PDT 2014
Author: ruiu
Date: Thu Oct 2 12:02:18 2014
New Revision: 218891
URL: http://llvm.org/viewvc/llvm-project?rev=218891&view=rev
Log:
This patch adds a new flag "-coff-imports" to llvm-readobj.
When the flag is given, the command prints out the COFF import table.
Currently only the import table directory will be printed.
I'm going to make another patch to print out the imported symbols.
The implementation of import directory entry iterator in
COFFObjectFile.cpp was buggy. This patch fixes that too.
http://reviews.llvm.org/D5569
Added:
llvm/trunk/test/tools/llvm-readobj/Inputs/imports.exe.coff-i386 (with props)
llvm/trunk/test/tools/llvm-readobj/Inputs/imports.exe.coff-x86-64 (with props)
llvm/trunk/test/tools/llvm-readobj/imports.test
Modified:
llvm/trunk/include/llvm/Object/COFF.h
llvm/trunk/lib/Object/COFFObjectFile.cpp
llvm/trunk/tools/llvm-readobj/COFFDumper.cpp
llvm/trunk/tools/llvm-readobj/ObjDumper.h
llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp
Modified: llvm/trunk/include/llvm/Object/COFF.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/COFF.h?rev=218891&r1=218890&r2=218891&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/COFF.h (original)
+++ llvm/trunk/include/llvm/Object/COFF.h Thu Oct 2 12:02:18 2014
@@ -651,6 +651,8 @@ public:
bool operator==(const ImportDirectoryEntryRef &Other) const;
void moveNext();
std::error_code getName(StringRef &Result) const;
+ std::error_code getImportLookupTableRVA(uint32_t &Result) const;
+ std::error_code getImportAddressTableRVA(uint32_t &Result) const;
std::error_code
getImportTableEntry(const import_directory_table_entry *&Result) const;
Modified: llvm/trunk/lib/Object/COFFObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/COFFObjectFile.cpp?rev=218891&r1=218890&r2=218891&view=diff
==============================================================================
--- llvm/trunk/lib/Object/COFFObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/COFFObjectFile.cpp Thu Oct 2 12:02:18 2014
@@ -491,8 +491,9 @@ std::error_code COFFObjectFile::initImpo
return object_error::success;
uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
+ // -1 because the last entry is the null entry.
NumberOfImportDirectory = DataEntry->Size /
- sizeof(import_directory_table_entry);
+ sizeof(import_directory_table_entry) - 1;
// Find the section that contains the RVA. This is needed because the RVA is
// the import table's memory address which is different from its file offset.
@@ -1029,24 +1030,36 @@ void ImportDirectoryEntryRef::moveNext()
std::error_code ImportDirectoryEntryRef::getImportTableEntry(
const import_directory_table_entry *&Result) const {
- Result = ImportTable;
+ Result = ImportTable + Index;
return object_error::success;
}
std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
uintptr_t IntPtr = 0;
if (std::error_code EC =
- OwningObject->getRvaPtr(ImportTable->NameRVA, IntPtr))
+ OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
return EC;
Result = StringRef(reinterpret_cast<const char *>(IntPtr));
return object_error::success;
}
+std::error_code
+ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
+ Result = ImportTable[Index].ImportLookupTableRVA;
+ return object_error::success;
+}
+
+std::error_code
+ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
+ Result = ImportTable[Index].ImportAddressTableRVA;
+ return object_error::success;
+}
+
std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
const import_lookup_table_entry32 *&Result) const {
uintptr_t IntPtr = 0;
- if (std::error_code EC =
- OwningObject->getRvaPtr(ImportTable->ImportLookupTableRVA, IntPtr))
+ uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
+ if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
return EC;
Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
return object_error::success;
Added: llvm/trunk/test/tools/llvm-readobj/Inputs/imports.exe.coff-i386
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-readobj/Inputs/imports.exe.coff-i386?rev=218891&view=auto
==============================================================================
Binary file - no diff available.
Propchange: llvm/trunk/test/tools/llvm-readobj/Inputs/imports.exe.coff-i386
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream
Added: llvm/trunk/test/tools/llvm-readobj/Inputs/imports.exe.coff-x86-64
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-readobj/Inputs/imports.exe.coff-x86-64?rev=218891&view=auto
==============================================================================
Binary file - no diff available.
Propchange: llvm/trunk/test/tools/llvm-readobj/Inputs/imports.exe.coff-x86-64
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream
Added: llvm/trunk/test/tools/llvm-readobj/imports.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-readobj/imports.test?rev=218891&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-readobj/imports.test (added)
+++ llvm/trunk/test/tools/llvm-readobj/imports.test Thu Oct 2 12:02:18 2014
@@ -0,0 +1,24 @@
+RUN: llvm-readobj --coff-imports %p/Inputs/imports.exe.coff-i386 | FileCheck -check-prefix=X86 %s
+RUN: llvm-readobj --coff-imports %p/Inputs/imports.exe.coff-x86-64 | FileCheck -check-prefix=X64 %s
+
+X86: Import {
+X86-NEXT: Name: KERNEL32.dll
+X86-NEXT: ImportLookupTableRVA: 0x204C
+X86-NEXT: ImportAddressTableRVA: 0x2000
+X86-NEXT: }
+X86-NEXT: Import {
+X86-NEXT: Name: USER32.dll
+X86-NEXT: ImportLookupTableRVA: 0x2054
+X86-NEXT: ImportAddressTableRVA: 0x2008
+X86-NEXT: }
+
+X64: Import {
+X64-NEXT: Name: KERNEL32.dll
+X64-NEXT: ImportLookupTableRVA: 0x2060
+X64-NEXT: ImportAddressTableRVA: 0x2000
+X64-NEXT: }
+X64-NEXT: Import {
+X64-NEXT: Name: USER32.dll
+X64-NEXT: ImportLookupTableRVA: 0x2070
+X64-NEXT: ImportAddressTableRVA: 0x2010
+X64-NEXT: }
Modified: llvm/trunk/tools/llvm-readobj/COFFDumper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/COFFDumper.cpp?rev=218891&r1=218890&r2=218891&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/COFFDumper.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/COFFDumper.cpp Thu Oct 2 12:02:18 2014
@@ -55,6 +55,7 @@ public:
void printSymbols() override;
void printDynamicSymbols() override;
void printUnwindInfo() override;
+ void printCOFFImports() override;
private:
void printSymbol(const SymbolRef &Sym);
@@ -882,3 +883,17 @@ void COFFDumper::printUnwindInfo() {
}
}
+void COFFDumper::printCOFFImports() {
+ for (auto I = Obj->import_directory_begin(), E = Obj->import_directory_end();
+ I != E; ++I) {
+ DictScope Import(W, "Import");
+ StringRef Name;
+ if (error(I->getName(Name))) return;
+ W.printString("Name", Name);
+ uint32_t Addr;
+ if (error(I->getImportLookupTableRVA(Addr))) return;
+ W.printHex("ImportLookupTableRVA", Addr);
+ if (error(I->getImportAddressTableRVA(Addr))) return;
+ W.printHex("ImportAddressTableRVA", Addr);
+ }
+}
Modified: llvm/trunk/tools/llvm-readobj/ObjDumper.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/ObjDumper.h?rev=218891&r1=218890&r2=218891&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/ObjDumper.h (original)
+++ llvm/trunk/tools/llvm-readobj/ObjDumper.h Thu Oct 2 12:02:18 2014
@@ -43,6 +43,9 @@ public:
// Only implemented for MIPS ELF at this time.
virtual void printMipsPLTGOT() { }
+ // Only implemented for PE/COFF.
+ virtual void printCOFFImports() { }
+
protected:
StreamWriter& W;
};
Modified: llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp?rev=218891&r1=218890&r2=218891&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp Thu Oct 2 12:02:18 2014
@@ -141,6 +141,10 @@ namespace opts {
cl::opt<bool>
MipsPLTGOT("mips-plt-got",
cl::desc("Display the MIPS GOT and PLT GOT sections"));
+
+ // -coff-imports
+ cl::opt<bool>
+ COFFImports("coff-imports", cl::desc("Display the PE/COFF import table"));
} // namespace opts
static int ReturnValue = EXIT_SUCCESS;
@@ -266,6 +270,8 @@ static void dumpObject(const ObjectFile
if (isMipsArch(Obj->getArch()) && Obj->isELF())
if (opts::MipsPLTGOT)
Dumper->printMipsPLTGOT();
+ if (opts::COFFImports)
+ Dumper->printCOFFImports();
}
More information about the llvm-commits
mailing list