1 //===- COFFObjectFile.cpp - COFF object file implementation -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the COFFObjectFile class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Object/COFF.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/Support/COFF.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/raw_ostream.h"
26 using namespace object;
28 using support::ulittle16_t;
29 using support::ulittle32_t;
30 using support::ulittle64_t;
31 using support::little16_t;
33 // Returns false if size is greater than the buffer size. And sets ec.
34 static bool checkSize(MemoryBufferRef M, std::error_code &EC, uint64_t Size) {
35 if (M.getBufferSize() < Size) {
36 EC = object_error::unexpected_eof;
42 // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
43 // Returns unexpected_eof if error.
45 static std::error_code getObject(const T *&Obj, MemoryBufferRef M,
47 const size_t Size = sizeof(T)) {
48 uintptr_t Addr = uintptr_t(Ptr);
49 if (Addr + Size < Addr || Addr + Size < Size ||
50 Addr + Size > uintptr_t(M.getBufferEnd())) {
51 return object_error::unexpected_eof;
53 Obj = reinterpret_cast<const T *>(Addr);
54 return object_error::success;
57 // Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without
59 static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) {
60 assert(Str.size() <= 6 && "String too long, possible overflow.");
65 while (!Str.empty()) {
67 if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25
68 CharVal = Str[0] - 'A';
69 else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51
70 CharVal = Str[0] - 'a' + 26;
71 else if (Str[0] >= '0' && Str[0] <= '9') // 52..61
72 CharVal = Str[0] - '0' + 52;
73 else if (Str[0] == '+') // 62
75 else if (Str[0] == '/') // 63
80 Value = (Value * 64) + CharVal;
84 if (Value > std::numeric_limits<uint32_t>::max())
87 Result = static_cast<uint32_t>(Value);
91 template <typename coff_symbol_type>
92 const coff_symbol_type *COFFObjectFile::toSymb(DataRefImpl Ref) const {
93 const coff_symbol_type *Addr =
94 reinterpret_cast<const coff_symbol_type *>(Ref.p);
97 // Verify that the symbol points to a valid entry in the symbol table.
98 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
99 if (Offset < getPointerToSymbolTable() ||
100 Offset >= getPointerToSymbolTable() +
101 (getNumberOfSymbols() * sizeof(coff_symbol_type)))
102 report_fatal_error("Symbol was outside of symbol table.");
104 assert((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 &&
105 "Symbol did not point to the beginning of a symbol");
111 const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
112 const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
115 // Verify that the section points to a valid entry in the section table.
116 if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections()))
117 report_fatal_error("Section was outside of section table.");
119 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
120 assert(Offset % sizeof(coff_section) == 0 &&
121 "Section did not point to the beginning of a section");
127 void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const {
129 const coff_symbol16 *Symb = toSymb<coff_symbol16>(Ref);
130 Symb += 1 + Symb->NumberOfAuxSymbols;
131 Ref.p = reinterpret_cast<uintptr_t>(Symb);
132 } else if (SymbolTable32) {
133 const coff_symbol32 *Symb = toSymb<coff_symbol32>(Ref);
134 Symb += 1 + Symb->NumberOfAuxSymbols;
135 Ref.p = reinterpret_cast<uintptr_t>(Symb);
137 llvm_unreachable("no symbol table pointer!");
141 std::error_code COFFObjectFile::getSymbolName(DataRefImpl Ref,
142 StringRef &Result) const {
143 COFFSymbolRef Symb = getCOFFSymbol(Ref);
144 return getSymbolName(Symb, Result);
147 std::error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
148 uint64_t &Result) const {
149 COFFSymbolRef Symb = getCOFFSymbol(Ref);
150 const coff_section *Section = nullptr;
151 if (std::error_code EC = getSection(Symb.getSectionNumber(), Section))
154 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED)
155 Result = UnknownAddressOrSize;
157 Result = Section->VirtualAddress + Symb.getValue();
159 Result = Symb.getValue();
160 return object_error::success;
163 std::error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
164 SymbolRef::Type &Result) const {
165 COFFSymbolRef Symb = getCOFFSymbol(Ref);
166 Result = SymbolRef::ST_Other;
168 if (Symb.getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
169 Symb.getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED) {
170 Result = SymbolRef::ST_Unknown;
171 } else if (Symb.isFunctionDefinition()) {
172 Result = SymbolRef::ST_Function;
174 uint32_t Characteristics = 0;
175 if (!COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
176 const coff_section *Section = nullptr;
177 if (std::error_code EC = getSection(Symb.getSectionNumber(), Section))
179 Characteristics = Section->Characteristics;
181 if (Characteristics & COFF::IMAGE_SCN_MEM_READ &&
182 ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
183 Result = SymbolRef::ST_Data;
185 return object_error::success;
188 uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
189 COFFSymbolRef Symb = getCOFFSymbol(Ref);
190 uint32_t Result = SymbolRef::SF_None;
192 // TODO: Correctly set SF_FormatSpecific, SF_Common
194 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED) {
195 if (Symb.getValue() == 0)
196 Result |= SymbolRef::SF_Undefined;
198 Result |= SymbolRef::SF_Common;
202 // TODO: This are certainly too restrictive.
203 if (Symb.getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL)
204 Result |= SymbolRef::SF_Global;
206 if (Symb.getStorageClass() == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
207 Result |= SymbolRef::SF_Weak;
209 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
210 Result |= SymbolRef::SF_Absolute;
215 std::error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref,
216 uint64_t &Result) const {
217 // FIXME: Return the correct size. This requires looking at all the symbols
218 // in the same section as this symbol, and looking for either the next
219 // symbol, or the end of the section.
220 COFFSymbolRef Symb = getCOFFSymbol(Ref);
221 const coff_section *Section = nullptr;
222 if (std::error_code EC = getSection(Symb.getSectionNumber(), Section))
225 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED) {
226 if (Symb.getValue() == 0)
227 Result = UnknownAddressOrSize;
229 Result = Symb.getValue();
230 } else if (Section) {
231 Result = Section->SizeOfRawData - Symb.getValue();
236 return object_error::success;
240 COFFObjectFile::getSymbolSection(DataRefImpl Ref,
241 section_iterator &Result) const {
242 COFFSymbolRef Symb = getCOFFSymbol(Ref);
243 if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
244 Result = section_end();
246 const coff_section *Sec = nullptr;
247 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
250 Ref.p = reinterpret_cast<uintptr_t>(Sec);
251 Result = section_iterator(SectionRef(Ref, this));
253 return object_error::success;
256 void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
257 const coff_section *Sec = toSec(Ref);
259 Ref.p = reinterpret_cast<uintptr_t>(Sec);
262 std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
263 StringRef &Result) const {
264 const coff_section *Sec = toSec(Ref);
265 return getSectionName(Sec, Result);
268 uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
269 const coff_section *Sec = toSec(Ref);
270 return Sec->VirtualAddress;
273 uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
274 return getSectionSize(toSec(Ref));
277 std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
278 StringRef &Result) const {
279 const coff_section *Sec = toSec(Ref);
280 ArrayRef<uint8_t> Res;
281 std::error_code EC = getSectionContents(Sec, Res);
282 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
286 uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
287 const coff_section *Sec = toSec(Ref);
288 return uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
291 bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
292 const coff_section *Sec = toSec(Ref);
293 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
296 bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
297 const coff_section *Sec = toSec(Ref);
298 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
301 bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
302 const coff_section *Sec = toSec(Ref);
303 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
306 bool COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref) const {
307 // FIXME: Unimplemented
311 bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
312 const coff_section *Sec = toSec(Ref);
313 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
316 bool COFFObjectFile::isSectionZeroInit(DataRefImpl Ref) const {
317 // FIXME: Unimplemented.
321 bool COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref) const {
322 // FIXME: Unimplemented.
326 bool COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
327 DataRefImpl SymbRef) const {
328 const coff_section *Sec = toSec(SecRef);
329 COFFSymbolRef Symb = getCOFFSymbol(SymbRef);
330 int32_t SecNumber = (Sec - SectionTable) + 1;
331 return SecNumber == Symb.getSectionNumber();
334 relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
335 const coff_section *Sec = toSec(Ref);
337 if (Sec->NumberOfRelocations == 0) {
340 auto begin = reinterpret_cast<const coff_relocation*>(
341 base() + Sec->PointerToRelocations);
342 if (Sec->hasExtendedRelocations()) {
343 // Skip the first relocation entry repurposed to store the number of
347 Ret.p = reinterpret_cast<uintptr_t>(begin);
349 return relocation_iterator(RelocationRef(Ret, this));
352 static uint32_t getNumberOfRelocations(const coff_section *Sec,
353 const uint8_t *base) {
354 // The field for the number of relocations in COFF section table is only
355 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
356 // NumberOfRelocations field, and the actual relocation count is stored in the
357 // VirtualAddress field in the first relocation entry.
358 if (Sec->hasExtendedRelocations()) {
359 auto *FirstReloc = reinterpret_cast<const coff_relocation*>(
360 base + Sec->PointerToRelocations);
361 return FirstReloc->VirtualAddress;
363 return Sec->NumberOfRelocations;
366 relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
367 const coff_section *Sec = toSec(Ref);
369 if (Sec->NumberOfRelocations == 0) {
372 auto begin = reinterpret_cast<const coff_relocation*>(
373 base() + Sec->PointerToRelocations);
374 uint32_t NumReloc = getNumberOfRelocations(Sec, base());
375 Ret.p = reinterpret_cast<uintptr_t>(begin + NumReloc);
377 return relocation_iterator(RelocationRef(Ret, this));
380 // Initialize the pointer to the symbol table.
381 std::error_code COFFObjectFile::initSymbolTablePtr() {
383 if (std::error_code EC =
384 getObject(SymbolTable16, Data, base() + getPointerToSymbolTable(),
385 getNumberOfSymbols() * getSymbolTableEntrySize()))
388 if (COFFBigObjHeader)
389 if (std::error_code EC =
390 getObject(SymbolTable32, Data, base() + getPointerToSymbolTable(),
391 getNumberOfSymbols() * getSymbolTableEntrySize()))
394 // Find string table. The first four byte of the string table contains the
395 // total size of the string table, including the size field itself. If the
396 // string table is empty, the value of the first four byte would be 4.
397 const uint8_t *StringTableAddr =
398 base() + getPointerToSymbolTable() +
399 getNumberOfSymbols() * getSymbolTableEntrySize();
400 const ulittle32_t *StringTableSizePtr;
401 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
403 StringTableSize = *StringTableSizePtr;
404 if (std::error_code EC =
405 getObject(StringTable, Data, StringTableAddr, StringTableSize))
408 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
409 // tools like cvtres write a size of 0 for an empty table instead of 4.
410 if (StringTableSize < 4)
413 // Check that the string table is null terminated if has any in it.
414 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
415 return object_error::parse_failed;
416 return object_error::success;
419 // Returns the file offset for the given VA.
420 std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
421 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
422 : (uint64_t)PE32PlusHeader->ImageBase;
423 uint64_t Rva = Addr - ImageBase;
424 assert(Rva <= UINT32_MAX);
425 return getRvaPtr((uint32_t)Rva, Res);
428 // Returns the file offset for the given RVA.
429 std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
430 for (const SectionRef &S : sections()) {
431 const coff_section *Section = getCOFFSection(S);
432 uint32_t SectionStart = Section->VirtualAddress;
433 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
434 if (SectionStart <= Addr && Addr < SectionEnd) {
435 uint32_t Offset = Addr - SectionStart;
436 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
437 return object_error::success;
440 return object_error::parse_failed;
443 // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
445 std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
446 StringRef &Name) const {
447 uintptr_t IntPtr = 0;
448 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
450 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
451 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
452 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
453 return object_error::success;
456 // Find the import table.
457 std::error_code COFFObjectFile::initImportTablePtr() {
458 // First, we get the RVA of the import table. If the file lacks a pointer to
459 // the import table, do nothing.
460 const data_directory *DataEntry;
461 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
462 return object_error::success;
464 // Do nothing if the pointer to import table is NULL.
465 if (DataEntry->RelativeVirtualAddress == 0)
466 return object_error::success;
468 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
469 // -1 because the last entry is the null entry.
470 NumberOfImportDirectory = DataEntry->Size /
471 sizeof(import_directory_table_entry) - 1;
473 // Find the section that contains the RVA. This is needed because the RVA is
474 // the import table's memory address which is different from its file offset.
475 uintptr_t IntPtr = 0;
476 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
478 ImportDirectory = reinterpret_cast<
479 const import_directory_table_entry *>(IntPtr);
480 return object_error::success;
483 // Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
484 std::error_code COFFObjectFile::initDelayImportTablePtr() {
485 const data_directory *DataEntry;
486 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
487 return object_error::success;
488 if (DataEntry->RelativeVirtualAddress == 0)
489 return object_error::success;
491 uint32_t RVA = DataEntry->RelativeVirtualAddress;
492 NumberOfDelayImportDirectory = DataEntry->Size /
493 sizeof(delay_import_directory_table_entry) - 1;
495 uintptr_t IntPtr = 0;
496 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
498 DelayImportDirectory = reinterpret_cast<
499 const delay_import_directory_table_entry *>(IntPtr);
500 return object_error::success;
503 // Find the export table.
504 std::error_code COFFObjectFile::initExportTablePtr() {
505 // First, we get the RVA of the export table. If the file lacks a pointer to
506 // the export table, do nothing.
507 const data_directory *DataEntry;
508 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
509 return object_error::success;
511 // Do nothing if the pointer to export table is NULL.
512 if (DataEntry->RelativeVirtualAddress == 0)
513 return object_error::success;
515 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
516 uintptr_t IntPtr = 0;
517 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
520 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
521 return object_error::success;
524 COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
525 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
526 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
527 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
528 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
529 ImportDirectory(nullptr), NumberOfImportDirectory(0),
530 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
531 ExportDirectory(nullptr) {
532 // Check that we at least have enough room for a header.
533 if (!checkSize(Data, EC, sizeof(coff_file_header)))
536 // The current location in the file where we are looking at.
539 // PE header is optional and is present only in executables. If it exists,
540 // it is placed right after COFF header.
541 bool HasPEHeader = false;
543 // Check if this is a PE/COFF file.
544 if (base()[0] == 0x4d && base()[1] == 0x5a) {
545 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
546 // PE signature to find 'normal' COFF header.
547 if (!checkSize(Data, EC, 0x3c + 8))
549 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
550 // Check the PE magic bytes. ("PE\0\0")
551 if (std::memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) !=
553 EC = object_error::parse_failed;
556 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
560 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
563 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
564 // import libraries share a common prefix but bigobj is more restrictive.
565 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
566 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
567 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
568 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
571 // Verify that we are dealing with bigobj.
572 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
573 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
574 sizeof(COFF::BigObjMagic)) == 0) {
575 COFFHeader = nullptr;
576 CurPtr += sizeof(coff_bigobj_file_header);
578 // It's not a bigobj.
579 COFFBigObjHeader = nullptr;
583 // The prior checkSize call may have failed. This isn't a hard error
584 // because we were just trying to sniff out bigobj.
585 EC = object_error::success;
586 CurPtr += sizeof(coff_file_header);
588 if (COFFHeader->isImportLibrary())
593 const pe32_header *Header;
594 if ((EC = getObject(Header, Data, base() + CurPtr)))
597 const uint8_t *DataDirAddr;
598 uint64_t DataDirSize;
599 if (Header->Magic == 0x10b) {
601 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
602 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
603 } else if (Header->Magic == 0x20b) {
604 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
605 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
606 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
608 // It's neither PE32 nor PE32+.
609 EC = object_error::parse_failed;
612 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
614 CurPtr += COFFHeader->SizeOfOptionalHeader;
617 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
618 getNumberOfSections() * sizeof(coff_section))))
621 // Initialize the pointer to the symbol table.
622 if (getPointerToSymbolTable() != 0)
623 if ((EC = initSymbolTablePtr()))
626 // Initialize the pointer to the beginning of the import table.
627 if ((EC = initImportTablePtr()))
629 if ((EC = initDelayImportTablePtr()))
632 // Initialize the pointer to the export table.
633 if ((EC = initExportTablePtr()))
636 EC = object_error::success;
639 basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
641 Ret.p = getSymbolTable();
642 return basic_symbol_iterator(SymbolRef(Ret, this));
645 basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
646 // The symbol table ends where the string table begins.
648 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
649 return basic_symbol_iterator(SymbolRef(Ret, this));
652 import_directory_iterator COFFObjectFile::import_directory_begin() const {
653 return import_directory_iterator(
654 ImportDirectoryEntryRef(ImportDirectory, 0, this));
657 import_directory_iterator COFFObjectFile::import_directory_end() const {
658 return import_directory_iterator(
659 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
662 delay_import_directory_iterator
663 COFFObjectFile::delay_import_directory_begin() const {
664 return delay_import_directory_iterator(
665 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
668 delay_import_directory_iterator
669 COFFObjectFile::delay_import_directory_end() const {
670 return delay_import_directory_iterator(
671 DelayImportDirectoryEntryRef(
672 DelayImportDirectory, NumberOfDelayImportDirectory, this));
675 export_directory_iterator COFFObjectFile::export_directory_begin() const {
676 return export_directory_iterator(
677 ExportDirectoryEntryRef(ExportDirectory, 0, this));
680 export_directory_iterator COFFObjectFile::export_directory_end() const {
681 if (!ExportDirectory)
682 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
683 ExportDirectoryEntryRef Ref(ExportDirectory,
684 ExportDirectory->AddressTableEntries, this);
685 return export_directory_iterator(Ref);
688 section_iterator COFFObjectFile::section_begin() const {
690 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
691 return section_iterator(SectionRef(Ret, this));
694 section_iterator COFFObjectFile::section_end() const {
697 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
698 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
699 return section_iterator(SectionRef(Ret, this));
702 uint8_t COFFObjectFile::getBytesInAddress() const {
703 return getArch() == Triple::x86_64 ? 8 : 4;
706 StringRef COFFObjectFile::getFileFormatName() const {
707 switch(getMachine()) {
708 case COFF::IMAGE_FILE_MACHINE_I386:
710 case COFF::IMAGE_FILE_MACHINE_AMD64:
711 return "COFF-x86-64";
712 case COFF::IMAGE_FILE_MACHINE_ARMNT:
715 return "COFF-<unknown arch>";
719 unsigned COFFObjectFile::getArch() const {
720 switch (getMachine()) {
721 case COFF::IMAGE_FILE_MACHINE_I386:
723 case COFF::IMAGE_FILE_MACHINE_AMD64:
724 return Triple::x86_64;
725 case COFF::IMAGE_FILE_MACHINE_ARMNT:
726 return Triple::thumb;
728 return Triple::UnknownArch;
732 iterator_range<import_directory_iterator>
733 COFFObjectFile::import_directories() const {
734 return make_range(import_directory_begin(), import_directory_end());
737 iterator_range<delay_import_directory_iterator>
738 COFFObjectFile::delay_import_directories() const {
739 return make_range(delay_import_directory_begin(),
740 delay_import_directory_end());
743 iterator_range<export_directory_iterator>
744 COFFObjectFile::export_directories() const {
745 return make_range(export_directory_begin(), export_directory_end());
748 std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
750 return object_error::success;
754 COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
755 Res = PE32PlusHeader;
756 return object_error::success;
760 COFFObjectFile::getDataDirectory(uint32_t Index,
761 const data_directory *&Res) const {
762 // Error if if there's no data directory or the index is out of range.
764 return object_error::parse_failed;
765 assert(PE32Header || PE32PlusHeader);
766 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
767 : PE32PlusHeader->NumberOfRvaAndSize;
769 return object_error::parse_failed;
770 Res = &DataDirectory[Index];
771 return object_error::success;
774 std::error_code COFFObjectFile::getSection(int32_t Index,
775 const coff_section *&Result) const {
776 // Check for special index values.
777 if (COFF::isReservedSectionNumber(Index))
779 else if (Index > 0 && static_cast<uint32_t>(Index) <= getNumberOfSections())
780 // We already verified the section table data, so no need to check again.
781 Result = SectionTable + (Index - 1);
783 return object_error::parse_failed;
784 return object_error::success;
787 std::error_code COFFObjectFile::getString(uint32_t Offset,
788 StringRef &Result) const {
789 if (StringTableSize <= 4)
790 // Tried to get a string from an empty string table.
791 return object_error::parse_failed;
792 if (Offset >= StringTableSize)
793 return object_error::unexpected_eof;
794 Result = StringRef(StringTable + Offset);
795 return object_error::success;
798 std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
799 StringRef &Res) const {
800 // Check for string table entry. First 4 bytes are 0.
801 if (Symbol.getStringTableOffset().Zeroes == 0) {
802 uint32_t Offset = Symbol.getStringTableOffset().Offset;
803 if (std::error_code EC = getString(Offset, Res))
805 return object_error::success;
808 if (Symbol.getShortName()[COFF::NameSize - 1] == 0)
809 // Null terminated, let ::strlen figure out the length.
810 Res = StringRef(Symbol.getShortName());
812 // Not null terminated, use all 8 bytes.
813 Res = StringRef(Symbol.getShortName(), COFF::NameSize);
814 return object_error::success;
818 COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
819 const uint8_t *Aux = nullptr;
821 size_t SymbolSize = getSymbolTableEntrySize();
822 if (Symbol.getNumberOfAuxSymbols() > 0) {
823 // AUX data comes immediately after the symbol in COFF
824 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
826 // Verify that the Aux symbol points to a valid entry in the symbol table.
827 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
828 if (Offset < getPointerToSymbolTable() ||
830 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
831 report_fatal_error("Aux Symbol data was outside of symbol table.");
833 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
834 "Aux Symbol data did not point to the beginning of a symbol");
837 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
840 std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
841 StringRef &Res) const {
843 if (Sec->Name[COFF::NameSize - 1] == 0)
844 // Null terminated, let ::strlen figure out the length.
847 // Not null terminated, use all 8 bytes.
848 Name = StringRef(Sec->Name, COFF::NameSize);
850 // Check for string table entry. First byte is '/'.
851 if (Name[0] == '/') {
853 if (Name[1] == '/') {
854 if (decodeBase64StringEntry(Name.substr(2), Offset))
855 return object_error::parse_failed;
857 if (Name.substr(1).getAsInteger(10, Offset))
858 return object_error::parse_failed;
860 if (std::error_code EC = getString(Offset, Name))
865 return object_error::success;
868 uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
869 // SizeOfRawData and VirtualSize change what they represent depending on
870 // whether or not we have an executable image.
872 // For object files, SizeOfRawData contains the size of section's data;
873 // VirtualSize is always zero.
875 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
876 // actual section size is in VirtualSize. It is possible for VirtualSize to
877 // be greater than SizeOfRawData; the contents past that point should be
878 // considered to be zero.
879 uint32_t SectionSize;
880 if (Sec->VirtualSize)
881 SectionSize = std::min(Sec->VirtualSize, Sec->SizeOfRawData);
883 SectionSize = Sec->SizeOfRawData;
889 COFFObjectFile::getSectionContents(const coff_section *Sec,
890 ArrayRef<uint8_t> &Res) const {
891 // PointerToRawData and SizeOfRawData won't make sense for BSS sections,
892 // don't do anything interesting for them.
893 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
894 "BSS sections don't have contents!");
895 // The only thing that we need to verify is that the contents is contained
896 // within the file bounds. We don't need to make sure it doesn't cover other
897 // data, as there's nothing that says that is not allowed.
898 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
899 uint32_t SectionSize = getSectionSize(Sec);
900 uintptr_t ConEnd = ConStart + SectionSize;
901 if (ConEnd > uintptr_t(Data.getBufferEnd()))
902 return object_error::parse_failed;
903 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
904 return object_error::success;
907 const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
908 return reinterpret_cast<const coff_relocation*>(Rel.p);
911 void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
912 Rel.p = reinterpret_cast<uintptr_t>(
913 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
916 std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
917 uint64_t &Res) const {
918 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
921 std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
922 uint64_t &Res) const {
923 Res = toRel(Rel)->VirtualAddress;
924 return object_error::success;
927 symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
928 const coff_relocation *R = toRel(Rel);
931 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
932 else if (SymbolTable32)
933 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
935 llvm_unreachable("no symbol table pointer!");
936 return symbol_iterator(SymbolRef(Ref, this));
939 std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
940 uint64_t &Res) const {
941 const coff_relocation* R = toRel(Rel);
943 return object_error::success;
947 COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
948 return toSec(Section.getRawDataRefImpl());
951 COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
953 return toSymb<coff_symbol16>(Ref);
955 return toSymb<coff_symbol32>(Ref);
956 llvm_unreachable("no symbol table pointer!");
959 COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
960 return getCOFFSymbol(Symbol.getRawDataRefImpl());
963 const coff_relocation *
964 COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
965 return toRel(Reloc.getRawDataRefImpl());
968 #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
969 case COFF::reloc_type: \
974 COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
975 SmallVectorImpl<char> &Result) const {
976 const coff_relocation *Reloc = toRel(Rel);
978 switch (getMachine()) {
979 case COFF::IMAGE_FILE_MACHINE_AMD64:
980 switch (Reloc->Type) {
981 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
982 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
983 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
984 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
985 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
986 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
987 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
988 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
989 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
990 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
991 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
992 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
993 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
994 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
995 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
996 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
997 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1002 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1003 switch (Reloc->Type) {
1004 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1005 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1006 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1007 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1008 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1009 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1010 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1011 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1012 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1013 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1014 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1015 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1016 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1017 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1018 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1023 case COFF::IMAGE_FILE_MACHINE_I386:
1024 switch (Reloc->Type) {
1025 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1026 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1027 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1028 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1029 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1030 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1031 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1032 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1033 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1034 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1035 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1043 Result.append(Res.begin(), Res.end());
1044 return object_error::success;
1047 #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1050 COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
1051 SmallVectorImpl<char> &Result) const {
1052 const coff_relocation *Reloc = toRel(Rel);
1054 ErrorOr<COFFSymbolRef> Symb = getSymbol(Reloc->SymbolTableIndex);
1055 if (std::error_code EC = Symb.getError())
1057 Sym.p = reinterpret_cast<uintptr_t>(Symb->getRawPtr());
1059 if (std::error_code EC = getSymbolName(Sym, SymName))
1061 Result.append(SymName.begin(), SymName.end());
1062 return object_error::success;
1065 bool COFFObjectFile::isRelocatableObject() const {
1066 return !DataDirectory;
1069 bool ImportDirectoryEntryRef::
1070 operator==(const ImportDirectoryEntryRef &Other) const {
1071 return ImportTable == Other.ImportTable && Index == Other.Index;
1074 void ImportDirectoryEntryRef::moveNext() {
1078 std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1079 const import_directory_table_entry *&Result) const {
1080 Result = ImportTable + Index;
1081 return object_error::success;
1084 static imported_symbol_iterator
1085 makeImportedSymbolIterator(const COFFObjectFile *Object,
1086 uintptr_t Ptr, int Index) {
1087 if (Object->getBytesInAddress() == 4) {
1088 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
1089 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
1091 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
1092 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
1095 static imported_symbol_iterator
1096 importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
1097 uintptr_t IntPtr = 0;
1098 Object->getRvaPtr(RVA, IntPtr);
1099 return makeImportedSymbolIterator(Object, IntPtr, 0);
1102 static imported_symbol_iterator
1103 importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
1104 uintptr_t IntPtr = 0;
1105 Object->getRvaPtr(RVA, IntPtr);
1106 // Forward the pointer to the last entry which is null.
1108 if (Object->getBytesInAddress() == 4) {
1109 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1113 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1117 return makeImportedSymbolIterator(Object, IntPtr, Index);
1120 imported_symbol_iterator
1121 ImportDirectoryEntryRef::imported_symbol_begin() const {
1122 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1126 imported_symbol_iterator
1127 ImportDirectoryEntryRef::imported_symbol_end() const {
1128 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1132 iterator_range<imported_symbol_iterator>
1133 ImportDirectoryEntryRef::imported_symbols() const {
1134 return make_range(imported_symbol_begin(), imported_symbol_end());
1137 std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
1138 uintptr_t IntPtr = 0;
1139 if (std::error_code EC =
1140 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
1142 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1143 return object_error::success;
1147 ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1148 Result = ImportTable[Index].ImportLookupTableRVA;
1149 return object_error::success;
1153 ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1154 Result = ImportTable[Index].ImportAddressTableRVA;
1155 return object_error::success;
1158 std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
1159 const import_lookup_table_entry32 *&Result) const {
1160 uintptr_t IntPtr = 0;
1161 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1162 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1164 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
1165 return object_error::success;
1168 bool DelayImportDirectoryEntryRef::
1169 operator==(const DelayImportDirectoryEntryRef &Other) const {
1170 return Table == Other.Table && Index == Other.Index;
1173 void DelayImportDirectoryEntryRef::moveNext() {
1177 imported_symbol_iterator
1178 DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1179 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1183 imported_symbol_iterator
1184 DelayImportDirectoryEntryRef::imported_symbol_end() const {
1185 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1189 iterator_range<imported_symbol_iterator>
1190 DelayImportDirectoryEntryRef::imported_symbols() const {
1191 return make_range(imported_symbol_begin(), imported_symbol_end());
1194 std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1195 uintptr_t IntPtr = 0;
1196 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1198 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1199 return object_error::success;
1202 std::error_code DelayImportDirectoryEntryRef::
1203 getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1205 return object_error::success;
1208 bool ExportDirectoryEntryRef::
1209 operator==(const ExportDirectoryEntryRef &Other) const {
1210 return ExportTable == Other.ExportTable && Index == Other.Index;
1213 void ExportDirectoryEntryRef::moveNext() {
1217 // Returns the name of the current export symbol. If the symbol is exported only
1218 // by ordinal, the empty string is set as a result.
1219 std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
1220 uintptr_t IntPtr = 0;
1221 if (std::error_code EC =
1222 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
1224 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1225 return object_error::success;
1228 // Returns the starting ordinal number.
1230 ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
1231 Result = ExportTable->OrdinalBase;
1232 return object_error::success;
1235 // Returns the export ordinal of the current export symbol.
1236 std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
1237 Result = ExportTable->OrdinalBase + Index;
1238 return object_error::success;
1241 // Returns the address of the current export symbol.
1242 std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
1243 uintptr_t IntPtr = 0;
1244 if (std::error_code EC =
1245 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
1247 const export_address_table_entry *entry =
1248 reinterpret_cast<const export_address_table_entry *>(IntPtr);
1249 Result = entry[Index].ExportRVA;
1250 return object_error::success;
1253 // Returns the name of the current export symbol. If the symbol is exported only
1254 // by ordinal, the empty string is set as a result.
1256 ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
1257 uintptr_t IntPtr = 0;
1258 if (std::error_code EC =
1259 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
1261 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1263 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1265 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1266 I < E; ++I, ++Offset) {
1269 if (std::error_code EC =
1270 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
1272 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
1273 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
1275 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1276 return object_error::success;
1279 return object_error::success;
1282 bool ImportedSymbolRef::
1283 operator==(const ImportedSymbolRef &Other) const {
1284 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1285 && Index == Other.Index;
1288 void ImportedSymbolRef::moveNext() {
1293 ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1296 // If a symbol is imported only by ordinal, it has no name.
1297 if (Entry32[Index].isOrdinal())
1298 return object_error::success;
1299 RVA = Entry32[Index].getHintNameRVA();
1301 if (Entry64[Index].isOrdinal())
1302 return object_error::success;
1303 RVA = Entry64[Index].getHintNameRVA();
1305 uintptr_t IntPtr = 0;
1306 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1308 // +2 because the first two bytes is hint.
1309 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
1310 return object_error::success;
1313 std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1316 if (Entry32[Index].isOrdinal()) {
1317 Result = Entry32[Index].getOrdinal();
1318 return object_error::success;
1320 RVA = Entry32[Index].getHintNameRVA();
1322 if (Entry64[Index].isOrdinal()) {
1323 Result = Entry64[Index].getOrdinal();
1324 return object_error::success;
1326 RVA = Entry64[Index].getHintNameRVA();
1328 uintptr_t IntPtr = 0;
1329 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1331 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
1332 return object_error::success;
1335 ErrorOr<std::unique_ptr<COFFObjectFile>>
1336 ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
1338 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
1341 return std::move(Ret);