diff options
| -rw-r--r-- | coff.cpp | 59 | ||||
| -rw-r--r-- | coff.h | 24 | ||||
| -rw-r--r-- | flowgraph/data.h | 2 | ||||
| -rw-r--r-- | flowgraph/node.h | 57 | 
4 files changed, 62 insertions, 80 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);  } @@ -3,6 +3,28 @@  #include <filesystem>  namespace COFF { + // 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; + + // 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; +   void Dump(std::filesystem::path path);   void Create(std::filesystem::path path); -}
\ No newline at end of file +} diff --git a/flowgraph/data.h b/flowgraph/data.h index abf046d..d1c2588 100644 --- a/flowgraph/data.h +++ b/flowgraph/data.h @@ -39,6 +39,7 @@ namespace FlowGraph {  namespace GlobalData { +#if 0   // variable of simple or struct type   class Element   { @@ -50,5 +51,6 @@ namespace GlobalData {   class Segment: public std::vector<Element>   {   }; +#endif  } diff --git a/flowgraph/node.h b/flowgraph/node.h index 853b017..d4a1306 100644 --- a/flowgraph/node.h +++ b/flowgraph/node.h @@ -29,18 +29,17 @@ namespace FlowGraph {   class AllocateDynamic: public Node   {   public: -  AllocateDynamic(Data& location, Data& size): m_location(location), m_size(size) {} - private: -  Data m_location; // in/out: Pointer -  Data m_size; // in: Size +  AllocateDynamic(Data& location, Data& size): +   Node(std::vector<Data>({location, size})) // in/out: Pointer; in: Size +  {}   };   class DeallocateDynamic: public Node   {   public: -  DeallocateDynamic(Data& location) : m_location(location) {} // in - private: -  Data m_location; // in: Pointer +  DeallocateDynamic(Data& location): +   Node(std::vector<Data>({location})) // in: Pointer +  {}   };   Data MakeConstantInt(int i); @@ -51,20 +50,17 @@ namespace FlowGraph {   class MemCopy: public Node   {   public: -  MemCopy(Data& destination, Data& source, Data& size): m_destination(destination), m_source(source), m_size(size) {} - private: -  Data m_destination; // Pointer -  Data m_source;      // Pointer -  Data m_size;        // in bytes +  MemCopy(Data& destination, Data& source, Data& size): // Pointer, Pointer, size in bytes +   Node(std::vector<Data>({destination, source, size})) +  {}   };   class Move: public Node   {   public: -  Move(Data& destination, Data& source): m_destination(destination), m_source(source) {} - private: -  Data m_destination; -  Data m_source; +  Move(Data& destination, Data& source): +   Node(std::vector<Data>({destination, source})) +  {}   };   enum class JumpVariant @@ -83,15 +79,13 @@ namespace FlowGraph {   {   public:    Jump(JumpVariant variant, Data& source0, Data& source1, std::shared_ptr<Node> destination): +   Node(std::vector<Data>({source0, source1})),     m_variant(variant), -   m_source0(source0), -   m_source1(source1),     m_destination(destination)    {} +  JumpVariant variant() { return m_variant; }   private:    JumpVariant m_variant; -  Data m_source0; -  Data m_source1;    std::shared_ptr<Node> m_destination; // successor on branch   }; @@ -99,19 +93,21 @@ namespace FlowGraph {   class Call: public Node   {   public: -  Call(std::shared_ptr<Node> destination, std::vector<Data> arguments): m_destination(destination), m_arguments(arguments) {} +  Call(std::shared_ptr<Node> destination, std::vector<Data> arguments): +   Node(arguments), +   m_destination(destination) +  {}   private:    std::shared_ptr<Node> m_destination; -  std::vector<Data> m_arguments;   };   // Return from Subroutine   class Return: public Node   {   public: -  Return(std::vector<Data> returnValues): m_returnValues(returnValues) {} - private: -  std::vector<Data> m_returnValues; +  Return(std::vector<Data> returnValues): +   Node(returnValues) +  {}   };   enum class UnaryOperationType { @@ -123,11 +119,13 @@ namespace FlowGraph {   class UnaryOperation: public Node   {   public: -  UnaryOperation(UnaryOperationType type, Data& destination, Data& source): m_type(type), m_destination(destination), m_source(source) {} +  UnaryOperation(UnaryOperationType type, Data& destination, Data& source): +   Node(std::vector<Data>({destination, source})), +   m_type(type) +   {} +  UnaryOperationType type() { return m_type; }   private:    UnaryOperationType m_type; -  Data m_destination; -  Data m_source;   };   enum class BinaryOperationType { @@ -151,7 +149,8 @@ namespace FlowGraph {   {   public:    BinaryOperation(BinaryOperationType type, Data& destination, Data& source0, Data& source1): -   Node(std::vector<Data>({destination, source0, source1})), m_type(type) +   Node(std::vector<Data>({destination, source0, source1})), +   m_type(type)   {}    BinaryOperationType type() {return m_type;}   private: | 
