pe_info.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
  2. // Copyright 2015-2019 Antony Polukhin.
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_DLL_DETAIL_WINDOWS_PE_INFO_HPP
  8. #define BOOST_DLL_DETAIL_WINDOWS_PE_INFO_HPP
  9. #include <boost/dll/config.hpp>
  10. #ifdef BOOST_HAS_PRAGMA_ONCE
  11. # pragma once
  12. #endif
  13. #include <cstring>
  14. #include <fstream>
  15. #include <boost/assert.hpp>
  16. #include <boost/cstdint.hpp>
  17. namespace boost { namespace dll { namespace detail {
  18. // reference:
  19. // http://www.joachim-bauch.de/tutorials/loading-a-dll-from-memory/
  20. // http://msdn.microsoft.com/en-us/magazine/ms809762.aspx
  21. // http://msdn.microsoft.com/en-us/magazine/cc301808.aspx
  22. //
  23. // Basic Windows typedefs. We can not use <boost/winapi/basic_types.hpp> header
  24. // because that header must be included only on Windows platform
  25. typedef unsigned char BYTE_;
  26. typedef unsigned short WORD_;
  27. typedef boost::uint32_t DWORD_;
  28. typedef boost::int32_t LONG_;
  29. typedef boost::uint32_t ULONG_;
  30. typedef boost::int64_t LONGLONG_;
  31. typedef boost::uint64_t ULONGLONG_;
  32. struct IMAGE_DOS_HEADER_ { // 32/64 independent header
  33. boost::dll::detail::WORD_ e_magic; // Magic number
  34. boost::dll::detail::WORD_ e_cblp; // Bytes on last page of file
  35. boost::dll::detail::WORD_ e_cp; // Pages in file
  36. boost::dll::detail::WORD_ e_crlc; // Relocations
  37. boost::dll::detail::WORD_ e_cparhdr; // Size of header in paragraphs
  38. boost::dll::detail::WORD_ e_minalloc; // Minimum extra paragraphs needed
  39. boost::dll::detail::WORD_ e_maxalloc; // Maximum extra paragraphs needed
  40. boost::dll::detail::WORD_ e_ss; // Initial (relative) SS value
  41. boost::dll::detail::WORD_ e_sp; // Initial SP value
  42. boost::dll::detail::WORD_ e_csum; // Checksum
  43. boost::dll::detail::WORD_ e_ip; // Initial IP value
  44. boost::dll::detail::WORD_ e_cs; // Initial (relative) CS value
  45. boost::dll::detail::WORD_ e_lfarlc; // File address of relocation table
  46. boost::dll::detail::WORD_ e_ovno; // Overlay number
  47. boost::dll::detail::WORD_ e_res[4]; // Reserved words
  48. boost::dll::detail::WORD_ e_oemid; // OEM identifier (for e_oeminfo)
  49. boost::dll::detail::WORD_ e_oeminfo; // OEM information; e_oemid specific
  50. boost::dll::detail::WORD_ e_res2[10]; // Reserved words
  51. boost::dll::detail::LONG_ e_lfanew; // File address of new exe header
  52. };
  53. struct IMAGE_FILE_HEADER_ { // 32/64 independent header
  54. boost::dll::detail::WORD_ Machine;
  55. boost::dll::detail::WORD_ NumberOfSections;
  56. boost::dll::detail::DWORD_ TimeDateStamp;
  57. boost::dll::detail::DWORD_ PointerToSymbolTable;
  58. boost::dll::detail::DWORD_ NumberOfSymbols;
  59. boost::dll::detail::WORD_ SizeOfOptionalHeader;
  60. boost::dll::detail::WORD_ Characteristics;
  61. };
  62. struct IMAGE_DATA_DIRECTORY_ { // 32/64 independent header
  63. boost::dll::detail::DWORD_ VirtualAddress;
  64. boost::dll::detail::DWORD_ Size;
  65. };
  66. struct IMAGE_EXPORT_DIRECTORY_ { // 32/64 independent header
  67. boost::dll::detail::DWORD_ Characteristics;
  68. boost::dll::detail::DWORD_ TimeDateStamp;
  69. boost::dll::detail::WORD_ MajorVersion;
  70. boost::dll::detail::WORD_ MinorVersion;
  71. boost::dll::detail::DWORD_ Name;
  72. boost::dll::detail::DWORD_ Base;
  73. boost::dll::detail::DWORD_ NumberOfFunctions;
  74. boost::dll::detail::DWORD_ NumberOfNames;
  75. boost::dll::detail::DWORD_ AddressOfFunctions;
  76. boost::dll::detail::DWORD_ AddressOfNames;
  77. boost::dll::detail::DWORD_ AddressOfNameOrdinals;
  78. };
  79. struct IMAGE_SECTION_HEADER_ { // 32/64 independent header
  80. static const std::size_t IMAGE_SIZEOF_SHORT_NAME_ = 8;
  81. boost::dll::detail::BYTE_ Name[IMAGE_SIZEOF_SHORT_NAME_];
  82. union {
  83. boost::dll::detail::DWORD_ PhysicalAddress;
  84. boost::dll::detail::DWORD_ VirtualSize;
  85. } Misc;
  86. boost::dll::detail::DWORD_ VirtualAddress;
  87. boost::dll::detail::DWORD_ SizeOfRawData;
  88. boost::dll::detail::DWORD_ PointerToRawData;
  89. boost::dll::detail::DWORD_ PointerToRelocations;
  90. boost::dll::detail::DWORD_ PointerToLinenumbers;
  91. boost::dll::detail::WORD_ NumberOfRelocations;
  92. boost::dll::detail::WORD_ NumberOfLinenumbers;
  93. boost::dll::detail::DWORD_ Characteristics;
  94. };
  95. template <class AddressOffsetT>
  96. struct IMAGE_OPTIONAL_HEADER_template {
  97. static const std::size_t IMAGE_NUMBEROF_DIRECTORY_ENTRIES_ = 16;
  98. boost::dll::detail::WORD_ Magic;
  99. boost::dll::detail::BYTE_ MajorLinkerVersion;
  100. boost::dll::detail::BYTE_ MinorLinkerVersion;
  101. boost::dll::detail::DWORD_ SizeOfCode;
  102. boost::dll::detail::DWORD_ SizeOfInitializedData;
  103. boost::dll::detail::DWORD_ SizeOfUninitializedData;
  104. boost::dll::detail::DWORD_ AddressOfEntryPoint;
  105. union {
  106. boost::dll::detail::DWORD_ BaseOfCode;
  107. unsigned char padding_[sizeof(AddressOffsetT) == 8 ? 4 : 8]; // in x64 version BaseOfData does not exist
  108. } BaseOfCode_and_BaseOfData;
  109. AddressOffsetT ImageBase;
  110. boost::dll::detail::DWORD_ SectionAlignment;
  111. boost::dll::detail::DWORD_ FileAlignment;
  112. boost::dll::detail::WORD_ MajorOperatingSystemVersion;
  113. boost::dll::detail::WORD_ MinorOperatingSystemVersion;
  114. boost::dll::detail::WORD_ MajorImageVersion;
  115. boost::dll::detail::WORD_ MinorImageVersion;
  116. boost::dll::detail::WORD_ MajorSubsystemVersion;
  117. boost::dll::detail::WORD_ MinorSubsystemVersion;
  118. boost::dll::detail::DWORD_ Win32VersionValue;
  119. boost::dll::detail::DWORD_ SizeOfImage;
  120. boost::dll::detail::DWORD_ SizeOfHeaders;
  121. boost::dll::detail::DWORD_ CheckSum;
  122. boost::dll::detail::WORD_ Subsystem;
  123. boost::dll::detail::WORD_ DllCharacteristics;
  124. AddressOffsetT SizeOfStackReserve;
  125. AddressOffsetT SizeOfStackCommit;
  126. AddressOffsetT SizeOfHeapReserve;
  127. AddressOffsetT SizeOfHeapCommit;
  128. boost::dll::detail::DWORD_ LoaderFlags;
  129. boost::dll::detail::DWORD_ NumberOfRvaAndSizes;
  130. IMAGE_DATA_DIRECTORY_ DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES_];
  131. };
  132. typedef IMAGE_OPTIONAL_HEADER_template<boost::dll::detail::DWORD_> IMAGE_OPTIONAL_HEADER32_;
  133. typedef IMAGE_OPTIONAL_HEADER_template<boost::dll::detail::ULONGLONG_> IMAGE_OPTIONAL_HEADER64_;
  134. template <class AddressOffsetT>
  135. struct IMAGE_NT_HEADERS_template {
  136. boost::dll::detail::DWORD_ Signature;
  137. IMAGE_FILE_HEADER_ FileHeader;
  138. IMAGE_OPTIONAL_HEADER_template<AddressOffsetT> OptionalHeader;
  139. };
  140. typedef IMAGE_NT_HEADERS_template<boost::dll::detail::DWORD_> IMAGE_NT_HEADERS32_;
  141. typedef IMAGE_NT_HEADERS_template<boost::dll::detail::ULONGLONG_> IMAGE_NT_HEADERS64_;
  142. template <class AddressOffsetT>
  143. class pe_info {
  144. typedef IMAGE_NT_HEADERS_template<AddressOffsetT> header_t;
  145. typedef IMAGE_EXPORT_DIRECTORY_ exports_t;
  146. typedef IMAGE_SECTION_HEADER_ section_t;
  147. typedef IMAGE_DOS_HEADER_ dos_t;
  148. template <class T>
  149. static void read_raw(std::ifstream& fs, T& value, std::size_t size = sizeof(T)) {
  150. fs.read(reinterpret_cast<char*>(&value), size);
  151. }
  152. public:
  153. static bool parsing_supported(std::ifstream& fs) {
  154. dos_t dos;
  155. fs.seekg(0);
  156. fs.read(reinterpret_cast<char*>(&dos), sizeof(dos));
  157. // 'MZ' and 'ZM' according to Wikipedia
  158. if (dos.e_magic != 0x4D5A && dos.e_magic != 0x5A4D) {
  159. return false;
  160. }
  161. header_t h;
  162. fs.seekg(dos.e_lfanew);
  163. fs.read(reinterpret_cast<char*>(&h), sizeof(h));
  164. return h.Signature == 0x00004550 // 'PE00'
  165. && h.OptionalHeader.Magic == (sizeof(boost::uint32_t) == sizeof(AddressOffsetT) ? 0x10B : 0x20B);
  166. }
  167. private:
  168. static header_t header(std::ifstream& fs) {
  169. header_t h;
  170. dos_t dos;
  171. fs.seekg(0);
  172. read_raw(fs, dos);
  173. fs.seekg(dos.e_lfanew);
  174. read_raw(fs, h);
  175. return h;
  176. }
  177. static exports_t exports(std::ifstream& fs, const header_t& h) {
  178. exports_t exports;
  179. static const unsigned int IMAGE_DIRECTORY_ENTRY_EXPORT_ = 0;
  180. const std::size_t exp_virtual_address = h.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT_].VirtualAddress;
  181. const std::size_t real_offset = get_file_offset(fs, exp_virtual_address, h);
  182. BOOST_ASSERT(real_offset);
  183. fs.seekg(real_offset);
  184. read_raw(fs, exports);
  185. return exports;
  186. }
  187. static std::size_t get_file_offset(std::ifstream& fs, std::size_t virtual_address, const header_t& h) {
  188. section_t image_section_header;
  189. { // fs.seekg to the beginning on section headers
  190. dos_t dos;
  191. fs.seekg(0);
  192. read_raw(fs, dos);
  193. fs.seekg(dos.e_lfanew + sizeof(header_t));
  194. }
  195. for (std::size_t i = 0;i < h.FileHeader.NumberOfSections;++i) {
  196. read_raw(fs, image_section_header);
  197. if (virtual_address >= image_section_header.VirtualAddress
  198. && virtual_address < image_section_header.VirtualAddress + image_section_header.SizeOfRawData)
  199. {
  200. return image_section_header.PointerToRawData + virtual_address - image_section_header.VirtualAddress;
  201. }
  202. }
  203. return 0;
  204. }
  205. public:
  206. static std::vector<std::string> sections(std::ifstream& fs) {
  207. std::vector<std::string> ret;
  208. const header_t h = header(fs);
  209. ret.reserve(h.FileHeader.NumberOfSections);
  210. // get names, e.g: .text .rdata .data .rsrc .reloc
  211. section_t image_section_header;
  212. char name_helper[section_t::IMAGE_SIZEOF_SHORT_NAME_ + 1];
  213. std::memset(name_helper, 0, sizeof(name_helper));
  214. for (std::size_t i = 0;i < h.FileHeader.NumberOfSections;++i) {
  215. // There is no terminating null character if the string is exactly eight characters long
  216. read_raw(fs, image_section_header);
  217. std::memcpy(name_helper, image_section_header.Name, section_t::IMAGE_SIZEOF_SHORT_NAME_);
  218. if (name_helper[0] != '/') {
  219. ret.push_back(name_helper);
  220. } else {
  221. // For longer names, image_section_header.Name contains a slash (/) followed by ASCII representation of a decimal number.
  222. // this number is an offset into the string table.
  223. // TODO: fixme
  224. ret.push_back(name_helper);
  225. }
  226. }
  227. return ret;
  228. }
  229. static std::vector<std::string> symbols(std::ifstream& fs) {
  230. std::vector<std::string> ret;
  231. const header_t h = header(fs);
  232. const exports_t exprt = exports(fs, h);
  233. const std::size_t exported_symbols = exprt.NumberOfNames;
  234. const std::size_t fixed_names_addr = get_file_offset(fs, exprt.AddressOfNames, h);
  235. ret.reserve(exported_symbols);
  236. boost::dll::detail::DWORD_ name_offset;
  237. std::string symbol_name;
  238. for (std::size_t i = 0;i < exported_symbols;++i) {
  239. fs.seekg(fixed_names_addr + i * sizeof(name_offset));
  240. read_raw(fs, name_offset);
  241. fs.seekg(get_file_offset(fs, name_offset, h));
  242. getline(fs, symbol_name, '\0');
  243. ret.push_back(symbol_name);
  244. }
  245. return ret;
  246. }
  247. static std::vector<std::string> symbols(std::ifstream& fs, const char* section_name) {
  248. std::vector<std::string> ret;
  249. const header_t h = header(fs);
  250. std::size_t section_begin_addr = 0;
  251. std::size_t section_end_addr = 0;
  252. { // getting address range for the section
  253. section_t image_section_header;
  254. char name_helper[section_t::IMAGE_SIZEOF_SHORT_NAME_ + 1];
  255. std::memset(name_helper, 0, sizeof(name_helper));
  256. for (std::size_t i = 0;i < h.FileHeader.NumberOfSections;++i) {
  257. // There is no terminating null character if the string is exactly eight characters long
  258. read_raw(fs, image_section_header);
  259. std::memcpy(name_helper, image_section_header.Name, section_t::IMAGE_SIZEOF_SHORT_NAME_);
  260. if (!std::strcmp(section_name, name_helper)) {
  261. section_begin_addr = image_section_header.PointerToRawData;
  262. section_end_addr = section_begin_addr + image_section_header.SizeOfRawData;
  263. }
  264. }
  265. // returning empty result if section was not found
  266. if(section_begin_addr == 0 || section_end_addr == 0)
  267. return ret;
  268. }
  269. const exports_t exprt = exports(fs, h);
  270. const std::size_t exported_symbols = exprt.NumberOfFunctions;
  271. const std::size_t fixed_names_addr = get_file_offset(fs, exprt.AddressOfNames, h);
  272. const std::size_t fixed_ordinals_addr = get_file_offset(fs, exprt.AddressOfNameOrdinals, h);
  273. const std::size_t fixed_functions_addr = get_file_offset(fs, exprt.AddressOfFunctions, h);
  274. ret.reserve(exported_symbols);
  275. boost::dll::detail::DWORD_ ptr;
  276. boost::dll::detail::WORD_ ordinal;
  277. std::string symbol_name;
  278. for (std::size_t i = 0;i < exported_symbols;++i) {
  279. // getting ordinal
  280. fs.seekg(fixed_ordinals_addr + i * sizeof(ordinal));
  281. read_raw(fs, ordinal);
  282. // getting function addr
  283. fs.seekg(fixed_functions_addr + ordinal * sizeof(ptr));
  284. read_raw(fs, ptr);
  285. ptr = static_cast<boost::dll::detail::DWORD_>( get_file_offset(fs, ptr, h) );
  286. if (ptr >= section_end_addr || ptr < section_begin_addr) {
  287. continue;
  288. }
  289. fs.seekg(fixed_names_addr + i * sizeof(ptr));
  290. read_raw(fs, ptr);
  291. fs.seekg(get_file_offset(fs, ptr, h));
  292. getline(fs, symbol_name, '\0');
  293. ret.push_back(symbol_name);
  294. }
  295. return ret;
  296. }
  297. // a test method to get dependents modules,
  298. // who my plugin imports (1st level only)
  299. /*
  300. e.g. for myself I get:
  301. KERNEL32.dll
  302. MSVCP110D.dll
  303. boost_system-vc-mt-gd-1_56.dll
  304. MSVCR110D.dll
  305. */
  306. /*
  307. static std::vector<std::string> depend_of(boost::dll::fs::error_code &ec) BOOST_NOEXCEPT {
  308. std::vector<std::string> ret;
  309. IMAGE_DOS_HEADER* image_dos_header = (IMAGE_DOS_HEADER*)native();
  310. if(!image_dos_header) {
  311. // ERROR_BAD_EXE_FORMAT
  312. ec = boost::dll::fs::make_error_code(
  313. boost::dll::fs::errc::executable_format_error
  314. );
  315. return ret;
  316. }
  317. IMAGE_OPTIONAL_HEADER* image_optional_header = (IMAGE_OPTIONAL_HEADER*)((boost::dll::detail::BYTE_*)native() + image_dos_header->e_lfanew + 24);
  318. if(!image_optional_header) {
  319. // ERROR_BAD_EXE_FORMAT
  320. ec = boost::dll::fs::make_error_code(
  321. boost::dll::fs::errc::executable_format_error
  322. );
  323. return ret;
  324. }
  325. IMAGE_IMPORT_DESCRIPTOR* image_import_descriptor = (IMAGE_IMPORT_DESCRIPTOR*)((boost::dll::detail::BYTE_*)native() + image_optional_header->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
  326. if(!image_import_descriptor) {
  327. // ERROR_BAD_EXE_FORMAT
  328. ec = boost::dll::fs::make_error_code(
  329. boost::dll::fs::errc::executable_format_error
  330. );
  331. return ret;
  332. }
  333. while(image_import_descriptor->FirstThunk) {
  334. std::string module_name = reinterpret_cast<char*>((boost::dll::detail::BYTE_*)native() + image_import_descriptor->Name);
  335. if(module_name.size()) {
  336. ret.push_back(module_name);
  337. }
  338. image_import_descriptor++;
  339. }
  340. return ret;
  341. }
  342. */
  343. };
  344. typedef pe_info<boost::dll::detail::DWORD_> pe_info32;
  345. typedef pe_info<boost::dll::detail::ULONGLONG_> pe_info64;
  346. }}} // namespace boost::dll::detail
  347. #endif // BOOST_DLL_DETAIL_WINDOWS_PE_INFO_HPP