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 Result = UnknownAddressOrSize;
228 Result = Section->SizeOfRawData - Symb.getValue();
231 return object_error::success;
235 COFFObjectFile::getSymbolSection(DataRefImpl Ref,
236 section_iterator &Result) const {
237 COFFSymbolRef Symb = getCOFFSymbol(Ref);
238 if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
239 Result = section_end();
241 const coff_section *Sec = nullptr;
242 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
245 Ref.p = reinterpret_cast<uintptr_t>(Sec);
246 Result = section_iterator(SectionRef(Ref, this));
248 return object_error::success;
251 void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
252 const coff_section *Sec = toSec(Ref);
254 Ref.p = reinterpret_cast<uintptr_t>(Sec);
257 std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
258 StringRef &Result) const {
259 const coff_section *Sec = toSec(Ref);
260 return getSectionName(Sec, Result);
263 std::error_code COFFObjectFile::getSectionAddress(DataRefImpl Ref,
264 uint64_t &Result) const {
265 const coff_section *Sec = toSec(Ref);
266 Result = Sec->VirtualAddress;
267 return object_error::success;
270 std::error_code COFFObjectFile::getSectionSize(DataRefImpl Ref,
271 uint64_t &Result) const {
272 const coff_section *Sec = toSec(Ref);
273 Result = Sec->SizeOfRawData;
274 return object_error::success;
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 std::error_code COFFObjectFile::getSectionAlignment(DataRefImpl Ref,
287 uint64_t &Res) const {
288 const coff_section *Sec = toSec(Ref);
290 return object_error::parse_failed;
291 Res = uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
292 return object_error::success;
295 std::error_code COFFObjectFile::isSectionText(DataRefImpl Ref,
296 bool &Result) const {
297 const coff_section *Sec = toSec(Ref);
298 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
299 return object_error::success;
302 std::error_code COFFObjectFile::isSectionData(DataRefImpl Ref,
303 bool &Result) const {
304 const coff_section *Sec = toSec(Ref);
305 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
306 return object_error::success;
309 std::error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref,
310 bool &Result) const {
311 const coff_section *Sec = toSec(Ref);
312 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
313 return object_error::success;
317 COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref,
318 bool &Result) const {
319 // FIXME: Unimplemented
321 return object_error::success;
324 std::error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref,
325 bool &Result) const {
326 const coff_section *Sec = toSec(Ref);
327 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
328 return object_error::success;
331 std::error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref,
332 bool &Result) const {
333 // FIXME: Unimplemented.
335 return object_error::success;
338 std::error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref,
339 bool &Result) const {
340 // FIXME: Unimplemented.
342 return object_error::success;
345 std::error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
347 bool &Result) const {
348 const coff_section *Sec = toSec(SecRef);
349 COFFSymbolRef Symb = getCOFFSymbol(SymbRef);
350 const coff_section *SymbSec = nullptr;
351 if (std::error_code EC = getSection(Symb.getSectionNumber(), SymbSec))
357 return object_error::success;
360 relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
361 const coff_section *Sec = toSec(Ref);
363 if (Sec->NumberOfRelocations == 0) {
366 auto begin = reinterpret_cast<const coff_relocation*>(
367 base() + Sec->PointerToRelocations);
368 if (Sec->hasExtendedRelocations()) {
369 // Skip the first relocation entry repurposed to store the number of
373 Ret.p = reinterpret_cast<uintptr_t>(begin);
375 return relocation_iterator(RelocationRef(Ret, this));
378 static uint32_t getNumberOfRelocations(const coff_section *Sec,
379 const uint8_t *base) {
380 // The field for the number of relocations in COFF section table is only
381 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
382 // NumberOfRelocations field, and the actual relocation count is stored in the
383 // VirtualAddress field in the first relocation entry.
384 if (Sec->hasExtendedRelocations()) {
385 auto *FirstReloc = reinterpret_cast<const coff_relocation*>(
386 base + Sec->PointerToRelocations);
387 return FirstReloc->VirtualAddress;
389 return Sec->NumberOfRelocations;
392 relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
393 const coff_section *Sec = toSec(Ref);
395 if (Sec->NumberOfRelocations == 0) {
398 auto begin = reinterpret_cast<const coff_relocation*>(
399 base() + Sec->PointerToRelocations);
400 uint32_t NumReloc = getNumberOfRelocations(Sec, base());
401 Ret.p = reinterpret_cast<uintptr_t>(begin + NumReloc);
403 return relocation_iterator(RelocationRef(Ret, this));
406 // Initialize the pointer to the symbol table.
407 std::error_code COFFObjectFile::initSymbolTablePtr() {
409 if (std::error_code EC =
410 getObject(SymbolTable16, Data, base() + getPointerToSymbolTable(),
411 getNumberOfSymbols() * getSymbolTableEntrySize()))
414 if (COFFBigObjHeader)
415 if (std::error_code EC =
416 getObject(SymbolTable32, Data, base() + getPointerToSymbolTable(),
417 getNumberOfSymbols() * getSymbolTableEntrySize()))
420 // Find string table. The first four byte of the string table contains the
421 // total size of the string table, including the size field itself. If the
422 // string table is empty, the value of the first four byte would be 4.
423 const uint8_t *StringTableAddr =
424 base() + getPointerToSymbolTable() +
425 getNumberOfSymbols() * getSymbolTableEntrySize();
426 const ulittle32_t *StringTableSizePtr;
427 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
429 StringTableSize = *StringTableSizePtr;
430 if (std::error_code EC =
431 getObject(StringTable, Data, StringTableAddr, StringTableSize))
434 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
435 // tools like cvtres write a size of 0 for an empty table instead of 4.
436 if (StringTableSize < 4)
439 // Check that the string table is null terminated if has any in it.
440 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
441 return object_error::parse_failed;
442 return object_error::success;
445 // Returns the file offset for the given VA.
446 std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
447 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
448 : (uint64_t)PE32PlusHeader->ImageBase;
449 uint64_t Rva = Addr - ImageBase;
450 assert(Rva <= UINT32_MAX);
451 return getRvaPtr((uint32_t)Rva, Res);
454 // Returns the file offset for the given RVA.
455 std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
456 for (const SectionRef &S : sections()) {
457 const coff_section *Section = getCOFFSection(S);
458 uint32_t SectionStart = Section->VirtualAddress;
459 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
460 if (SectionStart <= Addr && Addr < SectionEnd) {
461 uint32_t Offset = Addr - SectionStart;
462 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
463 return object_error::success;
466 return object_error::parse_failed;
469 // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
471 std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
472 StringRef &Name) const {
473 uintptr_t IntPtr = 0;
474 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
476 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
477 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
478 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
479 return object_error::success;
482 // Find the import table.
483 std::error_code COFFObjectFile::initImportTablePtr() {
484 // First, we get the RVA of the import table. If the file lacks a pointer to
485 // the import table, do nothing.
486 const data_directory *DataEntry;
487 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
488 return object_error::success;
490 // Do nothing if the pointer to import table is NULL.
491 if (DataEntry->RelativeVirtualAddress == 0)
492 return object_error::success;
494 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
495 // -1 because the last entry is the null entry.
496 NumberOfImportDirectory = DataEntry->Size /
497 sizeof(import_directory_table_entry) - 1;
499 // Find the section that contains the RVA. This is needed because the RVA is
500 // the import table's memory address which is different from its file offset.
501 uintptr_t IntPtr = 0;
502 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
504 ImportDirectory = reinterpret_cast<
505 const import_directory_table_entry *>(IntPtr);
506 return object_error::success;
509 // Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
510 std::error_code COFFObjectFile::initDelayImportTablePtr() {
511 const data_directory *DataEntry;
512 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
513 return object_error::success;
514 if (DataEntry->RelativeVirtualAddress == 0)
515 return object_error::success;
517 uint32_t RVA = DataEntry->RelativeVirtualAddress;
518 NumberOfDelayImportDirectory = DataEntry->Size /
519 sizeof(delay_import_directory_table_entry) - 1;
521 uintptr_t IntPtr = 0;
522 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
524 DelayImportDirectory = reinterpret_cast<
525 const delay_import_directory_table_entry *>(IntPtr);
526 return object_error::success;
529 // Find the export table.
530 std::error_code COFFObjectFile::initExportTablePtr() {
531 // First, we get the RVA of the export table. If the file lacks a pointer to
532 // the export table, do nothing.
533 const data_directory *DataEntry;
534 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
535 return object_error::success;
537 // Do nothing if the pointer to export table is NULL.
538 if (DataEntry->RelativeVirtualAddress == 0)
539 return object_error::success;
541 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
542 uintptr_t IntPtr = 0;
543 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
546 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
547 return object_error::success;
550 COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
551 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
552 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
553 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
554 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
555 ImportDirectory(nullptr), NumberOfImportDirectory(0),
556 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
557 ExportDirectory(nullptr) {
558 // Check that we at least have enough room for a header.
559 if (!checkSize(Data, EC, sizeof(coff_file_header)))
562 // The current location in the file where we are looking at.
565 // PE header is optional and is present only in executables. If it exists,
566 // it is placed right after COFF header.
567 bool HasPEHeader = false;
569 // Check if this is a PE/COFF file.
570 if (base()[0] == 0x4d && base()[1] == 0x5a) {
571 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
572 // PE signature to find 'normal' COFF header.
573 if (!checkSize(Data, EC, 0x3c + 8))
575 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
576 // Check the PE magic bytes. ("PE\0\0")
577 if (std::memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) !=
579 EC = object_error::parse_failed;
582 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
586 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
589 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
590 // import libraries share a common prefix but bigobj is more restrictive.
591 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
592 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
593 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
594 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
597 // Verify that we are dealing with bigobj.
598 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
599 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
600 sizeof(COFF::BigObjMagic)) == 0) {
601 COFFHeader = nullptr;
602 CurPtr += sizeof(coff_bigobj_file_header);
604 // It's not a bigobj.
605 COFFBigObjHeader = nullptr;
609 // The prior checkSize call may have failed. This isn't a hard error
610 // because we were just trying to sniff out bigobj.
611 EC = object_error::success;
612 CurPtr += sizeof(coff_file_header);
614 if (COFFHeader->isImportLibrary())
619 const pe32_header *Header;
620 if ((EC = getObject(Header, Data, base() + CurPtr)))
623 const uint8_t *DataDirAddr;
624 uint64_t DataDirSize;
625 if (Header->Magic == 0x10b) {
627 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
628 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
629 } else if (Header->Magic == 0x20b) {
630 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
631 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
632 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
634 // It's neither PE32 nor PE32+.
635 EC = object_error::parse_failed;
638 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
640 CurPtr += COFFHeader->SizeOfOptionalHeader;
643 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
644 getNumberOfSections() * sizeof(coff_section))))
647 // Initialize the pointer to the symbol table.
648 if (getPointerToSymbolTable() != 0)
649 if ((EC = initSymbolTablePtr()))
652 // Initialize the pointer to the beginning of the import table.
653 if ((EC = initImportTablePtr()))
655 if ((EC = initDelayImportTablePtr()))
658 // Initialize the pointer to the export table.
659 if ((EC = initExportTablePtr()))
662 EC = object_error::success;
665 basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
667 Ret.p = getSymbolTable();
668 return basic_symbol_iterator(SymbolRef(Ret, this));
671 basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
672 // The symbol table ends where the string table begins.
674 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
675 return basic_symbol_iterator(SymbolRef(Ret, this));
678 import_directory_iterator COFFObjectFile::import_directory_begin() const {
679 return import_directory_iterator(
680 ImportDirectoryEntryRef(ImportDirectory, 0, this));
683 import_directory_iterator COFFObjectFile::import_directory_end() const {
684 return import_directory_iterator(
685 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
688 delay_import_directory_iterator
689 COFFObjectFile::delay_import_directory_begin() const {
690 return delay_import_directory_iterator(
691 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
694 delay_import_directory_iterator
695 COFFObjectFile::delay_import_directory_end() const {
696 return delay_import_directory_iterator(
697 DelayImportDirectoryEntryRef(
698 DelayImportDirectory, NumberOfDelayImportDirectory, this));
701 export_directory_iterator COFFObjectFile::export_directory_begin() const {
702 return export_directory_iterator(
703 ExportDirectoryEntryRef(ExportDirectory, 0, this));
706 export_directory_iterator COFFObjectFile::export_directory_end() const {
707 if (!ExportDirectory)
708 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
709 ExportDirectoryEntryRef Ref(ExportDirectory,
710 ExportDirectory->AddressTableEntries, this);
711 return export_directory_iterator(Ref);
714 section_iterator COFFObjectFile::section_begin() const {
716 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
717 return section_iterator(SectionRef(Ret, this));
720 section_iterator COFFObjectFile::section_end() const {
723 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
724 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
725 return section_iterator(SectionRef(Ret, this));
728 uint8_t COFFObjectFile::getBytesInAddress() const {
729 return getArch() == Triple::x86_64 ? 8 : 4;
732 StringRef COFFObjectFile::getFileFormatName() const {
733 switch(getMachine()) {
734 case COFF::IMAGE_FILE_MACHINE_I386:
736 case COFF::IMAGE_FILE_MACHINE_AMD64:
737 return "COFF-x86-64";
738 case COFF::IMAGE_FILE_MACHINE_ARMNT:
741 return "COFF-<unknown arch>";
745 unsigned COFFObjectFile::getArch() const {
746 switch (getMachine()) {
747 case COFF::IMAGE_FILE_MACHINE_I386:
749 case COFF::IMAGE_FILE_MACHINE_AMD64:
750 return Triple::x86_64;
751 case COFF::IMAGE_FILE_MACHINE_ARMNT:
752 return Triple::thumb;
754 return Triple::UnknownArch;
758 std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
760 return object_error::success;
764 COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
765 Res = PE32PlusHeader;
766 return object_error::success;
770 COFFObjectFile::getDataDirectory(uint32_t Index,
771 const data_directory *&Res) const {
772 // Error if if there's no data directory or the index is out of range.
774 return object_error::parse_failed;
775 assert(PE32Header || PE32PlusHeader);
776 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
777 : PE32PlusHeader->NumberOfRvaAndSize;
779 return object_error::parse_failed;
780 Res = &DataDirectory[Index];
781 return object_error::success;
784 std::error_code COFFObjectFile::getSection(int32_t Index,
785 const coff_section *&Result) const {
786 // Check for special index values.
787 if (COFF::isReservedSectionNumber(Index))
789 else if (Index > 0 && static_cast<uint32_t>(Index) <= getNumberOfSections())
790 // We already verified the section table data, so no need to check again.
791 Result = SectionTable + (Index - 1);
793 return object_error::parse_failed;
794 return object_error::success;
797 std::error_code COFFObjectFile::getString(uint32_t Offset,
798 StringRef &Result) const {
799 if (StringTableSize <= 4)
800 // Tried to get a string from an empty string table.
801 return object_error::parse_failed;
802 if (Offset >= StringTableSize)
803 return object_error::unexpected_eof;
804 Result = StringRef(StringTable + Offset);
805 return object_error::success;
808 std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
809 StringRef &Res) const {
810 // Check for string table entry. First 4 bytes are 0.
811 if (Symbol.getStringTableOffset().Zeroes == 0) {
812 uint32_t Offset = Symbol.getStringTableOffset().Offset;
813 if (std::error_code EC = getString(Offset, Res))
815 return object_error::success;
818 if (Symbol.getShortName()[COFF::NameSize - 1] == 0)
819 // Null terminated, let ::strlen figure out the length.
820 Res = StringRef(Symbol.getShortName());
822 // Not null terminated, use all 8 bytes.
823 Res = StringRef(Symbol.getShortName(), COFF::NameSize);
824 return object_error::success;
828 COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
829 const uint8_t *Aux = nullptr;
831 size_t SymbolSize = getSymbolTableEntrySize();
832 if (Symbol.getNumberOfAuxSymbols() > 0) {
833 // AUX data comes immediately after the symbol in COFF
834 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
836 // Verify that the Aux symbol points to a valid entry in the symbol table.
837 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
838 if (Offset < getPointerToSymbolTable() ||
840 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
841 report_fatal_error("Aux Symbol data was outside of symbol table.");
843 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
844 "Aux Symbol data did not point to the beginning of a symbol");
847 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
850 std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
851 StringRef &Res) const {
853 if (Sec->Name[COFF::NameSize - 1] == 0)
854 // Null terminated, let ::strlen figure out the length.
857 // Not null terminated, use all 8 bytes.
858 Name = StringRef(Sec->Name, COFF::NameSize);
860 // Check for string table entry. First byte is '/'.
861 if (Name[0] == '/') {
863 if (Name[1] == '/') {
864 if (decodeBase64StringEntry(Name.substr(2), Offset))
865 return object_error::parse_failed;
867 if (Name.substr(1).getAsInteger(10, Offset))
868 return object_error::parse_failed;
870 if (std::error_code EC = getString(Offset, Name))
875 return object_error::success;
879 COFFObjectFile::getSectionContents(const coff_section *Sec,
880 ArrayRef<uint8_t> &Res) const {
881 // PointerToRawData and SizeOfRawData won't make sense for BSS sections, don't
882 // do anything interesting for them.
883 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
884 "BSS sections don't have contents!");
885 // The only thing that we need to verify is that the contents is contained
886 // within the file bounds. We don't need to make sure it doesn't cover other
887 // data, as there's nothing that says that is not allowed.
888 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
889 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
890 if (ConEnd > uintptr_t(Data.getBufferEnd()))
891 return object_error::parse_failed;
892 Res = makeArrayRef(reinterpret_cast<const uint8_t*>(ConStart),
894 return object_error::success;
897 const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
898 return reinterpret_cast<const coff_relocation*>(Rel.p);
901 void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
902 Rel.p = reinterpret_cast<uintptr_t>(
903 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
906 std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
907 uint64_t &Res) const {
908 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
911 std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
912 uint64_t &Res) const {
913 Res = toRel(Rel)->VirtualAddress;
914 return object_error::success;
917 symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
918 const coff_relocation *R = toRel(Rel);
921 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
922 else if (SymbolTable32)
923 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
925 llvm_unreachable("no symbol table pointer!");
926 return symbol_iterator(SymbolRef(Ref, this));
929 std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
930 uint64_t &Res) const {
931 const coff_relocation* R = toRel(Rel);
933 return object_error::success;
937 COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
938 return toSec(Section.getRawDataRefImpl());
941 COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
943 return toSymb<coff_symbol16>(Ref);
945 return toSymb<coff_symbol32>(Ref);
946 llvm_unreachable("no symbol table pointer!");
949 COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
950 return getCOFFSymbol(Symbol.getRawDataRefImpl());
953 const coff_relocation *
954 COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
955 return toRel(Reloc.getRawDataRefImpl());
958 #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
959 case COFF::reloc_type: \
964 COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
965 SmallVectorImpl<char> &Result) const {
966 const coff_relocation *Reloc = toRel(Rel);
968 switch (getMachine()) {
969 case COFF::IMAGE_FILE_MACHINE_AMD64:
970 switch (Reloc->Type) {
971 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
972 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
973 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
974 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
975 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
976 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
977 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
978 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
979 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
980 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
981 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
982 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
983 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
984 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
985 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
986 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
987 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
992 case COFF::IMAGE_FILE_MACHINE_ARMNT:
993 switch (Reloc->Type) {
994 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
995 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
996 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
997 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
998 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
999 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1000 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1001 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1002 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1003 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1004 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1005 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1006 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1007 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1008 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1013 case COFF::IMAGE_FILE_MACHINE_I386:
1014 switch (Reloc->Type) {
1015 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1016 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1017 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1018 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1019 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1020 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1021 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1022 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1023 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1024 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1025 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1033 Result.append(Res.begin(), Res.end());
1034 return object_error::success;
1037 #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1040 COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
1041 SmallVectorImpl<char> &Result) const {
1042 const coff_relocation *Reloc = toRel(Rel);
1044 ErrorOr<COFFSymbolRef> Symb = getSymbol(Reloc->SymbolTableIndex);
1045 if (std::error_code EC = Symb.getError())
1047 Sym.p = reinterpret_cast<uintptr_t>(Symb->getRawPtr());
1049 if (std::error_code EC = getSymbolName(Sym, SymName))
1051 Result.append(SymName.begin(), SymName.end());
1052 return object_error::success;
1055 bool COFFObjectFile::isRelocatableObject() const {
1056 return !DataDirectory;
1059 bool ImportDirectoryEntryRef::
1060 operator==(const ImportDirectoryEntryRef &Other) const {
1061 return ImportTable == Other.ImportTable && Index == Other.Index;
1064 void ImportDirectoryEntryRef::moveNext() {
1068 std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1069 const import_directory_table_entry *&Result) const {
1070 Result = ImportTable + Index;
1071 return object_error::success;
1074 static imported_symbol_iterator
1075 makeImportedSymbolIterator(const COFFObjectFile *Object,
1076 uintptr_t Ptr, int Index) {
1077 if (Object->getBytesInAddress() == 4) {
1078 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
1079 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
1081 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
1082 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
1085 static imported_symbol_iterator
1086 importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
1087 uintptr_t IntPtr = 0;
1088 Object->getRvaPtr(RVA, IntPtr);
1089 return makeImportedSymbolIterator(Object, IntPtr, 0);
1092 static imported_symbol_iterator
1093 importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
1094 uintptr_t IntPtr = 0;
1095 Object->getRvaPtr(RVA, IntPtr);
1096 // Forward the pointer to the last entry which is null.
1098 if (Object->getBytesInAddress() == 4) {
1099 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1103 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1107 return makeImportedSymbolIterator(Object, IntPtr, Index);
1110 imported_symbol_iterator
1111 ImportDirectoryEntryRef::imported_symbol_begin() const {
1112 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1116 imported_symbol_iterator
1117 ImportDirectoryEntryRef::imported_symbol_end() const {
1118 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1122 std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
1123 uintptr_t IntPtr = 0;
1124 if (std::error_code EC =
1125 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
1127 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1128 return object_error::success;
1132 ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1133 Result = ImportTable[Index].ImportLookupTableRVA;
1134 return object_error::success;
1138 ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1139 Result = ImportTable[Index].ImportAddressTableRVA;
1140 return object_error::success;
1143 std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
1144 const import_lookup_table_entry32 *&Result) const {
1145 uintptr_t IntPtr = 0;
1146 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1147 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1149 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
1150 return object_error::success;
1153 bool DelayImportDirectoryEntryRef::
1154 operator==(const DelayImportDirectoryEntryRef &Other) const {
1155 return Table == Other.Table && Index == Other.Index;
1158 void DelayImportDirectoryEntryRef::moveNext() {
1162 imported_symbol_iterator
1163 DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1164 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1168 imported_symbol_iterator
1169 DelayImportDirectoryEntryRef::imported_symbol_end() const {
1170 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1174 std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1175 uintptr_t IntPtr = 0;
1176 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1178 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1179 return object_error::success;
1182 std::error_code DelayImportDirectoryEntryRef::
1183 getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1185 return object_error::success;
1188 bool ExportDirectoryEntryRef::
1189 operator==(const ExportDirectoryEntryRef &Other) const {
1190 return ExportTable == Other.ExportTable && Index == Other.Index;
1193 void ExportDirectoryEntryRef::moveNext() {
1197 // Returns the name of the current export symbol. If the symbol is exported only
1198 // by ordinal, the empty string is set as a result.
1199 std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
1200 uintptr_t IntPtr = 0;
1201 if (std::error_code EC =
1202 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
1204 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1205 return object_error::success;
1208 // Returns the starting ordinal number.
1210 ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
1211 Result = ExportTable->OrdinalBase;
1212 return object_error::success;
1215 // Returns the export ordinal of the current export symbol.
1216 std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
1217 Result = ExportTable->OrdinalBase + Index;
1218 return object_error::success;
1221 // Returns the address of the current export symbol.
1222 std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
1223 uintptr_t IntPtr = 0;
1224 if (std::error_code EC =
1225 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
1227 const export_address_table_entry *entry =
1228 reinterpret_cast<const export_address_table_entry *>(IntPtr);
1229 Result = entry[Index].ExportRVA;
1230 return object_error::success;
1233 // Returns the name of the current export symbol. If the symbol is exported only
1234 // by ordinal, the empty string is set as a result.
1236 ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
1237 uintptr_t IntPtr = 0;
1238 if (std::error_code EC =
1239 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
1241 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1243 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1245 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1246 I < E; ++I, ++Offset) {
1249 if (std::error_code EC =
1250 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
1252 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
1253 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
1255 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1256 return object_error::success;
1259 return object_error::success;
1262 bool ImportedSymbolRef::
1263 operator==(const ImportedSymbolRef &Other) const {
1264 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1265 && Index == Other.Index;
1268 void ImportedSymbolRef::moveNext() {
1273 ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1276 // If a symbol is imported only by ordinal, it has no name.
1277 if (Entry32[Index].isOrdinal())
1278 return object_error::success;
1279 RVA = Entry32[Index].getHintNameRVA();
1281 if (Entry64[Index].isOrdinal())
1282 return object_error::success;
1283 RVA = Entry64[Index].getHintNameRVA();
1285 uintptr_t IntPtr = 0;
1286 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1288 // +2 because the first two bytes is hint.
1289 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
1290 return object_error::success;
1293 std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1296 if (Entry32[Index].isOrdinal()) {
1297 Result = Entry32[Index].getOrdinal();
1298 return object_error::success;
1300 RVA = Entry32[Index].getHintNameRVA();
1302 if (Entry64[Index].isOrdinal()) {
1303 Result = Entry64[Index].getOrdinal();
1304 return object_error::success;
1306 RVA = Entry64[Index].getHintNameRVA();
1308 uintptr_t IntPtr = 0;
1309 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1311 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
1312 return object_error::success;
1315 ErrorOr<std::unique_ptr<COFFObjectFile>>
1316 ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
1318 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
1321 return std::move(Ret);