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.

PEHeaders.h 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #ifndef PEHEADERS_H
  2. #define PEHEADERS_H
  3. /* VirtualAddress -- RVA
  4. * VA = ImageBase + VA
  5. * rawSection -- Offset to section from start of file
  6. * sectionRVA -- RVA from section
  7. * RAW = RVA - sectionRVA + rawSection;
  8. */
  9. #pragma pack(push, 1)
  10. namespace PEHeaders {
  11. struct File
  12. {
  13. unsigned short Machine;
  14. unsigned short NumberOfSections;
  15. unsigned int TimeDateStamp;
  16. unsigned int PointerToSymbolTable;
  17. unsigned int NumberOfSymbols;
  18. unsigned short SizeOfOptionalHeader;
  19. unsigned short Characteristics;
  20. };
  21. namespace Tables{
  22. struct Export{
  23. unsigned int Characteristics;
  24. unsigned int TimeDateStamp;
  25. unsigned short MajorVersion;
  26. unsigned short MinorVersion;
  27. unsigned int Name;
  28. unsigned int Base;
  29. unsigned int NumberOfFunctions;
  30. unsigned int NumberOfNames;
  31. unsigned int AddressOfFunctions;
  32. unsigned int AddressOfNames;
  33. unsigned int AddressOfNameOrdinals;
  34. };
  35. namespace Import{
  36. struct Base{
  37. union {
  38. unsigned int Characteristics;
  39. unsigned int OriginalFirstThunk;
  40. };
  41. unsigned int TimeDateStamp;
  42. unsigned int ForwarderChain;
  43. unsigned int Name;
  44. unsigned int FirstThunk;
  45. };
  46. struct Thunk32{
  47. union {
  48. unsigned int ForwarderString;
  49. unsigned int Function;
  50. unsigned int Ordinal;
  51. unsigned int AddressOfData;
  52. };
  53. };
  54. struct ImportByName{
  55. unsigned short Hint;
  56. unsigned char Name[1];
  57. };
  58. }
  59. }
  60. enum eImageData{
  61. eID_export,
  62. eID_import,
  63. eID_resource,
  64. eID_exception,
  65. eID_security,
  66. eID_basereloc,
  67. eID_debug,
  68. eID_arch,
  69. eID_globalptr,
  70. eID_tls,
  71. eID_load_config,
  72. eID_bound_import,
  73. eID_iat,
  74. eID_delay_import,
  75. eID_com_descriptor
  76. };
  77. struct ImageData{
  78. unsigned int VirtualAddress;
  79. unsigned int size;
  80. };
  81. struct Optional{
  82. unsigned short Magic;
  83. unsigned char MajorLinkerVersion;
  84. unsigned char MinorLinkerVersion;
  85. unsigned int SizeOfCode;
  86. unsigned int SizeOfInitializedData;
  87. unsigned int SizeOfUninitializedData;
  88. unsigned int AddressOfEntryPoint;
  89. unsigned int BaseOfCode;
  90. unsigned int BaseOfData;
  91. unsigned int ImageBase;
  92. unsigned int SectionAlignment;
  93. unsigned int FileAlignment;
  94. unsigned short MajorOperatingSystemVersion;
  95. unsigned short MinorOperatingSystemVersion;
  96. unsigned short MajorImageVersion;
  97. unsigned short MinorImageVersion;
  98. unsigned short MajorSubsystemVersion;
  99. unsigned short MinorSubsystemVersion;
  100. unsigned int Win32VersionValue;
  101. unsigned int SizeOfImage;
  102. unsigned int SizeOfHeaders;
  103. unsigned int CheckSum;
  104. unsigned short Subsystem;
  105. unsigned short DllCharacteristics;
  106. unsigned int SizeOfStackReserve;
  107. unsigned int SizeOfStackCommit;
  108. unsigned int SizeOfHeapReserve;
  109. unsigned int SizeOfHeapCommit;
  110. unsigned int LoaderFlags;
  111. unsigned int NumberOfRvaAndSizes; //const 16
  112. ImageData DataDirectory[16]; // eImageData
  113. };
  114. struct Section
  115. {
  116. unsigned char Name[8];
  117. union {
  118. unsigned int PhysicalAddress;
  119. unsigned int VirtualSize;
  120. };
  121. unsigned int VirtualAddress;
  122. unsigned int SizeOfRawData;
  123. unsigned int PointerToRawData;
  124. unsigned int PointerToRelocations;
  125. unsigned int PointerToLinenumbers;
  126. unsigned short NumberOfRelocations;
  127. unsigned short NumberOfLinenumbers;
  128. unsigned int Characteristics;
  129. bool isRVAInSection(unsigned int rva, int /* from Optional */ SectionAlignment)
  130. {
  131. auto ALIGN = [](unsigned int x, unsigned int align){
  132. return (x & (align-1))?(x & ~(align-1))+align:x;
  133. };
  134. unsigned int start = VirtualAddress;
  135. unsigned int end = start + ALIGN(VirtualSize, SectionAlignment);
  136. if(rva >= start && rva < end)
  137. return true;
  138. return false;
  139. }
  140. unsigned int RVA2Offset(unsigned int RVA, int /* from Optional */ SectionAlignment){
  141. if (!isRVAInSection(RVA, SectionAlignment))
  142. return 0;
  143. return RVA - VirtualAddress + PointerToRawData;
  144. }
  145. };
  146. struct Base{
  147. unsigned int signature;
  148. File file;
  149. Optional optional;
  150. };
  151. }
  152. #pragma pack(pop)
  153. #endif // PEHEADERS_H