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