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);
151 if (Symb.isAnyUndefined()) {
152 Result = UnknownAddressOrSize;
153 return object_error::success;
155 if (Symb.isCommon()) {
156 Result = UnknownAddressOrSize;
157 return object_error::success;
159 int32_t SectionNumber = Symb.getSectionNumber();
160 if (!COFF::isReservedSectionNumber(SectionNumber)) {
161 const coff_section *Section = nullptr;
162 if (std::error_code EC = getSection(SectionNumber, Section))
165 Result = Section->VirtualAddress + Symb.getValue();
166 return object_error::success;
169 Result = Symb.getValue();
170 return object_error::success;
173 std::error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
174 SymbolRef::Type &Result) const {
175 COFFSymbolRef Symb = getCOFFSymbol(Ref);
176 int32_t SectionNumber = Symb.getSectionNumber();
177 Result = SymbolRef::ST_Other;
179 if (Symb.isAnyUndefined()) {
180 Result = SymbolRef::ST_Unknown;
181 } else if (Symb.isFunctionDefinition()) {
182 Result = SymbolRef::ST_Function;
183 } else if (Symb.isCommon()) {
184 Result = SymbolRef::ST_Data;
185 } else if (Symb.isFileRecord()) {
186 Result = SymbolRef::ST_File;
187 } else if (SectionNumber == COFF::IMAGE_SYM_DEBUG) {
188 Result = SymbolRef::ST_Debug;
189 } else if (!COFF::isReservedSectionNumber(SectionNumber)) {
190 const coff_section *Section = nullptr;
191 if (std::error_code EC = getSection(SectionNumber, Section))
193 uint32_t Characteristics = Section->Characteristics;
194 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
195 Result = SymbolRef::ST_Function;
196 else if (Characteristics & (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
197 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA))
198 Result = SymbolRef::ST_Data;
200 return object_error::success;
203 uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
204 COFFSymbolRef Symb = getCOFFSymbol(Ref);
205 uint32_t Result = SymbolRef::SF_None;
207 if (Symb.isExternal() || Symb.isWeakExternal())
208 Result |= SymbolRef::SF_Global;
210 if (Symb.isWeakExternal())
211 Result |= SymbolRef::SF_Weak;
213 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
214 Result |= SymbolRef::SF_Absolute;
216 if (Symb.isFileRecord())
217 Result |= SymbolRef::SF_FormatSpecific;
219 if (Symb.isSectionDefinition())
220 Result |= SymbolRef::SF_FormatSpecific;
223 Result |= SymbolRef::SF_Common;
225 if (Symb.isAnyUndefined())
226 Result |= SymbolRef::SF_Undefined;
231 std::error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref,
232 uint64_t &Result) const {
233 COFFSymbolRef Symb = getCOFFSymbol(Ref);
235 if (Symb.isAnyUndefined()) {
236 Result = UnknownAddressOrSize;
237 return object_error::success;
239 if (Symb.isCommon()) {
240 Result = Symb.getValue();
241 return object_error::success;
244 // Let's attempt to get the size of the symbol by looking at the address of
245 // the symbol after the symbol in question.
247 if (std::error_code EC = getSymbolAddress(Ref, SymbAddr))
249 int32_t SectionNumber = Symb.getSectionNumber();
250 if (COFF::isReservedSectionNumber(SectionNumber)) {
251 // Absolute and debug symbols aren't sorted in any interesting way.
253 return object_error::success;
255 const section_iterator SecEnd = section_end();
256 uint64_t AfterAddr = UnknownAddressOrSize;
257 for (const symbol_iterator &SymbI : symbols()) {
258 section_iterator SecI = SecEnd;
259 if (std::error_code EC = SymbI->getSection(SecI))
261 // Check the symbol's section, skip it if it's in the wrong section.
262 // First, make sure it is in any section.
265 // Second, make sure it is in the same section as the symbol in question.
266 if (!sectionContainsSymbol(SecI->getRawDataRefImpl(), Ref))
269 if (std::error_code EC = SymbI->getAddress(Addr))
271 // We want to compare our symbol in question with the closest possible
272 // symbol that comes after.
273 if (AfterAddr > Addr && Addr > SymbAddr)
276 if (AfterAddr == UnknownAddressOrSize) {
277 // No symbol comes after this one, assume that everything after our symbol
279 const coff_section *Section = nullptr;
280 if (std::error_code EC = getSection(SectionNumber, Section))
282 Result = Section->SizeOfRawData - Symb.getValue();
284 // Take the difference between our symbol and the symbol that comes after
286 Result = AfterAddr - SymbAddr;
289 return object_error::success;
293 COFFObjectFile::getSymbolSection(DataRefImpl Ref,
294 section_iterator &Result) const {
295 COFFSymbolRef Symb = getCOFFSymbol(Ref);
296 if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
297 Result = section_end();
299 const coff_section *Sec = nullptr;
300 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
303 Ref.p = reinterpret_cast<uintptr_t>(Sec);
304 Result = section_iterator(SectionRef(Ref, this));
306 return object_error::success;
309 void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
310 const coff_section *Sec = toSec(Ref);
312 Ref.p = reinterpret_cast<uintptr_t>(Sec);
315 std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
316 StringRef &Result) const {
317 const coff_section *Sec = toSec(Ref);
318 return getSectionName(Sec, Result);
321 uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
322 const coff_section *Sec = toSec(Ref);
323 return Sec->VirtualAddress;
326 uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
327 return getSectionSize(toSec(Ref));
330 std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
331 StringRef &Result) const {
332 const coff_section *Sec = toSec(Ref);
333 ArrayRef<uint8_t> Res;
334 std::error_code EC = getSectionContents(Sec, Res);
335 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
339 uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
340 const coff_section *Sec = toSec(Ref);
341 return uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
344 bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
345 const coff_section *Sec = toSec(Ref);
346 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
349 bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
350 const coff_section *Sec = toSec(Ref);
351 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
354 bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
355 const coff_section *Sec = toSec(Ref);
356 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
359 bool COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref) const {
360 // FIXME: Unimplemented
364 bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
365 const coff_section *Sec = toSec(Ref);
366 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
369 bool COFFObjectFile::isSectionZeroInit(DataRefImpl Ref) const {
370 // FIXME: Unimplemented.
374 bool COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref) const {
375 // FIXME: Unimplemented.
379 bool COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
380 DataRefImpl SymbRef) const {
381 const coff_section *Sec = toSec(SecRef);
382 COFFSymbolRef Symb = getCOFFSymbol(SymbRef);
383 int32_t SecNumber = (Sec - SectionTable) + 1;
384 return SecNumber == Symb.getSectionNumber();
387 relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
388 const coff_section *Sec = toSec(Ref);
390 if (Sec->NumberOfRelocations == 0) {
393 auto begin = reinterpret_cast<const coff_relocation*>(
394 base() + Sec->PointerToRelocations);
395 if (Sec->hasExtendedRelocations()) {
396 // Skip the first relocation entry repurposed to store the number of
400 Ret.p = reinterpret_cast<uintptr_t>(begin);
402 return relocation_iterator(RelocationRef(Ret, this));
405 static uint32_t getNumberOfRelocations(const coff_section *Sec,
406 const uint8_t *base) {
407 // The field for the number of relocations in COFF section table is only
408 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
409 // NumberOfRelocations field, and the actual relocation count is stored in the
410 // VirtualAddress field in the first relocation entry.
411 if (Sec->hasExtendedRelocations()) {
412 auto *FirstReloc = reinterpret_cast<const coff_relocation*>(
413 base + Sec->PointerToRelocations);
414 return FirstReloc->VirtualAddress;
416 return Sec->NumberOfRelocations;
419 relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
420 const coff_section *Sec = toSec(Ref);
422 if (Sec->NumberOfRelocations == 0) {
425 auto begin = reinterpret_cast<const coff_relocation*>(
426 base() + Sec->PointerToRelocations);
427 uint32_t NumReloc = getNumberOfRelocations(Sec, base());
428 Ret.p = reinterpret_cast<uintptr_t>(begin + NumReloc);
430 return relocation_iterator(RelocationRef(Ret, this));
433 // Initialize the pointer to the symbol table.
434 std::error_code COFFObjectFile::initSymbolTablePtr() {
436 if (std::error_code EC =
437 getObject(SymbolTable16, Data, base() + getPointerToSymbolTable(),
438 getNumberOfSymbols() * getSymbolTableEntrySize()))
441 if (COFFBigObjHeader)
442 if (std::error_code EC =
443 getObject(SymbolTable32, Data, base() + getPointerToSymbolTable(),
444 getNumberOfSymbols() * getSymbolTableEntrySize()))
447 // Find string table. The first four byte of the string table contains the
448 // total size of the string table, including the size field itself. If the
449 // string table is empty, the value of the first four byte would be 4.
450 const uint8_t *StringTableAddr =
451 base() + getPointerToSymbolTable() +
452 getNumberOfSymbols() * getSymbolTableEntrySize();
453 const ulittle32_t *StringTableSizePtr;
454 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
456 StringTableSize = *StringTableSizePtr;
457 if (std::error_code EC =
458 getObject(StringTable, Data, StringTableAddr, StringTableSize))
461 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
462 // tools like cvtres write a size of 0 for an empty table instead of 4.
463 if (StringTableSize < 4)
466 // Check that the string table is null terminated if has any in it.
467 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
468 return object_error::parse_failed;
469 return object_error::success;
472 // Returns the file offset for the given VA.
473 std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
474 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
475 : (uint64_t)PE32PlusHeader->ImageBase;
476 uint64_t Rva = Addr - ImageBase;
477 assert(Rva <= UINT32_MAX);
478 return getRvaPtr((uint32_t)Rva, Res);
481 // Returns the file offset for the given RVA.
482 std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
483 for (const SectionRef &S : sections()) {
484 const coff_section *Section = getCOFFSection(S);
485 uint32_t SectionStart = Section->VirtualAddress;
486 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
487 if (SectionStart <= Addr && Addr < SectionEnd) {
488 uint32_t Offset = Addr - SectionStart;
489 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
490 return object_error::success;
493 return object_error::parse_failed;
496 // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
498 std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
499 StringRef &Name) const {
500 uintptr_t IntPtr = 0;
501 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
503 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
504 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
505 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
506 return object_error::success;
509 // Find the import table.
510 std::error_code COFFObjectFile::initImportTablePtr() {
511 // First, we get the RVA of the import table. If the file lacks a pointer to
512 // the import table, do nothing.
513 const data_directory *DataEntry;
514 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
515 return object_error::success;
517 // Do nothing if the pointer to import table is NULL.
518 if (DataEntry->RelativeVirtualAddress == 0)
519 return object_error::success;
521 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
522 // -1 because the last entry is the null entry.
523 NumberOfImportDirectory = DataEntry->Size /
524 sizeof(import_directory_table_entry) - 1;
526 // Find the section that contains the RVA. This is needed because the RVA is
527 // the import table's memory address which is different from its file offset.
528 uintptr_t IntPtr = 0;
529 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
531 ImportDirectory = reinterpret_cast<
532 const import_directory_table_entry *>(IntPtr);
533 return object_error::success;
536 // Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
537 std::error_code COFFObjectFile::initDelayImportTablePtr() {
538 const data_directory *DataEntry;
539 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
540 return object_error::success;
541 if (DataEntry->RelativeVirtualAddress == 0)
542 return object_error::success;
544 uint32_t RVA = DataEntry->RelativeVirtualAddress;
545 NumberOfDelayImportDirectory = DataEntry->Size /
546 sizeof(delay_import_directory_table_entry) - 1;
548 uintptr_t IntPtr = 0;
549 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
551 DelayImportDirectory = reinterpret_cast<
552 const delay_import_directory_table_entry *>(IntPtr);
553 return object_error::success;
556 // Find the export table.
557 std::error_code COFFObjectFile::initExportTablePtr() {
558 // First, we get the RVA of the export table. If the file lacks a pointer to
559 // the export table, do nothing.
560 const data_directory *DataEntry;
561 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
562 return object_error::success;
564 // Do nothing if the pointer to export table is NULL.
565 if (DataEntry->RelativeVirtualAddress == 0)
566 return object_error::success;
568 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
569 uintptr_t IntPtr = 0;
570 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
573 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
574 return object_error::success;
577 COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
578 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
579 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
580 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
581 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
582 ImportDirectory(nullptr), NumberOfImportDirectory(0),
583 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
584 ExportDirectory(nullptr) {
585 // Check that we at least have enough room for a header.
586 if (!checkSize(Data, EC, sizeof(coff_file_header)))
589 // The current location in the file where we are looking at.
592 // PE header is optional and is present only in executables. If it exists,
593 // it is placed right after COFF header.
594 bool HasPEHeader = false;
596 // Check if this is a PE/COFF file.
597 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
598 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
599 // PE signature to find 'normal' COFF header.
600 const auto *DH = reinterpret_cast<const dos_header *>(base());
601 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
602 CurPtr = DH->AddressOfNewExeHeader;
603 // Check the PE magic bytes. ("PE\0\0")
604 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
605 EC = object_error::parse_failed;
608 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
613 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
616 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
617 // import libraries share a common prefix but bigobj is more restrictive.
618 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
619 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
620 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
621 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
624 // Verify that we are dealing with bigobj.
625 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
626 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
627 sizeof(COFF::BigObjMagic)) == 0) {
628 COFFHeader = nullptr;
629 CurPtr += sizeof(coff_bigobj_file_header);
631 // It's not a bigobj.
632 COFFBigObjHeader = nullptr;
636 // The prior checkSize call may have failed. This isn't a hard error
637 // because we were just trying to sniff out bigobj.
638 EC = object_error::success;
639 CurPtr += sizeof(coff_file_header);
641 if (COFFHeader->isImportLibrary())
646 const pe32_header *Header;
647 if ((EC = getObject(Header, Data, base() + CurPtr)))
650 const uint8_t *DataDirAddr;
651 uint64_t DataDirSize;
652 if (Header->Magic == COFF::PE32Header::PE32) {
654 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
655 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
656 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
657 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
658 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
659 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
661 // It's neither PE32 nor PE32+.
662 EC = object_error::parse_failed;
665 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
667 CurPtr += COFFHeader->SizeOfOptionalHeader;
670 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
671 getNumberOfSections() * sizeof(coff_section))))
674 // Initialize the pointer to the symbol table.
675 if (getPointerToSymbolTable() != 0)
676 if ((EC = initSymbolTablePtr()))
679 // Initialize the pointer to the beginning of the import table.
680 if ((EC = initImportTablePtr()))
682 if ((EC = initDelayImportTablePtr()))
685 // Initialize the pointer to the export table.
686 if ((EC = initExportTablePtr()))
689 EC = object_error::success;
692 basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
694 Ret.p = getSymbolTable();
695 return basic_symbol_iterator(SymbolRef(Ret, this));
698 basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
699 // The symbol table ends where the string table begins.
701 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
702 return basic_symbol_iterator(SymbolRef(Ret, this));
705 import_directory_iterator COFFObjectFile::import_directory_begin() const {
706 return import_directory_iterator(
707 ImportDirectoryEntryRef(ImportDirectory, 0, this));
710 import_directory_iterator COFFObjectFile::import_directory_end() const {
711 return import_directory_iterator(
712 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
715 delay_import_directory_iterator
716 COFFObjectFile::delay_import_directory_begin() const {
717 return delay_import_directory_iterator(
718 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
721 delay_import_directory_iterator
722 COFFObjectFile::delay_import_directory_end() const {
723 return delay_import_directory_iterator(
724 DelayImportDirectoryEntryRef(
725 DelayImportDirectory, NumberOfDelayImportDirectory, this));
728 export_directory_iterator COFFObjectFile::export_directory_begin() const {
729 return export_directory_iterator(
730 ExportDirectoryEntryRef(ExportDirectory, 0, this));
733 export_directory_iterator COFFObjectFile::export_directory_end() const {
734 if (!ExportDirectory)
735 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
736 ExportDirectoryEntryRef Ref(ExportDirectory,
737 ExportDirectory->AddressTableEntries, this);
738 return export_directory_iterator(Ref);
741 section_iterator COFFObjectFile::section_begin() const {
743 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
744 return section_iterator(SectionRef(Ret, this));
747 section_iterator COFFObjectFile::section_end() const {
750 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
751 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
752 return section_iterator(SectionRef(Ret, this));
755 uint8_t COFFObjectFile::getBytesInAddress() const {
756 return getArch() == Triple::x86_64 ? 8 : 4;
759 StringRef COFFObjectFile::getFileFormatName() const {
760 switch(getMachine()) {
761 case COFF::IMAGE_FILE_MACHINE_I386:
763 case COFF::IMAGE_FILE_MACHINE_AMD64:
764 return "COFF-x86-64";
765 case COFF::IMAGE_FILE_MACHINE_ARMNT:
768 return "COFF-<unknown arch>";
772 unsigned COFFObjectFile::getArch() const {
773 switch (getMachine()) {
774 case COFF::IMAGE_FILE_MACHINE_I386:
776 case COFF::IMAGE_FILE_MACHINE_AMD64:
777 return Triple::x86_64;
778 case COFF::IMAGE_FILE_MACHINE_ARMNT:
779 return Triple::thumb;
781 return Triple::UnknownArch;
785 iterator_range<import_directory_iterator>
786 COFFObjectFile::import_directories() const {
787 return make_range(import_directory_begin(), import_directory_end());
790 iterator_range<delay_import_directory_iterator>
791 COFFObjectFile::delay_import_directories() const {
792 return make_range(delay_import_directory_begin(),
793 delay_import_directory_end());
796 iterator_range<export_directory_iterator>
797 COFFObjectFile::export_directories() const {
798 return make_range(export_directory_begin(), export_directory_end());
801 std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
803 return object_error::success;
807 COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
808 Res = PE32PlusHeader;
809 return object_error::success;
813 COFFObjectFile::getDataDirectory(uint32_t Index,
814 const data_directory *&Res) const {
815 // Error if if there's no data directory or the index is out of range.
817 return object_error::parse_failed;
818 assert(PE32Header || PE32PlusHeader);
819 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
820 : PE32PlusHeader->NumberOfRvaAndSize;
822 return object_error::parse_failed;
823 Res = &DataDirectory[Index];
824 return object_error::success;
827 std::error_code COFFObjectFile::getSection(int32_t Index,
828 const coff_section *&Result) const {
829 // Check for special index values.
830 if (COFF::isReservedSectionNumber(Index))
832 else if (Index > 0 && static_cast<uint32_t>(Index) <= getNumberOfSections())
833 // We already verified the section table data, so no need to check again.
834 Result = SectionTable + (Index - 1);
836 return object_error::parse_failed;
837 return object_error::success;
840 std::error_code COFFObjectFile::getString(uint32_t Offset,
841 StringRef &Result) const {
842 if (StringTableSize <= 4)
843 // Tried to get a string from an empty string table.
844 return object_error::parse_failed;
845 if (Offset >= StringTableSize)
846 return object_error::unexpected_eof;
847 Result = StringRef(StringTable + Offset);
848 return object_error::success;
851 std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
852 StringRef &Res) const {
853 // Check for string table entry. First 4 bytes are 0.
854 if (Symbol.getStringTableOffset().Zeroes == 0) {
855 uint32_t Offset = Symbol.getStringTableOffset().Offset;
856 if (std::error_code EC = getString(Offset, Res))
858 return object_error::success;
861 if (Symbol.getShortName()[COFF::NameSize - 1] == 0)
862 // Null terminated, let ::strlen figure out the length.
863 Res = StringRef(Symbol.getShortName());
865 // Not null terminated, use all 8 bytes.
866 Res = StringRef(Symbol.getShortName(), COFF::NameSize);
867 return object_error::success;
871 COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
872 const uint8_t *Aux = nullptr;
874 size_t SymbolSize = getSymbolTableEntrySize();
875 if (Symbol.getNumberOfAuxSymbols() > 0) {
876 // AUX data comes immediately after the symbol in COFF
877 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
879 // Verify that the Aux symbol points to a valid entry in the symbol table.
880 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
881 if (Offset < getPointerToSymbolTable() ||
883 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
884 report_fatal_error("Aux Symbol data was outside of symbol table.");
886 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
887 "Aux Symbol data did not point to the beginning of a symbol");
890 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
893 std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
894 StringRef &Res) const {
896 if (Sec->Name[COFF::NameSize - 1] == 0)
897 // Null terminated, let ::strlen figure out the length.
900 // Not null terminated, use all 8 bytes.
901 Name = StringRef(Sec->Name, COFF::NameSize);
903 // Check for string table entry. First byte is '/'.
904 if (Name[0] == '/') {
906 if (Name[1] == '/') {
907 if (decodeBase64StringEntry(Name.substr(2), Offset))
908 return object_error::parse_failed;
910 if (Name.substr(1).getAsInteger(10, Offset))
911 return object_error::parse_failed;
913 if (std::error_code EC = getString(Offset, Name))
918 return object_error::success;
921 uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
922 // SizeOfRawData and VirtualSize change what they represent depending on
923 // whether or not we have an executable image.
925 // For object files, SizeOfRawData contains the size of section's data;
926 // VirtualSize is always zero.
928 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
929 // actual section size is in VirtualSize. It is possible for VirtualSize to
930 // be greater than SizeOfRawData; the contents past that point should be
931 // considered to be zero.
932 uint32_t SectionSize;
933 if (Sec->VirtualSize)
934 SectionSize = std::min(Sec->VirtualSize, Sec->SizeOfRawData);
936 SectionSize = Sec->SizeOfRawData;
942 COFFObjectFile::getSectionContents(const coff_section *Sec,
943 ArrayRef<uint8_t> &Res) const {
944 // PointerToRawData and SizeOfRawData won't make sense for BSS sections,
945 // don't do anything interesting for them.
946 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
947 "BSS sections don't have contents!");
948 // The only thing that we need to verify is that the contents is contained
949 // within the file bounds. We don't need to make sure it doesn't cover other
950 // data, as there's nothing that says that is not allowed.
951 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
952 uint32_t SectionSize = getSectionSize(Sec);
953 uintptr_t ConEnd = ConStart + SectionSize;
954 if (ConEnd > uintptr_t(Data.getBufferEnd()))
955 return object_error::parse_failed;
956 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
957 return object_error::success;
960 const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
961 return reinterpret_cast<const coff_relocation*>(Rel.p);
964 void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
965 Rel.p = reinterpret_cast<uintptr_t>(
966 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
969 std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
970 uint64_t &Res) const {
971 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
974 std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
975 uint64_t &Res) const {
976 Res = toRel(Rel)->VirtualAddress;
977 return object_error::success;
980 symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
981 const coff_relocation *R = toRel(Rel);
984 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
985 else if (SymbolTable32)
986 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
988 llvm_unreachable("no symbol table pointer!");
989 return symbol_iterator(SymbolRef(Ref, this));
992 std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
993 uint64_t &Res) const {
994 const coff_relocation* R = toRel(Rel);
996 return object_error::success;
1000 COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
1001 return toSec(Section.getRawDataRefImpl());
1004 COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1006 return toSymb<coff_symbol16>(Ref);
1008 return toSymb<coff_symbol32>(Ref);
1009 llvm_unreachable("no symbol table pointer!");
1012 COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1013 return getCOFFSymbol(Symbol.getRawDataRefImpl());
1016 const coff_relocation *
1017 COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1018 return toRel(Reloc.getRawDataRefImpl());
1021 #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1022 case COFF::reloc_type: \
1023 Res = #reloc_type; \
1027 COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
1028 SmallVectorImpl<char> &Result) const {
1029 const coff_relocation *Reloc = toRel(Rel);
1031 switch (getMachine()) {
1032 case COFF::IMAGE_FILE_MACHINE_AMD64:
1033 switch (Reloc->Type) {
1034 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1035 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1036 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1037 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1038 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1039 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1040 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1041 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1042 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1043 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1044 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1045 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1046 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1047 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1048 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1049 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1050 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1055 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1056 switch (Reloc->Type) {
1057 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1058 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1059 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1060 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1061 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1062 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1063 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1064 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1065 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1066 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1067 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1068 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1069 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1070 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1071 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1076 case COFF::IMAGE_FILE_MACHINE_I386:
1077 switch (Reloc->Type) {
1078 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1079 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1080 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1081 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1082 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1083 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1084 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1085 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1086 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1087 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1088 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1096 Result.append(Res.begin(), Res.end());
1097 return object_error::success;
1100 #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1103 COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
1104 SmallVectorImpl<char> &Result) const {
1105 const coff_relocation *Reloc = toRel(Rel);
1107 ErrorOr<COFFSymbolRef> Symb = getSymbol(Reloc->SymbolTableIndex);
1108 if (std::error_code EC = Symb.getError())
1110 Sym.p = reinterpret_cast<uintptr_t>(Symb->getRawPtr());
1112 if (std::error_code EC = getSymbolName(Sym, SymName))
1114 Result.append(SymName.begin(), SymName.end());
1115 return object_error::success;
1118 bool COFFObjectFile::isRelocatableObject() const {
1119 return !DataDirectory;
1122 bool ImportDirectoryEntryRef::
1123 operator==(const ImportDirectoryEntryRef &Other) const {
1124 return ImportTable == Other.ImportTable && Index == Other.Index;
1127 void ImportDirectoryEntryRef::moveNext() {
1131 std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1132 const import_directory_table_entry *&Result) const {
1133 Result = ImportTable + Index;
1134 return object_error::success;
1137 static imported_symbol_iterator
1138 makeImportedSymbolIterator(const COFFObjectFile *Object,
1139 uintptr_t Ptr, int Index) {
1140 if (Object->getBytesInAddress() == 4) {
1141 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
1142 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
1144 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
1145 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
1148 static imported_symbol_iterator
1149 importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
1150 uintptr_t IntPtr = 0;
1151 Object->getRvaPtr(RVA, IntPtr);
1152 return makeImportedSymbolIterator(Object, IntPtr, 0);
1155 static imported_symbol_iterator
1156 importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
1157 uintptr_t IntPtr = 0;
1158 Object->getRvaPtr(RVA, IntPtr);
1159 // Forward the pointer to the last entry which is null.
1161 if (Object->getBytesInAddress() == 4) {
1162 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1166 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1170 return makeImportedSymbolIterator(Object, IntPtr, Index);
1173 imported_symbol_iterator
1174 ImportDirectoryEntryRef::imported_symbol_begin() const {
1175 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1179 imported_symbol_iterator
1180 ImportDirectoryEntryRef::imported_symbol_end() const {
1181 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1185 iterator_range<imported_symbol_iterator>
1186 ImportDirectoryEntryRef::imported_symbols() const {
1187 return make_range(imported_symbol_begin(), imported_symbol_end());
1190 std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
1191 uintptr_t IntPtr = 0;
1192 if (std::error_code EC =
1193 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
1195 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1196 return object_error::success;
1200 ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1201 Result = ImportTable[Index].ImportLookupTableRVA;
1202 return object_error::success;
1206 ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1207 Result = ImportTable[Index].ImportAddressTableRVA;
1208 return object_error::success;
1211 std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
1212 const import_lookup_table_entry32 *&Result) const {
1213 uintptr_t IntPtr = 0;
1214 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1215 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1217 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
1218 return object_error::success;
1221 bool DelayImportDirectoryEntryRef::
1222 operator==(const DelayImportDirectoryEntryRef &Other) const {
1223 return Table == Other.Table && Index == Other.Index;
1226 void DelayImportDirectoryEntryRef::moveNext() {
1230 imported_symbol_iterator
1231 DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1232 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1236 imported_symbol_iterator
1237 DelayImportDirectoryEntryRef::imported_symbol_end() const {
1238 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1242 iterator_range<imported_symbol_iterator>
1243 DelayImportDirectoryEntryRef::imported_symbols() const {
1244 return make_range(imported_symbol_begin(), imported_symbol_end());
1247 std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1248 uintptr_t IntPtr = 0;
1249 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1251 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1252 return object_error::success;
1255 std::error_code DelayImportDirectoryEntryRef::
1256 getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1258 return object_error::success;
1261 bool ExportDirectoryEntryRef::
1262 operator==(const ExportDirectoryEntryRef &Other) const {
1263 return ExportTable == Other.ExportTable && Index == Other.Index;
1266 void ExportDirectoryEntryRef::moveNext() {
1270 // Returns the name of the current export symbol. If the symbol is exported only
1271 // by ordinal, the empty string is set as a result.
1272 std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
1273 uintptr_t IntPtr = 0;
1274 if (std::error_code EC =
1275 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
1277 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1278 return object_error::success;
1281 // Returns the starting ordinal number.
1283 ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
1284 Result = ExportTable->OrdinalBase;
1285 return object_error::success;
1288 // Returns the export ordinal of the current export symbol.
1289 std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
1290 Result = ExportTable->OrdinalBase + Index;
1291 return object_error::success;
1294 // Returns the address of the current export symbol.
1295 std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
1296 uintptr_t IntPtr = 0;
1297 if (std::error_code EC =
1298 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
1300 const export_address_table_entry *entry =
1301 reinterpret_cast<const export_address_table_entry *>(IntPtr);
1302 Result = entry[Index].ExportRVA;
1303 return object_error::success;
1306 // Returns the name of the current export symbol. If the symbol is exported only
1307 // by ordinal, the empty string is set as a result.
1309 ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
1310 uintptr_t IntPtr = 0;
1311 if (std::error_code EC =
1312 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
1314 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1316 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1318 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1319 I < E; ++I, ++Offset) {
1322 if (std::error_code EC =
1323 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
1325 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
1326 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
1328 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1329 return object_error::success;
1332 return object_error::success;
1335 bool ImportedSymbolRef::
1336 operator==(const ImportedSymbolRef &Other) const {
1337 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1338 && Index == Other.Index;
1341 void ImportedSymbolRef::moveNext() {
1346 ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1349 // If a symbol is imported only by ordinal, it has no name.
1350 if (Entry32[Index].isOrdinal())
1351 return object_error::success;
1352 RVA = Entry32[Index].getHintNameRVA();
1354 if (Entry64[Index].isOrdinal())
1355 return object_error::success;
1356 RVA = Entry64[Index].getHintNameRVA();
1358 uintptr_t IntPtr = 0;
1359 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1361 // +2 because the first two bytes is hint.
1362 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
1363 return object_error::success;
1366 std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1369 if (Entry32[Index].isOrdinal()) {
1370 Result = Entry32[Index].getOrdinal();
1371 return object_error::success;
1373 RVA = Entry32[Index].getHintNameRVA();
1375 if (Entry64[Index].isOrdinal()) {
1376 Result = Entry64[Index].getOrdinal();
1377 return object_error::success;
1379 RVA = Entry64[Index].getHintNameRVA();
1381 uintptr_t IntPtr = 0;
1382 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1384 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
1385 return object_error::success;
1388 ErrorOr<std::unique_ptr<COFFObjectFile>>
1389 ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
1391 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
1394 return std::move(Ret);