llvm.org GIT mirror llvm / f3cd10b
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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@218891 91177308-0d34-0410-b5e6-96231b3b80d8 Rui Ueyama 5 years ago
8 changed file(s) with 68 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
650650 bool operator==(const ImportDirectoryEntryRef &Other) const;
651651 void moveNext();
652652 std::error_code getName(StringRef &Result) const;
653 std::error_code getImportLookupTableRVA(uint32_t &Result) const;
654 std::error_code getImportAddressTableRVA(uint32_t &Result) const;
653655
654656 std::error_code
655657 getImportTableEntry(const import_directory_table_entry *&Result) const;
490490 return object_error::success;
491491
492492 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
493 // -1 because the last entry is the null entry.
493494 NumberOfImportDirectory = DataEntry->Size /
494 sizeof(import_directory_table_entry);
495 sizeof(import_directory_table_entry) - 1;
495496
496497 // Find the section that contains the RVA. This is needed because the RVA is
497498 // the import table's memory address which is different from its file offset.
10281029
10291030 std::error_code ImportDirectoryEntryRef::getImportTableEntry(
10301031 const import_directory_table_entry *&Result) const {
1031 Result = ImportTable;
1032 Result = ImportTable + Index;
10321033 return object_error::success;
10331034 }
10341035
10351036 std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
10361037 uintptr_t IntPtr = 0;
10371038 if (std::error_code EC =
1038 OwningObject->getRvaPtr(ImportTable->NameRVA, IntPtr))
1039 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
10391040 return EC;
10401041 Result = StringRef(reinterpret_cast(IntPtr));
1042 return object_error::success;
1043 }
1044
1045 std::error_code
1046 ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1047 Result = ImportTable[Index].ImportLookupTableRVA;
1048 return object_error::success;
1049 }
1050
1051 std::error_code
1052 ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1053 Result = ImportTable[Index].ImportAddressTableRVA;
10411054 return object_error::success;
10421055 }
10431056
10441057 std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
10451058 const import_lookup_table_entry32 *&Result) const {
10461059 uintptr_t IntPtr = 0;
1047 if (std::error_code EC =
1048 OwningObject->getRvaPtr(ImportTable->ImportLookupTableRVA, IntPtr))
1060 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1061 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
10491062 return EC;
10501063 Result = reinterpret_cast(IntPtr);
10511064 return object_error::success;
0 RUN: llvm-readobj --coff-imports %p/Inputs/imports.exe.coff-i386 | FileCheck -check-prefix=X86 %s
1 RUN: llvm-readobj --coff-imports %p/Inputs/imports.exe.coff-x86-64 | FileCheck -check-prefix=X64 %s
2
3 X86: Import {
4 X86-NEXT: Name: KERNEL32.dll
5 X86-NEXT: ImportLookupTableRVA: 0x204C
6 X86-NEXT: ImportAddressTableRVA: 0x2000
7 X86-NEXT: }
8 X86-NEXT: Import {
9 X86-NEXT: Name: USER32.dll
10 X86-NEXT: ImportLookupTableRVA: 0x2054
11 X86-NEXT: ImportAddressTableRVA: 0x2008
12 X86-NEXT: }
13
14 X64: Import {
15 X64-NEXT: Name: KERNEL32.dll
16 X64-NEXT: ImportLookupTableRVA: 0x2060
17 X64-NEXT: ImportAddressTableRVA: 0x2000
18 X64-NEXT: }
19 X64-NEXT: Import {
20 X64-NEXT: Name: USER32.dll
21 X64-NEXT: ImportLookupTableRVA: 0x2070
22 X64-NEXT: ImportAddressTableRVA: 0x2010
23 X64-NEXT: }
5454 void printSymbols() override;
5555 void printDynamicSymbols() override;
5656 void printUnwindInfo() override;
57 void printCOFFImports() override;
5758
5859 private:
5960 void printSymbol(const SymbolRef &Sym);
881882 }
882883 }
883884
885 void COFFDumper::printCOFFImports() {
886 for (auto I = Obj->import_directory_begin(), E = Obj->import_directory_end();
887 I != E; ++I) {
888 DictScope Import(W, "Import");
889 StringRef Name;
890 if (error(I->getName(Name))) return;
891 W.printString("Name", Name);
892 uint32_t Addr;
893 if (error(I->getImportLookupTableRVA(Addr))) return;
894 W.printHex("ImportLookupTableRVA", Addr);
895 if (error(I->getImportAddressTableRVA(Addr))) return;
896 W.printHex("ImportAddressTableRVA", Addr);
897 }
898 }
4242 // Only implemented for MIPS ELF at this time.
4343 virtual void printMipsPLTGOT() { }
4444
45 // Only implemented for PE/COFF.
46 virtual void printCOFFImports() { }
47
4548 protected:
4649 StreamWriter& W;
4750 };
140140 cl::opt
141141 MipsPLTGOT("mips-plt-got",
142142 cl::desc("Display the MIPS GOT and PLT GOT sections"));
143
144 // -coff-imports
145 cl::opt
146 COFFImports("coff-imports", cl::desc("Display the PE/COFF import table"));
143147 } // namespace opts
144148
145149 static int ReturnValue = EXIT_SUCCESS;
265269 if (isMipsArch(Obj->getArch()) && Obj->isELF())
266270 if (opts::MipsPLTGOT)
267271 Dumper->printMipsPLTGOT();
272 if (opts::COFFImports)
273 Dumper->printCOFFImports();
268274 }
269275
270276