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