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 isExternal() const {
00305     return getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL;
00306   }
00307 
00308   bool isCommon() const {
00309     return isExternal() && getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED &&
00310            getValue() != 0;
00311   }
00312 
00313   bool isUndefined() const {
00314     return isExternal() && getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED &&
00315            getValue() == 0;
00316   }
00317 
00318   bool isWeakExternal() const {
00319     return getStorageClass() == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
00320   }
00321 
00322   bool isFunctionDefinition() const {
00323     return isExternal() && getBaseType() == COFF::IMAGE_SYM_TYPE_NULL &&
00324            getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION &&
00325            !COFF::isReservedSectionNumber(getSectionNumber());
00326   }
00327 
00328   bool isFunctionLineInfo() const {
00329     return getStorageClass() == COFF::IMAGE_SYM_CLASS_FUNCTION;
00330   }
00331 
00332   bool isAnyUndefined() const {
00333     return isUndefined() || isWeakExternal();
00334   }
00335 
00336   bool isFileRecord() const {
00337     return getStorageClass() == COFF::IMAGE_SYM_CLASS_FILE;
00338   }
00339 
00340   bool isSectionDefinition() const {
00341     // C++/CLI creates external ABS symbols for non-const appdomain globals.
00342     // These are also followed by an auxiliary section definition.
00343     bool isAppdomainGlobal =
00344         getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
00345         getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE;
00346     bool isOrdinarySection = getStorageClass() == COFF::IMAGE_SYM_CLASS_STATIC;
00347     if (!getNumberOfAuxSymbols())
00348       return false;
00349     return isAppdomainGlobal || isOrdinarySection;
00350   }
00351 
00352   bool isCLRToken() const {
00353     return getStorageClass() == COFF::IMAGE_SYM_CLASS_CLR_TOKEN;
00354   }
00355 
00356 private:
00357   const coff_symbol16 *CS16;
00358   const coff_symbol32 *CS32;
00359 };
00360 
00361 struct coff_section {
00362   char Name[COFF::NameSize];
00363   support::ulittle32_t VirtualSize;
00364   support::ulittle32_t VirtualAddress;
00365   support::ulittle32_t SizeOfRawData;
00366   support::ulittle32_t PointerToRawData;
00367   support::ulittle32_t PointerToRelocations;
00368   support::ulittle32_t PointerToLinenumbers;
00369   support::ulittle16_t NumberOfRelocations;
00370   support::ulittle16_t NumberOfLinenumbers;
00371   support::ulittle32_t Characteristics;
00372 
00373   // Returns true if the actual number of relocations is stored in
00374   // VirtualAddress field of the first relocation table entry.
00375   bool hasExtendedRelocations() const {
00376     return Characteristics & COFF::IMAGE_SCN_LNK_NRELOC_OVFL &&
00377         NumberOfRelocations == UINT16_MAX;
00378   };
00379 };
00380 
00381 struct coff_relocation {
00382   support::ulittle32_t VirtualAddress;
00383   support::ulittle32_t SymbolTableIndex;
00384   support::ulittle16_t Type;
00385 };
00386 
00387 struct coff_aux_function_definition {
00388   support::ulittle32_t TagIndex;
00389   support::ulittle32_t TotalSize;
00390   support::ulittle32_t PointerToLinenumber;
00391   support::ulittle32_t PointerToNextFunction;
00392 };
00393 
00394 struct coff_aux_bf_and_ef_symbol {
00395   char Unused1[4];
00396   support::ulittle16_t Linenumber;
00397   char Unused2[6];
00398   support::ulittle32_t PointerToNextFunction;
00399 };
00400 
00401 struct coff_aux_weak_external {
00402   support::ulittle32_t TagIndex;
00403   support::ulittle32_t Characteristics;
00404 };
00405 
00406 struct coff_aux_section_definition {
00407   support::ulittle32_t Length;
00408   support::ulittle16_t NumberOfRelocations;
00409   support::ulittle16_t NumberOfLinenumbers;
00410   support::ulittle32_t CheckSum;
00411   support::ulittle16_t NumberLowPart;
00412   uint8_t              Selection;
00413   uint8_t              Unused;
00414   support::ulittle16_t NumberHighPart;
00415   int32_t getNumber(bool IsBigObj) const {
00416     uint32_t Number = static_cast<uint32_t>(NumberLowPart);
00417     if (IsBigObj)
00418       Number |= static_cast<uint32_t>(NumberHighPart) << 16;
00419     return static_cast<int32_t>(Number);
00420   }
00421 };
00422 
00423 struct coff_aux_clr_token {
00424   uint8_t              AuxType;
00425   uint8_t              Reserved;
00426   support::ulittle32_t SymbolTableIndex;
00427 };
00428 
00429 struct coff_load_configuration32 {
00430   support::ulittle32_t Characteristics;
00431   support::ulittle32_t TimeDateStamp;
00432   support::ulittle16_t MajorVersion;
00433   support::ulittle16_t MinorVersion;
00434   support::ulittle32_t GlobalFlagsClear;
00435   support::ulittle32_t GlobalFlagsSet;
00436   support::ulittle32_t CriticalSectionDefaultTimeout;
00437   support::ulittle32_t DeCommitFreeBlockThreshold;
00438   support::ulittle32_t DeCommitTotalFreeThreshold;
00439   support::ulittle32_t LockPrefixTable;
00440   support::ulittle32_t MaximumAllocationSize;
00441   support::ulittle32_t VirtualMemoryThreshold;
00442   support::ulittle32_t ProcessAffinityMask;
00443   support::ulittle32_t ProcessHeapFlags;
00444   support::ulittle16_t CSDVersion;
00445   support::ulittle16_t Reserved;
00446   support::ulittle32_t EditList;
00447   support::ulittle32_t SecurityCookie;
00448   support::ulittle32_t SEHandlerTable;
00449   support::ulittle32_t SEHandlerCount;
00450 };
00451 
00452 struct coff_runtime_function_x64 {
00453   support::ulittle32_t BeginAddress;
00454   support::ulittle32_t EndAddress;
00455   support::ulittle32_t UnwindInformation;
00456 };
00457 
00458 class COFFObjectFile : public ObjectFile {
00459 private:
00460   friend class ImportDirectoryEntryRef;
00461   friend class ExportDirectoryEntryRef;
00462   const coff_file_header *COFFHeader;
00463   const coff_bigobj_file_header *COFFBigObjHeader;
00464   const pe32_header *PE32Header;
00465   const pe32plus_header *PE32PlusHeader;
00466   const data_directory *DataDirectory;
00467   const coff_section *SectionTable;
00468   const coff_symbol16 *SymbolTable16;
00469   const coff_symbol32 *SymbolTable32;
00470   const char *StringTable;
00471   uint32_t StringTableSize;
00472   const import_directory_table_entry *ImportDirectory;
00473   uint32_t NumberOfImportDirectory;
00474   const delay_import_directory_table_entry *DelayImportDirectory;
00475   uint32_t NumberOfDelayImportDirectory;
00476   const export_directory_table_entry *ExportDirectory;
00477 
00478   std::error_code getString(uint32_t offset, StringRef &Res) const;
00479 
00480   template <typename coff_symbol_type>
00481   const coff_symbol_type *toSymb(DataRefImpl Symb) const;
00482   const coff_section *toSec(DataRefImpl Sec) const;
00483   const coff_relocation *toRel(DataRefImpl Rel) const;
00484 
00485   std::error_code initSymbolTablePtr();
00486   std::error_code initImportTablePtr();
00487   std::error_code initDelayImportTablePtr();
00488   std::error_code initExportTablePtr();
00489 
00490 public:
00491   uintptr_t getSymbolTable() const {
00492     if (SymbolTable16)
00493       return reinterpret_cast<uintptr_t>(SymbolTable16);
00494     if (SymbolTable32)
00495       return reinterpret_cast<uintptr_t>(SymbolTable32);
00496     return uintptr_t(0);
00497   }
00498   uint16_t getMachine() const {
00499     if (COFFHeader)
00500       return COFFHeader->Machine;
00501     if (COFFBigObjHeader)
00502       return COFFBigObjHeader->Machine;
00503     llvm_unreachable("no COFF header!");
00504   }
00505   uint16_t getSizeOfOptionalHeader() const {
00506     if (COFFHeader)
00507       return COFFHeader->SizeOfOptionalHeader;
00508     // bigobj doesn't have this field.
00509     if (COFFBigObjHeader)
00510       return 0;
00511     llvm_unreachable("no COFF header!");
00512   }
00513   uint16_t getCharacteristics() const {
00514     if (COFFHeader)
00515       return COFFHeader->Characteristics;
00516     // bigobj doesn't have characteristics to speak of,
00517     // editbin will silently lie to you if you attempt to set any.
00518     if (COFFBigObjHeader)
00519       return 0;
00520     llvm_unreachable("no COFF header!");
00521   }
00522   uint32_t getTimeDateStamp() const {
00523     if (COFFHeader)
00524       return COFFHeader->TimeDateStamp;
00525     if (COFFBigObjHeader)
00526       return COFFBigObjHeader->TimeDateStamp;
00527     llvm_unreachable("no COFF header!");
00528   }
00529   uint32_t getNumberOfSections() const {
00530     if (COFFHeader)
00531       return COFFHeader->NumberOfSections;
00532     if (COFFBigObjHeader)
00533       return COFFBigObjHeader->NumberOfSections;
00534     llvm_unreachable("no COFF header!");
00535   }
00536   uint32_t getPointerToSymbolTable() const {
00537     if (COFFHeader)
00538       return COFFHeader->PointerToSymbolTable;
00539     if (COFFBigObjHeader)
00540       return COFFBigObjHeader->PointerToSymbolTable;
00541     llvm_unreachable("no COFF header!");
00542   }
00543   uint32_t getNumberOfSymbols() const {
00544     if (COFFHeader)
00545       return COFFHeader->NumberOfSymbols;
00546     if (COFFBigObjHeader)
00547       return COFFBigObjHeader->NumberOfSymbols;
00548     llvm_unreachable("no COFF header!");
00549   }
00550 protected:
00551   void moveSymbolNext(DataRefImpl &Symb) const override;
00552   std::error_code getSymbolName(DataRefImpl Symb,
00553                                 StringRef &Res) const override;
00554   std::error_code getSymbolAddress(DataRefImpl Symb,
00555                                    uint64_t &Res) const override;
00556   std::error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
00557   uint32_t getSymbolFlags(DataRefImpl Symb) const override;
00558   std::error_code getSymbolType(DataRefImpl Symb,
00559                                 SymbolRef::Type &Res) const override;
00560   std::error_code getSymbolSection(DataRefImpl Symb,
00561                                    section_iterator &Res) const override;
00562   void moveSectionNext(DataRefImpl &Sec) const override;
00563   std::error_code getSectionName(DataRefImpl Sec,
00564                                  StringRef &Res) const override;
00565   uint64_t getSectionAddress(DataRefImpl Sec) const override;
00566   uint64_t getSectionSize(DataRefImpl Sec) const override;
00567   std::error_code getSectionContents(DataRefImpl Sec,
00568                                      StringRef &Res) const override;
00569   uint64_t getSectionAlignment(DataRefImpl Sec) const override;
00570   bool isSectionText(DataRefImpl Sec) const override;
00571   bool isSectionData(DataRefImpl Sec) const override;
00572   bool isSectionBSS(DataRefImpl Sec) const override;
00573   bool isSectionVirtual(DataRefImpl Sec) const override;
00574   bool isSectionZeroInit(DataRefImpl Sec) const override;
00575   bool isSectionReadOnlyData(DataRefImpl Sec) const override;
00576   bool isSectionRequiredForExecution(DataRefImpl Sec) const override;
00577   bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
00578   relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
00579   relocation_iterator section_rel_end(DataRefImpl Sec) const override;
00580 
00581   void moveRelocationNext(DataRefImpl &Rel) const override;
00582   std::error_code getRelocationAddress(DataRefImpl Rel,
00583                                        uint64_t &Res) const override;
00584   std::error_code getRelocationOffset(DataRefImpl Rel,
00585                                       uint64_t &Res) const override;
00586   symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
00587   std::error_code getRelocationType(DataRefImpl Rel,
00588                                     uint64_t &Res) const override;
00589   std::error_code
00590   getRelocationTypeName(DataRefImpl Rel,
00591                         SmallVectorImpl<char> &Result) const override;
00592   std::error_code
00593   getRelocationValueString(DataRefImpl Rel,
00594                            SmallVectorImpl<char> &Result) const override;
00595 
00596 public:
00597   COFFObjectFile(MemoryBufferRef Object, std::error_code &EC);
00598   basic_symbol_iterator symbol_begin_impl() const override;
00599   basic_symbol_iterator symbol_end_impl() const override;
00600   section_iterator section_begin() const override;
00601   section_iterator section_end() const override;
00602 
00603   const coff_section *getCOFFSection(const SectionRef &Section) const;
00604   COFFSymbolRef getCOFFSymbol(const DataRefImpl &Ref) const;
00605   COFFSymbolRef getCOFFSymbol(const SymbolRef &Symbol) const;
00606   const coff_relocation *getCOFFRelocation(const RelocationRef &Reloc) const;
00607 
00608   uint8_t getBytesInAddress() const override;
00609   StringRef getFileFormatName() const override;
00610   unsigned getArch() const override;
00611 
00612   import_directory_iterator import_directory_begin() const;
00613   import_directory_iterator import_directory_end() const;
00614   delay_import_directory_iterator delay_import_directory_begin() const;
00615   delay_import_directory_iterator delay_import_directory_end() const;
00616   export_directory_iterator export_directory_begin() const;
00617   export_directory_iterator export_directory_end() const;
00618 
00619   iterator_range<import_directory_iterator> import_directories() const;
00620   iterator_range<delay_import_directory_iterator>
00621       delay_import_directories() const;
00622   iterator_range<export_directory_iterator> export_directories() const;
00623 
00624   std::error_code getPE32Header(const pe32_header *&Res) const;
00625   std::error_code getPE32PlusHeader(const pe32plus_header *&Res) const;
00626   std::error_code getDataDirectory(uint32_t index,
00627                                    const data_directory *&Res) const;
00628   std::error_code getSection(int32_t index, const coff_section *&Res) const;
00629   template <typename coff_symbol_type>
00630   std::error_code getSymbol(uint32_t Index,
00631                             const coff_symbol_type *&Res) const {
00632     if (Index >= getNumberOfSymbols())
00633       return object_error::parse_failed;
00634 
00635     Res = reinterpret_cast<coff_symbol_type *>(getSymbolTable()) + Index;
00636     return object_error::success;
00637   }
00638   ErrorOr<COFFSymbolRef> getSymbol(uint32_t index) const {
00639     if (SymbolTable16) {
00640       const coff_symbol16 *Symb = nullptr;
00641       if (std::error_code EC = getSymbol(index, Symb))
00642         return EC;
00643       return COFFSymbolRef(Symb);
00644     }
00645     if (SymbolTable32) {
00646       const coff_symbol32 *Symb = nullptr;
00647       if (std::error_code EC = getSymbol(index, Symb))
00648         return EC;
00649       return COFFSymbolRef(Symb);
00650     }
00651     llvm_unreachable("no symbol table pointer!");
00652   }
00653   template <typename T>
00654   std::error_code getAuxSymbol(uint32_t index, const T *&Res) const {
00655     ErrorOr<COFFSymbolRef> s = getSymbol(index);
00656     if (std::error_code EC = s.getError())
00657       return EC;
00658     Res = reinterpret_cast<const T *>(s->getRawPtr());
00659     return object_error::success;
00660   }
00661   std::error_code getSymbolName(COFFSymbolRef Symbol, StringRef &Res) const;
00662 
00663   ArrayRef<uint8_t> getSymbolAuxData(COFFSymbolRef Symbol) const;
00664 
00665   size_t getSymbolTableEntrySize() const {
00666     if (COFFHeader)
00667       return sizeof(coff_symbol16);
00668     if (COFFBigObjHeader)
00669       return sizeof(coff_symbol32);
00670     llvm_unreachable("null symbol table pointer!");
00671   }
00672 
00673   std::error_code getSectionName(const coff_section *Sec, StringRef &Res) const;
00674   uint64_t getSectionSize(const coff_section *Sec) const;
00675   std::error_code getSectionContents(const coff_section *Sec,
00676                                      ArrayRef<uint8_t> &Res) const;
00677 
00678   std::error_code getVaPtr(uint64_t VA, uintptr_t &Res) const;
00679   std::error_code getRvaPtr(uint32_t Rva, uintptr_t &Res) const;
00680   std::error_code getHintName(uint32_t Rva, uint16_t &Hint,
00681                               StringRef &Name) const;
00682 
00683   bool isRelocatableObject() const override;
00684 
00685   static inline bool classof(const Binary *v) { return v->isCOFF(); }
00686 };
00687 
00688 // The iterator for the import directory table.
00689 class ImportDirectoryEntryRef {
00690 public:
00691   ImportDirectoryEntryRef() : OwningObject(nullptr) {}
00692   ImportDirectoryEntryRef(const import_directory_table_entry *Table, uint32_t I,
00693                           const COFFObjectFile *Owner)
00694       : ImportTable(Table), Index(I), OwningObject(Owner) {}
00695 
00696   bool operator==(const ImportDirectoryEntryRef &Other) const;
00697   void moveNext();
00698 
00699   imported_symbol_iterator imported_symbol_begin() const;
00700   imported_symbol_iterator imported_symbol_end() const;
00701   iterator_range<imported_symbol_iterator> imported_symbols() const;
00702 
00703   std::error_code getName(StringRef &Result) const;
00704   std::error_code getImportLookupTableRVA(uint32_t &Result) const;
00705   std::error_code getImportAddressTableRVA(uint32_t &Result) const;
00706 
00707   std::error_code
00708   getImportTableEntry(const import_directory_table_entry *&Result) const;
00709 
00710   std::error_code
00711   getImportLookupEntry(const import_lookup_table_entry32 *&Result) const;
00712 
00713 private:
00714   const import_directory_table_entry *ImportTable;
00715   uint32_t Index;
00716   const COFFObjectFile *OwningObject;
00717 };
00718 
00719 class DelayImportDirectoryEntryRef {
00720 public:
00721   DelayImportDirectoryEntryRef() : OwningObject(nullptr) {}
00722   DelayImportDirectoryEntryRef(const delay_import_directory_table_entry *T,
00723                                uint32_t I, const COFFObjectFile *Owner)
00724       : Table(T), Index(I), OwningObject(Owner) {}
00725 
00726   bool operator==(const DelayImportDirectoryEntryRef &Other) const;
00727   void moveNext();
00728 
00729   imported_symbol_iterator imported_symbol_begin() const;
00730   imported_symbol_iterator imported_symbol_end() const;
00731   iterator_range<imported_symbol_iterator> imported_symbols() const;
00732 
00733   std::error_code getName(StringRef &Result) const;
00734   std::error_code getDelayImportTable(
00735       const delay_import_directory_table_entry *&Result) const;
00736 
00737 private:
00738   const delay_import_directory_table_entry *Table;
00739   uint32_t Index;
00740   const COFFObjectFile *OwningObject;
00741 };
00742 
00743 // The iterator for the export directory table entry.
00744 class ExportDirectoryEntryRef {
00745 public:
00746   ExportDirectoryEntryRef() : OwningObject(nullptr) {}
00747   ExportDirectoryEntryRef(const export_directory_table_entry *Table, uint32_t I,
00748                           const COFFObjectFile *Owner)
00749       : ExportTable(Table), Index(I), OwningObject(Owner) {}
00750 
00751   bool operator==(const ExportDirectoryEntryRef &Other) const;
00752   void moveNext();
00753 
00754   std::error_code getDllName(StringRef &Result) const;
00755   std::error_code getOrdinalBase(uint32_t &Result) const;
00756   std::error_code getOrdinal(uint32_t &Result) const;
00757   std::error_code getExportRVA(uint32_t &Result) const;
00758   std::error_code getSymbolName(StringRef &Result) const;
00759 
00760 private:
00761   const export_directory_table_entry *ExportTable;
00762   uint32_t Index;
00763   const COFFObjectFile *OwningObject;
00764 };
00765 
00766 class ImportedSymbolRef {
00767 public:
00768   ImportedSymbolRef() : OwningObject(nullptr) {}
00769   ImportedSymbolRef(const import_lookup_table_entry32 *Entry, uint32_t I,
00770                     const COFFObjectFile *Owner)
00771       : Entry32(Entry), Entry64(nullptr), Index(I), OwningObject(Owner) {}
00772   ImportedSymbolRef(const import_lookup_table_entry64 *Entry, uint32_t I,
00773                     const COFFObjectFile *Owner)
00774       : Entry32(nullptr), Entry64(Entry), Index(I), OwningObject(Owner) {}
00775 
00776   bool operator==(const ImportedSymbolRef &Other) const;
00777   void moveNext();
00778 
00779   std::error_code getSymbolName(StringRef &Result) const;
00780   std::error_code getOrdinal(uint16_t &Result) const;
00781 
00782 private:
00783   const import_lookup_table_entry32 *Entry32;
00784   const import_lookup_table_entry64 *Entry64;
00785   uint32_t Index;
00786   const COFFObjectFile *OwningObject;
00787 };
00788 } // end namespace object
00789 } // end namespace llvm
00790 
00791 #endif