1 // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
2 // Copyright 2015 Antony Polukhin.
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)
8 #ifndef BOOST_DLL_DETAIL_WINDOWS_PE_INFO_HPP
9 #define BOOST_DLL_DETAIL_WINDOWS_PE_INFO_HPP
11 #include <boost/config.hpp>
13 #ifdef BOOST_HAS_PRAGMA_ONCE
17 #include <boost/cstdint.hpp>
18 #include <boost/filesystem/fstream.hpp>
19 #include <boost/dll/detail/x_info_interface.hpp>
21 namespace boost { namespace dll { namespace detail {
24 // http://www.joachim-bauch.de/tutorials/loading-a-dll-from-memory/
25 // http://msdn.microsoft.com/en-us/magazine/ms809762.aspx
26 // http://msdn.microsoft.com/en-us/magazine/cc301808.aspx
29 // Basic Windows typedefs. We can not use <boost/winapi/basic_types.hpp> header
30 // because that header must be included only on Windows platform
31 typedef unsigned char BYTE_;
32 typedef unsigned short WORD_;
33 typedef boost::uint32_t DWORD_;
34 typedef boost::int32_t LONG_;
35 typedef boost::uint32_t ULONG_;
36 typedef boost::int64_t LONGLONG_;
37 typedef boost::uint64_t ULONGLONG_;
39 struct IMAGE_DOS_HEADER_ { // 32/64 independent header
40 boost::dll::detail::WORD_ e_magic; // Magic number
41 boost::dll::detail::WORD_ e_cblp; // Bytes on last page of file
42 boost::dll::detail::WORD_ e_cp; // Pages in file
43 boost::dll::detail::WORD_ e_crlc; // Relocations
44 boost::dll::detail::WORD_ e_cparhdr; // Size of header in paragraphs
45 boost::dll::detail::WORD_ e_minalloc; // Minimum extra paragraphs needed
46 boost::dll::detail::WORD_ e_maxalloc; // Maximum extra paragraphs needed
47 boost::dll::detail::WORD_ e_ss; // Initial (relative) SS value
48 boost::dll::detail::WORD_ e_sp; // Initial SP value
49 boost::dll::detail::WORD_ e_csum; // Checksum
50 boost::dll::detail::WORD_ e_ip; // Initial IP value
51 boost::dll::detail::WORD_ e_cs; // Initial (relative) CS value
52 boost::dll::detail::WORD_ e_lfarlc; // File address of relocation table
53 boost::dll::detail::WORD_ e_ovno; // Overlay number
54 boost::dll::detail::WORD_ e_res[4]; // Reserved words
55 boost::dll::detail::WORD_ e_oemid; // OEM identifier (for e_oeminfo)
56 boost::dll::detail::WORD_ e_oeminfo; // OEM information; e_oemid specific
57 boost::dll::detail::WORD_ e_res2[10]; // Reserved words
58 boost::dll::detail::LONG_ e_lfanew; // File address of new exe header
61 struct IMAGE_FILE_HEADER_ { // 32/64 independent header
62 boost::dll::detail::WORD_ Machine;
63 boost::dll::detail::WORD_ NumberOfSections;
64 boost::dll::detail::DWORD_ TimeDateStamp;
65 boost::dll::detail::DWORD_ PointerToSymbolTable;
66 boost::dll::detail::DWORD_ NumberOfSymbols;
67 boost::dll::detail::WORD_ SizeOfOptionalHeader;
68 boost::dll::detail::WORD_ Characteristics;
71 struct IMAGE_DATA_DIRECTORY_ { // 32/64 independent header
72 boost::dll::detail::DWORD_ VirtualAddress;
73 boost::dll::detail::DWORD_ Size;
76 struct IMAGE_EXPORT_DIRECTORY_ { // 32/64 independent header
77 boost::dll::detail::DWORD_ Characteristics;
78 boost::dll::detail::DWORD_ TimeDateStamp;
79 boost::dll::detail::WORD_ MajorVersion;
80 boost::dll::detail::WORD_ MinorVersion;
81 boost::dll::detail::DWORD_ Name;
82 boost::dll::detail::DWORD_ Base;
83 boost::dll::detail::DWORD_ NumberOfFunctions;
84 boost::dll::detail::DWORD_ NumberOfNames;
85 boost::dll::detail::DWORD_ AddressOfFunctions;
86 boost::dll::detail::DWORD_ AddressOfNames;
87 boost::dll::detail::DWORD_ AddressOfNameOrdinals;
90 struct IMAGE_SECTION_HEADER_ { // 32/64 independent header
91 static const std::size_t IMAGE_SIZEOF_SHORT_NAME_ = 8;
93 boost::dll::detail::BYTE_ Name[IMAGE_SIZEOF_SHORT_NAME_];
95 boost::dll::detail::DWORD_ PhysicalAddress;
96 boost::dll::detail::DWORD_ VirtualSize;
98 boost::dll::detail::DWORD_ VirtualAddress;
99 boost::dll::detail::DWORD_ SizeOfRawData;
100 boost::dll::detail::DWORD_ PointerToRawData;
101 boost::dll::detail::DWORD_ PointerToRelocations;
102 boost::dll::detail::DWORD_ PointerToLinenumbers;
103 boost::dll::detail::WORD_ NumberOfRelocations;
104 boost::dll::detail::WORD_ NumberOfLinenumbers;
105 boost::dll::detail::DWORD_ Characteristics;
109 template <class AddressOffsetT>
110 struct IMAGE_OPTIONAL_HEADER_template {
111 static const std::size_t IMAGE_NUMBEROF_DIRECTORY_ENTRIES_ = 16;
113 boost::dll::detail::WORD_ Magic;
114 boost::dll::detail::BYTE_ MajorLinkerVersion;
115 boost::dll::detail::BYTE_ MinorLinkerVersion;
116 boost::dll::detail::DWORD_ SizeOfCode;
117 boost::dll::detail::DWORD_ SizeOfInitializedData;
118 boost::dll::detail::DWORD_ SizeOfUninitializedData;
119 boost::dll::detail::DWORD_ AddressOfEntryPoint;
121 boost::dll::detail::DWORD_ BaseOfCode;
122 unsigned char padding_[sizeof(AddressOffsetT) == 8 ? 4 : 8]; // in x64 version BaseOfData does not exist
123 } BaseOfCode_and_BaseOfData;
125 AddressOffsetT ImageBase;
126 boost::dll::detail::DWORD_ SectionAlignment;
127 boost::dll::detail::DWORD_ FileAlignment;
128 boost::dll::detail::WORD_ MajorOperatingSystemVersion;
129 boost::dll::detail::WORD_ MinorOperatingSystemVersion;
130 boost::dll::detail::WORD_ MajorImageVersion;
131 boost::dll::detail::WORD_ MinorImageVersion;
132 boost::dll::detail::WORD_ MajorSubsystemVersion;
133 boost::dll::detail::WORD_ MinorSubsystemVersion;
134 boost::dll::detail::DWORD_ Win32VersionValue;
135 boost::dll::detail::DWORD_ SizeOfImage;
136 boost::dll::detail::DWORD_ SizeOfHeaders;
137 boost::dll::detail::DWORD_ CheckSum;
138 boost::dll::detail::WORD_ Subsystem;
139 boost::dll::detail::WORD_ DllCharacteristics;
140 AddressOffsetT SizeOfStackReserve;
141 AddressOffsetT SizeOfStackCommit;
142 AddressOffsetT SizeOfHeapReserve;
143 AddressOffsetT SizeOfHeapCommit;
144 boost::dll::detail::DWORD_ LoaderFlags;
145 boost::dll::detail::DWORD_ NumberOfRvaAndSizes;
146 IMAGE_DATA_DIRECTORY_ DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES_];
149 typedef IMAGE_OPTIONAL_HEADER_template<boost::dll::detail::DWORD_> IMAGE_OPTIONAL_HEADER32_;
150 typedef IMAGE_OPTIONAL_HEADER_template<boost::dll::detail::ULONGLONG_> IMAGE_OPTIONAL_HEADER64_;
152 template <class AddressOffsetT>
153 struct IMAGE_NT_HEADERS_template {
154 boost::dll::detail::DWORD_ Signature;
155 IMAGE_FILE_HEADER_ FileHeader;
156 IMAGE_OPTIONAL_HEADER_template<AddressOffsetT> OptionalHeader;
159 typedef IMAGE_NT_HEADERS_template<boost::dll::detail::DWORD_> IMAGE_NT_HEADERS32_;
160 typedef IMAGE_NT_HEADERS_template<boost::dll::detail::ULONGLONG_> IMAGE_NT_HEADERS64_;
163 template <class AddressOffsetT>
164 class pe_info: public x_info_interface {
165 boost::filesystem::ifstream& f_;
167 typedef IMAGE_NT_HEADERS_template<AddressOffsetT> header_t;
168 typedef IMAGE_EXPORT_DIRECTORY_ exports_t;
169 typedef IMAGE_SECTION_HEADER_ section_t;
170 typedef IMAGE_DOS_HEADER_ dos_t;
173 inline void read_raw(T& value, std::size_t size = sizeof(T)) const {
174 f_.read(reinterpret_cast<char*>(&value), size);
178 static bool parsing_supported(boost::filesystem::ifstream& f) {
181 f.read(reinterpret_cast<char*>(&dos), sizeof(dos));
183 // 'MZ' and 'ZM' according to Wikipedia
184 if (dos.e_magic != 0x4D5A && dos.e_magic != 0x5A4D) {
189 f.seekg(dos.e_lfanew);
190 f.read(reinterpret_cast<char*>(&h), sizeof(h));
192 return h.Signature == 0x00004550 // 'PE00'
193 && h.OptionalHeader.Magic == (sizeof(boost::uint32_t) == sizeof(AddressOffsetT) ? 0x10B : 0x20B);
197 explicit pe_info(boost::filesystem::ifstream& f) BOOST_NOEXCEPT
202 inline header_t header() {
209 f_.seekg(dos.e_lfanew);
215 inline exports_t exports(const header_t& h) {
218 static const unsigned int IMAGE_DIRECTORY_ENTRY_EXPORT_ = 0;
219 const std::size_t exp_virtual_address = h.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT_].VirtualAddress;
221 const std::size_t real_offset = get_file_offset(exp_virtual_address, h);
222 BOOST_ASSERT(real_offset);
224 f_.seekg(real_offset);
230 std::size_t get_file_offset(std::size_t virtual_address, const header_t& h) {
231 section_t image_section_header;
233 { // f_.seekg to the beginning on section headers
237 f_.seekg(dos.e_lfanew + sizeof(header_t));
240 for (std::size_t i = 0;i < h.FileHeader.NumberOfSections;++i) {
241 read_raw(image_section_header);
242 if (virtual_address >= image_section_header.VirtualAddress
243 && virtual_address < image_section_header.VirtualAddress + image_section_header.SizeOfRawData)
245 return image_section_header.PointerToRawData + virtual_address - image_section_header.VirtualAddress;
253 std::vector<std::string> sections() {
254 std::vector<std::string> ret;
256 const header_t h = header();
257 ret.reserve(h.FileHeader.NumberOfSections);
259 // get names, e.g: .text .rdata .data .rsrc .reloc
260 section_t image_section_header;
261 char name_helper[section_t::IMAGE_SIZEOF_SHORT_NAME_ + 1];
262 std::memset(name_helper, 0, sizeof(name_helper));
263 for (std::size_t i = 0;i < h.FileHeader.NumberOfSections;++i) {
264 // There is no terminating null character if the string is exactly eight characters long
265 read_raw(image_section_header);
266 std::memcpy(name_helper, image_section_header.Name, section_t::IMAGE_SIZEOF_SHORT_NAME_);
268 if (name_helper[0] != '/') {
269 ret.push_back(name_helper);
271 // For longer names, image_section_header.Name contains a slash (/) followed by ASCII representation of a decimal number.
272 // this number is an offset into the string table.
274 ret.push_back(name_helper);
281 std::vector<std::string> symbols() {
282 std::vector<std::string> ret;
284 const header_t h = header();
285 const exports_t exprt = exports(h);
286 const std::size_t exported_symbols = exprt.NumberOfNames;
287 const std::size_t fixed_names_addr = get_file_offset(exprt.AddressOfNames, h);
289 ret.reserve(exported_symbols);
290 boost::dll::detail::DWORD_ name_offset;
291 std::string symbol_name;
292 for (std::size_t i = 0;i < exported_symbols;++i) {
293 f_.seekg(fixed_names_addr + i * sizeof(name_offset));
294 read_raw(name_offset);
295 f_.seekg(get_file_offset(name_offset, h));
296 getline(f_, symbol_name, '\0');
297 ret.push_back(symbol_name);
303 std::vector<std::string> symbols(const char* section_name) {
304 std::vector<std::string> ret;
306 const header_t h = header();
308 std::size_t section_begin_addr = 0;
309 std::size_t section_end_addr = 0;
311 { // getting address range for the section
312 section_t image_section_header;
313 char name_helper[section_t::IMAGE_SIZEOF_SHORT_NAME_ + 1];
314 std::memset(name_helper, 0, sizeof(name_helper));
315 for (std::size_t i = 0;i < h.FileHeader.NumberOfSections;++i) {
316 // There is no terminating null character if the string is exactly eight characters long
317 read_raw(image_section_header);
318 std::memcpy(name_helper, image_section_header.Name, section_t::IMAGE_SIZEOF_SHORT_NAME_);
319 if (!std::strcmp(section_name, name_helper)) {
320 section_begin_addr = image_section_header.PointerToRawData;
321 section_end_addr = section_begin_addr + image_section_header.SizeOfRawData;
325 // returning empty result if section was not found
326 if(section_begin_addr == 0 || section_end_addr == 0)
330 const exports_t exprt = exports(h);
331 const std::size_t exported_symbols = exprt.NumberOfFunctions;
332 const std::size_t fixed_names_addr = get_file_offset(exprt.AddressOfNames, h);
333 const std::size_t fixed_ordinals_addr = get_file_offset(exprt.AddressOfNameOrdinals, h);
334 const std::size_t fixed_functions_addr = get_file_offset(exprt.AddressOfFunctions, h);
336 ret.reserve(exported_symbols);
337 boost::dll::detail::DWORD_ ptr;
338 boost::dll::detail::WORD_ ordinal;
339 std::string symbol_name;
340 for (std::size_t i = 0;i < exported_symbols;++i) {
342 f_.seekg(fixed_ordinals_addr + i * sizeof(ordinal));
345 // getting function addr
346 f_.seekg(fixed_functions_addr + ordinal * sizeof(ptr));
348 ptr = static_cast<boost::dll::detail::DWORD_>( get_file_offset(ptr, h) );
350 if (ptr >= section_end_addr || ptr < section_begin_addr) {
354 f_.seekg(fixed_names_addr + i * sizeof(ptr));
356 f_.seekg(get_file_offset(ptr, h));
357 getline(f_, symbol_name, '\0');
358 ret.push_back(symbol_name);
364 // a test method to get dependents modules,
365 // who my plugin imports (1st level only)
367 e.g. for myself I get:
370 boost_system-vc-mt-gd-1_56.dll
374 std::vector<std::string> depend_of(boost::system::error_code &ec) BOOST_NOEXCEPT {
375 std::vector<std::string> ret;
377 IMAGE_DOS_HEADER* image_dos_header = (IMAGE_DOS_HEADER*)native();
378 if(!image_dos_header) {
379 // ERROR_BAD_EXE_FORMAT
380 ec = boost::system::error_code(
381 boost::system::errc::executable_format_error,
382 boost::system::generic_category()
388 IMAGE_OPTIONAL_HEADER* image_optional_header = (IMAGE_OPTIONAL_HEADER*)((boost::dll::detail::BYTE_*)native() + image_dos_header->e_lfanew + 24);
389 if(!image_optional_header) {
390 // ERROR_BAD_EXE_FORMAT
391 ec = boost::system::error_code(
392 boost::system::errc::executable_format_error,
393 boost::system::generic_category()
399 IMAGE_IMPORT_DESCRIPTOR* image_import_descriptor = (IMAGE_IMPORT_DESCRIPTOR*)((boost::dll::detail::BYTE_*)native() + image_optional_header->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
400 if(!image_import_descriptor) {
401 // ERROR_BAD_EXE_FORMAT
402 ec = boost::system::error_code(
403 boost::system::errc::executable_format_error,
404 boost::system::generic_category()
410 while(image_import_descriptor->FirstThunk) {
411 std::string module_name = reinterpret_cast<char*>((boost::dll::detail::BYTE_*)native() + image_import_descriptor->Name);
413 if(module_name.size()) {
414 ret.push_back(module_name);
417 image_import_descriptor++;
425 typedef pe_info<boost::dll::detail::DWORD_> pe_info32;
426 typedef pe_info<boost::dll::detail::ULONGLONG_> pe_info64;
428 }}} // namespace boost::dll::detail
430 #endif // BOOST_DLL_DETAIL_WINDOWS_PE_INFO_HPP