| 1 |
#include "binary/pe.hpp" |
| 2 |
|
| 3 |
#include "platform/platform.hpp" |
| 4 |
#include "utils/error.hpp" |
| 5 |
#include "utils/utils.hpp" |
| 6 |
|
| 7 |
#if IS_WINDOWS |
| 8 |
#include <array> |
| 9 |
#include <cstdio> |
| 10 |
#include <cstring> |
| 11 |
#include <string> |
| 12 |
|
| 13 |
#ifndef WIN32_LEAN_AND_MEAN |
| 14 |
#define WIN32_LEAN_AND_MEAN |
| 15 |
#endif |
| 16 |
#include <windows.h> |
| 17 |
|
| 18 |
CPPTRACE_BEGIN_NAMESPACE |
| 19 |
namespace detail { |
| 20 |
template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0> |
| 21 |
T pe_byteswap_if_needed(T value) { |
| 22 |
// PE header values are little endian, I think dos e_lfanew should be too |
| 23 |
if(!is_little_endian()) { |
| 24 |
return byteswap(value); |
| 25 |
} else { |
| 26 |
return value; |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
Result<std::uintptr_t, internal_error> pe_get_module_image_base(cstring_view object_path) { |
| 31 |
// https://drive.google.com/file/d/0B3_wGJkuWLytbnIxY1J5WUs4MEk/view?pli=1&resourcekey=0-n5zZ2UW39xVTH8ZSu6C2aQ |
| 32 |
// https://0xrick.github.io/win-internals/pe3/ |
| 33 |
// Endianness should always be little for dos and pe headers |
| 34 |
std::FILE* file_ptr; |
| 35 |
errno_t ret = fopen_s(&file_ptr, object_path.c_str(), "rb"); |
| 36 |
auto file = raii_wrap(std::move(file_ptr), file_deleter); |
| 37 |
if(ret != 0 || file == nullptr) { |
| 38 |
return internal_error("Unable to read object file {}", object_path); |
| 39 |
} |
| 40 |
auto magic = load_bytes<std::array<char, 2>>(file, 0); |
| 41 |
if(!magic) { |
| 42 |
return std::move(magic).unwrap_error(); |
| 43 |
} |
| 44 |
if(std::memcmp(magic.unwrap_value().data(), "MZ", 2) != 0) { |
| 45 |
return internal_error("File is not a PE file {}", object_path); |
| 46 |
} |
| 47 |
auto e_lfanew = load_bytes<DWORD>(file, 0x3c); // dos header + 0x3c |
| 48 |
if(!e_lfanew) { |
| 49 |
return std::move(e_lfanew).unwrap_error(); |
| 50 |
} |
| 51 |
DWORD nt_header_offset = pe_byteswap_if_needed(e_lfanew.unwrap_value()); |
| 52 |
auto signature = load_bytes<std::array<char, 4>>(file, nt_header_offset); // nt header + 0 |
| 53 |
if(!signature) { |
| 54 |
return std::move(signature).unwrap_error(); |
| 55 |
} |
| 56 |
if(std::memcmp(signature.unwrap_value().data(), "PE\0\0", 4) != 0) { |
| 57 |
return internal_error("File is not a PE file {}", object_path); |
| 58 |
} |
| 59 |
auto size_of_optional_header_raw = load_bytes<WORD>(file, nt_header_offset + 4 + 0x10); // file header + 0x10 |
| 60 |
if(!size_of_optional_header_raw) { |
| 61 |
return std::move(size_of_optional_header_raw).unwrap_error(); |
| 62 |
} |
| 63 |
WORD size_of_optional_header = pe_byteswap_if_needed(size_of_optional_header_raw.unwrap_value()); |
| 64 |
if(size_of_optional_header == 0) { |
| 65 |
return internal_error("Unexpected optional header size for PE file"); |
| 66 |
} |
| 67 |
auto optional_header_magic_raw = load_bytes<WORD>(file, nt_header_offset + 0x18); // optional header + 0x0 |
| 68 |
if(!optional_header_magic_raw) { |
| 69 |
return std::move(optional_header_magic_raw).unwrap_error(); |
| 70 |
} |
| 71 |
WORD optional_header_magic = pe_byteswap_if_needed(optional_header_magic_raw.unwrap_value()); |
| 72 |
VERIFY( |
| 73 |
optional_header_magic == IMAGE_NT_OPTIONAL_HDR_MAGIC, |
| 74 |
("PE file does not match expected bit-mode " + std::string(object_path)).c_str() |
| 75 |
); |
| 76 |
// finally get image base |
| 77 |
if(optional_header_magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) { |
| 78 |
// 32 bit |
| 79 |
auto bytes = load_bytes<DWORD>(file, nt_header_offset + 0x18 + 0x1c); // optional header + 0x1c |
| 80 |
if(!bytes) { |
| 81 |
return std::move(bytes).unwrap_error(); |
| 82 |
} |
| 83 |
return to<std::uintptr_t>(pe_byteswap_if_needed(bytes.unwrap_value())); |
| 84 |
} else { |
| 85 |
// 64 bit |
| 86 |
// I get an "error: 'QWORD' was not declared in this scope" for some reason when using QWORD |
| 87 |
auto bytes = load_bytes<std::uint64_t>(file, nt_header_offset + 0x18 + 0x18); // optional header + 0x18 |
| 88 |
if(!bytes) { |
| 89 |
return std::move(bytes).unwrap_error(); |
| 90 |
} |
| 91 |
return to<std::uintptr_t>(pe_byteswap_if_needed(bytes.unwrap_value())); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
CPPTRACE_END_NAMESPACE |
| 96 |
|
| 97 |
#endif |