diff options
Diffstat (limited to 'coff.cpp')
-rw-r--r-- | coff.cpp | 59 |
1 files changed, 9 insertions, 50 deletions
@@ -1,5 +1,7 @@ #include "coff.h" +#include "file.h" + #include <boost/algorithm/string/predicate.hpp> #include <boost/endian/conversion.hpp> @@ -45,17 +47,6 @@ struct COFFHeader uint16_t Characteristics{}; }; -// COFFHeader.Machine: -const uint16_t IMAGE_FILE_MACHINE_UNKNOWN = 0; -const uint16_t IMAGE_FILE_MACHINE_AMD64 = 0x8664; - -// COFFHeader.Characteristics: -const uint16_t IMAGE_FILE_EXECUTABLE_IMAGE = 0x002; -const uint16_t IMAGE_FILE_LARGE_ADDRESS_AWARE = 0x020; - -// COFFOptionalHeader_Windows.SubSystem -const uint16_t IMAGE_SUBSYSTEM_WINDOWS_CUI = 3; - struct COFFOptionalHeader { uint16_t Magic{}; @@ -69,17 +60,6 @@ struct COFFOptionalHeader uint32_t BaseOfData{}; }; -// COFFOptionalHeader.Magic -const uint16_t MAGIC_PE32 = 0x010b; -const uint16_t MAGIC_PE32p = 0x020b; - -// SectionHeader.Characteristics -const uint32_t IMAGE_SCN_CNT_CODE = 0x00000020; -const uint32_t IMAGE_SCN_CNT_INITIALIZED_DATA = 0x00000040; -const uint32_t IMAGE_SCN_MEM_EXECUTE = 0x20000000; -const uint32_t IMAGE_SCN_MEM_READ = 0x40000000; -const uint32_t IMAGE_SCN_MEM_WRITE = 0x80000000; - struct COFFOptionalHeader_PE32p { uint16_t Magic{}; @@ -375,7 +355,7 @@ struct SecondLinkerMember { const COFFHeader& coffHeader{ *(reinterpret_cast<const COFFHeader*>(data.data() + offset)) }; std::cout << "Machine: " << to_0xhex(coffHeader.Machine) << "\n"; - if (coffHeader.Machine != IMAGE_FILE_MACHINE_AMD64) + if (coffHeader.Machine != COFF::IMAGE_FILE_MACHINE_AMD64) std::cout << " Warning: Unsupported.\n"; std::cout << "NumberOfSections: " << coffHeader.NumberOfSections << "\n"; @@ -406,7 +386,7 @@ struct SecondLinkerMember { const COFFHeader& coffHeader{ *(reinterpret_cast<const COFFHeader*>(data.data())) }; std::cout << "Machine: " << to_0xhex(coffHeader.Machine) << "\n"; - if (coffHeader.Machine != IMAGE_FILE_MACHINE_AMD64) { + if (coffHeader.Machine != COFF::IMAGE_FILE_MACHINE_AMD64) { std::cout << " Warning: Unsupported.\n"; return; } @@ -529,27 +509,6 @@ void COFF::Dump(fs::path path) namespace { - void setFile(const fs::path& filename, const char* data, size_t size) - { - std::ofstream file(filename.string(), std::ios::out | std::ios::binary | std::ios::trunc); - if (file.is_open()) { - file.write(data, size); - } - else { - throw std::runtime_error("Opening "s + filename.string() + " for writing"); - } - } - - void setFile(const fs::path& filename, const std::string& s) - { - setFile(filename, s.data(), s.size()); - } - - void setFile(const fs::path& filename, const std::vector<uint8_t>& s) - { - setFile(filename, reinterpret_cast<const char*>(s.data()), s.size()); - } - void PutDOSStub(std::vector<uint8_t>& data) { std::vector<uint8_t> x{ 'M', 'Z' }; @@ -573,7 +532,7 @@ namespace { header.Machine = 0x8664; // AMD64 header.NumberOfSections = 2; header.SizeOfOptionalHeader = sizeof(COFFOptionalHeader_PE32p) + sizeof(COFFOptionalHeader_Windows_PE32p) + 8 * 16; // 0xf0 - header.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_LARGE_ADDRESS_AWARE; + header.Characteristics = COFF::IMAGE_FILE_EXECUTABLE_IMAGE | COFF::IMAGE_FILE_LARGE_ADDRESS_AWARE; data.insert(data.end(), header_v.begin(), header_v.end()); } @@ -603,7 +562,7 @@ namespace { optional_windows.SizeOfImage = 0x3000; optional_windows.SizeOfHeaders = 512; optional_windows.CheckSum = 0; - optional_windows.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI; + optional_windows.Subsystem = COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI; #if 0 optional_windows.DllCharacteristics = 0x8160; #endif @@ -631,7 +590,7 @@ namespace { section_header.VirtualAddress = 0x1000; section_header.SizeOfRawData = 512; // multiple of optional_windows.FileAlignment section_header.PointerToRawData = 512; - section_header.Characteristics = IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ; + section_header.Characteristics = COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE | COFF::IMAGE_SCN_MEM_READ; data.insert(data.end(), section_header_v.begin(), section_header_v.end()); } @@ -663,7 +622,7 @@ namespace { section_header.VirtualAddress = 0x2000; section_header.SizeOfRawData = 512; // multiple of optional_windows.FileAlignment section_header.PointerToRawData = 1024; - section_header.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ; + section_header.Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ; data.insert(data.end(), section_header_v.begin(), section_header_v.end()); } @@ -689,5 +648,5 @@ void COFF::Create(std::filesystem::path path) PutCOFFSectionCode(data); PutCOFFSectionData(data); - setFile(path, data); + File::setFile(path, data); } |