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;
31 typedef content_iterator<ImportDirectoryEntryRef> import_directory_iterator;
32 typedef content_iterator<DelayImportDirectoryEntryRef>
33 delay_import_directory_iterator;
34 typedef content_iterator<ExportDirectoryEntryRef> export_directory_iterator;
35 typedef content_iterator<ImportedSymbolRef> imported_symbol_iterator;
37 /// The DOS compatible header at the front of all PE/COFF executables.
40 support::ulittle16_t UsedBytesInTheLastPage;
41 support::ulittle16_t FileSizeInPages;
42 support::ulittle16_t NumberOfRelocationItems;
43 support::ulittle16_t HeaderSizeInParagraphs;
44 support::ulittle16_t MinimumExtraParagraphs;
45 support::ulittle16_t MaximumExtraParagraphs;
46 support::ulittle16_t InitialRelativeSS;
47 support::ulittle16_t InitialSP;
48 support::ulittle16_t Checksum;
49 support::ulittle16_t InitialIP;
50 support::ulittle16_t InitialRelativeCS;
51 support::ulittle16_t AddressOfRelocationTable;
52 support::ulittle16_t OverlayNumber;
53 support::ulittle16_t Reserved[4];
54 support::ulittle16_t OEMid;
55 support::ulittle16_t OEMinfo;
56 support::ulittle16_t Reserved2[10];
57 support::ulittle32_t AddressOfNewExeHeader;
60 struct coff_file_header {
61 support::ulittle16_t Machine;
62 support::ulittle16_t NumberOfSections;
63 support::ulittle32_t TimeDateStamp;
64 support::ulittle32_t PointerToSymbolTable;
65 support::ulittle32_t NumberOfSymbols;
66 support::ulittle16_t SizeOfOptionalHeader;
67 support::ulittle16_t Characteristics;
69 bool isImportLibrary() const { return NumberOfSections == 0xffff; }
72 struct coff_bigobj_file_header {
73 support::ulittle16_t Sig1;
74 support::ulittle16_t Sig2;
75 support::ulittle16_t Version;
76 support::ulittle16_t Machine;
77 support::ulittle32_t TimeDateStamp;
79 support::ulittle32_t unused1;
80 support::ulittle32_t unused2;
81 support::ulittle32_t unused3;
82 support::ulittle32_t unused4;
83 support::ulittle32_t NumberOfSections;
84 support::ulittle32_t PointerToSymbolTable;
85 support::ulittle32_t NumberOfSymbols;
88 /// The 32-bit PE header that follows the COFF header.
90 support::ulittle16_t Magic;
91 uint8_t MajorLinkerVersion;
92 uint8_t MinorLinkerVersion;
93 support::ulittle32_t SizeOfCode;
94 support::ulittle32_t SizeOfInitializedData;
95 support::ulittle32_t SizeOfUninitializedData;
96 support::ulittle32_t AddressOfEntryPoint;
97 support::ulittle32_t BaseOfCode;
98 support::ulittle32_t BaseOfData;
99 support::ulittle32_t ImageBase;
100 support::ulittle32_t SectionAlignment;
101 support::ulittle32_t FileAlignment;
102 support::ulittle16_t MajorOperatingSystemVersion;
103 support::ulittle16_t MinorOperatingSystemVersion;
104 support::ulittle16_t MajorImageVersion;
105 support::ulittle16_t MinorImageVersion;
106 support::ulittle16_t MajorSubsystemVersion;
107 support::ulittle16_t MinorSubsystemVersion;
108 support::ulittle32_t Win32VersionValue;
109 support::ulittle32_t SizeOfImage;
110 support::ulittle32_t SizeOfHeaders;
111 support::ulittle32_t CheckSum;
112 support::ulittle16_t Subsystem;
113 support::ulittle16_t DLLCharacteristics;
114 support::ulittle32_t SizeOfStackReserve;
115 support::ulittle32_t SizeOfStackCommit;
116 support::ulittle32_t SizeOfHeapReserve;
117 support::ulittle32_t SizeOfHeapCommit;
118 support::ulittle32_t LoaderFlags;
119 support::ulittle32_t NumberOfRvaAndSize;
122 /// The 64-bit PE header that follows the COFF header.
123 struct pe32plus_header {
124 support::ulittle16_t Magic;
125 uint8_t MajorLinkerVersion;
126 uint8_t MinorLinkerVersion;
127 support::ulittle32_t SizeOfCode;
128 support::ulittle32_t SizeOfInitializedData;
129 support::ulittle32_t SizeOfUninitializedData;
130 support::ulittle32_t AddressOfEntryPoint;
131 support::ulittle32_t BaseOfCode;
132 support::ulittle64_t ImageBase;
133 support::ulittle32_t SectionAlignment;
134 support::ulittle32_t FileAlignment;
135 support::ulittle16_t MajorOperatingSystemVersion;
136 support::ulittle16_t MinorOperatingSystemVersion;
137 support::ulittle16_t MajorImageVersion;
138 support::ulittle16_t MinorImageVersion;
139 support::ulittle16_t MajorSubsystemVersion;
140 support::ulittle16_t MinorSubsystemVersion;
141 support::ulittle32_t Win32VersionValue;
142 support::ulittle32_t SizeOfImage;
143 support::ulittle32_t SizeOfHeaders;
144 support::ulittle32_t CheckSum;
145 support::ulittle16_t Subsystem;
146 support::ulittle16_t DLLCharacteristics;
147 support::ulittle64_t SizeOfStackReserve;
148 support::ulittle64_t SizeOfStackCommit;
149 support::ulittle64_t SizeOfHeapReserve;
150 support::ulittle64_t SizeOfHeapCommit;
151 support::ulittle32_t LoaderFlags;
152 support::ulittle32_t NumberOfRvaAndSize;
155 struct data_directory {
156 support::ulittle32_t RelativeVirtualAddress;
157 support::ulittle32_t Size;
160 struct import_directory_table_entry {
161 support::ulittle32_t ImportLookupTableRVA;
162 support::ulittle32_t TimeDateStamp;
163 support::ulittle32_t ForwarderChain;
164 support::ulittle32_t NameRVA;
165 support::ulittle32_t ImportAddressTableRVA;
168 template <typename IntTy>
169 struct import_lookup_table_entry {
172 bool isOrdinal() const { return Data < 0; }
174 uint16_t getOrdinal() const {
175 assert(isOrdinal() && "ILT entry is not an ordinal!");
176 return Data & 0xFFFF;
179 uint32_t getHintNameRVA() const {
180 assert(!isOrdinal() && "ILT entry is not a Hint/Name RVA!");
181 return Data & 0xFFFFFFFF;
185 typedef import_lookup_table_entry<support::little32_t>
186 import_lookup_table_entry32;
187 typedef import_lookup_table_entry<support::little64_t>
188 import_lookup_table_entry64;
190 struct delay_import_directory_table_entry {
191 // dumpbin reports this field as "Characteristics" instead of "Attributes".
192 support::ulittle32_t Attributes;
193 support::ulittle32_t Name;
194 support::ulittle32_t ModuleHandle;
195 support::ulittle32_t DelayImportAddressTable;
196 support::ulittle32_t DelayImportNameTable;
197 support::ulittle32_t BoundDelayImportTable;
198 support::ulittle32_t UnloadDelayImportTable;
199 support::ulittle32_t TimeStamp;
202 struct export_directory_table_entry {
203 support::ulittle32_t ExportFlags;
204 support::ulittle32_t TimeDateStamp;
205 support::ulittle16_t MajorVersion;
206 support::ulittle16_t MinorVersion;
207 support::ulittle32_t NameRVA;
208 support::ulittle32_t OrdinalBase;
209 support::ulittle32_t AddressTableEntries;
210 support::ulittle32_t NumberOfNamePointers;
211 support::ulittle32_t ExportAddressTableRVA;
212 support::ulittle32_t NamePointerRVA;
213 support::ulittle32_t OrdinalTableRVA;
216 union export_address_table_entry {
217 support::ulittle32_t ExportRVA;
218 support::ulittle32_t ForwarderRVA;
221 typedef support::ulittle32_t export_name_pointer_table_entry;
222 typedef support::ulittle16_t export_ordinal_table_entry;
224 struct StringTableOffset {
225 support::ulittle32_t Zeroes;
226 support::ulittle32_t Offset;
229 template <typename SectionNumberType>
232 char ShortName[COFF::NameSize];
233 StringTableOffset Offset;
236 support::ulittle32_t Value;
237 SectionNumberType SectionNumber;
239 support::ulittle16_t Type;
241 uint8_t StorageClass;
242 uint8_t NumberOfAuxSymbols;
245 typedef coff_symbol<support::ulittle16_t> coff_symbol16;
246 typedef coff_symbol<support::ulittle32_t> coff_symbol32;
248 class COFFSymbolRef {
250 COFFSymbolRef(const coff_symbol16 *CS) : CS16(CS), CS32(nullptr) {}
251 COFFSymbolRef(const coff_symbol32 *CS) : CS16(nullptr), CS32(CS) {}
252 COFFSymbolRef() : CS16(nullptr), CS32(nullptr) {}
254 const void *getRawPtr() const {
255 return CS16 ? static_cast<const void *>(CS16) : CS32;
258 friend bool operator<(COFFSymbolRef A, COFFSymbolRef B) {
259 return A.getRawPtr() < B.getRawPtr();
262 bool isBigObj() const {
267 llvm_unreachable("COFFSymbolRef points to nothing!");
270 const char *getShortName() const {
271 return CS16 ? CS16->Name.ShortName : CS32->Name.ShortName;
274 const StringTableOffset &getStringTableOffset() const {
275 return CS16 ? CS16->Name.Offset : CS32->Name.Offset;
278 uint32_t getValue() const { return CS16 ? CS16->Value : CS32->Value; }
280 int32_t getSectionNumber() const {
282 // Reserved sections are returned as negative numbers.
283 if (CS16->SectionNumber <= COFF::MaxNumberOfSections16)
284 return CS16->SectionNumber;
285 return static_cast<int16_t>(CS16->SectionNumber);
287 return static_cast<int32_t>(CS32->SectionNumber);
290 uint16_t getType() const { return CS16 ? CS16->Type : CS32->Type; }
292 uint8_t getStorageClass() const {
293 return CS16 ? CS16->StorageClass : CS32->StorageClass;
296 uint8_t getNumberOfAuxSymbols() const {
297 return CS16 ? CS16->NumberOfAuxSymbols : CS32->NumberOfAuxSymbols;
300 uint8_t getBaseType() const { return getType() & 0x0F; }
302 uint8_t getComplexType() const {
303 return (getType() & 0xF0) >> COFF::SCT_COMPLEX_TYPE_SHIFT;
306 bool isExternal() const {
307 return getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL;
310 bool isCommon() const {
311 return isExternal() && getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED &&
315 bool isUndefined() const {
316 return isExternal() && getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED &&
320 bool isWeakExternal() const {
321 return getStorageClass() == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
324 bool isFunctionDefinition() const {
325 return isExternal() && getBaseType() == COFF::IMAGE_SYM_TYPE_NULL &&
326 getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION &&
327 !COFF::isReservedSectionNumber(getSectionNumber());
330 bool isFunctionLineInfo() const {
331 return getStorageClass() == COFF::IMAGE_SYM_CLASS_FUNCTION;
334 bool isAnyUndefined() const {
335 return isUndefined() || isWeakExternal();
338 bool isFileRecord() const {
339 return getStorageClass() == COFF::IMAGE_SYM_CLASS_FILE;
342 bool isSectionDefinition() const {
343 // C++/CLI creates external ABS symbols for non-const appdomain globals.
344 // These are also followed by an auxiliary section definition.
345 bool isAppdomainGlobal =
346 getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
347 getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE;
348 bool isOrdinarySection = getStorageClass() == COFF::IMAGE_SYM_CLASS_STATIC;
349 if (!getNumberOfAuxSymbols())
351 return isAppdomainGlobal || isOrdinarySection;
354 bool isCLRToken() const {
355 return getStorageClass() == COFF::IMAGE_SYM_CLASS_CLR_TOKEN;
359 const coff_symbol16 *CS16;
360 const coff_symbol32 *CS32;
363 struct coff_section {
364 char Name[COFF::NameSize];
365 support::ulittle32_t VirtualSize;
366 support::ulittle32_t VirtualAddress;
367 support::ulittle32_t SizeOfRawData;
368 support::ulittle32_t PointerToRawData;
369 support::ulittle32_t PointerToRelocations;
370 support::ulittle32_t PointerToLinenumbers;
371 support::ulittle16_t NumberOfRelocations;
372 support::ulittle16_t NumberOfLinenumbers;
373 support::ulittle32_t Characteristics;
375 // Returns true if the actual number of relocations is stored in
376 // VirtualAddress field of the first relocation table entry.
377 bool hasExtendedRelocations() const {
378 return Characteristics & COFF::IMAGE_SCN_LNK_NRELOC_OVFL &&
379 NumberOfRelocations == UINT16_MAX;
383 struct coff_relocation {
384 support::ulittle32_t VirtualAddress;
385 support::ulittle32_t SymbolTableIndex;
386 support::ulittle16_t Type;
389 struct coff_aux_function_definition {
390 support::ulittle32_t TagIndex;
391 support::ulittle32_t TotalSize;
392 support::ulittle32_t PointerToLinenumber;
393 support::ulittle32_t PointerToNextFunction;
396 struct coff_aux_bf_and_ef_symbol {
398 support::ulittle16_t Linenumber;
400 support::ulittle32_t PointerToNextFunction;
403 struct coff_aux_weak_external {
404 support::ulittle32_t TagIndex;
405 support::ulittle32_t Characteristics;
408 struct coff_aux_section_definition {
409 support::ulittle32_t Length;
410 support::ulittle16_t NumberOfRelocations;
411 support::ulittle16_t NumberOfLinenumbers;
412 support::ulittle32_t CheckSum;
413 support::ulittle16_t NumberLowPart;
416 support::ulittle16_t NumberHighPart;
417 int32_t getNumber(bool IsBigObj) const {
418 uint32_t Number = static_cast<uint32_t>(NumberLowPart);
420 Number |= static_cast<uint32_t>(NumberHighPart) << 16;
421 return static_cast<int32_t>(Number);
425 struct coff_aux_clr_token {
428 support::ulittle32_t SymbolTableIndex;
431 struct coff_load_configuration32 {
432 support::ulittle32_t Characteristics;
433 support::ulittle32_t TimeDateStamp;
434 support::ulittle16_t MajorVersion;
435 support::ulittle16_t MinorVersion;
436 support::ulittle32_t GlobalFlagsClear;
437 support::ulittle32_t GlobalFlagsSet;
438 support::ulittle32_t CriticalSectionDefaultTimeout;
439 support::ulittle32_t DeCommitFreeBlockThreshold;
440 support::ulittle32_t DeCommitTotalFreeThreshold;
441 support::ulittle32_t LockPrefixTable;
442 support::ulittle32_t MaximumAllocationSize;
443 support::ulittle32_t VirtualMemoryThreshold;
444 support::ulittle32_t ProcessAffinityMask;
445 support::ulittle32_t ProcessHeapFlags;
446 support::ulittle16_t CSDVersion;
447 support::ulittle16_t Reserved;
448 support::ulittle32_t EditList;
449 support::ulittle32_t SecurityCookie;
450 support::ulittle32_t SEHandlerTable;
451 support::ulittle32_t SEHandlerCount;
454 struct coff_runtime_function_x64 {
455 support::ulittle32_t BeginAddress;
456 support::ulittle32_t EndAddress;
457 support::ulittle32_t UnwindInformation;
460 class COFFObjectFile : public ObjectFile {
462 friend class ImportDirectoryEntryRef;
463 friend class ExportDirectoryEntryRef;
464 const coff_file_header *COFFHeader;
465 const coff_bigobj_file_header *COFFBigObjHeader;
466 const pe32_header *PE32Header;
467 const pe32plus_header *PE32PlusHeader;
468 const data_directory *DataDirectory;
469 const coff_section *SectionTable;
470 const coff_symbol16 *SymbolTable16;
471 const coff_symbol32 *SymbolTable32;
472 const char *StringTable;
473 uint32_t StringTableSize;
474 const import_directory_table_entry *ImportDirectory;
475 uint32_t NumberOfImportDirectory;
476 const delay_import_directory_table_entry *DelayImportDirectory;
477 uint32_t NumberOfDelayImportDirectory;
478 const export_directory_table_entry *ExportDirectory;
480 std::error_code getString(uint32_t offset, StringRef &Res) const;
482 template <typename coff_symbol_type>
483 const coff_symbol_type *toSymb(DataRefImpl Symb) const;
484 const coff_section *toSec(DataRefImpl Sec) const;
485 const coff_relocation *toRel(DataRefImpl Rel) const;
487 std::error_code initSymbolTablePtr();
488 std::error_code initImportTablePtr();
489 std::error_code initDelayImportTablePtr();
490 std::error_code initExportTablePtr();
493 uintptr_t getSymbolTable() const {
495 return reinterpret_cast<uintptr_t>(SymbolTable16);
497 return reinterpret_cast<uintptr_t>(SymbolTable32);
500 uint16_t getMachine() const {
502 return COFFHeader->Machine;
503 if (COFFBigObjHeader)
504 return COFFBigObjHeader->Machine;
505 llvm_unreachable("no COFF header!");
507 uint16_t getSizeOfOptionalHeader() const {
509 return COFFHeader->SizeOfOptionalHeader;
510 // bigobj doesn't have this field.
511 if (COFFBigObjHeader)
513 llvm_unreachable("no COFF header!");
515 uint16_t getCharacteristics() const {
517 return COFFHeader->Characteristics;
518 // bigobj doesn't have characteristics to speak of,
519 // editbin will silently lie to you if you attempt to set any.
520 if (COFFBigObjHeader)
522 llvm_unreachable("no COFF header!");
524 uint32_t getTimeDateStamp() const {
526 return COFFHeader->TimeDateStamp;
527 if (COFFBigObjHeader)
528 return COFFBigObjHeader->TimeDateStamp;
529 llvm_unreachable("no COFF header!");
531 uint32_t getNumberOfSections() const {
533 return COFFHeader->NumberOfSections;
534 if (COFFBigObjHeader)
535 return COFFBigObjHeader->NumberOfSections;
536 llvm_unreachable("no COFF header!");
538 uint32_t getPointerToSymbolTable() const {
540 return COFFHeader->PointerToSymbolTable;
541 if (COFFBigObjHeader)
542 return COFFBigObjHeader->PointerToSymbolTable;
543 llvm_unreachable("no COFF header!");
545 uint32_t getNumberOfSymbols() const {
547 return COFFHeader->NumberOfSymbols;
548 if (COFFBigObjHeader)
549 return COFFBigObjHeader->NumberOfSymbols;
550 llvm_unreachable("no COFF header!");
553 void moveSymbolNext(DataRefImpl &Symb) const override;
554 std::error_code getSymbolName(DataRefImpl Symb,
555 StringRef &Res) const override;
556 std::error_code getSymbolAddress(DataRefImpl Symb,
557 uint64_t &Res) const override;
558 std::error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
559 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
560 std::error_code getSymbolType(DataRefImpl Symb,
561 SymbolRef::Type &Res) const override;
562 std::error_code getSymbolSection(DataRefImpl Symb,
563 section_iterator &Res) const override;
564 void moveSectionNext(DataRefImpl &Sec) const override;
565 std::error_code getSectionName(DataRefImpl Sec,
566 StringRef &Res) const override;
567 uint64_t getSectionAddress(DataRefImpl Sec) const override;
568 uint64_t getSectionSize(DataRefImpl Sec) const override;
569 std::error_code getSectionContents(DataRefImpl Sec,
570 StringRef &Res) const override;
571 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
572 bool isSectionText(DataRefImpl Sec) const override;
573 bool isSectionData(DataRefImpl Sec) const override;
574 bool isSectionBSS(DataRefImpl Sec) const override;
575 bool isSectionVirtual(DataRefImpl Sec) const override;
576 bool isSectionZeroInit(DataRefImpl Sec) const override;
577 bool isSectionReadOnlyData(DataRefImpl Sec) const override;
578 bool isSectionRequiredForExecution(DataRefImpl Sec) const override;
579 bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
580 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
581 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
583 void moveRelocationNext(DataRefImpl &Rel) const override;
584 std::error_code getRelocationAddress(DataRefImpl Rel,
585 uint64_t &Res) const override;
586 std::error_code getRelocationOffset(DataRefImpl Rel,
587 uint64_t &Res) const override;
588 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
589 std::error_code getRelocationType(DataRefImpl Rel,
590 uint64_t &Res) const override;
592 getRelocationTypeName(DataRefImpl Rel,
593 SmallVectorImpl<char> &Result) const override;
595 getRelocationValueString(DataRefImpl Rel,
596 SmallVectorImpl<char> &Result) const override;
599 COFFObjectFile(MemoryBufferRef Object, std::error_code &EC);
600 basic_symbol_iterator symbol_begin_impl() const override;
601 basic_symbol_iterator symbol_end_impl() const override;
602 section_iterator section_begin() const override;
603 section_iterator section_end() const override;
605 const coff_section *getCOFFSection(const SectionRef &Section) const;
606 COFFSymbolRef getCOFFSymbol(const DataRefImpl &Ref) const;
607 COFFSymbolRef getCOFFSymbol(const SymbolRef &Symbol) const;
608 const coff_relocation *getCOFFRelocation(const RelocationRef &Reloc) const;
610 uint8_t getBytesInAddress() const override;
611 StringRef getFileFormatName() const override;
612 unsigned getArch() const override;
614 import_directory_iterator import_directory_begin() const;
615 import_directory_iterator import_directory_end() const;
616 delay_import_directory_iterator delay_import_directory_begin() const;
617 delay_import_directory_iterator delay_import_directory_end() const;
618 export_directory_iterator export_directory_begin() const;
619 export_directory_iterator export_directory_end() const;
621 iterator_range<import_directory_iterator> import_directories() const;
622 iterator_range<delay_import_directory_iterator>
623 delay_import_directories() const;
624 iterator_range<export_directory_iterator> export_directories() const;
626 const dos_header *getDOSHeader() const {
627 if (!PE32Header && !PE32PlusHeader)
629 return reinterpret_cast<const dos_header *>(base());
631 std::error_code getPE32Header(const pe32_header *&Res) const;
632 std::error_code getPE32PlusHeader(const pe32plus_header *&Res) const;
633 std::error_code getDataDirectory(uint32_t index,
634 const data_directory *&Res) const;
635 std::error_code getSection(int32_t index, const coff_section *&Res) const;
636 template <typename coff_symbol_type>
637 std::error_code getSymbol(uint32_t Index,
638 const coff_symbol_type *&Res) const {
639 if (Index >= getNumberOfSymbols())
640 return object_error::parse_failed;
642 Res = reinterpret_cast<coff_symbol_type *>(getSymbolTable()) + Index;
643 return object_error::success;
645 ErrorOr<COFFSymbolRef> getSymbol(uint32_t index) const {
647 const coff_symbol16 *Symb = nullptr;
648 if (std::error_code EC = getSymbol(index, Symb))
650 return COFFSymbolRef(Symb);
653 const coff_symbol32 *Symb = nullptr;
654 if (std::error_code EC = getSymbol(index, Symb))
656 return COFFSymbolRef(Symb);
658 llvm_unreachable("no symbol table pointer!");
660 template <typename T>
661 std::error_code getAuxSymbol(uint32_t index, const T *&Res) const {
662 ErrorOr<COFFSymbolRef> s = getSymbol(index);
663 if (std::error_code EC = s.getError())
665 Res = reinterpret_cast<const T *>(s->getRawPtr());
666 return object_error::success;
668 std::error_code getSymbolName(COFFSymbolRef Symbol, StringRef &Res) const;
670 ArrayRef<uint8_t> getSymbolAuxData(COFFSymbolRef Symbol) const;
672 size_t getSymbolTableEntrySize() const {
674 return sizeof(coff_symbol16);
675 if (COFFBigObjHeader)
676 return sizeof(coff_symbol32);
677 llvm_unreachable("null symbol table pointer!");
680 std::error_code getSectionName(const coff_section *Sec, StringRef &Res) const;
681 uint64_t getSectionSize(const coff_section *Sec) const;
682 std::error_code getSectionContents(const coff_section *Sec,
683 ArrayRef<uint8_t> &Res) const;
685 std::error_code getVaPtr(uint64_t VA, uintptr_t &Res) const;
686 std::error_code getRvaPtr(uint32_t Rva, uintptr_t &Res) const;
687 std::error_code getHintName(uint32_t Rva, uint16_t &Hint,
688 StringRef &Name) const;
690 bool isRelocatableObject() const override;
691 bool is64() const { return PE32PlusHeader; }
693 static inline bool classof(const Binary *v) { return v->isCOFF(); }
696 // The iterator for the import directory table.
697 class ImportDirectoryEntryRef {
699 ImportDirectoryEntryRef() : OwningObject(nullptr) {}
700 ImportDirectoryEntryRef(const import_directory_table_entry *Table, uint32_t I,
701 const COFFObjectFile *Owner)
702 : ImportTable(Table), Index(I), OwningObject(Owner) {}
704 bool operator==(const ImportDirectoryEntryRef &Other) const;
707 imported_symbol_iterator imported_symbol_begin() const;
708 imported_symbol_iterator imported_symbol_end() const;
709 iterator_range<imported_symbol_iterator> imported_symbols() const;
711 std::error_code getName(StringRef &Result) const;
712 std::error_code getImportLookupTableRVA(uint32_t &Result) const;
713 std::error_code getImportAddressTableRVA(uint32_t &Result) const;
716 getImportTableEntry(const import_directory_table_entry *&Result) const;
719 getImportLookupEntry(const import_lookup_table_entry32 *&Result) const;
722 const import_directory_table_entry *ImportTable;
724 const COFFObjectFile *OwningObject;
727 class DelayImportDirectoryEntryRef {
729 DelayImportDirectoryEntryRef() : OwningObject(nullptr) {}
730 DelayImportDirectoryEntryRef(const delay_import_directory_table_entry *T,
731 uint32_t I, const COFFObjectFile *Owner)
732 : Table(T), Index(I), OwningObject(Owner) {}
734 bool operator==(const DelayImportDirectoryEntryRef &Other) const;
737 imported_symbol_iterator imported_symbol_begin() const;
738 imported_symbol_iterator imported_symbol_end() const;
739 iterator_range<imported_symbol_iterator> imported_symbols() const;
741 std::error_code getName(StringRef &Result) const;
742 std::error_code getDelayImportTable(
743 const delay_import_directory_table_entry *&Result) const;
744 std::error_code getImportAddress(int AddrIndex, uint64_t &Result) const;
747 const delay_import_directory_table_entry *Table;
749 const COFFObjectFile *OwningObject;
752 // The iterator for the export directory table entry.
753 class ExportDirectoryEntryRef {
755 ExportDirectoryEntryRef() : OwningObject(nullptr) {}
756 ExportDirectoryEntryRef(const export_directory_table_entry *Table, uint32_t I,
757 const COFFObjectFile *Owner)
758 : ExportTable(Table), Index(I), OwningObject(Owner) {}
760 bool operator==(const ExportDirectoryEntryRef &Other) const;
763 std::error_code getDllName(StringRef &Result) const;
764 std::error_code getOrdinalBase(uint32_t &Result) const;
765 std::error_code getOrdinal(uint32_t &Result) const;
766 std::error_code getExportRVA(uint32_t &Result) const;
767 std::error_code getSymbolName(StringRef &Result) const;
770 const export_directory_table_entry *ExportTable;
772 const COFFObjectFile *OwningObject;
775 class ImportedSymbolRef {
777 ImportedSymbolRef() : OwningObject(nullptr) {}
778 ImportedSymbolRef(const import_lookup_table_entry32 *Entry, uint32_t I,
779 const COFFObjectFile *Owner)
780 : Entry32(Entry), Entry64(nullptr), Index(I), OwningObject(Owner) {}
781 ImportedSymbolRef(const import_lookup_table_entry64 *Entry, uint32_t I,
782 const COFFObjectFile *Owner)
783 : Entry32(nullptr), Entry64(Entry), Index(I), OwningObject(Owner) {}
785 bool operator==(const ImportedSymbolRef &Other) const;
788 std::error_code getSymbolName(StringRef &Result) const;
789 std::error_code getOrdinal(uint16_t &Result) const;
792 const import_lookup_table_entry32 *Entry32;
793 const import_lookup_table_entry64 *Entry64;
795 const COFFObjectFile *OwningObject;
797 } // end namespace object
798 } // end namespace llvm