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