+// Initialize the pointer to the symbol table.
+std::error_code COFFObjectFile::initSymbolTablePtr() {
+ if (COFFHeader)
+ if (std::error_code EC =
+ getObject(SymbolTable16, Data, base() + getPointerToSymbolTable(),
+ getNumberOfSymbols() * getSymbolTableEntrySize()))
+ return EC;
+
+ if (COFFBigObjHeader)
+ if (std::error_code EC =
+ getObject(SymbolTable32, Data, base() + getPointerToSymbolTable(),
+ getNumberOfSymbols() * getSymbolTableEntrySize()))
+ return EC;
+
+ // Find string table. The first four byte of the string table contains the
+ // total size of the string table, including the size field itself. If the
+ // string table is empty, the value of the first four byte would be 4.
+ const uint8_t *StringTableAddr =
+ base() + getPointerToSymbolTable() +
+ getNumberOfSymbols() * getSymbolTableEntrySize();
+ const ulittle32_t *StringTableSizePtr;
+ if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
+ return EC;
+ StringTableSize = *StringTableSizePtr;
+ if (std::error_code EC =
+ getObject(StringTable, Data, StringTableAddr, StringTableSize))
+ return EC;
+
+ // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
+ // tools like cvtres write a size of 0 for an empty table instead of 4.
+ if (StringTableSize < 4)
+ StringTableSize = 4;
+
+ // Check that the string table is null terminated if has any in it.
+ if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
+ return object_error::parse_failed;
+ return object_error::success;
+}
+
+// Returns the file offset for the given VA.
+std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
+ uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
+ : (uint64_t)PE32PlusHeader->ImageBase;
+ uint64_t Rva = Addr - ImageBase;
+ assert(Rva <= UINT32_MAX);
+ return getRvaPtr((uint32_t)Rva, Res);
+}
+
+// Returns the file offset for the given RVA.
+std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
+ for (const SectionRef &S : sections()) {
+ const coff_section *Section = getCOFFSection(S);
+ uint32_t SectionStart = Section->VirtualAddress;
+ uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
+ if (SectionStart <= Addr && Addr < SectionEnd) {
+ uint32_t Offset = Addr - SectionStart;
+ Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
+ return object_error::success;
+ }
+ }
+ return object_error::parse_failed;
+}
+
+// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
+// table entry.
+std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
+ StringRef &Name) const {
+ uintptr_t IntPtr = 0;
+ if (std::error_code EC = getRvaPtr(Rva, IntPtr))
+ return EC;
+ const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
+ Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
+ Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
+ return object_error::success;
+}
+
+// Find the import table.
+std::error_code COFFObjectFile::initImportTablePtr() {
+ // First, we get the RVA of the import table. If the file lacks a pointer to
+ // the import table, do nothing.
+ const data_directory *DataEntry;
+ if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
+ return object_error::success;