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