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 class COFFSymbolRef {
254 COFFSymbolRef(const coff_symbol16 *CS) : CS16(CS), CS32(nullptr) {}
255 COFFSymbolRef(const coff_symbol32 *CS) : CS16(nullptr), CS32(CS) {}
256 COFFSymbolRef() : CS16(nullptr), CS32(nullptr) {}
258 const void *getRawPtr() const {
259 return CS16 ? static_cast<const void *>(CS16) : CS32;
262 friend bool operator<(COFFSymbolRef A, COFFSymbolRef B) {
263 return A.getRawPtr() < B.getRawPtr();
266 bool isBigObj() const {
271 llvm_unreachable("COFFSymbolRef points to nothing!");
274 const char *getShortName() const {
275 return CS16 ? CS16->Name.ShortName : CS32->Name.ShortName;
278 const StringTableOffset &getStringTableOffset() const {
279 assert(isSet() && "COFFSymbolRef points to nothing!");
280 return CS16 ? CS16->Name.Offset : CS32->Name.Offset;
283 uint32_t getValue() const { return CS16 ? CS16->Value : CS32->Value; }
285 int32_t getSectionNumber() const {
286 assert(isSet() && "COFFSymbolRef points to nothing!");
288 // Reserved sections are returned as negative numbers.
289 if (CS16->SectionNumber <= COFF::MaxNumberOfSections16)
290 return CS16->SectionNumber;
291 return static_cast<int16_t>(CS16->SectionNumber);
293 return static_cast<int32_t>(CS32->SectionNumber);
296 uint16_t getType() const {
297 assert(isSet() && "COFFSymbolRef points to nothing!");
298 return CS16 ? CS16->Type : CS32->Type;
301 uint8_t getStorageClass() const {
302 assert(isSet() && "COFFSymbolRef points to nothing!");
303 return CS16 ? CS16->StorageClass : CS32->StorageClass;
306 uint8_t getNumberOfAuxSymbols() const {
307 assert(isSet() && "COFFSymbolRef points to nothing!");
308 return CS16 ? CS16->NumberOfAuxSymbols : CS32->NumberOfAuxSymbols;
311 uint8_t getBaseType() const { return getType() & 0x0F; }
313 uint8_t getComplexType() const {
314 return (getType() & 0xF0) >> COFF::SCT_COMPLEX_TYPE_SHIFT;
317 bool isExternal() const {
318 return getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL;
321 bool isCommon() const {
322 return isExternal() && getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED &&
326 bool isUndefined() const {
327 return isExternal() && getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED &&
331 bool isWeakExternal() const {
332 return getStorageClass() == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
335 bool isFunctionDefinition() const {
336 return isExternal() && getBaseType() == COFF::IMAGE_SYM_TYPE_NULL &&
337 getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION &&
338 !COFF::isReservedSectionNumber(getSectionNumber());
341 bool isFunctionLineInfo() const {
342 return getStorageClass() == COFF::IMAGE_SYM_CLASS_FUNCTION;
345 bool isAnyUndefined() const {
346 return isUndefined() || isWeakExternal();
349 bool isFileRecord() const {
350 return getStorageClass() == COFF::IMAGE_SYM_CLASS_FILE;
353 bool isSection() const {
354 return getStorageClass() == COFF::IMAGE_SYM_CLASS_SECTION;
357 bool isSectionDefinition() const {
358 // C++/CLI creates external ABS symbols for non-const appdomain globals.
359 // These are also followed by an auxiliary section definition.
360 bool isAppdomainGlobal =
361 getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
362 getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE;
363 bool isOrdinarySection = getStorageClass() == COFF::IMAGE_SYM_CLASS_STATIC;
364 if (!getNumberOfAuxSymbols())
366 return isAppdomainGlobal || isOrdinarySection;
369 bool isCLRToken() const {
370 return getStorageClass() == COFF::IMAGE_SYM_CLASS_CLR_TOKEN;
374 bool isSet() const { return CS16 || CS32; }
376 const coff_symbol16 *CS16;
377 const coff_symbol32 *CS32;
380 struct coff_section {
381 char Name[COFF::NameSize];
382 support::ulittle32_t VirtualSize;
383 support::ulittle32_t VirtualAddress;
384 support::ulittle32_t SizeOfRawData;
385 support::ulittle32_t PointerToRawData;
386 support::ulittle32_t PointerToRelocations;
387 support::ulittle32_t PointerToLinenumbers;
388 support::ulittle16_t NumberOfRelocations;
389 support::ulittle16_t NumberOfLinenumbers;
390 support::ulittle32_t Characteristics;
392 // Returns true if the actual number of relocations is stored in
393 // VirtualAddress field of the first relocation table entry.
394 bool hasExtendedRelocations() const {
395 return (Characteristics & COFF::IMAGE_SCN_LNK_NRELOC_OVFL) &&
396 NumberOfRelocations == UINT16_MAX;
400 struct coff_relocation {
401 support::ulittle32_t VirtualAddress;
402 support::ulittle32_t SymbolTableIndex;
403 support::ulittle16_t Type;
406 struct coff_aux_function_definition {
407 support::ulittle32_t TagIndex;
408 support::ulittle32_t TotalSize;
409 support::ulittle32_t PointerToLinenumber;
410 support::ulittle32_t PointerToNextFunction;
413 struct coff_aux_bf_and_ef_symbol {
415 support::ulittle16_t Linenumber;
417 support::ulittle32_t PointerToNextFunction;
420 struct coff_aux_weak_external {
421 support::ulittle32_t TagIndex;
422 support::ulittle32_t Characteristics;
425 struct coff_aux_section_definition {
426 support::ulittle32_t Length;
427 support::ulittle16_t NumberOfRelocations;
428 support::ulittle16_t NumberOfLinenumbers;
429 support::ulittle32_t CheckSum;
430 support::ulittle16_t NumberLowPart;
433 support::ulittle16_t NumberHighPart;
434 int32_t getNumber(bool IsBigObj) const {
435 uint32_t Number = static_cast<uint32_t>(NumberLowPart);
437 Number |= static_cast<uint32_t>(NumberHighPart) << 16;
438 return static_cast<int32_t>(Number);
442 struct coff_aux_clr_token {
445 support::ulittle32_t SymbolTableIndex;
448 struct coff_import_directory_table_entry {
449 support::ulittle32_t ImportLookupTableRVA;
450 support::ulittle32_t TimeDateStamp;
451 support::ulittle32_t ForwarderChain;
452 support::ulittle32_t NameRVA;
453 support::ulittle32_t ImportAddressTableRVA;
456 struct coff_load_configuration32 {
457 support::ulittle32_t Characteristics;
458 support::ulittle32_t TimeDateStamp;
459 support::ulittle16_t MajorVersion;
460 support::ulittle16_t MinorVersion;
461 support::ulittle32_t GlobalFlagsClear;
462 support::ulittle32_t GlobalFlagsSet;
463 support::ulittle32_t CriticalSectionDefaultTimeout;
464 support::ulittle32_t DeCommitFreeBlockThreshold;
465 support::ulittle32_t DeCommitTotalFreeThreshold;
466 support::ulittle32_t LockPrefixTable;
467 support::ulittle32_t MaximumAllocationSize;
468 support::ulittle32_t VirtualMemoryThreshold;
469 support::ulittle32_t ProcessAffinityMask;
470 support::ulittle32_t ProcessHeapFlags;
471 support::ulittle16_t CSDVersion;
472 support::ulittle16_t Reserved;
473 support::ulittle32_t EditList;
474 support::ulittle32_t SecurityCookie;
475 support::ulittle32_t SEHandlerTable;
476 support::ulittle32_t SEHandlerCount;
479 struct coff_runtime_function_x64 {
480 support::ulittle32_t BeginAddress;
481 support::ulittle32_t EndAddress;
482 support::ulittle32_t UnwindInformation;
485 struct coff_base_reloc_block_header {
486 support::ulittle32_t PageRVA;
487 support::ulittle32_t BlockSize;
490 struct coff_base_reloc_block_entry {
491 support::ulittle16_t Data;
492 int getType() const { return Data >> 12; }
493 int getOffset() const { return Data & ((1 << 12) - 1); }
496 class COFFObjectFile : public ObjectFile {
498 friend class ImportDirectoryEntryRef;
499 friend class ExportDirectoryEntryRef;
500 const coff_file_header *COFFHeader;
501 const coff_bigobj_file_header *COFFBigObjHeader;
502 const pe32_header *PE32Header;
503 const pe32plus_header *PE32PlusHeader;
504 const data_directory *DataDirectory;
505 const coff_section *SectionTable;
506 const coff_symbol16 *SymbolTable16;
507 const coff_symbol32 *SymbolTable32;
508 const char *StringTable;
509 uint32_t StringTableSize;
510 const import_directory_table_entry *ImportDirectory;
511 uint32_t NumberOfImportDirectory;
512 const delay_import_directory_table_entry *DelayImportDirectory;
513 uint32_t NumberOfDelayImportDirectory;
514 const export_directory_table_entry *ExportDirectory;
515 const coff_base_reloc_block_header *BaseRelocHeader;
516 const coff_base_reloc_block_header *BaseRelocEnd;
518 std::error_code getString(uint32_t offset, StringRef &Res) const;
520 template <typename coff_symbol_type>
521 const coff_symbol_type *toSymb(DataRefImpl Symb) const;
522 const coff_section *toSec(DataRefImpl Sec) const;
523 const coff_relocation *toRel(DataRefImpl Rel) const;
525 std::error_code initSymbolTablePtr();
526 std::error_code initImportTablePtr();
527 std::error_code initDelayImportTablePtr();
528 std::error_code initExportTablePtr();
529 std::error_code initBaseRelocPtr();
532 uintptr_t getSymbolTable() const {
534 return reinterpret_cast<uintptr_t>(SymbolTable16);
536 return reinterpret_cast<uintptr_t>(SymbolTable32);
539 uint16_t getMachine() const {
541 return COFFHeader->Machine;
542 if (COFFBigObjHeader)
543 return COFFBigObjHeader->Machine;
544 llvm_unreachable("no COFF header!");
546 uint16_t getSizeOfOptionalHeader() const {
548 return COFFHeader->isImportLibrary() ? 0
549 : COFFHeader->SizeOfOptionalHeader;
550 // bigobj doesn't have this field.
551 if (COFFBigObjHeader)
553 llvm_unreachable("no COFF header!");
555 uint16_t getCharacteristics() const {
557 return COFFHeader->isImportLibrary() ? 0 : COFFHeader->Characteristics;
558 // bigobj doesn't have characteristics to speak of,
559 // editbin will silently lie to you if you attempt to set any.
560 if (COFFBigObjHeader)
562 llvm_unreachable("no COFF header!");
564 uint32_t getTimeDateStamp() const {
566 return COFFHeader->TimeDateStamp;
567 if (COFFBigObjHeader)
568 return COFFBigObjHeader->TimeDateStamp;
569 llvm_unreachable("no COFF header!");
571 uint32_t getNumberOfSections() const {
573 return COFFHeader->isImportLibrary() ? 0 : COFFHeader->NumberOfSections;
574 if (COFFBigObjHeader)
575 return COFFBigObjHeader->NumberOfSections;
576 llvm_unreachable("no COFF header!");
578 uint32_t getPointerToSymbolTable() const {
580 return COFFHeader->isImportLibrary() ? 0
581 : COFFHeader->PointerToSymbolTable;
582 if (COFFBigObjHeader)
583 return COFFBigObjHeader->PointerToSymbolTable;
584 llvm_unreachable("no COFF header!");
586 uint32_t getNumberOfSymbols() const {
588 return COFFHeader->isImportLibrary() ? 0 : COFFHeader->NumberOfSymbols;
589 if (COFFBigObjHeader)
590 return COFFBigObjHeader->NumberOfSymbols;
591 llvm_unreachable("no COFF header!");
594 void moveSymbolNext(DataRefImpl &Symb) const override;
595 std::error_code getSymbolName(DataRefImpl Symb,
596 StringRef &Res) const override;
597 std::error_code getSymbolAddress(DataRefImpl Symb,
598 uint64_t &Res) const override;
599 std::error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
600 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
601 std::error_code getSymbolType(DataRefImpl Symb,
602 SymbolRef::Type &Res) const override;
603 std::error_code getSymbolSection(DataRefImpl Symb,
604 section_iterator &Res) const override;
605 void moveSectionNext(DataRefImpl &Sec) const override;
606 std::error_code getSectionName(DataRefImpl Sec,
607 StringRef &Res) const override;
608 uint64_t getSectionAddress(DataRefImpl Sec) const override;
609 uint64_t getSectionSize(DataRefImpl Sec) const override;
610 std::error_code getSectionContents(DataRefImpl Sec,
611 StringRef &Res) const override;
612 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
613 bool isSectionText(DataRefImpl Sec) const override;
614 bool isSectionData(DataRefImpl Sec) const override;
615 bool isSectionBSS(DataRefImpl Sec) const override;
616 bool isSectionVirtual(DataRefImpl Sec) const override;
617 bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
618 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
619 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
621 void moveRelocationNext(DataRefImpl &Rel) const override;
622 std::error_code getRelocationAddress(DataRefImpl Rel,
623 uint64_t &Res) const override;
624 std::error_code getRelocationOffset(DataRefImpl Rel,
625 uint64_t &Res) const override;
626 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
627 section_iterator getRelocationSection(DataRefImpl Rel) const override;
628 std::error_code getRelocationType(DataRefImpl Rel,
629 uint64_t &Res) const override;
631 getRelocationTypeName(DataRefImpl Rel,
632 SmallVectorImpl<char> &Result) const override;
634 getRelocationValueString(DataRefImpl Rel,
635 SmallVectorImpl<char> &Result) const override;
638 COFFObjectFile(MemoryBufferRef Object, std::error_code &EC);
639 basic_symbol_iterator symbol_begin_impl() const override;
640 basic_symbol_iterator symbol_end_impl() const override;
641 section_iterator section_begin() const override;
642 section_iterator section_end() const override;
644 const coff_section *getCOFFSection(const SectionRef &Section) const;
645 COFFSymbolRef getCOFFSymbol(const DataRefImpl &Ref) const;
646 COFFSymbolRef getCOFFSymbol(const SymbolRef &Symbol) const;
647 const coff_relocation *getCOFFRelocation(const RelocationRef &Reloc) const;
649 uint8_t getBytesInAddress() const override;
650 StringRef getFileFormatName() const override;
651 unsigned getArch() const override;
653 import_directory_iterator import_directory_begin() const;
654 import_directory_iterator import_directory_end() const;
655 delay_import_directory_iterator delay_import_directory_begin() const;
656 delay_import_directory_iterator delay_import_directory_end() const;
657 export_directory_iterator export_directory_begin() const;
658 export_directory_iterator export_directory_end() const;
659 base_reloc_iterator base_reloc_begin() const;
660 base_reloc_iterator base_reloc_end() const;
662 iterator_range<import_directory_iterator> import_directories() const;
663 iterator_range<delay_import_directory_iterator>
664 delay_import_directories() const;
665 iterator_range<export_directory_iterator> export_directories() const;
666 iterator_range<base_reloc_iterator> base_relocs() const;
668 const dos_header *getDOSHeader() const {
669 if (!PE32Header && !PE32PlusHeader)
671 return reinterpret_cast<const dos_header *>(base());
673 std::error_code getPE32Header(const pe32_header *&Res) const;
674 std::error_code getPE32PlusHeader(const pe32plus_header *&Res) const;
675 std::error_code getDataDirectory(uint32_t index,
676 const data_directory *&Res) const;
677 std::error_code getSection(int32_t index, const coff_section *&Res) const;
678 template <typename coff_symbol_type>
679 std::error_code getSymbol(uint32_t Index,
680 const coff_symbol_type *&Res) const {
681 if (Index >= getNumberOfSymbols())
682 return object_error::parse_failed;
684 Res = reinterpret_cast<coff_symbol_type *>(getSymbolTable()) + Index;
685 return object_error::success;
687 ErrorOr<COFFSymbolRef> getSymbol(uint32_t index) const {
689 const coff_symbol16 *Symb = nullptr;
690 if (std::error_code EC = getSymbol(index, Symb))
692 return COFFSymbolRef(Symb);
695 const coff_symbol32 *Symb = nullptr;
696 if (std::error_code EC = getSymbol(index, Symb))
698 return COFFSymbolRef(Symb);
700 return object_error::parse_failed;
702 template <typename T>
703 std::error_code getAuxSymbol(uint32_t index, const T *&Res) const {
704 ErrorOr<COFFSymbolRef> s = getSymbol(index);
705 if (std::error_code EC = s.getError())
707 Res = reinterpret_cast<const T *>(s->getRawPtr());
708 return object_error::success;
710 std::error_code getSymbolName(COFFSymbolRef Symbol, StringRef &Res) const;
712 ArrayRef<uint8_t> getSymbolAuxData(COFFSymbolRef Symbol) const;
714 size_t getSymbolTableEntrySize() const {
716 return sizeof(coff_symbol16);
717 if (COFFBigObjHeader)
718 return sizeof(coff_symbol32);
719 llvm_unreachable("null symbol table pointer!");
722 std::error_code getSectionName(const coff_section *Sec, StringRef &Res) const;
723 uint64_t getSectionSize(const coff_section *Sec) const;
724 std::error_code getSectionContents(const coff_section *Sec,
725 ArrayRef<uint8_t> &Res) const;
727 std::error_code getVaPtr(uint64_t VA, uintptr_t &Res) const;
728 std::error_code getRvaPtr(uint32_t Rva, uintptr_t &Res) const;
729 std::error_code getHintName(uint32_t Rva, uint16_t &Hint,
730 StringRef &Name) const;
732 bool isRelocatableObject() const override;
733 bool is64() const { return PE32PlusHeader; }
735 static inline bool classof(const Binary *v) { return v->isCOFF(); }
738 // The iterator for the import directory table.
739 class ImportDirectoryEntryRef {
741 ImportDirectoryEntryRef() : OwningObject(nullptr) {}
742 ImportDirectoryEntryRef(const import_directory_table_entry *Table, uint32_t I,
743 const COFFObjectFile *Owner)
744 : ImportTable(Table), Index(I), OwningObject(Owner) {}
746 bool operator==(const ImportDirectoryEntryRef &Other) const;
749 imported_symbol_iterator imported_symbol_begin() const;
750 imported_symbol_iterator imported_symbol_end() const;
751 iterator_range<imported_symbol_iterator> imported_symbols() const;
753 std::error_code getName(StringRef &Result) const;
754 std::error_code getImportLookupTableRVA(uint32_t &Result) const;
755 std::error_code getImportAddressTableRVA(uint32_t &Result) const;
758 getImportTableEntry(const import_directory_table_entry *&Result) const;
761 getImportLookupEntry(const import_lookup_table_entry32 *&Result) const;
764 const import_directory_table_entry *ImportTable;
766 const COFFObjectFile *OwningObject;
769 class DelayImportDirectoryEntryRef {
771 DelayImportDirectoryEntryRef() : OwningObject(nullptr) {}
772 DelayImportDirectoryEntryRef(const delay_import_directory_table_entry *T,
773 uint32_t I, const COFFObjectFile *Owner)
774 : Table(T), Index(I), OwningObject(Owner) {}
776 bool operator==(const DelayImportDirectoryEntryRef &Other) const;
779 imported_symbol_iterator imported_symbol_begin() const;
780 imported_symbol_iterator imported_symbol_end() const;
781 iterator_range<imported_symbol_iterator> imported_symbols() const;
783 std::error_code getName(StringRef &Result) const;
784 std::error_code getDelayImportTable(
785 const delay_import_directory_table_entry *&Result) const;
786 std::error_code getImportAddress(int AddrIndex, uint64_t &Result) const;
789 const delay_import_directory_table_entry *Table;
791 const COFFObjectFile *OwningObject;
794 // The iterator for the export directory table entry.
795 class ExportDirectoryEntryRef {
797 ExportDirectoryEntryRef() : OwningObject(nullptr) {}
798 ExportDirectoryEntryRef(const export_directory_table_entry *Table, uint32_t I,
799 const COFFObjectFile *Owner)
800 : ExportTable(Table), Index(I), OwningObject(Owner) {}
802 bool operator==(const ExportDirectoryEntryRef &Other) const;
805 std::error_code getDllName(StringRef &Result) const;
806 std::error_code getOrdinalBase(uint32_t &Result) const;
807 std::error_code getOrdinal(uint32_t &Result) const;
808 std::error_code getExportRVA(uint32_t &Result) const;
809 std::error_code getSymbolName(StringRef &Result) const;
812 const export_directory_table_entry *ExportTable;
814 const COFFObjectFile *OwningObject;
817 class ImportedSymbolRef {
819 ImportedSymbolRef() : OwningObject(nullptr) {}
820 ImportedSymbolRef(const import_lookup_table_entry32 *Entry, uint32_t I,
821 const COFFObjectFile *Owner)
822 : Entry32(Entry), Entry64(nullptr), Index(I), OwningObject(Owner) {}
823 ImportedSymbolRef(const import_lookup_table_entry64 *Entry, uint32_t I,
824 const COFFObjectFile *Owner)
825 : Entry32(nullptr), Entry64(Entry), Index(I), OwningObject(Owner) {}
827 bool operator==(const ImportedSymbolRef &Other) const;
830 std::error_code getSymbolName(StringRef &Result) const;
831 std::error_code getOrdinal(uint16_t &Result) const;
834 const import_lookup_table_entry32 *Entry32;
835 const import_lookup_table_entry64 *Entry64;
837 const COFFObjectFile *OwningObject;
842 BaseRelocRef() : OwningObject(nullptr) {}
843 BaseRelocRef(const coff_base_reloc_block_header *Header,
844 const COFFObjectFile *Owner)
845 : Header(Header), Index(0), OwningObject(Owner) {}
847 bool operator==(const BaseRelocRef &Other) const;
850 std::error_code getType(uint8_t &Type) const;
851 std::error_code getRVA(uint32_t &Result) const;
854 const coff_base_reloc_block_header *Header;
856 const COFFObjectFile *OwningObject;
859 } // end namespace object
860 } // end namespace llvm