f7a578315c1284c77d406f530727894c76b0dbaf
[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   uint8_t StorageClass;
219   uint8_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   bool isBigObj() const {
240     if (CS16)
241       return false;
242     if (CS32)
243       return true;
244     llvm_unreachable("COFFSymbolRef points to nothing!");
245   }
246
247   const char *getShortName() const {
248     return CS16 ? CS16->Name.ShortName : CS32->Name.ShortName;
249   }
250
251   const StringTableOffset &getStringTableOffset() const {
252     return CS16 ? CS16->Name.Offset : CS32->Name.Offset;
253   }
254
255   uint32_t getValue() const { return CS16 ? CS16->Value : CS32->Value; }
256
257   int32_t getSectionNumber() const {
258     if (CS16) {
259       // Reserved sections are returned as negative numbers.
260       if (CS16->SectionNumber <= COFF::MaxNumberOfSections16)
261         return CS16->SectionNumber;
262       return static_cast<int16_t>(CS16->SectionNumber);
263     }
264     return static_cast<int32_t>(CS32->SectionNumber);
265   }
266
267   uint16_t getType() const { return CS16 ? CS16->Type : CS32->Type; }
268
269   uint8_t getStorageClass() const {
270     return CS16 ? CS16->StorageClass : CS32->StorageClass;
271   }
272
273   uint8_t getNumberOfAuxSymbols() const {
274     return CS16 ? CS16->NumberOfAuxSymbols : CS32->NumberOfAuxSymbols;
275   }
276
277   uint8_t getBaseType() const { return getType() & 0x0F; }
278
279   uint8_t getComplexType() const { return (getType() & 0xF0) >> 4; }
280
281   bool isFunctionDefinition() const {
282     return getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
283            getBaseType() == COFF::IMAGE_SYM_TYPE_NULL &&
284            getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION &&
285            !COFF::isReservedSectionNumber(getSectionNumber());
286   }
287
288   bool isFunctionLineInfo() const {
289     return getStorageClass() == COFF::IMAGE_SYM_CLASS_FUNCTION;
290   }
291
292   bool isWeakExternal() const {
293     return getStorageClass() == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL ||
294            (getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
295             getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED && getValue() == 0);
296   }
297
298   bool isFileRecord() const {
299     return getStorageClass() == COFF::IMAGE_SYM_CLASS_FILE;
300   }
301
302   bool isSectionDefinition() const {
303     // C++/CLI creates external ABS symbols for non-const appdomain globals.
304     // These are also followed by an auxiliary section definition.
305     bool isAppdomainGlobal =
306         getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
307         getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE;
308     bool isOrdinarySection =
309         getStorageClass() == COFF::IMAGE_SYM_CLASS_STATIC && getValue() == 0;
310     return isAppdomainGlobal || isOrdinarySection;
311   }
312
313   bool isCLRToken() const {
314     return getStorageClass() == COFF::IMAGE_SYM_CLASS_CLR_TOKEN;
315   }
316
317 private:
318   const coff_symbol16 *CS16;
319   const coff_symbol32 *CS32;
320 };
321
322 struct coff_section {
323   char Name[COFF::NameSize];
324   support::ulittle32_t VirtualSize;
325   support::ulittle32_t VirtualAddress;
326   support::ulittle32_t SizeOfRawData;
327   support::ulittle32_t PointerToRawData;
328   support::ulittle32_t PointerToRelocations;
329   support::ulittle32_t PointerToLinenumbers;
330   support::ulittle16_t NumberOfRelocations;
331   support::ulittle16_t NumberOfLinenumbers;
332   support::ulittle32_t Characteristics;
333
334   // Returns true if the actual number of relocations is stored in
335   // VirtualAddress field of the first relocation table entry.
336   bool hasExtendedRelocations() const {
337     return Characteristics & COFF::IMAGE_SCN_LNK_NRELOC_OVFL &&
338         NumberOfRelocations == UINT16_MAX;
339   };
340 };
341
342 struct coff_relocation {
343   support::ulittle32_t VirtualAddress;
344   support::ulittle32_t SymbolTableIndex;
345   support::ulittle16_t Type;
346 };
347
348 struct coff_aux_function_definition {
349   support::ulittle32_t TagIndex;
350   support::ulittle32_t TotalSize;
351   support::ulittle32_t PointerToLinenumber;
352   support::ulittle32_t PointerToNextFunction;
353 };
354
355 struct coff_aux_bf_and_ef_symbol {
356   char Unused1[4];
357   support::ulittle16_t Linenumber;
358   char Unused2[6];
359   support::ulittle32_t PointerToNextFunction;
360 };
361
362 struct coff_aux_weak_external {
363   support::ulittle32_t TagIndex;
364   support::ulittle32_t Characteristics;
365 };
366
367 struct coff_aux_section_definition {
368   support::ulittle32_t Length;
369   support::ulittle16_t NumberOfRelocations;
370   support::ulittle16_t NumberOfLinenumbers;
371   support::ulittle32_t CheckSum;
372   support::ulittle16_t NumberLowPart;
373   uint8_t              Selection;
374   uint8_t              Unused;
375   support::ulittle16_t NumberHighPart;
376   int32_t getNumber(bool IsBigObj) const {
377     uint32_t Number = static_cast<uint32_t>(NumberLowPart);
378     if (IsBigObj)
379       Number |= static_cast<uint32_t>(NumberHighPart) << 16;
380     return static_cast<int32_t>(Number);
381   }
382 };
383
384 struct coff_aux_clr_token {
385   uint8_t              AuxType;
386   uint8_t              Reserved;
387   support::ulittle32_t SymbolTableIndex;
388 };
389
390 struct coff_load_configuration32 {
391   support::ulittle32_t Characteristics;
392   support::ulittle32_t TimeDateStamp;
393   support::ulittle16_t MajorVersion;
394   support::ulittle16_t MinorVersion;
395   support::ulittle32_t GlobalFlagsClear;
396   support::ulittle32_t GlobalFlagsSet;
397   support::ulittle32_t CriticalSectionDefaultTimeout;
398   support::ulittle32_t DeCommitFreeBlockThreshold;
399   support::ulittle32_t DeCommitTotalFreeThreshold;
400   support::ulittle32_t LockPrefixTable;
401   support::ulittle32_t MaximumAllocationSize;
402   support::ulittle32_t VirtualMemoryThreshold;
403   support::ulittle32_t ProcessAffinityMask;
404   support::ulittle32_t ProcessHeapFlags;
405   support::ulittle16_t CSDVersion;
406   support::ulittle16_t Reserved;
407   support::ulittle32_t EditList;
408   support::ulittle32_t SecurityCookie;
409   support::ulittle32_t SEHandlerTable;
410   support::ulittle32_t SEHandlerCount;
411 };
412
413 struct coff_runtime_function_x64 {
414   support::ulittle32_t BeginAddress;
415   support::ulittle32_t EndAddress;
416   support::ulittle32_t UnwindInformation;
417 };
418
419 class COFFObjectFile : public ObjectFile {
420 private:
421   friend class ImportDirectoryEntryRef;
422   friend class ExportDirectoryEntryRef;
423   const coff_file_header *COFFHeader;
424   const coff_bigobj_file_header *COFFBigObjHeader;
425   const pe32_header *PE32Header;
426   const pe32plus_header *PE32PlusHeader;
427   const data_directory *DataDirectory;
428   const coff_section *SectionTable;
429   const coff_symbol16 *SymbolTable16;
430   const coff_symbol32 *SymbolTable32;
431   const char *StringTable;
432   uint32_t StringTableSize;
433   const import_directory_table_entry *ImportDirectory;
434   uint32_t NumberOfImportDirectory;
435   const export_directory_table_entry *ExportDirectory;
436
437   std::error_code getString(uint32_t offset, StringRef &Res) const;
438
439   template <typename coff_symbol_type>
440   const coff_symbol_type *toSymb(DataRefImpl Symb) const;
441   const coff_section *toSec(DataRefImpl Sec) const;
442   const coff_relocation *toRel(DataRefImpl Rel) const;
443
444   std::error_code initSymbolTablePtr();
445   std::error_code initImportTablePtr();
446   std::error_code initExportTablePtr();
447
448 public:
449   uintptr_t getSymbolTable() const {
450     if (SymbolTable16)
451       return reinterpret_cast<uintptr_t>(SymbolTable16);
452     if (SymbolTable32)
453       return reinterpret_cast<uintptr_t>(SymbolTable32);
454     return uintptr_t(0);
455   }
456   uint16_t getMachine() const {
457     if (COFFHeader)
458       return COFFHeader->Machine;
459     if (COFFBigObjHeader)
460       return COFFBigObjHeader->Machine;
461     llvm_unreachable("no COFF header!");
462   }
463   uint16_t getSizeOfOptionalHeader() const {
464     if (COFFHeader)
465       return COFFHeader->SizeOfOptionalHeader;
466     // bigobj doesn't have this field.
467     if (COFFBigObjHeader)
468       return 0;
469     llvm_unreachable("no COFF header!");
470   }
471   uint16_t getCharacteristics() const {
472     if (COFFHeader)
473       return COFFHeader->Characteristics;
474     // bigobj doesn't have characteristics to speak of,
475     // editbin will silently lie to you if you attempt to set any.
476     if (COFFBigObjHeader)
477       return 0;
478     llvm_unreachable("no COFF header!");
479   }
480   uint32_t getTimeDateStamp() const {
481     if (COFFHeader)
482       return COFFHeader->TimeDateStamp;
483     if (COFFBigObjHeader)
484       return COFFBigObjHeader->TimeDateStamp;
485     llvm_unreachable("no COFF header!");
486   }
487   uint32_t getNumberOfSections() const {
488     if (COFFHeader)
489       return COFFHeader->NumberOfSections;
490     if (COFFBigObjHeader)
491       return COFFBigObjHeader->NumberOfSections;
492     llvm_unreachable("no COFF header!");
493   }
494   uint32_t getPointerToSymbolTable() const {
495     if (COFFHeader)
496       return COFFHeader->PointerToSymbolTable;
497     if (COFFBigObjHeader)
498       return COFFBigObjHeader->PointerToSymbolTable;
499     llvm_unreachable("no COFF header!");
500   }
501   uint32_t getNumberOfSymbols() const {
502     if (COFFHeader)
503       return COFFHeader->NumberOfSymbols;
504     if (COFFBigObjHeader)
505       return COFFBigObjHeader->NumberOfSymbols;
506     llvm_unreachable("no COFF header!");
507   }
508 protected:
509   void moveSymbolNext(DataRefImpl &Symb) const override;
510   std::error_code getSymbolName(DataRefImpl Symb,
511                                 StringRef &Res) const override;
512   std::error_code getSymbolAddress(DataRefImpl Symb,
513                                    uint64_t &Res) const override;
514   std::error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
515   uint32_t getSymbolFlags(DataRefImpl Symb) const override;
516   std::error_code getSymbolType(DataRefImpl Symb,
517                                 SymbolRef::Type &Res) const override;
518   std::error_code getSymbolSection(DataRefImpl Symb,
519                                    section_iterator &Res) const override;
520   void moveSectionNext(DataRefImpl &Sec) const override;
521   std::error_code getSectionName(DataRefImpl Sec,
522                                  StringRef &Res) const override;
523   std::error_code getSectionAddress(DataRefImpl Sec,
524                                     uint64_t &Res) const override;
525   std::error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const override;
526   std::error_code getSectionContents(DataRefImpl Sec,
527                                      StringRef &Res) const override;
528   std::error_code getSectionAlignment(DataRefImpl Sec,
529                                       uint64_t &Res) const override;
530   std::error_code isSectionText(DataRefImpl Sec, bool &Res) const override;
531   std::error_code isSectionData(DataRefImpl Sec, bool &Res) const override;
532   std::error_code isSectionBSS(DataRefImpl Sec, bool &Res) const override;
533   std::error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const override;
534   std::error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const override;
535   std::error_code isSectionReadOnlyData(DataRefImpl Sec,
536                                         bool &Res) const override;
537   std::error_code isSectionRequiredForExecution(DataRefImpl Sec,
538                                                 bool &Res) const override;
539   std::error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
540                                         bool &Result) const override;
541   relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
542   relocation_iterator section_rel_end(DataRefImpl Sec) const override;
543
544   void moveRelocationNext(DataRefImpl &Rel) const override;
545   std::error_code getRelocationAddress(DataRefImpl Rel,
546                                        uint64_t &Res) const override;
547   std::error_code getRelocationOffset(DataRefImpl Rel,
548                                       uint64_t &Res) const override;
549   symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
550   std::error_code getRelocationType(DataRefImpl Rel,
551                                     uint64_t &Res) const override;
552   std::error_code
553   getRelocationTypeName(DataRefImpl Rel,
554                         SmallVectorImpl<char> &Result) const override;
555   std::error_code
556   getRelocationValueString(DataRefImpl Rel,
557                            SmallVectorImpl<char> &Result) const override;
558
559 public:
560   COFFObjectFile(MemoryBufferRef Object, std::error_code &EC);
561   basic_symbol_iterator symbol_begin_impl() const override;
562   basic_symbol_iterator symbol_end_impl() const override;
563   section_iterator section_begin() const override;
564   section_iterator section_end() const override;
565
566   const coff_section *getCOFFSection(const SectionRef &Section) const;
567   COFFSymbolRef getCOFFSymbol(const DataRefImpl &Ref) const;
568   COFFSymbolRef getCOFFSymbol(const SymbolRef &Symbol) const;
569   const coff_relocation *getCOFFRelocation(const RelocationRef &Reloc) const;
570
571   uint8_t getBytesInAddress() const override;
572   StringRef getFileFormatName() const override;
573   unsigned getArch() const override;
574
575   import_directory_iterator import_directory_begin() const;
576   import_directory_iterator import_directory_end() const;
577   export_directory_iterator export_directory_begin() const;
578   export_directory_iterator export_directory_end() const;
579
580   std::error_code getPE32Header(const pe32_header *&Res) const;
581   std::error_code getPE32PlusHeader(const pe32plus_header *&Res) const;
582   std::error_code getDataDirectory(uint32_t index,
583                                    const data_directory *&Res) const;
584   std::error_code getSection(int32_t index, const coff_section *&Res) const;
585   template <typename coff_symbol_type>
586   std::error_code getSymbol(uint32_t Index,
587                             const coff_symbol_type *&Res) const {
588     if (Index >= getNumberOfSymbols())
589       return object_error::parse_failed;
590
591     Res = reinterpret_cast<coff_symbol_type *>(getSymbolTable()) + Index;
592     return object_error::success;
593   }
594   ErrorOr<COFFSymbolRef> getSymbol(uint32_t index) const {
595     if (SymbolTable16) {
596       const coff_symbol16 *Symb = nullptr;
597       if (std::error_code EC = getSymbol(index, Symb))
598         return EC;
599       return COFFSymbolRef(Symb);
600     }
601     if (SymbolTable32) {
602       const coff_symbol32 *Symb = nullptr;
603       if (std::error_code EC = getSymbol(index, Symb))
604         return EC;
605       return COFFSymbolRef(Symb);
606     }
607     llvm_unreachable("no symbol table pointer!");
608   }
609   template <typename T>
610   std::error_code getAuxSymbol(uint32_t index, const T *&Res) const {
611     ErrorOr<COFFSymbolRef> s = getSymbol(index);
612     if (std::error_code EC = s.getError())
613       return EC;
614     Res = reinterpret_cast<const T *>(s->getRawPtr());
615     return object_error::success;
616   }
617   std::error_code getSymbolName(COFFSymbolRef Symbol, StringRef &Res) const;
618
619   ArrayRef<uint8_t> getSymbolAuxData(COFFSymbolRef Symbol) const;
620
621   size_t getSymbolTableEntrySize() const {
622     if (COFFHeader)
623       return sizeof(coff_symbol16);
624     if (COFFBigObjHeader)
625       return sizeof(coff_symbol32);
626     llvm_unreachable("null symbol table pointer!");
627   }
628
629   std::error_code getSectionName(const coff_section *Sec, StringRef &Res) const;
630   std::error_code getSectionContents(const coff_section *Sec,
631                                      ArrayRef<uint8_t> &Res) const;
632
633   std::error_code getVaPtr(uint64_t VA, uintptr_t &Res) const;
634   std::error_code getRvaPtr(uint32_t Rva, uintptr_t &Res) const;
635   std::error_code getHintName(uint32_t Rva, uint16_t &Hint,
636                               StringRef &Name) const;
637
638   bool isRelocatableObject() const override;
639
640   static inline bool classof(const Binary *v) { return v->isCOFF(); }
641 };
642
643 // The iterator for the import directory table.
644 class ImportDirectoryEntryRef {
645 public:
646   ImportDirectoryEntryRef() : OwningObject(nullptr) {}
647   ImportDirectoryEntryRef(const import_directory_table_entry *Table, uint32_t I,
648                           const COFFObjectFile *Owner)
649       : ImportTable(Table), Index(I), OwningObject(Owner) {}
650
651   bool operator==(const ImportDirectoryEntryRef &Other) const;
652   void moveNext();
653   std::error_code getName(StringRef &Result) const;
654   std::error_code getImportLookupTableRVA(uint32_t &Result) const;
655   std::error_code getImportAddressTableRVA(uint32_t &Result) const;
656
657   std::error_code
658   getImportTableEntry(const import_directory_table_entry *&Result) const;
659
660   std::error_code
661   getImportLookupEntry(const import_lookup_table_entry32 *&Result) const;
662
663 private:
664   const import_directory_table_entry *ImportTable;
665   uint32_t Index;
666   const COFFObjectFile *OwningObject;
667 };
668
669 // The iterator for the export directory table entry.
670 class ExportDirectoryEntryRef {
671 public:
672   ExportDirectoryEntryRef() : OwningObject(nullptr) {}
673   ExportDirectoryEntryRef(const export_directory_table_entry *Table, uint32_t I,
674                           const COFFObjectFile *Owner)
675       : ExportTable(Table), Index(I), OwningObject(Owner) {}
676
677   bool operator==(const ExportDirectoryEntryRef &Other) const;
678   void moveNext();
679
680   std::error_code getDllName(StringRef &Result) const;
681   std::error_code getOrdinalBase(uint32_t &Result) const;
682   std::error_code getOrdinal(uint32_t &Result) const;
683   std::error_code getExportRVA(uint32_t &Result) const;
684   std::error_code getSymbolName(StringRef &Result) const;
685
686 private:
687   const export_directory_table_entry *ExportTable;
688   uint32_t Index;
689   const COFFObjectFile *OwningObject;
690 };
691 } // end namespace object
692 } // end namespace llvm
693
694 #endif