namespace object {
class ImportDirectoryEntryRef;
+class ExportDirectoryEntryRef;
typedef content_iterator<ImportDirectoryEntryRef> import_directory_iterator;
+typedef content_iterator<ExportDirectoryEntryRef> export_directory_iterator;
/// The DOS compatible header at the front of all PE/COFF executables.
struct dos_header {
class COFFObjectFile : public ObjectFile {
private:
friend class ImportDirectoryEntryRef;
+ friend class ExportDirectoryEntryRef;
const coff_file_header *COFFHeader;
const pe32_header *PE32Header;
const data_directory *DataDirectory;
uint32_t StringTableSize;
const import_directory_table_entry *ImportDirectory;
uint32_t NumberOfImportDirectory;
+ const export_directory_table_entry *ExportDirectory;
error_code getString(uint32_t offset, StringRef &Res) const;
error_code initSymbolTablePtr();
error_code initImportTablePtr();
+ error_code initExportTablePtr();
protected:
virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
import_directory_iterator import_directory_begin() const;
import_directory_iterator import_directory_end() const;
+ export_directory_iterator export_directory_begin() const;
+ export_directory_iterator export_directory_end() const;
error_code getHeader(const coff_file_header *&Res) const;
error_code getCOFFHeader(const coff_file_header *&Res) const;
uint32_t Index;
const COFFObjectFile *OwningObject;
};
+
+// The iterator for the export directory table entry.
+class ExportDirectoryEntryRef {
+public:
+ ExportDirectoryEntryRef() : OwningObject(0) {}
+ ExportDirectoryEntryRef(const export_directory_table_entry *Table, uint32_t I,
+ const COFFObjectFile *Owner)
+ : ExportTable(Table), Index(I), OwningObject(Owner) {}
+
+ bool operator==(const ExportDirectoryEntryRef &Other) const;
+ error_code getNext(ExportDirectoryEntryRef &Result) const;
+ error_code getOrdinal(uint32_t &Result) const;
+ error_code getExportRVA(uint32_t &Result) const;
+ error_code getName(StringRef &Result) const;
+
+private:
+ const export_directory_table_entry *ExportTable;
+ uint32_t Index;
+ const COFFObjectFile *OwningObject;
+};
} // end namespace object
} // end namespace llvm
return ec;
ImportDirectory = reinterpret_cast<
const import_directory_table_entry *>(IntPtr);
+ return object_error::success;
+}
- // It's an error if there's no section containing the Import Table RVA.
- return object_error::parse_failed;
+// Find the export table.
+error_code COFFObjectFile::initExportTablePtr() {
+ // First, we get the RVA of the export table. If the file lacks a pointer to
+ // the export table, do nothing.
+ const data_directory *DataEntry;
+ if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
+ return object_error::success;
+
+ // Do nothing if the pointer to export table is NULL.
+ if (DataEntry->RelativeVirtualAddress == 0)
+ return object_error::success;
+
+ uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
+ uintptr_t IntPtr = 0;
+ if (error_code EC = getRvaPtr(ExportTableRva, IntPtr))
+ return EC;
+ ExportDirectory = reinterpret_cast<const export_directory_table_entry *>(IntPtr);
+ return object_error::success;
}
COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
, StringTable(0)
, StringTableSize(0)
, ImportDirectory(0)
- , NumberOfImportDirectory(0) {
+ , NumberOfImportDirectory(0)
+ , ExportDirectory(0) {
// Check that we at least have enough room for a header.
if (!checkSize(Data, ec, sizeof(coff_file_header))) return;
if ((ec = initImportTablePtr()))
return;
+ // Initialize the pointer to the export table.
+ if ((ec = initExportTablePtr()))
+ return;
+
ec = object_error::success;
}
ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
}
+export_directory_iterator COFFObjectFile::export_directory_begin() const {
+ return export_directory_iterator(
+ ExportDirectoryEntryRef(ExportDirectory, 0, this));
+}
+
+export_directory_iterator COFFObjectFile::export_directory_end() const {
+ if (ExportDirectory == 0)
+ return export_directory_iterator(ExportDirectoryEntryRef(0, 0, this));
+ ExportDirectoryEntryRef ref(ExportDirectory,
+ ExportDirectory->AddressTableEntries, this);
+ return export_directory_iterator(ref);
+}
+
section_iterator COFFObjectFile::begin_sections() const {
DataRefImpl ret;
ret.p = reinterpret_cast<uintptr_t>(SectionTable);
return object_error::success;
}
+bool ExportDirectoryEntryRef::
+operator==(const ExportDirectoryEntryRef &Other) const {
+ return ExportTable == Other.ExportTable && Index == Other.Index;
+}
+
+error_code
+ExportDirectoryEntryRef::getNext(ExportDirectoryEntryRef &Result) const {
+ Result = ExportDirectoryEntryRef(ExportTable, Index + 1, OwningObject);
+ return object_error::success;
+}
+
+// Returns the export ordinal of the current export symbol.
+error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
+ Result = ExportTable->OrdinalBase + Index;
+ return object_error::success;
+}
+
+// Returns the address of the current export symbol.
+error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
+ uintptr_t IntPtr = 0;
+ if (error_code EC = OwningObject->getRvaPtr(
+ ExportTable->ExportAddressTableRVA, IntPtr))
+ return EC;
+ const export_address_table_entry *entry = reinterpret_cast<const export_address_table_entry *>(IntPtr);
+ Result = entry[Index].ExportRVA;
+ return object_error::success;
+}
+
+// Returns the name of the current export symbol. If the symbol is exported only
+// by ordinal, the empty string is set as a result.
+error_code ExportDirectoryEntryRef::getName(StringRef &Result) const {
+ uintptr_t IntPtr = 0;
+ if (error_code EC = OwningObject->getRvaPtr(
+ ExportTable->OrdinalTableRVA, IntPtr))
+ return EC;
+ const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
+
+ uint32_t NumEntries = ExportTable->NumberOfNamePointers;
+ int Offset = 0;
+ for (const ulittle16_t *I = Start, *E = Start + NumEntries;
+ I < E; ++I, ++Offset) {
+ if (*I != Index)
+ continue;
+ if (error_code EC = OwningObject->getRvaPtr(
+ ExportTable->NamePointerRVA, IntPtr))
+ return EC;
+ const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
+ if (error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
+ return EC;
+ Result = StringRef(reinterpret_cast<const char *>(IntPtr));
+ return object_error::success;
+ }
+ Result = "";
+ return object_error::success;
+}
+
namespace llvm {
ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
error_code ec;
}
}
+// 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;
+ outs() << " Ordinal RVA Name\n";
+ error_code EC;
+ for (; I != E; I = I.increment(EC)) {
+ if (EC)
+ return;
+ uint32_t Ordinal;
+ if (I->getOrdinal(Ordinal))
+ return;
+ uint32_t RVA;
+ if (I->getExportRVA(RVA))
+ return;
+ outs() << format(" % 4d %# 8x", Ordinal, RVA);
+
+ StringRef Name;
+ if (I->getName(Name))
+ continue;
+ if (!Name.empty())
+ outs() << " " << Name;
+ outs() << "\n";
+ }
+}
+
void llvm::printCOFFUnwindInfo(const COFFObjectFile *Obj) {
const coff_file_header *Header;
if (error(Obj->getCOFFHeader(Header))) return;
}
void llvm::printCOFFFileHeader(const object::ObjectFile *Obj) {
- printImportTables(dyn_cast<const COFFObjectFile>(Obj));
+ const COFFObjectFile *file = dyn_cast<const COFFObjectFile>(Obj);
+ printImportTables(file);
+ printExportTable(file);
}