d9c0cd3870df7bafb3aa107f926e18543c6873d1
[oota-llvm.git] / include / llvm / Object / COFF.h
1 //===- COFF.h - COFF object file implementation -----------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the COFFObjectFile class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_OBJECT_COFF_H
15 #define LLVM_OBJECT_COFF_H
16
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"
22
23 namespace llvm {
24 template <typename T> class ArrayRef;
25
26 namespace object {
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;
36
37 /// The DOS compatible header at the front of all PE/COFF executables.
38 struct dos_header {
39   char                 Magic[2];
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;
58 };
59
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;
68
69   bool isImportLibrary() const { return NumberOfSections == 0xffff; }
70 };
71
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;
78   uint8_t              UUID[16];
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;
86 };
87
88 /// The 32-bit PE header that follows the COFF header.
89 struct pe32_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;
120 };
121
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;
153 };
154
155 struct data_directory {
156   support::ulittle32_t RelativeVirtualAddress;
157   support::ulittle32_t Size;
158 };
159
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;
166 };
167
168 template <typename IntTy>
169 struct import_lookup_table_entry {
170   IntTy Data;
171
172   bool isOrdinal() const { return Data < 0; }
173
174   uint16_t getOrdinal() const {
175     assert(isOrdinal() && "ILT entry is not an ordinal!");
176     return Data & 0xFFFF;
177   }
178
179   uint32_t getHintNameRVA() const {
180     assert(!isOrdinal() && "ILT entry is not a Hint/Name RVA!");
181     return Data & 0xFFFFFFFF;
182   }
183 };
184
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;
189
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;
200 };
201
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;
214 };
215
216 union export_address_table_entry {
217   support::ulittle32_t ExportRVA;
218   support::ulittle32_t ForwarderRVA;
219 };
220
221 typedef support::ulittle32_t export_name_pointer_table_entry;
222 typedef support::ulittle16_t export_ordinal_table_entry;
223
224 struct StringTableOffset {
225   support::ulittle32_t Zeroes;
226   support::ulittle32_t Offset;
227 };
228
229 template <typename SectionNumberType>
230 struct coff_symbol {
231   union {
232     char ShortName[COFF::NameSize];
233     StringTableOffset Offset;
234   } Name;
235
236   support::ulittle32_t Value;
237   SectionNumberType SectionNumber;
238
239   support::ulittle16_t Type;
240
241   uint8_t StorageClass;
242   uint8_t NumberOfAuxSymbols;
243 };
244
245 typedef coff_symbol<support::ulittle16_t> coff_symbol16;
246 typedef coff_symbol<support::ulittle32_t> coff_symbol32;
247
248 class COFFSymbolRef {
249 public:
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) {}
253
254   const void *getRawPtr() const {
255     return CS16 ? static_cast<const void *>(CS16) : CS32;
256   }
257
258   friend bool operator<(COFFSymbolRef A, COFFSymbolRef B) {
259     return A.getRawPtr() < B.getRawPtr();
260   }
261
262   bool isBigObj() const {
263     if (CS16)
264       return false;
265     if (CS32)
266       return true;
267     llvm_unreachable("COFFSymbolRef points to nothing!");
268   }
269
270   const char *getShortName() const {
271     return CS16 ? CS16->Name.ShortName : CS32->Name.ShortName;
272   }
273
274   const StringTableOffset &getStringTableOffset() const {
275     return CS16 ? CS16->Name.Offset : CS32->Name.Offset;
276   }
277
278   uint32_t getValue() const { return CS16 ? CS16->Value : CS32->Value; }
279
280   int32_t getSectionNumber() const {
281     if (CS16) {
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);
286     }
287     return static_cast<int32_t>(CS32->SectionNumber);
288   }
289
290   uint16_t getType() const { return CS16 ? CS16->Type : CS32->Type; }
291
292   uint8_t getStorageClass() const {
293     return CS16 ? CS16->StorageClass : CS32->StorageClass;
294   }
295
296   uint8_t getNumberOfAuxSymbols() const {
297     return CS16 ? CS16->NumberOfAuxSymbols : CS32->NumberOfAuxSymbols;
298   }
299
300   uint8_t getBaseType() const { return getType() & 0x0F; }
301
302   uint8_t getComplexType() const { return (getType() & 0xF0) >> 4; }
303
304   bool isExternal() const {
305     return getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL;
306   }
307
308   bool isCommon() const {
309     return isExternal() && getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED &&
310            getValue() != 0;
311   }
312
313   bool isUndefined() const {
314     return isExternal() && getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED &&
315            getValue() == 0;
316   }
317
318   bool isWeakExternal() const {
319     return getStorageClass() == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
320   }
321
322   bool isFunctionDefinition() const {
323     return isExternal() && getBaseType() == COFF::IMAGE_SYM_TYPE_NULL &&
324            getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION &&
325            !COFF::isReservedSectionNumber(getSectionNumber());
326   }
327
328   bool isFunctionLineInfo() const {
329     return getStorageClass() == COFF::IMAGE_SYM_CLASS_FUNCTION;
330   }
331
332   bool isAnyUndefined() const {
333     return isUndefined() || isWeakExternal();
334   }
335
336   bool isFileRecord() const {
337     return getStorageClass() == COFF::IMAGE_SYM_CLASS_FILE;
338   }
339
340   bool isSectionDefinition() const {
341     // C++/CLI creates external ABS symbols for non-const appdomain globals.
342     // These are also followed by an auxiliary section definition.
343     bool isAppdomainGlobal =
344         getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
345         getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE;
346     bool isOrdinarySection = getStorageClass() == COFF::IMAGE_SYM_CLASS_STATIC;
347     if (!getNumberOfAuxSymbols())
348       return false;
349     return isAppdomainGlobal || isOrdinarySection;
350   }
351
352   bool isCLRToken() const {
353     return getStorageClass() == COFF::IMAGE_SYM_CLASS_CLR_TOKEN;
354   }
355
356 private:
357   const coff_symbol16 *CS16;
358   const coff_symbol32 *CS32;
359 };
360
361 struct coff_section {
362   char Name[COFF::NameSize];
363   support::ulittle32_t VirtualSize;
364   support::ulittle32_t VirtualAddress;
365   support::ulittle32_t SizeOfRawData;
366   support::ulittle32_t PointerToRawData;
367   support::ulittle32_t PointerToRelocations;
368   support::ulittle32_t PointerToLinenumbers;
369   support::ulittle16_t NumberOfRelocations;
370   support::ulittle16_t NumberOfLinenumbers;
371   support::ulittle32_t Characteristics;
372
373   // Returns true if the actual number of relocations is stored in
374   // VirtualAddress field of the first relocation table entry.
375   bool hasExtendedRelocations() const {
376     return Characteristics & COFF::IMAGE_SCN_LNK_NRELOC_OVFL &&
377         NumberOfRelocations == UINT16_MAX;
378   };
379 };
380
381 struct coff_relocation {
382   support::ulittle32_t VirtualAddress;
383   support::ulittle32_t SymbolTableIndex;
384   support::ulittle16_t Type;
385 };
386
387 struct coff_aux_function_definition {
388   support::ulittle32_t TagIndex;
389   support::ulittle32_t TotalSize;
390   support::ulittle32_t PointerToLinenumber;
391   support::ulittle32_t PointerToNextFunction;
392 };
393
394 struct coff_aux_bf_and_ef_symbol {
395   char Unused1[4];
396   support::ulittle16_t Linenumber;
397   char Unused2[6];
398   support::ulittle32_t PointerToNextFunction;
399 };
400
401 struct coff_aux_weak_external {
402   support::ulittle32_t TagIndex;
403   support::ulittle32_t Characteristics;
404 };
405
406 struct coff_aux_section_definition {
407   support::ulittle32_t Length;
408   support::ulittle16_t NumberOfRelocations;
409   support::ulittle16_t NumberOfLinenumbers;
410   support::ulittle32_t CheckSum;
411   support::ulittle16_t NumberLowPart;
412   uint8_t              Selection;
413   uint8_t              Unused;
414   support::ulittle16_t NumberHighPart;
415   int32_t getNumber(bool IsBigObj) const {
416     uint32_t Number = static_cast<uint32_t>(NumberLowPart);
417     if (IsBigObj)
418       Number |= static_cast<uint32_t>(NumberHighPart) << 16;
419     return static_cast<int32_t>(Number);
420   }
421 };
422
423 struct coff_aux_clr_token {
424   uint8_t              AuxType;
425   uint8_t              Reserved;
426   support::ulittle32_t SymbolTableIndex;
427 };
428
429 struct coff_load_configuration32 {
430   support::ulittle32_t Characteristics;
431   support::ulittle32_t TimeDateStamp;
432   support::ulittle16_t MajorVersion;
433   support::ulittle16_t MinorVersion;
434   support::ulittle32_t GlobalFlagsClear;
435   support::ulittle32_t GlobalFlagsSet;
436   support::ulittle32_t CriticalSectionDefaultTimeout;
437   support::ulittle32_t DeCommitFreeBlockThreshold;
438   support::ulittle32_t DeCommitTotalFreeThreshold;
439   support::ulittle32_t LockPrefixTable;
440   support::ulittle32_t MaximumAllocationSize;
441   support::ulittle32_t VirtualMemoryThreshold;
442   support::ulittle32_t ProcessAffinityMask;
443   support::ulittle32_t ProcessHeapFlags;
444   support::ulittle16_t CSDVersion;
445   support::ulittle16_t Reserved;
446   support::ulittle32_t EditList;
447   support::ulittle32_t SecurityCookie;
448   support::ulittle32_t SEHandlerTable;
449   support::ulittle32_t SEHandlerCount;
450 };
451
452 struct coff_runtime_function_x64 {
453   support::ulittle32_t BeginAddress;
454   support::ulittle32_t EndAddress;
455   support::ulittle32_t UnwindInformation;
456 };
457
458 class COFFObjectFile : public ObjectFile {
459 private:
460   friend class ImportDirectoryEntryRef;
461   friend class ExportDirectoryEntryRef;
462   const coff_file_header *COFFHeader;
463   const coff_bigobj_file_header *COFFBigObjHeader;
464   const pe32_header *PE32Header;
465   const pe32plus_header *PE32PlusHeader;
466   const data_directory *DataDirectory;
467   const coff_section *SectionTable;
468   const coff_symbol16 *SymbolTable16;
469   const coff_symbol32 *SymbolTable32;
470   const char *StringTable;
471   uint32_t StringTableSize;
472   const import_directory_table_entry *ImportDirectory;
473   uint32_t NumberOfImportDirectory;
474   const delay_import_directory_table_entry *DelayImportDirectory;
475   uint32_t NumberOfDelayImportDirectory;
476   const export_directory_table_entry *ExportDirectory;
477
478   std::error_code getString(uint32_t offset, StringRef &Res) const;
479
480   template <typename coff_symbol_type>
481   const coff_symbol_type *toSymb(DataRefImpl Symb) const;
482   const coff_section *toSec(DataRefImpl Sec) const;
483   const coff_relocation *toRel(DataRefImpl Rel) const;
484
485   std::error_code initSymbolTablePtr();
486   std::error_code initImportTablePtr();
487   std::error_code initDelayImportTablePtr();
488   std::error_code initExportTablePtr();
489
490 public:
491   uintptr_t getSymbolTable() const {
492     if (SymbolTable16)
493       return reinterpret_cast<uintptr_t>(SymbolTable16);
494     if (SymbolTable32)
495       return reinterpret_cast<uintptr_t>(SymbolTable32);
496     return uintptr_t(0);
497   }
498   uint16_t getMachine() const {
499     if (COFFHeader)
500       return COFFHeader->Machine;
501     if (COFFBigObjHeader)
502       return COFFBigObjHeader->Machine;
503     llvm_unreachable("no COFF header!");
504   }
505   uint16_t getSizeOfOptionalHeader() const {
506     if (COFFHeader)
507       return COFFHeader->SizeOfOptionalHeader;
508     // bigobj doesn't have this field.
509     if (COFFBigObjHeader)
510       return 0;
511     llvm_unreachable("no COFF header!");
512   }
513   uint16_t getCharacteristics() const {
514     if (COFFHeader)
515       return COFFHeader->Characteristics;
516     // bigobj doesn't have characteristics to speak of,
517     // editbin will silently lie to you if you attempt to set any.
518     if (COFFBigObjHeader)
519       return 0;
520     llvm_unreachable("no COFF header!");
521   }
522   uint32_t getTimeDateStamp() const {
523     if (COFFHeader)
524       return COFFHeader->TimeDateStamp;
525     if (COFFBigObjHeader)
526       return COFFBigObjHeader->TimeDateStamp;
527     llvm_unreachable("no COFF header!");
528   }
529   uint32_t getNumberOfSections() const {
530     if (COFFHeader)
531       return COFFHeader->NumberOfSections;
532     if (COFFBigObjHeader)
533       return COFFBigObjHeader->NumberOfSections;
534     llvm_unreachable("no COFF header!");
535   }
536   uint32_t getPointerToSymbolTable() const {
537     if (COFFHeader)
538       return COFFHeader->PointerToSymbolTable;
539     if (COFFBigObjHeader)
540       return COFFBigObjHeader->PointerToSymbolTable;
541     llvm_unreachable("no COFF header!");
542   }
543   uint32_t getNumberOfSymbols() const {
544     if (COFFHeader)
545       return COFFHeader->NumberOfSymbols;
546     if (COFFBigObjHeader)
547       return COFFBigObjHeader->NumberOfSymbols;
548     llvm_unreachable("no COFF header!");
549   }
550 protected:
551   void moveSymbolNext(DataRefImpl &Symb) const override;
552   std::error_code getSymbolName(DataRefImpl Symb,
553                                 StringRef &Res) const override;
554   std::error_code getSymbolAddress(DataRefImpl Symb,
555                                    uint64_t &Res) const override;
556   std::error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
557   uint32_t getSymbolFlags(DataRefImpl Symb) const override;
558   std::error_code getSymbolType(DataRefImpl Symb,
559                                 SymbolRef::Type &Res) const override;
560   std::error_code getSymbolSection(DataRefImpl Symb,
561                                    section_iterator &Res) const override;
562   void moveSectionNext(DataRefImpl &Sec) const override;
563   std::error_code getSectionName(DataRefImpl Sec,
564                                  StringRef &Res) const override;
565   uint64_t getSectionAddress(DataRefImpl Sec) const override;
566   uint64_t getSectionSize(DataRefImpl Sec) const override;
567   std::error_code getSectionContents(DataRefImpl Sec,
568                                      StringRef &Res) const override;
569   uint64_t getSectionAlignment(DataRefImpl Sec) const override;
570   bool isSectionText(DataRefImpl Sec) const override;
571   bool isSectionData(DataRefImpl Sec) const override;
572   bool isSectionBSS(DataRefImpl Sec) const override;
573   bool isSectionVirtual(DataRefImpl Sec) const override;
574   bool isSectionZeroInit(DataRefImpl Sec) const override;
575   bool isSectionReadOnlyData(DataRefImpl Sec) const override;
576   bool isSectionRequiredForExecution(DataRefImpl Sec) const override;
577   bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
578   relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
579   relocation_iterator section_rel_end(DataRefImpl Sec) const override;
580
581   void moveRelocationNext(DataRefImpl &Rel) const override;
582   std::error_code getRelocationAddress(DataRefImpl Rel,
583                                        uint64_t &Res) const override;
584   std::error_code getRelocationOffset(DataRefImpl Rel,
585                                       uint64_t &Res) const override;
586   symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
587   std::error_code getRelocationType(DataRefImpl Rel,
588                                     uint64_t &Res) const override;
589   std::error_code
590   getRelocationTypeName(DataRefImpl Rel,
591                         SmallVectorImpl<char> &Result) const override;
592   std::error_code
593   getRelocationValueString(DataRefImpl Rel,
594                            SmallVectorImpl<char> &Result) const override;
595
596 public:
597   COFFObjectFile(MemoryBufferRef Object, std::error_code &EC);
598   basic_symbol_iterator symbol_begin_impl() const override;
599   basic_symbol_iterator symbol_end_impl() const override;
600   section_iterator section_begin() const override;
601   section_iterator section_end() const override;
602
603   const coff_section *getCOFFSection(const SectionRef &Section) const;
604   COFFSymbolRef getCOFFSymbol(const DataRefImpl &Ref) const;
605   COFFSymbolRef getCOFFSymbol(const SymbolRef &Symbol) const;
606   const coff_relocation *getCOFFRelocation(const RelocationRef &Reloc) const;
607
608   uint8_t getBytesInAddress() const override;
609   StringRef getFileFormatName() const override;
610   unsigned getArch() const override;
611
612   import_directory_iterator import_directory_begin() const;
613   import_directory_iterator import_directory_end() const;
614   delay_import_directory_iterator delay_import_directory_begin() const;
615   delay_import_directory_iterator delay_import_directory_end() const;
616   export_directory_iterator export_directory_begin() const;
617   export_directory_iterator export_directory_end() const;
618
619   iterator_range<import_directory_iterator> import_directories() const;
620   iterator_range<delay_import_directory_iterator>
621       delay_import_directories() const;
622   iterator_range<export_directory_iterator> export_directories() const;
623
624   const dos_header *getDOSHeader() const {
625     if (!PE32Header && !PE32PlusHeader)
626       return nullptr;
627     return reinterpret_cast<const dos_header *>(base());
628   }
629   std::error_code getPE32Header(const pe32_header *&Res) const;
630   std::error_code getPE32PlusHeader(const pe32plus_header *&Res) const;
631   std::error_code getDataDirectory(uint32_t index,
632                                    const data_directory *&Res) const;
633   std::error_code getSection(int32_t index, const coff_section *&Res) const;
634   template <typename coff_symbol_type>
635   std::error_code getSymbol(uint32_t Index,
636                             const coff_symbol_type *&Res) const {
637     if (Index >= getNumberOfSymbols())
638       return object_error::parse_failed;
639
640     Res = reinterpret_cast<coff_symbol_type *>(getSymbolTable()) + Index;
641     return object_error::success;
642   }
643   ErrorOr<COFFSymbolRef> getSymbol(uint32_t index) const {
644     if (SymbolTable16) {
645       const coff_symbol16 *Symb = nullptr;
646       if (std::error_code EC = getSymbol(index, Symb))
647         return EC;
648       return COFFSymbolRef(Symb);
649     }
650     if (SymbolTable32) {
651       const coff_symbol32 *Symb = nullptr;
652       if (std::error_code EC = getSymbol(index, Symb))
653         return EC;
654       return COFFSymbolRef(Symb);
655     }
656     llvm_unreachable("no symbol table pointer!");
657   }
658   template <typename T>
659   std::error_code getAuxSymbol(uint32_t index, const T *&Res) const {
660     ErrorOr<COFFSymbolRef> s = getSymbol(index);
661     if (std::error_code EC = s.getError())
662       return EC;
663     Res = reinterpret_cast<const T *>(s->getRawPtr());
664     return object_error::success;
665   }
666   std::error_code getSymbolName(COFFSymbolRef Symbol, StringRef &Res) const;
667
668   ArrayRef<uint8_t> getSymbolAuxData(COFFSymbolRef Symbol) const;
669
670   size_t getSymbolTableEntrySize() const {
671     if (COFFHeader)
672       return sizeof(coff_symbol16);
673     if (COFFBigObjHeader)
674       return sizeof(coff_symbol32);
675     llvm_unreachable("null symbol table pointer!");
676   }
677
678   std::error_code getSectionName(const coff_section *Sec, StringRef &Res) const;
679   uint64_t getSectionSize(const coff_section *Sec) const;
680   std::error_code getSectionContents(const coff_section *Sec,
681                                      ArrayRef<uint8_t> &Res) const;
682
683   std::error_code getVaPtr(uint64_t VA, uintptr_t &Res) const;
684   std::error_code getRvaPtr(uint32_t Rva, uintptr_t &Res) const;
685   std::error_code getHintName(uint32_t Rva, uint16_t &Hint,
686                               StringRef &Name) const;
687
688   bool isRelocatableObject() const override;
689
690   static inline bool classof(const Binary *v) { return v->isCOFF(); }
691 };
692
693 // The iterator for the import directory table.
694 class ImportDirectoryEntryRef {
695 public:
696   ImportDirectoryEntryRef() : OwningObject(nullptr) {}
697   ImportDirectoryEntryRef(const import_directory_table_entry *Table, uint32_t I,
698                           const COFFObjectFile *Owner)
699       : ImportTable(Table), Index(I), OwningObject(Owner) {}
700
701   bool operator==(const ImportDirectoryEntryRef &Other) const;
702   void moveNext();
703
704   imported_symbol_iterator imported_symbol_begin() const;
705   imported_symbol_iterator imported_symbol_end() const;
706   iterator_range<imported_symbol_iterator> imported_symbols() const;
707
708   std::error_code getName(StringRef &Result) const;
709   std::error_code getImportLookupTableRVA(uint32_t &Result) const;
710   std::error_code getImportAddressTableRVA(uint32_t &Result) const;
711
712   std::error_code
713   getImportTableEntry(const import_directory_table_entry *&Result) const;
714
715   std::error_code
716   getImportLookupEntry(const import_lookup_table_entry32 *&Result) const;
717
718 private:
719   const import_directory_table_entry *ImportTable;
720   uint32_t Index;
721   const COFFObjectFile *OwningObject;
722 };
723
724 class DelayImportDirectoryEntryRef {
725 public:
726   DelayImportDirectoryEntryRef() : OwningObject(nullptr) {}
727   DelayImportDirectoryEntryRef(const delay_import_directory_table_entry *T,
728                                uint32_t I, const COFFObjectFile *Owner)
729       : Table(T), Index(I), OwningObject(Owner) {}
730
731   bool operator==(const DelayImportDirectoryEntryRef &Other) const;
732   void moveNext();
733
734   imported_symbol_iterator imported_symbol_begin() const;
735   imported_symbol_iterator imported_symbol_end() const;
736   iterator_range<imported_symbol_iterator> imported_symbols() const;
737
738   std::error_code getName(StringRef &Result) const;
739   std::error_code getDelayImportTable(
740       const delay_import_directory_table_entry *&Result) const;
741
742 private:
743   const delay_import_directory_table_entry *Table;
744   uint32_t Index;
745   const COFFObjectFile *OwningObject;
746 };
747
748 // The iterator for the export directory table entry.
749 class ExportDirectoryEntryRef {
750 public:
751   ExportDirectoryEntryRef() : OwningObject(nullptr) {}
752   ExportDirectoryEntryRef(const export_directory_table_entry *Table, uint32_t I,
753                           const COFFObjectFile *Owner)
754       : ExportTable(Table), Index(I), OwningObject(Owner) {}
755
756   bool operator==(const ExportDirectoryEntryRef &Other) const;
757   void moveNext();
758
759   std::error_code getDllName(StringRef &Result) const;
760   std::error_code getOrdinalBase(uint32_t &Result) const;
761   std::error_code getOrdinal(uint32_t &Result) const;
762   std::error_code getExportRVA(uint32_t &Result) const;
763   std::error_code getSymbolName(StringRef &Result) const;
764
765 private:
766   const export_directory_table_entry *ExportTable;
767   uint32_t Index;
768   const COFFObjectFile *OwningObject;
769 };
770
771 class ImportedSymbolRef {
772 public:
773   ImportedSymbolRef() : OwningObject(nullptr) {}
774   ImportedSymbolRef(const import_lookup_table_entry32 *Entry, uint32_t I,
775                     const COFFObjectFile *Owner)
776       : Entry32(Entry), Entry64(nullptr), Index(I), OwningObject(Owner) {}
777   ImportedSymbolRef(const import_lookup_table_entry64 *Entry, uint32_t I,
778                     const COFFObjectFile *Owner)
779       : Entry32(nullptr), Entry64(Entry), Index(I), OwningObject(Owner) {}
780
781   bool operator==(const ImportedSymbolRef &Other) const;
782   void moveNext();
783
784   std::error_code getSymbolName(StringRef &Result) const;
785   std::error_code getOrdinal(uint16_t &Result) const;
786
787 private:
788   const import_lookup_table_entry32 *Entry32;
789   const import_lookup_table_entry64 *Entry64;
790   uint32_t Index;
791   const COFFObjectFile *OwningObject;
792 };
793 } // end namespace object
794 } // end namespace llvm
795
796 #endif