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