+relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
+ const coff_section *Sec = toSec(Ref);
+ DataRefImpl Ret;
+ if (Sec->NumberOfRelocations == 0) {
+ Ret.p = 0;
+ } else {
+ auto begin = reinterpret_cast<const coff_relocation*>(
+ base() + Sec->PointerToRelocations);
+ if (Sec->hasExtendedRelocations()) {
+ // Skip the first relocation entry repurposed to store the number of
+ // relocations.
+ begin++;
+ }
+ Ret.p = reinterpret_cast<uintptr_t>(begin);
+ }
+ return relocation_iterator(RelocationRef(Ret, this));
+}
+
+static uint32_t getNumberOfRelocations(const coff_section *Sec,
+ const uint8_t *base) {
+ // The field for the number of relocations in COFF section table is only
+ // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
+ // NumberOfRelocations field, and the actual relocation count is stored in the
+ // VirtualAddress field in the first relocation entry.
+ if (Sec->hasExtendedRelocations()) {
+ auto *FirstReloc = reinterpret_cast<const coff_relocation*>(
+ base + Sec->PointerToRelocations);
+ return FirstReloc->VirtualAddress;
+ }
+ return Sec->NumberOfRelocations;
+}
+
+relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
+ const coff_section *Sec = toSec(Ref);
+ DataRefImpl Ret;
+ if (Sec->NumberOfRelocations == 0) {
+ Ret.p = 0;
+ } else {
+ auto begin = reinterpret_cast<const coff_relocation*>(
+ base() + Sec->PointerToRelocations);
+ uint32_t NumReloc = getNumberOfRelocations(Sec, base());
+ Ret.p = reinterpret_cast<uintptr_t>(begin + NumReloc);
+ }
+ return relocation_iterator(RelocationRef(Ret, this));
+}
+
+// Initialize the pointer to the symbol table.
+std::error_code COFFObjectFile::initSymbolTablePtr() {
+ if (std::error_code EC = getObject(
+ SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable,
+ COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
+ 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() + COFFHeader->PointerToSymbolTable +
+ COFFHeader->NumberOfSymbols * sizeof(coff_symbol);
+ 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;
+}