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   std::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   std::error_code initSymbolTablePtr();
00363   std::error_code initImportTablePtr();
00364   std::error_code initExportTablePtr();
00365 
00366 protected:
00367   void moveSymbolNext(DataRefImpl &Symb) const override;
00368   std::error_code getSymbolName(DataRefImpl Symb,
00369                                 StringRef &Res) const override;
00370   std::error_code getSymbolAddress(DataRefImpl Symb,
00371                                    uint64_t &Res) const override;
00372   std::error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
00373   uint32_t getSymbolFlags(DataRefImpl Symb) const override;
00374   std::error_code getSymbolType(DataRefImpl Symb,
00375                                 SymbolRef::Type &Res) const override;
00376   std::error_code getSymbolSection(DataRefImpl Symb,
00377                                    section_iterator &Res) const override;
00378   void moveSectionNext(DataRefImpl &Sec) const override;
00379   std::error_code getSectionName(DataRefImpl Sec,
00380                                  StringRef &Res) const override;
00381   std::error_code getSectionAddress(DataRefImpl Sec,
00382                                     uint64_t &Res) const override;
00383   std::error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const override;
00384   std::error_code getSectionContents(DataRefImpl Sec,
00385                                      StringRef &Res) const override;
00386   std::error_code getSectionAlignment(DataRefImpl Sec,
00387                                       uint64_t &Res) const override;
00388   std::error_code isSectionText(DataRefImpl Sec, bool &Res) const override;
00389   std::error_code isSectionData(DataRefImpl Sec, bool &Res) const override;
00390   std::error_code isSectionBSS(DataRefImpl Sec, bool &Res) const override;
00391   std::error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const override;
00392   std::error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const override;
00393   std::error_code isSectionReadOnlyData(DataRefImpl Sec,
00394                                         bool &Res) const override;
00395   std::error_code isSectionRequiredForExecution(DataRefImpl Sec,
00396                                                 bool &Res) const override;
00397   std::error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
00398                                         bool &Result) const override;
00399   relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
00400   relocation_iterator section_rel_end(DataRefImpl Sec) const override;
00401 
00402   void moveRelocationNext(DataRefImpl &Rel) const override;
00403   std::error_code getRelocationAddress(DataRefImpl Rel,
00404                                        uint64_t &Res) const override;
00405   std::error_code getRelocationOffset(DataRefImpl Rel,
00406                                       uint64_t &Res) const override;
00407   symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
00408   std::error_code getRelocationType(DataRefImpl Rel,
00409                                     uint64_t &Res) const override;
00410   std::error_code
00411   getRelocationTypeName(DataRefImpl Rel,
00412                         SmallVectorImpl<char> &Result) const override;
00413   std::error_code
00414   getRelocationValueString(DataRefImpl Rel,
00415                            SmallVectorImpl<char> &Result) const override;
00416 
00417   std::error_code getLibraryNext(DataRefImpl LibData,
00418                                  LibraryRef &Result) const override;
00419   std::error_code getLibraryPath(DataRefImpl LibData,
00420                                  StringRef &Result) const override;
00421 
00422 public:
00423   COFFObjectFile(std::unique_ptr<MemoryBuffer> Object, std::error_code &EC);
00424   basic_symbol_iterator symbol_begin_impl() const override;
00425   basic_symbol_iterator symbol_end_impl() const override;
00426   library_iterator needed_library_begin() const override;
00427   library_iterator needed_library_end() const override;
00428   section_iterator section_begin() const override;
00429   section_iterator section_end() const override;
00430 
00431   const coff_section *getCOFFSection(const SectionRef &Section) const;
00432   const coff_symbol *getCOFFSymbol(const SymbolRef &Symbol) const;
00433   const coff_relocation *getCOFFRelocation(const RelocationRef &Reloc) const;
00434 
00435   uint8_t getBytesInAddress() const override;
00436   StringRef getFileFormatName() const override;
00437   unsigned getArch() const override;
00438   StringRef getLoadName() const override;
00439 
00440   import_directory_iterator import_directory_begin() const;
00441   import_directory_iterator import_directory_end() const;
00442   export_directory_iterator export_directory_begin() const;
00443   export_directory_iterator export_directory_end() const;
00444 
00445   std::error_code getHeader(const coff_file_header *&Res) const;
00446   std::error_code getCOFFHeader(const coff_file_header *&Res) const;
00447   std::error_code getPE32Header(const pe32_header *&Res) const;
00448   std::error_code getPE32PlusHeader(const pe32plus_header *&Res) const;
00449   std::error_code getDataDirectory(uint32_t index,
00450                                    const data_directory *&Res) const;
00451   std::error_code getSection(int32_t index, const coff_section *&Res) const;
00452   std::error_code getSymbol(uint32_t index, const coff_symbol *&Res) const;
00453   template <typename T>
00454   std::error_code getAuxSymbol(uint32_t index, const T *&Res) const {
00455     const coff_symbol *s;
00456     std::error_code ec = getSymbol(index, s);
00457     Res = reinterpret_cast<const T *>(s);
00458     return ec;
00459   }
00460   std::error_code getSymbolName(const coff_symbol *symbol,
00461                                 StringRef &Res) const;
00462   ArrayRef<uint8_t> getSymbolAuxData(const coff_symbol *symbol) const;
00463 
00464   std::error_code getSectionName(const coff_section *Sec, StringRef &Res) const;
00465   std::error_code getSectionContents(const coff_section *Sec,
00466                                      ArrayRef<uint8_t> &Res) const;
00467 
00468   std::error_code getVaPtr(uint64_t VA, uintptr_t &Res) const;
00469   std::error_code getRvaPtr(uint32_t Rva, uintptr_t &Res) const;
00470   std::error_code getHintName(uint32_t Rva, uint16_t &Hint,
00471                               StringRef &Name) const;
00472 
00473   static inline bool classof(const Binary *v) { return v->isCOFF(); }
00474 };
00475 
00476 // The iterator for the import directory table.
00477 class ImportDirectoryEntryRef {
00478 public:
00479   ImportDirectoryEntryRef() : OwningObject(nullptr) {}
00480   ImportDirectoryEntryRef(const import_directory_table_entry *Table, uint32_t I,
00481                           const COFFObjectFile *Owner)
00482       : ImportTable(Table), Index(I), OwningObject(Owner) {}
00483 
00484   bool operator==(const ImportDirectoryEntryRef &Other) const;
00485   void moveNext();
00486   std::error_code getName(StringRef &Result) const;
00487 
00488   std::error_code
00489   getImportTableEntry(const import_directory_table_entry *&Result) const;
00490 
00491   std::error_code
00492   getImportLookupEntry(const import_lookup_table_entry32 *&Result) const;
00493 
00494 private:
00495   const import_directory_table_entry *ImportTable;
00496   uint32_t Index;
00497   const COFFObjectFile *OwningObject;
00498 };
00499 
00500 // The iterator for the export directory table entry.
00501 class ExportDirectoryEntryRef {
00502 public:
00503   ExportDirectoryEntryRef() : OwningObject(nullptr) {}
00504   ExportDirectoryEntryRef(const export_directory_table_entry *Table, uint32_t I,
00505                           const COFFObjectFile *Owner)
00506       : ExportTable(Table), Index(I), OwningObject(Owner) {}
00507 
00508   bool operator==(const ExportDirectoryEntryRef &Other) const;
00509   void moveNext();
00510 
00511   std::error_code getDllName(StringRef &Result) const;
00512   std::error_code getOrdinalBase(uint32_t &Result) const;
00513   std::error_code getOrdinal(uint32_t &Result) const;
00514   std::error_code getExportRVA(uint32_t &Result) const;
00515   std::error_code getSymbolName(StringRef &Result) const;
00516 
00517 private:
00518   const export_directory_table_entry *ExportTable;
00519   uint32_t Index;
00520   const COFFObjectFile *OwningObject;
00521 };
00522 } // end namespace object
00523 } // end namespace llvm
00524 
00525 #endif