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