1 //===- COFF.h - 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 #ifndef LLVM_OBJECT_COFF_H
15 #define LLVM_OBJECT_COFF_H
17 #include "llvm/ADT/PointerUnion.h"
18 #include "llvm/Object/ObjectFile.h"
19 #include "llvm/Support/COFF.h"
20 #include "llvm/Support/Endian.h"
21 #include "llvm/Support/ErrorOr.h"
24 template <typename T> class ArrayRef;
27 class ImportDirectoryEntryRef;
28 class DelayImportDirectoryEntryRef;
29 class ExportDirectoryEntryRef;
30 class ImportedSymbolRef;
32 typedef content_iterator<ImportDirectoryEntryRef> import_directory_iterator;
33 typedef content_iterator<DelayImportDirectoryEntryRef>
34 delay_import_directory_iterator;
35 typedef content_iterator<ExportDirectoryEntryRef> export_directory_iterator;
36 typedef content_iterator<ImportedSymbolRef> imported_symbol_iterator;
37 typedef content_iterator<BaseRelocRef> base_reloc_iterator;
39 /// The DOS compatible header at the front of all PE/COFF executables.
42 support::ulittle16_t UsedBytesInTheLastPage;
43 support::ulittle16_t FileSizeInPages;
44 support::ulittle16_t NumberOfRelocationItems;
45 support::ulittle16_t HeaderSizeInParagraphs;
46 support::ulittle16_t MinimumExtraParagraphs;
47 support::ulittle16_t MaximumExtraParagraphs;
48 support::ulittle16_t InitialRelativeSS;
49 support::ulittle16_t InitialSP;
50 support::ulittle16_t Checksum;
51 support::ulittle16_t InitialIP;
52 support::ulittle16_t InitialRelativeCS;
53 support::ulittle16_t AddressOfRelocationTable;
54 support::ulittle16_t OverlayNumber;
55 support::ulittle16_t Reserved[4];
56 support::ulittle16_t OEMid;
57 support::ulittle16_t OEMinfo;
58 support::ulittle16_t Reserved2[10];
59 support::ulittle32_t AddressOfNewExeHeader;
62 struct coff_file_header {
63 support::ulittle16_t Machine;
64 support::ulittle16_t NumberOfSections;
65 support::ulittle32_t TimeDateStamp;
66 support::ulittle32_t PointerToSymbolTable;
67 support::ulittle32_t NumberOfSymbols;
68 support::ulittle16_t SizeOfOptionalHeader;
69 support::ulittle16_t Characteristics;
71 bool isImportLibrary() const { return NumberOfSections == 0xffff; }
74 struct coff_bigobj_file_header {
75 support::ulittle16_t Sig1;
76 support::ulittle16_t Sig2;
77 support::ulittle16_t Version;
78 support::ulittle16_t Machine;
79 support::ulittle32_t TimeDateStamp;
81 support::ulittle32_t unused1;
82 support::ulittle32_t unused2;
83 support::ulittle32_t unused3;
84 support::ulittle32_t unused4;
85 support::ulittle32_t NumberOfSections;
86 support::ulittle32_t PointerToSymbolTable;
87 support::ulittle32_t NumberOfSymbols;
90 /// The 32-bit PE header that follows the COFF header.
92 support::ulittle16_t Magic;
93 uint8_t MajorLinkerVersion;
94 uint8_t MinorLinkerVersion;
95 support::ulittle32_t SizeOfCode;
96 support::ulittle32_t SizeOfInitializedData;
97 support::ulittle32_t SizeOfUninitializedData;
98 support::ulittle32_t AddressOfEntryPoint;
99 support::ulittle32_t BaseOfCode;
100 support::ulittle32_t BaseOfData;
101 support::ulittle32_t ImageBase;
102 support::ulittle32_t SectionAlignment;
103 support::ulittle32_t FileAlignment;
104 support::ulittle16_t MajorOperatingSystemVersion;
105 support::ulittle16_t MinorOperatingSystemVersion;
106 support::ulittle16_t MajorImageVersion;
107 support::ulittle16_t MinorImageVersion;
108 support::ulittle16_t MajorSubsystemVersion;
109 support::ulittle16_t MinorSubsystemVersion;
110 support::ulittle32_t Win32VersionValue;
111 support::ulittle32_t SizeOfImage;
112 support::ulittle32_t SizeOfHeaders;
113 support::ulittle32_t CheckSum;
114 support::ulittle16_t Subsystem;
115 // FIXME: This should be DllCharacteristics.
116 support::ulittle16_t DLLCharacteristics;
117 support::ulittle32_t SizeOfStackReserve;
118 support::ulittle32_t SizeOfStackCommit;
119 support::ulittle32_t SizeOfHeapReserve;
120 support::ulittle32_t SizeOfHeapCommit;
121 support::ulittle32_t LoaderFlags;
122 // FIXME: This should be NumberOfRvaAndSizes.
123 support::ulittle32_t NumberOfRvaAndSize;
126 /// The 64-bit PE header that follows the COFF header.
127 struct pe32plus_header {
128 support::ulittle16_t Magic;
129 uint8_t MajorLinkerVersion;
130 uint8_t MinorLinkerVersion;
131 support::ulittle32_t SizeOfCode;
132 support::ulittle32_t SizeOfInitializedData;
133 support::ulittle32_t SizeOfUninitializedData;
134 support::ulittle32_t AddressOfEntryPoint;
135 support::ulittle32_t BaseOfCode;
136 support::ulittle64_t ImageBase;
137 support::ulittle32_t SectionAlignment;
138 support::ulittle32_t FileAlignment;
139 support::ulittle16_t MajorOperatingSystemVersion;
140 support::ulittle16_t MinorOperatingSystemVersion;
141 support::ulittle16_t MajorImageVersion;
142 support::ulittle16_t MinorImageVersion;
143 support::ulittle16_t MajorSubsystemVersion;
144 support::ulittle16_t MinorSubsystemVersion;
145 support::ulittle32_t Win32VersionValue;
146 support::ulittle32_t SizeOfImage;
147 support::ulittle32_t SizeOfHeaders;
148 support::ulittle32_t CheckSum;
149 support::ulittle16_t Subsystem;
150 support::ulittle16_t DLLCharacteristics;
151 support::ulittle64_t SizeOfStackReserve;
152 support::ulittle64_t SizeOfStackCommit;
153 support::ulittle64_t SizeOfHeapReserve;
154 support::ulittle64_t SizeOfHeapCommit;
155 support::ulittle32_t LoaderFlags;
156 support::ulittle32_t NumberOfRvaAndSize;
159 struct data_directory {
160 support::ulittle32_t RelativeVirtualAddress;
161 support::ulittle32_t Size;
164 struct import_directory_table_entry {
165 support::ulittle32_t ImportLookupTableRVA;
166 support::ulittle32_t TimeDateStamp;
167 support::ulittle32_t ForwarderChain;
168 support::ulittle32_t NameRVA;
169 support::ulittle32_t ImportAddressTableRVA;
172 template <typename IntTy>
173 struct import_lookup_table_entry {
176 bool isOrdinal() const { return Data < 0; }
178 uint16_t getOrdinal() const {
179 assert(isOrdinal() && "ILT entry is not an ordinal!");
180 return Data & 0xFFFF;
183 uint32_t getHintNameRVA() const {
184 assert(!isOrdinal() && "ILT entry is not a Hint/Name RVA!");
185 return Data & 0xFFFFFFFF;
189 typedef import_lookup_table_entry<support::little32_t>
190 import_lookup_table_entry32;
191 typedef import_lookup_table_entry<support::little64_t>
192 import_lookup_table_entry64;
194 struct delay_import_directory_table_entry {
195 // dumpbin reports this field as "Characteristics" instead of "Attributes".
196 support::ulittle32_t Attributes;
197 support::ulittle32_t Name;
198 support::ulittle32_t ModuleHandle;
199 support::ulittle32_t DelayImportAddressTable;
200 support::ulittle32_t DelayImportNameTable;
201 support::ulittle32_t BoundDelayImportTable;
202 support::ulittle32_t UnloadDelayImportTable;
203 support::ulittle32_t TimeStamp;
206 struct export_directory_table_entry {
207 support::ulittle32_t ExportFlags;
208 support::ulittle32_t TimeDateStamp;
209 support::ulittle16_t MajorVersion;
210 support::ulittle16_t MinorVersion;
211 support::ulittle32_t NameRVA;
212 support::ulittle32_t OrdinalBase;
213 support::ulittle32_t AddressTableEntries;
214 support::ulittle32_t NumberOfNamePointers;
215 support::ulittle32_t ExportAddressTableRVA;
216 support::ulittle32_t NamePointerRVA;
217 support::ulittle32_t OrdinalTableRVA;
220 union export_address_table_entry {
221 support::ulittle32_t ExportRVA;
222 support::ulittle32_t ForwarderRVA;
225 typedef support::ulittle32_t export_name_pointer_table_entry;
226 typedef support::ulittle16_t export_ordinal_table_entry;
228 struct StringTableOffset {
229 support::ulittle32_t Zeroes;
230 support::ulittle32_t Offset;
233 template <typename SectionNumberType>
236 char ShortName[COFF::NameSize];
237 StringTableOffset Offset;
240 support::ulittle32_t Value;
241 SectionNumberType SectionNumber;
243 support::ulittle16_t Type;
245 uint8_t StorageClass;
246 uint8_t NumberOfAuxSymbols;
249 typedef coff_symbol<support::ulittle16_t> coff_symbol16;
250 typedef coff_symbol<support::ulittle32_t> coff_symbol32;
252 // Contains only common parts of coff_symbol16 and coff_symbol32.
253 struct coff_symbol_generic {
255 char ShortName[COFF::NameSize];
256 StringTableOffset Offset;
258 support::ulittle32_t Value;
261 class COFFSymbolRef {
263 COFFSymbolRef(const coff_symbol16 *CS) : CS16(CS), CS32(nullptr) {}
264 COFFSymbolRef(const coff_symbol32 *CS) : CS16(nullptr), CS32(CS) {}
265 COFFSymbolRef() : CS16(nullptr), CS32(nullptr) {}
267 const void *getRawPtr() const {
268 return CS16 ? static_cast<const void *>(CS16) : CS32;
271 const coff_symbol_generic *getGeneric() const {
273 return reinterpret_cast<const coff_symbol_generic *>(CS16);
274 return reinterpret_cast<const coff_symbol_generic *>(CS32);
277 friend bool operator<(COFFSymbolRef A, COFFSymbolRef B) {
278 return A.getRawPtr() < B.getRawPtr();
281 bool isBigObj() const {
286 llvm_unreachable("COFFSymbolRef points to nothing!");
289 const char *getShortName() const {
290 return CS16 ? CS16->Name.ShortName : CS32->Name.ShortName;
293 const StringTableOffset &getStringTableOffset() const {
294 assert(isSet() && "COFFSymbolRef points to nothing!");
295 return CS16 ? CS16->Name.Offset : CS32->Name.Offset;
298 uint32_t getValue() const { return CS16 ? CS16->Value : CS32->Value; }
300 int32_t getSectionNumber() const {
301 assert(isSet() && "COFFSymbolRef points to nothing!");
303 // Reserved sections are returned as negative numbers.
304 if (CS16->SectionNumber <= COFF::MaxNumberOfSections16)
305 return CS16->SectionNumber;
306 return static_cast<int16_t>(CS16->SectionNumber);
308 return static_cast<int32_t>(CS32->SectionNumber);
311 uint16_t getType() const {
312 assert(isSet() && "COFFSymbolRef points to nothing!");
313 return CS16 ? CS16->Type : CS32->Type;
316 uint8_t getStorageClass() const {
317 assert(isSet() && "COFFSymbolRef points to nothing!");
318 return CS16 ? CS16->StorageClass : CS32->StorageClass;
321 uint8_t getNumberOfAuxSymbols() const {
322 assert(isSet() && "COFFSymbolRef points to nothing!");
323 return CS16 ? CS16->NumberOfAuxSymbols : CS32->NumberOfAuxSymbols;
326 uint8_t getBaseType() const { return getType() & 0x0F; }
328 uint8_t getComplexType() const {
329 return (getType() & 0xF0) >> COFF::SCT_COMPLEX_TYPE_SHIFT;
332 bool isAbsolute() const {
333 return getSectionNumber() == -1;
336 bool isExternal() const {
337 return getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL;
340 bool isCommon() const {
341 return isExternal() && getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED &&
345 bool isUndefined() const {
346 return isExternal() && getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED &&
350 bool isWeakExternal() const {
351 return getStorageClass() == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
354 bool isFunctionDefinition() const {
355 return isExternal() && getBaseType() == COFF::IMAGE_SYM_TYPE_NULL &&
356 getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION &&
357 !COFF::isReservedSectionNumber(getSectionNumber());
360 bool isFunctionLineInfo() const {
361 return getStorageClass() == COFF::IMAGE_SYM_CLASS_FUNCTION;
364 bool isAnyUndefined() const {
365 return isUndefined() || isWeakExternal();
368 bool isFileRecord() const {
369 return getStorageClass() == COFF::IMAGE_SYM_CLASS_FILE;
372 bool isSection() const {
373 return getStorageClass() == COFF::IMAGE_SYM_CLASS_SECTION;
376 bool isSectionDefinition() const {
377 // C++/CLI creates external ABS symbols for non-const appdomain globals.
378 // These are also followed by an auxiliary section definition.
379 bool isAppdomainGlobal =
380 getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
381 getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE;
382 bool isOrdinarySection = getStorageClass() == COFF::IMAGE_SYM_CLASS_STATIC;
383 if (!getNumberOfAuxSymbols())
385 return isAppdomainGlobal || isOrdinarySection;
388 bool isCLRToken() const {
389 return getStorageClass() == COFF::IMAGE_SYM_CLASS_CLR_TOKEN;
393 bool isSet() const { return CS16 || CS32; }
395 const coff_symbol16 *CS16;
396 const coff_symbol32 *CS32;
399 struct coff_section {
400 char Name[COFF::NameSize];
401 support::ulittle32_t VirtualSize;
402 support::ulittle32_t VirtualAddress;
403 support::ulittle32_t SizeOfRawData;
404 support::ulittle32_t PointerToRawData;
405 support::ulittle32_t PointerToRelocations;
406 support::ulittle32_t PointerToLinenumbers;
407 support::ulittle16_t NumberOfRelocations;
408 support::ulittle16_t NumberOfLinenumbers;
409 support::ulittle32_t Characteristics;
411 // Returns true if the actual number of relocations is stored in
412 // VirtualAddress field of the first relocation table entry.
413 bool hasExtendedRelocations() const {
414 return (Characteristics & COFF::IMAGE_SCN_LNK_NRELOC_OVFL) &&
415 NumberOfRelocations == UINT16_MAX;
419 struct coff_relocation {
420 support::ulittle32_t VirtualAddress;
421 support::ulittle32_t SymbolTableIndex;
422 support::ulittle16_t Type;
425 struct coff_aux_function_definition {
426 support::ulittle32_t TagIndex;
427 support::ulittle32_t TotalSize;
428 support::ulittle32_t PointerToLinenumber;
429 support::ulittle32_t PointerToNextFunction;
432 struct coff_aux_bf_and_ef_symbol {
434 support::ulittle16_t Linenumber;
436 support::ulittle32_t PointerToNextFunction;
439 struct coff_aux_weak_external {
440 support::ulittle32_t TagIndex;
441 support::ulittle32_t Characteristics;
444 struct coff_aux_section_definition {
445 support::ulittle32_t Length;
446 support::ulittle16_t NumberOfRelocations;
447 support::ulittle16_t NumberOfLinenumbers;
448 support::ulittle32_t CheckSum;
449 support::ulittle16_t NumberLowPart;
452 support::ulittle16_t NumberHighPart;
453 int32_t getNumber(bool IsBigObj) const {
454 uint32_t Number = static_cast<uint32_t>(NumberLowPart);
456 Number |= static_cast<uint32_t>(NumberHighPart) << 16;
457 return static_cast<int32_t>(Number);
461 struct coff_aux_clr_token {
464 support::ulittle32_t SymbolTableIndex;
467 struct coff_import_header {
468 support::ulittle16_t Sig1;
469 support::ulittle16_t Sig2;
470 support::ulittle16_t Version;
471 support::ulittle16_t Machine;
472 support::ulittle32_t TimeDateStamp;
473 support::ulittle32_t SizeOfData;
474 support::ulittle16_t OrdinalHint;
475 support::ulittle16_t TypeInfo;
476 int getType() const { return TypeInfo & 0x3; }
477 int getNameType() const { return (TypeInfo >> 2) & 0x7; }
480 struct coff_import_directory_table_entry {
481 support::ulittle32_t ImportLookupTableRVA;
482 support::ulittle32_t TimeDateStamp;
483 support::ulittle32_t ForwarderChain;
484 support::ulittle32_t NameRVA;
485 support::ulittle32_t ImportAddressTableRVA;
488 struct coff_load_configuration32 {
489 support::ulittle32_t Characteristics;
490 support::ulittle32_t TimeDateStamp;
491 support::ulittle16_t MajorVersion;
492 support::ulittle16_t MinorVersion;
493 support::ulittle32_t GlobalFlagsClear;
494 support::ulittle32_t GlobalFlagsSet;
495 support::ulittle32_t CriticalSectionDefaultTimeout;
496 support::ulittle32_t DeCommitFreeBlockThreshold;
497 support::ulittle32_t DeCommitTotalFreeThreshold;
498 support::ulittle32_t LockPrefixTable;
499 support::ulittle32_t MaximumAllocationSize;
500 support::ulittle32_t VirtualMemoryThreshold;
501 support::ulittle32_t ProcessAffinityMask;
502 support::ulittle32_t ProcessHeapFlags;
503 support::ulittle16_t CSDVersion;
504 support::ulittle16_t Reserved;
505 support::ulittle32_t EditList;
506 support::ulittle32_t SecurityCookie;
507 support::ulittle32_t SEHandlerTable;
508 support::ulittle32_t SEHandlerCount;
511 struct coff_load_configuration64 {
512 support::ulittle32_t Characteristics;
513 support::ulittle32_t TimeDateStamp;
514 support::ulittle16_t MajorVersion;
515 support::ulittle16_t MinorVersion;
516 support::ulittle32_t GlobalFlagsClear;
517 support::ulittle32_t GlobalFlagsSet;
518 support::ulittle32_t CriticalSectionDefaultTimeout;
519 support::ulittle32_t DeCommitFreeBlockThreshold;
520 support::ulittle32_t DeCommitTotalFreeThreshold;
521 support::ulittle32_t LockPrefixTable;
522 support::ulittle32_t MaximumAllocationSize;
523 support::ulittle32_t VirtualMemoryThreshold;
524 support::ulittle32_t ProcessAffinityMask;
525 support::ulittle32_t ProcessHeapFlags;
526 support::ulittle16_t CSDVersion;
527 support::ulittle16_t Reserved;
528 support::ulittle32_t EditList;
529 support::ulittle64_t SecurityCookie;
530 support::ulittle64_t SEHandlerTable;
531 support::ulittle64_t SEHandlerCount;
534 struct coff_runtime_function_x64 {
535 support::ulittle32_t BeginAddress;
536 support::ulittle32_t EndAddress;
537 support::ulittle32_t UnwindInformation;
540 struct coff_base_reloc_block_header {
541 support::ulittle32_t PageRVA;
542 support::ulittle32_t BlockSize;
545 struct coff_base_reloc_block_entry {
546 support::ulittle16_t Data;
547 int getType() const { return Data >> 12; }
548 int getOffset() const { return Data & ((1 << 12) - 1); }
551 class COFFObjectFile : public ObjectFile {
553 friend class ImportDirectoryEntryRef;
554 friend class ExportDirectoryEntryRef;
555 const coff_file_header *COFFHeader;
556 const coff_bigobj_file_header *COFFBigObjHeader;
557 const pe32_header *PE32Header;
558 const pe32plus_header *PE32PlusHeader;
559 const data_directory *DataDirectory;
560 const coff_section *SectionTable;
561 const coff_symbol16 *SymbolTable16;
562 const coff_symbol32 *SymbolTable32;
563 const char *StringTable;
564 uint32_t StringTableSize;
565 const import_directory_table_entry *ImportDirectory;
566 uint32_t NumberOfImportDirectory;
567 const delay_import_directory_table_entry *DelayImportDirectory;
568 uint32_t NumberOfDelayImportDirectory;
569 const export_directory_table_entry *ExportDirectory;
570 const coff_base_reloc_block_header *BaseRelocHeader;
571 const coff_base_reloc_block_header *BaseRelocEnd;
573 std::error_code getString(uint32_t offset, StringRef &Res) const;
575 template <typename coff_symbol_type>
576 const coff_symbol_type *toSymb(DataRefImpl Symb) const;
577 const coff_section *toSec(DataRefImpl Sec) const;
578 const coff_relocation *toRel(DataRefImpl Rel) const;
580 std::error_code initSymbolTablePtr();
581 std::error_code initImportTablePtr();
582 std::error_code initDelayImportTablePtr();
583 std::error_code initExportTablePtr();
584 std::error_code initBaseRelocPtr();
587 uintptr_t getSymbolTable() const {
589 return reinterpret_cast<uintptr_t>(SymbolTable16);
591 return reinterpret_cast<uintptr_t>(SymbolTable32);
594 uint16_t getMachine() const {
596 return COFFHeader->Machine;
597 if (COFFBigObjHeader)
598 return COFFBigObjHeader->Machine;
599 llvm_unreachable("no COFF header!");
601 uint16_t getSizeOfOptionalHeader() const {
603 return COFFHeader->isImportLibrary() ? 0
604 : COFFHeader->SizeOfOptionalHeader;
605 // bigobj doesn't have this field.
606 if (COFFBigObjHeader)
608 llvm_unreachable("no COFF header!");
610 uint16_t getCharacteristics() const {
612 return COFFHeader->isImportLibrary() ? 0 : COFFHeader->Characteristics;
613 // bigobj doesn't have characteristics to speak of,
614 // editbin will silently lie to you if you attempt to set any.
615 if (COFFBigObjHeader)
617 llvm_unreachable("no COFF header!");
619 uint32_t getTimeDateStamp() const {
621 return COFFHeader->TimeDateStamp;
622 if (COFFBigObjHeader)
623 return COFFBigObjHeader->TimeDateStamp;
624 llvm_unreachable("no COFF header!");
626 uint32_t getNumberOfSections() const {
628 return COFFHeader->isImportLibrary() ? 0 : COFFHeader->NumberOfSections;
629 if (COFFBigObjHeader)
630 return COFFBigObjHeader->NumberOfSections;
631 llvm_unreachable("no COFF header!");
633 uint32_t getPointerToSymbolTable() const {
635 return COFFHeader->isImportLibrary() ? 0
636 : COFFHeader->PointerToSymbolTable;
637 if (COFFBigObjHeader)
638 return COFFBigObjHeader->PointerToSymbolTable;
639 llvm_unreachable("no COFF header!");
641 uint32_t getNumberOfSymbols() const {
643 return COFFHeader->isImportLibrary() ? 0 : COFFHeader->NumberOfSymbols;
644 if (COFFBigObjHeader)
645 return COFFBigObjHeader->NumberOfSymbols;
646 llvm_unreachable("no COFF header!");
649 void moveSymbolNext(DataRefImpl &Symb) const override;
650 ErrorOr<StringRef> getSymbolName(DataRefImpl Symb) const override;
651 ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
652 uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
653 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
654 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
655 SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
656 ErrorOr<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
657 void moveSectionNext(DataRefImpl &Sec) const override;
658 std::error_code getSectionName(DataRefImpl Sec,
659 StringRef &Res) const override;
660 uint64_t getSectionAddress(DataRefImpl Sec) const override;
661 uint64_t getSectionSize(DataRefImpl Sec) const override;
662 std::error_code getSectionContents(DataRefImpl Sec,
663 StringRef &Res) const override;
664 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
665 bool isSectionText(DataRefImpl Sec) const override;
666 bool isSectionData(DataRefImpl Sec) const override;
667 bool isSectionBSS(DataRefImpl Sec) const override;
668 bool isSectionVirtual(DataRefImpl Sec) const override;
669 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
670 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
672 void moveRelocationNext(DataRefImpl &Rel) const override;
673 uint64_t getRelocationOffset(DataRefImpl Rel) const override;
674 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
675 uint64_t getRelocationType(DataRefImpl Rel) const override;
676 void getRelocationTypeName(DataRefImpl Rel,
677 SmallVectorImpl<char> &Result) const override;
680 COFFObjectFile(MemoryBufferRef Object, std::error_code &EC);
681 basic_symbol_iterator symbol_begin_impl() const override;
682 basic_symbol_iterator symbol_end_impl() const override;
683 section_iterator section_begin() const override;
684 section_iterator section_end() const override;
686 const coff_section *getCOFFSection(const SectionRef &Section) const;
687 COFFSymbolRef getCOFFSymbol(const DataRefImpl &Ref) const;
688 COFFSymbolRef getCOFFSymbol(const SymbolRef &Symbol) const;
689 const coff_relocation *getCOFFRelocation(const RelocationRef &Reloc) const;
690 unsigned getSectionID(SectionRef Sec) const;
691 unsigned getSymbolSectionID(SymbolRef Sym) const;
693 uint8_t getBytesInAddress() const override;
694 StringRef getFileFormatName() const override;
695 unsigned getArch() const override;
697 import_directory_iterator import_directory_begin() const;
698 import_directory_iterator import_directory_end() const;
699 delay_import_directory_iterator delay_import_directory_begin() const;
700 delay_import_directory_iterator delay_import_directory_end() const;
701 export_directory_iterator export_directory_begin() const;
702 export_directory_iterator export_directory_end() const;
703 base_reloc_iterator base_reloc_begin() const;
704 base_reloc_iterator base_reloc_end() const;
706 iterator_range<import_directory_iterator> import_directories() const;
707 iterator_range<delay_import_directory_iterator>
708 delay_import_directories() const;
709 iterator_range<export_directory_iterator> export_directories() const;
710 iterator_range<base_reloc_iterator> base_relocs() const;
712 const dos_header *getDOSHeader() const {
713 if (!PE32Header && !PE32PlusHeader)
715 return reinterpret_cast<const dos_header *>(base());
717 std::error_code getPE32Header(const pe32_header *&Res) const;
718 std::error_code getPE32PlusHeader(const pe32plus_header *&Res) const;
719 std::error_code getDataDirectory(uint32_t index,
720 const data_directory *&Res) const;
721 std::error_code getSection(int32_t index, const coff_section *&Res) const;
722 template <typename coff_symbol_type>
723 std::error_code getSymbol(uint32_t Index,
724 const coff_symbol_type *&Res) const {
725 if (Index >= getNumberOfSymbols())
726 return object_error::parse_failed;
728 Res = reinterpret_cast<coff_symbol_type *>(getSymbolTable()) + Index;
729 return std::error_code();
731 ErrorOr<COFFSymbolRef> getSymbol(uint32_t index) const {
733 const coff_symbol16 *Symb = nullptr;
734 if (std::error_code EC = getSymbol(index, Symb))
736 return COFFSymbolRef(Symb);
739 const coff_symbol32 *Symb = nullptr;
740 if (std::error_code EC = getSymbol(index, Symb))
742 return COFFSymbolRef(Symb);
744 return object_error::parse_failed;
746 template <typename T>
747 std::error_code getAuxSymbol(uint32_t index, const T *&Res) const {
748 ErrorOr<COFFSymbolRef> s = getSymbol(index);
749 if (std::error_code EC = s.getError())
751 Res = reinterpret_cast<const T *>(s->getRawPtr());
752 return std::error_code();
754 std::error_code getSymbolName(COFFSymbolRef Symbol, StringRef &Res) const;
755 std::error_code getSymbolName(const coff_symbol_generic *Symbol,
756 StringRef &Res) const;
758 ArrayRef<uint8_t> getSymbolAuxData(COFFSymbolRef Symbol) const;
760 size_t getSymbolTableEntrySize() const {
762 return sizeof(coff_symbol16);
763 if (COFFBigObjHeader)
764 return sizeof(coff_symbol32);
765 llvm_unreachable("null symbol table pointer!");
768 iterator_range<const coff_relocation *>
769 getRelocations(const coff_section *Sec) const;
771 std::error_code getSectionName(const coff_section *Sec, StringRef &Res) const;
772 uint64_t getSectionSize(const coff_section *Sec) const;
773 std::error_code getSectionContents(const coff_section *Sec,
774 ArrayRef<uint8_t> &Res) const;
776 uint64_t getImageBase() const;
777 std::error_code getVaPtr(uint64_t VA, uintptr_t &Res) const;
778 std::error_code getRvaPtr(uint32_t Rva, uintptr_t &Res) const;
779 std::error_code getHintName(uint32_t Rva, uint16_t &Hint,
780 StringRef &Name) const;
782 bool isRelocatableObject() const override;
783 bool is64() const { return PE32PlusHeader; }
785 static inline bool classof(const Binary *v) { return v->isCOFF(); }
788 // The iterator for the import directory table.
789 class ImportDirectoryEntryRef {
791 ImportDirectoryEntryRef() : OwningObject(nullptr) {}
792 ImportDirectoryEntryRef(const import_directory_table_entry *Table, uint32_t I,
793 const COFFObjectFile *Owner)
794 : ImportTable(Table), Index(I), OwningObject(Owner) {}
796 bool operator==(const ImportDirectoryEntryRef &Other) const;
799 imported_symbol_iterator imported_symbol_begin() const;
800 imported_symbol_iterator imported_symbol_end() const;
801 iterator_range<imported_symbol_iterator> imported_symbols() const;
803 std::error_code getName(StringRef &Result) const;
804 std::error_code getImportLookupTableRVA(uint32_t &Result) const;
805 std::error_code getImportAddressTableRVA(uint32_t &Result) const;
808 getImportTableEntry(const import_directory_table_entry *&Result) const;
811 getImportLookupEntry(const import_lookup_table_entry32 *&Result) const;
814 const import_directory_table_entry *ImportTable;
816 const COFFObjectFile *OwningObject;
819 class DelayImportDirectoryEntryRef {
821 DelayImportDirectoryEntryRef() : OwningObject(nullptr) {}
822 DelayImportDirectoryEntryRef(const delay_import_directory_table_entry *T,
823 uint32_t I, const COFFObjectFile *Owner)
824 : Table(T), Index(I), OwningObject(Owner) {}
826 bool operator==(const DelayImportDirectoryEntryRef &Other) const;
829 imported_symbol_iterator imported_symbol_begin() const;
830 imported_symbol_iterator imported_symbol_end() const;
831 iterator_range<imported_symbol_iterator> imported_symbols() const;
833 std::error_code getName(StringRef &Result) const;
834 std::error_code getDelayImportTable(
835 const delay_import_directory_table_entry *&Result) const;
836 std::error_code getImportAddress(int AddrIndex, uint64_t &Result) const;
839 const delay_import_directory_table_entry *Table;
841 const COFFObjectFile *OwningObject;
844 // The iterator for the export directory table entry.
845 class ExportDirectoryEntryRef {
847 ExportDirectoryEntryRef() : OwningObject(nullptr) {}
848 ExportDirectoryEntryRef(const export_directory_table_entry *Table, uint32_t I,
849 const COFFObjectFile *Owner)
850 : ExportTable(Table), Index(I), OwningObject(Owner) {}
852 bool operator==(const ExportDirectoryEntryRef &Other) const;
855 std::error_code getDllName(StringRef &Result) const;
856 std::error_code getOrdinalBase(uint32_t &Result) const;
857 std::error_code getOrdinal(uint32_t &Result) const;
858 std::error_code getExportRVA(uint32_t &Result) const;
859 std::error_code getSymbolName(StringRef &Result) const;
862 const export_directory_table_entry *ExportTable;
864 const COFFObjectFile *OwningObject;
867 class ImportedSymbolRef {
869 ImportedSymbolRef() : OwningObject(nullptr) {}
870 ImportedSymbolRef(const import_lookup_table_entry32 *Entry, uint32_t I,
871 const COFFObjectFile *Owner)
872 : Entry32(Entry), Entry64(nullptr), Index(I), OwningObject(Owner) {}
873 ImportedSymbolRef(const import_lookup_table_entry64 *Entry, uint32_t I,
874 const COFFObjectFile *Owner)
875 : Entry32(nullptr), Entry64(Entry), Index(I), OwningObject(Owner) {}
877 bool operator==(const ImportedSymbolRef &Other) const;
880 std::error_code getSymbolName(StringRef &Result) const;
881 std::error_code getOrdinal(uint16_t &Result) const;
884 const import_lookup_table_entry32 *Entry32;
885 const import_lookup_table_entry64 *Entry64;
887 const COFFObjectFile *OwningObject;
892 BaseRelocRef() : OwningObject(nullptr) {}
893 BaseRelocRef(const coff_base_reloc_block_header *Header,
894 const COFFObjectFile *Owner)
895 : Header(Header), Index(0), OwningObject(Owner) {}
897 bool operator==(const BaseRelocRef &Other) const;
900 std::error_code getType(uint8_t &Type) const;
901 std::error_code getRVA(uint32_t &Result) const;
904 const coff_base_reloc_block_header *Header;
906 const COFFObjectFile *OwningObject;
909 } // end namespace object
910 } // end namespace llvm