Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
7.8 KiB

  1. //===- COFF.h - COFF object file implementation -----------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file declares the COFFObjectFile class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_OBJECT_COFF_H
  14. #define LLVM_OBJECT_COFF_H
  15. #include "llvm/Object/ObjectFile.h"
  16. #include "llvm/Support/COFF.h"
  17. #include "llvm/Support/Endian.h"
  18. namespace llvm {
  19. template <typename T>
  20. class ArrayRef;
  21. namespace object {
  22. struct coff_file_header {
  23. support::ulittle16_t Machine;
  24. support::ulittle16_t NumberOfSections;
  25. support::ulittle32_t TimeDateStamp;
  26. support::ulittle32_t PointerToSymbolTable;
  27. support::ulittle32_t NumberOfSymbols;
  28. support::ulittle16_t SizeOfOptionalHeader;
  29. support::ulittle16_t Characteristics;
  30. };
  31. struct coff_symbol {
  32. struct StringTableOffset {
  33. support::ulittle32_t Zeroes;
  34. support::ulittle32_t Offset;
  35. };
  36. union {
  37. char ShortName[8];
  38. StringTableOffset Offset;
  39. } Name;
  40. support::ulittle32_t Value;
  41. support::little16_t SectionNumber;
  42. support::ulittle16_t Type;
  43. support::ulittle8_t StorageClass;
  44. support::ulittle8_t NumberOfAuxSymbols;
  45. uint8_t getBaseType() const {
  46. return Type & 0x0F;
  47. }
  48. uint8_t getComplexType() const {
  49. return (Type & 0xF0) >> 4;
  50. }
  51. };
  52. struct coff_section {
  53. char Name[8];
  54. support::ulittle32_t VirtualSize;
  55. support::ulittle32_t VirtualAddress;
  56. support::ulittle32_t SizeOfRawData;
  57. support::ulittle32_t PointerToRawData;
  58. support::ulittle32_t PointerToRelocations;
  59. support::ulittle32_t PointerToLinenumbers;
  60. support::ulittle16_t NumberOfRelocations;
  61. support::ulittle16_t NumberOfLinenumbers;
  62. support::ulittle32_t Characteristics;
  63. };
  64. struct coff_relocation {
  65. support::ulittle32_t VirtualAddress;
  66. support::ulittle32_t SymbolTableIndex;
  67. support::ulittle16_t Type;
  68. };
  69. struct coff_aux_section_definition {
  70. support::ulittle32_t Length;
  71. support::ulittle16_t NumberOfRelocations;
  72. support::ulittle16_t NumberOfLinenumbers;
  73. support::ulittle32_t CheckSum;
  74. support::ulittle16_t Number;
  75. support::ulittle8_t Selection;
  76. char Unused[3];
  77. };
  78. class COFFObjectFile : public ObjectFile {
  79. private:
  80. const coff_file_header *Header;
  81. const coff_section *SectionTable;
  82. const coff_symbol *SymbolTable;
  83. const char *StringTable;
  84. uint32_t StringTableSize;
  85. error_code getString(uint32_t offset, StringRef &Res) const;
  86. const coff_symbol *toSymb(DataRefImpl Symb) const;
  87. const coff_section *toSec(DataRefImpl Sec) const;
  88. const coff_relocation *toRel(DataRefImpl Rel) const;
  89. protected:
  90. virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
  91. virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
  92. virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
  93. virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
  94. virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
  95. virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
  96. virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
  97. virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
  98. virtual error_code getSymbolSection(DataRefImpl Symb,
  99. section_iterator &Res) const;
  100. virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
  101. virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
  102. virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
  103. virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
  104. virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
  105. virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
  106. virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
  107. virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
  108. virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
  109. virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
  110. virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
  111. virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
  112. virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
  113. virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
  114. bool &Res) const;
  115. virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
  116. bool &Result) const;
  117. virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
  118. virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
  119. virtual error_code getRelocationNext(DataRefImpl Rel,
  120. RelocationRef &Res) const;
  121. virtual error_code getRelocationAddress(DataRefImpl Rel,
  122. uint64_t &Res) const;
  123. virtual error_code getRelocationOffset(DataRefImpl Rel,
  124. uint64_t &Res) const;
  125. virtual error_code getRelocationSymbol(DataRefImpl Rel,
  126. SymbolRef &Res) const;
  127. virtual error_code getRelocationType(DataRefImpl Rel,
  128. uint64_t &Res) const;
  129. virtual error_code getRelocationTypeName(DataRefImpl Rel,
  130. SmallVectorImpl<char> &Result) const;
  131. virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
  132. int64_t &Res) const;
  133. virtual error_code getRelocationValueString(DataRefImpl Rel,
  134. SmallVectorImpl<char> &Result) const;
  135. virtual error_code getLibraryNext(DataRefImpl LibData,
  136. LibraryRef &Result) const;
  137. virtual error_code getLibraryPath(DataRefImpl LibData,
  138. StringRef &Result) const;
  139. public:
  140. COFFObjectFile(MemoryBuffer *Object, error_code &ec);
  141. virtual symbol_iterator begin_symbols() const;
  142. virtual symbol_iterator end_symbols() const;
  143. virtual symbol_iterator begin_dynamic_symbols() const;
  144. virtual symbol_iterator end_dynamic_symbols() const;
  145. virtual library_iterator begin_libraries_needed() const;
  146. virtual library_iterator end_libraries_needed() const;
  147. virtual section_iterator begin_sections() const;
  148. virtual section_iterator end_sections() const;
  149. const coff_section *getCOFFSection(section_iterator &It) const;
  150. const coff_symbol *getCOFFSymbol(symbol_iterator &It) const;
  151. const coff_relocation *getCOFFRelocation(relocation_iterator &It) const;
  152. virtual uint8_t getBytesInAddress() const;
  153. virtual StringRef getFileFormatName() const;
  154. virtual unsigned getArch() const;
  155. virtual StringRef getLoadName() const;
  156. error_code getHeader(const coff_file_header *&Res) const;
  157. error_code getSection(int32_t index, const coff_section *&Res) const;
  158. error_code getSymbol(uint32_t index, const coff_symbol *&Res) const;
  159. template <typename T>
  160. error_code getAuxSymbol(uint32_t index, const T *&Res) const {
  161. const coff_symbol *s;
  162. error_code ec = getSymbol(index, s);
  163. Res = reinterpret_cast<const T*>(s);
  164. return ec;
  165. }
  166. error_code getSymbolName(const coff_symbol *symbol, StringRef &Res) const;
  167. ArrayRef<uint8_t> getSymbolAuxData(const coff_symbol *symbol) const;
  168. error_code getSectionName(const coff_section *Sec, StringRef &Res) const;
  169. error_code getSectionContents(const coff_section *Sec,
  170. ArrayRef<uint8_t> &Res) const;
  171. static inline bool classof(const Binary *v) {
  172. return v->isCOFF();
  173. }
  174. };
  175. }
  176. }
  177. #endif