51706b4a180700ecca1238f9fe128d92dc8cf819
[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 ExportDirectoryEntryRef;
29 typedef content_iterator<ImportDirectoryEntryRef> import_directory_iterator;
30 typedef content_iterator<ExportDirectoryEntryRef> export_directory_iterator;
31
32 /// The DOS compatible header at the front of all PE/COFF executables.
33 struct dos_header {
34   support::ulittle16_t Magic;
35   support::ulittle16_t UsedBytesInTheLastPage;
36   support::ulittle16_t FileSizeInPages;
37   support::ulittle16_t NumberOfRelocationItems;
38   support::ulittle16_t HeaderSizeInParagraphs;
39   support::ulittle16_t MinimumExtraParagraphs;
40   support::ulittle16_t MaximumExtraParagraphs;
41   support::ulittle16_t InitialRelativeSS;
42   support::ulittle16_t InitialSP;
43   support::ulittle16_t Checksum;
44   support::ulittle16_t InitialIP;
45   support::ulittle16_t InitialRelativeCS;
46   support::ulittle16_t AddressOfRelocationTable;
47   support::ulittle16_t OverlayNumber;
48   support::ulittle16_t Reserved[4];
49   support::ulittle16_t OEMid;
50   support::ulittle16_t OEMinfo;
51   support::ulittle16_t Reserved2[10];
52   support::ulittle32_t AddressOfNewExeHeader;
53 };
54
55 struct coff_file_header {
56   support::ulittle16_t Machine;
57   support::ulittle16_t NumberOfSections;
58   support::ulittle32_t TimeDateStamp;
59   support::ulittle32_t PointerToSymbolTable;
60   support::ulittle32_t NumberOfSymbols;
61   support::ulittle16_t SizeOfOptionalHeader;
62   support::ulittle16_t Characteristics;
63
64   bool isImportLibrary() const { return NumberOfSections == 0xffff; }
65 };
66
67 struct coff_bigobj_file_header {
68   support::ulittle16_t Sig1;
69   support::ulittle16_t Sig2;
70   support::ulittle16_t Version;
71   support::ulittle16_t Machine;
72   support::ulittle32_t TimeDateStamp;
73   uint8_t              UUID[16];
74   support::ulittle32_t unused1;
75   support::ulittle32_t unused2;
76   support::ulittle32_t unused3;
77   support::ulittle32_t unused4;
78   support::ulittle32_t NumberOfSections;
79   support::ulittle32_t PointerToSymbolTable;
80   support::ulittle32_t NumberOfSymbols;
81 };
82
83 /// The 32-bit PE header that follows the COFF header.
84 struct pe32_header {
85   support::ulittle16_t Magic;
86   uint8_t MajorLinkerVersion;
87   uint8_t MinorLinkerVersion;
88   support::ulittle32_t SizeOfCode;
89   support::ulittle32_t SizeOfInitializedData;
90   support::ulittle32_t SizeOfUninitializedData;
91   support::ulittle32_t AddressOfEntryPoint;
92   support::ulittle32_t BaseOfCode;
93   support::ulittle32_t BaseOfData;
94   support::ulittle32_t ImageBase;
95   support::ulittle32_t SectionAlignment;
96   support::ulittle32_t FileAlignment;
97   support::ulittle16_t MajorOperatingSystemVersion;
98   support::ulittle16_t MinorOperatingSystemVersion;
99   support::ulittle16_t MajorImageVersion;
100   support::ulittle16_t MinorImageVersion;
101   support::ulittle16_t MajorSubsystemVersion;
102   support::ulittle16_t MinorSubsystemVersion;
103   support::ulittle32_t Win32VersionValue;
104   support::ulittle32_t SizeOfImage;
105   support::ulittle32_t SizeOfHeaders;
106   support::ulittle32_t CheckSum;
107   support::ulittle16_t Subsystem;
108   support::ulittle16_t DLLCharacteristics;
109   support::ulittle32_t SizeOfStackReserve;
110   support::ulittle32_t SizeOfStackCommit;
111   support::ulittle32_t SizeOfHeapReserve;
112   support::ulittle32_t SizeOfHeapCommit;
113   support::ulittle32_t LoaderFlags;
114   support::ulittle32_t NumberOfRvaAndSize;
115 };
116
117 /// The 64-bit PE header that follows the COFF header.
118 struct pe32plus_header {
119   support::ulittle16_t Magic;
120   uint8_t MajorLinkerVersion;
121   uint8_t MinorLinkerVersion;
122   support::ulittle32_t SizeOfCode;
123   support::ulittle32_t SizeOfInitializedData;
124   support::ulittle32_t SizeOfUninitializedData;
125   support::ulittle32_t AddressOfEntryPoint;
126   support::ulittle32_t BaseOfCode;
127   support::ulittle64_t ImageBase;
128   support::ulittle32_t SectionAlignment;
129   support::ulittle32_t FileAlignment;
130   support::ulittle16_t MajorOperatingSystemVersion;
131   support::ulittle16_t MinorOperatingSystemVersion;
132   support::ulittle16_t MajorImageVersion;
133   support::ulittle16_t MinorImageVersion;
134   support::ulittle16_t MajorSubsystemVersion;
135   support::ulittle16_t MinorSubsystemVersion;
136   support::ulittle32_t Win32VersionValue;
137   support::ulittle32_t SizeOfImage;
138   support::ulittle32_t SizeOfHeaders;
139   support::ulittle32_t CheckSum;
140   support::ulittle16_t Subsystem;
141   support::ulittle16_t DLLCharacteristics;
142   support::ulittle64_t SizeOfStackReserve;
143   support::ulittle64_t SizeOfStackCommit;
144   support::ulittle64_t SizeOfHeapReserve;
145   support::ulittle64_t SizeOfHeapCommit;
146   support::ulittle32_t LoaderFlags;
147   support::ulittle32_t NumberOfRvaAndSize;
148 };
149
150 struct data_directory {
151   support::ulittle32_t RelativeVirtualAddress;
152   support::ulittle32_t Size;
153 };
154
155 struct import_directory_table_entry {
156   support::ulittle32_t ImportLookupTableRVA;
157   support::ulittle32_t TimeDateStamp;
158   support::ulittle32_t ForwarderChain;
159   support::ulittle32_t NameRVA;
160   support::ulittle32_t ImportAddressTableRVA;
161 };
162
163 struct import_lookup_table_entry32 {
164   support::ulittle32_t data;
165
166   bool isOrdinal() const { return data & 0x80000000; }
167
168   uint16_t getOrdinal() const {
169     assert(isOrdinal() && "ILT entry is not an ordinal!");
170     return data & 0xFFFF;
171   }
172
173   uint32_t getHintNameRVA() const {
174     assert(!isOrdinal() && "ILT entry is not a Hint/Name RVA!");
175     return data;
176   }
177 };
178
179 struct export_directory_table_entry {
180   support::ulittle32_t ExportFlags;
181   support::ulittle32_t TimeDateStamp;
182   support::ulittle16_t MajorVersion;
183   support::ulittle16_t MinorVersion;
184   support::ulittle32_t NameRVA;
185   support::ulittle32_t OrdinalBase;
186   support::ulittle32_t AddressTableEntries;
187   support::ulittle32_t NumberOfNamePointers;
188   support::ulittle32_t ExportAddressTableRVA;
189   support::ulittle32_t NamePointerRVA;
190   support::ulittle32_t OrdinalTableRVA;
191 };
192
193 union export_address_table_entry {
194   support::ulittle32_t ExportRVA;
195   support::ulittle32_t ForwarderRVA;
196 };
197
198 typedef support::ulittle32_t export_name_pointer_table_entry;
199 typedef support::ulittle16_t export_ordinal_table_entry;
200
201 struct StringTableOffset {
202   support::ulittle32_t Zeroes;
203   support::ulittle32_t Offset;
204 };
205
206 template <typename SectionNumberType>
207 struct coff_symbol {
208   union {
209     char ShortName[COFF::NameSize];
210     StringTableOffset Offset;
211   } Name;
212
213   support::ulittle32_t Value;
214   SectionNumberType SectionNumber;
215
216   support::ulittle16_t Type;
217
218   support::ulittle8_t StorageClass;
219   support::ulittle8_t NumberOfAuxSymbols;
220 };
221
222 typedef coff_symbol<support::ulittle16_t> coff_symbol16;
223 typedef coff_symbol<support::ulittle32_t> coff_symbol32;
224
225 class COFFSymbolRef {
226 public:
227   COFFSymbolRef(const coff_symbol16 *CS) : CS16(CS), CS32(nullptr) {}
228   COFFSymbolRef(const coff_symbol32 *CS) : CS16(nullptr), CS32(CS) {}
229   COFFSymbolRef() : CS16(nullptr), CS32(nullptr) {}
230
231   const void *getRawPtr() const {
232     return CS16 ? static_cast<const void *>(CS16) : CS32;
233   }
234
235   friend bool operator<(COFFSymbolRef A, COFFSymbolRef B) {
236     return A.getRawPtr() < B.getRawPtr();
237   }
238
239   const char *getShortName() const {
240     return CS16 ? CS16->Name.ShortName : CS32->Name.ShortName;
241   }
242
243   const StringTableOffset &getStringTableOffset() const {
244     return CS16 ? CS16->Name.Offset : CS32->Name.Offset;
245   }
246
247   uint32_t getValue() const { return CS16 ? CS16->Value : CS32->Value; }
248
249   int32_t getSectionNumber() const {
250     if (CS16) {
251       // Reserved sections are returned as negative numbers.
252       if (CS16->SectionNumber <= COFF::MaxNumberOfSections16)
253         return CS16->SectionNumber;
254       return static_cast<int16_t>(CS16->SectionNumber);
255     }
256     return static_cast<int32_t>(CS32->SectionNumber);
257   }
258
259   uint16_t getType() const { return CS16 ? CS16->Type : CS32->Type; }
260
261   uint8_t getStorageClass() const {
262     return CS16 ? CS16->StorageClass : CS32->StorageClass;
263   }
264
265   uint8_t getNumberOfAuxSymbols() const {
266     return CS16 ? CS16->NumberOfAuxSymbols : CS32->NumberOfAuxSymbols;
267   }
268
269   uint8_t getBaseType() const { return getType() & 0x0F; }
270
271   uint8_t getComplexType() const { return (getType() & 0xF0) >> 4; }
272
273   bool isFunctionDefinition() const {
274     return getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
275            getBaseType() == COFF::IMAGE_SYM_TYPE_NULL &&
276            getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION &&
277            !COFF::isReservedSectionNumber(getSectionNumber());
278   }
279
280   bool isFunctionLineInfo() const {
281     return getStorageClass() == COFF::IMAGE_SYM_CLASS_FUNCTION;
282   }
283
284   bool isWeakExternal() const {
285     return getStorageClass() == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL ||
286            (getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
287             getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED && getValue() == 0);
288   }
289
290   bool isFileRecord() const {
291     return getStorageClass() == COFF::IMAGE_SYM_CLASS_FILE;
292   }
293
294   bool isSectionDefinition() const {
295     // C++/CLI creates external ABS symbols for non-const appdomain globals.
296     // These are also followed by an auxiliary section definition.
297     bool isAppdomainGlobal =
298         getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
299         getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE;
300     bool isOrdinarySection =
301         getStorageClass() == COFF::IMAGE_SYM_CLASS_STATIC && getValue() == 0;
302     return isAppdomainGlobal || isOrdinarySection;
303   }
304
305   bool isCLRToken() const {
306     return getStorageClass() == COFF::IMAGE_SYM_CLASS_CLR_TOKEN;
307   }
308
309 private:
310   const coff_symbol16 *CS16;
311   const coff_symbol32 *CS32;
312 };
313
314 struct coff_section {
315   char Name[COFF::NameSize];
316   support::ulittle32_t VirtualSize;
317   support::ulittle32_t VirtualAddress;
318   support::ulittle32_t SizeOfRawData;
319   support::ulittle32_t PointerToRawData;
320   support::ulittle32_t PointerToRelocations;
321   support::ulittle32_t PointerToLinenumbers;
322   support::ulittle16_t NumberOfRelocations;
323   support::ulittle16_t NumberOfLinenumbers;
324   support::ulittle32_t Characteristics;
325
326   // Returns true if the actual number of relocations is stored in
327   // VirtualAddress field of the first relocation table entry.
328   bool hasExtendedRelocations() const {
329     return Characteristics & COFF::IMAGE_SCN_LNK_NRELOC_OVFL &&
330         NumberOfRelocations == UINT16_MAX;
331   };
332 };
333
334 struct coff_relocation {
335   support::ulittle32_t VirtualAddress;
336   support::ulittle32_t SymbolTableIndex;
337   support::ulittle16_t Type;
338 };
339
340 struct coff_aux_function_definition {
341   support::ulittle32_t TagIndex;
342   support::ulittle32_t TotalSize;
343   support::ulittle32_t PointerToLinenumber;
344   support::ulittle32_t PointerToNextFunction;
345 };
346
347 struct coff_aux_bf_and_ef_symbol {
348   char Unused1[4];
349   support::ulittle16_t Linenumber;
350   char Unused2[6];
351   support::ulittle32_t PointerToNextFunction;
352 };
353
354 struct coff_aux_weak_external {
355   support::ulittle32_t TagIndex;
356   support::ulittle32_t Characteristics;
357 };
358
359 struct coff_aux_section_definition {
360   support::ulittle32_t Length;
361   support::ulittle16_t NumberOfRelocations;
362   support::ulittle16_t NumberOfLinenumbers;
363   support::ulittle32_t CheckSum;
364   support::ulittle16_t Number;
365   support::ulittle8_t Selection;
366 };
367
368 struct coff_aux_clr_token {
369   support::ulittle8_t AuxType;
370   support::ulittle8_t Reserved;
371   support::ulittle32_t SymbolTableIndex;
372 };
373
374 struct coff_load_configuration32 {
375   support::ulittle32_t Characteristics;
376   support::ulittle32_t TimeDateStamp;
377   support::ulittle16_t MajorVersion;
378   support::ulittle16_t MinorVersion;
379   support::ulittle32_t GlobalFlagsClear;
380   support::ulittle32_t GlobalFlagsSet;
381   support::ulittle32_t CriticalSectionDefaultTimeout;
382   support::ulittle32_t DeCommitFreeBlockThreshold;
383   support::ulittle32_t DeCommitTotalFreeThreshold;
384   support::ulittle32_t LockPrefixTable;
385   support::ulittle32_t MaximumAllocationSize;
386   support::ulittle32_t VirtualMemoryThreshold;
387   support::ulittle32_t ProcessAffinityMask;
388   support::ulittle32_t ProcessHeapFlags;
389   support::ulittle16_t CSDVersion;
390   support::ulittle16_t Reserved;
391   support::ulittle32_t EditList;
392   support::ulittle32_t SecurityCookie;
393   support::ulittle32_t SEHandlerTable;
394   support::ulittle32_t SEHandlerCount;
395 };
396
397 struct coff_runtime_function_x64 {
398   support::ulittle32_t BeginAddress;
399   support::ulittle32_t EndAddress;
400   support::ulittle32_t UnwindInformation;
401 };
402
403 class COFFObjectFile : public ObjectFile {
404 private:
405   friend class ImportDirectoryEntryRef;
406   friend class ExportDirectoryEntryRef;
407   const coff_file_header *COFFHeader;
408   const coff_bigobj_file_header *COFFBigObjHeader;
409   const pe32_header *PE32Header;
410   const pe32plus_header *PE32PlusHeader;
411   const data_directory *DataDirectory;
412   const coff_section *SectionTable;
413   const coff_symbol16 *SymbolTable16;
414   const coff_symbol32 *SymbolTable32;
415   const char *StringTable;
416   uint32_t StringTableSize;
417   const import_directory_table_entry *ImportDirectory;
418   uint32_t NumberOfImportDirectory;
419   const export_directory_table_entry *ExportDirectory;
420
421   std::error_code getString(uint32_t offset, StringRef &Res) const;
422
423   template <typename coff_symbol_type>
424   const coff_symbol_type *toSymb(DataRefImpl Symb) const;
425   const coff_section *toSec(DataRefImpl Sec) const;
426   const coff_relocation *toRel(DataRefImpl Rel) const;
427
428   std::error_code initSymbolTablePtr();
429   std::error_code initImportTablePtr();
430   std::error_code initExportTablePtr();
431
432 public:
433   uintptr_t getSymbolTable() const {
434     if (SymbolTable16)
435       return reinterpret_cast<uintptr_t>(SymbolTable16);
436     if (SymbolTable32)
437       return reinterpret_cast<uintptr_t>(SymbolTable32);
438     return uintptr_t(0);
439   }
440   uint16_t getMachine() const {
441     if (COFFHeader)
442       return COFFHeader->Machine;
443     if (COFFBigObjHeader)
444       return COFFBigObjHeader->Machine;
445     llvm_unreachable("no COFF header!");
446   }
447   uint16_t getSizeOfOptionalHeader() const {
448     if (COFFHeader)
449       return COFFHeader->SizeOfOptionalHeader;
450     // bigobj doesn't have this field.
451     if (COFFBigObjHeader)
452       return 0;
453     llvm_unreachable("no COFF header!");
454   }
455   uint16_t getCharacteristics() const {
456     if (COFFHeader)
457       return COFFHeader->Characteristics;
458     // bigobj doesn't have characteristics to speak of,
459     // editbin will silently lie to you if you attempt to set any.
460     if (COFFBigObjHeader)
461       return 0;
462     llvm_unreachable("no COFF header!");
463   }
464   uint32_t getTimeDateStamp() const {
465     if (COFFHeader)
466       return COFFHeader->TimeDateStamp;
467     if (COFFBigObjHeader)
468       return COFFBigObjHeader->TimeDateStamp;
469     llvm_unreachable("no COFF header!");
470   }
471   uint32_t getNumberOfSections() const {
472     if (COFFHeader)
473       return COFFHeader->NumberOfSections;
474     if (COFFBigObjHeader)
475       return COFFBigObjHeader->NumberOfSections;
476     llvm_unreachable("no COFF header!");
477   }
478   uint32_t getPointerToSymbolTable() const {
479     if (COFFHeader)
480       return COFFHeader->PointerToSymbolTable;
481     if (COFFBigObjHeader)
482       return COFFBigObjHeader->PointerToSymbolTable;
483     llvm_unreachable("no COFF header!");
484   }
485   uint32_t getNumberOfSymbols() const {
486     if (COFFHeader)
487       return COFFHeader->NumberOfSymbols;
488     if (COFFBigObjHeader)
489       return COFFBigObjHeader->NumberOfSymbols;
490     llvm_unreachable("no COFF header!");
491   }
492 protected:
493   void moveSymbolNext(DataRefImpl &Symb) const override;
494   std::error_code getSymbolName(DataRefImpl Symb,
495                                 StringRef &Res) const override;
496   std::error_code getSymbolAddress(DataRefImpl Symb,
497                                    uint64_t &Res) const override;
498   std::error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
499   uint32_t getSymbolFlags(DataRefImpl Symb) const override;
500   std::error_code getSymbolType(DataRefImpl Symb,
501                                 SymbolRef::Type &Res) const override;
502   std::error_code getSymbolSection(DataRefImpl Symb,
503                                    section_iterator &Res) const override;
504   void moveSectionNext(DataRefImpl &Sec) const override;
505   std::error_code getSectionName(DataRefImpl Sec,
506                                  StringRef &Res) const override;
507   std::error_code getSectionAddress(DataRefImpl Sec,
508                                     uint64_t &Res) const override;
509   std::error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const override;
510   std::error_code getSectionContents(DataRefImpl Sec,
511                                      StringRef &Res) const override;
512   std::error_code getSectionAlignment(DataRefImpl Sec,
513                                       uint64_t &Res) const override;
514   std::error_code isSectionText(DataRefImpl Sec, bool &Res) const override;
515   std::error_code isSectionData(DataRefImpl Sec, bool &Res) const override;
516   std::error_code isSectionBSS(DataRefImpl Sec, bool &Res) const override;
517   std::error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const override;
518   std::error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const override;
519   std::error_code isSectionReadOnlyData(DataRefImpl Sec,
520                                         bool &Res) const override;
521   std::error_code isSectionRequiredForExecution(DataRefImpl Sec,
522                                                 bool &Res) const override;
523   std::error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
524                                         bool &Result) const override;
525   relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
526   relocation_iterator section_rel_end(DataRefImpl Sec) const override;
527
528   void moveRelocationNext(DataRefImpl &Rel) const override;
529   std::error_code getRelocationAddress(DataRefImpl Rel,
530                                        uint64_t &Res) const override;
531   std::error_code getRelocationOffset(DataRefImpl Rel,
532                                       uint64_t &Res) const override;
533   symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
534   std::error_code getRelocationType(DataRefImpl Rel,
535                                     uint64_t &Res) const override;
536   std::error_code
537   getRelocationTypeName(DataRefImpl Rel,
538                         SmallVectorImpl<char> &Result) const override;
539   std::error_code
540   getRelocationValueString(DataRefImpl Rel,
541                            SmallVectorImpl<char> &Result) const override;
542
543 public:
544   COFFObjectFile(MemoryBufferRef Object, std::error_code &EC);
545   basic_symbol_iterator symbol_begin_impl() const override;
546   basic_symbol_iterator symbol_end_impl() const override;
547   section_iterator section_begin() const override;
548   section_iterator section_end() const override;
549
550   const coff_section *getCOFFSection(const SectionRef &Section) const;
551   COFFSymbolRef getCOFFSymbol(const DataRefImpl &Ref) const;
552   COFFSymbolRef getCOFFSymbol(const SymbolRef &Symbol) const;
553   const coff_relocation *getCOFFRelocation(const RelocationRef &Reloc) const;
554
555   uint8_t getBytesInAddress() const override;
556   StringRef getFileFormatName() const override;
557   unsigned getArch() const override;
558
559   import_directory_iterator import_directory_begin() const;
560   import_directory_iterator import_directory_end() const;
561   export_directory_iterator export_directory_begin() const;
562   export_directory_iterator export_directory_end() const;
563
564   std::error_code getPE32Header(const pe32_header *&Res) const;
565   std::error_code getPE32PlusHeader(const pe32plus_header *&Res) const;
566   std::error_code getDataDirectory(uint32_t index,
567                                    const data_directory *&Res) const;
568   std::error_code getSection(int32_t index, const coff_section *&Res) const;
569   template <typename coff_symbol_type>
570   std::error_code getSymbol(uint32_t Index,
571                             const coff_symbol_type *&Res) const {
572     if (Index < getNumberOfSymbols())
573       Res = reinterpret_cast<coff_symbol_type *>(getSymbolTable()) + Index;
574     else
575       return object_error::parse_failed;
576     return object_error::success;
577   }
578   ErrorOr<COFFSymbolRef> getSymbol(uint32_t index) const {
579     if (SymbolTable16) {
580       const coff_symbol16 *Symb = nullptr;
581       if (std::error_code EC = getSymbol(index, Symb))
582         return EC;
583       return COFFSymbolRef(Symb);
584     }
585     if (SymbolTable32) {
586       const coff_symbol32 *Symb = nullptr;
587       if (std::error_code EC = getSymbol(index, Symb))
588         return EC;
589       return COFFSymbolRef(Symb);
590     }
591     llvm_unreachable("no symbol table pointer!");
592   }
593   template <typename T>
594   std::error_code getAuxSymbol(uint32_t index, const T *&Res) const {
595     ErrorOr<COFFSymbolRef> s = getSymbol(index);
596     if (std::error_code EC = s.getError())
597       return EC;
598     Res = reinterpret_cast<const T *>(s->getRawPtr());
599     return object_error::success;
600   }
601   std::error_code getSymbolName(COFFSymbolRef Symbol, StringRef &Res) const;
602
603   ArrayRef<uint8_t> getSymbolAuxData(COFFSymbolRef Symbol) const;
604
605   size_t getSymbolTableEntrySize() const {
606     if (COFFHeader)
607       return sizeof(coff_symbol16);
608     if (COFFBigObjHeader)
609       return sizeof(coff_symbol32);
610     llvm_unreachable("null symbol table pointer!");
611   }
612
613   std::error_code getSectionName(const coff_section *Sec, StringRef &Res) const;
614   std::error_code getSectionContents(const coff_section *Sec,
615                                      ArrayRef<uint8_t> &Res) const;
616
617   std::error_code getVaPtr(uint64_t VA, uintptr_t &Res) const;
618   std::error_code getRvaPtr(uint32_t Rva, uintptr_t &Res) const;
619   std::error_code getHintName(uint32_t Rva, uint16_t &Hint,
620                               StringRef &Name) const;
621
622   bool isRelocatableObject() const override;
623
624   static inline bool classof(const Binary *v) { return v->isCOFF(); }
625 };
626
627 // The iterator for the import directory table.
628 class ImportDirectoryEntryRef {
629 public:
630   ImportDirectoryEntryRef() : OwningObject(nullptr) {}
631   ImportDirectoryEntryRef(const import_directory_table_entry *Table, uint32_t I,
632                           const COFFObjectFile *Owner)
633       : ImportTable(Table), Index(I), OwningObject(Owner) {}
634
635   bool operator==(const ImportDirectoryEntryRef &Other) const;
636   void moveNext();
637   std::error_code getName(StringRef &Result) const;
638
639   std::error_code
640   getImportTableEntry(const import_directory_table_entry *&Result) const;
641
642   std::error_code
643   getImportLookupEntry(const import_lookup_table_entry32 *&Result) const;
644
645 private:
646   const import_directory_table_entry *ImportTable;
647   uint32_t Index;
648   const COFFObjectFile *OwningObject;
649 };
650
651 // The iterator for the export directory table entry.
652 class ExportDirectoryEntryRef {
653 public:
654   ExportDirectoryEntryRef() : OwningObject(nullptr) {}
655   ExportDirectoryEntryRef(const export_directory_table_entry *Table, uint32_t I,
656                           const COFFObjectFile *Owner)
657       : ExportTable(Table), Index(I), OwningObject(Owner) {}
658
659   bool operator==(const ExportDirectoryEntryRef &Other) const;
660   void moveNext();
661
662   std::error_code getDllName(StringRef &Result) const;
663   std::error_code getOrdinalBase(uint32_t &Result) const;
664   std::error_code getOrdinal(uint32_t &Result) const;
665   std::error_code getExportRVA(uint32_t &Result) const;
666   std::error_code getSymbolName(StringRef &Result) const;
667
668 private:
669   const export_directory_table_entry *ExportTable;
670   uint32_t Index;
671   const COFFObjectFile *OwningObject;
672 };
673 } // end namespace object
674 } // end namespace llvm
675
676 #endif