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 const coff_section *Sec = toSec(Ref);
275 return Sec->SizeOfRawData;
278 std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
279 StringRef &Result) const {
280 const coff_section *Sec = toSec(Ref);
281 ArrayRef<uint8_t> Res;
282 std::error_code EC = getSectionContents(Sec, Res);
283 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
287 uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
288 const coff_section *Sec = toSec(Ref);
289 return uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
292 bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
293 const coff_section *Sec = toSec(Ref);
294 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
297 bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
298 const coff_section *Sec = toSec(Ref);
299 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
302 bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
303 const coff_section *Sec = toSec(Ref);
304 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
307 bool COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref) const {
308 // FIXME: Unimplemented
312 bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
313 const coff_section *Sec = toSec(Ref);
314 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
317 bool COFFObjectFile::isSectionZeroInit(DataRefImpl Ref) const {
318 // FIXME: Unimplemented.
322 bool COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref) const {
323 // FIXME: Unimplemented.
327 bool COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
328 DataRefImpl SymbRef) const {
329 const coff_section *Sec = toSec(SecRef);
330 COFFSymbolRef Symb = getCOFFSymbol(SymbRef);
331 int32_t SecNumber = (Sec - SectionTable) + 1;
332 return SecNumber == Symb.getSectionNumber();
335 relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
336 const coff_section *Sec = toSec(Ref);
338 if (Sec->NumberOfRelocations == 0) {
341 auto begin = reinterpret_cast<const coff_relocation*>(
342 base() + Sec->PointerToRelocations);
343 if (Sec->hasExtendedRelocations()) {
344 // Skip the first relocation entry repurposed to store the number of
348 Ret.p = reinterpret_cast<uintptr_t>(begin);
350 return relocation_iterator(RelocationRef(Ret, this));
353 static uint32_t getNumberOfRelocations(const coff_section *Sec,
354 const uint8_t *base) {
355 // The field for the number of relocations in COFF section table is only
356 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
357 // NumberOfRelocations field, and the actual relocation count is stored in the
358 // VirtualAddress field in the first relocation entry.
359 if (Sec->hasExtendedRelocations()) {
360 auto *FirstReloc = reinterpret_cast<const coff_relocation*>(
361 base + Sec->PointerToRelocations);
362 return FirstReloc->VirtualAddress;
364 return Sec->NumberOfRelocations;
367 relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
368 const coff_section *Sec = toSec(Ref);
370 if (Sec->NumberOfRelocations == 0) {
373 auto begin = reinterpret_cast<const coff_relocation*>(
374 base() + Sec->PointerToRelocations);
375 uint32_t NumReloc = getNumberOfRelocations(Sec, base());
376 Ret.p = reinterpret_cast<uintptr_t>(begin + NumReloc);
378 return relocation_iterator(RelocationRef(Ret, this));
381 // Initialize the pointer to the symbol table.
382 std::error_code COFFObjectFile::initSymbolTablePtr() {
384 if (std::error_code EC =
385 getObject(SymbolTable16, Data, base() + getPointerToSymbolTable(),
386 getNumberOfSymbols() * getSymbolTableEntrySize()))
389 if (COFFBigObjHeader)
390 if (std::error_code EC =
391 getObject(SymbolTable32, Data, base() + getPointerToSymbolTable(),
392 getNumberOfSymbols() * getSymbolTableEntrySize()))
395 // Find string table. The first four byte of the string table contains the
396 // total size of the string table, including the size field itself. If the
397 // string table is empty, the value of the first four byte would be 4.
398 const uint8_t *StringTableAddr =
399 base() + getPointerToSymbolTable() +
400 getNumberOfSymbols() * getSymbolTableEntrySize();
401 const ulittle32_t *StringTableSizePtr;
402 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
404 StringTableSize = *StringTableSizePtr;
405 if (std::error_code EC =
406 getObject(StringTable, Data, StringTableAddr, StringTableSize))
409 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
410 // tools like cvtres write a size of 0 for an empty table instead of 4.
411 if (StringTableSize < 4)
414 // Check that the string table is null terminated if has any in it.
415 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
416 return object_error::parse_failed;
417 return object_error::success;
420 // Returns the file offset for the given VA.
421 std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
422 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
423 : (uint64_t)PE32PlusHeader->ImageBase;
424 uint64_t Rva = Addr - ImageBase;
425 assert(Rva <= UINT32_MAX);
426 return getRvaPtr((uint32_t)Rva, Res);
429 // Returns the file offset for the given RVA.
430 std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
431 for (const SectionRef &S : sections()) {
432 const coff_section *Section = getCOFFSection(S);
433 uint32_t SectionStart = Section->VirtualAddress;
434 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
435 if (SectionStart <= Addr && Addr < SectionEnd) {
436 uint32_t Offset = Addr - SectionStart;
437 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
438 return object_error::success;
441 return object_error::parse_failed;
444 // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
446 std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
447 StringRef &Name) const {
448 uintptr_t IntPtr = 0;
449 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
451 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
452 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
453 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
454 return object_error::success;
457 // Find the import table.
458 std::error_code COFFObjectFile::initImportTablePtr() {
459 // First, we get the RVA of the import table. If the file lacks a pointer to
460 // the import table, do nothing.
461 const data_directory *DataEntry;
462 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
463 return object_error::success;
465 // Do nothing if the pointer to import table is NULL.
466 if (DataEntry->RelativeVirtualAddress == 0)
467 return object_error::success;
469 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
470 // -1 because the last entry is the null entry.
471 NumberOfImportDirectory = DataEntry->Size /
472 sizeof(import_directory_table_entry) - 1;
474 // Find the section that contains the RVA. This is needed because the RVA is
475 // the import table's memory address which is different from its file offset.
476 uintptr_t IntPtr = 0;
477 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
479 ImportDirectory = reinterpret_cast<
480 const import_directory_table_entry *>(IntPtr);
481 return object_error::success;
484 // Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
485 std::error_code COFFObjectFile::initDelayImportTablePtr() {
486 const data_directory *DataEntry;
487 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
488 return object_error::success;
489 if (DataEntry->RelativeVirtualAddress == 0)
490 return object_error::success;
492 uint32_t RVA = DataEntry->RelativeVirtualAddress;
493 NumberOfDelayImportDirectory = DataEntry->Size /
494 sizeof(delay_import_directory_table_entry) - 1;
496 uintptr_t IntPtr = 0;
497 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
499 DelayImportDirectory = reinterpret_cast<
500 const delay_import_directory_table_entry *>(IntPtr);
501 return object_error::success;
504 // Find the export table.
505 std::error_code COFFObjectFile::initExportTablePtr() {
506 // First, we get the RVA of the export table. If the file lacks a pointer to
507 // the export table, do nothing.
508 const data_directory *DataEntry;
509 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
510 return object_error::success;
512 // Do nothing if the pointer to export table is NULL.
513 if (DataEntry->RelativeVirtualAddress == 0)
514 return object_error::success;
516 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
517 uintptr_t IntPtr = 0;
518 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
521 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
522 return object_error::success;
525 COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
526 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
527 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
528 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
529 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
530 ImportDirectory(nullptr), NumberOfImportDirectory(0),
531 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
532 ExportDirectory(nullptr) {
533 // Check that we at least have enough room for a header.
534 if (!checkSize(Data, EC, sizeof(coff_file_header)))
537 // The current location in the file where we are looking at.
540 // PE header is optional and is present only in executables. If it exists,
541 // it is placed right after COFF header.
542 bool HasPEHeader = false;
544 // Check if this is a PE/COFF file.
545 if (base()[0] == 0x4d && base()[1] == 0x5a) {
546 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
547 // PE signature to find 'normal' COFF header.
548 if (!checkSize(Data, EC, 0x3c + 8))
550 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
551 // Check the PE magic bytes. ("PE\0\0")
552 if (std::memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) !=
554 EC = object_error::parse_failed;
557 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
561 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
564 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
565 // import libraries share a common prefix but bigobj is more restrictive.
566 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
567 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
568 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
569 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
572 // Verify that we are dealing with bigobj.
573 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
574 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
575 sizeof(COFF::BigObjMagic)) == 0) {
576 COFFHeader = nullptr;
577 CurPtr += sizeof(coff_bigobj_file_header);
579 // It's not a bigobj.
580 COFFBigObjHeader = nullptr;
584 // The prior checkSize call may have failed. This isn't a hard error
585 // because we were just trying to sniff out bigobj.
586 EC = object_error::success;
587 CurPtr += sizeof(coff_file_header);
589 if (COFFHeader->isImportLibrary())
594 const pe32_header *Header;
595 if ((EC = getObject(Header, Data, base() + CurPtr)))
598 const uint8_t *DataDirAddr;
599 uint64_t DataDirSize;
600 if (Header->Magic == 0x10b) {
602 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
603 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
604 } else if (Header->Magic == 0x20b) {
605 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
606 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
607 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
609 // It's neither PE32 nor PE32+.
610 EC = object_error::parse_failed;
613 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
615 CurPtr += COFFHeader->SizeOfOptionalHeader;
618 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
619 getNumberOfSections() * sizeof(coff_section))))
622 // Initialize the pointer to the symbol table.
623 if (getPointerToSymbolTable() != 0)
624 if ((EC = initSymbolTablePtr()))
627 // Initialize the pointer to the beginning of the import table.
628 if ((EC = initImportTablePtr()))
630 if ((EC = initDelayImportTablePtr()))
633 // Initialize the pointer to the export table.
634 if ((EC = initExportTablePtr()))
637 EC = object_error::success;
640 basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
642 Ret.p = getSymbolTable();
643 return basic_symbol_iterator(SymbolRef(Ret, this));
646 basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
647 // The symbol table ends where the string table begins.
649 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
650 return basic_symbol_iterator(SymbolRef(Ret, this));
653 import_directory_iterator COFFObjectFile::import_directory_begin() const {
654 return import_directory_iterator(
655 ImportDirectoryEntryRef(ImportDirectory, 0, this));
658 import_directory_iterator COFFObjectFile::import_directory_end() const {
659 return import_directory_iterator(
660 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
663 delay_import_directory_iterator
664 COFFObjectFile::delay_import_directory_begin() const {
665 return delay_import_directory_iterator(
666 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
669 delay_import_directory_iterator
670 COFFObjectFile::delay_import_directory_end() const {
671 return delay_import_directory_iterator(
672 DelayImportDirectoryEntryRef(
673 DelayImportDirectory, NumberOfDelayImportDirectory, this));
676 export_directory_iterator COFFObjectFile::export_directory_begin() const {
677 return export_directory_iterator(
678 ExportDirectoryEntryRef(ExportDirectory, 0, this));
681 export_directory_iterator COFFObjectFile::export_directory_end() const {
682 if (!ExportDirectory)
683 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
684 ExportDirectoryEntryRef Ref(ExportDirectory,
685 ExportDirectory->AddressTableEntries, this);
686 return export_directory_iterator(Ref);
689 section_iterator COFFObjectFile::section_begin() const {
691 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
692 return section_iterator(SectionRef(Ret, this));
695 section_iterator COFFObjectFile::section_end() const {
698 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
699 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
700 return section_iterator(SectionRef(Ret, this));
703 uint8_t COFFObjectFile::getBytesInAddress() const {
704 return getArch() == Triple::x86_64 ? 8 : 4;
707 StringRef COFFObjectFile::getFileFormatName() const {
708 switch(getMachine()) {
709 case COFF::IMAGE_FILE_MACHINE_I386:
711 case COFF::IMAGE_FILE_MACHINE_AMD64:
712 return "COFF-x86-64";
713 case COFF::IMAGE_FILE_MACHINE_ARMNT:
716 return "COFF-<unknown arch>";
720 unsigned COFFObjectFile::getArch() const {
721 switch (getMachine()) {
722 case COFF::IMAGE_FILE_MACHINE_I386:
724 case COFF::IMAGE_FILE_MACHINE_AMD64:
725 return Triple::x86_64;
726 case COFF::IMAGE_FILE_MACHINE_ARMNT:
727 return Triple::thumb;
729 return Triple::UnknownArch;
733 std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
735 return object_error::success;
739 COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
740 Res = PE32PlusHeader;
741 return object_error::success;
745 COFFObjectFile::getDataDirectory(uint32_t Index,
746 const data_directory *&Res) const {
747 // Error if if there's no data directory or the index is out of range.
749 return object_error::parse_failed;
750 assert(PE32Header || PE32PlusHeader);
751 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
752 : PE32PlusHeader->NumberOfRvaAndSize;
754 return object_error::parse_failed;
755 Res = &DataDirectory[Index];
756 return object_error::success;
759 std::error_code COFFObjectFile::getSection(int32_t Index,
760 const coff_section *&Result) const {
761 // Check for special index values.
762 if (COFF::isReservedSectionNumber(Index))
764 else if (Index > 0 && static_cast<uint32_t>(Index) <= getNumberOfSections())
765 // We already verified the section table data, so no need to check again.
766 Result = SectionTable + (Index - 1);
768 return object_error::parse_failed;
769 return object_error::success;
772 std::error_code COFFObjectFile::getString(uint32_t Offset,
773 StringRef &Result) const {
774 if (StringTableSize <= 4)
775 // Tried to get a string from an empty string table.
776 return object_error::parse_failed;
777 if (Offset >= StringTableSize)
778 return object_error::unexpected_eof;
779 Result = StringRef(StringTable + Offset);
780 return object_error::success;
783 std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
784 StringRef &Res) const {
785 // Check for string table entry. First 4 bytes are 0.
786 if (Symbol.getStringTableOffset().Zeroes == 0) {
787 uint32_t Offset = Symbol.getStringTableOffset().Offset;
788 if (std::error_code EC = getString(Offset, Res))
790 return object_error::success;
793 if (Symbol.getShortName()[COFF::NameSize - 1] == 0)
794 // Null terminated, let ::strlen figure out the length.
795 Res = StringRef(Symbol.getShortName());
797 // Not null terminated, use all 8 bytes.
798 Res = StringRef(Symbol.getShortName(), COFF::NameSize);
799 return object_error::success;
803 COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
804 const uint8_t *Aux = nullptr;
806 size_t SymbolSize = getSymbolTableEntrySize();
807 if (Symbol.getNumberOfAuxSymbols() > 0) {
808 // AUX data comes immediately after the symbol in COFF
809 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
811 // Verify that the Aux symbol points to a valid entry in the symbol table.
812 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
813 if (Offset < getPointerToSymbolTable() ||
815 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
816 report_fatal_error("Aux Symbol data was outside of symbol table.");
818 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
819 "Aux Symbol data did not point to the beginning of a symbol");
822 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
825 std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
826 StringRef &Res) const {
828 if (Sec->Name[COFF::NameSize - 1] == 0)
829 // Null terminated, let ::strlen figure out the length.
832 // Not null terminated, use all 8 bytes.
833 Name = StringRef(Sec->Name, COFF::NameSize);
835 // Check for string table entry. First byte is '/'.
836 if (Name[0] == '/') {
838 if (Name[1] == '/') {
839 if (decodeBase64StringEntry(Name.substr(2), Offset))
840 return object_error::parse_failed;
842 if (Name.substr(1).getAsInteger(10, Offset))
843 return object_error::parse_failed;
845 if (std::error_code EC = getString(Offset, Name))
850 return object_error::success;
854 COFFObjectFile::getSectionContents(const coff_section *Sec,
855 ArrayRef<uint8_t> &Res) const {
856 // PointerToRawData and SizeOfRawData won't make sense for BSS sections, don't
857 // do anything interesting for them.
858 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
859 "BSS sections don't have contents!");
860 // The only thing that we need to verify is that the contents is contained
861 // within the file bounds. We don't need to make sure it doesn't cover other
862 // data, as there's nothing that says that is not allowed.
863 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
864 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
865 if (ConEnd > uintptr_t(Data.getBufferEnd()))
866 return object_error::parse_failed;
867 Res = makeArrayRef(reinterpret_cast<const uint8_t*>(ConStart),
869 return object_error::success;
872 const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
873 return reinterpret_cast<const coff_relocation*>(Rel.p);
876 void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
877 Rel.p = reinterpret_cast<uintptr_t>(
878 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
881 std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
882 uint64_t &Res) const {
883 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
886 std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
887 uint64_t &Res) const {
888 Res = toRel(Rel)->VirtualAddress;
889 return object_error::success;
892 symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
893 const coff_relocation *R = toRel(Rel);
896 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
897 else if (SymbolTable32)
898 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
900 llvm_unreachable("no symbol table pointer!");
901 return symbol_iterator(SymbolRef(Ref, this));
904 std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
905 uint64_t &Res) const {
906 const coff_relocation* R = toRel(Rel);
908 return object_error::success;
912 COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
913 return toSec(Section.getRawDataRefImpl());
916 COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
918 return toSymb<coff_symbol16>(Ref);
920 return toSymb<coff_symbol32>(Ref);
921 llvm_unreachable("no symbol table pointer!");
924 COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
925 return getCOFFSymbol(Symbol.getRawDataRefImpl());
928 const coff_relocation *
929 COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
930 return toRel(Reloc.getRawDataRefImpl());
933 #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
934 case COFF::reloc_type: \
939 COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
940 SmallVectorImpl<char> &Result) const {
941 const coff_relocation *Reloc = toRel(Rel);
943 switch (getMachine()) {
944 case COFF::IMAGE_FILE_MACHINE_AMD64:
945 switch (Reloc->Type) {
946 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
947 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
948 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
949 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
950 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
951 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
952 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
953 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
954 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
955 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
956 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
957 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
958 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
959 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
960 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
961 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
962 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
967 case COFF::IMAGE_FILE_MACHINE_ARMNT:
968 switch (Reloc->Type) {
969 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
970 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
971 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
972 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
973 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
974 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
975 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
976 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
977 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
978 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
979 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
980 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
981 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
982 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
983 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
988 case COFF::IMAGE_FILE_MACHINE_I386:
989 switch (Reloc->Type) {
990 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
991 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
992 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
993 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
994 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
995 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
996 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
997 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
998 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
999 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1000 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1008 Result.append(Res.begin(), Res.end());
1009 return object_error::success;
1012 #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1015 COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
1016 SmallVectorImpl<char> &Result) const {
1017 const coff_relocation *Reloc = toRel(Rel);
1019 ErrorOr<COFFSymbolRef> Symb = getSymbol(Reloc->SymbolTableIndex);
1020 if (std::error_code EC = Symb.getError())
1022 Sym.p = reinterpret_cast<uintptr_t>(Symb->getRawPtr());
1024 if (std::error_code EC = getSymbolName(Sym, SymName))
1026 Result.append(SymName.begin(), SymName.end());
1027 return object_error::success;
1030 bool COFFObjectFile::isRelocatableObject() const {
1031 return !DataDirectory;
1034 bool ImportDirectoryEntryRef::
1035 operator==(const ImportDirectoryEntryRef &Other) const {
1036 return ImportTable == Other.ImportTable && Index == Other.Index;
1039 void ImportDirectoryEntryRef::moveNext() {
1043 std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1044 const import_directory_table_entry *&Result) const {
1045 Result = ImportTable + Index;
1046 return object_error::success;
1049 static imported_symbol_iterator
1050 makeImportedSymbolIterator(const COFFObjectFile *Object,
1051 uintptr_t Ptr, int Index) {
1052 if (Object->getBytesInAddress() == 4) {
1053 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
1054 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
1056 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
1057 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
1060 static imported_symbol_iterator
1061 importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
1062 uintptr_t IntPtr = 0;
1063 Object->getRvaPtr(RVA, IntPtr);
1064 return makeImportedSymbolIterator(Object, IntPtr, 0);
1067 static imported_symbol_iterator
1068 importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
1069 uintptr_t IntPtr = 0;
1070 Object->getRvaPtr(RVA, IntPtr);
1071 // Forward the pointer to the last entry which is null.
1073 if (Object->getBytesInAddress() == 4) {
1074 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1078 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1082 return makeImportedSymbolIterator(Object, IntPtr, Index);
1085 imported_symbol_iterator
1086 ImportDirectoryEntryRef::imported_symbol_begin() const {
1087 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1091 imported_symbol_iterator
1092 ImportDirectoryEntryRef::imported_symbol_end() const {
1093 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1097 std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
1098 uintptr_t IntPtr = 0;
1099 if (std::error_code EC =
1100 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
1102 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1103 return object_error::success;
1107 ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1108 Result = ImportTable[Index].ImportLookupTableRVA;
1109 return object_error::success;
1113 ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1114 Result = ImportTable[Index].ImportAddressTableRVA;
1115 return object_error::success;
1118 std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
1119 const import_lookup_table_entry32 *&Result) const {
1120 uintptr_t IntPtr = 0;
1121 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1122 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1124 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
1125 return object_error::success;
1128 bool DelayImportDirectoryEntryRef::
1129 operator==(const DelayImportDirectoryEntryRef &Other) const {
1130 return Table == Other.Table && Index == Other.Index;
1133 void DelayImportDirectoryEntryRef::moveNext() {
1137 imported_symbol_iterator
1138 DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1139 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1143 imported_symbol_iterator
1144 DelayImportDirectoryEntryRef::imported_symbol_end() const {
1145 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1149 std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1150 uintptr_t IntPtr = 0;
1151 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1153 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1154 return object_error::success;
1157 std::error_code DelayImportDirectoryEntryRef::
1158 getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1160 return object_error::success;
1163 bool ExportDirectoryEntryRef::
1164 operator==(const ExportDirectoryEntryRef &Other) const {
1165 return ExportTable == Other.ExportTable && Index == Other.Index;
1168 void ExportDirectoryEntryRef::moveNext() {
1172 // Returns the name of the current export symbol. If the symbol is exported only
1173 // by ordinal, the empty string is set as a result.
1174 std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
1175 uintptr_t IntPtr = 0;
1176 if (std::error_code EC =
1177 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
1179 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1180 return object_error::success;
1183 // Returns the starting ordinal number.
1185 ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
1186 Result = ExportTable->OrdinalBase;
1187 return object_error::success;
1190 // Returns the export ordinal of the current export symbol.
1191 std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
1192 Result = ExportTable->OrdinalBase + Index;
1193 return object_error::success;
1196 // Returns the address of the current export symbol.
1197 std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
1198 uintptr_t IntPtr = 0;
1199 if (std::error_code EC =
1200 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
1202 const export_address_table_entry *entry =
1203 reinterpret_cast<const export_address_table_entry *>(IntPtr);
1204 Result = entry[Index].ExportRVA;
1205 return object_error::success;
1208 // Returns the name of the current export symbol. If the symbol is exported only
1209 // by ordinal, the empty string is set as a result.
1211 ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
1212 uintptr_t IntPtr = 0;
1213 if (std::error_code EC =
1214 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
1216 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1218 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1220 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1221 I < E; ++I, ++Offset) {
1224 if (std::error_code EC =
1225 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
1227 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
1228 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
1230 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1231 return object_error::success;
1234 return object_error::success;
1237 bool ImportedSymbolRef::
1238 operator==(const ImportedSymbolRef &Other) const {
1239 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1240 && Index == Other.Index;
1243 void ImportedSymbolRef::moveNext() {
1248 ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1251 // If a symbol is imported only by ordinal, it has no name.
1252 if (Entry32[Index].isOrdinal())
1253 return object_error::success;
1254 RVA = Entry32[Index].getHintNameRVA();
1256 if (Entry64[Index].isOrdinal())
1257 return object_error::success;
1258 RVA = Entry64[Index].getHintNameRVA();
1260 uintptr_t IntPtr = 0;
1261 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1263 // +2 because the first two bytes is hint.
1264 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
1265 return object_error::success;
1268 std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1271 if (Entry32[Index].isOrdinal()) {
1272 Result = Entry32[Index].getOrdinal();
1273 return object_error::success;
1275 RVA = Entry32[Index].getHintNameRVA();
1277 if (Entry64[Index].isOrdinal()) {
1278 Result = Entry64[Index].getOrdinal();
1279 return object_error::success;
1281 RVA = Entry64[Index].getHintNameRVA();
1283 uintptr_t IntPtr = 0;
1284 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1286 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
1287 return object_error::success;
1290 ErrorOr<std::unique_ptr<COFFObjectFile>>
1291 ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
1293 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
1296 return std::move(Ret);