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

source code of boost/libs/dll/include/boost/dll/detail/pe_info.hpp