pe_info.hpp 16 KB

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