LLVM API Documentation

Object/COFF.h
Go to the documentation of this file.
00001 //===- COFF.h - COFF object file implementation -----------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file declares the COFFObjectFile class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_OBJECT_COFF_H
00015 #define LLVM_OBJECT_COFF_H
00016 
00017 #include "llvm/ADT/PointerUnion.h"
00018 #include "llvm/Object/ObjectFile.h"
00019 #include "llvm/Support/COFF.h"
00020 #include "llvm/Support/Endian.h"
00021 #include "llvm/Support/ErrorOr.h"
00022 
00023 namespace llvm {
00024 template <typename T> class ArrayRef;
00025 
00026 namespace object {
00027 class ImportDirectoryEntryRef;
00028 class DelayImportDirectoryEntryRef;
00029 class ExportDirectoryEntryRef;
00030 class ImportedSymbolRef;
00031 typedef content_iterator<ImportDirectoryEntryRef> import_directory_iterator;
00032 typedef content_iterator<DelayImportDirectoryEntryRef>
00033     delay_import_directory_iterator;
00034 typedef content_iterator<ExportDirectoryEntryRef> export_directory_iterator;
00035 typedef content_iterator<ImportedSymbolRef> imported_symbol_iterator;
00036 
00037 /// The DOS compatible header at the front of all PE/COFF executables.
00038 struct dos_header {
00039   support::ulittle16_t Magic;
00040   support::ulittle16_t UsedBytesInTheLastPage;
00041   support::ulittle16_t FileSizeInPages;
00042   support::ulittle16_t NumberOfRelocationItems;
00043   support::ulittle16_t HeaderSizeInParagraphs;
00044   support::ulittle16_t MinimumExtraParagraphs;
00045   support::ulittle16_t MaximumExtraParagraphs;
00046   support::ulittle16_t InitialRelativeSS;
00047   support::ulittle16_t InitialSP;
00048   support::ulittle16_t Checksum;
00049   support::ulittle16_t InitialIP;
00050   support::ulittle16_t InitialRelativeCS;
00051   support::ulittle16_t AddressOfRelocationTable;
00052   support::ulittle16_t OverlayNumber;
00053   support::ulittle16_t Reserved[4];
00054   support::ulittle16_t OEMid;
00055   support::ulittle16_t OEMinfo;
00056   support::ulittle16_t Reserved2[10];
00057   support::ulittle32_t AddressOfNewExeHeader;
00058 };
00059 
00060 struct coff_file_header {
00061   support::ulittle16_t Machine;
00062   support::ulittle16_t NumberOfSections;
00063   support::ulittle32_t TimeDateStamp;
00064   support::ulittle32_t PointerToSymbolTable;
00065   support::ulittle32_t NumberOfSymbols;
00066   support::ulittle16_t SizeOfOptionalHeader;
00067   support::ulittle16_t Characteristics;
00068 
00069   bool isImportLibrary() const { return NumberOfSections == 0xffff; }
00070 };
00071 
00072 struct coff_bigobj_file_header {
00073   support::ulittle16_t Sig1;
00074   support::ulittle16_t Sig2;
00075   support::ulittle16_t Version;
00076   support::ulittle16_t Machine;
00077   support::ulittle32_t TimeDateStamp;
00078   uint8_t              UUID[16];
00079   support::ulittle32_t unused1;
00080   support::ulittle32_t unused2;
00081   support::ulittle32_t unused3;
00082   support::ulittle32_t unused4;
00083   support::ulittle32_t NumberOfSections;
00084   support::ulittle32_t PointerToSymbolTable;
00085   support::ulittle32_t NumberOfSymbols;
00086 };
00087 
00088 /// The 32-bit PE header that follows the COFF header.
00089 struct pe32_header {
00090   support::ulittle16_t Magic;
00091   uint8_t MajorLinkerVersion;
00092   uint8_t MinorLinkerVersion;
00093   support::ulittle32_t SizeOfCode;
00094   support::ulittle32_t SizeOfInitializedData;
00095   support::ulittle32_t SizeOfUninitializedData;
00096   support::ulittle32_t AddressOfEntryPoint;
00097   support::ulittle32_t BaseOfCode;
00098   support::ulittle32_t BaseOfData;
00099   support::ulittle32_t ImageBase;
00100   support::ulittle32_t SectionAlignment;
00101   support::ulittle32_t FileAlignment;
00102   support::ulittle16_t MajorOperatingSystemVersion;
00103   support::ulittle16_t MinorOperatingSystemVersion;
00104   support::ulittle16_t MajorImageVersion;
00105   support::ulittle16_t MinorImageVersion;
00106   support::ulittle16_t MajorSubsystemVersion;
00107   support::ulittle16_t MinorSubsystemVersion;
00108   support::ulittle32_t Win32VersionValue;
00109   support::ulittle32_t SizeOfImage;
00110   support::ulittle32_t SizeOfHeaders;
00111   support::ulittle32_t CheckSum;
00112   support::ulittle16_t Subsystem;
00113   support::ulittle16_t DLLCharacteristics;
00114   support::ulittle32_t SizeOfStackReserve;
00115   support::ulittle32_t SizeOfStackCommit;
00116   support::ulittle32_t SizeOfHeapReserve;
00117   support::ulittle32_t SizeOfHeapCommit;
00118   support::ulittle32_t LoaderFlags;
00119   support::ulittle32_t NumberOfRvaAndSize;
00120 };
00121 
00122 /// The 64-bit PE header that follows the COFF header.
00123 struct pe32plus_header {
00124   support::ulittle16_t Magic;
00125   uint8_t MajorLinkerVersion;
00126   uint8_t MinorLinkerVersion;
00127   support::ulittle32_t SizeOfCode;
00128   support::ulittle32_t SizeOfInitializedData;
00129   support::ulittle32_t SizeOfUninitializedData;
00130   support::ulittle32_t AddressOfEntryPoint;
00131   support::ulittle32_t BaseOfCode;
00132   support::ulittle64_t ImageBase;
00133   support::ulittle32_t SectionAlignment;
00134   support::ulittle32_t FileAlignment;
00135   support::ulittle16_t MajorOperatingSystemVersion;
00136   support::ulittle16_t MinorOperatingSystemVersion;
00137   support::ulittle16_t MajorImageVersion;
00138   support::ulittle16_t MinorImageVersion;
00139   support::ulittle16_t MajorSubsystemVersion;
00140   support::ulittle16_t MinorSubsystemVersion;
00141   support::ulittle32_t Win32VersionValue;
00142   support::ulittle32_t SizeOfImage;
00143   support::ulittle32_t SizeOfHeaders;
00144   support::ulittle32_t CheckSum;
00145   support::ulittle16_t Subsystem;
00146   support::ulittle16_t DLLCharacteristics;
00147   support::ulittle64_t SizeOfStackReserve;
00148   support::ulittle64_t SizeOfStackCommit;
00149   support::ulittle64_t SizeOfHeapReserve;
00150   support::ulittle64_t SizeOfHeapCommit;
00151   support::ulittle32_t LoaderFlags;
00152   support::ulittle32_t NumberOfRvaAndSize;
00153 };
00154 
00155 struct data_directory {
00156   support::ulittle32_t RelativeVirtualAddress;
00157   support::ulittle32_t Size;
00158 };
00159 
00160 struct import_directory_table_entry {
00161   support::ulittle32_t ImportLookupTableRVA;
00162   support::ulittle32_t TimeDateStamp;
00163   support::ulittle32_t ForwarderChain;
00164   support::ulittle32_t NameRVA;
00165   support::ulittle32_t ImportAddressTableRVA;
00166 };
00167 
00168 template <typename IntTy>
00169 struct import_lookup_table_entry {
00170   IntTy Data;
00171 
00172   bool isOrdinal() const { return Data < 0; }
00173 
00174   uint16_t getOrdinal() const {
00175     assert(isOrdinal() && "ILT entry is not an ordinal!");
00176     return Data & 0xFFFF;
00177   }
00178 
00179   uint32_t getHintNameRVA() const {
00180     assert(!isOrdinal() && "ILT entry is not a Hint/Name RVA!");
00181     return Data & 0xFFFFFFFF;
00182   }
00183 };
00184 
00185 typedef import_lookup_table_entry<support::little32_t>
00186     import_lookup_table_entry32;
00187 typedef import_lookup_table_entry<support::little64_t>
00188     import_lookup_table_entry64;
00189 
00190 struct delay_import_directory_table_entry {
00191   // dumpbin reports this field as "Characteristics" instead of "Attributes".
00192   support::ulittle32_t Attributes;
00193   support::ulittle32_t Name;
00194   support::ulittle32_t ModuleHandle;
00195   support::ulittle32_t DelayImportAddressTable;
00196   support::ulittle32_t DelayImportNameTable;
00197   support::ulittle32_t BoundDelayImportTable;
00198   support::ulittle32_t UnloadDelayImportTable;
00199   support::ulittle32_t TimeStamp;
00200 };
00201 
00202 struct export_directory_table_entry {
00203   support::ulittle32_t ExportFlags;
00204   support::ulittle32_t TimeDateStamp;
00205   support::ulittle16_t MajorVersion;
00206   support::ulittle16_t MinorVersion;
00207   support::ulittle32_t NameRVA;
00208   support::ulittle32_t OrdinalBase;
00209   support::ulittle32_t AddressTableEntries;
00210   support::ulittle32_t NumberOfNamePointers;
00211   support::ulittle32_t ExportAddressTableRVA;
00212   support::ulittle32_t NamePointerRVA;
00213   support::ulittle32_t OrdinalTableRVA;
00214 };
00215 
00216 union export_address_table_entry {
00217   support::ulittle32_t ExportRVA;
00218   support::ulittle32_t ForwarderRVA;
00219 };
00220 
00221 typedef support::ulittle32_t export_name_pointer_table_entry;
00222 typedef support::ulittle16_t export_ordinal_table_entry;
00223 
00224 struct StringTableOffset {
00225   support::ulittle32_t Zeroes;
00226   support::ulittle32_t Offset;
00227 };
00228 
00229 template <typename SectionNumberType>
00230 struct coff_symbol {
00231   union {
00232     char ShortName[COFF::NameSize];
00233     StringTableOffset Offset;
00234   } Name;
00235 
00236   support::ulittle32_t Value;
00237   SectionNumberType SectionNumber;
00238 
00239   support::ulittle16_t Type;
00240 
00241   uint8_t StorageClass;
00242   uint8_t NumberOfAuxSymbols;
00243 };
00244 
00245 typedef coff_symbol<support::ulittle16_t> coff_symbol16;
00246 typedef coff_symbol<support::ulittle32_t> coff_symbol32;
00247 
00248 class COFFSymbolRef {
00249 public:
00250   COFFSymbolRef(const coff_symbol16 *CS) : CS16(CS), CS32(nullptr) {}
00251   COFFSymbolRef(const coff_symbol32 *CS) : CS16(nullptr), CS32(CS) {}
00252   COFFSymbolRef() : CS16(nullptr), CS32(nullptr) {}
00253 
00254   const void *getRawPtr() const {
00255     return CS16 ? static_cast<const void *>(CS16) : CS32;
00256   }
00257 
00258   friend bool operator<(COFFSymbolRef A, COFFSymbolRef B) {
00259     return A.getRawPtr() < B.getRawPtr();
00260   }
00261 
00262   bool isBigObj() const {
00263     if (CS16)
00264       return false;
00265     if (CS32)
00266       return true;
00267     llvm_unreachable("COFFSymbolRef points to nothing!");
00268   }
00269 
00270   const char *getShortName() const {
00271     return CS16 ? CS16->Name.ShortName : CS32->Name.ShortName;
00272   }
00273 
00274   const StringTableOffset &getStringTableOffset() const {
00275     return CS16 ? CS16->Name.Offset : CS32->Name.Offset;
00276   }
00277 
00278   uint32_t getValue() const { return CS16 ? CS16->Value : CS32->Value; }
00279 
00280   int32_t getSectionNumber() const {
00281     if (CS16) {
00282       // Reserved sections are returned as negative numbers.
00283       if (CS16->SectionNumber <= COFF::MaxNumberOfSections16)
00284         return CS16->SectionNumber;
00285       return static_cast<int16_t>(CS16->SectionNumber);
00286     }
00287     return static_cast<int32_t>(CS32->SectionNumber);
00288   }
00289 
00290   uint16_t getType() const { return CS16 ? CS16->Type : CS32->Type; }
00291 
00292   uint8_t getStorageClass() const {
00293     return CS16 ? CS16->StorageClass : CS32->StorageClass;
00294   }
00295 
00296   uint8_t getNumberOfAuxSymbols() const {
00297     return CS16 ? CS16->NumberOfAuxSymbols : CS32->NumberOfAuxSymbols;
00298   }
00299 
00300   uint8_t getBaseType() const { return getType() & 0x0F; }
00301 
00302   uint8_t getComplexType() const { return (getType() & 0xF0) >> 4; }
00303 
00304   bool isFunctionDefinition() const {
00305     return getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
00306            getBaseType() == COFF::IMAGE_SYM_TYPE_NULL &&
00307            getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION &&
00308            !COFF::isReservedSectionNumber(getSectionNumber());
00309   }
00310 
00311   bool isFunctionLineInfo() const {
00312     return getStorageClass() == COFF::IMAGE_SYM_CLASS_FUNCTION;
00313   }
00314 
00315   bool isWeakExternal() const {
00316     return getStorageClass() == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL ||
00317            (getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
00318             getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED && getValue() == 0);
00319   }
00320 
00321   bool isFileRecord() const {
00322     return getStorageClass() == COFF::IMAGE_SYM_CLASS_FILE;
00323   }
00324 
00325   bool isSectionDefinition() const {
00326     // C++/CLI creates external ABS symbols for non-const appdomain globals.
00327     // These are also followed by an auxiliary section definition.
00328     bool isAppdomainGlobal =
00329         getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
00330         getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE;
00331     bool isOrdinarySection = getStorageClass() == COFF::IMAGE_SYM_CLASS_STATIC;
00332     return isAppdomainGlobal || isOrdinarySection;
00333   }
00334 
00335   bool isCLRToken() const {
00336     return getStorageClass() == COFF::IMAGE_SYM_CLASS_CLR_TOKEN;
00337   }
00338 
00339 private:
00340   const coff_symbol16 *CS16;
00341   const coff_symbol32 *CS32;
00342 };
00343 
00344 struct coff_section {
00345   char Name[COFF::NameSize];
00346   support::ulittle32_t VirtualSize;
00347   support::ulittle32_t VirtualAddress;
00348   support::ulittle32_t SizeOfRawData;
00349   support::ulittle32_t PointerToRawData;
00350   support::ulittle32_t PointerToRelocations;
00351   support::ulittle32_t PointerToLinenumbers;
00352   support::ulittle16_t NumberOfRelocations;
00353   support::ulittle16_t NumberOfLinenumbers;
00354   support::ulittle32_t Characteristics;
00355 
00356   // Returns true if the actual number of relocations is stored in
00357   // VirtualAddress field of the first relocation table entry.
00358   bool hasExtendedRelocations() const {
00359     return Characteristics & COFF::IMAGE_SCN_LNK_NRELOC_OVFL &&
00360         NumberOfRelocations == UINT16_MAX;
00361   };
00362 };
00363 
00364 struct coff_relocation {
00365   support::ulittle32_t VirtualAddress;
00366   support::ulittle32_t SymbolTableIndex;
00367   support::ulittle16_t Type;
00368 };
00369 
00370 struct coff_aux_function_definition {
00371   support::ulittle32_t TagIndex;
00372   support::ulittle32_t TotalSize;
00373   support::ulittle32_t PointerToLinenumber;
00374   support::ulittle32_t PointerToNextFunction;
00375 };
00376 
00377 struct coff_aux_bf_and_ef_symbol {
00378   char Unused1[4];
00379   support::ulittle16_t Linenumber;
00380   char Unused2[6];
00381   support::ulittle32_t PointerToNextFunction;
00382 };
00383 
00384 struct coff_aux_weak_external {
00385   support::ulittle32_t TagIndex;
00386   support::ulittle32_t Characteristics;
00387 };
00388 
00389 struct coff_aux_section_definition {
00390   support::ulittle32_t Length;
00391   support::ulittle16_t NumberOfRelocations;
00392   support::ulittle16_t NumberOfLinenumbers;
00393   support::ulittle32_t CheckSum;
00394   support::ulittle16_t NumberLowPart;
00395   uint8_t              Selection;
00396   uint8_t              Unused;
00397   support::ulittle16_t NumberHighPart;
00398   int32_t getNumber(bool IsBigObj) const {
00399     uint32_t Number = static_cast<uint32_t>(NumberLowPart);
00400     if (IsBigObj)
00401       Number |= static_cast<uint32_t>(NumberHighPart) << 16;
00402     return static_cast<int32_t>(Number);
00403   }
00404 };
00405 
00406 struct coff_aux_clr_token {
00407   uint8_t              AuxType;
00408   uint8_t              Reserved;
00409   support::ulittle32_t SymbolTableIndex;
00410 };
00411 
00412 struct coff_load_configuration32 {
00413   support::ulittle32_t Characteristics;
00414   support::ulittle32_t TimeDateStamp;
00415   support::ulittle16_t MajorVersion;
00416   support::ulittle16_t MinorVersion;
00417   support::ulittle32_t GlobalFlagsClear;
00418   support::ulittle32_t GlobalFlagsSet;
00419   support::ulittle32_t CriticalSectionDefaultTimeout;
00420   support::ulittle32_t DeCommitFreeBlockThreshold;
00421   support::ulittle32_t DeCommitTotalFreeThreshold;
00422   support::ulittle32_t LockPrefixTable;
00423   support::ulittle32_t MaximumAllocationSize;
00424   support::ulittle32_t VirtualMemoryThreshold;
00425   support::ulittle32_t ProcessAffinityMask;
00426   support::ulittle32_t ProcessHeapFlags;
00427   support::ulittle16_t CSDVersion;
00428   support::ulittle16_t Reserved;
00429   support::ulittle32_t EditList;
00430   support::ulittle32_t SecurityCookie;
00431   support::ulittle32_t SEHandlerTable;
00432   support::ulittle32_t SEHandlerCount;
00433 };
00434 
00435 struct coff_runtime_function_x64 {
00436   support::ulittle32_t BeginAddress;
00437   support::ulittle32_t EndAddress;
00438   support::ulittle32_t UnwindInformation;
00439 };
00440 
00441 class COFFObjectFile : public ObjectFile {
00442 private:
00443   friend class ImportDirectoryEntryRef;
00444   friend class ExportDirectoryEntryRef;
00445   const coff_file_header *COFFHeader;
00446   const coff_bigobj_file_header *COFFBigObjHeader;
00447   const pe32_header *PE32Header;
00448   const pe32plus_header *PE32PlusHeader;
00449   const data_directory *DataDirectory;
00450   const coff_section *SectionTable;
00451   const coff_symbol16 *SymbolTable16;
00452   const coff_symbol32 *SymbolTable32;
00453   const char *StringTable;
00454   uint32_t StringTableSize;
00455   const import_directory_table_entry *ImportDirectory;
00456   uint32_t NumberOfImportDirectory;
00457   const delay_import_directory_table_entry *DelayImportDirectory;
00458   uint32_t NumberOfDelayImportDirectory;
00459   const export_directory_table_entry *ExportDirectory;
00460 
00461   std::error_code getString(uint32_t offset, StringRef &Res) const;
00462 
00463   template <typename coff_symbol_type>
00464   const coff_symbol_type *toSymb(DataRefImpl Symb) const;
00465   const coff_section *toSec(DataRefImpl Sec) const;
00466   const coff_relocation *toRel(DataRefImpl Rel) const;
00467 
00468   std::error_code initSymbolTablePtr();
00469   std::error_code initImportTablePtr();
00470   std::error_code initDelayImportTablePtr();
00471   std::error_code initExportTablePtr();
00472 
00473 public:
00474   uintptr_t getSymbolTable() const {
00475     if (SymbolTable16)
00476       return reinterpret_cast<uintptr_t>(SymbolTable16);
00477     if (SymbolTable32)
00478       return reinterpret_cast<uintptr_t>(SymbolTable32);
00479     return uintptr_t(0);
00480   }
00481   uint16_t getMachine() const {
00482     if (COFFHeader)
00483       return COFFHeader->Machine;
00484     if (COFFBigObjHeader)
00485       return COFFBigObjHeader->Machine;
00486     llvm_unreachable("no COFF header!");
00487   }
00488   uint16_t getSizeOfOptionalHeader() const {
00489     if (COFFHeader)
00490       return COFFHeader->SizeOfOptionalHeader;
00491     // bigobj doesn't have this field.
00492     if (COFFBigObjHeader)
00493       return 0;
00494     llvm_unreachable("no COFF header!");
00495   }
00496   uint16_t getCharacteristics() const {
00497     if (COFFHeader)
00498       return COFFHeader->Characteristics;
00499     // bigobj doesn't have characteristics to speak of,
00500     // editbin will silently lie to you if you attempt to set any.
00501     if (COFFBigObjHeader)
00502       return 0;
00503     llvm_unreachable("no COFF header!");
00504   }
00505   uint32_t getTimeDateStamp() const {
00506     if (COFFHeader)
00507       return COFFHeader->TimeDateStamp;
00508     if (COFFBigObjHeader)
00509       return COFFBigObjHeader->TimeDateStamp;
00510     llvm_unreachable("no COFF header!");
00511   }
00512   uint32_t getNumberOfSections() const {
00513     if (COFFHeader)
00514       return COFFHeader->NumberOfSections;
00515     if (COFFBigObjHeader)
00516       return COFFBigObjHeader->NumberOfSections;
00517     llvm_unreachable("no COFF header!");
00518   }
00519   uint32_t getPointerToSymbolTable() const {
00520     if (COFFHeader)
00521       return COFFHeader->PointerToSymbolTable;
00522     if (COFFBigObjHeader)
00523       return COFFBigObjHeader->PointerToSymbolTable;
00524     llvm_unreachable("no COFF header!");
00525   }
00526   uint32_t getNumberOfSymbols() const {
00527     if (COFFHeader)
00528       return COFFHeader->NumberOfSymbols;
00529     if (COFFBigObjHeader)
00530       return COFFBigObjHeader->NumberOfSymbols;
00531     llvm_unreachable("no COFF header!");
00532   }
00533 protected:
00534   void moveSymbolNext(DataRefImpl &Symb) const override;
00535   std::error_code getSymbolName(DataRefImpl Symb,
00536                                 StringRef &Res) const override;
00537   std::error_code getSymbolAddress(DataRefImpl Symb,
00538                                    uint64_t &Res) const override;
00539   std::error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
00540   uint32_t getSymbolFlags(DataRefImpl Symb) const override;
00541   std::error_code getSymbolType(DataRefImpl Symb,
00542                                 SymbolRef::Type &Res) const override;
00543   std::error_code getSymbolSection(DataRefImpl Symb,
00544                                    section_iterator &Res) const override;
00545   void moveSectionNext(DataRefImpl &Sec) const override;
00546   std::error_code getSectionName(DataRefImpl Sec,
00547                                  StringRef &Res) const override;
00548   uint64_t getSectionAddress(DataRefImpl Sec) const override;
00549   uint64_t getSectionSize(DataRefImpl Sec) const override;
00550   std::error_code getSectionContents(DataRefImpl Sec,
00551                                      StringRef &Res) const override;
00552   uint64_t getSectionAlignment(DataRefImpl Sec) const override;
00553   bool isSectionText(DataRefImpl Sec) const override;
00554   bool isSectionData(DataRefImpl Sec) const override;
00555   bool isSectionBSS(DataRefImpl Sec) const override;
00556   bool isSectionVirtual(DataRefImpl Sec) const override;
00557   bool isSectionZeroInit(DataRefImpl Sec) const override;
00558   bool isSectionReadOnlyData(DataRefImpl Sec) const override;
00559   bool isSectionRequiredForExecution(DataRefImpl Sec) const override;
00560   bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
00561   relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
00562   relocation_iterator section_rel_end(DataRefImpl Sec) const override;
00563 
00564   void moveRelocationNext(DataRefImpl &Rel) const override;
00565   std::error_code getRelocationAddress(DataRefImpl Rel,
00566                                        uint64_t &Res) const override;
00567   std::error_code getRelocationOffset(DataRefImpl Rel,
00568                                       uint64_t &Res) const override;
00569   symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
00570   std::error_code getRelocationType(DataRefImpl Rel,
00571                                     uint64_t &Res) const override;
00572   std::error_code
00573   getRelocationTypeName(DataRefImpl Rel,
00574                         SmallVectorImpl<char> &Result) const override;
00575   std::error_code
00576   getRelocationValueString(DataRefImpl Rel,
00577                            SmallVectorImpl<char> &Result) const override;
00578 
00579 public:
00580   COFFObjectFile(MemoryBufferRef Object, std::error_code &EC);
00581   basic_symbol_iterator symbol_begin_impl() const override;
00582   basic_symbol_iterator symbol_end_impl() const override;
00583   section_iterator section_begin() const override;
00584   section_iterator section_end() const override;
00585 
00586   const coff_section *getCOFFSection(const SectionRef &Section) const;
00587   COFFSymbolRef getCOFFSymbol(const DataRefImpl &Ref) const;
00588   COFFSymbolRef getCOFFSymbol(const SymbolRef &Symbol) const;
00589   const coff_relocation *getCOFFRelocation(const RelocationRef &Reloc) const;
00590 
00591   uint8_t getBytesInAddress() const override;
00592   StringRef getFileFormatName() const override;
00593   unsigned getArch() const override;
00594 
00595   import_directory_iterator import_directory_begin() const;
00596   import_directory_iterator import_directory_end() const;
00597   delay_import_directory_iterator delay_import_directory_begin() const;
00598   delay_import_directory_iterator delay_import_directory_end() const;
00599   export_directory_iterator export_directory_begin() const;
00600   export_directory_iterator export_directory_end() const;
00601 
00602   iterator_range<import_directory_iterator> import_directories() const;
00603   iterator_range<delay_import_directory_iterator>
00604       delay_import_directories() const;
00605   iterator_range<export_directory_iterator> export_directories() const;
00606 
00607   std::error_code getPE32Header(const pe32_header *&Res) const;
00608   std::error_code getPE32PlusHeader(const pe32plus_header *&Res) const;
00609   std::error_code getDataDirectory(uint32_t index,
00610                                    const data_directory *&Res) const;
00611   std::error_code getSection(int32_t index, const coff_section *&Res) const;
00612   template <typename coff_symbol_type>
00613   std::error_code getSymbol(uint32_t Index,
00614                             const coff_symbol_type *&Res) const {
00615     if (Index >= getNumberOfSymbols())
00616       return object_error::parse_failed;
00617 
00618     Res = reinterpret_cast<coff_symbol_type *>(getSymbolTable()) + Index;
00619     return object_error::success;
00620   }
00621   ErrorOr<COFFSymbolRef> getSymbol(uint32_t index) const {
00622     if (SymbolTable16) {
00623       const coff_symbol16 *Symb = nullptr;
00624       if (std::error_code EC = getSymbol(index, Symb))
00625         return EC;
00626       return COFFSymbolRef(Symb);
00627     }
00628     if (SymbolTable32) {
00629       const coff_symbol32 *Symb = nullptr;
00630       if (std::error_code EC = getSymbol(index, Symb))
00631         return EC;
00632       return COFFSymbolRef(Symb);
00633     }
00634     llvm_unreachable("no symbol table pointer!");
00635   }
00636   template <typename T>
00637   std::error_code getAuxSymbol(uint32_t index, const T *&Res) const {
00638     ErrorOr<COFFSymbolRef> s = getSymbol(index);
00639     if (std::error_code EC = s.getError())
00640       return EC;
00641     Res = reinterpret_cast<const T *>(s->getRawPtr());
00642     return object_error::success;
00643   }
00644   std::error_code getSymbolName(COFFSymbolRef Symbol, StringRef &Res) const;
00645 
00646   ArrayRef<uint8_t> getSymbolAuxData(COFFSymbolRef Symbol) const;
00647 
00648   size_t getSymbolTableEntrySize() const {
00649     if (COFFHeader)
00650       return sizeof(coff_symbol16);
00651     if (COFFBigObjHeader)
00652       return sizeof(coff_symbol32);
00653     llvm_unreachable("null symbol table pointer!");
00654   }
00655 
00656   std::error_code getSectionName(const coff_section *Sec, StringRef &Res) const;
00657   uint64_t getSectionSize(const coff_section *Sec) const;
00658   std::error_code getSectionContents(const coff_section *Sec,
00659                                      ArrayRef<uint8_t> &Res) const;
00660 
00661   std::error_code getVaPtr(uint64_t VA, uintptr_t &Res) const;
00662   std::error_code getRvaPtr(uint32_t Rva, uintptr_t &Res) const;
00663   std::error_code getHintName(uint32_t Rva, uint16_t &Hint,
00664                               StringRef &Name) const;
00665 
00666   bool isRelocatableObject() const override;
00667 
00668   static inline bool classof(const Binary *v) { return v->isCOFF(); }
00669 };
00670 
00671 // The iterator for the import directory table.
00672 class ImportDirectoryEntryRef {
00673 public:
00674   ImportDirectoryEntryRef() : OwningObject(nullptr) {}
00675   ImportDirectoryEntryRef(const import_directory_table_entry *Table, uint32_t I,
00676                           const COFFObjectFile *Owner)
00677       : ImportTable(Table), Index(I), OwningObject(Owner) {}
00678 
00679   bool operator==(const ImportDirectoryEntryRef &Other) const;
00680   void moveNext();
00681 
00682   imported_symbol_iterator imported_symbol_begin() const;
00683   imported_symbol_iterator imported_symbol_end() const;
00684   iterator_range<imported_symbol_iterator> imported_symbols() const;
00685 
00686   std::error_code getName(StringRef &Result) const;
00687   std::error_code getImportLookupTableRVA(uint32_t &Result) const;
00688   std::error_code getImportAddressTableRVA(uint32_t &Result) const;
00689 
00690   std::error_code
00691   getImportTableEntry(const import_directory_table_entry *&Result) const;
00692 
00693   std::error_code
00694   getImportLookupEntry(const import_lookup_table_entry32 *&Result) const;
00695 
00696 private:
00697   const import_directory_table_entry *ImportTable;
00698   uint32_t Index;
00699   const COFFObjectFile *OwningObject;
00700 };
00701 
00702 class DelayImportDirectoryEntryRef {
00703 public:
00704   DelayImportDirectoryEntryRef() : OwningObject(nullptr) {}
00705   DelayImportDirectoryEntryRef(const delay_import_directory_table_entry *T,
00706                                uint32_t I, const COFFObjectFile *Owner)
00707       : Table(T), Index(I), OwningObject(Owner) {}
00708 
00709   bool operator==(const DelayImportDirectoryEntryRef &Other) const;
00710   void moveNext();
00711 
00712   imported_symbol_iterator imported_symbol_begin() const;
00713   imported_symbol_iterator imported_symbol_end() const;
00714   iterator_range<imported_symbol_iterator> imported_symbols() const;
00715 
00716   std::error_code getName(StringRef &Result) const;
00717   std::error_code getDelayImportTable(
00718       const delay_import_directory_table_entry *&Result) const;
00719 
00720 private:
00721   const delay_import_directory_table_entry *Table;
00722   uint32_t Index;
00723   const COFFObjectFile *OwningObject;
00724 };
00725 
00726 // The iterator for the export directory table entry.
00727 class ExportDirectoryEntryRef {
00728 public:
00729   ExportDirectoryEntryRef() : OwningObject(nullptr) {}
00730   ExportDirectoryEntryRef(const export_directory_table_entry *Table, uint32_t I,
00731                           const COFFObjectFile *Owner)
00732       : ExportTable(Table), Index(I), OwningObject(Owner) {}
00733 
00734   bool operator==(const ExportDirectoryEntryRef &Other) const;
00735   void moveNext();
00736 
00737   std::error_code getDllName(StringRef &Result) const;
00738   std::error_code getOrdinalBase(uint32_t &Result) const;
00739   std::error_code getOrdinal(uint32_t &Result) const;
00740   std::error_code getExportRVA(uint32_t &Result) const;
00741   std::error_code getSymbolName(StringRef &Result) const;
00742 
00743 private:
00744   const export_directory_table_entry *ExportTable;
00745   uint32_t Index;
00746   const COFFObjectFile *OwningObject;
00747 };
00748 
00749 class ImportedSymbolRef {
00750 public:
00751   ImportedSymbolRef() : OwningObject(nullptr) {}
00752   ImportedSymbolRef(const import_lookup_table_entry32 *Entry, uint32_t I,
00753                     const COFFObjectFile *Owner)
00754       : Entry32(Entry), Entry64(nullptr), Index(I), OwningObject(Owner) {}
00755   ImportedSymbolRef(const import_lookup_table_entry64 *Entry, uint32_t I,
00756                     const COFFObjectFile *Owner)
00757       : Entry32(nullptr), Entry64(Entry), Index(I), OwningObject(Owner) {}
00758 
00759   bool operator==(const ImportedSymbolRef &Other) const;
00760   void moveNext();
00761 
00762   std::error_code getSymbolName(StringRef &Result) const;
00763   std::error_code getOrdinal(uint16_t &Result) const;
00764 
00765 private:
00766   const import_lookup_table_entry32 *Entry32;
00767   const import_lookup_table_entry64 *Entry64;
00768   uint32_t Index;
00769   const COFFObjectFile *OwningObject;
00770 };
00771 } // end namespace object
00772 } // end namespace llvm
00773 
00774 #endif