pe_info.hpp 16 KB

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