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/Object/ObjectFile.h"
00018 #include "llvm/Support/COFF.h"
00019 #include "llvm/Support/Endian.h"
00020 
00021 namespace llvm {
00022 template <typename T> class ArrayRef;
00023 
00024 namespace object {
00025 class ImportDirectoryEntryRef;
00026 class ExportDirectoryEntryRef;
00027 typedef content_iterator<ImportDirectoryEntryRef> import_directory_iterator;
00028 typedef content_iterator<ExportDirectoryEntryRef> export_directory_iterator;
00029 
00030 /// The DOS compatible header at the front of all PE/COFF executables.
00031 struct dos_header {
00032   support::ulittle16_t Magic;
00033   support::ulittle16_t UsedBytesInTheLastPage;
00034   support::ulittle16_t FileSizeInPages;
00035   support::ulittle16_t NumberOfRelocationItems;
00036   support::ulittle16_t HeaderSizeInParagraphs;
00037   support::ulittle16_t MinimumExtraParagraphs;
00038   support::ulittle16_t MaximumExtraParagraphs;
00039   support::ulittle16_t InitialRelativeSS;
00040   support::ulittle16_t InitialSP;
00041   support::ulittle16_t Checksum;
00042   support::ulittle16_t InitialIP;
00043   support::ulittle16_t InitialRelativeCS;
00044   support::ulittle16_t AddressOfRelocationTable;
00045   support::ulittle16_t OverlayNumber;
00046   support::ulittle16_t Reserved[4];
00047   support::ulittle16_t OEMid;
00048   support::ulittle16_t OEMinfo;
00049   support::ulittle16_t Reserved2[10];
00050   support::ulittle32_t AddressOfNewExeHeader;
00051 };
00052 
00053 struct coff_file_header {
00054   support::ulittle16_t Machine;
00055   support::ulittle16_t NumberOfSections;
00056   support::ulittle32_t TimeDateStamp;
00057   support::ulittle32_t PointerToSymbolTable;
00058   support::ulittle32_t NumberOfSymbols;
00059   support::ulittle16_t SizeOfOptionalHeader;
00060   support::ulittle16_t Characteristics;
00061 
00062   bool isImportLibrary() const { return NumberOfSections == 0xffff; }
00063 };
00064 
00065 /// The 32-bit PE header that follows the COFF header.
00066 struct pe32_header {
00067   support::ulittle16_t Magic;
00068   uint8_t MajorLinkerVersion;
00069   uint8_t MinorLinkerVersion;
00070   support::ulittle32_t SizeOfCode;
00071   support::ulittle32_t SizeOfInitializedData;
00072   support::ulittle32_t SizeOfUninitializedData;
00073   support::ulittle32_t AddressOfEntryPoint;
00074   support::ulittle32_t BaseOfCode;
00075   support::ulittle32_t BaseOfData;
00076   support::ulittle32_t ImageBase;
00077   support::ulittle32_t SectionAlignment;
00078   support::ulittle32_t FileAlignment;
00079   support::ulittle16_t MajorOperatingSystemVersion;
00080   support::ulittle16_t MinorOperatingSystemVersion;
00081   support::ulittle16_t MajorImageVersion;
00082   support::ulittle16_t MinorImageVersion;
00083   support::ulittle16_t MajorSubsystemVersion;
00084   support::ulittle16_t MinorSubsystemVersion;
00085   support::ulittle32_t Win32VersionValue;
00086   support::ulittle32_t SizeOfImage;
00087   support::ulittle32_t SizeOfHeaders;
00088   support::ulittle32_t CheckSum;
00089   support::ulittle16_t Subsystem;
00090   support::ulittle16_t DLLCharacteristics;
00091   support::ulittle32_t SizeOfStackReserve;
00092   support::ulittle32_t SizeOfStackCommit;
00093   support::ulittle32_t SizeOfHeapReserve;
00094   support::ulittle32_t SizeOfHeapCommit;
00095   support::ulittle32_t LoaderFlags;
00096   support::ulittle32_t NumberOfRvaAndSize;
00097 };
00098 
00099 /// The 64-bit PE header that follows the COFF header.
00100 struct pe32plus_header {
00101   support::ulittle16_t Magic;
00102   uint8_t MajorLinkerVersion;
00103   uint8_t MinorLinkerVersion;
00104   support::ulittle32_t SizeOfCode;
00105   support::ulittle32_t SizeOfInitializedData;
00106   support::ulittle32_t SizeOfUninitializedData;
00107   support::ulittle32_t AddressOfEntryPoint;
00108   support::ulittle32_t BaseOfCode;
00109   support::ulittle64_t ImageBase;
00110   support::ulittle32_t SectionAlignment;
00111   support::ulittle32_t FileAlignment;
00112   support::ulittle16_t MajorOperatingSystemVersion;
00113   support::ulittle16_t MinorOperatingSystemVersion;
00114   support::ulittle16_t MajorImageVersion;
00115   support::ulittle16_t MinorImageVersion;
00116   support::ulittle16_t MajorSubsystemVersion;
00117   support::ulittle16_t MinorSubsystemVersion;
00118   support::ulittle32_t Win32VersionValue;
00119   support::ulittle32_t SizeOfImage;
00120   support::ulittle32_t SizeOfHeaders;
00121   support::ulittle32_t CheckSum;
00122   support::ulittle16_t Subsystem;
00123   support::ulittle16_t DLLCharacteristics;
00124   support::ulittle64_t SizeOfStackReserve;
00125   support::ulittle64_t SizeOfStackCommit;
00126   support::ulittle64_t SizeOfHeapReserve;
00127   support::ulittle64_t SizeOfHeapCommit;
00128   support::ulittle32_t LoaderFlags;
00129   support::ulittle32_t NumberOfRvaAndSize;
00130 };
00131 
00132 struct data_directory {
00133   support::ulittle32_t RelativeVirtualAddress;
00134   support::ulittle32_t Size;
00135 };
00136 
00137 struct import_directory_table_entry {
00138   support::ulittle32_t ImportLookupTableRVA;
00139   support::ulittle32_t TimeDateStamp;
00140   support::ulittle32_t ForwarderChain;
00141   support::ulittle32_t NameRVA;
00142   support::ulittle32_t ImportAddressTableRVA;
00143 };
00144 
00145 struct import_lookup_table_entry32 {
00146   support::ulittle32_t data;
00147 
00148   bool isOrdinal() const { return data & 0x80000000; }
00149 
00150   uint16_t getOrdinal() const {
00151     assert(isOrdinal() && "ILT entry is not an ordinal!");
00152     return data & 0xFFFF;
00153   }
00154 
00155   uint32_t getHintNameRVA() const {
00156     assert(!isOrdinal() && "ILT entry is not a Hint/Name RVA!");
00157     return data;
00158   }
00159 };
00160 
00161 struct export_directory_table_entry {
00162   support::ulittle32_t ExportFlags;
00163   support::ulittle32_t TimeDateStamp;
00164   support::ulittle16_t MajorVersion;
00165   support::ulittle16_t MinorVersion;
00166   support::ulittle32_t NameRVA;
00167   support::ulittle32_t OrdinalBase;
00168   support::ulittle32_t AddressTableEntries;
00169   support::ulittle32_t NumberOfNamePointers;
00170   support::ulittle32_t ExportAddressTableRVA;
00171   support::ulittle32_t NamePointerRVA;
00172   support::ulittle32_t OrdinalTableRVA;
00173 };
00174 
00175 union export_address_table_entry {
00176   support::ulittle32_t ExportRVA;
00177   support::ulittle32_t ForwarderRVA;
00178 };
00179 
00180 typedef support::ulittle32_t export_name_pointer_table_entry;
00181 typedef support::ulittle16_t export_ordinal_table_entry;
00182 
00183 struct coff_symbol {
00184   struct StringTableOffset {
00185     support::ulittle32_t Zeroes;
00186     support::ulittle32_t Offset;
00187   };
00188 
00189   union {
00190     char ShortName[8];
00191     StringTableOffset Offset;
00192   } Name;
00193 
00194   support::ulittle32_t Value;
00195   support::ulittle16_t SectionNumber;
00196 
00197   support::ulittle16_t Type;
00198 
00199   support::ulittle8_t StorageClass;
00200   support::ulittle8_t NumberOfAuxSymbols;
00201 
00202   uint8_t getBaseType() const { return Type & 0x0F; }
00203 
00204   uint8_t getComplexType() const { return (Type & 0xF0) >> 4; }
00205 
00206   bool isFunctionDefinition() const {
00207     return StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
00208            getBaseType() == COFF::IMAGE_SYM_TYPE_NULL &&
00209            getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION &&
00210            !COFF::isReservedSectionNumber(SectionNumber);
00211   }
00212 
00213   bool isFunctionLineInfo() const {
00214     return StorageClass == COFF::IMAGE_SYM_CLASS_FUNCTION;
00215   }
00216 
00217   bool isWeakExternal() const {
00218     return StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL ||
00219            (StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
00220             SectionNumber == COFF::IMAGE_SYM_UNDEFINED && Value == 0);
00221   }
00222 
00223   bool isFileRecord() const {
00224     return StorageClass == COFF::IMAGE_SYM_CLASS_FILE;
00225   }
00226 
00227   bool isSectionDefinition() const {
00228     // C++/CLI creates external ABS symbols for non-const appdomain globals.
00229     // These are also followed by an auxiliary section definition.
00230     bool isAppdomainGlobal = StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
00231                              SectionNumber == COFF::IMAGE_SYM_ABSOLUTE;
00232     bool isOrdinarySection =
00233         StorageClass == COFF::IMAGE_SYM_CLASS_STATIC && Value == 0;
00234     return isAppdomainGlobal || isOrdinarySection;
00235   }
00236 
00237   bool isCLRToken() const {
00238     return StorageClass == COFF::IMAGE_SYM_CLASS_CLR_TOKEN;
00239   }
00240 };
00241 
00242 struct coff_section {
00243   char Name[8];
00244   support::ulittle32_t VirtualSize;
00245   support::ulittle32_t VirtualAddress;
00246   support::ulittle32_t SizeOfRawData;
00247   support::ulittle32_t PointerToRawData;
00248   support::ulittle32_t PointerToRelocations;
00249   support::ulittle32_t PointerToLinenumbers;
00250   support::ulittle16_t NumberOfRelocations;
00251   support::ulittle16_t NumberOfLinenumbers;
00252   support::ulittle32_t Characteristics;
00253 
00254   // Returns true if the actual number of relocations is stored in
00255   // VirtualAddress field of the first relocation table entry.
00256   bool hasExtendedRelocations() const {
00257     return Characteristics & COFF::IMAGE_SCN_LNK_NRELOC_OVFL &&
00258         NumberOfRelocations == UINT16_MAX;
00259   };
00260 };
00261 
00262 struct coff_relocation {
00263   support::ulittle32_t VirtualAddress;
00264   support::ulittle32_t SymbolTableIndex;
00265   support::ulittle16_t Type;
00266 };
00267 
00268 struct coff_aux_function_definition {
00269   support::ulittle32_t TagIndex;
00270   support::ulittle32_t TotalSize;
00271   support::ulittle32_t PointerToLinenumber;
00272   support::ulittle32_t PointerToNextFunction;
00273   char Unused[2];
00274 };
00275 
00276 struct coff_aux_bf_and_ef_symbol {
00277   char Unused1[4];
00278   support::ulittle16_t Linenumber;
00279   char Unused2[6];
00280   support::ulittle32_t PointerToNextFunction;
00281   char Unused3[2];
00282 };
00283 
00284 struct coff_aux_weak_external {
00285   support::ulittle32_t TagIndex;
00286   support::ulittle32_t Characteristics;
00287   char Unused[10];
00288 };
00289 
00290 struct coff_aux_file {
00291   char FileName[18];
00292 };
00293 
00294 struct coff_aux_section_definition {
00295   support::ulittle32_t Length;
00296   support::ulittle16_t NumberOfRelocations;
00297   support::ulittle16_t NumberOfLinenumbers;
00298   support::ulittle32_t CheckSum;
00299   support::ulittle16_t Number;
00300   support::ulittle8_t Selection;
00301   char Unused[3];
00302 };
00303 
00304 struct coff_aux_clr_token {
00305   support::ulittle8_t AuxType;
00306   support::ulittle8_t Reserved;
00307   support::ulittle32_t SymbolTableIndex;
00308   char Unused[12];
00309 };
00310 
00311 struct coff_load_configuration32 {
00312   support::ulittle32_t Characteristics;
00313   support::ulittle32_t TimeDateStamp;
00314   support::ulittle16_t MajorVersion;
00315   support::ulittle16_t MinorVersion;
00316   support::ulittle32_t GlobalFlagsClear;
00317   support::ulittle32_t GlobalFlagsSet;
00318   support::ulittle32_t CriticalSectionDefaultTimeout;
00319   support::ulittle32_t DeCommitFreeBlockThreshold;
00320   support::ulittle32_t DeCommitTotalFreeThreshold;
00321   support::ulittle32_t LockPrefixTable;
00322   support::ulittle32_t MaximumAllocationSize;
00323   support::ulittle32_t VirtualMemoryThreshold;
00324   support::ulittle32_t ProcessAffinityMask;
00325   support::ulittle32_t ProcessHeapFlags;
00326   support::ulittle16_t CSDVersion;
00327   uint16_t Reserved;
00328   support::ulittle32_t EditList;
00329   support::ulittle32_t SecurityCookie;
00330   support::ulittle32_t SEHandlerTable;
00331   support::ulittle32_t SEHandlerCount;
00332 };
00333 
00334 struct coff_runtime_function_x64 {
00335   support::ulittle32_t BeginAddress;
00336   support::ulittle32_t EndAddress;
00337   support::ulittle32_t UnwindInformation;
00338 };
00339 
00340 class COFFObjectFile : public ObjectFile {
00341 private:
00342   friend class ImportDirectoryEntryRef;
00343   friend class ExportDirectoryEntryRef;
00344   const coff_file_header *COFFHeader;
00345   const pe32_header *PE32Header;
00346   const pe32plus_header *PE32PlusHeader;
00347   const data_directory *DataDirectory;
00348   const coff_section *SectionTable;
00349   const coff_symbol *SymbolTable;
00350   const char *StringTable;
00351   uint32_t StringTableSize;
00352   const import_directory_table_entry *ImportDirectory;
00353   uint32_t NumberOfImportDirectory;
00354   const export_directory_table_entry *ExportDirectory;
00355 
00356   error_code getString(uint32_t offset, StringRef &Res) const;
00357 
00358   const coff_symbol *toSymb(DataRefImpl Symb) const;
00359   const coff_section *toSec(DataRefImpl Sec) const;
00360   const coff_relocation *toRel(DataRefImpl Rel) const;
00361 
00362   error_code initSymbolTablePtr();
00363   error_code initImportTablePtr();
00364   error_code initExportTablePtr();
00365 
00366 protected:
00367   void moveSymbolNext(DataRefImpl &Symb) const override;
00368   error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const override;
00369   error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const override;
00370   error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
00371   uint32_t getSymbolFlags(DataRefImpl Symb) const override;
00372   error_code getSymbolType(DataRefImpl Symb,
00373                            SymbolRef::Type &Res) const override;
00374   error_code getSymbolSection(DataRefImpl Symb,
00375                               section_iterator &Res) const override;
00376   void moveSectionNext(DataRefImpl &Sec) const override;
00377   error_code getSectionName(DataRefImpl Sec, StringRef &Res) const override;
00378   error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const override;
00379   error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const override;
00380   error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const override;
00381   error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const override;
00382   error_code isSectionText(DataRefImpl Sec, bool &Res) const override;
00383   error_code isSectionData(DataRefImpl Sec, bool &Res) const override;
00384   error_code isSectionBSS(DataRefImpl Sec, bool &Res) const override;
00385   error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const override;
00386   error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const override;
00387   error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const override;
00388   error_code isSectionRequiredForExecution(DataRefImpl Sec,
00389                                            bool &Res) const override;
00390   error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
00391                                    bool &Result) const override;
00392   relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
00393   relocation_iterator section_rel_end(DataRefImpl Sec) const override;
00394 
00395   void moveRelocationNext(DataRefImpl &Rel) const override;
00396   error_code getRelocationAddress(DataRefImpl Rel,
00397                                   uint64_t &Res) const override;
00398   error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const override;
00399   symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
00400   error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const override;
00401   error_code
00402   getRelocationTypeName(DataRefImpl Rel,
00403                         SmallVectorImpl<char> &Result) const override;
00404   error_code
00405   getRelocationValueString(DataRefImpl Rel,
00406                            SmallVectorImpl<char> &Result) const override;
00407 
00408   error_code getLibraryNext(DataRefImpl LibData,
00409                             LibraryRef &Result) const override;
00410   error_code getLibraryPath(DataRefImpl LibData,
00411                             StringRef &Result) const override;
00412 
00413 public:
00414   COFFObjectFile(MemoryBuffer *Object, error_code &EC, bool BufferOwned = true);
00415   basic_symbol_iterator symbol_begin_impl() const override;
00416   basic_symbol_iterator symbol_end_impl() const override;
00417   library_iterator needed_library_begin() const override;
00418   library_iterator needed_library_end() const override;
00419   section_iterator section_begin() const override;
00420   section_iterator section_end() const override;
00421 
00422   const coff_section *getCOFFSection(const SectionRef &Section) const;
00423   const coff_symbol *getCOFFSymbol(const SymbolRef &Symbol) const;
00424   const coff_relocation *getCOFFRelocation(const RelocationRef &Reloc) const;
00425 
00426   uint8_t getBytesInAddress() const override;
00427   StringRef getFileFormatName() const override;
00428   unsigned getArch() const override;
00429   StringRef getLoadName() const override;
00430 
00431   import_directory_iterator import_directory_begin() const;
00432   import_directory_iterator import_directory_end() const;
00433   export_directory_iterator export_directory_begin() const;
00434   export_directory_iterator export_directory_end() const;
00435 
00436   error_code getHeader(const coff_file_header *&Res) const;
00437   error_code getCOFFHeader(const coff_file_header *&Res) const;
00438   error_code getPE32Header(const pe32_header *&Res) const;
00439   error_code getPE32PlusHeader(const pe32plus_header *&Res) const;
00440   error_code getDataDirectory(uint32_t index, const data_directory *&Res) const;
00441   error_code getSection(int32_t index, const coff_section *&Res) const;
00442   error_code getSymbol(uint32_t index, const coff_symbol *&Res) const;
00443   template <typename T>
00444   error_code getAuxSymbol(uint32_t index, const T *&Res) const {
00445     const coff_symbol *s;
00446     error_code ec = getSymbol(index, s);
00447     Res = reinterpret_cast<const T *>(s);
00448     return ec;
00449   }
00450   error_code getSymbolName(const coff_symbol *symbol, StringRef &Res) const;
00451   ArrayRef<uint8_t> getSymbolAuxData(const coff_symbol *symbol) const;
00452 
00453   error_code getSectionName(const coff_section *Sec, StringRef &Res) const;
00454   error_code getSectionContents(const coff_section *Sec,
00455                                 ArrayRef<uint8_t> &Res) const;
00456 
00457   error_code getVaPtr(uint64_t VA, uintptr_t &Res) const;
00458   error_code getRvaPtr(uint32_t Rva, uintptr_t &Res) const;
00459   error_code getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const;
00460 
00461   static inline bool classof(const Binary *v) { return v->isCOFF(); }
00462 };
00463 
00464 // The iterator for the import directory table.
00465 class ImportDirectoryEntryRef {
00466 public:
00467   ImportDirectoryEntryRef() : OwningObject(nullptr) {}
00468   ImportDirectoryEntryRef(const import_directory_table_entry *Table, uint32_t I,
00469                           const COFFObjectFile *Owner)
00470       : ImportTable(Table), Index(I), OwningObject(Owner) {}
00471 
00472   bool operator==(const ImportDirectoryEntryRef &Other) const;
00473   void moveNext();
00474   error_code getName(StringRef &Result) const;
00475 
00476   error_code
00477   getImportTableEntry(const import_directory_table_entry *&Result) const;
00478 
00479   error_code
00480   getImportLookupEntry(const import_lookup_table_entry32 *&Result) const;
00481 
00482 private:
00483   const import_directory_table_entry *ImportTable;
00484   uint32_t Index;
00485   const COFFObjectFile *OwningObject;
00486 };
00487 
00488 // The iterator for the export directory table entry.
00489 class ExportDirectoryEntryRef {
00490 public:
00491   ExportDirectoryEntryRef() : OwningObject(nullptr) {}
00492   ExportDirectoryEntryRef(const export_directory_table_entry *Table, uint32_t I,
00493                           const COFFObjectFile *Owner)
00494       : ExportTable(Table), Index(I), OwningObject(Owner) {}
00495 
00496   bool operator==(const ExportDirectoryEntryRef &Other) const;
00497   void moveNext();
00498 
00499   error_code getDllName(StringRef &Result) const;
00500   error_code getOrdinalBase(uint32_t &Result) const;
00501   error_code getOrdinal(uint32_t &Result) const;
00502   error_code getExportRVA(uint32_t &Result) const;
00503   error_code getSymbolName(StringRef &Result) const;
00504 
00505 private:
00506   const export_directory_table_entry *ExportTable;
00507   uint32_t Index;
00508   const COFFObjectFile *OwningObject;
00509 };
00510 } // end namespace object
00511 } // end namespace llvm
00512 
00513 #endif