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