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 static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr,
43 const uint64_t Size) {
44 if (Addr + Size < Addr || Addr + Size < Size ||
45 Addr + Size > uintptr_t(M.getBufferEnd()) ||
46 Addr < uintptr_t(M.getBufferStart())) {
47 return object_error::unexpected_eof;
49 return object_error::success;
52 // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
53 // Returns unexpected_eof if error.
55 static std::error_code getObject(const T *&Obj, MemoryBufferRef M,
57 const uint64_t Size = sizeof(T)) {
58 uintptr_t Addr = uintptr_t(Ptr);
59 if (std::error_code EC = checkOffset(M, Addr, Size))
61 Obj = reinterpret_cast<const T *>(Addr);
62 return object_error::success;
65 // Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without
67 static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) {
68 assert(Str.size() <= 6 && "String too long, possible overflow.");
73 while (!Str.empty()) {
75 if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25
76 CharVal = Str[0] - 'A';
77 else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51
78 CharVal = Str[0] - 'a' + 26;
79 else if (Str[0] >= '0' && Str[0] <= '9') // 52..61
80 CharVal = Str[0] - '0' + 52;
81 else if (Str[0] == '+') // 62
83 else if (Str[0] == '/') // 63
88 Value = (Value * 64) + CharVal;
92 if (Value > std::numeric_limits<uint32_t>::max())
95 Result = static_cast<uint32_t>(Value);
99 template <typename coff_symbol_type>
100 const coff_symbol_type *COFFObjectFile::toSymb(DataRefImpl Ref) const {
101 const coff_symbol_type *Addr =
102 reinterpret_cast<const coff_symbol_type *>(Ref.p);
104 assert(!checkOffset(Data, uintptr_t(Addr), sizeof(*Addr)));
106 // Verify that the symbol points to a valid entry in the symbol table.
107 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
109 assert((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 &&
110 "Symbol did not point to the beginning of a symbol");
116 const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
117 const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
120 // Verify that the section points to a valid entry in the section table.
121 if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections()))
122 report_fatal_error("Section was outside of section table.");
124 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
125 assert(Offset % sizeof(coff_section) == 0 &&
126 "Section did not point to the beginning of a section");
132 void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const {
133 auto End = reinterpret_cast<uintptr_t>(StringTable);
135 const coff_symbol16 *Symb = toSymb<coff_symbol16>(Ref);
136 Symb += 1 + Symb->NumberOfAuxSymbols;
137 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
138 } else if (SymbolTable32) {
139 const coff_symbol32 *Symb = toSymb<coff_symbol32>(Ref);
140 Symb += 1 + Symb->NumberOfAuxSymbols;
141 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
143 llvm_unreachable("no symbol table pointer!");
147 std::error_code COFFObjectFile::getSymbolName(DataRefImpl Ref,
148 StringRef &Result) const {
149 COFFSymbolRef Symb = getCOFFSymbol(Ref);
150 return getSymbolName(Symb, Result);
153 std::error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
154 uint64_t &Result) const {
155 COFFSymbolRef Symb = getCOFFSymbol(Ref);
157 if (Symb.isAnyUndefined()) {
158 Result = UnknownAddressOrSize;
159 return object_error::success;
161 if (Symb.isCommon()) {
162 Result = UnknownAddressOrSize;
163 return object_error::success;
165 int32_t SectionNumber = Symb.getSectionNumber();
166 if (!COFF::isReservedSectionNumber(SectionNumber)) {
167 const coff_section *Section = nullptr;
168 if (std::error_code EC = getSection(SectionNumber, Section))
171 Result = Section->VirtualAddress + Symb.getValue();
172 return object_error::success;
175 Result = Symb.getValue();
176 return object_error::success;
179 std::error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
180 SymbolRef::Type &Result) const {
181 COFFSymbolRef Symb = getCOFFSymbol(Ref);
182 int32_t SectionNumber = Symb.getSectionNumber();
183 Result = SymbolRef::ST_Other;
185 if (Symb.isAnyUndefined()) {
186 Result = SymbolRef::ST_Unknown;
187 } else if (Symb.isFunctionDefinition()) {
188 Result = SymbolRef::ST_Function;
189 } else if (Symb.isCommon()) {
190 Result = SymbolRef::ST_Data;
191 } else if (Symb.isFileRecord()) {
192 Result = SymbolRef::ST_File;
193 } else if (SectionNumber == COFF::IMAGE_SYM_DEBUG) {
194 Result = SymbolRef::ST_Debug;
195 } else if (!COFF::isReservedSectionNumber(SectionNumber)) {
196 const coff_section *Section = nullptr;
197 if (std::error_code EC = getSection(SectionNumber, Section))
199 uint32_t Characteristics = Section->Characteristics;
200 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
201 Result = SymbolRef::ST_Function;
202 else if (Characteristics & (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
203 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA))
204 Result = SymbolRef::ST_Data;
206 return object_error::success;
209 uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
210 COFFSymbolRef Symb = getCOFFSymbol(Ref);
211 uint32_t Result = SymbolRef::SF_None;
213 if (Symb.isExternal() || Symb.isWeakExternal())
214 Result |= SymbolRef::SF_Global;
216 if (Symb.isWeakExternal())
217 Result |= SymbolRef::SF_Weak;
219 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
220 Result |= SymbolRef::SF_Absolute;
222 if (Symb.isFileRecord())
223 Result |= SymbolRef::SF_FormatSpecific;
225 if (Symb.isSectionDefinition())
226 Result |= SymbolRef::SF_FormatSpecific;
229 Result |= SymbolRef::SF_Common;
231 if (Symb.isAnyUndefined())
232 Result |= SymbolRef::SF_Undefined;
237 std::error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref,
238 uint64_t &Result) const {
239 COFFSymbolRef Symb = getCOFFSymbol(Ref);
241 if (Symb.isAnyUndefined()) {
242 Result = UnknownAddressOrSize;
243 return object_error::success;
245 if (Symb.isCommon()) {
246 Result = Symb.getValue();
247 return object_error::success;
250 // Let's attempt to get the size of the symbol by looking at the address of
251 // the symbol after the symbol in question.
253 if (std::error_code EC = getSymbolAddress(Ref, SymbAddr))
255 int32_t SectionNumber = Symb.getSectionNumber();
256 if (COFF::isReservedSectionNumber(SectionNumber)) {
257 // Absolute and debug symbols aren't sorted in any interesting way.
259 return object_error::success;
261 const section_iterator SecEnd = section_end();
262 uint64_t AfterAddr = UnknownAddressOrSize;
263 for (const symbol_iterator &SymbI : symbols()) {
264 section_iterator SecI = SecEnd;
265 if (std::error_code EC = SymbI->getSection(SecI))
267 // Check the symbol's section, skip it if it's in the wrong section.
268 // First, make sure it is in any section.
271 // Second, make sure it is in the same section as the symbol in question.
272 if (!sectionContainsSymbol(SecI->getRawDataRefImpl(), Ref))
275 if (std::error_code EC = SymbI->getAddress(Addr))
277 // We want to compare our symbol in question with the closest possible
278 // symbol that comes after.
279 if (AfterAddr > Addr && Addr > SymbAddr)
282 if (AfterAddr == UnknownAddressOrSize) {
283 // No symbol comes after this one, assume that everything after our symbol
285 const coff_section *Section = nullptr;
286 if (std::error_code EC = getSection(SectionNumber, Section))
288 Result = Section->SizeOfRawData - Symb.getValue();
290 // Take the difference between our symbol and the symbol that comes after
292 Result = AfterAddr - SymbAddr;
295 return object_error::success;
299 COFFObjectFile::getSymbolSection(DataRefImpl Ref,
300 section_iterator &Result) const {
301 COFFSymbolRef Symb = getCOFFSymbol(Ref);
302 if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
303 Result = section_end();
305 const coff_section *Sec = nullptr;
306 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
309 Ref.p = reinterpret_cast<uintptr_t>(Sec);
310 Result = section_iterator(SectionRef(Ref, this));
312 return object_error::success;
315 void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
316 const coff_section *Sec = toSec(Ref);
318 Ref.p = reinterpret_cast<uintptr_t>(Sec);
321 std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
322 StringRef &Result) const {
323 const coff_section *Sec = toSec(Ref);
324 return getSectionName(Sec, Result);
327 uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
328 const coff_section *Sec = toSec(Ref);
329 return Sec->VirtualAddress;
332 uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
333 return getSectionSize(toSec(Ref));
336 std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
337 StringRef &Result) const {
338 const coff_section *Sec = toSec(Ref);
339 ArrayRef<uint8_t> Res;
340 std::error_code EC = getSectionContents(Sec, Res);
341 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
345 uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
346 const coff_section *Sec = toSec(Ref);
347 return uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
350 bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
351 const coff_section *Sec = toSec(Ref);
352 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
355 bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
356 const coff_section *Sec = toSec(Ref);
357 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
360 bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
361 const coff_section *Sec = toSec(Ref);
362 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
365 bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
366 const coff_section *Sec = toSec(Ref);
367 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
370 bool COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
371 DataRefImpl SymbRef) const {
372 const coff_section *Sec = toSec(SecRef);
373 COFFSymbolRef Symb = getCOFFSymbol(SymbRef);
374 int32_t SecNumber = (Sec - SectionTable) + 1;
375 return SecNumber == Symb.getSectionNumber();
378 static uint32_t getNumberOfRelocations(const coff_section *Sec,
379 MemoryBufferRef M, 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 const coff_relocation *FirstReloc;
386 if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
387 base + Sec->PointerToRelocations)))
389 // -1 to exclude this first relocation entry.
390 return FirstReloc->VirtualAddress - 1;
392 return Sec->NumberOfRelocations;
395 static const coff_relocation *
396 getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
397 uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
400 auto begin = reinterpret_cast<const coff_relocation *>(
401 Base + Sec->PointerToRelocations);
402 if (Sec->hasExtendedRelocations()) {
403 // Skip the first relocation entry repurposed to store the number of
407 if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs))
412 relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
413 const coff_section *Sec = toSec(Ref);
414 const coff_relocation *begin = getFirstReloc(Sec, Data, base());
416 Ret.p = reinterpret_cast<uintptr_t>(begin);
417 return relocation_iterator(RelocationRef(Ret, this));
420 relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
421 const coff_section *Sec = toSec(Ref);
422 const coff_relocation *I = getFirstReloc(Sec, Data, base());
424 I += getNumberOfRelocations(Sec, Data, base());
426 Ret.p = reinterpret_cast<uintptr_t>(I);
427 return relocation_iterator(RelocationRef(Ret, this));
430 // Initialize the pointer to the symbol table.
431 std::error_code COFFObjectFile::initSymbolTablePtr() {
433 if (std::error_code EC = getObject(
434 SymbolTable16, Data, base() + getPointerToSymbolTable(),
435 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
438 if (COFFBigObjHeader)
439 if (std::error_code EC = getObject(
440 SymbolTable32, Data, base() + getPointerToSymbolTable(),
441 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
444 // Find string table. The first four byte of the string table contains the
445 // total size of the string table, including the size field itself. If the
446 // string table is empty, the value of the first four byte would be 4.
447 uint32_t StringTableOffset = getPointerToSymbolTable() +
448 getNumberOfSymbols() * getSymbolTableEntrySize();
449 const uint8_t *StringTableAddr = base() + StringTableOffset;
450 const ulittle32_t *StringTableSizePtr;
451 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
453 StringTableSize = *StringTableSizePtr;
454 if (std::error_code EC =
455 getObject(StringTable, Data, StringTableAddr, StringTableSize))
458 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
459 // tools like cvtres write a size of 0 for an empty table instead of 4.
460 if (StringTableSize < 4)
463 // Check that the string table is null terminated if has any in it.
464 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
465 return object_error::parse_failed;
466 return object_error::success;
469 // Returns the file offset for the given VA.
470 std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
471 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
472 : (uint64_t)PE32PlusHeader->ImageBase;
473 uint64_t Rva = Addr - ImageBase;
474 assert(Rva <= UINT32_MAX);
475 return getRvaPtr((uint32_t)Rva, Res);
478 // Returns the file offset for the given RVA.
479 std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
480 for (const SectionRef &S : sections()) {
481 const coff_section *Section = getCOFFSection(S);
482 uint32_t SectionStart = Section->VirtualAddress;
483 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
484 if (SectionStart <= Addr && Addr < SectionEnd) {
485 uint32_t Offset = Addr - SectionStart;
486 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
487 return object_error::success;
490 return object_error::parse_failed;
493 // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
495 std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
496 StringRef &Name) const {
497 uintptr_t IntPtr = 0;
498 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
500 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
501 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
502 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
503 return object_error::success;
506 // Find the import table.
507 std::error_code COFFObjectFile::initImportTablePtr() {
508 // First, we get the RVA of the import table. If the file lacks a pointer to
509 // the import table, do nothing.
510 const data_directory *DataEntry;
511 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
512 return object_error::success;
514 // Do nothing if the pointer to import table is NULL.
515 if (DataEntry->RelativeVirtualAddress == 0)
516 return object_error::success;
518 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
519 // -1 because the last entry is the null entry.
520 NumberOfImportDirectory = DataEntry->Size /
521 sizeof(import_directory_table_entry) - 1;
523 // Find the section that contains the RVA. This is needed because the RVA is
524 // the import table's memory address which is different from its file offset.
525 uintptr_t IntPtr = 0;
526 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
528 ImportDirectory = reinterpret_cast<
529 const import_directory_table_entry *>(IntPtr);
530 return object_error::success;
533 // Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
534 std::error_code COFFObjectFile::initDelayImportTablePtr() {
535 const data_directory *DataEntry;
536 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
537 return object_error::success;
538 if (DataEntry->RelativeVirtualAddress == 0)
539 return object_error::success;
541 uint32_t RVA = DataEntry->RelativeVirtualAddress;
542 NumberOfDelayImportDirectory = DataEntry->Size /
543 sizeof(delay_import_directory_table_entry) - 1;
545 uintptr_t IntPtr = 0;
546 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
548 DelayImportDirectory = reinterpret_cast<
549 const delay_import_directory_table_entry *>(IntPtr);
550 return object_error::success;
553 // Find the export table.
554 std::error_code COFFObjectFile::initExportTablePtr() {
555 // First, we get the RVA of the export table. If the file lacks a pointer to
556 // the export table, do nothing.
557 const data_directory *DataEntry;
558 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
559 return object_error::success;
561 // Do nothing if the pointer to export table is NULL.
562 if (DataEntry->RelativeVirtualAddress == 0)
563 return object_error::success;
565 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
566 uintptr_t IntPtr = 0;
567 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
570 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
571 return object_error::success;
574 std::error_code COFFObjectFile::initBaseRelocPtr() {
575 const data_directory *DataEntry;
576 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry))
577 return object_error::success;
578 if (DataEntry->RelativeVirtualAddress == 0)
579 return object_error::success;
581 uintptr_t IntPtr = 0;
582 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
584 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
586 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
587 IntPtr + DataEntry->Size);
588 return object_error::success;
591 COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
592 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
593 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
594 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
595 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
596 ImportDirectory(nullptr), NumberOfImportDirectory(0),
597 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
598 ExportDirectory(nullptr), BaseRelocHeader(nullptr),
599 BaseRelocEnd(nullptr) {
600 // Check that we at least have enough room for a header.
601 if (!checkSize(Data, EC, sizeof(coff_file_header)))
604 // The current location in the file where we are looking at.
607 // PE header is optional and is present only in executables. If it exists,
608 // it is placed right after COFF header.
609 bool HasPEHeader = false;
611 // Check if this is a PE/COFF file.
612 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
613 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
614 // PE signature to find 'normal' COFF header.
615 const auto *DH = reinterpret_cast<const dos_header *>(base());
616 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
617 CurPtr = DH->AddressOfNewExeHeader;
618 // Check the PE magic bytes. ("PE\0\0")
619 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
620 EC = object_error::parse_failed;
623 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
628 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
631 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
632 // import libraries share a common prefix but bigobj is more restrictive.
633 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
634 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
635 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
636 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
639 // Verify that we are dealing with bigobj.
640 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
641 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
642 sizeof(COFF::BigObjMagic)) == 0) {
643 COFFHeader = nullptr;
644 CurPtr += sizeof(coff_bigobj_file_header);
646 // It's not a bigobj.
647 COFFBigObjHeader = nullptr;
651 // The prior checkSize call may have failed. This isn't a hard error
652 // because we were just trying to sniff out bigobj.
653 EC = object_error::success;
654 CurPtr += sizeof(coff_file_header);
656 if (COFFHeader->isImportLibrary())
661 const pe32_header *Header;
662 if ((EC = getObject(Header, Data, base() + CurPtr)))
665 const uint8_t *DataDirAddr;
666 uint64_t DataDirSize;
667 if (Header->Magic == COFF::PE32Header::PE32) {
669 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
670 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
671 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
672 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
673 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
674 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
676 // It's neither PE32 nor PE32+.
677 EC = object_error::parse_failed;
680 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
682 CurPtr += COFFHeader->SizeOfOptionalHeader;
685 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
686 (uint64_t)getNumberOfSections() * sizeof(coff_section))))
689 // Initialize the pointer to the symbol table.
690 if (getPointerToSymbolTable() != 0) {
691 if ((EC = initSymbolTablePtr()))
694 // We had better not have any symbols if we don't have a symbol table.
695 if (getNumberOfSymbols() != 0) {
696 EC = object_error::parse_failed;
701 // Initialize the pointer to the beginning of the import table.
702 if ((EC = initImportTablePtr()))
704 if ((EC = initDelayImportTablePtr()))
707 // Initialize the pointer to the export table.
708 if ((EC = initExportTablePtr()))
711 // Initialize the pointer to the base relocation table.
712 if ((EC = initBaseRelocPtr()))
715 EC = object_error::success;
718 basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
720 Ret.p = getSymbolTable();
721 return basic_symbol_iterator(SymbolRef(Ret, this));
724 basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
725 // The symbol table ends where the string table begins.
727 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
728 return basic_symbol_iterator(SymbolRef(Ret, this));
731 import_directory_iterator COFFObjectFile::import_directory_begin() const {
732 return import_directory_iterator(
733 ImportDirectoryEntryRef(ImportDirectory, 0, this));
736 import_directory_iterator COFFObjectFile::import_directory_end() const {
737 return import_directory_iterator(
738 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
741 delay_import_directory_iterator
742 COFFObjectFile::delay_import_directory_begin() const {
743 return delay_import_directory_iterator(
744 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
747 delay_import_directory_iterator
748 COFFObjectFile::delay_import_directory_end() const {
749 return delay_import_directory_iterator(
750 DelayImportDirectoryEntryRef(
751 DelayImportDirectory, NumberOfDelayImportDirectory, this));
754 export_directory_iterator COFFObjectFile::export_directory_begin() const {
755 return export_directory_iterator(
756 ExportDirectoryEntryRef(ExportDirectory, 0, this));
759 export_directory_iterator COFFObjectFile::export_directory_end() const {
760 if (!ExportDirectory)
761 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
762 ExportDirectoryEntryRef Ref(ExportDirectory,
763 ExportDirectory->AddressTableEntries, this);
764 return export_directory_iterator(Ref);
767 section_iterator COFFObjectFile::section_begin() const {
769 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
770 return section_iterator(SectionRef(Ret, this));
773 section_iterator COFFObjectFile::section_end() const {
776 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
777 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
778 return section_iterator(SectionRef(Ret, this));
781 base_reloc_iterator COFFObjectFile::base_reloc_begin() const {
782 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
785 base_reloc_iterator COFFObjectFile::base_reloc_end() const {
786 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
789 uint8_t COFFObjectFile::getBytesInAddress() const {
790 return getArch() == Triple::x86_64 ? 8 : 4;
793 StringRef COFFObjectFile::getFileFormatName() const {
794 switch(getMachine()) {
795 case COFF::IMAGE_FILE_MACHINE_I386:
797 case COFF::IMAGE_FILE_MACHINE_AMD64:
798 return "COFF-x86-64";
799 case COFF::IMAGE_FILE_MACHINE_ARMNT:
802 return "COFF-<unknown arch>";
806 unsigned COFFObjectFile::getArch() const {
807 switch (getMachine()) {
808 case COFF::IMAGE_FILE_MACHINE_I386:
810 case COFF::IMAGE_FILE_MACHINE_AMD64:
811 return Triple::x86_64;
812 case COFF::IMAGE_FILE_MACHINE_ARMNT:
813 return Triple::thumb;
815 return Triple::UnknownArch;
819 iterator_range<import_directory_iterator>
820 COFFObjectFile::import_directories() const {
821 return make_range(import_directory_begin(), import_directory_end());
824 iterator_range<delay_import_directory_iterator>
825 COFFObjectFile::delay_import_directories() const {
826 return make_range(delay_import_directory_begin(),
827 delay_import_directory_end());
830 iterator_range<export_directory_iterator>
831 COFFObjectFile::export_directories() const {
832 return make_range(export_directory_begin(), export_directory_end());
835 iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const {
836 return make_range(base_reloc_begin(), base_reloc_end());
839 std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
841 return object_error::success;
845 COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
846 Res = PE32PlusHeader;
847 return object_error::success;
851 COFFObjectFile::getDataDirectory(uint32_t Index,
852 const data_directory *&Res) const {
853 // Error if if there's no data directory or the index is out of range.
854 if (!DataDirectory) {
856 return object_error::parse_failed;
858 assert(PE32Header || PE32PlusHeader);
859 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
860 : PE32PlusHeader->NumberOfRvaAndSize;
861 if (Index >= NumEnt) {
863 return object_error::parse_failed;
865 Res = &DataDirectory[Index];
866 return object_error::success;
869 std::error_code COFFObjectFile::getSection(int32_t Index,
870 const coff_section *&Result) const {
872 if (COFF::isReservedSectionNumber(Index))
873 return object_error::success;
874 if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
875 // We already verified the section table data, so no need to check again.
876 Result = SectionTable + (Index - 1);
877 return object_error::success;
879 return object_error::parse_failed;
882 std::error_code COFFObjectFile::getString(uint32_t Offset,
883 StringRef &Result) const {
884 if (StringTableSize <= 4)
885 // Tried to get a string from an empty string table.
886 return object_error::parse_failed;
887 if (Offset >= StringTableSize)
888 return object_error::unexpected_eof;
889 Result = StringRef(StringTable + Offset);
890 return object_error::success;
893 std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
894 StringRef &Res) const {
895 // Check for string table entry. First 4 bytes are 0.
896 if (Symbol.getStringTableOffset().Zeroes == 0) {
897 uint32_t Offset = Symbol.getStringTableOffset().Offset;
898 if (std::error_code EC = getString(Offset, Res))
900 return object_error::success;
903 if (Symbol.getShortName()[COFF::NameSize - 1] == 0)
904 // Null terminated, let ::strlen figure out the length.
905 Res = StringRef(Symbol.getShortName());
907 // Not null terminated, use all 8 bytes.
908 Res = StringRef(Symbol.getShortName(), COFF::NameSize);
909 return object_error::success;
913 COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
914 const uint8_t *Aux = nullptr;
916 size_t SymbolSize = getSymbolTableEntrySize();
917 if (Symbol.getNumberOfAuxSymbols() > 0) {
918 // AUX data comes immediately after the symbol in COFF
919 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
921 // Verify that the Aux symbol points to a valid entry in the symbol table.
922 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
923 if (Offset < getPointerToSymbolTable() ||
925 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
926 report_fatal_error("Aux Symbol data was outside of symbol table.");
928 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
929 "Aux Symbol data did not point to the beginning of a symbol");
932 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
935 std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
936 StringRef &Res) const {
938 if (Sec->Name[COFF::NameSize - 1] == 0)
939 // Null terminated, let ::strlen figure out the length.
942 // Not null terminated, use all 8 bytes.
943 Name = StringRef(Sec->Name, COFF::NameSize);
945 // Check for string table entry. First byte is '/'.
946 if (Name.startswith("/")) {
948 if (Name.startswith("//")) {
949 if (decodeBase64StringEntry(Name.substr(2), Offset))
950 return object_error::parse_failed;
952 if (Name.substr(1).getAsInteger(10, Offset))
953 return object_error::parse_failed;
955 if (std::error_code EC = getString(Offset, Name))
960 return object_error::success;
963 uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
964 // SizeOfRawData and VirtualSize change what they represent depending on
965 // whether or not we have an executable image.
967 // For object files, SizeOfRawData contains the size of section's data;
968 // VirtualSize is always zero.
970 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
971 // actual section size is in VirtualSize. It is possible for VirtualSize to
972 // be greater than SizeOfRawData; the contents past that point should be
973 // considered to be zero.
974 uint32_t SectionSize;
975 if (Sec->VirtualSize)
976 SectionSize = std::min(Sec->VirtualSize, Sec->SizeOfRawData);
978 SectionSize = Sec->SizeOfRawData;
984 COFFObjectFile::getSectionContents(const coff_section *Sec,
985 ArrayRef<uint8_t> &Res) const {
986 // PointerToRawData and SizeOfRawData won't make sense for BSS sections,
987 // don't do anything interesting for them.
988 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
989 "BSS sections don't have contents!");
990 // The only thing that we need to verify is that the contents is contained
991 // within the file bounds. We don't need to make sure it doesn't cover other
992 // data, as there's nothing that says that is not allowed.
993 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
994 uint32_t SectionSize = getSectionSize(Sec);
995 if (checkOffset(Data, ConStart, SectionSize))
996 return object_error::parse_failed;
997 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
998 return object_error::success;
1001 const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
1002 return reinterpret_cast<const coff_relocation*>(Rel.p);
1005 void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
1006 Rel.p = reinterpret_cast<uintptr_t>(
1007 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
1010 std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
1011 uint64_t &Res) const {
1012 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
1015 std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
1016 uint64_t &Res) const {
1017 const coff_relocation *R = toRel(Rel);
1018 const support::ulittle32_t *VirtualAddressPtr;
1019 if (std::error_code EC =
1020 getObject(VirtualAddressPtr, Data, &R->VirtualAddress))
1022 Res = *VirtualAddressPtr;
1023 return object_error::success;
1026 symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
1027 const coff_relocation *R = toRel(Rel);
1029 if (R->SymbolTableIndex >= getNumberOfSymbols())
1030 return symbol_end();
1032 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
1033 else if (SymbolTable32)
1034 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
1036 llvm_unreachable("no symbol table pointer!");
1037 return symbol_iterator(SymbolRef(Ref, this));
1040 std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
1041 uint64_t &Res) const {
1042 const coff_relocation* R = toRel(Rel);
1044 return object_error::success;
1047 const coff_section *
1048 COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
1049 return toSec(Section.getRawDataRefImpl());
1052 COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1054 return toSymb<coff_symbol16>(Ref);
1056 return toSymb<coff_symbol32>(Ref);
1057 llvm_unreachable("no symbol table pointer!");
1060 COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1061 return getCOFFSymbol(Symbol.getRawDataRefImpl());
1064 const coff_relocation *
1065 COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1066 return toRel(Reloc.getRawDataRefImpl());
1069 #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1070 case COFF::reloc_type: \
1071 Res = #reloc_type; \
1075 COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
1076 SmallVectorImpl<char> &Result) const {
1077 const coff_relocation *Reloc = toRel(Rel);
1079 switch (getMachine()) {
1080 case COFF::IMAGE_FILE_MACHINE_AMD64:
1081 switch (Reloc->Type) {
1082 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1083 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1084 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1085 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1086 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1087 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1088 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1089 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1090 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1091 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1092 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1093 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1094 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1095 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1096 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1097 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1098 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1103 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1104 switch (Reloc->Type) {
1105 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1106 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1107 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1108 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1109 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1110 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1111 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1112 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1113 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1114 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1115 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1116 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1117 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1118 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1119 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1124 case COFF::IMAGE_FILE_MACHINE_I386:
1125 switch (Reloc->Type) {
1126 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1127 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1128 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1129 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1130 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1131 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1132 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1133 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1134 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1135 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1136 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1144 Result.append(Res.begin(), Res.end());
1145 return object_error::success;
1148 #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1151 COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
1152 SmallVectorImpl<char> &Result) const {
1153 const coff_relocation *Reloc = toRel(Rel);
1155 ErrorOr<COFFSymbolRef> Symb = getSymbol(Reloc->SymbolTableIndex);
1156 if (std::error_code EC = Symb.getError())
1158 Sym.p = reinterpret_cast<uintptr_t>(Symb->getRawPtr());
1160 if (std::error_code EC = getSymbolName(Sym, SymName))
1162 Result.append(SymName.begin(), SymName.end());
1163 return object_error::success;
1166 bool COFFObjectFile::isRelocatableObject() const {
1167 return !DataDirectory;
1170 bool ImportDirectoryEntryRef::
1171 operator==(const ImportDirectoryEntryRef &Other) const {
1172 return ImportTable == Other.ImportTable && Index == Other.Index;
1175 void ImportDirectoryEntryRef::moveNext() {
1179 std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1180 const import_directory_table_entry *&Result) const {
1181 Result = ImportTable + Index;
1182 return object_error::success;
1185 static imported_symbol_iterator
1186 makeImportedSymbolIterator(const COFFObjectFile *Object,
1187 uintptr_t Ptr, int Index) {
1188 if (Object->getBytesInAddress() == 4) {
1189 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
1190 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
1192 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
1193 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
1196 static imported_symbol_iterator
1197 importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
1198 uintptr_t IntPtr = 0;
1199 Object->getRvaPtr(RVA, IntPtr);
1200 return makeImportedSymbolIterator(Object, IntPtr, 0);
1203 static imported_symbol_iterator
1204 importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
1205 uintptr_t IntPtr = 0;
1206 Object->getRvaPtr(RVA, IntPtr);
1207 // Forward the pointer to the last entry which is null.
1209 if (Object->getBytesInAddress() == 4) {
1210 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1214 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1218 return makeImportedSymbolIterator(Object, IntPtr, Index);
1221 imported_symbol_iterator
1222 ImportDirectoryEntryRef::imported_symbol_begin() const {
1223 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1227 imported_symbol_iterator
1228 ImportDirectoryEntryRef::imported_symbol_end() const {
1229 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1233 iterator_range<imported_symbol_iterator>
1234 ImportDirectoryEntryRef::imported_symbols() const {
1235 return make_range(imported_symbol_begin(), imported_symbol_end());
1238 std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
1239 uintptr_t IntPtr = 0;
1240 if (std::error_code EC =
1241 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
1243 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1244 return object_error::success;
1248 ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1249 Result = ImportTable[Index].ImportLookupTableRVA;
1250 return object_error::success;
1254 ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1255 Result = ImportTable[Index].ImportAddressTableRVA;
1256 return object_error::success;
1259 std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
1260 const import_lookup_table_entry32 *&Result) const {
1261 uintptr_t IntPtr = 0;
1262 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1263 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1265 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
1266 return object_error::success;
1269 bool DelayImportDirectoryEntryRef::
1270 operator==(const DelayImportDirectoryEntryRef &Other) const {
1271 return Table == Other.Table && Index == Other.Index;
1274 void DelayImportDirectoryEntryRef::moveNext() {
1278 imported_symbol_iterator
1279 DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1280 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1284 imported_symbol_iterator
1285 DelayImportDirectoryEntryRef::imported_symbol_end() const {
1286 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1290 iterator_range<imported_symbol_iterator>
1291 DelayImportDirectoryEntryRef::imported_symbols() const {
1292 return make_range(imported_symbol_begin(), imported_symbol_end());
1295 std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1296 uintptr_t IntPtr = 0;
1297 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1299 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1300 return object_error::success;
1303 std::error_code DelayImportDirectoryEntryRef::
1304 getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1306 return object_error::success;
1309 std::error_code DelayImportDirectoryEntryRef::
1310 getImportAddress(int AddrIndex, uint64_t &Result) const {
1311 uint32_t RVA = Table[Index].DelayImportAddressTable +
1312 AddrIndex * (OwningObject->is64() ? 8 : 4);
1313 uintptr_t IntPtr = 0;
1314 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1316 if (OwningObject->is64())
1317 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
1319 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
1320 return object_error::success;
1323 bool ExportDirectoryEntryRef::
1324 operator==(const ExportDirectoryEntryRef &Other) const {
1325 return ExportTable == Other.ExportTable && Index == Other.Index;
1328 void ExportDirectoryEntryRef::moveNext() {
1332 // Returns the name of the current export symbol. If the symbol is exported only
1333 // by ordinal, the empty string is set as a result.
1334 std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
1335 uintptr_t IntPtr = 0;
1336 if (std::error_code EC =
1337 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
1339 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1340 return object_error::success;
1343 // Returns the starting ordinal number.
1345 ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
1346 Result = ExportTable->OrdinalBase;
1347 return object_error::success;
1350 // Returns the export ordinal of the current export symbol.
1351 std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
1352 Result = ExportTable->OrdinalBase + Index;
1353 return object_error::success;
1356 // Returns the address of the current export symbol.
1357 std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
1358 uintptr_t IntPtr = 0;
1359 if (std::error_code EC =
1360 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
1362 const export_address_table_entry *entry =
1363 reinterpret_cast<const export_address_table_entry *>(IntPtr);
1364 Result = entry[Index].ExportRVA;
1365 return object_error::success;
1368 // Returns the name of the current export symbol. If the symbol is exported only
1369 // by ordinal, the empty string is set as a result.
1371 ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
1372 uintptr_t IntPtr = 0;
1373 if (std::error_code EC =
1374 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
1376 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1378 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1380 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1381 I < E; ++I, ++Offset) {
1384 if (std::error_code EC =
1385 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
1387 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
1388 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
1390 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1391 return object_error::success;
1394 return object_error::success;
1397 bool ImportedSymbolRef::
1398 operator==(const ImportedSymbolRef &Other) const {
1399 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1400 && Index == Other.Index;
1403 void ImportedSymbolRef::moveNext() {
1408 ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1411 // If a symbol is imported only by ordinal, it has no name.
1412 if (Entry32[Index].isOrdinal())
1413 return object_error::success;
1414 RVA = Entry32[Index].getHintNameRVA();
1416 if (Entry64[Index].isOrdinal())
1417 return object_error::success;
1418 RVA = Entry64[Index].getHintNameRVA();
1420 uintptr_t IntPtr = 0;
1421 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1423 // +2 because the first two bytes is hint.
1424 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
1425 return object_error::success;
1428 std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1431 if (Entry32[Index].isOrdinal()) {
1432 Result = Entry32[Index].getOrdinal();
1433 return object_error::success;
1435 RVA = Entry32[Index].getHintNameRVA();
1437 if (Entry64[Index].isOrdinal()) {
1438 Result = Entry64[Index].getOrdinal();
1439 return object_error::success;
1441 RVA = Entry64[Index].getHintNameRVA();
1443 uintptr_t IntPtr = 0;
1444 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1446 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
1447 return object_error::success;
1450 ErrorOr<std::unique_ptr<COFFObjectFile>>
1451 ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
1453 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
1456 return std::move(Ret);
1459 bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1460 return Header == Other.Header && Index == Other.Index;
1463 void BaseRelocRef::moveNext() {
1464 // Header->BlockSize is the size of the current block, including the
1465 // size of the header itself.
1466 uint32_t Size = sizeof(*Header) +
1467 sizeof(coff_base_reloc_block_entry) * (Index + 1);
1468 if (Size == Header->BlockSize) {
1469 // .reloc contains a list of base relocation blocks. Each block
1470 // consists of the header followed by entries. The header contains
1471 // how many entories will follow. When we reach the end of the
1472 // current block, proceed to the next block.
1473 Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1474 reinterpret_cast<const uint8_t *>(Header) + Size);
1481 std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1482 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1483 Type = Entry[Index].getType();
1484 return object_error::success;
1487 std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1488 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1489 Result = Header->PageRVA + Entry[Index].getOffset();
1490 return object_error::success;